Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

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