usb_osal_threadx.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. tx_semaphore_get((TX_SEMAPHORE *)sem, 0);
  96. }
  97. usb_osal_mutex_t usb_osal_mutex_create(void)
  98. {
  99. TX_MUTEX *mutex_ptr = TX_NULL;
  100. tx_byte_allocate(&usb_byte_pool, (VOID **)&mutex_ptr, sizeof(TX_MUTEX), TX_NO_WAIT);
  101. if (mutex_ptr == TX_NULL) {
  102. USB_LOG_ERR("Create mutex failed\r\n");
  103. while (1) {
  104. }
  105. }
  106. tx_mutex_create(mutex_ptr, "usbh_mutx", TX_INHERIT);
  107. return (usb_osal_mutex_t)mutex_ptr;
  108. }
  109. void usb_osal_mutex_delete(usb_osal_mutex_t mutex)
  110. {
  111. tx_mutex_delete((TX_MUTEX *)mutex);
  112. tx_byte_release(mutex);
  113. }
  114. int usb_osal_mutex_take(usb_osal_mutex_t mutex)
  115. {
  116. int ret = 0;
  117. ret = tx_mutex_get((TX_MUTEX *)mutex, TX_WAIT_FOREVER);
  118. if (ret == TX_SUCCESS) {
  119. ret = 0;
  120. } else if ((ret == TX_WAIT_ABORTED) || (ret == TX_NO_INSTANCE)) {
  121. ret = -USB_ERR_TIMEOUT;
  122. } else {
  123. ret = -USB_ERR_INVAL;
  124. }
  125. return (int)ret;
  126. }
  127. int usb_osal_mutex_give(usb_osal_mutex_t mutex)
  128. {
  129. return (int)(tx_mutex_put((TX_MUTEX *)mutex) == TX_SUCCESS) ? 0 : -USB_ERR_INVAL;
  130. }
  131. usb_osal_mq_t usb_osal_mq_create(uint32_t max_msgs)
  132. {
  133. CHAR *pointer = TX_NULL;
  134. TX_QUEUE *queue_ptr = TX_NULL;
  135. tx_byte_allocate(&usb_byte_pool, (VOID **)&queue_ptr, sizeof(TX_QUEUE), TX_NO_WAIT);
  136. if (queue_ptr == TX_NULL) {
  137. USB_LOG_ERR("Create TX_QUEUE failed\r\n");
  138. while (1) {
  139. }
  140. }
  141. tx_byte_allocate(&usb_byte_pool, (VOID **)&pointer, sizeof(uintptr_t) * max_msgs, TX_NO_WAIT);
  142. if (pointer == TX_NULL) {
  143. USB_LOG_ERR("Create mq failed\r\n");
  144. while (1) {
  145. }
  146. }
  147. tx_queue_create(queue_ptr, "usbh_mq", sizeof(uintptr_t) / 4, pointer, sizeof(uintptr_t) * max_msgs);
  148. return (usb_osal_mq_t)queue_ptr;
  149. }
  150. void usb_osal_mq_delete(usb_osal_mq_t mq)
  151. {
  152. tx_queue_delete((TX_QUEUE *)mq);
  153. tx_byte_release(((TX_QUEUE *)mq)->tx_queue_start);
  154. tx_byte_release(mq);
  155. }
  156. int usb_osal_mq_send(usb_osal_mq_t mq, uintptr_t addr)
  157. {
  158. return (tx_queue_send((TX_QUEUE *)mq, &addr, TX_NO_WAIT) == TX_SUCCESS) ? 0 : -USB_ERR_INVAL;
  159. }
  160. int usb_osal_mq_recv(usb_osal_mq_t mq, uintptr_t *addr, uint32_t timeout)
  161. {
  162. int ret = 0;
  163. ret = tx_queue_receive((TX_QUEUE *)mq, addr, timeout);
  164. if (ret == TX_SUCCESS) {
  165. ret = 0;
  166. } else if (ret == TX_QUEUE_EMPTY) {
  167. ret = -USB_ERR_TIMEOUT;
  168. } else {
  169. ret = -USB_ERR_INVAL;
  170. }
  171. return (int)ret;
  172. }
  173. 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)
  174. {
  175. TX_TIMER *timer_ptr = TX_NULL;
  176. struct usb_osal_timer *timer;
  177. tx_byte_allocate(&usb_byte_pool, (VOID **)&timer, sizeof(struct usb_osal_timer), TX_NO_WAIT);
  178. if (timer == TX_NULL) {
  179. USB_LOG_ERR("Create usb_osal_timer failed\r\n");
  180. while (1) {
  181. }
  182. }
  183. memset(timer, 0, sizeof(struct usb_osal_timer));
  184. tx_byte_allocate(&usb_byte_pool, (VOID **)&timer_ptr, sizeof(TX_TIMER), TX_NO_WAIT);
  185. if (timer_ptr == TX_NULL) {
  186. USB_LOG_ERR("Create TX_TIMER failed\r\n");
  187. while (1) {
  188. }
  189. }
  190. timer->timer = timer_ptr;
  191. timer->ticks = timeout_ms;
  192. timer->is_period = is_period;
  193. if (tx_timer_create(timer_ptr, (CHAR *)name, (void (*)(ULONG))handler, (uintptr_t)argument, 1, is_period ? 1 : 0,
  194. TX_NO_ACTIVATE) != TX_SUCCESS) {
  195. return NULL;
  196. }
  197. return timer;
  198. }
  199. void usb_osal_timer_delete(struct usb_osal_timer *timer)
  200. {
  201. tx_timer_deactivate((TX_TIMER *)timer->timer);
  202. tx_timer_delete((TX_TIMER *)timer->timer);
  203. tx_byte_release(timer->timer);
  204. tx_byte_release(timer);
  205. }
  206. void usb_osal_timer_start(struct usb_osal_timer *timer)
  207. {
  208. if (tx_timer_change((TX_TIMER *)timer->timer, timer->ticks, timer->is_period ? timer->ticks : 0) == TX_SUCCESS) {
  209. /* Call the tx_timer_activate to activates the specified application
  210. timer. The expiration routines of timers that expire at the same
  211. time are executed in the order they were activated. */
  212. if (tx_timer_activate((TX_TIMER *)timer->timer) == TX_SUCCESS) {
  213. /* Return osOK for success */
  214. } else {
  215. /* Return osErrorResource in case of error */
  216. }
  217. } else {
  218. }
  219. }
  220. void usb_osal_timer_stop(struct usb_osal_timer *timer)
  221. {
  222. tx_timer_deactivate((TX_TIMER *)timer->timer);
  223. }
  224. size_t usb_osal_enter_critical_section(void)
  225. {
  226. TX_INTERRUPT_SAVE_AREA
  227. TX_DISABLE
  228. return interrupt_save;
  229. }
  230. void usb_osal_leave_critical_section(size_t flag)
  231. {
  232. int interrupt_save;
  233. interrupt_save = flag;
  234. TX_RESTORE
  235. }
  236. void usb_osal_msleep(uint32_t delay)
  237. {
  238. #if TX_TIMER_TICKS_PER_SECOND != 1000
  239. #error "TX_TIMER_TICKS_PER_SECOND must be 1000"
  240. #endif
  241. tx_thread_sleep(delay);
  242. }
  243. void *usb_osal_malloc(size_t size)
  244. {
  245. CHAR *pointer = TX_NULL;
  246. tx_byte_allocate(&usb_byte_pool, (VOID **)&pointer, size, TX_WAIT_FOREVER);
  247. return pointer;
  248. }
  249. void usb_osal_free(void *ptr)
  250. {
  251. tx_byte_release(ptr);
  252. }