usb_osal_threadx.c 8.4 KB

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