mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
<>
Date:
Fri Oct 28 11:17:30 2016 +0100
Revision:
149:156823d33999
Child:
160:d5399cc887bb
This updates the lib to the mbed lib v128

NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1 /* mbed Microcontroller Library
<> 149:156823d33999 2 * Copyright (c) 2013 Nordic Semiconductor
<> 149:156823d33999 3 *
<> 149:156823d33999 4 * Licensed under the Apache License, Version 2.0 (the "License");
<> 149:156823d33999 5 * you may not use this file except in compliance with the License.
<> 149:156823d33999 6 * You may obtain a copy of the License at
<> 149:156823d33999 7 *
<> 149:156823d33999 8 * http://www.apache.org/licenses/LICENSE-2.0
<> 149:156823d33999 9 *
<> 149:156823d33999 10 * Unless required by applicable law or agreed to in writing, software
<> 149:156823d33999 11 * distributed under the License is distributed on an "AS IS" BASIS,
<> 149:156823d33999 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 149:156823d33999 13 * See the License for the specific language governing permissions and
<> 149:156823d33999 14 * limitations under the License.
<> 149:156823d33999 15 */
<> 149:156823d33999 16 #include <stddef.h>
<> 149:156823d33999 17 #include <stdbool.h>
<> 149:156823d33999 18 #include "us_ticker_api.h"
<> 149:156823d33999 19 #include "cmsis.h"
<> 149:156823d33999 20 #include "PeripheralNames.h"
<> 149:156823d33999 21 #include "nrf_delay.h"
<> 149:156823d33999 22 #include "toolchain.h"
<> 149:156823d33999 23
<> 149:156823d33999 24 /*
<> 149:156823d33999 25 * Note: The micro-second timer API on the nRF51 platform is implemented using
<> 149:156823d33999 26 * the RTC counter run at 32kHz (sourced from an external oscillator). This is
<> 149:156823d33999 27 * a trade-off between precision and power. Running a normal 32-bit MCU counter
<> 149:156823d33999 28 * at high frequency causes the average power consumption to rise to a few
<> 149:156823d33999 29 * hundred micro-amps, which is prohibitive for typical low-power BLE
<> 149:156823d33999 30 * applications.
<> 149:156823d33999 31 * A 32kHz clock doesn't offer the precision needed for keeping u-second time,
<> 149:156823d33999 32 * but we're assuming that this will not be a problem for the average user.
<> 149:156823d33999 33 */
<> 149:156823d33999 34
<> 149:156823d33999 35 #define MAX_RTC_COUNTER_VAL 0x00FFFFFF /**< Maximum value of the RTC counter. */
<> 149:156823d33999 36 #define RTC_CLOCK_FREQ (uint32_t)(32768)
<> 149:156823d33999 37 #define RTC1_IRQ_PRI 3 /**< Priority of the RTC1 interrupt (used
<> 149:156823d33999 38 * for checking for timeouts and executing
<> 149:156823d33999 39 * timeout handlers). This must be the same
<> 149:156823d33999 40 * as APP_IRQ_PRIORITY_LOW; taken from the
<> 149:156823d33999 41 * Nordic SDK. */
<> 149:156823d33999 42 #define MAX_RTC_TASKS_DELAY 47 /**< Maximum delay until an RTC task is executed. */
<> 149:156823d33999 43
<> 149:156823d33999 44 #define FUZZY_RTC_TICKS 2 /* RTC COMPARE occurs when a CC register is N and the RTC
<> 149:156823d33999 45 * COUNTER value transitions from N-1 to N. If we're trying to
<> 149:156823d33999 46 * setup a callback for a time which will arrive very shortly,
<> 149:156823d33999 47 * there are limits to how short the callback interval may be for us
<> 149:156823d33999 48 * to rely upon the RTC Compare trigger. If the COUNTER is N,
<> 149:156823d33999 49 * writing N+2 to a CC register is guaranteed to trigger a COMPARE
<> 149:156823d33999 50 * event at N+2. */
<> 149:156823d33999 51
<> 149:156823d33999 52 #define RTC_UNITS_TO_MICROSECONDS(RTC_UNITS) (((RTC_UNITS) * (uint64_t)1000000) / RTC_CLOCK_FREQ)
<> 149:156823d33999 53 #define MICROSECONDS_TO_RTC_UNITS(MICROS) ((((uint64_t)(MICROS) * RTC_CLOCK_FREQ) + 999999) / 1000000)
<> 149:156823d33999 54
<> 149:156823d33999 55 static bool us_ticker_inited = false;
<> 149:156823d33999 56 static volatile uint32_t overflowCount; /**< The number of times the 24-bit RTC counter has overflowed. */
<> 149:156823d33999 57 static volatile bool us_ticker_callbackPending = false;
<> 149:156823d33999 58 static uint32_t us_ticker_callbackTimestamp;
<> 149:156823d33999 59 static bool os_tick_started = false; /**< flag indicating if the os_tick has started */
<> 149:156823d33999 60 /**
<> 149:156823d33999 61 * The value previously set in the capture compare register of channel 1
<> 149:156823d33999 62 */
<> 149:156823d33999 63 static uint32_t previous_tick_cc_value = 0;
<> 149:156823d33999 64
<> 149:156823d33999 65 /*
<> 149:156823d33999 66 RTX provide the following definitions which are used by the tick code:
<> 149:156823d33999 67 * os_trv: The number (minus 1) of clock cycle between two tick.
<> 149:156823d33999 68 * os_clockrate: Time duration between two ticks (in us).
<> 149:156823d33999 69 * OS_Tick_Handler: The function which handle a tick event.
<> 149:156823d33999 70 This function is special because it never returns.
<> 149:156823d33999 71 Those definitions are used by the code which handle the os tick.
<> 149:156823d33999 72 To allow compilation of us_ticker programs without RTOS, those symbols are
<> 149:156823d33999 73 exported from this module as weak ones.
<> 149:156823d33999 74 */
<> 149:156823d33999 75 MBED_WEAK uint32_t const os_trv;
<> 149:156823d33999 76 MBED_WEAK uint32_t const os_clockrate;
<> 149:156823d33999 77 MBED_WEAK void OS_Tick_Handler() { }
<> 149:156823d33999 78
<> 149:156823d33999 79 static inline void rtc1_enableCompareInterrupt(void)
<> 149:156823d33999 80 {
<> 149:156823d33999 81 NRF_RTC1->EVTENCLR = RTC_EVTEN_COMPARE0_Msk;
<> 149:156823d33999 82 NRF_RTC1->INTENSET = RTC_INTENSET_COMPARE0_Msk;
<> 149:156823d33999 83 }
<> 149:156823d33999 84
<> 149:156823d33999 85 static inline void rtc1_disableCompareInterrupt(void)
<> 149:156823d33999 86 {
<> 149:156823d33999 87 NRF_RTC1->INTENCLR = RTC_INTENSET_COMPARE0_Msk;
<> 149:156823d33999 88 NRF_RTC1->EVTENCLR = RTC_EVTEN_COMPARE0_Msk;
<> 149:156823d33999 89 }
<> 149:156823d33999 90
<> 149:156823d33999 91 static inline void rtc1_enableOverflowInterrupt(void)
<> 149:156823d33999 92 {
<> 149:156823d33999 93 NRF_RTC1->EVTENCLR = RTC_EVTEN_OVRFLW_Msk;
<> 149:156823d33999 94 NRF_RTC1->INTENSET = RTC_INTENSET_OVRFLW_Msk;
<> 149:156823d33999 95 }
<> 149:156823d33999 96
<> 149:156823d33999 97 static inline void rtc1_disableOverflowInterrupt(void)
<> 149:156823d33999 98 {
<> 149:156823d33999 99 NRF_RTC1->INTENCLR = RTC_INTENSET_OVRFLW_Msk;
<> 149:156823d33999 100 NRF_RTC1->EVTENCLR = RTC_EVTEN_OVRFLW_Msk;
<> 149:156823d33999 101 }
<> 149:156823d33999 102
<> 149:156823d33999 103 static inline void invokeCallback(void)
<> 149:156823d33999 104 {
<> 149:156823d33999 105 us_ticker_callbackPending = false;
<> 149:156823d33999 106 rtc1_disableCompareInterrupt();
<> 149:156823d33999 107 us_ticker_irq_handler();
<> 149:156823d33999 108 }
<> 149:156823d33999 109
<> 149:156823d33999 110 /**
<> 149:156823d33999 111 * @brief Function for starting the RTC1 timer. The RTC timer is expected to
<> 149:156823d33999 112 * keep running--some interrupts may be disabled temporarily.
<> 149:156823d33999 113 */
<> 149:156823d33999 114 static void rtc1_start()
<> 149:156823d33999 115 {
<> 149:156823d33999 116 NRF_RTC1->PRESCALER = 0; /* for no pre-scaling. */
<> 149:156823d33999 117
<> 149:156823d33999 118 rtc1_enableOverflowInterrupt();
<> 149:156823d33999 119
<> 149:156823d33999 120 NVIC_SetPriority(RTC1_IRQn, RTC1_IRQ_PRI);
<> 149:156823d33999 121 NVIC_ClearPendingIRQ(RTC1_IRQn);
<> 149:156823d33999 122 NVIC_EnableIRQ(RTC1_IRQn);
<> 149:156823d33999 123
<> 149:156823d33999 124 NRF_RTC1->TASKS_START = 1;
<> 149:156823d33999 125 nrf_delay_us(MAX_RTC_TASKS_DELAY);
<> 149:156823d33999 126 }
<> 149:156823d33999 127
<> 149:156823d33999 128 /**
<> 149:156823d33999 129 * @brief Function for stopping the RTC1 timer. We don't expect to call this.
<> 149:156823d33999 130 */
<> 149:156823d33999 131 void rtc1_stop(void)
<> 149:156823d33999 132 {
<> 149:156823d33999 133 // If the os tick has been started, RTC1 shouldn't be stopped
<> 149:156823d33999 134 // In that case, us ticker and overflow interrupt are disabled.
<> 149:156823d33999 135 if (os_tick_started) {
<> 149:156823d33999 136 rtc1_disableCompareInterrupt();
<> 149:156823d33999 137 rtc1_disableOverflowInterrupt();
<> 149:156823d33999 138 } else {
<> 149:156823d33999 139 NVIC_DisableIRQ(RTC1_IRQn);
<> 149:156823d33999 140 rtc1_disableCompareInterrupt();
<> 149:156823d33999 141 rtc1_disableOverflowInterrupt();
<> 149:156823d33999 142
<> 149:156823d33999 143 NRF_RTC1->TASKS_STOP = 1;
<> 149:156823d33999 144 nrf_delay_us(MAX_RTC_TASKS_DELAY);
<> 149:156823d33999 145
<> 149:156823d33999 146 NRF_RTC1->TASKS_CLEAR = 1;
<> 149:156823d33999 147 nrf_delay_us(MAX_RTC_TASKS_DELAY);
<> 149:156823d33999 148 }
<> 149:156823d33999 149 }
<> 149:156823d33999 150
<> 149:156823d33999 151 /**
<> 149:156823d33999 152 * @brief Function for returning the current value of the RTC1 counter.
<> 149:156823d33999 153 *
<> 149:156823d33999 154 * @return Current RTC1 counter as a 64-bit value with 56-bit precision (even
<> 149:156823d33999 155 * though the underlying counter is 24-bit)
<> 149:156823d33999 156 */
<> 149:156823d33999 157 static inline uint64_t rtc1_getCounter64(void)
<> 149:156823d33999 158 {
<> 149:156823d33999 159 if (NRF_RTC1->EVENTS_OVRFLW) {
<> 149:156823d33999 160 overflowCount++;
<> 149:156823d33999 161 NRF_RTC1->EVENTS_OVRFLW = 0;
<> 149:156823d33999 162 NRF_RTC1->EVTENCLR = RTC_EVTEN_OVRFLW_Msk;
<> 149:156823d33999 163 }
<> 149:156823d33999 164 return ((uint64_t)overflowCount << 24) | NRF_RTC1->COUNTER;
<> 149:156823d33999 165 }
<> 149:156823d33999 166
<> 149:156823d33999 167 /**
<> 149:156823d33999 168 * @brief Function for returning the current value of the RTC1 counter.
<> 149:156823d33999 169 *
<> 149:156823d33999 170 * @return Current RTC1 counter as a 32-bit value (even though the underlying counter is 24-bit)
<> 149:156823d33999 171 */
<> 149:156823d33999 172 static inline uint32_t rtc1_getCounter(void)
<> 149:156823d33999 173 {
<> 149:156823d33999 174 return rtc1_getCounter64();
<> 149:156823d33999 175 }
<> 149:156823d33999 176
<> 149:156823d33999 177 /**
<> 149:156823d33999 178 * @brief Function for handling the RTC1 interrupt for us ticker (capture compare channel 0 and overflow).
<> 149:156823d33999 179 *
<> 149:156823d33999 180 * @details Checks for timeouts, and executes timeout handlers for expired timers.
<> 149:156823d33999 181 */
<> 149:156823d33999 182 void us_ticker_handler(void)
<> 149:156823d33999 183 {
<> 149:156823d33999 184 if (NRF_RTC1->EVENTS_OVRFLW) {
<> 149:156823d33999 185 overflowCount++;
<> 149:156823d33999 186 NRF_RTC1->EVENTS_OVRFLW = 0;
<> 149:156823d33999 187 NRF_RTC1->EVTENCLR = RTC_EVTEN_OVRFLW_Msk;
<> 149:156823d33999 188 }
<> 149:156823d33999 189 if (NRF_RTC1->EVENTS_COMPARE[0]) {
<> 149:156823d33999 190 NRF_RTC1->EVENTS_COMPARE[0] = 0;
<> 149:156823d33999 191 NRF_RTC1->EVTENCLR = RTC_EVTEN_COMPARE0_Msk;
<> 149:156823d33999 192 if (us_ticker_callbackPending && ((int)(us_ticker_callbackTimestamp - rtc1_getCounter()) <= 0))
<> 149:156823d33999 193 invokeCallback();
<> 149:156823d33999 194 }
<> 149:156823d33999 195 }
<> 149:156823d33999 196
<> 149:156823d33999 197 void us_ticker_init(void)
<> 149:156823d33999 198 {
<> 149:156823d33999 199 if (us_ticker_inited) {
<> 149:156823d33999 200 return;
<> 149:156823d33999 201 }
<> 149:156823d33999 202
<> 149:156823d33999 203 rtc1_start();
<> 149:156823d33999 204 us_ticker_inited = true;
<> 149:156823d33999 205 }
<> 149:156823d33999 206
<> 149:156823d33999 207 uint32_t us_ticker_read()
<> 149:156823d33999 208 {
<> 149:156823d33999 209 if (!us_ticker_inited) {
<> 149:156823d33999 210 us_ticker_init();
<> 149:156823d33999 211 }
<> 149:156823d33999 212
<> 149:156823d33999 213 /* Return a pseudo microsecond counter value. This is only as precise as the
<> 149:156823d33999 214 * 32khz low-freq clock source, but could be adequate.*/
<> 149:156823d33999 215 return RTC_UNITS_TO_MICROSECONDS(rtc1_getCounter64());
<> 149:156823d33999 216 }
<> 149:156823d33999 217
<> 149:156823d33999 218 /**
<> 149:156823d33999 219 * Setup the us_ticker callback interrupt to go at the given timestamp.
<> 149:156823d33999 220 *
<> 149:156823d33999 221 * @Note: Only one callback is pending at any time.
<> 149:156823d33999 222 *
<> 149:156823d33999 223 * @Note: If a callback is pending, and this function is called again, the new
<> 149:156823d33999 224 * callback-time overrides the existing callback setting. It is the caller's
<> 149:156823d33999 225 * responsibility to ensure that this function is called to setup a callback for
<> 149:156823d33999 226 * the earliest timeout.
<> 149:156823d33999 227 *
<> 149:156823d33999 228 * @Note: If this function is used to setup an interrupt which is immediately
<> 149:156823d33999 229 * pending--such as for 'now' or a time in the past,--then the callback is
<> 149:156823d33999 230 * invoked a few ticks later.
<> 149:156823d33999 231 */
<> 149:156823d33999 232 void us_ticker_set_interrupt(timestamp_t timestamp)
<> 149:156823d33999 233 {
<> 149:156823d33999 234 if (!us_ticker_inited) {
<> 149:156823d33999 235 us_ticker_init();
<> 149:156823d33999 236 }
<> 149:156823d33999 237
<> 149:156823d33999 238 /*
<> 149:156823d33999 239 * The argument to this function is a 32-bit microsecond timestamp for when
<> 149:156823d33999 240 * a callback should be invoked. On the nRF51, we use an RTC timer running
<> 149:156823d33999 241 * at 32kHz to implement a low-power us-ticker. This results in a problem
<> 149:156823d33999 242 * based on the fact that 1000000 is not a multiple of 32768.
<> 149:156823d33999 243 *
<> 149:156823d33999 244 * Going from a micro-second based timestamp to a 32kHz based RTC-time is a
<> 149:156823d33999 245 * linear mapping; but this mapping doesn't preserve wraparounds--i.e. when
<> 149:156823d33999 246 * the 32-bit micro-second timestamp wraps around unfortunately the
<> 149:156823d33999 247 * underlying RTC counter doesn't. The result is that timestamp expiry
<> 149:156823d33999 248 * checks on micro-second timestamps don't yield the same result when
<> 149:156823d33999 249 * applied on the corresponding RTC timestamp values.
<> 149:156823d33999 250 *
<> 149:156823d33999 251 * One solution is to translate the incoming 32-bit timestamp into a virtual
<> 149:156823d33999 252 * 64-bit timestamp based on the knowledge of system-uptime, and then use
<> 149:156823d33999 253 * this wraparound-free 64-bit value to do a linear mapping to RTC time.
<> 149:156823d33999 254 * System uptime on an nRF is maintained using the 24-bit RTC counter. We
<> 149:156823d33999 255 * track the overflow count to extend the 24-bit hardware counter by an
<> 149:156823d33999 256 * additional 32 bits. RTC_UNITS_TO_MICROSECONDS() converts this into
<> 149:156823d33999 257 * microsecond units (in 64-bits).
<> 149:156823d33999 258 */
<> 149:156823d33999 259 const uint64_t currentTime64 = RTC_UNITS_TO_MICROSECONDS(rtc1_getCounter64());
<> 149:156823d33999 260 uint64_t timestamp64 = (currentTime64 & ~(uint64_t)0xFFFFFFFFULL) + timestamp;
<> 149:156823d33999 261 if (((uint32_t)currentTime64 > 0x80000000) && (timestamp < 0x80000000)) {
<> 149:156823d33999 262 timestamp64 += (uint64_t)0x100000000ULL;
<> 149:156823d33999 263 }
<> 149:156823d33999 264 uint32_t newCallbackTime = MICROSECONDS_TO_RTC_UNITS(timestamp64);
<> 149:156823d33999 265
<> 149:156823d33999 266 /* Check for repeat setup of an existing callback. This is actually not
<> 149:156823d33999 267 * important; the following code should work even without this check. */
<> 149:156823d33999 268 if (us_ticker_callbackPending && (newCallbackTime == us_ticker_callbackTimestamp)) {
<> 149:156823d33999 269 return;
<> 149:156823d33999 270 }
<> 149:156823d33999 271
<> 149:156823d33999 272 /* Check for callbacks which are immediately (or will *very* shortly become) pending.
<> 149:156823d33999 273 * Even if they are immediately pending, they are scheduled to trigger a few
<> 149:156823d33999 274 * ticks later. This keeps things simple by invoking the callback from an
<> 149:156823d33999 275 * independent interrupt context. */
<> 149:156823d33999 276 if ((int)(newCallbackTime - rtc1_getCounter()) <= (int)FUZZY_RTC_TICKS) {
<> 149:156823d33999 277 newCallbackTime = rtc1_getCounter() + FUZZY_RTC_TICKS;
<> 149:156823d33999 278 }
<> 149:156823d33999 279
<> 149:156823d33999 280 NRF_RTC1->CC[0] = newCallbackTime & MAX_RTC_COUNTER_VAL;
<> 149:156823d33999 281 us_ticker_callbackTimestamp = newCallbackTime;
<> 149:156823d33999 282 if (!us_ticker_callbackPending) {
<> 149:156823d33999 283 us_ticker_callbackPending = true;
<> 149:156823d33999 284 rtc1_enableCompareInterrupt();
<> 149:156823d33999 285 }
<> 149:156823d33999 286 }
<> 149:156823d33999 287
<> 149:156823d33999 288 void us_ticker_disable_interrupt(void)
<> 149:156823d33999 289 {
<> 149:156823d33999 290 if (us_ticker_callbackPending) {
<> 149:156823d33999 291 rtc1_disableCompareInterrupt();
<> 149:156823d33999 292 us_ticker_callbackPending = false;
<> 149:156823d33999 293 }
<> 149:156823d33999 294 }
<> 149:156823d33999 295
<> 149:156823d33999 296 void us_ticker_clear_interrupt(void)
<> 149:156823d33999 297 {
<> 149:156823d33999 298 NRF_RTC1->EVENTS_OVRFLW = 0;
<> 149:156823d33999 299 NRF_RTC1->EVENTS_COMPARE[0] = 0;
<> 149:156823d33999 300 }
<> 149:156823d33999 301
<> 149:156823d33999 302
<> 149:156823d33999 303 #if defined (__CC_ARM) /* ARMCC Compiler */
<> 149:156823d33999 304
<> 149:156823d33999 305 __asm void RTC1_IRQHandler(void)
<> 149:156823d33999 306 {
<> 149:156823d33999 307 IMPORT OS_Tick_Handler
<> 149:156823d33999 308 IMPORT us_ticker_handler
<> 149:156823d33999 309
<> 149:156823d33999 310 /**
<> 149:156823d33999 311 * Chanel 1 of RTC1 is used by RTX as a systick.
<> 149:156823d33999 312 * If the compare event on channel 1 is set, then branch to OS_Tick_Handler.
<> 149:156823d33999 313 * Otherwise, just execute us_ticker_handler.
<> 149:156823d33999 314 * This function has to be written in assembly and tagged as naked because OS_Tick_Handler
<> 149:156823d33999 315 * will never return.
<> 149:156823d33999 316 * A c function would put lr on the stack before calling OS_Tick_Handler and this value
<> 149:156823d33999 317 * would never been dequeued.
<> 149:156823d33999 318 *
<> 149:156823d33999 319 * \code
<> 149:156823d33999 320 * void RTC1_IRQHandler(void) {
<> 149:156823d33999 321 if(NRF_RTC1->EVENTS_COMPARE[1]) {
<> 149:156823d33999 322 // never return...
<> 149:156823d33999 323 OS_Tick_Handler();
<> 149:156823d33999 324 } else {
<> 149:156823d33999 325 us_ticker_handler();
<> 149:156823d33999 326 }
<> 149:156823d33999 327 }
<> 149:156823d33999 328 * \endcode
<> 149:156823d33999 329 */
<> 149:156823d33999 330 ldr r0,=0x40011144
<> 149:156823d33999 331 ldr r1, [r0, #0]
<> 149:156823d33999 332 cmp r1, #0
<> 149:156823d33999 333 beq US_TICKER_HANDLER
<> 149:156823d33999 334 bl OS_Tick_Handler
<> 149:156823d33999 335 US_TICKER_HANDLER
<> 149:156823d33999 336 push {r3, lr}
<> 149:156823d33999 337 bl us_ticker_handler
<> 149:156823d33999 338 pop {r3, pc}
<> 149:156823d33999 339 nop /* padding */
<> 149:156823d33999 340 }
<> 149:156823d33999 341
<> 149:156823d33999 342 #elif defined (__GNUC__) /* GNU Compiler */
<> 149:156823d33999 343
<> 149:156823d33999 344 __attribute__((naked)) void RTC1_IRQHandler(void)
<> 149:156823d33999 345 {
<> 149:156823d33999 346 /**
<> 149:156823d33999 347 * Chanel 1 of RTC1 is used by RTX as a systick.
<> 149:156823d33999 348 * If the compare event on channel 1 is set, then branch to OS_Tick_Handler.
<> 149:156823d33999 349 * Otherwise, just execute us_ticker_handler.
<> 149:156823d33999 350 * This function has to be written in assembly and tagged as naked because OS_Tick_Handler
<> 149:156823d33999 351 * will never return.
<> 149:156823d33999 352 * A c function would put lr on the stack before calling OS_Tick_Handler and this value
<> 149:156823d33999 353 * would never been dequeued.
<> 149:156823d33999 354 *
<> 149:156823d33999 355 * \code
<> 149:156823d33999 356 * void RTC1_IRQHandler(void) {
<> 149:156823d33999 357 if(NRF_RTC1->EVENTS_COMPARE[1]) {
<> 149:156823d33999 358 // never return...
<> 149:156823d33999 359 OS_Tick_Handler();
<> 149:156823d33999 360 } else {
<> 149:156823d33999 361 us_ticker_handler();
<> 149:156823d33999 362 }
<> 149:156823d33999 363 }
<> 149:156823d33999 364 * \endcode
<> 149:156823d33999 365 */
<> 149:156823d33999 366 __asm__ (
<> 149:156823d33999 367 "ldr r0,=0x40011144\n"
<> 149:156823d33999 368 "ldr r1, [r0, #0]\n"
<> 149:156823d33999 369 "cmp r1, #0\n"
<> 149:156823d33999 370 "beq US_TICKER_HANDLER\n"
<> 149:156823d33999 371 "bl OS_Tick_Handler\n"
<> 149:156823d33999 372 "US_TICKER_HANDLER:\n"
<> 149:156823d33999 373 "push {r3, lr}\n"
<> 149:156823d33999 374 "bl us_ticker_handler\n"
<> 149:156823d33999 375 "pop {r3, pc}\n"
<> 149:156823d33999 376 "nop"
<> 149:156823d33999 377 );
<> 149:156823d33999 378 }
<> 149:156823d33999 379
<> 149:156823d33999 380 #else
<> 149:156823d33999 381
<> 149:156823d33999 382 #error Compiler not supported.
<> 149:156823d33999 383 #error Provide a definition of RTC1_IRQHandler.
<> 149:156823d33999 384
<> 149:156823d33999 385 /*
<> 149:156823d33999 386 * Chanel 1 of RTC1 is used by RTX as a systick.
<> 149:156823d33999 387 * If the compare event on channel 1 is set, then branch to OS_Tick_Handler.
<> 149:156823d33999 388 * Otherwise, just execute us_ticker_handler.
<> 149:156823d33999 389 * This function has to be written in assembly and tagged as naked because OS_Tick_Handler
<> 149:156823d33999 390 * will never return.
<> 149:156823d33999 391 * A c function would put lr on the stack before calling OS_Tick_Handler and this value
<> 149:156823d33999 392 * will never been dequeued. After a certain time a stack overflow will happen.
<> 149:156823d33999 393 *
<> 149:156823d33999 394 * \code
<> 149:156823d33999 395 * void RTC1_IRQHandler(void) {
<> 149:156823d33999 396 if(NRF_RTC1->EVENTS_COMPARE[1]) {
<> 149:156823d33999 397 // never return...
<> 149:156823d33999 398 OS_Tick_Handler();
<> 149:156823d33999 399 } else {
<> 149:156823d33999 400 us_ticker_handler();
<> 149:156823d33999 401 }
<> 149:156823d33999 402 }
<> 149:156823d33999 403 * \endcode
<> 149:156823d33999 404 */
<> 149:156823d33999 405
<> 149:156823d33999 406 #endif
<> 149:156823d33999 407
<> 149:156823d33999 408 /**
<> 149:156823d33999 409 * Return the next number of clock cycle needed for the next tick.
<> 149:156823d33999 410 * @note This function has been carrefuly optimized for a systick occuring every 1000us.
<> 149:156823d33999 411 */
<> 149:156823d33999 412 static uint32_t get_next_tick_cc_delta() {
<> 149:156823d33999 413 uint32_t delta = 0;
<> 149:156823d33999 414
<> 149:156823d33999 415 if (os_clockrate != 1000) {
<> 149:156823d33999 416 // In RTX, by default SYSTICK is is used.
<> 149:156823d33999 417 // A tick event is generated every os_trv + 1 clock cycles of the system timer.
<> 149:156823d33999 418 delta = os_trv + 1;
<> 149:156823d33999 419 } else {
<> 149:156823d33999 420 // If the clockrate is set to 1000us then 1000 tick should happen every second.
<> 149:156823d33999 421 // Unfortunatelly, when clockrate is set to 1000, os_trv is equal to 31.
<> 149:156823d33999 422 // If (os_trv + 1) is used as the delta value between two ticks, 1000 ticks will be
<> 149:156823d33999 423 // generated in 32000 clock cycle instead of 32768 clock cycles.
<> 149:156823d33999 424 // As a result, if a user schedule an OS timer to start in 100s, the timer will start
<> 149:156823d33999 425 // instead after 97.656s
<> 149:156823d33999 426 // The code below fix this issue, a clock rate of 1000s will generate 1000 ticks in 32768
<> 149:156823d33999 427 // clock cycles.
<> 149:156823d33999 428 // The strategy is simple, for 1000 ticks:
<> 149:156823d33999 429 // * 768 ticks will occur 33 clock cycles after the previous tick
<> 149:156823d33999 430 // * 232 ticks will occur 32 clock cycles after the previous tick
<> 149:156823d33999 431 // By default every delta is equal to 33.
<> 149:156823d33999 432 // Every five ticks (20%, 200 delta in one second), the delta is equal to 32
<> 149:156823d33999 433 // The remaining (32) deltas equal to 32 are distributed using primes numbers.
<> 149:156823d33999 434 static uint32_t counter = 0;
<> 149:156823d33999 435 if ((counter % 5) == 0 || (counter % 31) == 0 || (counter % 139) == 0 || (counter == 503)) {
<> 149:156823d33999 436 delta = 32;
<> 149:156823d33999 437 } else {
<> 149:156823d33999 438 delta = 33;
<> 149:156823d33999 439 }
<> 149:156823d33999 440 ++counter;
<> 149:156823d33999 441 if (counter == 1000) {
<> 149:156823d33999 442 counter = 0;
<> 149:156823d33999 443 }
<> 149:156823d33999 444 }
<> 149:156823d33999 445 return delta;
<> 149:156823d33999 446 }
<> 149:156823d33999 447
<> 149:156823d33999 448 static inline void clear_tick_interrupt() {
<> 149:156823d33999 449 NRF_RTC1->EVENTS_COMPARE[1] = 0;
<> 149:156823d33999 450 NRF_RTC1->EVTENCLR = (1 << 17);
<> 149:156823d33999 451 }
<> 149:156823d33999 452
<> 149:156823d33999 453 /**
<> 149:156823d33999 454 * Indicate if a value is included in a range which can be wrapped.
<> 149:156823d33999 455 * @param begin start of the range
<> 149:156823d33999 456 * @param end end of the range
<> 149:156823d33999 457 * @param val value to check
<> 149:156823d33999 458 * @return true if the value is included in the range and false otherwise.
<> 149:156823d33999 459 */
<> 149:156823d33999 460 static inline bool is_in_wrapped_range(uint32_t begin, uint32_t end, uint32_t val) {
<> 149:156823d33999 461 // regular case, begin < end
<> 149:156823d33999 462 // return true if begin <= val < end
<> 149:156823d33999 463 if (begin < end) {
<> 149:156823d33999 464 if (begin <= val && val < end) {
<> 149:156823d33999 465 return true;
<> 149:156823d33999 466 } else {
<> 149:156823d33999 467 return false;
<> 149:156823d33999 468 }
<> 149:156823d33999 469 } else {
<> 149:156823d33999 470 // In this case end < begin because it has wrap around the limits
<> 149:156823d33999 471 // return false if end < val < begin
<> 149:156823d33999 472 if (end < val && val < begin) {
<> 149:156823d33999 473 return false;
<> 149:156823d33999 474 } else {
<> 149:156823d33999 475 return true;
<> 149:156823d33999 476 }
<> 149:156823d33999 477 }
<> 149:156823d33999 478
<> 149:156823d33999 479 }
<> 149:156823d33999 480
<> 149:156823d33999 481 /**
<> 149:156823d33999 482 * Register the next tick.
<> 149:156823d33999 483 */
<> 149:156823d33999 484 static void register_next_tick() {
<> 149:156823d33999 485 previous_tick_cc_value = NRF_RTC1->CC[1];
<> 149:156823d33999 486 uint32_t delta = get_next_tick_cc_delta();
<> 149:156823d33999 487 uint32_t new_compare_value = (previous_tick_cc_value + delta) & MAX_RTC_COUNTER_VAL;
<> 149:156823d33999 488
<> 149:156823d33999 489 // Disable irq directly for few cycles,
<> 149:156823d33999 490 // Validation of the new CC value against the COUNTER,
<> 149:156823d33999 491 // Setting the new CC value and enabling CC IRQ should be an atomic operation
<> 149:156823d33999 492 // Otherwise, there is a possibility to set an invalid CC value because
<> 149:156823d33999 493 // the RTC1 keeps running.
<> 149:156823d33999 494 // This code is very short 20-38 cycles in the worst case, it shouldn't
<> 149:156823d33999 495 // disturb softdevice.
<> 149:156823d33999 496 __disable_irq();
<> 149:156823d33999 497 uint32_t current_counter = NRF_RTC1->COUNTER;
<> 149:156823d33999 498
<> 149:156823d33999 499 // If an overflow occur, set the next tick in COUNTER + delta clock cycles
<> 149:156823d33999 500 if (is_in_wrapped_range(previous_tick_cc_value, new_compare_value, current_counter) == false) {
<> 149:156823d33999 501 new_compare_value = current_counter + delta;
<> 149:156823d33999 502 }
<> 149:156823d33999 503 NRF_RTC1->CC[1] = new_compare_value;
<> 149:156823d33999 504
<> 149:156823d33999 505 // set the interrupt of CC channel 1 and reenable IRQs
<> 149:156823d33999 506 NRF_RTC1->INTENSET = RTC_INTENSET_COMPARE1_Msk;
<> 149:156823d33999 507 __enable_irq();
<> 149:156823d33999 508 }
<> 149:156823d33999 509
<> 149:156823d33999 510 /**
<> 149:156823d33999 511 * Initialize alternative hardware timer as RTX kernel timer
<> 149:156823d33999 512 * This function is directly called by RTX.
<> 149:156823d33999 513 * @note this function shouldn't be called directly.
<> 149:156823d33999 514 * @return IRQ number of the alternative hardware timer
<> 149:156823d33999 515 */
<> 149:156823d33999 516 int os_tick_init (void)
<> 149:156823d33999 517 {
<> 149:156823d33999 518 // their is no need to start the LF clock, it is already started by SystemInit.
<> 149:156823d33999 519 NVIC_SetPriority(RTC1_IRQn, RTC1_IRQ_PRI);
<> 149:156823d33999 520 NVIC_ClearPendingIRQ(RTC1_IRQn);
<> 149:156823d33999 521 NVIC_EnableIRQ(RTC1_IRQn);
<> 149:156823d33999 522
<> 149:156823d33999 523 NRF_RTC1->TASKS_START = 1;
<> 149:156823d33999 524 nrf_delay_us(MAX_RTC_TASKS_DELAY);
<> 149:156823d33999 525
<> 149:156823d33999 526 NRF_RTC1->CC[1] = 0;
<> 149:156823d33999 527 clear_tick_interrupt();
<> 149:156823d33999 528 register_next_tick();
<> 149:156823d33999 529
<> 149:156823d33999 530 os_tick_started = true;
<> 149:156823d33999 531
<> 149:156823d33999 532 return RTC1_IRQn;
<> 149:156823d33999 533 }
<> 149:156823d33999 534
<> 149:156823d33999 535 /**
<> 149:156823d33999 536 * Acknowledge the tick interrupt.
<> 149:156823d33999 537 * This function is called by the function OS_Tick_Handler of RTX.
<> 149:156823d33999 538 * @note this function shouldn't be called directly.
<> 149:156823d33999 539 */
<> 149:156823d33999 540 void os_tick_irqack(void)
<> 149:156823d33999 541 {
<> 149:156823d33999 542 clear_tick_interrupt();
<> 149:156823d33999 543 register_next_tick();
<> 149:156823d33999 544 }
<> 149:156823d33999 545
<> 149:156823d33999 546 /**
<> 149:156823d33999 547 * Returns the overflow flag of the alternative hardware timer.
<> 149:156823d33999 548 * @note This function is exposed by RTX kernel.
<> 149:156823d33999 549 * @return 1 if the timer has overflowed and 0 otherwise.
<> 149:156823d33999 550 */
<> 149:156823d33999 551 uint32_t os_tick_ovf(void) {
<> 149:156823d33999 552 uint32_t current_counter = NRF_RTC1->COUNTER;
<> 149:156823d33999 553 uint32_t next_tick_cc_value = NRF_RTC1->CC[1];
<> 149:156823d33999 554
<> 149:156823d33999 555 return is_in_wrapped_range(previous_tick_cc_value, next_tick_cc_value, current_counter) ? 0 : 1;
<> 149:156823d33999 556 }
<> 149:156823d33999 557
<> 149:156823d33999 558 /**
<> 149:156823d33999 559 * Return the value of the alternative hardware timer.
<> 149:156823d33999 560 * @note The documentation is not very clear about what is expected as a result,
<> 149:156823d33999 561 * is it an ascending counter, a descending one ?
<> 149:156823d33999 562 * None of this is specified.
<> 149:156823d33999 563 * The default systick is a descending counter and this function return values in
<> 149:156823d33999 564 * descending order, even if the internal counter used is an ascending one.
<> 149:156823d33999 565 * @return the value of the alternative hardware timer.
<> 149:156823d33999 566 */
<> 149:156823d33999 567 uint32_t os_tick_val(void) {
<> 149:156823d33999 568 uint32_t current_counter = NRF_RTC1->COUNTER;
<> 149:156823d33999 569 uint32_t next_tick_cc_value = NRF_RTC1->CC[1];
<> 149:156823d33999 570
<> 149:156823d33999 571 // do not use os_tick_ovf because its counter value can be different
<> 149:156823d33999 572 if(is_in_wrapped_range(previous_tick_cc_value, next_tick_cc_value, current_counter)) {
<> 149:156823d33999 573 if (next_tick_cc_value > previous_tick_cc_value) {
<> 149:156823d33999 574 return next_tick_cc_value - current_counter;
<> 149:156823d33999 575 } else if(current_counter <= next_tick_cc_value) {
<> 149:156823d33999 576 return next_tick_cc_value - current_counter;
<> 149:156823d33999 577 } else {
<> 149:156823d33999 578 return next_tick_cc_value + (MAX_RTC_COUNTER_VAL - current_counter);
<> 149:156823d33999 579 }
<> 149:156823d33999 580 } else {
<> 149:156823d33999 581 // use (os_trv + 1) has the base step, can be totally inacurate ...
<> 149:156823d33999 582 uint32_t clock_cycles_by_tick = os_trv + 1;
<> 149:156823d33999 583
<> 149:156823d33999 584 // if current counter has wrap arround, add the limit to it.
<> 149:156823d33999 585 if (current_counter < next_tick_cc_value) {
<> 149:156823d33999 586 current_counter = current_counter + MAX_RTC_COUNTER_VAL;
<> 149:156823d33999 587 }
<> 149:156823d33999 588
<> 149:156823d33999 589 return clock_cycles_by_tick - ((current_counter - next_tick_cc_value) % clock_cycles_by_tick);
<> 149:156823d33999 590 }
<> 149:156823d33999 591 }