|
40 | 40 | #include "nrf_clock.h"
|
41 | 41 | #endif
|
42 | 42 |
|
| 43 | +#if !defined(USE_WORKAROUND_FOR_ANOMALY_132) && \ |
| 44 | + (defined(NRF52832_XXAA) || defined(NRF52832_XXAB)) |
| 45 | +// ANOMALY 132 - LFCLK needs to avoid frame from 66us to 138us after LFCLK stop. |
| 46 | +#define USE_WORKAROUND_FOR_ANOMALY_132 1 |
| 47 | +#endif |
| 48 | + |
| 49 | +#if USE_WORKAROUND_FOR_ANOMALY_132 |
| 50 | +#include "soc/nrfx_coredep.h" |
| 51 | +#endif |
| 52 | + |
| 53 | +void mp_nrf_start_lfclk(void) { |
| 54 | + if (!nrf_clock_lf_start_task_status_get(NRF_CLOCK)) { |
| 55 | + // Check if the clock was recently stopped but is still running. |
| 56 | + #if USE_WORKAROUND_FOR_ANOMALY_132 |
| 57 | + bool was_running = nrf_clock_lf_is_running(NRF_CLOCK); |
| 58 | + // If so, wait for it to stop. This ensures that the delay for anomaly 132 workaround does |
| 59 | + // not land us in the middle of the forbidden interval. |
| 60 | + while (nrf_clock_lf_is_running(NRF_CLOCK)) { |
| 61 | + } |
| 62 | + // If the clock just stopped, we can start it again right away as we are certainly before |
| 63 | + // the forbidden 66-138us interval. Otherwise, apply a delay of 138us to make sure we are |
| 64 | + // after the interval. |
| 65 | + if (!was_running) { |
| 66 | + nrfx_coredep_delay_us(138); |
| 67 | + } |
| 68 | + #endif |
| 69 | + nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_LFCLKSTART); |
| 70 | + } |
| 71 | +} |
| 72 | + |
43 | 73 | #if MICROPY_PY_TIME_TICKS
|
44 | 74 |
|
45 | 75 | // Use RTC1 for time ticks generation (ms and us) with 32kHz tick resolution
|
@@ -99,9 +129,7 @@ STATIC void rtc_irq_time(nrfx_rtc_int_type_t event) {
|
99 | 129 |
|
100 | 130 | void rtc1_init_time_ticks(void) {
|
101 | 131 | // Start the low-frequency clock (if it hasn't been started already)
|
102 |
| - if (!nrf_clock_lf_is_running(NRF_CLOCK)) { |
103 |
| - nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_LFCLKSTART); |
104 |
| - } |
| 132 | + mp_nrf_start_lfclk(); |
105 | 133 | // Uninitialize first, then set overflow IRQ and first CC event
|
106 | 134 | nrfx_rtc_uninit(&rtc1);
|
107 | 135 | nrfx_rtc_init(&rtc1, &rtc_config_time_ticks, rtc_irq_time);
|
|
0 commit comments