app_timer2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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 "app_timer.h"
  41. #include "nrf_atfifo.h"
  42. #include "nrf_sortlist.h"
  43. #include "nrf_delay.h"
  44. #if APP_TIMER_WITH_PROFILER
  45. #include "app_util_platform.h"
  46. #endif
  47. #if APP_TIMER_CONFIG_USE_SCHEDULER
  48. #include "app_scheduler.h"
  49. #endif
  50. #include <stddef.h>
  51. #define NRF_LOG_MODULE_NAME APP_TIMER_LOG_NAME
  52. #if APP_TIMER_CONFIG_LOG_ENABLED
  53. #define NRF_LOG_LEVEL APP_TIMER_CONFIG_LOG_LEVEL
  54. #define NRF_LOG_INFO_COLOR APP_TIMER_CONFIG_INFO_COLOR
  55. #define NRF_LOG_DEBUG_COLOR APP_TIMER_CONFIG_DEBUG_COLOR
  56. #else //APP_TIMER_CONFIG_LOG_ENABLED
  57. #define NRF_LOG_LEVEL 0
  58. #endif //APP_TIMER_CONFIG_LOG_ENABLED
  59. #include "nrf_log.h"
  60. NRF_LOG_MODULE_REGISTER();
  61. #include "drv_rtc.h"
  62. /**
  63. * Maximum possible relative value is limited by safe window to detect cases when requested
  64. * compare event has already occured.
  65. */
  66. #define APP_TIMER_SAFE_WINDOW APP_TIMER_TICKS(APP_TIMER_SAFE_WINDOW_MS)
  67. #define APP_TIMER_RTC_MAX_VALUE (DRV_RTC_MAX_CNT - APP_TIMER_SAFE_WINDOW)
  68. static drv_rtc_t m_rtc_inst = DRV_RTC_INSTANCE(1);
  69. #if APP_TIMER_WITH_PROFILER
  70. static uint8_t m_max_user_op_queue_utilization; /**< Maximum observed timer user operations queue utilization. */
  71. static uint8_t m_current_user_op_queue_utilization; /**< Currently observed timer user operations queue utilization. */
  72. #endif /* APP_TIMER_WITH_PROFILER */
  73. /**
  74. * @brief Timer requests types.
  75. */
  76. typedef enum
  77. {
  78. TIMER_REQ_START,
  79. TIMER_REQ_STOP,
  80. TIMER_REQ_STOP_ALL
  81. } app_timer_req_type_t;
  82. /**
  83. * @brief Operation request structure.
  84. */
  85. typedef struct
  86. {
  87. app_timer_req_type_t type; /**< Request type. */
  88. app_timer_t * p_timer; /**< Timer instance. */
  89. } timer_req_t;
  90. static app_timer_t * volatile mp_active_timer; /**< Timer currently handled by RTC driver. */
  91. static bool m_global_active; /**< Flag used to globally disable all timers. */
  92. static uint64_t m_base_counter;
  93. static uint64_t m_stamp64;
  94. /* Request FIFO instance. */
  95. NRF_ATFIFO_DEF(m_req_fifo, timer_req_t, APP_TIMER_CONFIG_OP_QUEUE_SIZE);
  96. /* Sortlist instance. */
  97. static bool compare_func(nrf_sortlist_item_t * p_item0, nrf_sortlist_item_t *p_item1);
  98. NRF_SORTLIST_DEF(m_app_timer_sortlist, compare_func); /**< Sortlist used for storing queued timers. */
  99. /**
  100. * @brief Return current 64 bit timestamp
  101. */
  102. static uint64_t get_now(void)
  103. {
  104. uint64_t now = m_base_counter + drv_rtc_counter_get(&m_rtc_inst);
  105. /* it is possible that base was not updated and overflow occured, in that case 'now' will be
  106. * 24bit value behind. Additional timestamp updated on every 24 bit period is used to detect
  107. * that case. Apart from that 'now' should never be behind previously read timestamp.
  108. */
  109. if (now < m_stamp64) {
  110. now += (DRV_RTC_MAX_CNT + 1);
  111. }
  112. return now;
  113. }
  114. /**
  115. * @brief Function used for comparing items in sorted list.
  116. */
  117. static inline bool compare_func(nrf_sortlist_item_t * p_item0, nrf_sortlist_item_t *p_item1)
  118. {
  119. app_timer_t * p0 = CONTAINER_OF(p_item0, app_timer_t, list_item);
  120. app_timer_t * p1 = CONTAINER_OF(p_item1, app_timer_t, list_item);
  121. uint64_t p0_end = p0->end_val;
  122. uint64_t p1_end = p1->end_val;
  123. return (p0_end <= p1_end) ? true : false;
  124. }
  125. #if APP_TIMER_CONFIG_USE_SCHEDULER
  126. static void scheduled_timeout_handler(void * p_event_data, uint16_t event_size)
  127. {
  128. ASSERT(event_size == sizeof(app_timer_event_t));
  129. app_timer_event_t const * p_timer_event = (app_timer_event_t *)p_event_data;
  130. p_timer_event->timeout_handler(p_timer_event->p_context);
  131. }
  132. #endif
  133. /**
  134. * @brief Function called on timer expiration
  135. * If end value is not reached it is assumed that it was partial expiration and time is put back
  136. * into the list. Otherwise function calls user handler if timer was not stopped before. If timer
  137. * is in repeated mode then timer is rescheduled.
  138. *
  139. * @param p_timer Timer instance.
  140. *
  141. * @return True if reevaluation of sortlist needed (becasue it was updated).
  142. */
  143. static bool timer_expire(app_timer_t * p_timer)
  144. {
  145. ASSERT(p_timer->handler);
  146. bool ret = false;
  147. if ((m_global_active == true) && (p_timer != NULL) && (p_timer->active))
  148. {
  149. if (get_now() >= p_timer->end_val) {
  150. /* timer expired */
  151. if (p_timer->repeat_period == 0)
  152. {
  153. p_timer->active = false;
  154. }
  155. #if APP_TIMER_CONFIG_USE_SCHEDULER
  156. app_timer_event_t timer_event;
  157. timer_event.timeout_handler = p_timer->handler;
  158. timer_event.p_context = p_timer->p_context;
  159. uint32_t err_code = app_sched_event_put(&timer_event,
  160. sizeof(timer_event),
  161. scheduled_timeout_handler);
  162. APP_ERROR_CHECK(err_code);
  163. #else
  164. NRF_LOG_DEBUG("Timer expired (context: %d)", (uint32_t)p_timer->p_context)
  165. p_timer->handler(p_timer->p_context);
  166. #endif
  167. /* check active flag as it may have been stopped in the user handler */
  168. if ((p_timer->repeat_period) && (p_timer->active))
  169. {
  170. p_timer->end_val += p_timer->repeat_period;
  171. nrf_sortlist_add(&m_app_timer_sortlist, &p_timer->list_item);
  172. ret = true;
  173. }
  174. }
  175. else
  176. {
  177. nrf_sortlist_add(&m_app_timer_sortlist, &p_timer->list_item);
  178. ret = true;
  179. }
  180. }
  181. return ret;
  182. }
  183. /**
  184. * @brief Function is configuring RTC driver to trigger timeout interrupt for given timer.
  185. *
  186. * It is possible that RTC driver will indicate that timeout already occured. In that case timer
  187. * expires and function indicates that RTC was not configured.
  188. *
  189. * @param p_timer Timer instance.
  190. * @param [in,out] p_rerun Flag indicating that sortlist reevaluation is required.
  191. *
  192. * @return True if RTC was successfully configured, false if timer already expired and RTC was not
  193. * configured.
  194. *
  195. */
  196. static bool rtc_schedule(app_timer_t * p_timer, bool * p_rerun)
  197. {
  198. ret_code_t ret = NRF_ERROR_TIMEOUT;
  199. *p_rerun = false;
  200. int64_t remaining = (int64_t)(p_timer->end_val - get_now());
  201. if (remaining > 0) {
  202. uint32_t cc_val = ((uint32_t)remaining > APP_TIMER_RTC_MAX_VALUE) ?
  203. (app_timer_cnt_get() + APP_TIMER_RTC_MAX_VALUE) : p_timer->end_val;
  204. ret = drv_rtc_windowed_compare_set(&m_rtc_inst, 0, cc_val, APP_TIMER_SAFE_WINDOW);
  205. NRF_LOG_DEBUG("Setting CC to 0x%08x (err: %d)", cc_val & DRV_RTC_MAX_CNT, ret);
  206. if (ret == NRF_SUCCESS)
  207. {
  208. return true;
  209. }
  210. }
  211. else
  212. {
  213. drv_rtc_compare_disable(&m_rtc_inst, 0);
  214. }
  215. if (ret == NRF_ERROR_TIMEOUT)
  216. {
  217. *p_rerun = timer_expire(p_timer);
  218. }
  219. else
  220. {
  221. NRF_LOG_ERROR("Unexpected error: %d", ret);
  222. ASSERT(0);
  223. }
  224. return false;
  225. }
  226. static inline app_timer_t * sortlist_pop(void)
  227. {
  228. nrf_sortlist_item_t * p_next_item = nrf_sortlist_pop(&m_app_timer_sortlist);
  229. return p_next_item ? CONTAINER_OF(p_next_item, app_timer_t, list_item) : NULL;
  230. }
  231. static inline app_timer_t * sortlist_peek(void)
  232. {
  233. nrf_sortlist_item_t const * p_next_item = nrf_sortlist_peek(&m_app_timer_sortlist);
  234. return p_next_item ? CONTAINER_OF(p_next_item, app_timer_t, list_item) : NULL;
  235. }
  236. /**
  237. * @brief Function for deactivating all timers which are in the sorted list (active timers).
  238. */
  239. static void sorted_list_stop_all(void)
  240. {
  241. app_timer_t * p_next;
  242. do
  243. {
  244. p_next = sortlist_pop();
  245. if (p_next)
  246. {
  247. p_next->active = false;
  248. }
  249. } while (p_next);
  250. }
  251. /**
  252. * @brief Function for handling RTC counter overflow.
  253. *
  254. * Increment base counter used to calculate 64 bit timestamp.
  255. */
  256. static void on_overflow_evt(void)
  257. {
  258. NRF_LOG_DEBUG("Overflow EVT");
  259. m_base_counter += (DRV_RTC_MAX_CNT + 1);
  260. }
  261. /**
  262. * #brief Function for handling RTC compare event - active timer expiration.
  263. */
  264. static void on_compare_evt(drv_rtc_t const * const p_instance)
  265. {
  266. if (mp_active_timer)
  267. {
  268. /* If assert fails it suggests that safe window should be increased. */
  269. ASSERT(app_timer_cnt_diff_compute(drv_rtc_counter_get(p_instance),
  270. drv_rtc_compare_get(p_instance, 0)) < APP_TIMER_SAFE_WINDOW);
  271. NRF_LOG_INST_DEBUG(mp_active_timer->p_log, "Compare EVT");
  272. UNUSED_RETURN_VALUE(timer_expire(mp_active_timer));
  273. mp_active_timer = NULL;
  274. }
  275. else
  276. {
  277. NRF_LOG_WARNING("Compare event but no active timer (already stopped?)");
  278. }
  279. }
  280. /**
  281. * @brief Channel 1 is triggered in the middle of 24 bit period to updated control timestamp in
  282. * place where there is no risk of overflow.
  283. */
  284. static void on_compare1_evt(drv_rtc_t const * const p_instance)
  285. {
  286. m_stamp64 = get_now();
  287. }
  288. /**
  289. * @brief Function updates RTC.
  290. *
  291. * Function is called at the end of RTC interrupt when all new user request and/or timer expiration
  292. * occured. It configures RTC if there is any pending timer, reconfigures if the are timers with
  293. * shorted timeout than active one or stops RTC if there is no active timers.
  294. */
  295. static void rtc_update(drv_rtc_t const * const p_instance)
  296. {
  297. while(1)
  298. {
  299. app_timer_t * p_next = sortlist_peek();
  300. bool rtc_reconf = false;
  301. if (p_next) //Candidate for active timer
  302. {
  303. if (mp_active_timer == NULL)
  304. {
  305. //There is no active timer so candidate will become active timer.
  306. rtc_reconf = true;
  307. }
  308. else if (p_next->end_val < mp_active_timer->end_val)
  309. {
  310. //Candidate has shorter timeout than current active timer. Candidate will replace active timer.
  311. //Active timer is put back into sorted list.
  312. rtc_reconf = true;
  313. if (mp_active_timer->active)
  314. {
  315. NRF_LOG_INST_DEBUG(mp_active_timer->p_log, "Timer preempted.");
  316. nrf_sortlist_add(&m_app_timer_sortlist, &mp_active_timer->list_item);
  317. }
  318. }
  319. if (rtc_reconf)
  320. {
  321. bool rerun;
  322. p_next = sortlist_pop();
  323. NRF_LOG_INST_DEBUG(p_next->p_log, "Activating timer (CC:%d/%08x).", p_next->end_val, p_next->end_val);
  324. if (rtc_schedule(p_next, &rerun))
  325. {
  326. if (!APP_TIMER_KEEPS_RTC_ACTIVE && (mp_active_timer == NULL))
  327. {
  328. drv_rtc_start(p_instance);
  329. }
  330. mp_active_timer = p_next;
  331. if (rerun == false)
  332. {
  333. //RTC was successfully updated and sortlist was not updated. Function can be terminated.
  334. break;
  335. }
  336. }
  337. else
  338. {
  339. //If RTC driver indicated that timeout already occured a new candidate will be taken from sorted list.
  340. NRF_LOG_INST_DEBUG(p_next->p_log,"Timer expired before scheduled to RTC.");
  341. mp_active_timer = NULL;
  342. }
  343. }
  344. else
  345. {
  346. //RTC will not be updated. Function can terminate.
  347. break;
  348. }
  349. }
  350. else //No candidate for active timer.
  351. {
  352. if (!APP_TIMER_KEEPS_RTC_ACTIVE && (mp_active_timer == NULL))
  353. {
  354. drv_rtc_stop(p_instance);
  355. }
  356. break;
  357. }
  358. }
  359. }
  360. /**
  361. * @brief Function for processing user requests.
  362. *
  363. * Function is called only in the context of RTC interrupt.
  364. */
  365. static void timer_req_process(drv_rtc_t const * const p_instance)
  366. {
  367. nrf_atfifo_item_get_t fifo_ctx;
  368. timer_req_t * p_req = nrf_atfifo_item_get(m_req_fifo, &fifo_ctx);
  369. while (p_req)
  370. {
  371. switch (p_req->type)
  372. {
  373. case TIMER_REQ_START:
  374. if (!p_req->p_timer->active)
  375. {
  376. p_req->p_timer->active = true;
  377. nrf_sortlist_add(&m_app_timer_sortlist, &(p_req->p_timer->list_item));
  378. NRF_LOG_INST_DEBUG(p_req->p_timer->p_log,"Start request (expiring at %d/0x%08x).",
  379. p_req->p_timer->end_val, p_req->p_timer->end_val);
  380. }
  381. break;
  382. case TIMER_REQ_STOP:
  383. if (p_req->p_timer == mp_active_timer)
  384. {
  385. mp_active_timer = NULL;
  386. }
  387. else
  388. {
  389. bool found = nrf_sortlist_remove(&m_app_timer_sortlist, &(p_req->p_timer->list_item));
  390. if (!found)
  391. {
  392. NRF_LOG_INFO("Timer not found on sortlist (stopping expired timer).");
  393. }
  394. }
  395. NRF_LOG_INST_DEBUG(p_req->p_timer->p_log,"Stop request.");
  396. break;
  397. case TIMER_REQ_STOP_ALL:
  398. sorted_list_stop_all();
  399. m_global_active = true;
  400. NRF_LOG_INFO("Stop all request.");
  401. break;
  402. default:
  403. break;
  404. }
  405. #if APP_TIMER_WITH_PROFILER
  406. CRITICAL_REGION_ENTER();
  407. #endif
  408. UNUSED_RETURN_VALUE(nrf_atfifo_item_free(m_req_fifo, &fifo_ctx));
  409. #if APP_TIMER_WITH_PROFILER
  410. if (m_max_user_op_queue_utilization < m_current_user_op_queue_utilization)
  411. {
  412. m_max_user_op_queue_utilization = m_current_user_op_queue_utilization;
  413. }
  414. --m_current_user_op_queue_utilization;
  415. CRITICAL_REGION_EXIT();
  416. #endif /* APP_TIMER_WITH_PROFILER */
  417. p_req = nrf_atfifo_item_get(m_req_fifo, &fifo_ctx);
  418. }
  419. }
  420. static void rtc_irq(drv_rtc_t const * const p_instance)
  421. {
  422. if (drv_rtc_overflow_pending(p_instance))
  423. {
  424. on_overflow_evt();
  425. }
  426. if (drv_rtc_compare_pending(p_instance, 0))
  427. {
  428. on_compare_evt(p_instance);
  429. }
  430. if (drv_rtc_compare_pending(p_instance, 1))
  431. {
  432. on_compare1_evt(p_instance);
  433. }
  434. timer_req_process(p_instance);
  435. rtc_update(p_instance);
  436. }
  437. /**
  438. * @brief Function for triggering processing user requests.
  439. *
  440. * @note All user requests are processed in a single context - RTC interrupt.
  441. */
  442. static inline void timer_request_proc_trigger(void)
  443. {
  444. drv_rtc_irq_trigger(&m_rtc_inst);
  445. }
  446. /**
  447. * @brief Function for putting user request into the request queue
  448. */
  449. static ret_code_t timer_req_schedule(app_timer_req_type_t type, app_timer_t * p_timer)
  450. {
  451. nrf_atfifo_item_put_t fifo_ctx;
  452. timer_req_t * p_req;
  453. #if APP_TIMER_WITH_PROFILER
  454. CRITICAL_REGION_ENTER();
  455. #endif
  456. p_req = nrf_atfifo_item_alloc(m_req_fifo, &fifo_ctx);
  457. #if APP_TIMER_WITH_PROFILER
  458. if (p_req)
  459. {
  460. ++m_current_user_op_queue_utilization;
  461. }
  462. CRITICAL_REGION_EXIT();
  463. #endif /* APP_TIMER_WITH_PROFILER */
  464. if (p_req)
  465. {
  466. p_req->type = type;
  467. p_req->p_timer = p_timer;
  468. if (nrf_atfifo_item_put(m_req_fifo, &fifo_ctx))
  469. {
  470. timer_request_proc_trigger();
  471. }
  472. else
  473. {
  474. NRF_LOG_WARNING("Scheduling interrupted another scheduling.");
  475. }
  476. return NRF_SUCCESS;
  477. }
  478. else
  479. {
  480. return NRF_ERROR_NO_MEM;
  481. }
  482. }
  483. ret_code_t app_timer_init(void)
  484. {
  485. ret_code_t err_code;
  486. drv_rtc_config_t config = {
  487. .prescaler = APP_TIMER_CONFIG_RTC_FREQUENCY,
  488. .interrupt_priority = APP_TIMER_CONFIG_IRQ_PRIORITY
  489. };
  490. err_code = NRF_ATFIFO_INIT(m_req_fifo);
  491. if (err_code != NRFX_SUCCESS)
  492. {
  493. return err_code;
  494. }
  495. err_code = drv_rtc_init(&m_rtc_inst, &config, rtc_irq);
  496. if (err_code != NRFX_SUCCESS)
  497. {
  498. return err_code;
  499. }
  500. drv_rtc_overflow_enable(&m_rtc_inst, true);
  501. drv_rtc_compare_set(&m_rtc_inst, 1, DRV_RTC_MAX_CNT >> 1, true);
  502. if (APP_TIMER_KEEPS_RTC_ACTIVE)
  503. {
  504. drv_rtc_start(&m_rtc_inst);
  505. }
  506. m_global_active = true;
  507. return err_code;
  508. }
  509. ret_code_t app_timer_create(app_timer_id_t const * p_timer_id,
  510. app_timer_mode_t mode,
  511. app_timer_timeout_handler_t timeout_handler)
  512. {
  513. ASSERT(p_timer_id);
  514. ASSERT(timeout_handler);
  515. if (timeout_handler == NULL)
  516. {
  517. return NRF_ERROR_INVALID_PARAM;
  518. }
  519. app_timer_t * p_t = (app_timer_t *) *p_timer_id;
  520. p_t->handler = timeout_handler;
  521. p_t->repeat_period = (mode == APP_TIMER_MODE_REPEATED) ? 1 : 0;
  522. return NRF_SUCCESS;
  523. }
  524. ret_code_t app_timer_start(app_timer_t * p_timer, uint32_t timeout_ticks, void * p_context)
  525. {
  526. ASSERT(p_timer);
  527. app_timer_t * p_t = (app_timer_t *) p_timer;
  528. if (p_t->active)
  529. {
  530. return NRF_SUCCESS;
  531. }
  532. p_t->p_context = p_context;
  533. p_t->end_val = get_now() + timeout_ticks;
  534. if (p_t->repeat_period)
  535. {
  536. p_t->repeat_period = timeout_ticks;
  537. }
  538. return timer_req_schedule(TIMER_REQ_START, p_t);
  539. }
  540. ret_code_t app_timer_stop(app_timer_t * p_timer)
  541. {
  542. ASSERT(p_timer);
  543. app_timer_t * p_t = (app_timer_t *) p_timer;
  544. p_t->active = false;
  545. return timer_req_schedule(TIMER_REQ_STOP, p_t);
  546. }
  547. ret_code_t app_timer_stop_all(void)
  548. {
  549. //block timer globally
  550. m_global_active = false;
  551. return timer_req_schedule(TIMER_REQ_STOP_ALL, NULL);
  552. }
  553. #if APP_TIMER_WITH_PROFILER
  554. uint8_t app_timer_op_queue_utilization_get(void)
  555. {
  556. return m_max_user_op_queue_utilization;
  557. }
  558. #endif /* APP_TIMER_WITH_PROFILER */
  559. uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to,
  560. uint32_t ticks_from)
  561. {
  562. return ((ticks_to - ticks_from) & RTC_COUNTER_COUNTER_Msk);
  563. }
  564. uint32_t app_timer_cnt_get(void)
  565. {
  566. return drv_rtc_counter_get(&m_rtc_inst);
  567. }
  568. void app_timer_pause(void)
  569. {
  570. drv_rtc_stop(&m_rtc_inst);
  571. }
  572. void app_timer_resume(void)
  573. {
  574. drv_rtc_start(&m_rtc_inst);
  575. }