app_timer.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /** @file
  13. *
  14. * @defgroup app_timer Application Timer
  15. * @{
  16. * @ingroup app_common
  17. *
  18. * @brief Application timer functionality.
  19. *
  20. * @details This module enables the application to create multiple timer instances based on the RTC1
  21. * peripheral. Checking for time-outs and invokation of user time-out handlers is performed
  22. * in the RTC1 interrupt handler. List handling is done using a software interrupt (SWI0).
  23. * Both interrupt handlers are running in APP_LOW priority level.
  24. *
  25. * @details When calling app_timer_start() or app_timer_stop(), the timer operation is just queued,
  26. * and the software interrupt is triggered. The actual timer start/stop operation is
  27. * executed by the SWI0 interrupt handler. Since the SWI0 interrupt is running in APP_LOW,
  28. * if the application code calling the timer function is running in APP_LOW or APP_HIGH,
  29. * the timer operation will not be performed until the application handler has returned.
  30. * This will be the case, for example, when stopping a timer from a time-out handler when not using
  31. * the scheduler.
  32. *
  33. * @details Use the USE_SCHEDULER parameter of the APP_TIMER_INIT() macro to select if the
  34. * @ref app_scheduler should be used or not. Even if the scheduler is
  35. * not used, app_timer.h will include app_scheduler.h, so when
  36. * compiling, app_scheduler.h must be available in one of the compiler include paths.
  37. */
  38. #ifndef APP_TIMER_H__
  39. #define APP_TIMER_H__
  40. #include <stdint.h>
  41. #include <stdbool.h>
  42. #include <stdio.h>
  43. #include "app_error.h"
  44. #include "app_util.h"
  45. #include "compiler_abstraction.h"
  46. #define APP_TIMER_CLOCK_FREQ 32768 /**< Clock frequency of the RTC timer used to implement the app timer module. */
  47. #define APP_TIMER_MIN_TIMEOUT_TICKS 5 /**< Minimum value of the timeout_ticks parameter of app_timer_start(). */
  48. #define APP_TIMER_NODE_SIZE 32 /**< Size of app_timer.timer_node_t (used to allocate data). */
  49. #define APP_TIMER_USER_OP_SIZE 24 /**< Size of app_timer.timer_user_op_t (only for use inside APP_TIMER_BUF_SIZE()). */
  50. #define APP_TIMER_USER_SIZE 8 /**< Size of app_timer.timer_user_t (only for use inside APP_TIMER_BUF_SIZE()). */
  51. #define APP_TIMER_INT_LEVELS 3 /**< Number of interrupt levels from where timer operations may be initiated (only for use inside APP_TIMER_BUF_SIZE()). */
  52. /**@brief Compute number of bytes required to hold the application timer data structures.
  53. *
  54. * @param[in] OP_QUEUE_SIZE Size of queues holding timer operations that are pending execution.
  55. * Note that due to the queue implementation, this size must be one more
  56. * than the size that is actually needed.
  57. *
  58. * @return Required application timer buffer size (in bytes).
  59. */
  60. #define APP_TIMER_BUF_SIZE(OP_QUEUE_SIZE) \
  61. ( \
  62. ( \
  63. APP_TIMER_INT_LEVELS \
  64. * \
  65. (APP_TIMER_USER_SIZE + ((OP_QUEUE_SIZE) + 1) * APP_TIMER_USER_OP_SIZE) \
  66. ) \
  67. )
  68. /**@brief Convert milliseconds to timer ticks.
  69. *
  70. * This macro uses 64-bit integer arithmetic, but as long as the macro parameters are
  71. * constants (i.e. defines), the computation will be done by the preprocessor.
  72. *
  73. * When using this macro, ensure that the
  74. * values provided as input result in an output value that is supported by the
  75. * @ref app_timer_start function. For example, when the ticks for 1 ms is needed, the
  76. * maximum possible value of PRESCALER must be 6, when @ref APP_TIMER_CLOCK_FREQ is 32768.
  77. * This will result in a ticks value as 5. Any higher value for PRESCALER will result in a
  78. * ticks value that is not supported by this module.
  79. *
  80. * @param[in] MS Milliseconds.
  81. * @param[in] PRESCALER Value of the RTC1 PRESCALER register (must be the same value that was
  82. * passed to APP_TIMER_INIT()).
  83. *
  84. * @return Number of timer ticks.
  85. */
  86. #define APP_TIMER_TICKS(MS, PRESCALER)\
  87. ((uint32_t)ROUNDED_DIV((MS) * (uint64_t)APP_TIMER_CLOCK_FREQ, ((PRESCALER) + 1) * 1000))
  88. typedef struct app_timer_t { uint32_t data[CEIL_DIV(APP_TIMER_NODE_SIZE, sizeof(uint32_t))]; } app_timer_t;
  89. /**@brief Timer ID type.
  90. * Never declare a variable of this type, but use the macro @ref APP_TIMER_DEF instead.*/
  91. typedef app_timer_t * app_timer_id_t;
  92. /**
  93. * @brief Create a timer identifier and statically allocate memory for the timer.
  94. *
  95. * @param timer_id Name of the timer identifier variable that will be used to control the timer.
  96. */
  97. #define APP_TIMER_DEF(timer_id) \
  98. static app_timer_t timer_id##_data = { {0} }; \
  99. static const app_timer_id_t timer_id = &timer_id##_data
  100. /**@brief Application time-out handler type. */
  101. typedef void (*app_timer_timeout_handler_t)(void * p_context);
  102. /**@brief Type of function for passing events from the timer module to the scheduler. */
  103. typedef uint32_t (*app_timer_evt_schedule_func_t) (app_timer_timeout_handler_t timeout_handler,
  104. void * p_context);
  105. /**@brief Timer modes. */
  106. typedef enum
  107. {
  108. APP_TIMER_MODE_SINGLE_SHOT, /**< The timer will expire only once. */
  109. APP_TIMER_MODE_REPEATED /**< The timer will restart each time it expires. */
  110. } app_timer_mode_t;
  111. /**@brief Initialize the application timer module.
  112. *
  113. * @details This macro handles dimensioning and allocation of the memory buffer required by the timer,
  114. * making sure that the buffer is correctly aligned. It will also connect the timer module
  115. * to the scheduler (if specified).
  116. *
  117. * @note This module assumes that the LFCLK is already running. If it is not, the module will
  118. * be non-functional, since the RTC will not run. If you do not use a SoftDevice, you
  119. * must start the LFCLK manually. See the rtc_example's lfclk_config() function
  120. * for an example of how to do this. If you use a SoftDevice, the LFCLK is started on
  121. * SoftDevice init.
  122. *
  123. *
  124. * @param[in] PRESCALER Value of the RTC1 PRESCALER register. This will decide the
  125. * timer tick rate. Set to 0 for no prescaling.
  126. * @param[in] OP_QUEUES_SIZE Size of queues holding timer operations that are pending execution.
  127. * @param[in] SCHEDULER_FUNC Pointer to scheduler event handler
  128. *
  129. * @note Since this macro allocates a buffer, it must only be called once (it is OK to call it
  130. * several times as long as it is from the same location, for example, to do a re-initialization).
  131. */
  132. /*lint -emacro(506, APP_TIMER_INIT) */ /* Suppress "Constant value Boolean */
  133. #define APP_TIMER_INIT(PRESCALER, OP_QUEUES_SIZE, SCHEDULER_FUNC) \
  134. do \
  135. { \
  136. static uint32_t APP_TIMER_BUF[CEIL_DIV(APP_TIMER_BUF_SIZE((OP_QUEUES_SIZE) + 1), \
  137. sizeof(uint32_t))]; \
  138. uint32_t ERR_CODE = app_timer_init((PRESCALER), \
  139. (OP_QUEUES_SIZE) + 1, \
  140. APP_TIMER_BUF, \
  141. SCHEDULER_FUNC); \
  142. APP_ERROR_CHECK(ERR_CODE); \
  143. } while (0)
  144. /**@brief Function for initializing the timer module.
  145. *
  146. * Normally, initialization should be done using the APP_TIMER_INIT() macro, because that macro will both
  147. * allocate the buffers needed by the timer module (including aligning the buffers correctly)
  148. * and take care of connecting the timer module to the scheduler (if specified).
  149. *
  150. * @param[in] prescaler Value of the RTC1 PRESCALER register. Set to 0 for no prescaling.
  151. * @param[in] op_queues_size Size of queues holding timer operations that are pending
  152. * execution. Note that due to the queue implementation, this size must
  153. * be one more than the size that is actually needed.
  154. * @param[in] p_buffer Pointer to memory buffer for internal use in the app_timer
  155. * module. The size of the buffer can be computed using the
  156. * APP_TIMER_BUF_SIZE() macro. The buffer must be aligned to a
  157. * 4 byte boundary.
  158. * @param[in] evt_schedule_func Function for passing time-out events to the scheduler. Point to
  159. * app_timer_evt_schedule() to connect to the scheduler. Set to NULL
  160. * to make the timer module call the time-out handler directly from
  161. * the timer interrupt handler.
  162. *
  163. * @retval NRF_SUCCESS If the module was initialized successfully.
  164. * @retval NRF_ERROR_INVALID_PARAM If a parameter was invalid (buffer not aligned to a 4 byte
  165. * boundary or NULL).
  166. */
  167. uint32_t app_timer_init(uint32_t prescaler,
  168. uint8_t op_queues_size,
  169. void * p_buffer,
  170. app_timer_evt_schedule_func_t evt_schedule_func);
  171. /**@brief Function for creating a timer instance.
  172. *
  173. * @param[in] p_timer_id Pointer to timer identifier.
  174. * @param[in] mode Timer mode.
  175. * @param[in] timeout_handler Function to be executed when the timer expires.
  176. *
  177. * @retval NRF_SUCCESS If the timer was successfully created.
  178. * @retval NRF_ERROR_INVALID_PARAM If a parameter was invalid.
  179. * @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized or
  180. * the timer is running.
  181. *
  182. * @note This function does the timer allocation in the caller's context. It is also not protected
  183. * by a critical region. Therefore care must be taken not to call it from several interrupt
  184. * levels simultaneously.
  185. * @note The function can be called again on the timer instance and will re-initialize the instance if
  186. * the timer is not running.
  187. * @attention The FreeRTOS and RTX app_timer implementation does not allow app_timer_create to
  188. * be called on the previously initialized instance.
  189. */
  190. uint32_t app_timer_create(app_timer_id_t const * p_timer_id,
  191. app_timer_mode_t mode,
  192. app_timer_timeout_handler_t timeout_handler);
  193. /**@brief Function for starting a timer.
  194. *
  195. * @param[in] timer_id Timer identifier.
  196. * @param[in] timeout_ticks Number of ticks (of RTC1, including prescaling) to time-out event
  197. * (minimum 5 ticks).
  198. * @param[in] p_context General purpose pointer. Will be passed to the time-out handler when
  199. * the timer expires.
  200. *
  201. * @retval NRF_SUCCESS If the timer was successfully started.
  202. * @retval NRF_ERROR_INVALID_PARAM If a parameter was invalid.
  203. * @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized or the timer
  204. * has not been created.
  205. * @retval NRF_ERROR_NO_MEM If the timer operations queue was full.
  206. *
  207. * @note The minimum timeout_ticks value is 5.
  208. * @note For multiple active timers, time-outs occurring in close proximity to each other (in the
  209. * range of 1 to 3 ticks) will have a positive jitter of maximum 3 ticks.
  210. * @note When calling this method on a timer that is already running, the second start operation
  211. * is ignored.
  212. */
  213. uint32_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context);
  214. /**@brief Function for stopping the specified timer.
  215. *
  216. * @param[in] timer_id Timer identifier.
  217. *
  218. * @retval NRF_SUCCESS If the timer was successfully stopped.
  219. * @retval NRF_ERROR_INVALID_PARAM If a parameter was invalid.
  220. * @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized or the timer
  221. * has not been created.
  222. * @retval NRF_ERROR_NO_MEM If the timer operations queue was full.
  223. */
  224. uint32_t app_timer_stop(app_timer_id_t timer_id);
  225. /**@brief Function for stopping all running timers.
  226. *
  227. * @retval NRF_SUCCESS If all timers were successfully stopped.
  228. * @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized.
  229. * @retval NRF_ERROR_NO_MEM If the timer operations queue was full.
  230. */
  231. uint32_t app_timer_stop_all(void);
  232. /**@brief Function for returning the current value of the RTC1 counter.
  233. *
  234. * @param[out] p_ticks Current value of the RTC1 counter.
  235. *
  236. * @retval NRF_SUCCESS If the counter was successfully read.
  237. */
  238. uint32_t app_timer_cnt_get(uint32_t * p_ticks);
  239. /**@brief Function for computing the difference between two RTC1 counter values.
  240. *
  241. * @param[in] ticks_to Value returned by app_timer_cnt_get().
  242. * @param[in] ticks_from Value returned by app_timer_cnt_get().
  243. * @param[out] p_ticks_diff Number of ticks from ticks_from to ticks_to.
  244. *
  245. * @retval NRF_SUCCESS If the counter difference was successfully computed.
  246. */
  247. uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to,
  248. uint32_t ticks_from,
  249. uint32_t * p_ticks_diff);
  250. #ifdef APP_TIMER_WITH_PROFILER
  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. * @return Maximum number of events in queue observed so far.
  256. */
  257. uint16_t app_timer_op_queue_utilization_get(void);
  258. #endif
  259. #endif // APP_TIMER_H__
  260. /** @} */