app_timer_freertos.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Copyright (c) 2014 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. #include "FreeRTOS.h"
  13. #include "task.h"
  14. #include "timers.h"
  15. #include "app_timer.h"
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "nrf.h"
  19. #include "app_error.h"
  20. #include "app_util.h"
  21. #include "nordic_common.h"
  22. /* Check if RTC FreeRTOS version is used */
  23. #if configTICK_SOURCE != FREERTOS_USE_RTC
  24. #error app_timer in FreeRTOS variant have to be used with RTC tick source configuration. Default configuration have to be used in other case.
  25. #endif
  26. /**
  27. * @brief Waiting time for the timer queue
  28. *
  29. * Number of system ticks to wait for the timer queue to put the message.
  30. * It is strongly recommended to set this to the value bigger than 1.
  31. * In other case if timer message queue is full - any operation on timer may fail.
  32. * @note
  33. * Timer functions called from interrupt context would never wait.
  34. */
  35. #define APP_TIMER_WAIT_FOR_QUEUE 2
  36. /**@brief This structure keeps information about osTimer.*/
  37. typedef struct
  38. {
  39. void * argument;
  40. TimerHandle_t osHandle;
  41. app_timer_timeout_handler_t func;
  42. /**
  43. * This member is to make sure that timer function is only called if timer is running.
  44. * FreeRTOS may have timer running even after stop function is called,
  45. * because it processes commands in Timer task and stopping function only puts command into the queue. */
  46. bool active;
  47. }app_timer_info_t;
  48. /**
  49. * @brief Prescaler that was set by the user
  50. *
  51. * In FreeRTOS version of app_timer the prescaler setting is constant and done by the operating system.
  52. * But the application expect the prescaler to be set according to value given in setup and then
  53. * calculate required ticks using this value.
  54. * For compatibility we remember the value set and use it for recalculation of required timer setting.
  55. */
  56. static uint32_t m_prescaler;
  57. /* Check if freeRTOS timers are activated */
  58. #if configUSE_TIMERS == 0
  59. #error app_timer for freeRTOS requires configUSE_TIMERS option to be activated.
  60. #endif
  61. /* Check if app_timer_t variable type can held our app_timer_info_t structure */
  62. STATIC_ASSERT(sizeof(app_timer_info_t) <= sizeof(app_timer_t));
  63. /**
  64. * @brief Internal callback function for the system timer
  65. *
  66. * Internal function that is called from the system timer.
  67. * It gets our parameter from timer data and sends it to user function.
  68. * @param[in] xTimer Timer handler
  69. */
  70. static void app_timer_callback(TimerHandle_t xTimer)
  71. {
  72. app_timer_info_t * pinfo = (app_timer_info_t*)(pvTimerGetTimerID(xTimer));
  73. ASSERT(pinfo->osHandle == xTimer);
  74. ASSERT(pinfo->func != NULL);
  75. if(pinfo->active)
  76. pinfo->func(pinfo->argument);
  77. }
  78. uint32_t app_timer_init(uint32_t prescaler,
  79. uint8_t op_queues_size,
  80. void * p_buffer,
  81. app_timer_evt_schedule_func_t evt_schedule_func)
  82. {
  83. UNUSED_PARAMETER(op_queues_size);
  84. UNUSED_PARAMETER(p_buffer);
  85. UNUSED_PARAMETER(evt_schedule_func);
  86. m_prescaler = prescaler + 1;
  87. return NRF_SUCCESS;
  88. }
  89. uint32_t app_timer_create(app_timer_id_t const * p_timer_id,
  90. app_timer_mode_t mode,
  91. app_timer_timeout_handler_t timeout_handler)
  92. {
  93. app_timer_info_t * pinfo = (app_timer_info_t*)(*p_timer_id);
  94. uint32_t err_code = NRF_SUCCESS;
  95. unsigned long timer_mode;
  96. if((timeout_handler == NULL) || (p_timer_id == NULL))
  97. {
  98. return NRF_ERROR_INVALID_PARAM;
  99. }
  100. if(pinfo->active)
  101. {
  102. return NRF_ERROR_INVALID_STATE;
  103. }
  104. if(pinfo->osHandle == NULL)
  105. {
  106. /* New timer is created */
  107. memset(pinfo, 0, sizeof(pinfo));
  108. if(mode == APP_TIMER_MODE_SINGLE_SHOT)
  109. timer_mode = pdFALSE;
  110. else
  111. timer_mode = pdTRUE;
  112. pinfo->func = timeout_handler;
  113. pinfo->osHandle = xTimerCreate(" ", 1000, timer_mode, pinfo, app_timer_callback);
  114. if(pinfo->osHandle == NULL)
  115. err_code = NRF_ERROR_NULL;
  116. }
  117. else
  118. {
  119. /* Timer cannot be reinitialized using FreeRTOS API */
  120. return NRF_ERROR_INVALID_STATE;
  121. }
  122. return err_code;
  123. }
  124. uint32_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context)
  125. {
  126. app_timer_info_t * pinfo = (app_timer_info_t*)(timer_id);
  127. TimerHandle_t hTimer = pinfo->osHandle;
  128. uint32_t rtc_prescaler = portNRF_RTC_REG->PRESCALER + 1;
  129. /* Get back the microseconds to wait */
  130. uint32_t timeout_corrected = ROUNDED_DIV(timeout_ticks*m_prescaler, rtc_prescaler);
  131. if(hTimer == NULL)
  132. {
  133. return NRF_ERROR_INVALID_STATE;
  134. }
  135. if(pinfo->active && (xTimerIsTimerActive(hTimer) != pdFALSE))
  136. {
  137. // Timer already running - exit silently
  138. return NRF_SUCCESS;
  139. }
  140. pinfo->argument = p_context;
  141. if(__get_IPSR() != 0)
  142. {
  143. BaseType_t yieldReq = pdFALSE;
  144. if(xTimerChangePeriodFromISR(hTimer, timeout_corrected, &yieldReq) != pdPASS)
  145. {
  146. return NRF_ERROR_NO_MEM;
  147. }
  148. if( xTimerStartFromISR(hTimer, &yieldReq) != pdPASS )
  149. {
  150. return NRF_ERROR_NO_MEM;
  151. }
  152. portYIELD_FROM_ISR(yieldReq);
  153. }
  154. else
  155. {
  156. if(xTimerChangePeriod(hTimer, timeout_corrected, APP_TIMER_WAIT_FOR_QUEUE) != pdPASS)
  157. {
  158. return NRF_ERROR_NO_MEM;
  159. }
  160. if(xTimerStart(hTimer, APP_TIMER_WAIT_FOR_QUEUE) != pdPASS)
  161. {
  162. return NRF_ERROR_NO_MEM;
  163. }
  164. }
  165. pinfo->active = true;
  166. return NRF_SUCCESS;
  167. }
  168. uint32_t app_timer_stop(app_timer_id_t timer_id)
  169. {
  170. app_timer_info_t * pinfo = (app_timer_info_t*)(timer_id);
  171. TimerHandle_t hTimer = pinfo->osHandle;
  172. if(hTimer == NULL)
  173. {
  174. return NRF_ERROR_INVALID_STATE;
  175. }
  176. if(__get_IPSR() != 0)
  177. {
  178. BaseType_t yieldReq = pdFALSE;
  179. if(xTimerStopFromISR(timer_id, &yieldReq) != pdPASS)
  180. {
  181. return NRF_ERROR_NO_MEM;
  182. }
  183. portYIELD_FROM_ISR(yieldReq);
  184. }
  185. else
  186. {
  187. if(xTimerStop(timer_id, APP_TIMER_WAIT_FOR_QUEUE) != pdPASS)
  188. {
  189. return NRF_ERROR_NO_MEM;
  190. }
  191. }
  192. pinfo->active = false;
  193. return NRF_SUCCESS;
  194. }