luat_rtos_air101.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "luat_base.h"
  22. #include "luat_rtos.h"
  23. #include "c_common.h"
  24. #include "luat_malloc.h"
  25. #include "wm_osal.h"
  26. #include "FreeRTOS.h"
  27. #include "semphr.h"
  28. #include "task.h"
  29. #define LUAT_LOG_TAG "luat.rtos"
  30. #include "luat_log.h"
  31. int luat_rtos_task_create(luat_rtos_task_handle *task_handle, uint32_t stack_size, uint8_t priority, const char *task_name, luat_rtos_task_entry task_fun, void* user_data, uint16_t event_cout)
  32. {
  33. if (!task_handle) return -1;
  34. return xTaskCreate(task_fun,
  35. task_name,
  36. stack_size/sizeof(uint32_t),
  37. user_data,
  38. priority,
  39. task_handle );
  40. }
  41. int luat_rtos_task_delete(luat_rtos_task_handle task_handle)
  42. {
  43. if (!task_handle) return -1;
  44. vTaskDelete(task_handle);
  45. return 0;
  46. }
  47. int luat_rtos_task_suspend(luat_rtos_task_handle task_handle)
  48. {
  49. if (!task_handle) return -1;
  50. vTaskSuspend(task_handle);
  51. return 0;
  52. }
  53. int luat_rtos_task_resume(luat_rtos_task_handle task_handle)
  54. {
  55. if (!task_handle) return -1;
  56. vTaskResume(task_handle);
  57. return 0;
  58. }
  59. uint32_t luat_rtos_task_HighWaterMark(luat_rtos_task_handle task_handle)
  60. {
  61. if (!task_handle) return -1;
  62. return uxTaskGetStackHighWaterMark(task_handle);
  63. }
  64. void luat_rtos_task_sleep(uint32_t ms)
  65. {
  66. vTaskDelay(ms);
  67. }
  68. void luat_task_suspend_all(void)
  69. {
  70. vTaskSuspendAll();
  71. }
  72. void luat_task_resume_all(void)
  73. {
  74. xTaskResumeAll();
  75. }
  76. void *luat_get_current_task(void)
  77. {
  78. return xTaskGetCurrentTaskHandle();
  79. }
  80. int luat_rtos_queue_create(luat_rtos_queue_t *queue_handle, uint32_t item_count, uint32_t item_size)
  81. {
  82. if (!queue_handle) return -1;
  83. xQueueHandle pxNewQueue;
  84. pxNewQueue = xQueueCreate(item_count, item_size);
  85. if (!pxNewQueue)
  86. return -1;
  87. *queue_handle = pxNewQueue;
  88. return 0;
  89. }
  90. int luat_rtos_queue_delete(luat_rtos_queue_t queue_handle)
  91. {
  92. if (!queue_handle) return -1;
  93. vQueueDelete ((xQueueHandle)queue_handle);
  94. return 0;
  95. }
  96. int luat_rtos_queue_send(luat_rtos_queue_t queue_handle, void *item, uint32_t item_size, uint32_t timeout)
  97. {
  98. if (!queue_handle || !item) return -1;
  99. if (tls_get_isr_count()>0)
  100. {
  101. BaseType_t pxHigherPriorityTaskWoken;
  102. if (xQueueSendToBackFromISR(queue_handle, item, &pxHigherPriorityTaskWoken) != pdPASS)
  103. return -1;
  104. portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);
  105. return 0;
  106. }
  107. else
  108. {
  109. if (xQueueSendToBack (queue_handle, item, timeout) != pdPASS)
  110. return -1;
  111. }
  112. return 0;
  113. }
  114. int luat_rtos_queue_recv(luat_rtos_queue_t queue_handle, void *item, uint32_t item_size, uint32_t timeout)
  115. {
  116. if (!queue_handle || !item)
  117. return -1;
  118. BaseType_t yield = pdFALSE;
  119. if (tls_get_isr_count()>0)
  120. {
  121. if (xQueueReceiveFromISR(queue_handle, item, &yield) != pdPASS)
  122. return -1;
  123. portYIELD_FROM_ISR(yield);
  124. return 0;
  125. }
  126. else
  127. {
  128. if (xQueueReceive(queue_handle, item, timeout) != pdPASS)
  129. return -1;
  130. }
  131. return 0;
  132. }
  133. // LUAT_RET luat_send_event_to_task(void *task_handle, uint32_t id, uint32_t param1, uint32_t param2, uint32_t param3) {
  134. // return LUAT_ERR_OK;
  135. // }
  136. // LUAT_RET luat_wait_event_from_task(void *task_handle, uint32_t wait_event_id, void *out_event, void *call_back, uint32_t ms) {
  137. // return LUAT_ERR_OK;
  138. // }
  139. // int luat_sem_create(luat_sem_t* semaphore){
  140. // semaphore->userdata = (tls_os_sem_t *)luat_heap_malloc(sizeof(tls_os_sem_t));
  141. // return tls_os_sem_create((tls_os_sem_t **)&(semaphore->userdata),semaphore->value);
  142. // }
  143. // int luat_sem_delete(luat_sem_t* semaphore){
  144. // tls_os_sem_delete((tls_os_sem_t *)(semaphore->userdata));
  145. // luat_heap_free(semaphore->userdata);
  146. // return 0;
  147. // }
  148. // int luat_sem_take(luat_sem_t* semaphore,uint32_t timeout){
  149. // return tls_os_sem_acquire((tls_os_sem_t *)(semaphore->userdata),timeout);
  150. // }
  151. // int luat_sem_release(luat_sem_t* semaphore){
  152. // return tls_os_sem_release((tls_os_sem_t *)(semaphore->userdata));
  153. // }
  154. typedef struct luat_rtos_timer {
  155. tls_os_timer_t *timer;
  156. void *cb;
  157. void *param;
  158. }air101_timer;
  159. void *luat_create_rtos_timer(void *cb, void *param, void *task_handle){
  160. air101_timer *luat_timer = luat_heap_malloc(sizeof(air101_timer));
  161. luat_timer->cb = cb;
  162. luat_timer->param = param;
  163. return luat_timer;
  164. }
  165. int luat_start_rtos_timer(void *timer, uint32_t ms, uint8_t is_repeat){
  166. air101_timer *luat_timer = (air101_timer *)timer;
  167. tls_os_timer_create(&(luat_timer->timer), luat_timer->cb, luat_timer->param,ms, is_repeat, NULL);
  168. tls_os_timer_start(luat_timer->timer);
  169. return 0;
  170. }
  171. void luat_stop_rtos_timer(void *timer){
  172. air101_timer *luat_timer = (air101_timer *)timer;
  173. tls_os_timer_stop(luat_timer->timer);
  174. }
  175. void luat_release_rtos_timer(void *timer){
  176. air101_timer *luat_timer = (air101_timer *)timer;
  177. tls_os_timer_delete(luat_timer->timer);
  178. luat_heap_free(luat_timer);
  179. }