app_timer_freertos.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. * Copyright (c) 2014 - 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 "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(APP_TIMER)
  42. #include "FreeRTOS.h"
  43. #include "task.h"
  44. #include "timers.h"
  45. #include "app_timer.h"
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include "nrf.h"
  49. #include "app_error.h"
  50. /**
  51. * Note that this implementation is made only for enable SDK components which interacts with app_timer to work with FreeRTOS.
  52. * It is more suitable to use native FreeRTOS timer for other purposes.
  53. */
  54. /* Check if RTC FreeRTOS version is used */
  55. #if configTICK_SOURCE != FREERTOS_USE_RTC
  56. #error app_timer in FreeRTOS variant have to be used with RTC tick source configuration. Default configuration have to be used in other case.
  57. #endif
  58. /**
  59. * @brief Waiting time for the timer queue
  60. *
  61. * Number of system ticks to wait for the timer queue to put the message.
  62. * It is strongly recommended to set this to the value bigger than 1.
  63. * In other case if timer message queue is full - any operation on timer may fail.
  64. * @note
  65. * Timer functions called from interrupt context would never wait.
  66. */
  67. #define APP_TIMER_WAIT_FOR_QUEUE 2
  68. /**@brief This structure keeps information about osTimer.*/
  69. typedef struct
  70. {
  71. void * argument;
  72. TimerHandle_t osHandle;
  73. app_timer_timeout_handler_t func;
  74. /**
  75. * This member is to make sure that timer function is only called if timer is running.
  76. * FreeRTOS may have timer running even after stop function is called,
  77. * because it processes commands in Timer task and stopping function only puts command into the queue. */
  78. bool active;
  79. bool single_shot;
  80. }app_timer_info_t;
  81. /* Check if freeRTOS timers are activated */
  82. #if configUSE_TIMERS == 0
  83. #error app_timer for freeRTOS requires configUSE_TIMERS option to be activated.
  84. #endif
  85. /* Check if app_timer_t variable type can held our app_timer_info_t structure */
  86. STATIC_ASSERT(sizeof(app_timer_info_t) <= sizeof(app_timer_t));
  87. /**
  88. * @brief Internal callback function for the system timer
  89. *
  90. * Internal function that is called from the system timer.
  91. * It gets our parameter from timer data and sends it to user function.
  92. * @param[in] xTimer Timer handler
  93. */
  94. static void app_timer_callback(TimerHandle_t xTimer)
  95. {
  96. app_timer_info_t * pinfo = (app_timer_info_t*)(pvTimerGetTimerID(xTimer));
  97. ASSERT(pinfo->osHandle == xTimer);
  98. ASSERT(pinfo->func != NULL);
  99. if (pinfo->active)
  100. {
  101. pinfo->active = (pinfo->single_shot) ? false : true;
  102. pinfo->func(pinfo->argument);
  103. }
  104. }
  105. uint32_t app_timer_init(void)
  106. {
  107. return NRF_SUCCESS;
  108. }
  109. uint32_t app_timer_create(app_timer_id_t const * p_timer_id,
  110. app_timer_mode_t mode,
  111. app_timer_timeout_handler_t timeout_handler)
  112. {
  113. app_timer_info_t * pinfo = (app_timer_info_t*)(*p_timer_id);
  114. uint32_t err_code = NRF_SUCCESS;
  115. unsigned long timer_mode;
  116. if ((timeout_handler == NULL) || (p_timer_id == NULL))
  117. {
  118. return NRF_ERROR_INVALID_PARAM;
  119. }
  120. if (pinfo->active)
  121. {
  122. return NRF_ERROR_INVALID_STATE;
  123. }
  124. if (pinfo->osHandle == NULL)
  125. {
  126. /* New timer is created */
  127. memset(pinfo, 0, sizeof(app_timer_info_t));
  128. timer_mode = (mode == APP_TIMER_MODE_SINGLE_SHOT) ? pdFALSE : pdTRUE;
  129. pinfo->single_shot = (mode == APP_TIMER_MODE_SINGLE_SHOT);
  130. pinfo->func = timeout_handler;
  131. pinfo->osHandle = xTimerCreate(" ", 1000, timer_mode, pinfo, app_timer_callback);
  132. if (pinfo->osHandle == NULL)
  133. err_code = NRF_ERROR_NULL;
  134. }
  135. else
  136. {
  137. /* Timer cannot be reinitialized using FreeRTOS API */
  138. return NRF_ERROR_INVALID_STATE;
  139. }
  140. return err_code;
  141. }
  142. uint32_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context)
  143. {
  144. app_timer_info_t * pinfo = (app_timer_info_t*)(timer_id);
  145. TimerHandle_t hTimer = pinfo->osHandle;
  146. if (hTimer == NULL)
  147. {
  148. return NRF_ERROR_INVALID_STATE;
  149. }
  150. if (pinfo->active)
  151. {
  152. // Timer already running - exit silently
  153. return NRF_SUCCESS;
  154. }
  155. pinfo->argument = p_context;
  156. if (__get_IPSR() != 0)
  157. {
  158. BaseType_t yieldReq = pdFALSE;
  159. if (xTimerChangePeriodFromISR(hTimer, timeout_ticks, &yieldReq) != pdPASS)
  160. {
  161. return NRF_ERROR_NO_MEM;
  162. }
  163. if ( xTimerStartFromISR(hTimer, &yieldReq) != pdPASS )
  164. {
  165. return NRF_ERROR_NO_MEM;
  166. }
  167. portYIELD_FROM_ISR(yieldReq);
  168. }
  169. else
  170. {
  171. if (xTimerChangePeriod(hTimer, timeout_ticks, APP_TIMER_WAIT_FOR_QUEUE) != pdPASS)
  172. {
  173. return NRF_ERROR_NO_MEM;
  174. }
  175. if (xTimerStart(hTimer, APP_TIMER_WAIT_FOR_QUEUE) != pdPASS)
  176. {
  177. return NRF_ERROR_NO_MEM;
  178. }
  179. }
  180. pinfo->active = true;
  181. return NRF_SUCCESS;
  182. }
  183. uint32_t app_timer_stop(app_timer_id_t timer_id)
  184. {
  185. app_timer_info_t * pinfo = (app_timer_info_t*)(timer_id);
  186. TimerHandle_t hTimer = pinfo->osHandle;
  187. if (hTimer == NULL)
  188. {
  189. return NRF_ERROR_INVALID_STATE;
  190. }
  191. if (__get_IPSR() != 0)
  192. {
  193. BaseType_t yieldReq = pdFALSE;
  194. if (xTimerStopFromISR(hTimer, &yieldReq) != pdPASS)
  195. {
  196. return NRF_ERROR_NO_MEM;
  197. }
  198. portYIELD_FROM_ISR(yieldReq);
  199. }
  200. else
  201. {
  202. if (xTimerStop(hTimer, APP_TIMER_WAIT_FOR_QUEUE) != pdPASS)
  203. {
  204. return NRF_ERROR_NO_MEM;
  205. }
  206. }
  207. pinfo->active = false;
  208. return NRF_SUCCESS;
  209. }
  210. #endif //NRF_MODULE_ENABLED(APP_TIMER)