nrf_bootloader_dfu_timers.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. * Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "nrf_bootloader_dfu_timers.h"
  41. #include <stdint.h>
  42. #include <stdbool.h>
  43. #include <nrfx.h>
  44. #include "nrf_clock.h"
  45. #include "nrf_rtc.h"
  46. #include "nrf_delay.h"
  47. #include "nrf_log.h"
  48. #define RTC_PRESCALER (0) //!< The value provided to the RTC as the prescaler. 0 corresponds to one tick per clock cycle of the LFCLK (32768 ticks/s).
  49. #define RTC_WRAP_TICKS ((1 << 24) - 1) //!< The largest possible value in the RTC counter register.
  50. #define MAX_TIMEOUT_TICKS (RTC_WRAP_TICKS) //!< The longest fire timeout allowed. Longer timeouts are handled by multiple firings.
  51. typedef struct
  52. {
  53. nrf_bootloader_dfu_timeout_callback_t callback; //!< Callback that is called when this timer times out.
  54. uint32_t timeout; //!< The number of ticks from the next firing until the actual timeout. This value will be different from 0 if the original timeout was longer than MAX_TIMEOUT_TICKS, so multiple firings were needed.
  55. uint32_t repeated_timeout; //!< If different from 0, this timer will be reactivated with this value after timing out.
  56. uint8_t cc_channel; //!< Which CC register this timer uses.
  57. } dfu_timer_t;
  58. dfu_timer_t m_timers[2] = {{.cc_channel = 0}, {.cc_channel = 1}}; //!< The timers used by this module.
  59. dfu_timer_t * mp_inactivity = &m_timers[0]; //!< Direct pointer to the inactivity timer, for convenience and readability.
  60. dfu_timer_t * mp_wdt_feed = &m_timers[1]; //!< Direct pointer to the wdt feed timer, for convenience and readability.
  61. uint32_t m_counter_loops = 0; //!< The number of times the RTC counter register has overflowed (wrapped around) since the RTC was started.
  62. #if RTC_COUNT > 2
  63. #define RTC_INSTANCE 2
  64. #define RTC_STRUCT NRF_RTC2
  65. #define RTC_IRQHandler RTC2_IRQHandler
  66. #define RTC_IRQn RTC2_IRQn
  67. #define RTC_CC_COUNT NRF_RTC_CC_CHANNEL_COUNT(2))
  68. #elif RTC_COUNT > 1
  69. #define RTC_INSTANCE 1
  70. #define RTC_STRUCT NRF_RTC1
  71. #define RTC_IRQHandler RTC1_IRQHandler
  72. #define RTC_IRQn RTC1_IRQn
  73. #define RTC_CC_COUNT NRF_RTC_CC_CHANNEL_COUNT(1))
  74. #else
  75. #error Not enough RTC instances.
  76. #endif
  77. /**@brief Function for initializing the timer if it is not already initialized.
  78. */
  79. static void timer_init(void)
  80. {
  81. static bool m_timer_initialized;
  82. if (!m_timer_initialized)
  83. {
  84. if (!nrf_clock_lf_is_running())
  85. {
  86. nrf_clock_task_trigger(NRF_CLOCK_TASK_LFCLKSTART);
  87. }
  88. nrf_rtc_event_clear(RTC_STRUCT, NRF_RTC_EVENT_TICK);
  89. nrf_rtc_event_clear(RTC_STRUCT, NRF_RTC_EVENT_COMPARE_0);
  90. nrf_rtc_event_clear(RTC_STRUCT, NRF_RTC_EVENT_COMPARE_1);
  91. NRFX_IRQ_PRIORITY_SET(RTC_IRQn, 5);
  92. NRFX_IRQ_ENABLE(RTC_IRQn);
  93. nrf_rtc_prescaler_set(RTC_STRUCT, RTC_PRESCALER);
  94. nrf_rtc_task_trigger(RTC_STRUCT, NRF_RTC_TASK_CLEAR);
  95. nrf_rtc_task_trigger(RTC_STRUCT, NRF_RTC_TASK_START);
  96. nrf_rtc_int_enable(RTC_STRUCT, RTC_INTENSET_OVRFLW_Msk);
  97. m_timer_initialized = true;
  98. }
  99. }
  100. /**@brief Function for scheduling an RTC compare event.
  101. *
  102. * @param[in] cc_channel Which of the RTC compare registers to use.
  103. * @param[in] cc_value The ticks value at which to receive the event.
  104. */
  105. static void rtc_update(uint32_t cc_channel, uint32_t cc_value)
  106. {
  107. ASSERT(cc_channel < NRF_RTC_CC_CHANNEL_COUNT(RTC_INSTANCE));
  108. nrf_rtc_cc_set(RTC_STRUCT, cc_channel, cc_value);
  109. nrf_delay_us(31);
  110. nrf_rtc_event_clear(RTC_STRUCT, RTC_CHANNEL_EVENT_ADDR(cc_channel));
  111. nrf_rtc_int_enable(RTC_STRUCT, RTC_CHANNEL_INT_MASK(cc_channel));
  112. }
  113. /**@brief Function for activating a timer, so that it will be fired.
  114. *
  115. * This can happen multiple times before the actual timeout happens if the timeout is longer than
  116. * @ref MAX_TIMEOUT_TICKS.
  117. *
  118. * @param[in] p_timer The timer to activate.
  119. * @param[in] timeout_ticks The number of ticks until the timeout.
  120. *
  121. * @retval true If the timer was activated.
  122. * @retval false If the timer is already active.
  123. */
  124. static void timer_activate(dfu_timer_t * p_timer, uint32_t timeout_ticks)
  125. {
  126. NRF_LOG_DEBUG("timer_activate (0x%x)", p_timer);
  127. ASSERT(timeout_ticks <= MAX_TIMEOUT_TICKS);
  128. ASSERT(timeout_ticks >= NRF_BOOTLOADER_MIN_TIMEOUT_TICKS);
  129. uint32_t next_timeout_ticks = MIN(timeout_ticks, MAX_TIMEOUT_TICKS);
  130. uint32_t cc_value = RTC_WRAP(next_timeout_ticks + nrf_rtc_counter_get(RTC_STRUCT));
  131. p_timer->timeout = timeout_ticks - next_timeout_ticks;
  132. if ((p_timer->timeout > 0) && (p_timer->timeout < NRF_BOOTLOADER_MIN_TIMEOUT_TICKS))
  133. {
  134. p_timer->timeout += NRF_BOOTLOADER_MIN_TIMEOUT_TICKS;
  135. cc_value -= NRF_BOOTLOADER_MIN_TIMEOUT_TICKS;
  136. }
  137. rtc_update(p_timer->cc_channel, cc_value);
  138. }
  139. /**@brief Function for deactivating a timer, so that it will not fire.
  140. *
  141. * @param[in] p_timer The timer to deactivate.
  142. *
  143. * @retval true If the timer was deactivated.
  144. * @retval false If the timer is already inactive.
  145. */
  146. static void timer_stop(dfu_timer_t * p_timer)
  147. {
  148. NRF_LOG_DEBUG("timer_stop (0x%x)", p_timer);
  149. nrf_rtc_int_disable(RTC_STRUCT, RTC_CHANNEL_INT_MASK(p_timer->cc_channel));
  150. }
  151. /**@brief Function for firing a timer.
  152. *
  153. * This can happen multiple times before the actual timeout happens if the timeout is longer than
  154. * @ref MAX_TIMEOUT_TICKS.
  155. * This function reactivates the timer if the timer is repeating, or if the timer has not yet
  156. * timed out. It then calls the callback if the timer (repeating or not) has timed out.
  157. *
  158. * @param[in] p_timer The timer to fire.
  159. */
  160. static void timer_fire(dfu_timer_t * p_timer)
  161. {
  162. NRF_LOG_DEBUG("timer_fire (0x%x)", p_timer);
  163. if (p_timer->timeout != 0)
  164. {
  165. // The timer has not yet timed out.
  166. timer_activate(p_timer, p_timer->timeout);
  167. return;
  168. }
  169. if (p_timer->repeated_timeout != 0)
  170. {
  171. timer_activate(p_timer, p_timer->repeated_timeout);
  172. }
  173. if (p_timer->callback != NULL)
  174. {
  175. p_timer->callback();
  176. }
  177. }
  178. /**@brief Function for requesting a timeout.
  179. *
  180. * The timer will time out @p timeout_ticks ticks from now. When it times out, @p callback
  181. * will be called, and if @p p_timer->repeated_timeout is not 0, a new timeout will be scheduled.
  182. *
  183. * @param[in] p_timer The timer to start.
  184. * @param[in] timeout_ticks The number of ticks until the timeout.
  185. * @param[in] callback The callback to call when the timer times out.
  186. */
  187. static void timer_start(dfu_timer_t * p_timer,
  188. uint32_t timeout_ticks,
  189. nrf_bootloader_dfu_timeout_callback_t callback)
  190. {
  191. timer_init(); // Initialize if needed.
  192. p_timer->callback = callback;
  193. timer_activate(p_timer, timeout_ticks);
  194. }
  195. /**@brief Interrupt handler for the RTC (Real Time Clock) used for the DFU timers.
  196. */
  197. void RTC_IRQHandler(void)
  198. {
  199. if (nrf_rtc_event_pending(RTC_STRUCT, NRF_RTC_EVENT_OVERFLOW))
  200. {
  201. m_counter_loops++;
  202. nrf_rtc_event_clear(RTC_STRUCT, NRF_RTC_EVENT_OVERFLOW);
  203. }
  204. for (uint32_t channel = 0; channel < 2; channel++)
  205. {
  206. if (nrf_rtc_event_pending(RTC_STRUCT, RTC_CHANNEL_EVENT_ADDR(channel)))
  207. {
  208. nrf_rtc_event_clear(RTC_STRUCT, RTC_CHANNEL_EVENT_ADDR(channel));
  209. timer_stop(&m_timers[channel]);
  210. timer_fire(&m_timers[channel]);
  211. }
  212. }
  213. }
  214. void nrf_bootloader_dfu_inactivity_timer_restart(uint32_t timeout_ticks,
  215. nrf_bootloader_dfu_timeout_callback_t callback)
  216. {
  217. timer_stop(mp_inactivity);
  218. if (timeout_ticks != 0)
  219. {
  220. timer_start(mp_inactivity, timeout_ticks, callback);
  221. }
  222. }
  223. void nrf_bootloader_wdt_feed_timer_start(uint32_t timeout_ticks,
  224. nrf_bootloader_dfu_timeout_callback_t callback)
  225. {
  226. mp_wdt_feed->repeated_timeout = timeout_ticks;
  227. timer_start(mp_wdt_feed, timeout_ticks, callback);
  228. }
  229. uint32_t nrf_bootloader_dfu_timer_counter_get(void)
  230. {
  231. return nrf_rtc_counter_get(RTC_STRUCT) + (m_counter_loops << 24);
  232. }