mbed library sources. Supersedes mbed-src.

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

Committer:
<>
Date:
Tue Mar 14 16:40:56 2017 +0000
Revision:
160:d5399cc887bb
Parent:
151:5eaa88a5bcc7
Child:
163:74e0ce7f98e8
This updates the lib to the mbed lib v138

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1 /*
<> 149:156823d33999 2 * Copyright (c) 2013 Nordic Semiconductor ASA
<> 149:156823d33999 3 * All rights reserved.
<> 149:156823d33999 4 *
<> 149:156823d33999 5 * Redistribution and use in source and binary forms, with or without modification,
<> 149:156823d33999 6 * are permitted provided that the following conditions are met:
<> 149:156823d33999 7 *
<> 149:156823d33999 8 * 1. Redistributions of source code must retain the above copyright notice, this list
<> 149:156823d33999 9 * of conditions and the following disclaimer.
<> 149:156823d33999 10 *
<> 149:156823d33999 11 * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
<> 149:156823d33999 12 * integrated circuit in a product or a software update for such product, must reproduce
<> 149:156823d33999 13 * the above copyright notice, this list of conditions and the following disclaimer in
<> 149:156823d33999 14 * the documentation and/or other materials provided with the distribution.
<> 149:156823d33999 15 *
<> 149:156823d33999 16 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
<> 149:156823d33999 17 * used to endorse or promote products derived from this software without specific prior
<> 149:156823d33999 18 * written permission.
<> 149:156823d33999 19 *
<> 149:156823d33999 20 * 4. This software, with or without modification, must only be used with a
<> 149:156823d33999 21 * Nordic Semiconductor ASA integrated circuit.
<> 149:156823d33999 22 *
<> 149:156823d33999 23 * 5. Any software provided in binary or object form under this license must not be reverse
<> 149:156823d33999 24 * engineered, decompiled, modified and/or disassembled.
<> 149:156823d33999 25 *
<> 149:156823d33999 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
<> 149:156823d33999 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
<> 149:156823d33999 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
<> 149:156823d33999 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
<> 149:156823d33999 30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
<> 149:156823d33999 31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
<> 149:156823d33999 32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
<> 149:156823d33999 33 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
<> 149:156823d33999 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
<> 149:156823d33999 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<> 149:156823d33999 36 *
<> 149:156823d33999 37 */
<> 149:156823d33999 38
<> 149:156823d33999 39 #include "us_ticker_api.h"
<> 149:156823d33999 40 #include "common_rtc.h"
<> 149:156823d33999 41 #include "app_util.h"
<> 149:156823d33999 42 #include "nrf_drv_common.h"
<> 150:02e0a0aed4ec 43 #include "nrf_drv_config.h"
<> 149:156823d33999 44 #include "lp_ticker_api.h"
<> 149:156823d33999 45
<> 149:156823d33999 46
<> 149:156823d33999 47 //------------------------------------------------------------------------------
<> 149:156823d33999 48 // Common stuff used also by lp_ticker and rtc_api (see "common_rtc.h").
<> 149:156823d33999 49 //
<> 149:156823d33999 50 #include "app_util_platform.h"
<> 149:156823d33999 51
<> 149:156823d33999 52 bool m_common_rtc_enabled = false;
<> 149:156823d33999 53 uint32_t volatile m_common_rtc_overflows = 0;
<> 149:156823d33999 54
<> 149:156823d33999 55 #if defined(TARGET_MCU_NRF51822)
<> 149:156823d33999 56 void common_rtc_irq_handler(void)
<> 149:156823d33999 57 #else
<> 149:156823d33999 58 void COMMON_RTC_IRQ_HANDLER(void)
<> 149:156823d33999 59 #endif
<> 149:156823d33999 60 {
<> 149:156823d33999 61 if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, US_TICKER_EVENT)) {
<> 149:156823d33999 62 us_ticker_irq_handler();
<> 149:156823d33999 63 }
<> 149:156823d33999 64
<> 149:156823d33999 65 #if DEVICE_LOWPOWERTIMER
<> 149:156823d33999 66 if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, LP_TICKER_EVENT)) {
<> 149:156823d33999 67
<> 149:156823d33999 68 lp_ticker_irq_handler();
<> 149:156823d33999 69 }
<> 149:156823d33999 70 #endif
<> 149:156823d33999 71
<> 149:156823d33999 72 if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW)) {
<> 149:156823d33999 73 nrf_rtc_event_clear(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW);
<> 149:156823d33999 74 // Don't disable this event. It shall occur periodically.
<> 149:156823d33999 75
<> 149:156823d33999 76 ++m_common_rtc_overflows;
<> 149:156823d33999 77 }
<> 149:156823d33999 78 }
<> 149:156823d33999 79
<> 150:02e0a0aed4ec 80 #if (defined (__ICCARM__)) && defined(TARGET_MCU_NRF51822)//IAR
<> 150:02e0a0aed4ec 81 __stackless __task
<> 150:02e0a0aed4ec 82 #endif
<> 150:02e0a0aed4ec 83 void RTC1_IRQHandler(void);
<> 150:02e0a0aed4ec 84
<> 149:156823d33999 85 void common_rtc_init(void)
<> 149:156823d33999 86 {
<> 149:156823d33999 87 if (m_common_rtc_enabled) {
<> 149:156823d33999 88 return;
<> 149:156823d33999 89 }
<> 149:156823d33999 90
<> 150:02e0a0aed4ec 91 NVIC_SetVector(RTC1_IRQn, (uint32_t)RTC1_IRQHandler);
<> 150:02e0a0aed4ec 92
<> 149:156823d33999 93 // RTC is driven by the low frequency (32.768 kHz) clock, a proper request
<> 149:156823d33999 94 // must be made to have it running.
<> 149:156823d33999 95 // Currently this clock is started in 'SystemInit' (see "system_nrf51.c"
<> 149:156823d33999 96 // or "system_nrf52.c", respectively).
<> 149:156823d33999 97
<> 149:156823d33999 98 nrf_rtc_prescaler_set(COMMON_RTC_INSTANCE, 0);
<> 149:156823d33999 99
<> 149:156823d33999 100 nrf_rtc_event_clear(COMMON_RTC_INSTANCE, US_TICKER_EVENT);
<> 149:156823d33999 101 #if defined(TARGET_MCU_NRF51822)
<> 149:156823d33999 102 nrf_rtc_event_clear(COMMON_RTC_INSTANCE, OS_TICK_EVENT);
<> 149:156823d33999 103 #endif
<> 149:156823d33999 104 #if DEVICE_LOWPOWERTIMER
<> 149:156823d33999 105 nrf_rtc_event_clear(COMMON_RTC_INSTANCE, LP_TICKER_EVENT);
<> 149:156823d33999 106 #endif
<> 149:156823d33999 107 nrf_rtc_event_clear(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW);
<> 149:156823d33999 108
<> 149:156823d33999 109 // Interrupts on all related events are enabled permanently. Particular
<> 149:156823d33999 110 // events will be enabled or disabled as needed (such approach is more
<> 149:156823d33999 111 // energy efficient).
<> 149:156823d33999 112 nrf_rtc_int_enable(COMMON_RTC_INSTANCE,
<> 149:156823d33999 113 #if DEVICE_LOWPOWERTIMER
<> 149:156823d33999 114 LP_TICKER_INT_MASK |
<> 149:156823d33999 115 #endif
<> 149:156823d33999 116 US_TICKER_INT_MASK |
<> 149:156823d33999 117 NRF_RTC_INT_OVERFLOW_MASK);
<> 149:156823d33999 118
<> 149:156823d33999 119 // This event is enabled permanently, since overflow indications are needed
<> 149:156823d33999 120 // continuously.
<> 149:156823d33999 121 nrf_rtc_event_enable(COMMON_RTC_INSTANCE, NRF_RTC_INT_OVERFLOW_MASK);
<> 149:156823d33999 122 // All other relevant events are initially disabled.
<> 149:156823d33999 123 nrf_rtc_event_disable(COMMON_RTC_INSTANCE,
<> 149:156823d33999 124 #if defined(TARGET_MCU_NRF51822)
<> 149:156823d33999 125 OS_TICK_INT_MASK |
<> 149:156823d33999 126 #endif
<> 149:156823d33999 127 #if DEVICE_LOWPOWERTIMER
<> 149:156823d33999 128 LP_TICKER_INT_MASK |
<> 149:156823d33999 129 #endif
<> 149:156823d33999 130 US_TICKER_INT_MASK);
<> 149:156823d33999 131
<> 149:156823d33999 132 nrf_drv_common_irq_enable(nrf_drv_get_IRQn(COMMON_RTC_INSTANCE),
<> 150:02e0a0aed4ec 133 #ifdef NRF51
<> 150:02e0a0aed4ec 134 APP_IRQ_PRIORITY_LOW
<> 150:02e0a0aed4ec 135 #elif defined(NRF52)
<> 150:02e0a0aed4ec 136 APP_IRQ_PRIORITY_LOWEST
<> 150:02e0a0aed4ec 137 #endif
<> 150:02e0a0aed4ec 138 );
<> 149:156823d33999 139
<> 149:156823d33999 140 nrf_rtc_task_trigger(COMMON_RTC_INSTANCE, NRF_RTC_TASK_START);
<> 149:156823d33999 141
<> 149:156823d33999 142 m_common_rtc_enabled = true;
<> 149:156823d33999 143 }
<> 149:156823d33999 144
<> 149:156823d33999 145 uint32_t common_rtc_32bit_ticks_get(void)
<> 149:156823d33999 146 {
<> 149:156823d33999 147 uint32_t ticks = nrf_rtc_counter_get(COMMON_RTC_INSTANCE);
<> 149:156823d33999 148 // The counter used for time measurements is less than 32 bit wide,
<> 149:156823d33999 149 // so its value is complemented with the number of registered overflows
<> 149:156823d33999 150 // of the counter.
<> 149:156823d33999 151 ticks += (m_common_rtc_overflows << RTC_COUNTER_BITS);
<> 149:156823d33999 152 return ticks;
<> 149:156823d33999 153 }
<> 149:156823d33999 154
<> 149:156823d33999 155 uint64_t common_rtc_64bit_us_get(void)
<> 149:156823d33999 156 {
<> 149:156823d33999 157 uint32_t ticks = common_rtc_32bit_ticks_get();
<> 149:156823d33999 158 // [ticks -> microseconds]
<> 149:156823d33999 159 return ROUNDED_DIV(((uint64_t)ticks) * 1000000, RTC_INPUT_FREQ);
<> 149:156823d33999 160 }
<> 149:156823d33999 161
<> 149:156823d33999 162 void common_rtc_set_interrupt(uint32_t us_timestamp, uint32_t cc_channel,
<> 149:156823d33999 163 uint32_t int_mask)
<> 149:156823d33999 164 {
<> 149:156823d33999 165 // The internal counter is clocked with a frequency that cannot be easily
<> 149:156823d33999 166 // multiplied to 1 MHz, therefore besides the translation of values
<> 149:156823d33999 167 // (microsecond <-> ticks) a special care of overflows handling must be
<> 149:156823d33999 168 // taken. Here the 32-bit timestamp value is complemented with information
<> 149:156823d33999 169 // about current the system up time of (ticks + number of overflows of tick
<> 149:156823d33999 170 // counter on upper bits, converted to microseconds), and such 64-bit value
<> 149:156823d33999 171 // is then translated to counter ticks. Finally, the lower 24 bits of thus
<> 149:156823d33999 172 // calculated value is written to the counter compare register to prepare
<> 149:156823d33999 173 // the interrupt generation.
<> 149:156823d33999 174 uint64_t current_time64 = common_rtc_64bit_us_get();
<> 149:156823d33999 175 // [add upper 32 bits from the current time to the timestamp value]
<> 149:156823d33999 176 uint64_t timestamp64 = us_timestamp +
<> 149:156823d33999 177 (current_time64 & ~(uint64_t)0xFFFFFFFF);
<> 149:156823d33999 178 // [if the original timestamp value happens to be after the 32 bit counter
<> 149:156823d33999 179 // of microsends overflows, correct the upper 32 bits accordingly]
<> 149:156823d33999 180 if (us_timestamp < (uint32_t)(current_time64 & 0xFFFFFFFF)) {
<> 149:156823d33999 181 timestamp64 += ((uint64_t)1 << 32);
<> 149:156823d33999 182 }
<> 149:156823d33999 183 // [microseconds -> ticks, always round the result up to avoid too early
<> 149:156823d33999 184 // interrupt generation]
<> 149:156823d33999 185 uint32_t compare_value =
<> 149:156823d33999 186 (uint32_t)CEIL_DIV((timestamp64) * RTC_INPUT_FREQ, 1000000);
<> 149:156823d33999 187
<> 149:156823d33999 188 // The COMPARE event occurs when the value in compare register is N and
<> 149:156823d33999 189 // the counter value changes from N-1 to N. Therefore, the minimal safe
<> 149:156823d33999 190 // difference between the compare value to be set and the current counter
<> 149:156823d33999 191 // value is 2 ticks. This guarantees that the compare trigger is properly
<> 149:156823d33999 192 // setup before the compare condition occurs.
<> 149:156823d33999 193 uint32_t closest_safe_compare = common_rtc_32bit_ticks_get() + 2;
<> 149:156823d33999 194 if ((int)(compare_value - closest_safe_compare) <= 0) {
<> 149:156823d33999 195 compare_value = closest_safe_compare;
<> 149:156823d33999 196 }
<> 149:156823d33999 197
<> 149:156823d33999 198 nrf_rtc_cc_set(COMMON_RTC_INSTANCE, cc_channel, RTC_WRAP(compare_value));
<> 149:156823d33999 199 nrf_rtc_event_enable(COMMON_RTC_INSTANCE, int_mask);
<> 149:156823d33999 200 }
<> 149:156823d33999 201 //------------------------------------------------------------------------------
<> 149:156823d33999 202
<> 149:156823d33999 203
<> 149:156823d33999 204 void us_ticker_init(void)
<> 149:156823d33999 205 {
<> 149:156823d33999 206 common_rtc_init();
<> 149:156823d33999 207 }
<> 149:156823d33999 208
<> 149:156823d33999 209 uint32_t us_ticker_read()
<> 149:156823d33999 210 {
<> 149:156823d33999 211 us_ticker_init();
<> 149:156823d33999 212 return (uint32_t)common_rtc_64bit_us_get();
<> 149:156823d33999 213 }
<> 149:156823d33999 214
<> 149:156823d33999 215 void us_ticker_set_interrupt(timestamp_t timestamp)
<> 149:156823d33999 216 {
<> 149:156823d33999 217 common_rtc_set_interrupt(timestamp,
<> 149:156823d33999 218 US_TICKER_CC_CHANNEL, US_TICKER_INT_MASK);
<> 149:156823d33999 219 }
<> 149:156823d33999 220
<> 149:156823d33999 221 void us_ticker_disable_interrupt(void)
<> 149:156823d33999 222 {
<> 149:156823d33999 223 nrf_rtc_event_disable(COMMON_RTC_INSTANCE, US_TICKER_INT_MASK);
<> 149:156823d33999 224 }
<> 149:156823d33999 225
<> 149:156823d33999 226 void us_ticker_clear_interrupt(void)
<> 149:156823d33999 227 {
<> 149:156823d33999 228 nrf_rtc_event_clear(COMMON_RTC_INSTANCE, US_TICKER_EVENT);
<> 149:156823d33999 229 }
<> 149:156823d33999 230
<> 149:156823d33999 231
<> 149:156823d33999 232 // Since there is no SysTick on NRF51, the RTC1 channel 1 is used as an
<> 149:156823d33999 233 // alternative source of RTOS ticks.
<> 149:156823d33999 234 #if defined(TARGET_MCU_NRF51822)
<> 149:156823d33999 235
<> 160:d5399cc887bb 236 #include "mbed_toolchain.h"
<> 149:156823d33999 237
<> 149:156823d33999 238
<> 149:156823d33999 239 #define MAX_RTC_COUNTER_VAL ((1uL << RTC_COUNTER_BITS) - 1)
<> 149:156823d33999 240
<> 149:156823d33999 241 /**
<> 149:156823d33999 242 * The value previously set in the capture compare register of channel 1
<> 149:156823d33999 243 */
<> 149:156823d33999 244 static uint32_t previous_tick_cc_value = 0;
<> 149:156823d33999 245
<> 149:156823d33999 246 /*
<> 149:156823d33999 247 RTX provide the following definitions which are used by the tick code:
<> 149:156823d33999 248 * os_trv: The number (minus 1) of clock cycle between two tick.
<> 149:156823d33999 249 * os_clockrate: Time duration between two ticks (in us).
<> 149:156823d33999 250 * OS_Tick_Handler: The function which handle a tick event.
<> 149:156823d33999 251 This function is special because it never returns.
<> 149:156823d33999 252 Those definitions are used by the code which handle the os tick.
<> 149:156823d33999 253 To allow compilation of us_ticker programs without RTOS, those symbols are
<> 149:156823d33999 254 exported from this module as weak ones.
<> 149:156823d33999 255 */
<> 149:156823d33999 256 MBED_WEAK uint32_t const os_trv;
<> 149:156823d33999 257 MBED_WEAK uint32_t const os_clockrate;
<> 149:156823d33999 258 MBED_WEAK void OS_Tick_Handler() { }
<> 149:156823d33999 259
<> 149:156823d33999 260
<> 149:156823d33999 261 #if defined (__CC_ARM) /* ARMCC Compiler */
<> 149:156823d33999 262
<> 149:156823d33999 263 __asm void COMMON_RTC_IRQ_HANDLER(void)
<> 149:156823d33999 264 {
<> 149:156823d33999 265 IMPORT OS_Tick_Handler
<> 149:156823d33999 266 IMPORT common_rtc_irq_handler
<> 149:156823d33999 267
<> 149:156823d33999 268 /**
<> 149:156823d33999 269 * Chanel 1 of RTC1 is used by RTX as a systick.
<> 149:156823d33999 270 * If the compare event on channel 1 is set, then branch to OS_Tick_Handler.
<> 149:156823d33999 271 * Otherwise, just execute common_rtc_irq_handler.
<> 149:156823d33999 272 * This function has to be written in assembly and tagged as naked because OS_Tick_Handler
<> 149:156823d33999 273 * will never return.
<> 149:156823d33999 274 * A c function would put lr on the stack before calling OS_Tick_Handler and this value
<> 149:156823d33999 275 * would never been dequeued.
<> 149:156823d33999 276 *
<> 149:156823d33999 277 * \code
<> 149:156823d33999 278 * void COMMON_RTC_IRQ_HANDLER(void) {
<> 149:156823d33999 279 if(NRF_RTC1->EVENTS_COMPARE[1]) {
<> 149:156823d33999 280 // never return...
<> 149:156823d33999 281 OS_Tick_Handler();
<> 149:156823d33999 282 } else {
<> 149:156823d33999 283 common_rtc_irq_handler();
<> 149:156823d33999 284 }
<> 149:156823d33999 285 }
<> 149:156823d33999 286 * \endcode
<> 149:156823d33999 287 */
<> 149:156823d33999 288 ldr r0,=0x40011144
<> 149:156823d33999 289 ldr r1, [r0, #0]
<> 149:156823d33999 290 cmp r1, #0
<> 149:156823d33999 291 beq US_TICKER_HANDLER
<> 149:156823d33999 292 bl OS_Tick_Handler
<> 149:156823d33999 293 US_TICKER_HANDLER
<> 149:156823d33999 294 push {r3, lr}
<> 149:156823d33999 295 bl common_rtc_irq_handler
<> 149:156823d33999 296 pop {r3, pc}
<> 149:156823d33999 297 ; ALIGN ;
<> 149:156823d33999 298 }
<> 149:156823d33999 299
<> 149:156823d33999 300 #elif defined (__GNUC__) /* GNU Compiler */
<> 149:156823d33999 301
<> 149:156823d33999 302 __attribute__((naked)) void COMMON_RTC_IRQ_HANDLER(void)
<> 149:156823d33999 303 {
<> 149:156823d33999 304 /**
<> 149:156823d33999 305 * Chanel 1 of RTC1 is used by RTX as a systick.
<> 149:156823d33999 306 * If the compare event on channel 1 is set, then branch to OS_Tick_Handler.
<> 149:156823d33999 307 * Otherwise, just execute common_rtc_irq_handler.
<> 149:156823d33999 308 * This function has to be written in assembly and tagged as naked because OS_Tick_Handler
<> 149:156823d33999 309 * will never return.
<> 149:156823d33999 310 * A c function would put lr on the stack before calling OS_Tick_Handler and this value
<> 149:156823d33999 311 * would never been dequeued.
<> 149:156823d33999 312 *
<> 149:156823d33999 313 * \code
<> 149:156823d33999 314 * void COMMON_RTC_IRQ_HANDLER(void) {
<> 149:156823d33999 315 if(NRF_RTC1->EVENTS_COMPARE[1]) {
<> 149:156823d33999 316 // never return...
<> 149:156823d33999 317 OS_Tick_Handler();
<> 149:156823d33999 318 } else {
<> 149:156823d33999 319 common_rtc_irq_handler();
<> 149:156823d33999 320 }
<> 149:156823d33999 321 }
<> 149:156823d33999 322 * \endcode
<> 149:156823d33999 323 */
<> 149:156823d33999 324 __asm__ (
<> 149:156823d33999 325 "ldr r0,=0x40011144\n"
<> 149:156823d33999 326 "ldr r1, [r0, #0]\n"
<> 149:156823d33999 327 "cmp r1, #0\n"
<> 149:156823d33999 328 "beq US_TICKER_HANDLER\n"
<> 149:156823d33999 329 "bl OS_Tick_Handler\n"
<> 149:156823d33999 330 "US_TICKER_HANDLER:\n"
<> 149:156823d33999 331 "push {r3, lr}\n"
<> 149:156823d33999 332 "bl common_rtc_irq_handler\n"
<> 149:156823d33999 333 "pop {r3, pc}\n"
<> 149:156823d33999 334 "nop"
<> 149:156823d33999 335 );
<> 149:156823d33999 336 }
<> 149:156823d33999 337
<> 149:156823d33999 338 #elif defined (__ICCARM__)//IAR
<> 149:156823d33999 339 void common_rtc_irq_handler(void);
<> 149:156823d33999 340
<> 149:156823d33999 341 __stackless __task void COMMON_RTC_IRQ_HANDLER(void)
<> 149:156823d33999 342 {
<> 149:156823d33999 343 uint32_t temp;
<> 149:156823d33999 344
<> 149:156823d33999 345 __asm volatile(
<> 149:156823d33999 346 " ldr %[temp], [%[reg2check]] \n"
<> 149:156823d33999 347 " cmp %[temp], #0 \n"
<> 149:156823d33999 348 " beq 1f \n"
<> 149:156823d33999 349 " bl.w OS_Tick_Handler \n"
<> 149:156823d33999 350 "1: \n"
<> 149:156823d33999 351 " push {r3, lr}\n"
<> 149:156823d33999 352 " blx %[rtc_irq] \n"
<> 149:156823d33999 353 " pop {r3, pc}\n"
<> 149:156823d33999 354
<> 149:156823d33999 355 : /* Outputs */
<> 149:156823d33999 356 [temp] "=&r"(temp)
<> 149:156823d33999 357 : /* Inputs */
<> 149:156823d33999 358 [reg2check] "r"(0x40011144),
<> 149:156823d33999 359 [rtc_irq] "r"(common_rtc_irq_handler)
<> 149:156823d33999 360 : /* Clobbers */
<> 149:156823d33999 361 "cc"
<> 149:156823d33999 362 );
<> 149:156823d33999 363 (void)temp;
<> 149:156823d33999 364 }
<> 149:156823d33999 365
<> 149:156823d33999 366
<> 149:156823d33999 367 #else
<> 149:156823d33999 368
<> 149:156823d33999 369 #error Compiler not supported.
<> 149:156823d33999 370 #error Provide a definition of COMMON_RTC_IRQ_HANDLER.
<> 149:156823d33999 371
<> 149:156823d33999 372 /*
<> 149:156823d33999 373 * Chanel 1 of RTC1 is used by RTX as a systick.
<> 149:156823d33999 374 * If the compare event on channel 1 is set, then branch to OS_Tick_Handler.
<> 149:156823d33999 375 * Otherwise, just execute common_rtc_irq_handler.
<> 149:156823d33999 376 * This function has to be written in assembly and tagged as naked because OS_Tick_Handler
<> 149:156823d33999 377 * will never return.
<> 149:156823d33999 378 * A c function would put lr on the stack before calling OS_Tick_Handler and this value
<> 149:156823d33999 379 * will never been dequeued. After a certain time a stack overflow will happen.
<> 149:156823d33999 380 *
<> 149:156823d33999 381 * \code
<> 149:156823d33999 382 * void COMMON_RTC_IRQ_HANDLER(void) {
<> 149:156823d33999 383 if(NRF_RTC1->EVENTS_COMPARE[1]) {
<> 149:156823d33999 384 // never return...
<> 149:156823d33999 385 OS_Tick_Handler();
<> 149:156823d33999 386 } else {
<> 149:156823d33999 387 common_rtc_irq_handler();
<> 149:156823d33999 388 }
<> 149:156823d33999 389 }
<> 149:156823d33999 390 * \endcode
<> 149:156823d33999 391 */
<> 149:156823d33999 392
<> 149:156823d33999 393 #endif
<> 149:156823d33999 394
<> 149:156823d33999 395 /**
<> 149:156823d33999 396 * Return the next number of clock cycle needed for the next tick.
<> 149:156823d33999 397 * @note This function has been carrefuly optimized for a systick occuring every 1000us.
<> 149:156823d33999 398 */
<> 149:156823d33999 399 static uint32_t get_next_tick_cc_delta() {
<> 149:156823d33999 400 uint32_t delta = 0;
<> 149:156823d33999 401
<> 149:156823d33999 402 if (os_clockrate != 1000) {
<> 149:156823d33999 403 // In RTX, by default SYSTICK is is used.
<> 149:156823d33999 404 // A tick event is generated every os_trv + 1 clock cycles of the system timer.
<> 149:156823d33999 405 delta = os_trv + 1;
<> 149:156823d33999 406 } else {
<> 149:156823d33999 407 // If the clockrate is set to 1000us then 1000 tick should happen every second.
<> 149:156823d33999 408 // Unfortunatelly, when clockrate is set to 1000, os_trv is equal to 31.
<> 149:156823d33999 409 // If (os_trv + 1) is used as the delta value between two ticks, 1000 ticks will be
<> 149:156823d33999 410 // generated in 32000 clock cycle instead of 32768 clock cycles.
<> 149:156823d33999 411 // As a result, if a user schedule an OS timer to start in 100s, the timer will start
<> 149:156823d33999 412 // instead after 97.656s
<> 149:156823d33999 413 // The code below fix this issue, a clock rate of 1000s will generate 1000 ticks in 32768
<> 149:156823d33999 414 // clock cycles.
<> 149:156823d33999 415 // The strategy is simple, for 1000 ticks:
<> 149:156823d33999 416 // * 768 ticks will occur 33 clock cycles after the previous tick
<> 149:156823d33999 417 // * 232 ticks will occur 32 clock cycles after the previous tick
<> 149:156823d33999 418 // By default every delta is equal to 33.
<> 149:156823d33999 419 // Every five ticks (20%, 200 delta in one second), the delta is equal to 32
<> 149:156823d33999 420 // The remaining (32) deltas equal to 32 are distributed using primes numbers.
<> 149:156823d33999 421 static uint32_t counter = 0;
<> 149:156823d33999 422 if ((counter % 5) == 0 || (counter % 31) == 0 || (counter % 139) == 0 || (counter == 503)) {
<> 149:156823d33999 423 delta = 32;
<> 149:156823d33999 424 } else {
<> 149:156823d33999 425 delta = 33;
<> 149:156823d33999 426 }
<> 149:156823d33999 427 ++counter;
<> 149:156823d33999 428 if (counter == 1000) {
<> 149:156823d33999 429 counter = 0;
<> 149:156823d33999 430 }
<> 149:156823d33999 431 }
<> 149:156823d33999 432 return delta;
<> 149:156823d33999 433 }
<> 149:156823d33999 434
<> 149:156823d33999 435 static inline void clear_tick_interrupt() {
<> 149:156823d33999 436 nrf_rtc_event_clear(COMMON_RTC_INSTANCE, OS_TICK_EVENT);
<> 149:156823d33999 437 nrf_rtc_event_disable(COMMON_RTC_INSTANCE, OS_TICK_INT_MASK);
<> 149:156823d33999 438 }
<> 149:156823d33999 439
<> 149:156823d33999 440 /**
<> 149:156823d33999 441 * Indicate if a value is included in a range which can be wrapped.
<> 149:156823d33999 442 * @param begin start of the range
<> 149:156823d33999 443 * @param end end of the range
<> 149:156823d33999 444 * @param val value to check
<> 149:156823d33999 445 * @return true if the value is included in the range and false otherwise.
<> 149:156823d33999 446 */
<> 149:156823d33999 447 static inline bool is_in_wrapped_range(uint32_t begin, uint32_t end, uint32_t val) {
<> 149:156823d33999 448 // regular case, begin < end
<> 149:156823d33999 449 // return true if begin <= val < end
<> 149:156823d33999 450 if (begin < end) {
<> 149:156823d33999 451 if (begin <= val && val < end) {
<> 149:156823d33999 452 return true;
<> 149:156823d33999 453 } else {
<> 149:156823d33999 454 return false;
<> 149:156823d33999 455 }
<> 149:156823d33999 456 } else {
<> 149:156823d33999 457 // In this case end < begin because it has wrap around the limits
<> 149:156823d33999 458 // return false if end < val < begin
<> 149:156823d33999 459 if (end < val && val < begin) {
<> 149:156823d33999 460 return false;
<> 149:156823d33999 461 } else {
<> 149:156823d33999 462 return true;
<> 149:156823d33999 463 }
<> 149:156823d33999 464 }
<> 149:156823d33999 465
<> 149:156823d33999 466 }
<> 149:156823d33999 467
<> 149:156823d33999 468 /**
<> 149:156823d33999 469 * Register the next tick.
<> 149:156823d33999 470 */
<> 149:156823d33999 471 static void register_next_tick() {
<> 149:156823d33999 472 previous_tick_cc_value = nrf_rtc_cc_get(COMMON_RTC_INSTANCE, OS_TICK_CC_CHANNEL);
<> 149:156823d33999 473 uint32_t delta = get_next_tick_cc_delta();
<> 149:156823d33999 474 uint32_t new_compare_value = (previous_tick_cc_value + delta) & MAX_RTC_COUNTER_VAL;
<> 149:156823d33999 475
<> 149:156823d33999 476 // Disable irq directly for few cycles,
<> 149:156823d33999 477 // Validation of the new CC value against the COUNTER,
<> 149:156823d33999 478 // Setting the new CC value and enabling CC IRQ should be an atomic operation
<> 149:156823d33999 479 // Otherwise, there is a possibility to set an invalid CC value because
<> 149:156823d33999 480 // the RTC1 keeps running.
<> 149:156823d33999 481 // This code is very short 20-38 cycles in the worst case, it shouldn't
<> 149:156823d33999 482 // disturb softdevice.
<> 149:156823d33999 483 __disable_irq();
<> 149:156823d33999 484 uint32_t current_counter = nrf_rtc_counter_get(COMMON_RTC_INSTANCE);
<> 149:156823d33999 485
<> 149:156823d33999 486 // If an overflow occur, set the next tick in COUNTER + delta clock cycles
<> 149:156823d33999 487 if (is_in_wrapped_range(previous_tick_cc_value, new_compare_value, current_counter + 1) == false) {
<> 149:156823d33999 488 new_compare_value = current_counter + delta;
<> 149:156823d33999 489 }
<> 149:156823d33999 490 nrf_rtc_cc_set(COMMON_RTC_INSTANCE, OS_TICK_CC_CHANNEL, new_compare_value);
<> 149:156823d33999 491 // Enable generation of the compare event for the value set above (this
<> 149:156823d33999 492 // event will trigger the interrupt).
<> 149:156823d33999 493 nrf_rtc_event_enable(COMMON_RTC_INSTANCE, OS_TICK_INT_MASK);
<> 149:156823d33999 494 __enable_irq();
<> 149:156823d33999 495 }
<> 149:156823d33999 496
<> 149:156823d33999 497 /**
<> 149:156823d33999 498 * Initialize alternative hardware timer as RTX kernel timer
<> 149:156823d33999 499 * This function is directly called by RTX.
<> 149:156823d33999 500 * @note this function shouldn't be called directly.
<> 149:156823d33999 501 * @return IRQ number of the alternative hardware timer
<> 149:156823d33999 502 */
<> 149:156823d33999 503 int os_tick_init (void)
<> 149:156823d33999 504 {
<> 149:156823d33999 505 common_rtc_init();
<> 151:5eaa88a5bcc7 506 nrf_rtc_int_enable(COMMON_RTC_INSTANCE, OS_TICK_INT_MASK);
<> 149:156823d33999 507
<> 149:156823d33999 508 nrf_rtc_cc_set(COMMON_RTC_INSTANCE, OS_TICK_CC_CHANNEL, 0);
<> 149:156823d33999 509 register_next_tick();
<> 149:156823d33999 510
<> 149:156823d33999 511 return nrf_drv_get_IRQn(COMMON_RTC_INSTANCE);
<> 149:156823d33999 512 }
<> 149:156823d33999 513
<> 149:156823d33999 514 /**
<> 149:156823d33999 515 * Acknowledge the tick interrupt.
<> 149:156823d33999 516 * This function is called by the function OS_Tick_Handler of RTX.
<> 149:156823d33999 517 * @note this function shouldn't be called directly.
<> 149:156823d33999 518 */
<> 149:156823d33999 519 void os_tick_irqack(void)
<> 149:156823d33999 520 {
<> 149:156823d33999 521 clear_tick_interrupt();
<> 149:156823d33999 522 register_next_tick();
<> 149:156823d33999 523 }
<> 149:156823d33999 524
<> 149:156823d33999 525 /**
<> 149:156823d33999 526 * Returns the overflow flag of the alternative hardware timer.
<> 149:156823d33999 527 * @note This function is exposed by RTX kernel.
<> 149:156823d33999 528 * @return 1 if the timer has overflowed and 0 otherwise.
<> 149:156823d33999 529 */
<> 149:156823d33999 530 uint32_t os_tick_ovf(void) {
<> 149:156823d33999 531 uint32_t current_counter = nrf_rtc_counter_get(COMMON_RTC_INSTANCE);
<> 149:156823d33999 532 uint32_t next_tick_cc_value = nrf_rtc_cc_get(COMMON_RTC_INSTANCE, OS_TICK_CC_CHANNEL);
<> 149:156823d33999 533
<> 149:156823d33999 534 return is_in_wrapped_range(previous_tick_cc_value, next_tick_cc_value, current_counter) ? 0 : 1;
<> 149:156823d33999 535 }
<> 149:156823d33999 536
<> 149:156823d33999 537 /**
<> 149:156823d33999 538 * Return the value of the alternative hardware timer.
<> 149:156823d33999 539 * @note The documentation is not very clear about what is expected as a result,
<> 149:156823d33999 540 * is it an ascending counter, a descending one ?
<> 149:156823d33999 541 * None of this is specified.
<> 149:156823d33999 542 * The default systick is a descending counter and this function return values in
<> 149:156823d33999 543 * descending order, even if the internal counter used is an ascending one.
<> 149:156823d33999 544 * @return the value of the alternative hardware timer.
<> 149:156823d33999 545 */
<> 149:156823d33999 546 uint32_t os_tick_val(void) {
<> 149:156823d33999 547 uint32_t current_counter = nrf_rtc_counter_get(COMMON_RTC_INSTANCE);
<> 149:156823d33999 548 uint32_t next_tick_cc_value = nrf_rtc_cc_get(COMMON_RTC_INSTANCE, OS_TICK_CC_CHANNEL);
<> 149:156823d33999 549
<> 149:156823d33999 550 // do not use os_tick_ovf because its counter value can be different
<> 149:156823d33999 551 if(is_in_wrapped_range(previous_tick_cc_value, next_tick_cc_value, current_counter)) {
<> 149:156823d33999 552 if (next_tick_cc_value > previous_tick_cc_value) {
<> 149:156823d33999 553 return next_tick_cc_value - current_counter;
<> 149:156823d33999 554 } else if(current_counter <= next_tick_cc_value) {
<> 149:156823d33999 555 return next_tick_cc_value - current_counter;
<> 149:156823d33999 556 } else {
<> 149:156823d33999 557 return next_tick_cc_value + (MAX_RTC_COUNTER_VAL - current_counter);
<> 149:156823d33999 558 }
<> 149:156823d33999 559 } else {
<> 149:156823d33999 560 // use (os_trv + 1) has the base step, can be totally inacurate ...
<> 149:156823d33999 561 uint32_t clock_cycles_by_tick = os_trv + 1;
<> 149:156823d33999 562
<> 149:156823d33999 563 // if current counter has wrap arround, add the limit to it.
<> 149:156823d33999 564 if (current_counter < next_tick_cc_value) {
<> 149:156823d33999 565 current_counter = current_counter + MAX_RTC_COUNTER_VAL;
<> 149:156823d33999 566 }
<> 149:156823d33999 567
<> 149:156823d33999 568 return clock_cycles_by_tick - ((current_counter - next_tick_cc_value) % clock_cycles_by_tick);
<> 149:156823d33999 569 }
<> 149:156823d33999 570
<> 149:156823d33999 571 }
<> 149:156823d33999 572
<> 149:156823d33999 573 #endif // defined(TARGET_MCU_NRF51822)