ec_osal_threadx.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (c) 2025, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "ec_master.h"
  7. #include "tx_api.h"
  8. extern TX_BYTE_POOL ec_byte_pool; // define ec_byte_pool and call ec_osal_init first
  9. TX_QUEUE *ec_osal_mq = TX_NULL;
  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. TX_THREAD *thread_ptr = TX_NULL;
  13. tx_byte_allocate(&ec_byte_pool, (VOID **)&thread_ptr, EC_ALIGN_UP(sizeof(TX_THREAD), 4) + stack_size, TX_NO_WAIT);
  14. if (thread_ptr == TX_NULL) {
  15. EC_LOG_ERR("Create thread %s failed\r\n", name);
  16. while (1) {
  17. }
  18. }
  19. tx_thread_create(thread_ptr, (CHAR *)name, (VOID(*)(ULONG))entry, (uintptr_t)args,
  20. ((CHAR *)thread_ptr + EC_ALIGN_UP(sizeof(TX_THREAD), 4)), stack_size,
  21. prio, prio, TX_NO_TIME_SLICE, TX_AUTO_START);
  22. return (ec_osal_thread_t)thread_ptr;
  23. }
  24. void ec_osal_thread_delete(ec_osal_thread_t thread)
  25. {
  26. if (thread == NULL) {
  27. thread = tx_thread_identify();
  28. uintptr_t addr = (uintptr_t)thread;
  29. tx_queue_send((TX_QUEUE *)ec_osal_mq, &addr, TX_NO_WAIT);
  30. tx_thread_terminate(thread);
  31. return;
  32. }
  33. tx_thread_terminate(thread);
  34. tx_thread_delete(thread);
  35. tx_byte_release(thread);
  36. }
  37. void ec_osal_thread_suspend(ec_osal_thread_t thread)
  38. {
  39. tx_thread_suspend((TX_THREAD *)thread);
  40. }
  41. void ec_osal_thread_resume(ec_osal_thread_t thread)
  42. {
  43. tx_thread_resume((TX_THREAD *)thread);
  44. }
  45. ec_osal_sem_t ec_osal_sem_create(uint32_t max_count, uint32_t initial_count)
  46. {
  47. TX_SEMAPHORE *sem_ptr = TX_NULL;
  48. tx_byte_allocate(&ec_byte_pool, (VOID **)&sem_ptr, sizeof(TX_SEMAPHORE), TX_NO_WAIT);
  49. if (sem_ptr == TX_NULL) {
  50. EC_LOG_ERR("Create semaphore failed\r\n");
  51. while (1) {
  52. }
  53. }
  54. tx_semaphore_create(sem_ptr, "ec_sem", initial_count);
  55. return (ec_osal_sem_t)sem_ptr;
  56. }
  57. void ec_osal_sem_delete(ec_osal_sem_t sem)
  58. {
  59. tx_semaphore_delete((TX_SEMAPHORE *)sem);
  60. tx_byte_release(sem);
  61. }
  62. int ec_osal_sem_take(ec_osal_sem_t sem, uint32_t timeout)
  63. {
  64. int ret = 0;
  65. ret = tx_semaphore_get((TX_SEMAPHORE *)sem, timeout);
  66. if (ret == TX_SUCCESS) {
  67. ret = 0;
  68. } else if ((ret == TX_WAIT_ABORTED) || (ret == TX_NO_INSTANCE)) {
  69. ret = -EC_ERR_TIMEOUT;
  70. } else {
  71. ret = -EC_ERR_INVAL;
  72. }
  73. return (int)ret;
  74. }
  75. int ec_osal_sem_give(ec_osal_sem_t sem)
  76. {
  77. return (tx_semaphore_put((TX_SEMAPHORE *)sem) == TX_SUCCESS) ? 0 : -EC_ERR_INVAL;
  78. }
  79. void ec_osal_sem_reset(ec_osal_sem_t sem)
  80. {
  81. tx_semaphore_get((TX_SEMAPHORE *)sem, 0);
  82. }
  83. ec_osal_mutex_t ec_osal_mutex_create(void)
  84. {
  85. TX_MUTEX *mutex_ptr = TX_NULL;
  86. tx_byte_allocate(&ec_byte_pool, (VOID **)&mutex_ptr, sizeof(TX_MUTEX), TX_NO_WAIT);
  87. if (mutex_ptr == TX_NULL) {
  88. EC_LOG_ERR("Create mutex failed\r\n");
  89. while (1) {
  90. }
  91. }
  92. tx_mutex_create(mutex_ptr, "ec_mutex", TX_INHERIT);
  93. return (ec_osal_mutex_t)mutex_ptr;
  94. }
  95. void ec_osal_mutex_delete(ec_osal_mutex_t mutex)
  96. {
  97. tx_mutex_delete((TX_MUTEX *)mutex);
  98. tx_byte_release(mutex);
  99. }
  100. int ec_osal_mutex_take(ec_osal_mutex_t mutex)
  101. {
  102. int ret = 0;
  103. ret = tx_mutex_get((TX_MUTEX *)mutex, TX_WAIT_FOREVER);
  104. if (ret == TX_SUCCESS) {
  105. ret = 0;
  106. } else if ((ret == TX_WAIT_ABORTED) || (ret == TX_NO_INSTANCE)) {
  107. ret = -EC_ERR_TIMEOUT;
  108. } else {
  109. ret = -EC_ERR_INVAL;
  110. }
  111. return (int)ret;
  112. }
  113. int ec_osal_mutex_give(ec_osal_mutex_t mutex)
  114. {
  115. return (tx_mutex_put((TX_MUTEX *)mutex) == TX_SUCCESS) ? 0 : -EC_ERR_INVAL;
  116. }
  117. ec_osal_mq_t ec_osal_mq_create(uint32_t max_msgs)
  118. {
  119. TX_QUEUE *queue_ptr = TX_NULL;
  120. tx_byte_allocate(&ec_byte_pool, (VOID **)&queue_ptr, EC_ALIGN_UP(sizeof(TX_QUEUE), 4) + sizeof(uintptr_t) * max_msgs, TX_NO_WAIT);
  121. if (queue_ptr == TX_NULL) {
  122. EC_LOG_ERR("Create TX_QUEUE failed\r\n");
  123. while (1) {
  124. }
  125. }
  126. tx_queue_create(queue_ptr, "ec_mq", sizeof(uintptr_t) / 4, (CHAR *)queue_ptr + EC_ALIGN_UP(sizeof(TX_QUEUE), 4), sizeof(uintptr_t) * max_msgs);
  127. return (ec_osal_mq_t)queue_ptr;
  128. }
  129. void ec_osal_mq_delete(ec_osal_mq_t mq)
  130. {
  131. tx_queue_delete((TX_QUEUE *)mq);
  132. tx_byte_release(mq);
  133. }
  134. int ec_osal_mq_send(ec_osal_mq_t mq, uintptr_t addr)
  135. {
  136. return (tx_queue_send((TX_QUEUE *)mq, &addr, TX_NO_WAIT) == TX_SUCCESS) ? 0 : -EC_ERR_INVAL;
  137. }
  138. int ec_osal_mq_recv(ec_osal_mq_t mq, uintptr_t *addr, uint32_t timeout)
  139. {
  140. int ret = 0;
  141. ret = tx_queue_receive((TX_QUEUE *)mq, addr, timeout);
  142. if (ret == TX_SUCCESS) {
  143. ret = 0;
  144. } else if (ret == TX_QUEUE_EMPTY) {
  145. ret = -EC_ERR_TIMEOUT;
  146. } else {
  147. ret = -EC_ERR_INVAL;
  148. }
  149. return (int)ret;
  150. }
  151. 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)
  152. {
  153. TX_TIMER *timer_ptr = TX_NULL;
  154. struct ec_osal_timer *timer;
  155. tx_byte_allocate(&ec_byte_pool, (VOID **)&timer, sizeof(struct ec_osal_timer), TX_NO_WAIT);
  156. if (timer == TX_NULL) {
  157. EC_LOG_ERR("Create ec_osal_timer failed\r\n");
  158. while (1) {
  159. }
  160. }
  161. memset(timer, 0, sizeof(struct ec_osal_timer));
  162. tx_byte_allocate(&ec_byte_pool, (VOID **)&timer_ptr, sizeof(TX_TIMER), TX_NO_WAIT);
  163. if (timer_ptr == TX_NULL) {
  164. EC_LOG_ERR("Create TX_TIMER failed\r\n");
  165. while (1) {
  166. }
  167. }
  168. timer->timer = timer_ptr;
  169. timer->timeout_ms = timeout_ms;
  170. timer->is_period = is_period;
  171. if (tx_timer_create(timer_ptr, (CHAR *)name, (void (*)(ULONG))handler, (uintptr_t)argument, 1, is_period ? 1 : 0,
  172. TX_NO_ACTIVATE) != TX_SUCCESS) {
  173. return NULL;
  174. }
  175. return timer;
  176. }
  177. void ec_osal_timer_delete(struct ec_osal_timer *timer)
  178. {
  179. tx_timer_deactivate((TX_TIMER *)timer->timer);
  180. tx_timer_delete((TX_TIMER *)timer->timer);
  181. tx_byte_release(timer->timer);
  182. tx_byte_release(timer);
  183. }
  184. void ec_osal_timer_start(struct ec_osal_timer *timer)
  185. {
  186. if (tx_timer_change((TX_TIMER *)timer->timer, timer->timeout_ms, timer->is_period ? timer->timeout_ms : 0) == TX_SUCCESS) {
  187. /* Call the tx_timer_activate to activates the specified application
  188. timer. The expiration routines of timers that expire at the same
  189. time are executed in the order they were activated. */
  190. if (tx_timer_activate((TX_TIMER *)timer->timer) == TX_SUCCESS) {
  191. /* Return osOK for success */
  192. } else {
  193. /* Return osErrorResource in case of error */
  194. }
  195. } else {
  196. }
  197. }
  198. void ec_osal_timer_stop(struct ec_osal_timer *timer)
  199. {
  200. tx_timer_deactivate((TX_TIMER *)timer->timer);
  201. }
  202. size_t ec_osal_enter_critical_section(void)
  203. {
  204. TX_INTERRUPT_SAVE_AREA
  205. TX_DISABLE
  206. return interrupt_save;
  207. }
  208. void ec_osal_leave_critical_section(size_t flag)
  209. {
  210. TX_INTERRUPT_SAVE_AREA
  211. interrupt_save = flag;
  212. TX_RESTORE
  213. }
  214. void ec_osal_msleep(uint32_t delay)
  215. {
  216. #if TX_TIMER_TICKS_PER_SECOND != 1000
  217. #error "TX_TIMER_TICKS_PER_SECOND must be 1000"
  218. #endif
  219. tx_thread_sleep(delay);
  220. }
  221. void *ec_osal_malloc(size_t size)
  222. {
  223. CHAR *pointer = TX_NULL;
  224. tx_byte_allocate(&ec_byte_pool, (VOID **)&pointer, size, TX_WAIT_FOREVER);
  225. return pointer;
  226. }
  227. void ec_osal_free(void *ptr)
  228. {
  229. tx_byte_release(ptr);
  230. }
  231. static void ec_osal_thread(CONFIG_EC_OSAL_THREAD_SET_ARGV)
  232. {
  233. int ret;
  234. ec_osal_thread_t thread;
  235. while (1) {
  236. ret = tx_queue_receive(ec_osal_mq, (uintptr_t *)&thread, TX_WAIT_FOREVER);
  237. if (ret != TX_SUCCESS) {
  238. continue;
  239. }
  240. tx_thread_delete(thread);
  241. tx_byte_release(thread);
  242. }
  243. }
  244. void ec_osal_init(uint8_t *mem, uint32_t mem_size)
  245. {
  246. ec_osal_thread_t thread;
  247. tx_byte_pool_create(&ec_byte_pool, "ec byte pool", mem, mem_size);
  248. thread = ec_osal_thread_create("ec_osal", 2048, 10, ec_osal_thread, NULL);
  249. if (thread == NULL) {
  250. EC_LOG_ERR("Create ec_osal_thread failed\r\n");
  251. while (1) {
  252. }
  253. }
  254. tx_byte_allocate(&ec_byte_pool, (VOID **)&ec_osal_mq, EC_ALIGN_UP(sizeof(TX_QUEUE), 4) + sizeof(uintptr_t) * max_msgs, TX_NO_WAIT);
  255. if (ec_osal_mq == TX_NULL) {
  256. EC_LOG_ERR("Create ec_osal_mq failed\r\n");
  257. while (1) {
  258. }
  259. }
  260. tx_queue_create(ec_osal_mq, "ec_mq", sizeof(uintptr_t) / 4, (CHAR *)ec_osal_mq + EC_ALIGN_UP(sizeof(TX_QUEUE), 4), sizeof(uintptr_t) * 32);
  261. }