ec_osal_freertos.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (c) 2025, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "ec_master.h"
  7. #include <FreeRTOS.h>
  8. #include "semphr.h"
  9. #include "event_groups.h"
  10. ec_osal_thread_t ec_osal_thread_create(const char *name, uint32_t stack_size, uint32_t prio, ec_thread_entry_t entry, void *args)
  11. {
  12. TaskHandle_t htask = NULL;
  13. stack_size /= sizeof(StackType_t);
  14. xTaskCreate(entry, name, stack_size, args, configMAX_PRIORITIES - 1 - prio, &htask);
  15. if (htask == NULL) {
  16. EC_LOG_ERR("Create thread %s failed\r\n", name);
  17. while (1) {
  18. }
  19. }
  20. return (ec_osal_thread_t)htask;
  21. }
  22. void ec_osal_thread_delete(ec_osal_thread_t thread)
  23. {
  24. vTaskDelete(thread);
  25. }
  26. void ec_osal_thread_suspend(ec_osal_thread_t thread)
  27. {
  28. vTaskSuspend((TaskHandle_t)thread);
  29. }
  30. void ec_osal_thread_resume(ec_osal_thread_t thread)
  31. {
  32. vTaskResume((TaskHandle_t)thread);
  33. }
  34. ec_osal_sem_t ec_osal_sem_create(uint32_t max_count, uint32_t initial_count)
  35. {
  36. ec_osal_sem_t sem = (ec_osal_sem_t)xSemaphoreCreateCounting(max_count, initial_count);
  37. if (sem == NULL) {
  38. EC_LOG_ERR("Create semaphore failed\r\n");
  39. while (1) {
  40. }
  41. }
  42. return sem;
  43. }
  44. void ec_osal_sem_delete(ec_osal_sem_t sem)
  45. {
  46. vSemaphoreDelete((SemaphoreHandle_t)sem);
  47. }
  48. int ec_osal_sem_take(ec_osal_sem_t sem, uint32_t timeout)
  49. {
  50. if (timeout == EC_OSAL_WAITING_FOREVER) {
  51. return (xSemaphoreTake((SemaphoreHandle_t)sem, portMAX_DELAY) == pdPASS) ? 0 : -EC_ERR_TIMEOUT;
  52. } else {
  53. return (xSemaphoreTake((SemaphoreHandle_t)sem, pdMS_TO_TICKS(timeout)) == pdPASS) ? 0 : -EC_ERR_TIMEOUT;
  54. }
  55. }
  56. int ec_osal_sem_give(ec_osal_sem_t sem)
  57. {
  58. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  59. int ret;
  60. if (xPortIsInsideInterrupt()) {
  61. ret = xSemaphoreGiveFromISR((SemaphoreHandle_t)sem, &xHigherPriorityTaskWoken);
  62. if (ret == pdPASS) {
  63. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  64. }
  65. } else {
  66. ret = xSemaphoreGive((SemaphoreHandle_t)sem);
  67. }
  68. return (ret == pdPASS) ? 0 : -EC_ERR_TIMEOUT;
  69. }
  70. void ec_osal_sem_reset(ec_osal_sem_t sem)
  71. {
  72. xQueueReset((QueueHandle_t)sem);
  73. }
  74. ec_osal_mutex_t ec_osal_mutex_create(void)
  75. {
  76. ec_osal_mutex_t mutex = (ec_osal_mutex_t)xSemaphoreCreateMutex();
  77. if (mutex == NULL) {
  78. EC_LOG_ERR("Create mutex failed\r\n");
  79. while (1) {
  80. }
  81. }
  82. return mutex;
  83. }
  84. void ec_osal_mutex_delete(ec_osal_mutex_t mutex)
  85. {
  86. vSemaphoreDelete((SemaphoreHandle_t)mutex);
  87. }
  88. int ec_osal_mutex_take(ec_osal_mutex_t mutex)
  89. {
  90. return (xSemaphoreTake((SemaphoreHandle_t)mutex, portMAX_DELAY) == pdPASS) ? 0 : -EC_ERR_TIMEOUT;
  91. }
  92. int ec_osal_mutex_give(ec_osal_mutex_t mutex)
  93. {
  94. return (xSemaphoreGive((SemaphoreHandle_t)mutex) == pdPASS) ? 0 : -EC_ERR_TIMEOUT;
  95. }
  96. ec_osal_mq_t ec_osal_mq_create(uint32_t max_msgs)
  97. {
  98. return (ec_osal_mq_t)xQueueCreate(max_msgs, sizeof(uintptr_t));
  99. }
  100. void ec_osal_mq_delete(ec_osal_mq_t mq)
  101. {
  102. vQueueDelete((QueueHandle_t)mq);
  103. }
  104. int ec_osal_mq_send(ec_osal_mq_t mq, uintptr_t addr)
  105. {
  106. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  107. int ret;
  108. if (xPortIsInsideInterrupt()) {
  109. ret = xQueueSendFromISR((ec_osal_mq_t)mq, &addr, &xHigherPriorityTaskWoken);
  110. if (ret == pdPASS) {
  111. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  112. }
  113. } else {
  114. ret = xQueueSend((ec_osal_mq_t)mq, &addr, 0);
  115. }
  116. return (ret == pdPASS) ? 0 : -EC_ERR_TIMEOUT;
  117. }
  118. int ec_osal_mq_recv(ec_osal_mq_t mq, uintptr_t *addr, uint32_t timeout)
  119. {
  120. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  121. int ret;
  122. if (xPortIsInsideInterrupt()) {
  123. ret = xQueueReceiveFromISR((ec_osal_mq_t)mq, addr, &xHigherPriorityTaskWoken);
  124. if (ret == pdPASS) {
  125. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  126. }
  127. return (ret == pdPASS) ? 0 : -EC_ERR_TIMEOUT;
  128. } else {
  129. if (timeout == EC_OSAL_WAITING_FOREVER) {
  130. return (xQueueReceive((ec_osal_mq_t)mq, addr, portMAX_DELAY) == pdPASS) ? 0 : -EC_ERR_TIMEOUT;
  131. } else {
  132. return (xQueueReceive((ec_osal_mq_t)mq, addr, pdMS_TO_TICKS(timeout)) == pdPASS) ? 0 : -EC_ERR_TIMEOUT;
  133. }
  134. }
  135. }
  136. static void __ec_timeout(TimerHandle_t *handle)
  137. {
  138. struct ec_osal_timer *timer = (struct ec_osal_timer *)pvTimerGetTimerID((TimerHandle_t)handle);
  139. timer->handler(timer->argument);
  140. }
  141. struct ec_osal_timer *ec_osal_timer_create(const char *name, uint32_t timeout_ms, ec_timer_handler_t handler, void *argument, bool is_period)
  142. {
  143. struct ec_osal_timer *timer;
  144. (void)name;
  145. timer = pvPortMalloc(sizeof(struct ec_osal_timer));
  146. if (timer == NULL) {
  147. EC_LOG_ERR("Create ec_osal_timer failed\r\n");
  148. while (1) {
  149. }
  150. }
  151. memset(timer, 0, sizeof(struct ec_osal_timer));
  152. timer->handler = handler;
  153. timer->argument = argument;
  154. timer->timer = (void *)xTimerCreate("ec_tim", pdMS_TO_TICKS(timeout_ms), is_period, timer, (TimerCallbackFunction_t)__ec_timeout);
  155. if (timer->timer == NULL) {
  156. EC_LOG_ERR("Create timer failed\r\n");
  157. while (1) {
  158. }
  159. }
  160. return timer;
  161. }
  162. void ec_osal_timer_delete(struct ec_osal_timer *timer)
  163. {
  164. xTimerStop(timer->timer, 0);
  165. xTimerDelete(timer->timer, 0);
  166. vPortFree(timer);
  167. }
  168. void ec_osal_timer_start(struct ec_osal_timer *timer)
  169. {
  170. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  171. int ret;
  172. if (xPortIsInsideInterrupt()) {
  173. ret = xTimerStartFromISR(timer->timer, &xHigherPriorityTaskWoken);
  174. if (ret == pdPASS) {
  175. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  176. }
  177. } else {
  178. xTimerStart(timer->timer, 0);
  179. }
  180. }
  181. void ec_osal_timer_stop(struct ec_osal_timer *timer)
  182. {
  183. xTimerStop(timer->timer, 0);
  184. }
  185. size_t ec_osal_enter_critical_section(void)
  186. {
  187. size_t ret;
  188. if (xPortIsInsideInterrupt()) {
  189. ret = taskENTER_CRITICAL_FROM_ISR();
  190. } else {
  191. taskENTER_CRITICAL();
  192. ret = 1;
  193. }
  194. return ret;
  195. }
  196. void ec_osal_leave_critical_section(size_t flag)
  197. {
  198. if (xPortIsInsideInterrupt()) {
  199. taskEXIT_CRITICAL_FROM_ISR(flag);
  200. } else {
  201. taskEXIT_CRITICAL();
  202. }
  203. }
  204. void ec_osal_msleep(uint32_t delay)
  205. {
  206. vTaskDelay(pdMS_TO_TICKS(delay));
  207. }
  208. void *ec_osal_malloc(size_t size)
  209. {
  210. return pvPortMalloc(size);
  211. }
  212. void ec_osal_free(void *ptr)
  213. {
  214. vPortFree(ptr);
  215. }