luat_rtos_air105.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "app_interface.h"
  24. #include "FreeRTOS.h"
  25. #include "semphr.h"
  26. #include "task.h"
  27. #define LUAT_LOG_TAG "luat.rtos"
  28. #include "luat_log.h"
  29. 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)
  30. {
  31. priority = configMAX_PRIORITIES * priority / 100;
  32. if (!priority) priority = 2;
  33. if (priority >= configMAX_PRIORITIES) priority -= 1;
  34. *task_handle = Task_Create(task_fun, user_data, stack_size, priority, task_name);
  35. return (*task_handle)?0:-1;
  36. }
  37. int luat_send_event_to_task(void* task_handle, uint32_t id, uint32_t param1, uint32_t param2, uint32_t param3)
  38. {
  39. Task_SendEvent(task_handle, id, param1, param2, param3);
  40. return 0;
  41. }
  42. int luat_wait_event_from_task(void* task_handle, uint32_t wait_event_id, luat_event_t *out_event, void *call_back, uint32_t ms)
  43. {
  44. return Task_GetEventByMS(task_handle, wait_event_id, out_event, call_back, ms);
  45. }
  46. int luat_rtos_task_delete(void* task_handle)
  47. {
  48. if (!task_handle) return -1;
  49. Task_Exit(task_handle);
  50. return 0;
  51. }
  52. void *luat_create_rtos_timer(void *cb, void *param, void *task_handle)
  53. {
  54. return Timer_Create(cb, param, task_handle);
  55. }
  56. int luat_start_rtos_timer(void *timer, uint32_t ms, uint8_t is_repeat)
  57. {
  58. return Timer_StartMS(timer, ms, is_repeat);
  59. }
  60. int luat_rtos_timer_start(void *timer, uint32_t timeout, uint8_t repeat, luat_rtos_timer_callback_t callback_fun, void *user_param)
  61. {
  62. Timer_Stop(timer);
  63. Timer_SetCallback(timer, callback_fun, user_param);
  64. return Timer_StartMS(timer, timeout, repeat);
  65. }
  66. void luat_stop_rtos_timer(void *timer)
  67. {
  68. Timer_Stop(timer);
  69. }
  70. void luat_release_rtos_timer(void *timer)
  71. {
  72. Timer_Release(timer);
  73. }
  74. void luat_task_suspend_all(void)
  75. {
  76. OS_SuspendTask(NULL);
  77. }
  78. void luat_task_resume_all(void)
  79. {
  80. OS_ResumeTask(NULL);
  81. }
  82. void *luat_get_current_task(void)
  83. {
  84. return xTaskGetCurrentTaskHandle();
  85. }
  86. void luat_rtos_task_sleep(uint32_t ms)
  87. {
  88. Task_DelayMS(ms);
  89. }
  90. void *luat_mutex_create(void)
  91. {
  92. return OS_MutexCreateUnlock();
  93. }
  94. LUAT_RET luat_mutex_lock(void *mutex)
  95. {
  96. OS_MutexLock(mutex);
  97. return 0;
  98. }
  99. LUAT_RET luat_mutex_unlock(void *mutex)
  100. {
  101. OS_MutexRelease(mutex);
  102. return 0;
  103. }
  104. void luat_mutex_release(void *mutex)
  105. {
  106. OS_MutexDelete(mutex);
  107. }
  108. int luat_rtos_queue_create(luat_rtos_queue_t *queue_handle, uint32_t item_count, uint32_t item_size)
  109. {
  110. if (!queue_handle) return -1;
  111. QueueHandle_t pxNewQueue;
  112. pxNewQueue = xQueueCreate(item_count, item_size);
  113. if (!pxNewQueue)
  114. return -1;
  115. *queue_handle = pxNewQueue;
  116. return 0;
  117. }
  118. int luat_rtos_queue_delete(luat_rtos_queue_t queue_handle)
  119. {
  120. if (!queue_handle) return -1;
  121. vQueueDelete ((QueueHandle_t)queue_handle);
  122. return 0;
  123. }
  124. int luat_rtos_queue_send(luat_rtos_queue_t queue_handle, void *item, uint32_t item_size, uint32_t timeout)
  125. {
  126. if (!queue_handle || !item) return -1;
  127. if (OS_CheckInIrq())
  128. {
  129. BaseType_t pxHigherPriorityTaskWoken;
  130. if (xQueueSendToBackFromISR(queue_handle, item, &pxHigherPriorityTaskWoken) != pdPASS)
  131. return -1;
  132. portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);
  133. return 0;
  134. }
  135. else
  136. {
  137. if (xQueueSendToBack (queue_handle, item, timeout) != pdPASS)
  138. return -1;
  139. }
  140. return 0;
  141. }
  142. int luat_rtos_queue_recv(luat_rtos_queue_t queue_handle, void *item, uint32_t item_size, uint32_t timeout)
  143. {
  144. if (!queue_handle || !item)
  145. return -1;
  146. BaseType_t yield = pdFALSE;
  147. if (OS_CheckInIrq())
  148. {
  149. if (xQueueReceiveFromISR(queue_handle, item, &yield) != pdPASS)
  150. return -1;
  151. portYIELD_FROM_ISR(yield);
  152. return 0;
  153. }
  154. else
  155. {
  156. if (xQueueReceive(queue_handle, item, timeout) != pdPASS)
  157. return -1;
  158. }
  159. return 0;
  160. }