app_timer_freertos.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * Copyright (c) 2014 - 2018, 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. }app_timer_info_t;
  80. /* Check if freeRTOS timers are activated */
  81. #if configUSE_TIMERS == 0
  82. #error app_timer for freeRTOS requires configUSE_TIMERS option to be activated.
  83. #endif
  84. /* Check if app_timer_t variable type can held our app_timer_info_t structure */
  85. STATIC_ASSERT(sizeof(app_timer_info_t) <= sizeof(app_timer_t));
  86. /**
  87. * @brief Internal callback function for the system timer
  88. *
  89. * Internal function that is called from the system timer.
  90. * It gets our parameter from timer data and sends it to user function.
  91. * @param[in] xTimer Timer handler
  92. */
  93. static void app_timer_callback(TimerHandle_t xTimer)
  94. {
  95. app_timer_info_t * pinfo = (app_timer_info_t*)(pvTimerGetTimerID(xTimer));
  96. ASSERT(pinfo->osHandle == xTimer);
  97. ASSERT(pinfo->func != NULL);
  98. if (pinfo->active)
  99. pinfo->func(pinfo->argument);
  100. }
  101. uint32_t app_timer_init(void)
  102. {
  103. return NRF_SUCCESS;
  104. }
  105. uint32_t app_timer_create(app_timer_id_t const * p_timer_id,
  106. app_timer_mode_t mode,
  107. app_timer_timeout_handler_t timeout_handler)
  108. {
  109. app_timer_info_t * pinfo = (app_timer_info_t*)(*p_timer_id);
  110. uint32_t err_code = NRF_SUCCESS;
  111. unsigned long timer_mode;
  112. if ((timeout_handler == NULL) || (p_timer_id == NULL))
  113. {
  114. return NRF_ERROR_INVALID_PARAM;
  115. }
  116. if (pinfo->active)
  117. {
  118. return NRF_ERROR_INVALID_STATE;
  119. }
  120. if (pinfo->osHandle == NULL)
  121. {
  122. /* New timer is created */
  123. memset(pinfo, 0, sizeof(app_timer_info_t));
  124. if (mode == APP_TIMER_MODE_SINGLE_SHOT)
  125. timer_mode = pdFALSE;
  126. else
  127. timer_mode = pdTRUE;
  128. pinfo->func = timeout_handler;
  129. pinfo->osHandle = xTimerCreate(" ", 1000, timer_mode, pinfo, app_timer_callback);
  130. if (pinfo->osHandle == NULL)
  131. err_code = NRF_ERROR_NULL;
  132. }
  133. else
  134. {
  135. /* Timer cannot be reinitialized using FreeRTOS API */
  136. return NRF_ERROR_INVALID_STATE;
  137. }
  138. return err_code;
  139. }
  140. uint32_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context)
  141. {
  142. app_timer_info_t * pinfo = (app_timer_info_t*)(timer_id);
  143. TimerHandle_t hTimer = pinfo->osHandle;
  144. if (hTimer == NULL)
  145. {
  146. return NRF_ERROR_INVALID_STATE;
  147. }
  148. if (pinfo->active && (xTimerIsTimerActive(hTimer) != pdFALSE))
  149. {
  150. // Timer already running - exit silently
  151. return NRF_SUCCESS;
  152. }
  153. pinfo->argument = p_context;
  154. if (__get_IPSR() != 0)
  155. {
  156. BaseType_t yieldReq = pdFALSE;
  157. if (xTimerChangePeriodFromISR(hTimer, timeout_ticks, &yieldReq) != pdPASS)
  158. {
  159. return NRF_ERROR_NO_MEM;
  160. }
  161. if ( xTimerStartFromISR(hTimer, &yieldReq) != pdPASS )
  162. {
  163. return NRF_ERROR_NO_MEM;
  164. }
  165. portYIELD_FROM_ISR(yieldReq);
  166. }
  167. else
  168. {
  169. if (xTimerChangePeriod(hTimer, timeout_ticks, APP_TIMER_WAIT_FOR_QUEUE) != pdPASS)
  170. {
  171. return NRF_ERROR_NO_MEM;
  172. }
  173. if (xTimerStart(hTimer, APP_TIMER_WAIT_FOR_QUEUE) != pdPASS)
  174. {
  175. return NRF_ERROR_NO_MEM;
  176. }
  177. }
  178. pinfo->active = true;
  179. return NRF_SUCCESS;
  180. }
  181. uint32_t app_timer_stop(app_timer_id_t timer_id)
  182. {
  183. app_timer_info_t * pinfo = (app_timer_info_t*)(timer_id);
  184. TimerHandle_t hTimer = pinfo->osHandle;
  185. if (hTimer == NULL)
  186. {
  187. return NRF_ERROR_INVALID_STATE;
  188. }
  189. if (__get_IPSR() != 0)
  190. {
  191. BaseType_t yieldReq = pdFALSE;
  192. if (xTimerStopFromISR(hTimer, &yieldReq) != pdPASS)
  193. {
  194. return NRF_ERROR_NO_MEM;
  195. }
  196. portYIELD_FROM_ISR(yieldReq);
  197. }
  198. else
  199. {
  200. if (xTimerStop(hTimer, APP_TIMER_WAIT_FOR_QUEUE) != pdPASS)
  201. {
  202. return NRF_ERROR_NO_MEM;
  203. }
  204. }
  205. pinfo->active = false;
  206. return NRF_SUCCESS;
  207. }
  208. #endif //NRF_MODULE_ENABLED(APP_TIMER)