|
| 1 | +/* |
| 2 | + * Copyright (c) 2013 Nordic Semiconductor ASA |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without modification, |
| 6 | + * are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this list |
| 9 | + * of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA |
| 12 | + * integrated circuit in a product or a software update for such product, must reproduce |
| 13 | + * the above copyright notice, this list of conditions and the following disclaimer in |
| 14 | + * the documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be |
| 17 | + * used to endorse or promote products derived from this software without specific prior |
| 18 | + * written permission. |
| 19 | + * |
| 20 | + * 4. This software, with or without modification, must only be used with a |
| 21 | + * Nordic Semiconductor ASA integrated circuit. |
| 22 | + * |
| 23 | + * 5. Any software provided in binary or object form under this license must not be reverse |
| 24 | + * engineered, decompiled, modified and/or disassembled. |
| 25 | + * |
| 26 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 27 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 28 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 29 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 30 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 31 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 32 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 33 | + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 34 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 35 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | + * |
| 37 | + */ |
| 38 | + |
| 39 | +#include "us_ticker_api.h" |
| 40 | +#include "common_rtc.h" |
| 41 | +#include "app_util.h" |
| 42 | +#include "nrf_drv_common.h" |
| 43 | +#include "lp_ticker_api.h" |
| 44 | +#include "mbed_critical.h" |
| 45 | + |
| 46 | +#if defined(NRF52_ERRATA_20) |
| 47 | + #include "softdevice_handler.h" |
| 48 | +#endif |
| 49 | + |
| 50 | + |
| 51 | +//------------------------------------------------------------------------------ |
| 52 | +// Common stuff used also by lp_ticker and rtc_api (see "common_rtc.h"). |
| 53 | +// |
| 54 | +#include "app_util_platform.h" |
| 55 | + |
| 56 | +bool m_common_rtc_enabled = false; |
| 57 | +uint32_t volatile m_common_rtc_overflows = 0; |
| 58 | +bool volatile lp_ticker_interrupt_fire = false; |
| 59 | + |
| 60 | +__STATIC_INLINE void rtc_ovf_event_check(void) |
| 61 | +{ |
| 62 | + if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW)) { |
| 63 | + nrf_rtc_event_clear(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW); |
| 64 | + /* Don't disable this event. It shall occur periodically. |
| 65 | + * It is needed for RTC. */ |
| 66 | + |
| 67 | + ++m_common_rtc_overflows; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +#if defined(TARGET_MCU_NRF51822) |
| 72 | +void common_rtc_irq_handler(void) |
| 73 | +#else |
| 74 | +void COMMON_RTC_IRQ_HANDLER(void) |
| 75 | +#endif |
| 76 | +{ |
| 77 | + rtc_ovf_event_check(); |
| 78 | + |
| 79 | +#if DEVICE_LPTICKER |
| 80 | + if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, LP_TICKER_EVENT) || |
| 81 | + lp_ticker_interrupt_fire) { |
| 82 | + |
| 83 | + if (lp_ticker_interrupt_fire) { |
| 84 | + lp_ticker_interrupt_fire = false; |
| 85 | + } |
| 86 | + |
| 87 | + lp_ticker_irq_handler(); |
| 88 | + } |
| 89 | +#endif |
| 90 | +} |
| 91 | + |
| 92 | +/* Function for fix errata 20: RTC Register values are invalid. */ |
| 93 | +__STATIC_INLINE void errata_20(void) |
| 94 | +{ |
| 95 | +#if defined(NRF52_ERRATA_20) |
| 96 | + if (!softdevice_handler_is_enabled()) |
| 97 | + { |
| 98 | + NRF_CLOCK->EVENTS_LFCLKSTARTED = 0; |
| 99 | + NRF_CLOCK->TASKS_LFCLKSTART = 1; |
| 100 | + |
| 101 | + while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0) |
| 102 | + { |
| 103 | + } |
| 104 | + } |
| 105 | + NRF_RTC1->TASKS_STOP = 0; |
| 106 | +#endif |
| 107 | +} |
| 108 | + |
| 109 | +void RTC1_IRQHandler(void); |
| 110 | + |
| 111 | +void common_rtc_init(void) |
| 112 | +{ |
| 113 | + if (m_common_rtc_enabled) { |
| 114 | +#if DEVICE_LPTICKER |
| 115 | + nrf_rtc_event_clear(COMMON_RTC_INSTANCE, LP_TICKER_EVENT); |
| 116 | + nrf_rtc_int_disable(COMMON_RTC_INSTANCE, LP_TICKER_INT_MASK); |
| 117 | +#endif |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + errata_20(); |
| 122 | + |
| 123 | + nrf_rtc_task_trigger(COMMON_RTC_INSTANCE, NRF_RTC_TASK_STOP); |
| 124 | + |
| 125 | + NVIC_SetVector(RTC1_IRQn, (uint32_t)RTC1_IRQHandler); |
| 126 | + |
| 127 | + /* RTC is driven by the low frequency (32.768 kHz) clock, a proper request |
| 128 | + * must be made to have it running. |
| 129 | + * Currently this clock is started in 'SystemInit' (see "system_nrf51.c" |
| 130 | + * or "system_nrf52.c", respectively). |
| 131 | + */ |
| 132 | + |
| 133 | + nrf_rtc_prescaler_set(COMMON_RTC_INSTANCE, 0); |
| 134 | + |
| 135 | + /* Clear all RTC events. */ |
| 136 | +#if defined(TARGET_MCU_NRF51822) |
| 137 | + nrf_rtc_event_clear(COMMON_RTC_INSTANCE, OS_TICK_EVENT); |
| 138 | +#endif |
| 139 | +#if DEVICE_LPTICKER |
| 140 | + nrf_rtc_event_clear(COMMON_RTC_INSTANCE, LP_TICKER_EVENT); |
| 141 | +#endif |
| 142 | + nrf_rtc_event_clear(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW); |
| 143 | + |
| 144 | + /* Disable all RTC events (According to NRF_51 Reference Manual |
| 145 | + * RTC events can not be used to control RTC interrupts). |
| 146 | + * IRQ signal to NVIC is provided if interrupt is enabled. |
| 147 | + */ |
| 148 | + |
| 149 | + nrf_rtc_event_disable(COMMON_RTC_INSTANCE, NRF_RTC_INT_OVERFLOW_MASK |
| 150 | +#if defined(TARGET_MCU_NRF51822) |
| 151 | + | OS_TICK_INT_MASK |
| 152 | +#endif |
| 153 | +#if DEVICE_LPTICKER |
| 154 | + | LP_TICKER_INT_MASK |
| 155 | +#endif |
| 156 | + ); |
| 157 | + |
| 158 | + /* This interrupt is enabled permanently, since overflow indications are needed |
| 159 | + * continuously. |
| 160 | + */ |
| 161 | + nrf_rtc_int_enable(COMMON_RTC_INSTANCE, NRF_RTC_INT_OVERFLOW_MASK); |
| 162 | + |
| 163 | + /* Disable LP ticker interrupt for now. */ |
| 164 | +#if DEVICE_LPTICKER |
| 165 | + nrf_rtc_int_disable(COMMON_RTC_INSTANCE, LP_TICKER_INT_MASK); |
| 166 | +#endif |
| 167 | + |
| 168 | + nrf_drv_common_irq_enable(nrf_drv_get_IRQn(COMMON_RTC_INSTANCE), |
| 169 | +#ifdef NRF51 |
| 170 | + APP_IRQ_PRIORITY_LOW |
| 171 | +#elif defined(NRF52) || defined(NRF52840_XXAA) |
| 172 | + APP_IRQ_PRIORITY_LOWEST |
| 173 | +#endif |
| 174 | + ); |
| 175 | + |
| 176 | + nrf_rtc_task_trigger(COMMON_RTC_INSTANCE, NRF_RTC_TASK_START); |
| 177 | + |
| 178 | + m_common_rtc_enabled = true; |
| 179 | +} |
| 180 | + |
| 181 | +void common_rtc_set_interrupt(uint32_t ticks_count, uint32_t cc_channel, |
| 182 | + uint32_t int_mask) |
| 183 | +{ |
| 184 | + /* Set ticks value when interrupt should be fired. |
| 185 | + * Interrupt scheduling is performed in upper layers. */ |
| 186 | + |
| 187 | + core_util_critical_section_enter(); |
| 188 | + |
| 189 | + /* COMPARE occurs when a CC register is N and the COUNTER value transitions from N-1 to N. |
| 190 | + * If the COUNTER is N, writing N+2 to a CC register is guaranteed to trigger a |
| 191 | + * COMPARE event at N+2. |
| 192 | + */ |
| 193 | + const uint32_t now = nrf_rtc_counter_get(COMMON_RTC_INSTANCE); |
| 194 | + |
| 195 | + if (now == ticks_count || |
| 196 | + RTC_WRAP(now + 1) == ticks_count) { |
| 197 | + ticks_count += 2; |
| 198 | + } |
| 199 | + |
| 200 | + nrf_rtc_cc_set(COMMON_RTC_INSTANCE, cc_channel, RTC_WRAP(ticks_count)); |
| 201 | + |
| 202 | + if (!nrf_rtc_int_is_enabled(COMMON_RTC_INSTANCE, int_mask)) { |
| 203 | + nrf_rtc_event_clear(COMMON_RTC_INSTANCE, LP_TICKER_EVENT); |
| 204 | + nrf_rtc_int_enable(COMMON_RTC_INSTANCE, int_mask); |
| 205 | + } |
| 206 | + |
| 207 | + core_util_critical_section_exit(); |
| 208 | +} |
| 209 | + |
| 210 | +/* Since there is no SysTick on NRF51, the RTC1 channel 0 is used as an |
| 211 | + * alternative source of RTOS ticks. |
| 212 | + */ |
| 213 | +#if defined(TARGET_MCU_NRF51822) |
| 214 | + |
| 215 | +#include "mbed_toolchain.h" |
| 216 | + |
| 217 | + |
| 218 | +#define MAX_RTC_COUNTER_VAL ((1uL << RTC_COUNTER_BITS) - 1) |
| 219 | + |
| 220 | +#ifndef RTC1_CONFIG_FREQUENCY |
| 221 | + #define RTC1_CONFIG_FREQUENCY 32678 // [Hz] |
| 222 | +#endif |
| 223 | + |
| 224 | + |
| 225 | + |
| 226 | +void COMMON_RTC_IRQ_HANDLER(void) |
| 227 | +{ |
| 228 | + if(!nrf_rtc_event_pending(COMMON_RTC_INSTANCE, OS_TICK_EVENT)) { |
| 229 | + common_rtc_irq_handler(); |
| 230 | + } |
| 231 | +} |
| 232 | + |
| 233 | +IRQn_Type mbed_get_m0_tick_irqn() |
| 234 | +{ |
| 235 | + return SWI3_IRQn; |
| 236 | +} |
| 237 | + |
| 238 | + |
| 239 | +#endif // defined(TARGET_MCU_NRF51822) |
0 commit comments