123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- #include "nrf_bootloader_dfu_timers.h"
- #include <stdint.h>
- #include <stdbool.h>
- #include <nrfx.h>
- #include "nrf_clock.h"
- #include "nrf_rtc.h"
- #include "nrf_delay.h"
- #include "nrf_log.h"
- #define RTC_PRESCALER (0)
- #define RTC_WRAP_TICKS ((1 << 24) - 1)
- #define MAX_TIMEOUT_TICKS (RTC_WRAP_TICKS)
- typedef struct
- {
- nrf_bootloader_dfu_timeout_callback_t callback;
- uint32_t timeout;
- uint32_t repeated_timeout;
- uint8_t cc_channel;
- } dfu_timer_t;
- dfu_timer_t m_timers[2] = {{.cc_channel = 0}, {.cc_channel = 1}};
- dfu_timer_t * mp_inactivity = &m_timers[0];
- dfu_timer_t * mp_wdt_feed = &m_timers[1];
- uint32_t m_counter_loops = 0;
- #if RTC_COUNT > 2
- #define RTC_INSTANCE 2
- #define RTC_STRUCT NRF_RTC2
- #define RTC_IRQHandler RTC2_IRQHandler
- #define RTC_IRQn RTC2_IRQn
- #define RTC_CC_COUNT NRF_RTC_CC_CHANNEL_COUNT(2))
- #elif RTC_COUNT > 1
- #define RTC_INSTANCE 1
- #define RTC_STRUCT NRF_RTC1
- #define RTC_IRQHandler RTC1_IRQHandler
- #define RTC_IRQn RTC1_IRQn
- #define RTC_CC_COUNT NRF_RTC_CC_CHANNEL_COUNT(1))
- #else
- #error Not enough RTC instances.
- #endif
- static void timer_init(void)
- {
- static bool m_timer_initialized;
- if (!m_timer_initialized)
- {
- if (!nrf_clock_lf_is_running())
- {
- nrf_clock_task_trigger(NRF_CLOCK_TASK_LFCLKSTART);
- }
- nrf_rtc_event_clear(RTC_STRUCT, NRF_RTC_EVENT_TICK);
- nrf_rtc_event_clear(RTC_STRUCT, NRF_RTC_EVENT_COMPARE_0);
- nrf_rtc_event_clear(RTC_STRUCT, NRF_RTC_EVENT_COMPARE_1);
- NRFX_IRQ_PRIORITY_SET(RTC_IRQn, 5);
- NRFX_IRQ_ENABLE(RTC_IRQn);
- nrf_rtc_prescaler_set(RTC_STRUCT, RTC_PRESCALER);
- nrf_rtc_task_trigger(RTC_STRUCT, NRF_RTC_TASK_CLEAR);
- nrf_rtc_task_trigger(RTC_STRUCT, NRF_RTC_TASK_START);
- nrf_rtc_int_enable(RTC_STRUCT, RTC_INTENSET_OVRFLW_Msk);
- m_timer_initialized = true;
- }
- }
- static void rtc_update(uint32_t cc_channel, uint32_t cc_value)
- {
- ASSERT(cc_channel < NRF_RTC_CC_CHANNEL_COUNT(RTC_INSTANCE));
- nrf_rtc_cc_set(RTC_STRUCT, cc_channel, cc_value);
- nrf_delay_us(31);
- nrf_rtc_event_clear(RTC_STRUCT, RTC_CHANNEL_EVENT_ADDR(cc_channel));
- nrf_rtc_int_enable(RTC_STRUCT, RTC_CHANNEL_INT_MASK(cc_channel));
- }
- static void timer_activate(dfu_timer_t * p_timer, uint32_t timeout_ticks)
- {
- NRF_LOG_DEBUG("timer_activate (0x%x)", p_timer);
- ASSERT(timeout_ticks <= MAX_TIMEOUT_TICKS);
- ASSERT(timeout_ticks >= NRF_BOOTLOADER_MIN_TIMEOUT_TICKS);
- uint32_t next_timeout_ticks = MIN(timeout_ticks, MAX_TIMEOUT_TICKS);
- uint32_t cc_value = RTC_WRAP(next_timeout_ticks + nrf_rtc_counter_get(RTC_STRUCT));
- p_timer->timeout = timeout_ticks - next_timeout_ticks;
- if ((p_timer->timeout > 0) && (p_timer->timeout < NRF_BOOTLOADER_MIN_TIMEOUT_TICKS))
- {
- p_timer->timeout += NRF_BOOTLOADER_MIN_TIMEOUT_TICKS;
- cc_value -= NRF_BOOTLOADER_MIN_TIMEOUT_TICKS;
- }
- rtc_update(p_timer->cc_channel, cc_value);
- }
- static void timer_stop(dfu_timer_t * p_timer)
- {
- NRF_LOG_DEBUG("timer_stop (0x%x)", p_timer);
- nrf_rtc_int_disable(RTC_STRUCT, RTC_CHANNEL_INT_MASK(p_timer->cc_channel));
- }
- static void timer_fire(dfu_timer_t * p_timer)
- {
- NRF_LOG_DEBUG("timer_fire (0x%x)", p_timer);
- if (p_timer->timeout != 0)
- {
-
- timer_activate(p_timer, p_timer->timeout);
- return;
- }
- if (p_timer->repeated_timeout != 0)
- {
- timer_activate(p_timer, p_timer->repeated_timeout);
- }
- if (p_timer->callback != NULL)
- {
- p_timer->callback();
- }
- }
- static void timer_start(dfu_timer_t * p_timer,
- uint32_t timeout_ticks,
- nrf_bootloader_dfu_timeout_callback_t callback)
- {
- timer_init();
- p_timer->callback = callback;
- timer_activate(p_timer, timeout_ticks);
- }
- void RTC_IRQHandler(void)
- {
- if (nrf_rtc_event_pending(RTC_STRUCT, NRF_RTC_EVENT_OVERFLOW))
- {
- m_counter_loops++;
- nrf_rtc_event_clear(RTC_STRUCT, NRF_RTC_EVENT_OVERFLOW);
- }
- for (uint32_t channel = 0; channel < 2; channel++)
- {
- if (nrf_rtc_event_pending(RTC_STRUCT, RTC_CHANNEL_EVENT_ADDR(channel)))
- {
- nrf_rtc_event_clear(RTC_STRUCT, RTC_CHANNEL_EVENT_ADDR(channel));
- timer_stop(&m_timers[channel]);
- timer_fire(&m_timers[channel]);
- }
- }
- }
- void nrf_bootloader_dfu_inactivity_timer_restart(uint32_t timeout_ticks,
- nrf_bootloader_dfu_timeout_callback_t callback)
- {
- timer_stop(mp_inactivity);
- if (timeout_ticks != 0)
- {
- timer_start(mp_inactivity, timeout_ticks, callback);
- }
- }
- void nrf_bootloader_wdt_feed_timer_start(uint32_t timeout_ticks,
- nrf_bootloader_dfu_timeout_callback_t callback)
- {
- mp_wdt_feed->repeated_timeout = timeout_ticks;
- timer_start(mp_wdt_feed, timeout_ticks, callback);
- }
- uint32_t nrf_bootloader_dfu_timer_counter_get(void)
- {
- return nrf_rtc_counter_get(RTC_STRUCT) + (m_counter_loops << 24);
- }
|