Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-src by
targets/hal/TARGET_NORDIC/TARGET_NRF51822/us_ticker.c@104:a6a92e2e5a92, 2014-02-21 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Feb 21 12:15:10 2014 +0000
- Revision:
- 104:a6a92e2e5a92
- Parent:
- 85:e1a8e879a6a9
- Child:
- 127:ce7cebc0511f
Synchronized with git revision 195a50befc6da080be0051d0d9bc0838f6cb873e
Full URL: https://github.com/mbedmicro/mbed/commit/195a50befc6da080be0051d0d9bc0838f6cb873e/
nrf-mbed merge
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 85:e1a8e879a6a9 | 1 | /* mbed Microcontroller Library |
mbed_official | 104:a6a92e2e5a92 | 2 | * Copyright (c) 2013 Nordic Semiconductor |
mbed_official | 85:e1a8e879a6a9 | 3 | * |
mbed_official | 85:e1a8e879a6a9 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 85:e1a8e879a6a9 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 85:e1a8e879a6a9 | 6 | * You may obtain a copy of the License at |
mbed_official | 85:e1a8e879a6a9 | 7 | * |
mbed_official | 85:e1a8e879a6a9 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 85:e1a8e879a6a9 | 9 | * |
mbed_official | 85:e1a8e879a6a9 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 85:e1a8e879a6a9 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 85:e1a8e879a6a9 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 85:e1a8e879a6a9 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 85:e1a8e879a6a9 | 14 | * limitations under the License. |
mbed_official | 85:e1a8e879a6a9 | 15 | */ |
mbed_official | 85:e1a8e879a6a9 | 16 | #include <stddef.h> |
mbed_official | 85:e1a8e879a6a9 | 17 | #include "us_ticker_api.h" |
mbed_official | 85:e1a8e879a6a9 | 18 | #include "cmsis.h" |
mbed_official | 85:e1a8e879a6a9 | 19 | #include "PeripheralNames.h" |
mbed_official | 85:e1a8e879a6a9 | 20 | |
mbed_official | 85:e1a8e879a6a9 | 21 | #define US_TICKER_TIMER NRF_TIMER1 |
mbed_official | 85:e1a8e879a6a9 | 22 | #define US_TICKER_TIMER_IRQn TIMER1_IRQn |
mbed_official | 85:e1a8e879a6a9 | 23 | |
mbed_official | 85:e1a8e879a6a9 | 24 | int us_ticker_inited = 0; |
mbed_official | 85:e1a8e879a6a9 | 25 | volatile uint16_t overflow=0; //overflow value that forms the upper 16 bits of the counter |
mbed_official | 85:e1a8e879a6a9 | 26 | volatile uint16_t timeStamp=0; |
mbed_official | 85:e1a8e879a6a9 | 27 | |
mbed_official | 85:e1a8e879a6a9 | 28 | #ifdef __cplusplus |
mbed_official | 85:e1a8e879a6a9 | 29 | extern "C" { |
mbed_official | 85:e1a8e879a6a9 | 30 | #endif |
mbed_official | 85:e1a8e879a6a9 | 31 | void TIMER1_IRQHandler(void){ |
mbed_official | 85:e1a8e879a6a9 | 32 | if ((US_TICKER_TIMER->EVENTS_COMPARE[1] != 0) && |
mbed_official | 85:e1a8e879a6a9 | 33 | ((US_TICKER_TIMER->INTENSET & TIMER_INTENSET_COMPARE1_Msk) != 0)) |
mbed_official | 85:e1a8e879a6a9 | 34 | { |
mbed_official | 85:e1a8e879a6a9 | 35 | US_TICKER_TIMER->EVENTS_COMPARE[1] = 0; |
mbed_official | 85:e1a8e879a6a9 | 36 | overflow++; |
mbed_official | 85:e1a8e879a6a9 | 37 | US_TICKER_TIMER->CC[1] =0xFFFF; |
mbed_official | 85:e1a8e879a6a9 | 38 | if(timeStamp>0) |
mbed_official | 85:e1a8e879a6a9 | 39 | { |
mbed_official | 85:e1a8e879a6a9 | 40 | timeStamp--; |
mbed_official | 85:e1a8e879a6a9 | 41 | if(timeStamp==0) |
mbed_official | 85:e1a8e879a6a9 | 42 | { |
mbed_official | 85:e1a8e879a6a9 | 43 | us_ticker_clear_interrupt(); |
mbed_official | 85:e1a8e879a6a9 | 44 | us_ticker_disable_interrupt(); |
mbed_official | 85:e1a8e879a6a9 | 45 | us_ticker_irq_handler(); |
mbed_official | 85:e1a8e879a6a9 | 46 | return; |
mbed_official | 85:e1a8e879a6a9 | 47 | } |
mbed_official | 85:e1a8e879a6a9 | 48 | } |
mbed_official | 85:e1a8e879a6a9 | 49 | } |
mbed_official | 85:e1a8e879a6a9 | 50 | if ((US_TICKER_TIMER->EVENTS_COMPARE[0] != 0) && |
mbed_official | 85:e1a8e879a6a9 | 51 | ((US_TICKER_TIMER->INTENSET & TIMER_INTENSET_COMPARE0_Msk) != 0)) |
mbed_official | 85:e1a8e879a6a9 | 52 | { |
mbed_official | 85:e1a8e879a6a9 | 53 | us_ticker_clear_interrupt(); |
mbed_official | 85:e1a8e879a6a9 | 54 | us_ticker_disable_interrupt(); |
mbed_official | 85:e1a8e879a6a9 | 55 | if(timeStamp==0) |
mbed_official | 85:e1a8e879a6a9 | 56 | us_ticker_irq_handler(); |
mbed_official | 85:e1a8e879a6a9 | 57 | } |
mbed_official | 85:e1a8e879a6a9 | 58 | |
mbed_official | 85:e1a8e879a6a9 | 59 | } |
mbed_official | 85:e1a8e879a6a9 | 60 | #ifdef __cplusplus |
mbed_official | 85:e1a8e879a6a9 | 61 | } |
mbed_official | 85:e1a8e879a6a9 | 62 | #endif |
mbed_official | 85:e1a8e879a6a9 | 63 | void us_ticker_init(void){ |
mbed_official | 85:e1a8e879a6a9 | 64 | if (us_ticker_inited){ |
mbed_official | 85:e1a8e879a6a9 | 65 | return; |
mbed_official | 85:e1a8e879a6a9 | 66 | } |
mbed_official | 85:e1a8e879a6a9 | 67 | |
mbed_official | 85:e1a8e879a6a9 | 68 | us_ticker_inited = 1; |
mbed_official | 85:e1a8e879a6a9 | 69 | |
mbed_official | 85:e1a8e879a6a9 | 70 | US_TICKER_TIMER->POWER = 0; |
mbed_official | 85:e1a8e879a6a9 | 71 | US_TICKER_TIMER->POWER = 1; |
mbed_official | 85:e1a8e879a6a9 | 72 | |
mbed_official | 85:e1a8e879a6a9 | 73 | US_TICKER_TIMER->MODE = TIMER_MODE_MODE_Timer; |
mbed_official | 85:e1a8e879a6a9 | 74 | |
mbed_official | 85:e1a8e879a6a9 | 75 | US_TICKER_TIMER->PRESCALER = 4; |
mbed_official | 85:e1a8e879a6a9 | 76 | US_TICKER_TIMER->BITMODE = TIMER_BITMODE_BITMODE_16Bit; |
mbed_official | 85:e1a8e879a6a9 | 77 | US_TICKER_TIMER->TASKS_CLEAR =1; |
mbed_official | 85:e1a8e879a6a9 | 78 | US_TICKER_TIMER->CC[1] = 0xFFFF; |
mbed_official | 85:e1a8e879a6a9 | 79 | US_TICKER_TIMER->INTENSET = TIMER_INTENSET_COMPARE1_Set << TIMER_INTENSET_COMPARE1_Pos; |
mbed_official | 85:e1a8e879a6a9 | 80 | |
mbed_official | 85:e1a8e879a6a9 | 81 | NVIC_SetPriority(US_TICKER_TIMER_IRQn, 3); |
mbed_official | 85:e1a8e879a6a9 | 82 | NVIC_EnableIRQ(US_TICKER_TIMER_IRQn); |
mbed_official | 85:e1a8e879a6a9 | 83 | |
mbed_official | 85:e1a8e879a6a9 | 84 | US_TICKER_TIMER->TASKS_START = 0x01; |
mbed_official | 85:e1a8e879a6a9 | 85 | } |
mbed_official | 85:e1a8e879a6a9 | 86 | |
mbed_official | 85:e1a8e879a6a9 | 87 | uint32_t us_ticker_read(){ |
mbed_official | 85:e1a8e879a6a9 | 88 | if (!us_ticker_inited){ |
mbed_official | 85:e1a8e879a6a9 | 89 | us_ticker_init(); |
mbed_official | 85:e1a8e879a6a9 | 90 | } |
mbed_official | 85:e1a8e879a6a9 | 91 | |
mbed_official | 85:e1a8e879a6a9 | 92 | uint16_t bufferedOverFlow = overflow; |
mbed_official | 85:e1a8e879a6a9 | 93 | US_TICKER_TIMER->TASKS_CAPTURE[2] = 1; |
mbed_official | 85:e1a8e879a6a9 | 94 | |
mbed_official | 85:e1a8e879a6a9 | 95 | if(overflow!=bufferedOverFlow){ |
mbed_official | 85:e1a8e879a6a9 | 96 | bufferedOverFlow = overflow; |
mbed_official | 85:e1a8e879a6a9 | 97 | US_TICKER_TIMER->TASKS_CAPTURE[2] = 1; |
mbed_official | 85:e1a8e879a6a9 | 98 | } |
mbed_official | 85:e1a8e879a6a9 | 99 | return (((uint32_t)bufferedOverFlow<<16) | US_TICKER_TIMER->CC[2]); |
mbed_official | 85:e1a8e879a6a9 | 100 | } |
mbed_official | 85:e1a8e879a6a9 | 101 | |
mbed_official | 85:e1a8e879a6a9 | 102 | void us_ticker_set_interrupt(unsigned int timestamp){ |
mbed_official | 85:e1a8e879a6a9 | 103 | if (!us_ticker_inited) |
mbed_official | 85:e1a8e879a6a9 | 104 | { |
mbed_official | 85:e1a8e879a6a9 | 105 | us_ticker_init(); |
mbed_official | 85:e1a8e879a6a9 | 106 | } |
mbed_official | 85:e1a8e879a6a9 | 107 | |
mbed_official | 85:e1a8e879a6a9 | 108 | US_TICKER_TIMER->TASKS_CAPTURE[0] = 1; |
mbed_official | 85:e1a8e879a6a9 | 109 | uint16_t tsUpper16 = (uint16_t)((timestamp-us_ticker_read())>>16); |
mbed_official | 85:e1a8e879a6a9 | 110 | if(tsUpper16>0){ |
mbed_official | 85:e1a8e879a6a9 | 111 | if(timeStamp ==0 || timeStamp> tsUpper16){ |
mbed_official | 85:e1a8e879a6a9 | 112 | timeStamp = tsUpper16; |
mbed_official | 85:e1a8e879a6a9 | 113 | } |
mbed_official | 85:e1a8e879a6a9 | 114 | } |
mbed_official | 85:e1a8e879a6a9 | 115 | else{ |
mbed_official | 85:e1a8e879a6a9 | 116 | US_TICKER_TIMER->INTENSET |= TIMER_INTENSET_COMPARE0_Set << TIMER_INTENSET_COMPARE0_Pos; |
mbed_official | 85:e1a8e879a6a9 | 117 | US_TICKER_TIMER->CC[0] += timestamp-us_ticker_read(); |
mbed_official | 85:e1a8e879a6a9 | 118 | } |
mbed_official | 85:e1a8e879a6a9 | 119 | } |
mbed_official | 85:e1a8e879a6a9 | 120 | |
mbed_official | 85:e1a8e879a6a9 | 121 | void us_ticker_disable_interrupt(void){ |
mbed_official | 85:e1a8e879a6a9 | 122 | US_TICKER_TIMER->INTENCLR = TIMER_INTENCLR_COMPARE0_Clear << TIMER_INTENCLR_COMPARE0_Pos; |
mbed_official | 85:e1a8e879a6a9 | 123 | } |
mbed_official | 85:e1a8e879a6a9 | 124 | void us_ticker_clear_interrupt(void){ |
mbed_official | 85:e1a8e879a6a9 | 125 | US_TICKER_TIMER->EVENTS_COMPARE[0] = 0; |
mbed_official | 85:e1a8e879a6a9 | 126 | } |