ec_osal_rtthread.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2025, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "ec_master.h"
  7. #include <rtthread.h>
  8. #include <rthw.h>
  9. 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)
  10. {
  11. rt_thread_t htask;
  12. htask = rt_thread_create(name, entry, args, stack_size, prio, 10);
  13. if (htask == NULL) {
  14. EC_LOG_ERR("Create thread %s failed\r\n", name);
  15. while (1) {
  16. }
  17. }
  18. rt_thread_startup(htask);
  19. return (ec_osal_thread_t)htask;
  20. }
  21. void ec_osal_thread_delete(ec_osal_thread_t thread)
  22. {
  23. if (thread == NULL) {
  24. rt_thread_t self = rt_thread_self();
  25. rt_thread_control(self, RT_THREAD_CTRL_CLOSE, RT_NULL);
  26. return;
  27. }
  28. rt_thread_delete(thread);
  29. }
  30. void ec_osal_thread_suspend(ec_osal_thread_t thread)
  31. {
  32. rt_thread_suspend((rt_thread_t)thread);
  33. }
  34. void ec_osal_thread_resume(ec_osal_thread_t thread)
  35. {
  36. rt_thread_resume((rt_thread_t)thread);
  37. }
  38. ec_osal_sem_t ec_osal_sem_create(uint32_t max_count, uint32_t initial_count)
  39. {
  40. ec_osal_sem_t sem = (ec_osal_sem_t)rt_sem_create("ec_sem", initial_count, RT_IPC_FLAG_FIFO);
  41. if (sem == NULL) {
  42. EC_LOG_ERR("Create semaphore failed\r\n");
  43. while (1) {
  44. }
  45. }
  46. return sem;
  47. }
  48. void ec_osal_sem_delete(ec_osal_sem_t sem)
  49. {
  50. rt_sem_delete((rt_sem_t)sem);
  51. }
  52. int ec_osal_sem_take(ec_osal_sem_t sem, uint32_t timeout)
  53. {
  54. int ret = 0;
  55. rt_err_t result = RT_EOK;
  56. if (timeout == EC_OSAL_WAITING_FOREVER) {
  57. result = rt_sem_take((rt_sem_t)sem, RT_WAITING_FOREVER);
  58. } else {
  59. result = rt_sem_take((rt_sem_t)sem, rt_tick_from_millisecond(timeout));
  60. }
  61. if (result == -RT_ETIMEOUT) {
  62. ret = -EC_ERR_TIMEOUT;
  63. } else if (result == -RT_ERROR) {
  64. ret = -EC_ERR_INVAL;
  65. } else {
  66. ret = 0;
  67. }
  68. return (int)ret;
  69. }
  70. int ec_osal_sem_give(ec_osal_sem_t sem)
  71. {
  72. return (int)rt_sem_release((rt_sem_t)sem);
  73. }
  74. void ec_osal_sem_reset(ec_osal_sem_t sem)
  75. {
  76. rt_sem_control((rt_sem_t)sem, RT_IPC_CMD_RESET, (void *)0);
  77. }
  78. ec_osal_mutex_t ec_osal_mutex_create(void)
  79. {
  80. ec_osal_mutex_t mutex = (ec_osal_mutex_t)rt_mutex_create("ec_mutex", RT_IPC_FLAG_FIFO);
  81. if (mutex == NULL) {
  82. EC_LOG_ERR("Create mutex failed\r\n");
  83. while (1) {
  84. }
  85. }
  86. return mutex;
  87. }
  88. void ec_osal_mutex_delete(ec_osal_mutex_t mutex)
  89. {
  90. rt_mutex_delete((rt_mutex_t)mutex);
  91. }
  92. int ec_osal_mutex_take(ec_osal_mutex_t mutex)
  93. {
  94. return (int)rt_mutex_take((rt_mutex_t)mutex, RT_WAITING_FOREVER);
  95. }
  96. int ec_osal_mutex_give(ec_osal_mutex_t mutex)
  97. {
  98. return (int)rt_mutex_release((rt_mutex_t)mutex);
  99. }
  100. ec_osal_mq_t ec_osal_mq_create(uint32_t max_msgs)
  101. {
  102. return (ec_osal_mq_t)rt_mq_create("ec_mq", sizeof(uintptr_t), max_msgs, RT_IPC_FLAG_FIFO);
  103. }
  104. void ec_osal_mq_delete(ec_osal_mq_t mq)
  105. {
  106. rt_mq_delete((rt_mq_t)mq);
  107. }
  108. int ec_osal_mq_send(ec_osal_mq_t mq, uintptr_t addr)
  109. {
  110. return rt_mq_send((rt_mq_t)mq, &addr, sizeof(uintptr_t));
  111. }
  112. int ec_osal_mq_recv(ec_osal_mq_t mq, uintptr_t *addr, uint32_t timeout)
  113. {
  114. int ret = 0;
  115. rt_err_t result = RT_EOK;
  116. if (timeout == EC_OSAL_WAITING_FOREVER) {
  117. result = rt_mq_recv((rt_mq_t)mq, addr, sizeof(uintptr_t), RT_WAITING_FOREVER);
  118. } else {
  119. result = rt_mq_recv((rt_mq_t)mq, addr, sizeof(uintptr_t), rt_tick_from_millisecond(timeout));
  120. }
  121. if (result == -RT_ETIMEOUT) {
  122. ret = -EC_ERR_TIMEOUT;
  123. } else if (result == -RT_ERROR) {
  124. ret = -EC_ERR_INVAL;
  125. } else {
  126. ret = 0;
  127. }
  128. return (int)ret;
  129. }
  130. 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)
  131. {
  132. struct ec_osal_timer *timer;
  133. timer = rt_malloc(sizeof(struct ec_osal_timer));
  134. if (timer == NULL) {
  135. EC_LOG_ERR("Create ec_osal_timer failed\r\n");
  136. while (1) {
  137. }
  138. }
  139. memset(timer, 0, sizeof(struct ec_osal_timer));
  140. timer->timer = (void *)rt_timer_create(name, handler, argument, timeout_ms, is_period ? (RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER) : (RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER));
  141. if (timer->timer == NULL) {
  142. EC_LOG_ERR("Create timer failed\r\n");
  143. while (1) {
  144. }
  145. }
  146. return timer;
  147. }
  148. void ec_osal_timer_delete(struct ec_osal_timer *timer)
  149. {
  150. rt_timer_stop(timer->timer);
  151. rt_timer_delete(timer->timer);
  152. rt_free(timer);
  153. }
  154. void ec_osal_timer_start(struct ec_osal_timer *timer)
  155. {
  156. rt_timer_start(timer->timer);
  157. }
  158. void ec_osal_timer_stop(struct ec_osal_timer *timer)
  159. {
  160. rt_timer_stop(timer->timer);
  161. }
  162. size_t ec_osal_enter_critical_section(void)
  163. {
  164. return rt_hw_interrupt_disable();
  165. }
  166. void ec_osal_leave_critical_section(size_t flag)
  167. {
  168. rt_hw_interrupt_enable(flag);
  169. }
  170. void ec_osal_msleep(uint32_t delay)
  171. {
  172. rt_thread_mdelay(delay);
  173. }
  174. void *ec_osal_malloc(size_t size)
  175. {
  176. return rt_malloc(size);
  177. }
  178. void ec_osal_free(void *ptr)
  179. {
  180. rt_free(ptr);
  181. }