app_timer.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /**
  2. * Copyright (c) 2012 - 2018, 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 "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(APP_TIMER)
  42. #include "app_timer.h"
  43. #include <stdlib.h>
  44. #include "nrf.h"
  45. #include "nrf_peripherals.h"
  46. #include "nrf_soc.h"
  47. #include "app_error.h"
  48. #include "nrf_delay.h"
  49. #include "app_util_platform.h"
  50. #if APP_TIMER_CONFIG_USE_SCHEDULER
  51. #include "app_scheduler.h"
  52. #endif
  53. #define RTC1_IRQ_PRI APP_TIMER_CONFIG_IRQ_PRIORITY /**< Priority of the RTC1 interrupt (used for checking for timeouts and executing timeout handlers). */
  54. #define SWI_IRQ_PRI APP_TIMER_CONFIG_IRQ_PRIORITY /**< Priority of the SWI interrupt (used for updating the timer list). */
  55. // The current design assumes that both interrupt handlers run at the same interrupt level.
  56. // If this is to be changed, protection must be added to prevent them from interrupting each other
  57. // (e.g. by using guard/trigger flags).
  58. STATIC_ASSERT(RTC1_IRQ_PRI == SWI_IRQ_PRI);
  59. #define MAX_RTC_COUNTER_VAL 0x00FFFFFF /**< Maximum value of the RTC counter. */
  60. #define RTC_COMPARE_OFFSET_MIN 3 /**< Minimum offset between the current RTC counter value and the Capture Compare register. Although the nRF51 Series User Specification recommends this value to be 2, we use 3 to be safer.*/
  61. #define MAX_RTC_TASKS_DELAY 47 /**< Maximum delay until an RTC task is executed. */
  62. #ifdef EGU_PRESENT
  63. #define SWI_PART(_id) CONCAT_2(SWI,_id)
  64. #define EGU_PART(_id) CONCAT_2(_EGU,_id)
  65. #define SWI_IRQ_n(_id) CONCAT_3(SWI_PART(_id), EGU_PART(_id),_IRQn)
  66. #define SWI_IRQ_Handler_n(_id) CONCAT_3(SWI_PART(_id), EGU_PART(_id),_IRQHandler)
  67. #else //EGU_PRESENT
  68. #define SWI_IRQ_n(_id) CONCAT_3(SWI,_id,_IRQn)
  69. #define SWI_IRQ_Handler_n(_id) CONCAT_3(SWI,_id,_IRQHandler)
  70. #endif
  71. #define SWI_IRQn SWI_IRQ_n(APP_TIMER_CONFIG_SWI_NUMBER)
  72. #define SWI_IRQHandler SWI_IRQ_Handler_n(APP_TIMER_CONFIG_SWI_NUMBER)
  73. #define MODULE_INITIALIZED (m_op_queue.size != 0) /**< Macro designating whether the module has been initialized properly. */
  74. /**@brief Timer node type. The nodes will be used form a linked list of running timers. */
  75. typedef struct
  76. {
  77. uint32_t ticks_to_expire; /**< Number of ticks from previous timer interrupt to timer expiry. */
  78. uint32_t ticks_at_start; /**< Current RTC counter value when the timer was started. */
  79. uint32_t ticks_first_interval; /**< Number of ticks in the first timer interval. */
  80. uint32_t ticks_periodic_interval; /**< Timer period (for repeating timers). */
  81. bool is_running; /**< True if timer is running, False otherwise. */
  82. app_timer_mode_t mode; /**< Timer mode. */
  83. app_timer_timeout_handler_t p_timeout_handler; /**< Pointer to function to be executed when the timer expires. */
  84. void * p_context; /**< General purpose pointer. Will be passed to the timeout handler when the timer expires. */
  85. void * next; /**< Pointer to the next node. */
  86. } timer_node_t;
  87. STATIC_ASSERT(sizeof(timer_node_t) == APP_TIMER_NODE_SIZE);
  88. /**@brief Set of available timer operation types. */
  89. typedef enum
  90. {
  91. TIMER_USER_OP_TYPE_NONE, /**< Invalid timer operation type. */
  92. TIMER_USER_OP_TYPE_START, /**< Timer operation type Start. */
  93. TIMER_USER_OP_TYPE_STOP, /**< Timer operation type Stop. */
  94. TIMER_USER_OP_TYPE_STOP_ALL /**< Timer operation type Stop All. */
  95. } timer_user_op_type_t;
  96. /**@brief Structure describing a timer start operation. */
  97. typedef struct
  98. {
  99. uint32_t ticks_at_start; /**< Current RTC counter value when the timer was started. */
  100. uint32_t ticks_first_interval; /**< Number of ticks in the first timer interval. */
  101. uint32_t ticks_periodic_interval; /**< Timer period (for repeating timers). */
  102. void * p_context; /**< General purpose pointer. Will be passed to the timeout handler when the timer expires. */
  103. } timer_user_op_start_t;
  104. /**@brief Structure describing a timer operation. */
  105. typedef struct
  106. {
  107. timer_user_op_type_t op_type; /**< Id of timer on which the operation is to be performed. */
  108. timer_node_t * p_node;
  109. union
  110. {
  111. timer_user_op_start_t start; /**< Structure describing a timer start operation. */
  112. } params;
  113. } timer_user_op_t;
  114. /**@brief Structure describing a timer operations queue.
  115. *
  116. * @details This queue will hold timer operations issued by the application
  117. * until the timer interrupt handler processes these operations.
  118. */
  119. typedef struct
  120. {
  121. uint8_t first; /**< Index of first entry to have been inserted in the queue (i.e. the next entry to be executed). */
  122. uint8_t last; /**< Index of last entry to have been inserted in the queue. */
  123. uint8_t size; /**< Queue size. */
  124. timer_user_op_t user_op_queue[APP_TIMER_CONFIG_OP_QUEUE_SIZE+1]; /**< Queue buffer. */
  125. } timer_op_queue_t;
  126. STATIC_ASSERT(sizeof(timer_op_queue_t) % 4 == 0);
  127. #define CONTEXT_QUEUE_SIZE_MAX (2)
  128. static timer_op_queue_t m_op_queue; /**< Timer operations queue. */
  129. static timer_node_t * mp_timer_id_head; /**< First timer in list of running timers. */
  130. static uint32_t m_ticks_latest; /**< Last known RTC counter value. */
  131. static uint32_t m_ticks_elapsed[CONTEXT_QUEUE_SIZE_MAX]; /**< Timer internal elapsed ticks queue. */
  132. static uint8_t m_ticks_elapsed_q_read_ind; /**< Timer internal elapsed ticks queue read index. */
  133. static uint8_t m_ticks_elapsed_q_write_ind; /**< Timer internal elapsed ticks queue write index. */
  134. static bool m_rtc1_running; /**< Boolean indicating if RTC1 is running. */
  135. static bool m_rtc1_reset; /**< Boolean indicating if RTC1 counter has been reset due to last timer removed from timer list during the timer list handling. */
  136. #if APP_TIMER_WITH_PROFILER
  137. static uint8_t m_max_user_op_queue_utilization; /**< Maximum observed timer user operations queue utilization. */
  138. #endif
  139. /**@brief Function for initializing the RTC1 counter.
  140. *
  141. * @param[in] prescaler Value of the RTC1 PRESCALER register. Set to 0 for no prescaling.
  142. */
  143. static void rtc1_init(uint32_t prescaler)
  144. {
  145. NRF_RTC1->PRESCALER = prescaler;
  146. NVIC_SetPriority(RTC1_IRQn, RTC1_IRQ_PRI);
  147. }
  148. /**@brief Function for starting the RTC1 timer.
  149. */
  150. static void rtc1_start(void)
  151. {
  152. NRF_RTC1->EVTENSET = RTC_EVTEN_COMPARE0_Msk;
  153. NRF_RTC1->INTENSET = RTC_INTENSET_COMPARE0_Msk;
  154. NVIC_ClearPendingIRQ(RTC1_IRQn);
  155. NVIC_EnableIRQ(RTC1_IRQn);
  156. NRF_RTC1->TASKS_START = 1;
  157. nrf_delay_us(MAX_RTC_TASKS_DELAY);
  158. m_rtc1_running = true;
  159. }
  160. /**@brief Function for stopping the RTC1 timer.
  161. */
  162. static void rtc1_stop(void)
  163. {
  164. NVIC_DisableIRQ(RTC1_IRQn);
  165. NRF_RTC1->EVTENCLR = RTC_EVTEN_COMPARE0_Msk;
  166. NRF_RTC1->INTENCLR = RTC_INTENSET_COMPARE0_Msk;
  167. NRF_RTC1->TASKS_STOP = 1;
  168. nrf_delay_us(MAX_RTC_TASKS_DELAY);
  169. NRF_RTC1->TASKS_CLEAR = 1;
  170. m_ticks_latest = 0;
  171. nrf_delay_us(MAX_RTC_TASKS_DELAY);
  172. m_rtc1_running = false;
  173. }
  174. /**@brief Function for returning the current value of the RTC1 counter.
  175. *
  176. * @return Current value of the RTC1 counter.
  177. */
  178. static __INLINE uint32_t rtc1_counter_get(void)
  179. {
  180. return NRF_RTC1->COUNTER;
  181. }
  182. /**@brief Function for computing the difference between two RTC1 counter values.
  183. *
  184. * @return Number of ticks elapsed from ticks_old to ticks_now.
  185. */
  186. static __INLINE uint32_t ticks_diff_get(uint32_t ticks_now, uint32_t ticks_old)
  187. {
  188. return ((ticks_now - ticks_old) & MAX_RTC_COUNTER_VAL);
  189. }
  190. /**@brief Function for setting the RTC1 Capture Compare register 0, and enabling the corresponding
  191. * event.
  192. *
  193. * @param[in] value New value of Capture Compare register 0.
  194. */
  195. static __INLINE void rtc1_compare0_set(uint32_t value)
  196. {
  197. NRF_RTC1->CC[0] = value;
  198. }
  199. /**@brief Function for inserting a timer in the timer list.
  200. *
  201. * @param[in] timer_id Id of timer to insert.
  202. */
  203. static void timer_list_insert(timer_node_t * p_timer)
  204. {
  205. if (mp_timer_id_head == NULL)
  206. {
  207. mp_timer_id_head = p_timer;
  208. }
  209. else
  210. {
  211. if (p_timer->ticks_to_expire <= mp_timer_id_head->ticks_to_expire)
  212. {
  213. mp_timer_id_head->ticks_to_expire -= p_timer->ticks_to_expire;
  214. p_timer->next = mp_timer_id_head;
  215. mp_timer_id_head = p_timer;
  216. }
  217. else
  218. {
  219. timer_node_t * p_previous;
  220. timer_node_t * p_current;
  221. uint32_t ticks_to_expire;
  222. ticks_to_expire = p_timer->ticks_to_expire;
  223. p_previous = mp_timer_id_head;
  224. p_current = mp_timer_id_head;
  225. while ((p_current != NULL) && (ticks_to_expire > p_current->ticks_to_expire))
  226. {
  227. ticks_to_expire -= p_current->ticks_to_expire;
  228. p_previous = p_current;
  229. p_current = p_current->next;
  230. }
  231. if (p_current != NULL)
  232. {
  233. p_current->ticks_to_expire -= ticks_to_expire;
  234. }
  235. p_timer->ticks_to_expire = ticks_to_expire;
  236. p_timer->next = p_current;
  237. p_previous->next = p_timer;
  238. }
  239. }
  240. }
  241. /**@brief Function for removing a timer from the timer queue.
  242. *
  243. * @param[in] timer_id Id of timer to remove.
  244. *
  245. * @return TRUE if Capture Compare register must be updated, FALSE otherwise.
  246. */
  247. static bool timer_list_remove(timer_node_t * p_timer)
  248. {
  249. timer_node_t * p_old_head;
  250. timer_node_t * p_previous;
  251. timer_node_t * p_current;
  252. uint32_t timeout;
  253. // Find the timer's position in timer list.
  254. p_old_head = mp_timer_id_head;
  255. p_previous = mp_timer_id_head;
  256. p_current = p_previous;
  257. while (p_current != NULL)
  258. {
  259. if (p_current == p_timer)
  260. {
  261. break;
  262. }
  263. p_previous = p_current;
  264. p_current = p_current->next;
  265. }
  266. // Timer not in active list.
  267. if (p_current == NULL)
  268. {
  269. return false;
  270. }
  271. // Timer is the first in the list
  272. if (p_previous == p_current)
  273. {
  274. mp_timer_id_head = mp_timer_id_head->next;
  275. // No more timers in the list. Reset RTC1 in case Start timer operations are present in the queue.
  276. if (mp_timer_id_head == NULL)
  277. {
  278. NRF_RTC1->TASKS_CLEAR = 1;
  279. m_ticks_latest = 0;
  280. m_rtc1_reset = true;
  281. nrf_delay_us(MAX_RTC_TASKS_DELAY);
  282. }
  283. }
  284. // Remaining timeout between next timeout.
  285. timeout = p_current->ticks_to_expire;
  286. // Link previous timer with next of this timer, i.e. removing the timer from list.
  287. p_previous->next = p_current->next;
  288. // If this is not the last timer, increment the next timer by this timer timeout.
  289. p_current = p_previous->next;
  290. if (p_current != NULL)
  291. {
  292. p_current->ticks_to_expire += timeout;
  293. }
  294. return (p_old_head != mp_timer_id_head);
  295. }
  296. /**@brief Function for scheduling a check for timeouts by generating a RTC1 interrupt.
  297. */
  298. static void timer_timeouts_check_sched(void)
  299. {
  300. NVIC_SetPendingIRQ(RTC1_IRQn);
  301. }
  302. /**@brief Function for scheduling a timer list update by generating a SWI interrupt.
  303. */
  304. static void timer_list_handler_sched(void)
  305. {
  306. NVIC_SetPendingIRQ(SWI_IRQn);
  307. }
  308. #if APP_TIMER_CONFIG_USE_SCHEDULER
  309. static void timeout_handler_scheduled_exec(void * p_event_data, uint16_t event_size)
  310. {
  311. APP_ERROR_CHECK_BOOL(event_size == sizeof(app_timer_event_t));
  312. app_timer_event_t const * p_timer_event = (app_timer_event_t *)p_event_data;
  313. p_timer_event->timeout_handler(p_timer_event->p_context);
  314. }
  315. #endif
  316. /**@brief Function for executing an application timeout handler, either by calling it directly, or
  317. * by passing an event to the @ref app_scheduler.
  318. *
  319. * @param[in] p_timer Pointer to expired timer.
  320. */
  321. static void timeout_handler_exec(timer_node_t * p_timer)
  322. {
  323. #if APP_TIMER_CONFIG_USE_SCHEDULER
  324. app_timer_event_t timer_event;
  325. timer_event.timeout_handler = p_timer->p_timeout_handler;
  326. timer_event.p_context = p_timer->p_context;
  327. uint32_t err_code = app_sched_event_put(&timer_event, sizeof(timer_event), timeout_handler_scheduled_exec);
  328. APP_ERROR_CHECK(err_code);
  329. #else
  330. p_timer->p_timeout_handler(p_timer->p_context);
  331. #endif
  332. }
  333. /**@brief Function for checking for expired timers.
  334. */
  335. static void timer_timeouts_check(void)
  336. {
  337. // Handle expired of timer
  338. if (mp_timer_id_head != NULL)
  339. {
  340. timer_node_t * p_timer;
  341. timer_node_t * p_previous_timer;
  342. uint32_t ticks_elapsed;
  343. uint32_t ticks_expired;
  344. // Initialize actual elapsed ticks being consumed to 0.
  345. ticks_expired = 0;
  346. // ticks_elapsed is collected here, job will use it.
  347. ticks_elapsed = ticks_diff_get(rtc1_counter_get(), m_ticks_latest);
  348. // Auto variable containing the head of timers expiring.
  349. p_timer = mp_timer_id_head;
  350. // Expire all timers within ticks_elapsed and collect ticks_expired.
  351. while (p_timer != NULL)
  352. {
  353. // Do nothing if timer did not expire.
  354. if (ticks_elapsed < p_timer->ticks_to_expire)
  355. {
  356. break;
  357. }
  358. // Decrement ticks_elapsed and collect expired ticks.
  359. ticks_elapsed -= p_timer->ticks_to_expire;
  360. ticks_expired += p_timer->ticks_to_expire;
  361. // Move to next timer.
  362. p_previous_timer = p_timer;
  363. p_timer = p_timer->next;
  364. // Execute Task.
  365. if (p_previous_timer->is_running)
  366. {
  367. p_previous_timer->is_running = false;
  368. timeout_handler_exec(p_previous_timer);
  369. }
  370. }
  371. // Prepare to queue the ticks expired in the m_ticks_elapsed queue.
  372. if (m_ticks_elapsed_q_read_ind == m_ticks_elapsed_q_write_ind)
  373. {
  374. // The read index of the queue is equal to the write index. This means the new
  375. // value of ticks_expired should be stored at a new location in the m_ticks_elapsed
  376. // queue (which is implemented as a double buffer).
  377. // Check if there will be a queue overflow.
  378. if (++m_ticks_elapsed_q_write_ind == CONTEXT_QUEUE_SIZE_MAX)
  379. {
  380. // There will be a queue overflow. Hence the write index should point to the start
  381. // of the queue.
  382. m_ticks_elapsed_q_write_ind = 0;
  383. }
  384. }
  385. // Queue the ticks expired.
  386. m_ticks_elapsed[m_ticks_elapsed_q_write_ind] = ticks_expired;
  387. timer_list_handler_sched();
  388. }
  389. }
  390. /**@brief Function for acquiring the number of ticks elapsed.
  391. *
  392. * @param[out] p_ticks_elapsed Number of ticks elapsed.
  393. *
  394. * @return TRUE if elapsed ticks was read from queue, FALSE otherwise.
  395. */
  396. static bool elapsed_ticks_acquire(uint32_t * p_ticks_elapsed)
  397. {
  398. // Pick the elapsed value from queue.
  399. if (m_ticks_elapsed_q_read_ind != m_ticks_elapsed_q_write_ind)
  400. {
  401. // Dequeue elapsed value.
  402. m_ticks_elapsed_q_read_ind++;
  403. if (m_ticks_elapsed_q_read_ind == CONTEXT_QUEUE_SIZE_MAX)
  404. {
  405. m_ticks_elapsed_q_read_ind = 0;
  406. }
  407. *p_ticks_elapsed = m_ticks_elapsed[m_ticks_elapsed_q_read_ind];
  408. m_ticks_latest += *p_ticks_elapsed;
  409. m_ticks_latest &= MAX_RTC_COUNTER_VAL;
  410. return true;
  411. }
  412. else
  413. {
  414. // No elapsed value in queue.
  415. *p_ticks_elapsed = 0;
  416. return false;
  417. }
  418. }
  419. /**@brief Function for updating the timer list for expired timers.
  420. *
  421. * @param[in] ticks_elapsed Number of elapsed ticks.
  422. * @param[in] ticks_previous Previous known value of the RTC counter.
  423. * @param[out] p_restart_list_head List of repeating timers to be restarted.
  424. */
  425. static void expired_timers_handler(uint32_t ticks_elapsed,
  426. uint32_t ticks_previous,
  427. timer_node_t ** p_restart_list_head)
  428. {
  429. uint32_t ticks_expired = 0;
  430. while (mp_timer_id_head != NULL)
  431. {
  432. timer_node_t * p_timer;
  433. timer_node_t * p_timer_expired;
  434. // Auto variable for current timer node.
  435. p_timer = mp_timer_id_head;
  436. // Do nothing if timer did not expire
  437. if (ticks_elapsed < p_timer->ticks_to_expire)
  438. {
  439. p_timer->ticks_to_expire -= ticks_elapsed;
  440. break;
  441. }
  442. // Decrement ticks_elapsed and collect expired ticks.
  443. ticks_elapsed -= p_timer->ticks_to_expire;
  444. ticks_expired += p_timer->ticks_to_expire;
  445. // Timer expired, set ticks_to_expire zero.
  446. p_timer->ticks_to_expire = 0;
  447. // Remove the expired timer from head.
  448. p_timer_expired = mp_timer_id_head;
  449. mp_timer_id_head = p_timer->next;
  450. // Timer will be restarted if periodic.
  451. if (p_timer->ticks_periodic_interval != 0)
  452. {
  453. p_timer->ticks_at_start = (ticks_previous + ticks_expired) & MAX_RTC_COUNTER_VAL;
  454. p_timer->ticks_first_interval = p_timer->ticks_periodic_interval;
  455. p_timer->next = *p_restart_list_head;
  456. *p_restart_list_head = p_timer_expired;
  457. }
  458. }
  459. }
  460. /**@brief Function for handling timer list insertions.
  461. *
  462. * @param[in] p_restart_list_head List of repeating timers to be restarted.
  463. *
  464. * @return TRUE if Capture Compare register must be updated, FALSE otherwise.
  465. */
  466. static bool list_insertions_handler(timer_node_t * p_restart_list_head)
  467. {
  468. bool compare_update = false;
  469. timer_node_t * p_timer_id_old_head;
  470. // Remember the old head, so as to decide if new compare needs to be set.
  471. p_timer_id_old_head = mp_timer_id_head;
  472. // Handle insertions of timers.
  473. while ((p_restart_list_head != NULL) || (m_op_queue.first != m_op_queue.last))
  474. {
  475. timer_node_t * p_timer;
  476. if (p_restart_list_head != NULL)
  477. {
  478. p_timer = p_restart_list_head;
  479. p_restart_list_head = p_timer->next;
  480. }
  481. else
  482. {
  483. timer_user_op_t * p_user_op = &m_op_queue.user_op_queue[m_op_queue.first];
  484. m_op_queue.first++;
  485. if (m_op_queue.first == m_op_queue.size)
  486. {
  487. m_op_queue.first = 0;
  488. }
  489. p_timer = p_user_op->p_node;
  490. switch (p_user_op->op_type)
  491. {
  492. case TIMER_USER_OP_TYPE_STOP:
  493. // Delete node if timer is running.
  494. if (timer_list_remove(p_user_op->p_node))
  495. {
  496. compare_update = true;
  497. }
  498. p_timer->is_running = false;
  499. continue;
  500. case TIMER_USER_OP_TYPE_STOP_ALL:
  501. // Delete list of running timers, and mark all timers as not running.
  502. while (mp_timer_id_head != NULL)
  503. {
  504. timer_node_t * p_head = mp_timer_id_head;
  505. p_head->is_running = false;
  506. mp_timer_id_head = p_head->next;
  507. }
  508. continue;
  509. case TIMER_USER_OP_TYPE_START:
  510. break;
  511. default:
  512. // No implementation needed.
  513. continue;
  514. }
  515. if (p_timer->is_running)
  516. {
  517. continue;
  518. }
  519. p_timer->ticks_at_start = p_user_op->params.start.ticks_at_start;
  520. p_timer->ticks_first_interval = p_user_op->params.start.ticks_first_interval;
  521. p_timer->ticks_periodic_interval = p_user_op->params.start.ticks_periodic_interval;
  522. p_timer->p_context = p_user_op->params.start.p_context;
  523. if (m_rtc1_reset)
  524. {
  525. p_timer->ticks_at_start = 0;
  526. }
  527. }
  528. // Prepare the node to be inserted.
  529. if (
  530. ((p_timer->ticks_at_start - m_ticks_latest) & MAX_RTC_COUNTER_VAL)
  531. <
  532. (MAX_RTC_COUNTER_VAL / 2)
  533. )
  534. {
  535. p_timer->ticks_to_expire = ticks_diff_get(p_timer->ticks_at_start, m_ticks_latest) +
  536. p_timer->ticks_first_interval;
  537. }
  538. else
  539. {
  540. uint32_t delta_current_start;
  541. delta_current_start = ticks_diff_get(m_ticks_latest, p_timer->ticks_at_start);
  542. if (p_timer->ticks_first_interval > delta_current_start)
  543. {
  544. p_timer->ticks_to_expire = p_timer->ticks_first_interval - delta_current_start;
  545. }
  546. else
  547. {
  548. p_timer->ticks_to_expire = 0;
  549. }
  550. }
  551. p_timer->ticks_at_start = 0;
  552. p_timer->ticks_first_interval = 0;
  553. p_timer->is_running = true;
  554. p_timer->next = NULL;
  555. // Insert into list
  556. timer_list_insert(p_timer);
  557. }
  558. return (compare_update || (mp_timer_id_head != p_timer_id_old_head));
  559. }
  560. /**@brief Function for updating the Capture Compare register.
  561. */
  562. static void compare_reg_update(timer_node_t * p_timer_id_head_old)
  563. {
  564. // Setup the timeout for timers on the head of the list
  565. if (mp_timer_id_head != NULL)
  566. {
  567. uint32_t ticks_to_expire = mp_timer_id_head->ticks_to_expire;
  568. uint32_t pre_counter_val = rtc1_counter_get();
  569. uint32_t cc = m_ticks_latest;
  570. uint32_t ticks_elapsed = ticks_diff_get(pre_counter_val, cc) + RTC_COMPARE_OFFSET_MIN;
  571. if (!m_rtc1_running)
  572. {
  573. // No timers were already running, start RTC
  574. rtc1_start();
  575. }
  576. cc += (ticks_elapsed < ticks_to_expire) ? ticks_to_expire : ticks_elapsed;
  577. cc &= MAX_RTC_COUNTER_VAL;
  578. rtc1_compare0_set(cc);
  579. uint32_t post_counter_val = rtc1_counter_get();
  580. if (
  581. (ticks_diff_get(post_counter_val, pre_counter_val) + RTC_COMPARE_OFFSET_MIN)
  582. >
  583. ticks_diff_get(cc, pre_counter_val)
  584. )
  585. {
  586. // When this happens the COMPARE event may not be triggered by the RTC.
  587. // The nRF51 Series User Specification states that if the COUNTER value is N
  588. // (i.e post_counter_val = N), writing N or N + 1 to a CC register may not trigger a
  589. // COMPARE event. Hence the RTC interrupt is forcefully pended by calling the following
  590. // function.
  591. rtc1_compare0_set(rtc1_counter_get()); // this should prevent CC to fire again in the background while the code is in RTC-ISR
  592. nrf_delay_us(MAX_RTC_TASKS_DELAY);
  593. timer_timeouts_check_sched();
  594. }
  595. }
  596. else
  597. {
  598. #if (APP_TIMER_KEEPS_RTC_ACTIVE == 0)
  599. // No timers are running, stop RTC
  600. rtc1_stop();
  601. #endif //(APP_TIMER_KEEPS_RTC_ACTIVE == 0)
  602. }
  603. }
  604. /**@brief Function for handling changes to the timer list.
  605. */
  606. static void timer_list_handler(void)
  607. {
  608. timer_node_t * p_restart_list_head = NULL;
  609. uint32_t ticks_elapsed;
  610. uint32_t ticks_previous;
  611. bool ticks_have_elapsed;
  612. bool compare_update = false;
  613. timer_node_t * p_timer_id_head_old;
  614. #if APP_TIMER_WITH_PROFILER
  615. {
  616. uint8_t size = m_op_queue.size;
  617. uint8_t first = m_op_queue.first;
  618. uint8_t last = m_op_queue.last;
  619. uint8_t utilization = (first <= last) ? (last - first) : (size + 1 - first + last);
  620. if (utilization > m_max_user_op_queue_utilization)
  621. {
  622. m_max_user_op_queue_utilization = utilization;
  623. }
  624. }
  625. #endif
  626. // Back up the previous known tick and previous list head
  627. ticks_previous = m_ticks_latest;
  628. p_timer_id_head_old = mp_timer_id_head;
  629. // Get number of elapsed ticks
  630. ticks_have_elapsed = elapsed_ticks_acquire(&ticks_elapsed);
  631. // Handle expired timers
  632. if (ticks_have_elapsed)
  633. {
  634. expired_timers_handler(ticks_elapsed, ticks_previous, &p_restart_list_head);
  635. compare_update = true;
  636. }
  637. // Handle list insertions
  638. if (list_insertions_handler(p_restart_list_head))
  639. {
  640. compare_update = true;
  641. }
  642. // Update compare register if necessary
  643. if (compare_update)
  644. {
  645. compare_reg_update(p_timer_id_head_old);
  646. }
  647. m_rtc1_reset = false;
  648. }
  649. /**@brief Function for enqueueing a new operations queue entry.
  650. *
  651. * @param[in] last_index Index of the next last index to be enqueued.
  652. */
  653. static void user_op_enque(uint8_t last_index)
  654. {
  655. m_op_queue.last = last_index;
  656. }
  657. /**@brief Function for allocating a new operations queue entry.
  658. *
  659. * @param[out] p_last_index Index of the next last index to be enqueued.
  660. *
  661. * @return Pointer to allocated queue entry, or NULL if queue is full.
  662. */
  663. static timer_user_op_t * user_op_alloc( uint8_t * p_last_index)
  664. {
  665. uint8_t last;
  666. timer_user_op_t * p_user_op;
  667. last = m_op_queue.last + 1;
  668. if (last == m_op_queue.size)
  669. {
  670. // Overflow case.
  671. last = 0;
  672. }
  673. if (last == m_op_queue.first)
  674. {
  675. // Queue is full.
  676. return NULL;
  677. }
  678. *p_last_index = last;
  679. p_user_op = &m_op_queue.user_op_queue[m_op_queue.last];
  680. return p_user_op;
  681. }
  682. /**@brief Function for scheduling a Timer Start operation.
  683. *
  684. * @param[in] timer_id Id of timer to start.
  685. * @param[in] timeout_initial Time (in ticks) to first timer expiry.
  686. * @param[in] timeout_periodic Time (in ticks) between periodic expiries.
  687. * @param[in] p_context General purpose pointer. Will be passed to the timeout handler when
  688. * the timer expires.
  689. * @return NRF_SUCCESS on success, otherwise an error code.
  690. */
  691. static uint32_t timer_start_op_schedule(timer_node_t * p_node,
  692. uint32_t timeout_initial,
  693. uint32_t timeout_periodic,
  694. void * p_context)
  695. {
  696. uint8_t last_index;
  697. uint32_t err_code = NRF_SUCCESS;
  698. CRITICAL_REGION_ENTER();
  699. timer_user_op_t * p_user_op = user_op_alloc(&last_index);
  700. if (p_user_op == NULL)
  701. {
  702. err_code = NRF_ERROR_NO_MEM;
  703. }
  704. else
  705. {
  706. p_user_op->op_type = TIMER_USER_OP_TYPE_START;
  707. p_user_op->p_node = p_node;
  708. p_user_op->params.start.ticks_at_start = rtc1_counter_get();
  709. p_user_op->params.start.ticks_first_interval = timeout_initial;
  710. p_user_op->params.start.ticks_periodic_interval = timeout_periodic;
  711. p_user_op->params.start.p_context = p_context;
  712. user_op_enque(last_index);
  713. }
  714. CRITICAL_REGION_EXIT();
  715. if (err_code == NRF_SUCCESS)
  716. {
  717. timer_list_handler_sched();
  718. }
  719. return err_code;
  720. }
  721. /**@brief Function for scheduling a Timer Stop operation.
  722. *
  723. * @param[in] timer_id Id of timer to stop.
  724. * @param[in] op_type Type of stop operation
  725. *
  726. * @return NRF_SUCCESS on successful scheduling a timer stop operation. NRF_ERROR_NO_MEM when there
  727. * is no memory left to schedule the timer stop operation.
  728. */
  729. static uint32_t timer_stop_op_schedule(timer_node_t * p_node,
  730. timer_user_op_type_t op_type)
  731. {
  732. uint8_t last_index;
  733. uint32_t err_code = NRF_SUCCESS;
  734. CRITICAL_REGION_ENTER();
  735. timer_user_op_t * p_user_op = user_op_alloc(&last_index);
  736. if (p_user_op == NULL)
  737. {
  738. err_code = NRF_ERROR_NO_MEM;
  739. }
  740. else
  741. {
  742. p_user_op->op_type = op_type;
  743. p_user_op->p_node = p_node;
  744. user_op_enque(last_index);
  745. }
  746. CRITICAL_REGION_EXIT();
  747. if (err_code == NRF_SUCCESS)
  748. {
  749. timer_list_handler_sched();
  750. }
  751. return err_code;
  752. }
  753. /**@brief Function for handling the RTC1 interrupt.
  754. *
  755. * @details Checks for timeouts, and executes timeout handlers for expired timers.
  756. */
  757. void RTC1_IRQHandler(void)
  758. {
  759. // Clear all events (also unexpected ones)
  760. NRF_RTC1->EVENTS_COMPARE[0] = 0;
  761. NRF_RTC1->EVENTS_COMPARE[1] = 0;
  762. NRF_RTC1->EVENTS_COMPARE[2] = 0;
  763. NRF_RTC1->EVENTS_COMPARE[3] = 0;
  764. NRF_RTC1->EVENTS_TICK = 0;
  765. NRF_RTC1->EVENTS_OVRFLW = 0;
  766. // Check for expired timers
  767. timer_timeouts_check();
  768. }
  769. /**@brief Function for handling the SWI interrupt.
  770. *
  771. * @details Performs all updates to the timer list.
  772. */
  773. void SWI_IRQHandler(void)
  774. {
  775. timer_list_handler();
  776. }
  777. ret_code_t app_timer_init(void)
  778. {
  779. // Stop RTC to prevent any running timers from expiring (in case of reinitialization)
  780. rtc1_stop();
  781. // Initialize operation queue
  782. m_op_queue.first = 0;
  783. m_op_queue.last = 0;
  784. m_op_queue.size = APP_TIMER_CONFIG_OP_QUEUE_SIZE+1;
  785. mp_timer_id_head = NULL;
  786. m_ticks_elapsed_q_read_ind = 0;
  787. m_ticks_elapsed_q_write_ind = 0;
  788. #if APP_TIMER_WITH_PROFILER
  789. m_max_user_op_queue_utilization = 0;
  790. #endif
  791. NVIC_ClearPendingIRQ(SWI_IRQn);
  792. NVIC_SetPriority(SWI_IRQn, SWI_IRQ_PRI);
  793. NVIC_EnableIRQ(SWI_IRQn);
  794. rtc1_init(APP_TIMER_CONFIG_RTC_FREQUENCY);
  795. m_ticks_latest = rtc1_counter_get();
  796. return NRF_SUCCESS;
  797. }
  798. ret_code_t app_timer_create(app_timer_id_t const * p_timer_id,
  799. app_timer_mode_t mode,
  800. app_timer_timeout_handler_t timeout_handler)
  801. {
  802. // Check state and parameters
  803. VERIFY_MODULE_INITIALIZED();
  804. if (timeout_handler == NULL)
  805. {
  806. return NRF_ERROR_INVALID_PARAM;
  807. }
  808. if (p_timer_id == NULL)
  809. {
  810. return NRF_ERROR_INVALID_PARAM;
  811. }
  812. if (((timer_node_t*)*p_timer_id)->is_running)
  813. {
  814. return NRF_ERROR_INVALID_STATE;
  815. }
  816. timer_node_t * p_node = (timer_node_t *)*p_timer_id;
  817. p_node->is_running = false;
  818. p_node->mode = mode;
  819. p_node->p_timeout_handler = timeout_handler;
  820. return NRF_SUCCESS;
  821. }
  822. ret_code_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context)
  823. {
  824. uint32_t timeout_periodic;
  825. timer_node_t * p_node = (timer_node_t*)timer_id;
  826. // Check state and parameters
  827. VERIFY_MODULE_INITIALIZED();
  828. if (timer_id == 0)
  829. {
  830. return NRF_ERROR_INVALID_STATE;
  831. }
  832. if (timeout_ticks < APP_TIMER_MIN_TIMEOUT_TICKS)
  833. {
  834. return NRF_ERROR_INVALID_PARAM;
  835. }
  836. if (p_node->p_timeout_handler == NULL)
  837. {
  838. return NRF_ERROR_INVALID_STATE;
  839. }
  840. // Schedule timer start operation
  841. timeout_periodic = (p_node->mode == APP_TIMER_MODE_REPEATED) ? timeout_ticks : 0;
  842. return timer_start_op_schedule(p_node,
  843. timeout_ticks,
  844. timeout_periodic,
  845. p_context);
  846. }
  847. ret_code_t app_timer_stop(app_timer_id_t timer_id)
  848. {
  849. timer_node_t * p_node = (timer_node_t*)timer_id;
  850. // Check state and parameters
  851. VERIFY_MODULE_INITIALIZED();
  852. if ((timer_id == NULL) || (p_node->p_timeout_handler == NULL))
  853. {
  854. return NRF_ERROR_INVALID_STATE;
  855. }
  856. p_node->is_running = false;
  857. // Schedule timer stop operation
  858. return timer_stop_op_schedule(p_node, TIMER_USER_OP_TYPE_STOP);
  859. }
  860. ret_code_t app_timer_stop_all(void)
  861. {
  862. // Check state
  863. VERIFY_MODULE_INITIALIZED();
  864. return timer_stop_op_schedule(NULL, TIMER_USER_OP_TYPE_STOP_ALL);
  865. }
  866. uint32_t app_timer_cnt_get(void)
  867. {
  868. return rtc1_counter_get();
  869. }
  870. uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to,
  871. uint32_t ticks_from)
  872. {
  873. return ticks_diff_get(ticks_to, ticks_from);
  874. }
  875. #if APP_TIMER_WITH_PROFILER
  876. uint8_t app_timer_op_queue_utilization_get(void)
  877. {
  878. return m_max_user_op_queue_utilization;
  879. }
  880. #endif
  881. void app_timer_pause(void)
  882. {
  883. NRF_RTC1->TASKS_STOP = 1;
  884. }
  885. void app_timer_resume(void)
  886. {
  887. NRF_RTC1->TASKS_START = 1;
  888. }
  889. #endif //NRF_MODULE_ENABLED(APP_TIMER)