mbed library sources. Supersedes mbed-src.

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

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
188:bcfe06ba3d64
mbed library release version 165

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