usb_osal_threadx.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usb_osal.h"
  7. #include "usb_errno.h"
  8. #include "usb_config.h"
  9. #include "usb_log.h"
  10. #include "tx_api.h"
  11. /* create bytepool in tx_application_define
  12. *
  13. * tx_byte_pool_create(&usb_byte_pool, "usb byte pool", memory_area, 65536);
  14. */
  15. extern TX_BYTE_POOL usb_byte_pool;
  16. usb_osal_thread_t usb_osal_thread_create(const char *name, uint32_t stack_size, uint32_t prio, usb_thread_entry_t entry, void *args)
  17. {
  18. CHAR *pointer = TX_NULL;
  19. TX_THREAD *thread_ptr = TX_NULL;
  20. tx_byte_allocate(&usb_byte_pool, (VOID **)&thread_ptr, sizeof(TX_THREAD), TX_NO_WAIT);
  21. if (thread_ptr == TX_NULL) {
  22. USB_LOG_ERR("Create thread %s failed\r\n", name);
  23. while (1) {
  24. }
  25. }
  26. tx_byte_allocate(&usb_byte_pool, (VOID **)&pointer, stack_size, TX_NO_WAIT);
  27. if (pointer == TX_NULL) {
  28. USB_LOG_ERR("Create thread %s failed\r\n", name);
  29. while (1) {
  30. }
  31. }
  32. tx_thread_create(thread_ptr, (CHAR *)name, (VOID(*)(ULONG))entry, (uintptr_t)args,
  33. pointer, stack_size,
  34. prio, prio, TX_NO_TIME_SLICE, TX_AUTO_START);
  35. return (usb_osal_thread_t)thread_ptr;
  36. }
  37. void usb_osal_thread_delete(usb_osal_thread_t thread)
  38. {
  39. TX_THREAD *thread_ptr = NULL;
  40. if (thread == NULL) {
  41. /* Call the tx_thread_identify to get the control block pointer of the
  42. currently executing thread. */
  43. thread_ptr = tx_thread_identify();
  44. /* Check if the current running thread pointer is not NULL */
  45. if (thread_ptr != NULL) {
  46. /* Call the tx_thread_terminate to terminates the specified application
  47. thread regardless of whether the thread is suspended or not. A thread
  48. may call this service to terminate itself. */
  49. tx_thread_terminate(thread_ptr);
  50. tx_byte_release(thread_ptr->tx_thread_stack_start);
  51. tx_byte_release(thread_ptr);
  52. }
  53. return;
  54. }
  55. tx_thread_terminate(thread);
  56. tx_byte_release(thread_ptr->tx_thread_stack_start);
  57. tx_byte_release(thread);
  58. }
  59. usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count)
  60. {
  61. TX_SEMAPHORE *sem_ptr = TX_NULL;
  62. tx_byte_allocate(&usb_byte_pool, (VOID **)&sem_ptr, sizeof(TX_SEMAPHORE), TX_NO_WAIT);
  63. if (sem_ptr == TX_NULL) {
  64. USB_LOG_ERR("Create semaphore failed\r\n");
  65. while (1) {
  66. }
  67. }
  68. tx_semaphore_create(sem_ptr, "usbh_sem", initial_count);
  69. return (usb_osal_sem_t)sem_ptr;
  70. }
  71. void usb_osal_sem_delete(usb_osal_sem_t sem)
  72. {
  73. tx_semaphore_delete((TX_SEMAPHORE *)sem);
  74. tx_byte_release(sem);
  75. }
  76. int usb_osal_sem_take(usb_osal_sem_t sem, uint32_t timeout)
  77. {
  78. int ret = 0;
  79. ret = tx_semaphore_get((TX_SEMAPHORE *)sem, timeout);
  80. if (ret == TX_SUCCESS) {
  81. ret = 0;
  82. } else if ((ret == TX_WAIT_ABORTED) || (ret == TX_NO_INSTANCE)) {
  83. ret = -USB_ERR_TIMEOUT;
  84. } else {
  85. ret = -USB_ERR_INVAL;
  86. }
  87. return (int)ret;
  88. }
  89. int usb_osal_sem_give(usb_osal_sem_t sem)
  90. {
  91. return (int)tx_semaphore_put((TX_SEMAPHORE *)sem);
  92. }
  93. void usb_osal_sem_reset(usb_osal_sem_t sem)
  94. {
  95. }
  96. usb_osal_mutex_t usb_osal_mutex_create(void)
  97. {
  98. TX_MUTEX *mutex_ptr = TX_NULL;
  99. tx_byte_allocate(&usb_byte_pool, (VOID **)&mutex_ptr, sizeof(TX_MUTEX), TX_NO_WAIT);
  100. if (mutex_ptr == TX_NULL) {
  101. USB_LOG_ERR("Create mutex failed\r\n");
  102. while (1) {
  103. }
  104. }
  105. tx_mutex_create(mutex_ptr, "usbh_mutx", TX_INHERIT);
  106. return (usb_osal_mutex_t)mutex_ptr;
  107. }
  108. void usb_osal_mutex_delete(usb_osal_mutex_t mutex)
  109. {
  110. tx_mutex_delete((TX_MUTEX *)mutex);
  111. tx_byte_release(mutex);
  112. }
  113. int usb_osal_mutex_take(usb_osal_mutex_t mutex)
  114. {
  115. int ret = 0;
  116. ret = tx_mutex_get((TX_MUTEX *)mutex, TX_WAIT_FOREVER);
  117. if (ret == TX_SUCCESS) {
  118. ret = 0;
  119. } else if ((ret == TX_WAIT_ABORTED) || (ret == TX_NO_INSTANCE)) {
  120. ret = -USB_ERR_TIMEOUT;
  121. } else {
  122. ret = -USB_ERR_INVAL;
  123. }
  124. return (int)ret;
  125. }
  126. int usb_osal_mutex_give(usb_osal_mutex_t mutex)
  127. {
  128. return (int)(tx_mutex_put((TX_MUTEX *)mutex) == TX_SUCCESS) ? 0 : -USB_ERR_INVAL;
  129. }
  130. usb_osal_mq_t usb_osal_mq_create(uint32_t max_msgs)
  131. {
  132. CHAR *pointer = TX_NULL;
  133. TX_QUEUE *queue_ptr = TX_NULL;
  134. tx_byte_allocate(&usb_byte_pool, (VOID **)&queue_ptr, sizeof(TX_QUEUE), TX_NO_WAIT);
  135. if (queue_ptr == TX_NULL) {
  136. USB_LOG_ERR("Create TX_QUEUE failed\r\n");
  137. while (1) {
  138. }
  139. }
  140. tx_byte_allocate(&usb_byte_pool, (VOID **)&pointer, sizeof(uintptr_t) * max_msgs, TX_NO_WAIT);
  141. if (pointer == TX_NULL) {
  142. USB_LOG_ERR("Create mq failed\r\n");
  143. while (1) {
  144. }
  145. }
  146. tx_queue_create(queue_ptr, "usbh_mq", sizeof(uintptr_t) / 4, pointer, sizeof(uintptr_t) * max_msgs);
  147. return (usb_osal_mq_t)queue_ptr;
  148. }
  149. void usb_osal_mq_delete(usb_osal_mq_t mq)
  150. {
  151. tx_queue_delete((TX_QUEUE *)mq);
  152. tx_byte_release(((TX_QUEUE *)mq)->tx_queue_start);
  153. tx_byte_release(mq);
  154. }
  155. int usb_osal_mq_send(usb_osal_mq_t mq, uintptr_t addr)
  156. {
  157. return (tx_queue_send((TX_QUEUE *)mq, &addr, TX_NO_WAIT) == TX_SUCCESS) ? 0 : -USB_ERR_INVAL;
  158. }
  159. int usb_osal_mq_recv(usb_osal_mq_t mq, uintptr_t *addr, uint32_t timeout)
  160. {
  161. int ret = 0;
  162. ret = tx_queue_receive((TX_QUEUE *)mq, addr, timeout);
  163. if (ret == TX_SUCCESS) {
  164. ret = 0;
  165. } else if (ret == TX_QUEUE_EMPTY) {
  166. ret = -USB_ERR_TIMEOUT;
  167. } else {
  168. ret = -USB_ERR_INVAL;
  169. }
  170. return (int)ret;
  171. }
  172. struct usb_osal_timer *usb_osal_timer_create(const char *name, uint32_t timeout_ms, usb_timer_handler_t handler, void *argument, bool is_period)
  173. {
  174. TX_TIMER *timer_ptr = TX_NULL;
  175. struct usb_osal_timer *timer;
  176. tx_byte_allocate(&usb_byte_pool, (VOID **)&timer, sizeof(struct usb_osal_timer), TX_NO_WAIT);
  177. if (timer == TX_NULL) {
  178. USB_LOG_ERR("Create usb_osal_timer failed\r\n");
  179. while (1) {
  180. }
  181. }
  182. memset(timer, 0, sizeof(struct usb_osal_timer));
  183. tx_byte_allocate(&usb_byte_pool, (VOID **)&timer_ptr, sizeof(TX_TIMER), TX_NO_WAIT);
  184. if (timer_ptr == TX_NULL) {
  185. USB_LOG_ERR("Create TX_TIMER failed\r\n");
  186. while (1) {
  187. }
  188. }
  189. timer->timer = timer_ptr;
  190. timer->ticks = timeout_ms;
  191. timer->is_period = is_period;
  192. if (tx_timer_create(timer_ptr, (CHAR *)name, (void (*)(ULONG))handler, (uintptr_t)argument, 1, is_period ? 1 : 0,
  193. TX_NO_ACTIVATE) != TX_SUCCESS) {
  194. return NULL;
  195. }
  196. return timer;
  197. }
  198. void usb_osal_timer_delete(struct usb_osal_timer *timer)
  199. {
  200. tx_timer_deactivate((TX_TIMER *)timer->timer);
  201. tx_timer_delete((TX_TIMER *)timer->timer);
  202. tx_byte_release(timer->timer);
  203. tx_byte_release(timer);
  204. }
  205. void usb_osal_timer_start(struct usb_osal_timer *timer)
  206. {
  207. if (tx_timer_change((TX_TIMER *)timer->timer, timer->ticks, timer->is_period ? timer->ticks : 0) == TX_SUCCESS) {
  208. /* Call the tx_timer_activate to activates the specified application
  209. timer. The expiration routines of timers that expire at the same
  210. time are executed in the order they were activated. */
  211. if (tx_timer_activate((TX_TIMER *)timer->timer) == TX_SUCCESS) {
  212. /* Return osOK for success */
  213. } else {
  214. /* Return osErrorResource in case of error */
  215. }
  216. } else {
  217. }
  218. }
  219. void usb_osal_timer_stop(struct usb_osal_timer *timer)
  220. {
  221. tx_timer_deactivate((TX_TIMER *)timer->timer);
  222. }
  223. size_t usb_osal_enter_critical_section(void)
  224. {
  225. TX_INTERRUPT_SAVE_AREA
  226. TX_DISABLE
  227. return interrupt_save;
  228. }
  229. void usb_osal_leave_critical_section(size_t flag)
  230. {
  231. int interrupt_save;
  232. interrupt_save = flag;
  233. TX_RESTORE
  234. }
  235. void usb_osal_msleep(uint32_t delay)
  236. {
  237. #if TX_TIMER_TICKS_PER_SECOND != 1000
  238. #error "TX_TIMER_TICKS_PER_SECOND must be 1000"
  239. #endif
  240. tx_thread_sleep(delay);
  241. }
  242. void *usb_osal_malloc(size_t size)
  243. {
  244. CHAR *pointer = TX_NULL;
  245. tx_byte_allocate(&usb_byte_pool, (VOID **)&pointer, size, TX_WAIT_FOREVER);
  246. return pointer;
  247. }
  248. void usb_osal_free(void *ptr)
  249. {
  250. tx_byte_release(ptr);
  251. }