mbed library sources. With a patch for the can_api

Fork of mbed-dev by mbed official

Committer:
DangerousElectrician
Date:
Mon Nov 14 04:39:23 2016 +0000
Revision:
151:91825d030f9b
Parent:
150:02e0a0aed4ec
stuff changed?

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 defined(TARGET_MCU_NRF51822)
<> 149:156823d33999 114 OS_TICK_INT_MASK |
<> 149:156823d33999 115 #endif
<> 149:156823d33999 116 #if DEVICE_LOWPOWERTIMER
<> 149:156823d33999 117 LP_TICKER_INT_MASK |
<> 149:156823d33999 118 #endif
<> 149:156823d33999 119 US_TICKER_INT_MASK |
<> 149:156823d33999 120 NRF_RTC_INT_OVERFLOW_MASK);
<> 149:156823d33999 121
<> 149:156823d33999 122 // This event is enabled permanently, since overflow indications are needed
<> 149:156823d33999 123 // continuously.
<> 149:156823d33999 124 nrf_rtc_event_enable(COMMON_RTC_INSTANCE, NRF_RTC_INT_OVERFLOW_MASK);
<> 149:156823d33999 125 // All other relevant events are initially disabled.
<> 149:156823d33999 126 nrf_rtc_event_disable(COMMON_RTC_INSTANCE,
<> 149:156823d33999 127 #if defined(TARGET_MCU_NRF51822)
<> 149:156823d33999 128 OS_TICK_INT_MASK |
<> 149:156823d33999 129 #endif
<> 149:156823d33999 130 #if DEVICE_LOWPOWERTIMER
<> 149:156823d33999 131 LP_TICKER_INT_MASK |
<> 149:156823d33999 132 #endif
<> 149:156823d33999 133 US_TICKER_INT_MASK);
<> 149:156823d33999 134
<> 149:156823d33999 135 nrf_drv_common_irq_enable(nrf_drv_get_IRQn(COMMON_RTC_INSTANCE),
<> 150:02e0a0aed4ec 136 #ifdef NRF51
<> 150:02e0a0aed4ec 137 APP_IRQ_PRIORITY_LOW
<> 150:02e0a0aed4ec 138 #elif defined(NRF52)
<> 150:02e0a0aed4ec 139 APP_IRQ_PRIORITY_LOWEST
<> 150:02e0a0aed4ec 140 #endif
<> 150:02e0a0aed4ec 141 );
<> 149:156823d33999 142
<> 149:156823d33999 143 nrf_rtc_task_trigger(COMMON_RTC_INSTANCE, NRF_RTC_TASK_START);
<> 149:156823d33999 144
<> 149:156823d33999 145 m_common_rtc_enabled = true;
<> 149:156823d33999 146 }
<> 149:156823d33999 147
<> 149:156823d33999 148 uint32_t common_rtc_32bit_ticks_get(void)
<> 149:156823d33999 149 {
<> 149:156823d33999 150 uint32_t ticks = nrf_rtc_counter_get(COMMON_RTC_INSTANCE);
<> 149:156823d33999 151 // The counter used for time measurements is less than 32 bit wide,
<> 149:156823d33999 152 // so its value is complemented with the number of registered overflows
<> 149:156823d33999 153 // of the counter.
<> 149:156823d33999 154 ticks += (m_common_rtc_overflows << RTC_COUNTER_BITS);
<> 149:156823d33999 155 return ticks;
<> 149:156823d33999 156 }
<> 149:156823d33999 157
<> 149:156823d33999 158 uint64_t common_rtc_64bit_us_get(void)
<> 149:156823d33999 159 {
<> 149:156823d33999 160 uint32_t ticks = common_rtc_32bit_ticks_get();
<> 149:156823d33999 161 // [ticks -> microseconds]
<> 149:156823d33999 162 return ROUNDED_DIV(((uint64_t)ticks) * 1000000, RTC_INPUT_FREQ);
<> 149:156823d33999 163 }
<> 149:156823d33999 164
<> 149:156823d33999 165 void common_rtc_set_interrupt(uint32_t us_timestamp, uint32_t cc_channel,
<> 149:156823d33999 166 uint32_t int_mask)
<> 149:156823d33999 167 {
<> 149:156823d33999 168 // The internal counter is clocked with a frequency that cannot be easily
<> 149:156823d33999 169 // multiplied to 1 MHz, therefore besides the translation of values
<> 149:156823d33999 170 // (microsecond <-> ticks) a special care of overflows handling must be
<> 149:156823d33999 171 // taken. Here the 32-bit timestamp value is complemented with information
<> 149:156823d33999 172 // about current the system up time of (ticks + number of overflows of tick
<> 149:156823d33999 173 // counter on upper bits, converted to microseconds), and such 64-bit value
<> 149:156823d33999 174 // is then translated to counter ticks. Finally, the lower 24 bits of thus
<> 149:156823d33999 175 // calculated value is written to the counter compare register to prepare
<> 149:156823d33999 176 // the interrupt generation.
<> 149:156823d33999 177 uint64_t current_time64 = common_rtc_64bit_us_get();
<> 149:156823d33999 178 // [add upper 32 bits from the current time to the timestamp value]
<> 149:156823d33999 179 uint64_t timestamp64 = us_timestamp +
<> 149:156823d33999 180 (current_time64 & ~(uint64_t)0xFFFFFFFF);
<> 149:156823d33999 181 // [if the original timestamp value happens to be after the 32 bit counter
<> 149:156823d33999 182 // of microsends overflows, correct the upper 32 bits accordingly]
<> 149:156823d33999 183 if (us_timestamp < (uint32_t)(current_time64 & 0xFFFFFFFF)) {
<> 149:156823d33999 184 timestamp64 += ((uint64_t)1 << 32);
<> 149:156823d33999 185 }
<> 149:156823d33999 186 // [microseconds -> ticks, always round the result up to avoid too early
<> 149:156823d33999 187 // interrupt generation]
<> 149:156823d33999 188 uint32_t compare_value =
<> 149:156823d33999 189 (uint32_t)CEIL_DIV((timestamp64) * RTC_INPUT_FREQ, 1000000);
<> 149:156823d33999 190
<> 149:156823d33999 191 // The COMPARE event occurs when the value in compare register is N and
<> 149:156823d33999 192 // the counter value changes from N-1 to N. Therefore, the minimal safe
<> 149:156823d33999 193 // difference between the compare value to be set and the current counter
<> 149:156823d33999 194 // value is 2 ticks. This guarantees that the compare trigger is properly
<> 149:156823d33999 195 // setup before the compare condition occurs.
<> 149:156823d33999 196 uint32_t closest_safe_compare = common_rtc_32bit_ticks_get() + 2;
<> 149:156823d33999 197 if ((int)(compare_value - closest_safe_compare) <= 0) {
<> 149:156823d33999 198 compare_value = closest_safe_compare;
<> 149:156823d33999 199 }
<> 149:156823d33999 200
<> 149:156823d33999 201 nrf_rtc_cc_set(COMMON_RTC_INSTANCE, cc_channel, RTC_WRAP(compare_value));
<> 149:156823d33999 202 nrf_rtc_event_enable(COMMON_RTC_INSTANCE, int_mask);
<> 149:156823d33999 203 }
<> 149:156823d33999 204 //------------------------------------------------------------------------------
<> 149:156823d33999 205
<> 149:156823d33999 206
<> 149:156823d33999 207 void us_ticker_init(void)
<> 149:156823d33999 208 {
<> 149:156823d33999 209 common_rtc_init();
<> 149:156823d33999 210 }
<> 149:156823d33999 211
<> 149:156823d33999 212 uint32_t us_ticker_read()
<> 149:156823d33999 213 {
<> 149:156823d33999 214 us_ticker_init();
<> 149:156823d33999 215 return (uint32_t)common_rtc_64bit_us_get();
<> 149:156823d33999 216 }
<> 149:156823d33999 217
<> 149:156823d33999 218 void us_ticker_set_interrupt(timestamp_t timestamp)
<> 149:156823d33999 219 {
<> 149:156823d33999 220 common_rtc_set_interrupt(timestamp,
<> 149:156823d33999 221 US_TICKER_CC_CHANNEL, US_TICKER_INT_MASK);
<> 149:156823d33999 222 }
<> 149:156823d33999 223
<> 149:156823d33999 224 void us_ticker_disable_interrupt(void)
<> 149:156823d33999 225 {
<> 149:156823d33999 226 nrf_rtc_event_disable(COMMON_RTC_INSTANCE, US_TICKER_INT_MASK);
<> 149:156823d33999 227 }
<> 149:156823d33999 228
<> 149:156823d33999 229 void us_ticker_clear_interrupt(void)
<> 149:156823d33999 230 {
<> 149:156823d33999 231 nrf_rtc_event_clear(COMMON_RTC_INSTANCE, US_TICKER_EVENT);
<> 149:156823d33999 232 }
<> 149:156823d33999 233
<> 149:156823d33999 234
<> 149:156823d33999 235 // Since there is no SysTick on NRF51, the RTC1 channel 1 is used as an
<> 149:156823d33999 236 // alternative source of RTOS ticks.
<> 149:156823d33999 237 #if defined(TARGET_MCU_NRF51822)
<> 149:156823d33999 238
<> 149:156823d33999 239 #include "toolchain.h"
<> 149:156823d33999 240
<> 149:156823d33999 241
<> 149:156823d33999 242 #define MAX_RTC_COUNTER_VAL ((1uL << RTC_COUNTER_BITS) - 1)
<> 149:156823d33999 243
<> 149:156823d33999 244 /**
<> 149:156823d33999 245 * The value previously set in the capture compare register of channel 1
<> 149:156823d33999 246 */
<> 149:156823d33999 247 static uint32_t previous_tick_cc_value = 0;
<> 149:156823d33999 248
<> 149:156823d33999 249 /*
<> 149:156823d33999 250 RTX provide the following definitions which are used by the tick code:
<> 149:156823d33999 251 * os_trv: The number (minus 1) of clock cycle between two tick.
<> 149:156823d33999 252 * os_clockrate: Time duration between two ticks (in us).
<> 149:156823d33999 253 * OS_Tick_Handler: The function which handle a tick event.
<> 149:156823d33999 254 This function is special because it never returns.
<> 149:156823d33999 255 Those definitions are used by the code which handle the os tick.
<> 149:156823d33999 256 To allow compilation of us_ticker programs without RTOS, those symbols are
<> 149:156823d33999 257 exported from this module as weak ones.
<> 149:156823d33999 258 */
<> 149:156823d33999 259 MBED_WEAK uint32_t const os_trv;
<> 149:156823d33999 260 MBED_WEAK uint32_t const os_clockrate;
<> 149:156823d33999 261 MBED_WEAK void OS_Tick_Handler() { }
<> 149:156823d33999 262
<> 149:156823d33999 263
<> 149:156823d33999 264 #if defined (__CC_ARM) /* ARMCC Compiler */
<> 149:156823d33999 265
<> 149:156823d33999 266 __asm void COMMON_RTC_IRQ_HANDLER(void)
<> 149:156823d33999 267 {
<> 149:156823d33999 268 IMPORT OS_Tick_Handler
<> 149:156823d33999 269 IMPORT common_rtc_irq_handler
<> 149:156823d33999 270
<> 149:156823d33999 271 /**
<> 149:156823d33999 272 * Chanel 1 of RTC1 is used by RTX as a systick.
<> 149:156823d33999 273 * If the compare event on channel 1 is set, then branch to OS_Tick_Handler.
<> 149:156823d33999 274 * Otherwise, just execute common_rtc_irq_handler.
<> 149:156823d33999 275 * This function has to be written in assembly and tagged as naked because OS_Tick_Handler
<> 149:156823d33999 276 * will never return.
<> 149:156823d33999 277 * A c function would put lr on the stack before calling OS_Tick_Handler and this value
<> 149:156823d33999 278 * would never been dequeued.
<> 149:156823d33999 279 *
<> 149:156823d33999 280 * \code
<> 149:156823d33999 281 * void COMMON_RTC_IRQ_HANDLER(void) {
<> 149:156823d33999 282 if(NRF_RTC1->EVENTS_COMPARE[1]) {
<> 149:156823d33999 283 // never return...
<> 149:156823d33999 284 OS_Tick_Handler();
<> 149:156823d33999 285 } else {
<> 149:156823d33999 286 common_rtc_irq_handler();
<> 149:156823d33999 287 }
<> 149:156823d33999 288 }
<> 149:156823d33999 289 * \endcode
<> 149:156823d33999 290 */
<> 149:156823d33999 291 ldr r0,=0x40011144
<> 149:156823d33999 292 ldr r1, [r0, #0]
<> 149:156823d33999 293 cmp r1, #0
<> 149:156823d33999 294 beq US_TICKER_HANDLER
<> 149:156823d33999 295 bl OS_Tick_Handler
<> 149:156823d33999 296 US_TICKER_HANDLER
<> 149:156823d33999 297 push {r3, lr}
<> 149:156823d33999 298 bl common_rtc_irq_handler
<> 149:156823d33999 299 pop {r3, pc}
<> 149:156823d33999 300 ; ALIGN ;
<> 149:156823d33999 301 }
<> 149:156823d33999 302
<> 149:156823d33999 303 #elif defined (__GNUC__) /* GNU Compiler */
<> 149:156823d33999 304
<> 149:156823d33999 305 __attribute__((naked)) void COMMON_RTC_IRQ_HANDLER(void)
<> 149:156823d33999 306 {
<> 149:156823d33999 307 /**
<> 149:156823d33999 308 * Chanel 1 of RTC1 is used by RTX as a systick.
<> 149:156823d33999 309 * If the compare event on channel 1 is set, then branch to OS_Tick_Handler.
<> 149:156823d33999 310 * Otherwise, just execute common_rtc_irq_handler.
<> 149:156823d33999 311 * This function has to be written in assembly and tagged as naked because OS_Tick_Handler
<> 149:156823d33999 312 * will never return.
<> 149:156823d33999 313 * A c function would put lr on the stack before calling OS_Tick_Handler and this value
<> 149:156823d33999 314 * would never been dequeued.
<> 149:156823d33999 315 *
<> 149:156823d33999 316 * \code
<> 149:156823d33999 317 * void COMMON_RTC_IRQ_HANDLER(void) {
<> 149:156823d33999 318 if(NRF_RTC1->EVENTS_COMPARE[1]) {
<> 149:156823d33999 319 // never return...
<> 149:156823d33999 320 OS_Tick_Handler();
<> 149:156823d33999 321 } else {
<> 149:156823d33999 322 common_rtc_irq_handler();
<> 149:156823d33999 323 }
<> 149:156823d33999 324 }
<> 149:156823d33999 325 * \endcode
<> 149:156823d33999 326 */
<> 149:156823d33999 327 __asm__ (
<> 149:156823d33999 328 "ldr r0,=0x40011144\n"
<> 149:156823d33999 329 "ldr r1, [r0, #0]\n"
<> 149:156823d33999 330 "cmp r1, #0\n"
<> 149:156823d33999 331 "beq US_TICKER_HANDLER\n"
<> 149:156823d33999 332 "bl OS_Tick_Handler\n"
<> 149:156823d33999 333 "US_TICKER_HANDLER:\n"
<> 149:156823d33999 334 "push {r3, lr}\n"
<> 149:156823d33999 335 "bl common_rtc_irq_handler\n"
<> 149:156823d33999 336 "pop {r3, pc}\n"
<> 149:156823d33999 337 "nop"
<> 149:156823d33999 338 );
<> 149:156823d33999 339 }
<> 149:156823d33999 340
<> 149:156823d33999 341 #elif defined (__ICCARM__)//IAR
<> 149:156823d33999 342 void common_rtc_irq_handler(void);
<> 149:156823d33999 343
<> 149:156823d33999 344 __stackless __task void COMMON_RTC_IRQ_HANDLER(void)
<> 149:156823d33999 345 {
<> 149:156823d33999 346 uint32_t temp;
<> 149:156823d33999 347
<> 149:156823d33999 348 __asm volatile(
<> 149:156823d33999 349 " ldr %[temp], [%[reg2check]] \n"
<> 149:156823d33999 350 " cmp %[temp], #0 \n"
<> 149:156823d33999 351 " beq 1f \n"
<> 149:156823d33999 352 " bl.w OS_Tick_Handler \n"
<> 149:156823d33999 353 "1: \n"
<> 149:156823d33999 354 " push {r3, lr}\n"
<> 149:156823d33999 355 " blx %[rtc_irq] \n"
<> 149:156823d33999 356 " pop {r3, pc}\n"
<> 149:156823d33999 357
<> 149:156823d33999 358 : /* Outputs */
<> 149:156823d33999 359 [temp] "=&r"(temp)
<> 149:156823d33999 360 : /* Inputs */
<> 149:156823d33999 361 [reg2check] "r"(0x40011144),
<> 149:156823d33999 362 [rtc_irq] "r"(common_rtc_irq_handler)
<> 149:156823d33999 363 : /* Clobbers */
<> 149:156823d33999 364 "cc"
<> 149:156823d33999 365 );
<> 149:156823d33999 366 (void)temp;
<> 149:156823d33999 367 }
<> 149:156823d33999 368
<> 149:156823d33999 369
<> 149:156823d33999 370 #else
<> 149:156823d33999 371
<> 149:156823d33999 372 #error Compiler not supported.
<> 149:156823d33999 373 #error Provide a definition of COMMON_RTC_IRQ_HANDLER.
<> 149:156823d33999 374
<> 149:156823d33999 375 /*
<> 149:156823d33999 376 * Chanel 1 of RTC1 is used by RTX as a systick.
<> 149:156823d33999 377 * If the compare event on channel 1 is set, then branch to OS_Tick_Handler.
<> 149:156823d33999 378 * Otherwise, just execute common_rtc_irq_handler.
<> 149:156823d33999 379 * This function has to be written in assembly and tagged as naked because OS_Tick_Handler
<> 149:156823d33999 380 * will never return.
<> 149:156823d33999 381 * A c function would put lr on the stack before calling OS_Tick_Handler and this value
<> 149:156823d33999 382 * will never been dequeued. After a certain time a stack overflow will happen.
<> 149:156823d33999 383 *
<> 149:156823d33999 384 * \code
<> 149:156823d33999 385 * void COMMON_RTC_IRQ_HANDLER(void) {
<> 149:156823d33999 386 if(NRF_RTC1->EVENTS_COMPARE[1]) {
<> 149:156823d33999 387 // never return...
<> 149:156823d33999 388 OS_Tick_Handler();
<> 149:156823d33999 389 } else {
<> 149:156823d33999 390 common_rtc_irq_handler();
<> 149:156823d33999 391 }
<> 149:156823d33999 392 }
<> 149:156823d33999 393 * \endcode
<> 149:156823d33999 394 */
<> 149:156823d33999 395
<> 149:156823d33999 396 #endif
<> 149:156823d33999 397
<> 149:156823d33999 398 /**
<> 149:156823d33999 399 * Return the next number of clock cycle needed for the next tick.
<> 149:156823d33999 400 * @note This function has been carrefuly optimized for a systick occuring every 1000us.
<> 149:156823d33999 401 */
<> 149:156823d33999 402 static uint32_t get_next_tick_cc_delta() {
<> 149:156823d33999 403 uint32_t delta = 0;
<> 149:156823d33999 404
<> 149:156823d33999 405 if (os_clockrate != 1000) {
<> 149:156823d33999 406 // In RTX, by default SYSTICK is is used.
<> 149:156823d33999 407 // A tick event is generated every os_trv + 1 clock cycles of the system timer.
<> 149:156823d33999 408 delta = os_trv + 1;
<> 149:156823d33999 409 } else {
<> 149:156823d33999 410 // If the clockrate is set to 1000us then 1000 tick should happen every second.
<> 149:156823d33999 411 // Unfortunatelly, when clockrate is set to 1000, os_trv is equal to 31.
<> 149:156823d33999 412 // If (os_trv + 1) is used as the delta value between two ticks, 1000 ticks will be
<> 149:156823d33999 413 // generated in 32000 clock cycle instead of 32768 clock cycles.
<> 149:156823d33999 414 // As a result, if a user schedule an OS timer to start in 100s, the timer will start
<> 149:156823d33999 415 // instead after 97.656s
<> 149:156823d33999 416 // The code below fix this issue, a clock rate of 1000s will generate 1000 ticks in 32768
<> 149:156823d33999 417 // clock cycles.
<> 149:156823d33999 418 // The strategy is simple, for 1000 ticks:
<> 149:156823d33999 419 // * 768 ticks will occur 33 clock cycles after the previous tick
<> 149:156823d33999 420 // * 232 ticks will occur 32 clock cycles after the previous tick
<> 149:156823d33999 421 // By default every delta is equal to 33.
<> 149:156823d33999 422 // Every five ticks (20%, 200 delta in one second), the delta is equal to 32
<> 149:156823d33999 423 // The remaining (32) deltas equal to 32 are distributed using primes numbers.
<> 149:156823d33999 424 static uint32_t counter = 0;
<> 149:156823d33999 425 if ((counter % 5) == 0 || (counter % 31) == 0 || (counter % 139) == 0 || (counter == 503)) {
<> 149:156823d33999 426 delta = 32;
<> 149:156823d33999 427 } else {
<> 149:156823d33999 428 delta = 33;
<> 149:156823d33999 429 }
<> 149:156823d33999 430 ++counter;
<> 149:156823d33999 431 if (counter == 1000) {
<> 149:156823d33999 432 counter = 0;
<> 149:156823d33999 433 }
<> 149:156823d33999 434 }
<> 149:156823d33999 435 return delta;
<> 149:156823d33999 436 }
<> 149:156823d33999 437
<> 149:156823d33999 438 static inline void clear_tick_interrupt() {
<> 149:156823d33999 439 nrf_rtc_event_clear(COMMON_RTC_INSTANCE, OS_TICK_EVENT);
<> 149:156823d33999 440 nrf_rtc_event_disable(COMMON_RTC_INSTANCE, OS_TICK_INT_MASK);
<> 149:156823d33999 441 }
<> 149:156823d33999 442
<> 149:156823d33999 443 /**
<> 149:156823d33999 444 * Indicate if a value is included in a range which can be wrapped.
<> 149:156823d33999 445 * @param begin start of the range
<> 149:156823d33999 446 * @param end end of the range
<> 149:156823d33999 447 * @param val value to check
<> 149:156823d33999 448 * @return true if the value is included in the range and false otherwise.
<> 149:156823d33999 449 */
<> 149:156823d33999 450 static inline bool is_in_wrapped_range(uint32_t begin, uint32_t end, uint32_t val) {
<> 149:156823d33999 451 // regular case, begin < end
<> 149:156823d33999 452 // return true if begin <= val < end
<> 149:156823d33999 453 if (begin < end) {
<> 149:156823d33999 454 if (begin <= val && val < end) {
<> 149:156823d33999 455 return true;
<> 149:156823d33999 456 } else {
<> 149:156823d33999 457 return false;
<> 149:156823d33999 458 }
<> 149:156823d33999 459 } else {
<> 149:156823d33999 460 // In this case end < begin because it has wrap around the limits
<> 149:156823d33999 461 // return false if end < val < begin
<> 149:156823d33999 462 if (end < val && val < begin) {
<> 149:156823d33999 463 return false;
<> 149:156823d33999 464 } else {
<> 149:156823d33999 465 return true;
<> 149:156823d33999 466 }
<> 149:156823d33999 467 }
<> 149:156823d33999 468
<> 149:156823d33999 469 }
<> 149:156823d33999 470
<> 149:156823d33999 471 /**
<> 149:156823d33999 472 * Register the next tick.
<> 149:156823d33999 473 */
<> 149:156823d33999 474 static void register_next_tick() {
<> 149:156823d33999 475 previous_tick_cc_value = nrf_rtc_cc_get(COMMON_RTC_INSTANCE, OS_TICK_CC_CHANNEL);
<> 149:156823d33999 476 uint32_t delta = get_next_tick_cc_delta();
<> 149:156823d33999 477 uint32_t new_compare_value = (previous_tick_cc_value + delta) & MAX_RTC_COUNTER_VAL;
<> 149:156823d33999 478
<> 149:156823d33999 479 // Disable irq directly for few cycles,
<> 149:156823d33999 480 // Validation of the new CC value against the COUNTER,
<> 149:156823d33999 481 // Setting the new CC value and enabling CC IRQ should be an atomic operation
<> 149:156823d33999 482 // Otherwise, there is a possibility to set an invalid CC value because
<> 149:156823d33999 483 // the RTC1 keeps running.
<> 149:156823d33999 484 // This code is very short 20-38 cycles in the worst case, it shouldn't
<> 149:156823d33999 485 // disturb softdevice.
<> 149:156823d33999 486 __disable_irq();
<> 149:156823d33999 487 uint32_t current_counter = nrf_rtc_counter_get(COMMON_RTC_INSTANCE);
<> 149:156823d33999 488
<> 149:156823d33999 489 // If an overflow occur, set the next tick in COUNTER + delta clock cycles
<> 149:156823d33999 490 if (is_in_wrapped_range(previous_tick_cc_value, new_compare_value, current_counter + 1) == false) {
<> 149:156823d33999 491 new_compare_value = current_counter + delta;
<> 149:156823d33999 492 }
<> 149:156823d33999 493 nrf_rtc_cc_set(COMMON_RTC_INSTANCE, OS_TICK_CC_CHANNEL, new_compare_value);
<> 149:156823d33999 494 // Enable generation of the compare event for the value set above (this
<> 149:156823d33999 495 // event will trigger the interrupt).
<> 149:156823d33999 496 nrf_rtc_event_enable(COMMON_RTC_INSTANCE, OS_TICK_INT_MASK);
<> 149:156823d33999 497 __enable_irq();
<> 149:156823d33999 498 }
<> 149:156823d33999 499
<> 149:156823d33999 500 /**
<> 149:156823d33999 501 * Initialize alternative hardware timer as RTX kernel timer
<> 149:156823d33999 502 * This function is directly called by RTX.
<> 149:156823d33999 503 * @note this function shouldn't be called directly.
<> 149:156823d33999 504 * @return IRQ number of the alternative hardware timer
<> 149:156823d33999 505 */
<> 149:156823d33999 506 int os_tick_init (void)
<> 149:156823d33999 507 {
<> 149:156823d33999 508 common_rtc_init();
<> 149:156823d33999 509
<> 149:156823d33999 510 nrf_rtc_cc_set(COMMON_RTC_INSTANCE, OS_TICK_CC_CHANNEL, 0);
<> 149:156823d33999 511 register_next_tick();
<> 149:156823d33999 512
<> 149:156823d33999 513 return nrf_drv_get_IRQn(COMMON_RTC_INSTANCE);
<> 149:156823d33999 514 }
<> 149:156823d33999 515
<> 149:156823d33999 516 /**
<> 149:156823d33999 517 * Acknowledge the tick interrupt.
<> 149:156823d33999 518 * This function is called by the function OS_Tick_Handler of RTX.
<> 149:156823d33999 519 * @note this function shouldn't be called directly.
<> 149:156823d33999 520 */
<> 149:156823d33999 521 void os_tick_irqack(void)
<> 149:156823d33999 522 {
<> 149:156823d33999 523 clear_tick_interrupt();
<> 149:156823d33999 524 register_next_tick();
<> 149:156823d33999 525 }
<> 149:156823d33999 526
<> 149:156823d33999 527 /**
<> 149:156823d33999 528 * Returns the overflow flag of the alternative hardware timer.
<> 149:156823d33999 529 * @note This function is exposed by RTX kernel.
<> 149:156823d33999 530 * @return 1 if the timer has overflowed and 0 otherwise.
<> 149:156823d33999 531 */
<> 149:156823d33999 532 uint32_t os_tick_ovf(void) {
<> 149:156823d33999 533 uint32_t current_counter = nrf_rtc_counter_get(COMMON_RTC_INSTANCE);
<> 149:156823d33999 534 uint32_t next_tick_cc_value = nrf_rtc_cc_get(COMMON_RTC_INSTANCE, OS_TICK_CC_CHANNEL);
<> 149:156823d33999 535
<> 149:156823d33999 536 return is_in_wrapped_range(previous_tick_cc_value, next_tick_cc_value, current_counter) ? 0 : 1;
<> 149:156823d33999 537 }
<> 149:156823d33999 538
<> 149:156823d33999 539 /**
<> 149:156823d33999 540 * Return the value of the alternative hardware timer.
<> 149:156823d33999 541 * @note The documentation is not very clear about what is expected as a result,
<> 149:156823d33999 542 * is it an ascending counter, a descending one ?
<> 149:156823d33999 543 * None of this is specified.
<> 149:156823d33999 544 * The default systick is a descending counter and this function return values in
<> 149:156823d33999 545 * descending order, even if the internal counter used is an ascending one.
<> 149:156823d33999 546 * @return the value of the alternative hardware timer.
<> 149:156823d33999 547 */
<> 149:156823d33999 548 uint32_t os_tick_val(void) {
<> 149:156823d33999 549 uint32_t current_counter = nrf_rtc_counter_get(COMMON_RTC_INSTANCE);
<> 149:156823d33999 550 uint32_t next_tick_cc_value = nrf_rtc_cc_get(COMMON_RTC_INSTANCE, OS_TICK_CC_CHANNEL);
<> 149:156823d33999 551
<> 149:156823d33999 552 // do not use os_tick_ovf because its counter value can be different
<> 149:156823d33999 553 if(is_in_wrapped_range(previous_tick_cc_value, next_tick_cc_value, current_counter)) {
<> 149:156823d33999 554 if (next_tick_cc_value > previous_tick_cc_value) {
<> 149:156823d33999 555 return next_tick_cc_value - current_counter;
<> 149:156823d33999 556 } else if(current_counter <= next_tick_cc_value) {
<> 149:156823d33999 557 return next_tick_cc_value - current_counter;
<> 149:156823d33999 558 } else {
<> 149:156823d33999 559 return next_tick_cc_value + (MAX_RTC_COUNTER_VAL - current_counter);
<> 149:156823d33999 560 }
<> 149:156823d33999 561 } else {
<> 149:156823d33999 562 // use (os_trv + 1) has the base step, can be totally inacurate ...
<> 149:156823d33999 563 uint32_t clock_cycles_by_tick = os_trv + 1;
<> 149:156823d33999 564
<> 149:156823d33999 565 // if current counter has wrap arround, add the limit to it.
<> 149:156823d33999 566 if (current_counter < next_tick_cc_value) {
<> 149:156823d33999 567 current_counter = current_counter + MAX_RTC_COUNTER_VAL;
<> 149:156823d33999 568 }
<> 149:156823d33999 569
<> 149:156823d33999 570 return clock_cycles_by_tick - ((current_counter - next_tick_cc_value) % clock_cycles_by_tick);
<> 149:156823d33999 571 }
<> 149:156823d33999 572
<> 149:156823d33999 573 }
<> 149:156823d33999 574
<> 149:156823d33999 575 #endif // defined(TARGET_MCU_NRF51822)