usb_osal_threadx.c 8.3 KB

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