app_timer.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /**
  2. * Copyright (c) 2012 - 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. /** @file
  41. *
  42. * @defgroup app_timer Application Timer
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief Application timer functionality.
  47. *
  48. * @details This module enables the application to create multiple timer instances based on the RTC1
  49. * peripheral. Checking for time-outs and invocation of user time-out handlers is performed
  50. * in the RTC1 interrupt handler. List handling is done using a software interrupt (SWI0).
  51. * Both interrupt handlers are running in APP_LOW priority level.
  52. *
  53. * @details When calling app_timer_start() or app_timer_stop(), the timer operation is just queued,
  54. * and the software interrupt is triggered. The actual timer start/stop operation is
  55. * executed by the SWI0 interrupt handler. Since the SWI0 interrupt is running in APP_LOW,
  56. * if the application code calling the timer function is running in APP_LOW or APP_HIGH,
  57. * the timer operation will not be performed until the application handler has returned.
  58. * This will be the case, for example, when stopping a timer from a time-out handler when not using
  59. * the scheduler.
  60. *
  61. * @details Use the USE_SCHEDULER parameter of the APP_TIMER_INIT() macro to select if the
  62. * @ref app_scheduler should be used or not. Even if the scheduler is
  63. * not used, app_timer.h will include app_scheduler.h, so when
  64. * compiling, app_scheduler.h must be available in one of the compiler include paths.
  65. */
  66. #ifndef APP_TIMER_H__
  67. #define APP_TIMER_H__
  68. #include "sdk_config.h"
  69. #include "app_error.h"
  70. #include "app_util.h"
  71. #include "compiler_abstraction.h"
  72. #include "nordic_common.h"
  73. #ifdef APP_TIMER_V2
  74. #include "nrf_log_instance.h"
  75. #include "nrf_sortlist.h"
  76. #endif
  77. #include <stdint.h>
  78. #include <stdbool.h>
  79. #include <stdio.h>
  80. #ifdef __cplusplus
  81. extern "C" {
  82. #endif
  83. /** @brief Name of the module used for logger messaging.
  84. */
  85. #define APP_TIMER_LOG_NAME app_timer
  86. #define APP_TIMER_CLOCK_FREQ 32768 /**< Clock frequency of the RTC timer used to implement the app timer module. */
  87. #define APP_TIMER_MIN_TIMEOUT_TICKS 5 /**< Minimum value of the timeout_ticks parameter of app_timer_start(). */
  88. #ifdef RTX
  89. #define APP_TIMER_NODE_SIZE 40 /**< Size of app_timer.timer_node_t (used to allocate data). */
  90. #else
  91. #define APP_TIMER_NODE_SIZE 32 /**< Size of app_timer.timer_node_t (used to allocate data). */
  92. #endif // RTX
  93. #define APP_TIMER_SCHED_EVENT_DATA_SIZE sizeof(app_timer_event_t) /**< Size of event data when scheduler is used. */
  94. #define APP_TIMER_MAX_CNT_VAL RTC_COUNTER_COUNTER_Msk /**< Maximum counter value that can be returned by @ref app_timer_cnt_get. */
  95. /**@brief Convert milliseconds to timer ticks.
  96. *
  97. * This macro uses 64-bit integer arithmetic, but as long as the macro parameters are
  98. * constants (i.e. defines), the computation will be done by the preprocessor.
  99. *
  100. * @param[in] MS Milliseconds.
  101. *
  102. * @return Number of timer ticks.
  103. */
  104. #ifndef FREERTOS
  105. #define APP_TIMER_TICKS(MS) \
  106. ((uint32_t)ROUNDED_DIV( \
  107. (MS) * (uint64_t)APP_TIMER_CLOCK_FREQ, \
  108. 1000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1)))
  109. #else
  110. #include "FreeRTOSConfig.h"
  111. #define APP_TIMER_TICKS(MS) (uint32_t)ROUNDED_DIV((MS)*configTICK_RATE_HZ,1000)
  112. #endif
  113. /**
  114. * @brief Create a timer identifier and statically allocate memory for the timer.
  115. *
  116. * @param timer_id Name of the timer identifier variable that will be used to control the timer.
  117. */
  118. #define APP_TIMER_DEF(timer_id) _APP_TIMER_DEF(timer_id)
  119. /**@brief Application time-out handler type. */
  120. typedef void (*app_timer_timeout_handler_t)(void * p_context);
  121. #ifdef APP_TIMER_V2
  122. /**
  123. * @brief app_timer control block
  124. */
  125. typedef struct
  126. {
  127. nrf_sortlist_item_t list_item; /**< Token used by sortlist. */
  128. uint64_t end_val; /**< RTC counter value when timer expires. */
  129. uint32_t repeat_period; /**< Repeat period (0 if single shot mode). */
  130. app_timer_timeout_handler_t handler; /**< User handler. */
  131. void * p_context; /**< User context. */
  132. NRF_LOG_INSTANCE_PTR_DECLARE(p_log) /**< Pointer to instance of the logger object (Conditionally compiled). */
  133. volatile bool active; /**< Flag indicating that timer is active. */
  134. } app_timer_t;
  135. /**@brief Timer ID type.
  136. * Never declare a variable of this type, but use the macro @ref APP_TIMER_DEF instead.*/
  137. typedef app_timer_t * app_timer_id_t;
  138. #define _APP_TIMER_DEF(timer_id) \
  139. NRF_LOG_INSTANCE_REGISTER(APP_TIMER_LOG_NAME, timer_id, \
  140. APP_TIMER_CONFIG_INFO_COLOR, \
  141. APP_TIMER_CONFIG_DEBUG_COLOR, \
  142. APP_TIMER_CONFIG_INITIAL_LOG_LEVEL, \
  143. APP_TIMER_CONFIG_LOG_ENABLED ? \
  144. APP_TIMER_CONFIG_LOG_LEVEL : NRF_LOG_SEVERITY_NONE); \
  145. static app_timer_t CONCAT_2(timer_id,_data) = { \
  146. .active = false, \
  147. NRF_LOG_INSTANCE_PTR_INIT(p_log, APP_TIMER_LOG_NAME, timer_id) \
  148. }; \
  149. static const app_timer_id_t timer_id = &CONCAT_2(timer_id,_data)
  150. #else //APP_TIMER_V2
  151. typedef struct app_timer_t { uint32_t data[CEIL_DIV(APP_TIMER_NODE_SIZE, sizeof(uint32_t))]; } app_timer_t;
  152. /**@brief Timer ID type.
  153. * Never declare a variable of this type, but use the macro @ref APP_TIMER_DEF instead.*/
  154. typedef app_timer_t * app_timer_id_t;
  155. #define _APP_TIMER_DEF(timer_id) \
  156. static app_timer_t CONCAT_2(timer_id,_data) = { {0} }; \
  157. static const app_timer_id_t timer_id = &CONCAT_2(timer_id,_data)
  158. #endif
  159. /**@brief Structure passed to app_scheduler. */
  160. typedef struct
  161. {
  162. app_timer_timeout_handler_t timeout_handler;
  163. void * p_context;
  164. } app_timer_event_t;
  165. /**@brief Timer modes. */
  166. typedef enum
  167. {
  168. APP_TIMER_MODE_SINGLE_SHOT, /**< The timer will expire only once. */
  169. APP_TIMER_MODE_REPEATED /**< The timer will restart each time it expires. */
  170. } app_timer_mode_t;
  171. /**@brief Function for initializing the timer module.
  172. *
  173. * @retval NRF_SUCCESS If the module was initialized successfully.
  174. */
  175. ret_code_t app_timer_init(void);
  176. /**@brief Function for creating a timer instance.
  177. *
  178. * @param[in] p_timer_id Pointer to timer identifier.
  179. * @param[in] mode Timer mode.
  180. * @param[in] timeout_handler Function to be executed when the timer expires.
  181. *
  182. * @retval NRF_SUCCESS If the timer was successfully created.
  183. * @retval NRF_ERROR_INVALID_PARAM If a parameter was invalid.
  184. * @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized or
  185. * the timer is running.
  186. *
  187. * @note This function does the timer allocation in the caller's context. It is also not protected
  188. * by a critical region. Therefore care must be taken not to call it from several interrupt
  189. * levels simultaneously.
  190. * @note The function can be called again on the timer instance and will re-initialize the instance if
  191. * the timer is not running.
  192. * @attention The FreeRTOS and RTX app_timer implementation does not allow app_timer_create to
  193. * be called on the previously initialized instance.
  194. */
  195. ret_code_t app_timer_create(app_timer_id_t const * p_timer_id,
  196. app_timer_mode_t mode,
  197. app_timer_timeout_handler_t timeout_handler);
  198. /**@brief Function for starting a timer.
  199. *
  200. * @param[in] timer_id Timer identifier.
  201. * @param[in] timeout_ticks Number of ticks (of RTC1, including prescaling) to time-out event
  202. * (minimum 5 ticks).
  203. * @param[in] p_context General purpose pointer. Will be passed to the time-out handler when
  204. * the timer expires.
  205. *
  206. * @retval NRF_SUCCESS If the timer was successfully started.
  207. * @retval NRF_ERROR_INVALID_PARAM If a parameter was invalid.
  208. * @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized or the timer
  209. * has not been created.
  210. * @retval NRF_ERROR_NO_MEM If the timer operations queue was full.
  211. *
  212. * @note The minimum timeout_ticks value is 5.
  213. * @note For multiple active timers, time-outs occurring in close proximity to each other (in the
  214. * range of 1 to 3 ticks) will have a positive jitter of maximum 3 ticks.
  215. * @note When calling this method on a timer that is already running, the second start operation
  216. * is ignored.
  217. */
  218. ret_code_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context);
  219. /**@brief Function for stopping the specified timer.
  220. *
  221. * @param[in] timer_id Timer identifier.
  222. *
  223. * @retval NRF_SUCCESS If the timer was successfully stopped.
  224. * @retval NRF_ERROR_INVALID_PARAM If a parameter was invalid.
  225. * @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized or the timer
  226. * has not been created.
  227. * @retval NRF_ERROR_NO_MEM If the timer operations queue was full.
  228. */
  229. ret_code_t app_timer_stop(app_timer_id_t timer_id);
  230. /**@brief Function for stopping all running timers.
  231. *
  232. * @retval NRF_SUCCESS If all timers were successfully stopped.
  233. * @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized.
  234. * @retval NRF_ERROR_NO_MEM If the timer operations queue was full.
  235. */
  236. ret_code_t app_timer_stop_all(void);
  237. /**@brief Function for returning the current value of the RTC1 counter.
  238. *
  239. * @return Current value of the RTC1 counter.
  240. */
  241. uint32_t app_timer_cnt_get(void);
  242. /**@brief Function for computing the difference between two RTC1 counter values.
  243. *
  244. * @param[in] ticks_to Value returned by app_timer_cnt_get().
  245. * @param[in] ticks_from Value returned by app_timer_cnt_get().
  246. *
  247. * @return Number of ticks from ticks_from to ticks_to.
  248. */
  249. uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to,
  250. uint32_t ticks_from);
  251. /**@brief Function for getting the maximum observed operation queue utilization.
  252. *
  253. * Function for tuning the module and determining OP_QUEUE_SIZE value and thus module RAM usage.
  254. *
  255. * @note APP_TIMER_WITH_PROFILER must be enabled to use this functionality.
  256. *
  257. * @return Maximum number of events in queue observed so far.
  258. */
  259. uint8_t app_timer_op_queue_utilization_get(void);
  260. /**
  261. * @brief Function for pausing RTC activity which drives app_timer.
  262. *
  263. * @note This function can be used for debugging purposes to ensure
  264. * that application is halted when entering a breakpoint.
  265. */
  266. void app_timer_pause(void);
  267. /**
  268. * @brief Function for resuming RTC activity which drives app_timer.
  269. *
  270. * @note This function can be used for debugging purposes to resume
  271. * application activity.
  272. */
  273. void app_timer_resume(void);
  274. #ifdef __cplusplus
  275. }
  276. #endif
  277. #endif // APP_TIMER_H__
  278. /** @} */