ec_osal_threadx.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. /* create bytepool in tx_application_define
  9. *
  10. * tx_byte_pool_create(&ec_byte_pool, "ec byte pool", memory_area, 65536);
  11. */
  12. extern TX_BYTE_POOL ec_byte_pool;
  13. 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)
  14. {
  15. CHAR *pointer = TX_NULL;
  16. TX_THREAD *thread_ptr = TX_NULL;
  17. tx_byte_allocate(&ec_byte_pool, (VOID **)&thread_ptr, sizeof(TX_THREAD), TX_NO_WAIT);
  18. if (thread_ptr == TX_NULL) {
  19. EC_LOG_ERR("Create thread %s failed\r\n", name);
  20. while (1) {
  21. }
  22. }
  23. tx_byte_allocate(&ec_byte_pool, (VOID **)&pointer, stack_size, TX_NO_WAIT);
  24. if (pointer == TX_NULL) {
  25. EC_LOG_ERR("Create thread %s failed\r\n", name);
  26. while (1) {
  27. }
  28. }
  29. tx_thread_create(thread_ptr, (CHAR *)name, (VOID(*)(ULONG))entry, (uintptr_t)args,
  30. pointer, stack_size,
  31. prio, prio, TX_NO_TIME_SLICE, TX_AUTO_START);
  32. return (ec_osal_thread_t)thread_ptr;
  33. }
  34. void ec_osal_thread_delete(ec_osal_thread_t thread)
  35. {
  36. TX_THREAD *thread_ptr = NULL;
  37. if (thread == NULL) {
  38. /* Call the tx_thread_identify to get the control block pointer of the
  39. currently executing thread. */
  40. thread_ptr = tx_thread_identify();
  41. /* Check if the current running thread pointer is not NULL */
  42. if (thread_ptr != NULL) {
  43. /* Call the tx_thread_terminate to terminates the specified application
  44. thread regardless of whether the thread is suspended or not. A thread
  45. may call this service to terminate itself. */
  46. tx_thread_terminate(thread_ptr);
  47. tx_byte_release(thread_ptr->tx_thread_stack_start);
  48. tx_byte_release(thread_ptr);
  49. }
  50. return;
  51. }
  52. tx_thread_terminate(thread);
  53. tx_byte_release(thread_ptr->tx_thread_stack_start);
  54. tx_byte_release(thread);
  55. }
  56. void ec_osal_thread_suspend(ec_osal_thread_t thread)
  57. {
  58. tx_thread_suspend((TX_THREAD *)thread);
  59. }
  60. void ec_osal_thread_resume(ec_osal_thread_t thread)
  61. {
  62. tx_thread_resume((TX_THREAD *)thread);
  63. }
  64. ec_osal_sem_t ec_osal_sem_create(uint32_t max_count, uint32_t initial_count)
  65. {
  66. TX_SEMAPHORE *sem_ptr = TX_NULL;
  67. tx_byte_allocate(&ec_byte_pool, (VOID **)&sem_ptr, sizeof(TX_SEMAPHORE), TX_NO_WAIT);
  68. if (sem_ptr == TX_NULL) {
  69. EC_LOG_ERR("Create semaphore failed\r\n");
  70. while (1) {
  71. }
  72. }
  73. tx_semaphore_create(sem_ptr, "ec_sem", initial_count);
  74. return (ec_osal_sem_t)sem_ptr;
  75. }
  76. void ec_osal_sem_delete(ec_osal_sem_t sem)
  77. {
  78. tx_semaphore_delete((TX_SEMAPHORE *)sem);
  79. tx_byte_release(sem);
  80. }
  81. int ec_osal_sem_take(ec_osal_sem_t sem, uint32_t timeout)
  82. {
  83. int ret = 0;
  84. ret = tx_semaphore_get((TX_SEMAPHORE *)sem, timeout);
  85. if (ret == TX_SUCCESS) {
  86. ret = 0;
  87. } else if ((ret == TX_WAIT_ABORTED) || (ret == TX_NO_INSTANCE)) {
  88. ret = -EC_ERR_TIMEOUT;
  89. } else {
  90. ret = -EC_ERR_INVAL;
  91. }
  92. return (int)ret;
  93. }
  94. int ec_osal_sem_give(ec_osal_sem_t sem)
  95. {
  96. return (int)tx_semaphore_put((TX_SEMAPHORE *)sem);
  97. }
  98. void ec_osal_sem_reset(ec_osal_sem_t sem)
  99. {
  100. tx_semaphore_get((TX_SEMAPHORE *)sem, 0);
  101. }
  102. ec_osal_mutex_t ec_osal_mutex_create(void)
  103. {
  104. TX_MUTEX *mutex_ptr = TX_NULL;
  105. tx_byte_allocate(&ec_byte_pool, (VOID **)&mutex_ptr, sizeof(TX_MUTEX), TX_NO_WAIT);
  106. if (mutex_ptr == TX_NULL) {
  107. EC_LOG_ERR("Create mutex failed\r\n");
  108. while (1) {
  109. }
  110. }
  111. tx_mutex_create(mutex_ptr, "ec_mutx", TX_INHERIT);
  112. return (ec_osal_mutex_t)mutex_ptr;
  113. }
  114. void ec_osal_mutex_delete(ec_osal_mutex_t mutex)
  115. {
  116. tx_mutex_delete((TX_MUTEX *)mutex);
  117. tx_byte_release(mutex);
  118. }
  119. int ec_osal_mutex_take(ec_osal_mutex_t mutex)
  120. {
  121. int ret = 0;
  122. ret = tx_mutex_get((TX_MUTEX *)mutex, TX_WAIT_FOREVER);
  123. if (ret == TX_SUCCESS) {
  124. ret = 0;
  125. } else if ((ret == TX_WAIT_ABORTED) || (ret == TX_NO_INSTANCE)) {
  126. ret = -EC_ERR_TIMEOUT;
  127. } else {
  128. ret = -EC_ERR_INVAL;
  129. }
  130. return (int)ret;
  131. }
  132. int ec_osal_mutex_give(ec_osal_mutex_t mutex)
  133. {
  134. return (int)(tx_mutex_put((TX_MUTEX *)mutex) == TX_SUCCESS) ? 0 : -EC_ERR_INVAL;
  135. }
  136. 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)
  137. {
  138. TX_TIMER *timer_ptr = TX_NULL;
  139. struct ec_osal_timer *timer;
  140. tx_byte_allocate(&ec_byte_pool, (VOID **)&timer, sizeof(struct ec_osal_timer), TX_NO_WAIT);
  141. if (timer == TX_NULL) {
  142. EC_LOG_ERR("Create ec_osal_timer failed\r\n");
  143. while (1) {
  144. }
  145. }
  146. memset(timer, 0, sizeof(struct ec_osal_timer));
  147. tx_byte_allocate(&ec_byte_pool, (VOID **)&timer_ptr, sizeof(TX_TIMER), TX_NO_WAIT);
  148. if (timer_ptr == TX_NULL) {
  149. EC_LOG_ERR("Create TX_TIMER failed\r\n");
  150. while (1) {
  151. }
  152. }
  153. timer->timer = timer_ptr;
  154. timer->timeout_ms = timeout_ms;
  155. timer->is_period = is_period;
  156. if (tx_timer_create(timer_ptr, (CHAR *)name, (void (*)(ULONG))handler, (uintptr_t)argument, 1, is_period ? 1 : 0,
  157. TX_NO_ACTIVATE) != TX_SUCCESS) {
  158. return NULL;
  159. }
  160. return timer;
  161. }
  162. void ec_osal_timer_delete(struct ec_osal_timer *timer)
  163. {
  164. tx_timer_deactivate((TX_TIMER *)timer->timer);
  165. tx_timer_delete((TX_TIMER *)timer->timer);
  166. tx_byte_release(timer->timer);
  167. tx_byte_release(timer);
  168. }
  169. void ec_osal_timer_start(struct ec_osal_timer *timer)
  170. {
  171. if (tx_timer_change((TX_TIMER *)timer->timer, timer->timeout_ms, timer->is_period ? timer->timeout_ms : 0) == TX_SUCCESS) {
  172. /* Call the tx_timer_activate to activates the specified application
  173. timer. The expiration routines of timers that expire at the same
  174. time are executed in the order they were activated. */
  175. if (tx_timer_activate((TX_TIMER *)timer->timer) == TX_SUCCESS) {
  176. /* Return osOK for success */
  177. } else {
  178. /* Return osErrorResource in case of error */
  179. }
  180. } else {
  181. }
  182. }
  183. void ec_osal_timer_stop(struct ec_osal_timer *timer)
  184. {
  185. tx_timer_deactivate((TX_TIMER *)timer->timer);
  186. }
  187. size_t ec_osal_enter_critical_section(void)
  188. {
  189. TX_INTERRUPT_SAVE_AREA
  190. TX_DISABLE
  191. return interrupt_save;
  192. }
  193. void ec_osal_leave_critical_section(size_t flag)
  194. {
  195. int interrupt_save;
  196. interrupt_save = flag;
  197. TX_RESTORE
  198. }
  199. void ec_osal_msleep(uint32_t delay)
  200. {
  201. #if TX_TIMER_TICKS_PER_SECOND != 1000
  202. #error "TX_TIMER_TICKS_PER_SECOND must be 1000"
  203. #endif
  204. tx_thread_sleep(delay);
  205. }
  206. void *ec_osal_malloc(size_t size)
  207. {
  208. CHAR *pointer = TX_NULL;
  209. tx_byte_allocate(&ec_byte_pool, (VOID **)&pointer, size, TX_WAIT_FOREVER);
  210. return pointer;
  211. }
  212. void ec_osal_free(void *ptr)
  213. {
  214. tx_byte_release(ptr);
  215. }