luat_rtos_freertos_timer.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #ifdef LUAT_FREERTOS_FULL_INCLUDE
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "freertos/queue.h"
  5. #include "freertos/semphr.h"
  6. #include "freertos/timers.h"
  7. #else
  8. #include "FreeRTOS.h"
  9. #include "task.h"
  10. #include "queue.h"
  11. #include "semphr.h"
  12. #include "timers.h"
  13. #endif
  14. #include "luat_base.h"
  15. #include "luat_rtos.h"
  16. #include "luat_mem.h"
  17. typedef struct
  18. {
  19. void *timer;
  20. luat_rtos_timer_callback_t call_back;
  21. void *user_param;
  22. uint8_t is_repeat;
  23. }luat_rtos_user_timer_t;
  24. static void s_timer_callback(TimerHandle_t hTimer)
  25. {
  26. luat_rtos_user_timer_t *timer = (luat_rtos_user_timer_t *)pvTimerGetTimerID(hTimer);
  27. if (!timer)
  28. return;
  29. if (!timer->is_repeat)
  30. {
  31. xTimerStop(hTimer, 0);
  32. }
  33. if (timer->call_back)
  34. {
  35. timer->call_back(timer->user_param);
  36. }
  37. }
  38. void *luat_create_rtos_timer(void *cb, void *param, void *task_handle)
  39. {
  40. luat_rtos_user_timer_t *timer = luat_heap_malloc(sizeof(luat_rtos_user_timer_t));
  41. if (timer)
  42. {
  43. timer->timer = xTimerCreate(NULL, 1, 1, timer, s_timer_callback);
  44. if (!timer->timer)
  45. {
  46. luat_heap_free(timer);
  47. return NULL;
  48. }
  49. timer->call_back = cb;
  50. timer->user_param = param;
  51. timer->is_repeat = 0;
  52. }
  53. return timer;
  54. }
  55. int luat_start_rtos_timer(void *timer, uint32_t ms, uint8_t is_repeat)
  56. {
  57. luat_rtos_user_timer_t *htimer = (luat_rtos_user_timer_t *)timer;
  58. if (xTimerIsTimerActive (htimer->timer))
  59. {
  60. if (luat_rtos_get_ipsr())
  61. {
  62. BaseType_t pxHigherPriorityTaskWoken;
  63. if ((xTimerStopFromISR(htimer->timer, &pxHigherPriorityTaskWoken) != pdPASS))
  64. return -1;
  65. portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);
  66. return 0;
  67. }
  68. else
  69. {
  70. if (xTimerStop(htimer->timer, LUAT_WAIT_FOREVER) != pdPASS)
  71. return -1;
  72. }
  73. }
  74. htimer->is_repeat = is_repeat;
  75. if (luat_rtos_get_ipsr())
  76. {
  77. BaseType_t pxHigherPriorityTaskWoken;
  78. if ((xTimerChangePeriodFromISR(htimer->timer, luat_rtos_ms2tick(ms), &pxHigherPriorityTaskWoken) != pdPASS))
  79. return -1;
  80. portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);
  81. return 0;
  82. }
  83. else
  84. {
  85. if (xTimerChangePeriod(htimer->timer, ms, LUAT_WAIT_FOREVER) != pdPASS)
  86. return -1;
  87. }
  88. return 0;
  89. }
  90. void luat_stop_rtos_timer(void *timer)
  91. {
  92. luat_rtos_user_timer_t *htimer = (luat_rtos_user_timer_t *)timer;
  93. if (xTimerIsTimerActive (htimer->timer))
  94. {
  95. if (luat_rtos_get_ipsr())
  96. {
  97. BaseType_t pxHigherPriorityTaskWoken;
  98. if ((xTimerStopFromISR(htimer->timer, &pxHigherPriorityTaskWoken) != pdPASS))
  99. return ;
  100. portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);
  101. }
  102. else
  103. {
  104. xTimerStop(htimer->timer, LUAT_WAIT_FOREVER);
  105. }
  106. }
  107. }
  108. void luat_release_rtos_timer(void *timer)
  109. {
  110. luat_rtos_user_timer_t *htimer = (luat_rtos_user_timer_t *)timer;
  111. xTimerDelete(htimer->timer, LUAT_WAIT_FOREVER);
  112. luat_heap_free(htimer);
  113. }
  114. int luat_rtos_timer_create(luat_rtos_timer_t *timer_handle)
  115. {
  116. if (!timer_handle) return -1;
  117. *timer_handle = luat_create_rtos_timer(NULL, NULL, NULL);
  118. return (*timer_handle)?0:-1;
  119. }
  120. int luat_rtos_timer_delete(luat_rtos_timer_t timer_handle)
  121. {
  122. if (!timer_handle) return -1;
  123. luat_release_rtos_timer(timer_handle);
  124. return 0;
  125. }
  126. int luat_rtos_timer_start(luat_rtos_timer_t timer_handle, uint32_t timeout, uint8_t repeat, luat_rtos_timer_callback_t callback_fun, void *user_param)
  127. {
  128. if (!timer_handle) return -1;
  129. luat_rtos_user_timer_t *htimer = (luat_rtos_user_timer_t *)timer_handle;
  130. if (xTimerIsTimerActive (htimer->timer))
  131. {
  132. if (luat_rtos_get_ipsr())
  133. {
  134. BaseType_t pxHigherPriorityTaskWoken;
  135. if ((xTimerStopFromISR(htimer->timer, &pxHigherPriorityTaskWoken) != pdPASS))
  136. return -1;
  137. portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);
  138. return 0;
  139. }
  140. else
  141. {
  142. if (xTimerStop(htimer->timer, LUAT_WAIT_FOREVER) != pdPASS)
  143. return -1;
  144. }
  145. }
  146. htimer->is_repeat = repeat;
  147. htimer->call_back = callback_fun;
  148. htimer->user_param = user_param;
  149. if (luat_rtos_get_ipsr())
  150. {
  151. BaseType_t pxHigherPriorityTaskWoken;
  152. if ((xTimerChangePeriodFromISR(htimer->timer, luat_rtos_ms2tick(timeout), &pxHigherPriorityTaskWoken) != pdPASS))
  153. return -1;
  154. portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);
  155. return 0;
  156. }
  157. else
  158. {
  159. if (xTimerChangePeriod(htimer->timer, luat_rtos_ms2tick(timeout), 0) != pdPASS)
  160. return -1;
  161. }
  162. return 0;
  163. }
  164. int luat_rtos_timer_stop(luat_rtos_timer_t timer_handle)
  165. {
  166. if (!timer_handle) return -1;
  167. luat_stop_rtos_timer(timer_handle);
  168. return 0;
  169. }
  170. int luat_rtos_timer_is_active(luat_rtos_timer_t timer_handle)
  171. {
  172. if (!timer_handle) return -1;
  173. luat_rtos_user_timer_t *htimer = (luat_rtos_user_timer_t *)timer_handle;
  174. if (pdTRUE == xTimerIsTimerActive (htimer->timer))
  175. return 1;
  176. else
  177. return 0;
  178. }
  179. /*------------------------------------------------ timer end----------------------------------------------- */
  180. /** @}*/