usb_osal_zephyr.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c) 2025, 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 <version.h>
  11. #if (KERNELVERSION >= 0x3020000)
  12. #include <zephyr/kernel.h>
  13. #else
  14. #include <kernel.h>
  15. #endif
  16. struct release_thread_work {
  17. struct k_work work;
  18. usb_osal_thread_t thread;
  19. };
  20. 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)
  21. {
  22. k_tid_t tid = NULL;
  23. struct k_thread *thread = (struct k_thread *)k_aligned_alloc(8, sizeof(struct k_thread) + stack_size);
  24. k_thread_stack_t *stack = (k_thread_stack_t *)thread;
  25. if (thread == NULL) {
  26. USB_LOG_ERR("Create thread faild\r\n");
  27. return NULL;
  28. }
  29. tid = k_thread_create(thread, (k_thread_stack_t *)&stack[sizeof(struct k_thread)],
  30. stack_size,
  31. (k_thread_entry_t)entry,
  32. args, NULL, NULL,
  33. prio,
  34. 0,
  35. K_NO_WAIT);
  36. #if defined(CONFIG_THREAD_NAME)
  37. k_thread_name_set(tid, name);
  38. #endif
  39. return (usb_osal_thread_t)tid;
  40. }
  41. static void release_thread_handler(struct k_work *work)
  42. {
  43. struct release_thread_work *release_work = (struct release_thread_work *)work;
  44. k_free(release_work->thread);
  45. k_work_cancel(work);
  46. k_free(release_work);
  47. }
  48. void usb_osal_thread_delete(usb_osal_thread_t thread)
  49. {
  50. struct release_thread_work *release_work;
  51. if (thread == NULL) {
  52. #if (KERNELVERSION >= 0x3070000)
  53. thread = k_sched_current_thread_query();
  54. #else
  55. thread = z_current_get();
  56. #endif
  57. release_work = k_malloc(sizeof(struct release_thread_work));
  58. release_work->thread = thread;
  59. k_work_init(&release_work->work, release_thread_handler);
  60. k_work_submit(&release_work->work);
  61. k_thread_abort(thread);
  62. return;
  63. }
  64. k_thread_abort(thread);
  65. k_free(thread);
  66. }
  67. void usb_osal_thread_schedule_other(void)
  68. {
  69. #if (KERNELVERSION >= 0x3070000)
  70. struct k_thread *current_thread = k_sched_current_thread_query();
  71. #else
  72. struct k_thread *current_thread = z_current_get();
  73. #endif
  74. const int old_priority = k_thread_priority_get(current_thread);
  75. k_thread_priority_set(current_thread, K_LOWEST_APPLICATION_THREAD_PRIO);
  76. k_yield();
  77. k_thread_priority_set(current_thread, old_priority);
  78. }
  79. usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count)
  80. {
  81. struct k_sem *sem;
  82. sem = k_malloc(sizeof(struct k_sem));
  83. if (sem == NULL) {
  84. USB_LOG_ERR("Create semaphore faild\r\n");
  85. return NULL;
  86. }
  87. k_sem_init(sem, initial_count, 1);
  88. return (usb_osal_sem_t)sem;
  89. }
  90. void usb_osal_sem_delete(usb_osal_sem_t sem)
  91. {
  92. while (k_sem_take((struct k_sem *)sem, K_NO_WAIT) != 0) {
  93. break;
  94. }
  95. k_free((struct k_sem *)sem);
  96. }
  97. int usb_osal_sem_take(usb_osal_sem_t sem, uint32_t timeout)
  98. {
  99. if (timeout == USB_OSAL_WAITING_FOREVER) {
  100. return (k_sem_take((struct k_sem *)sem, K_FOREVER) == 0) ? 0 : -USB_ERR_TIMEOUT;
  101. } else {
  102. return (k_sem_take((struct k_sem *)sem, K_MSEC(timeout)) == 0) ? 0 : -USB_ERR_TIMEOUT;
  103. }
  104. }
  105. int usb_osal_sem_give(usb_osal_sem_t sem)
  106. {
  107. k_sem_give((struct k_sem *)sem);
  108. return 0;
  109. }
  110. void usb_osal_sem_reset(usb_osal_sem_t sem)
  111. {
  112. k_sem_reset((struct k_sem *)sem);
  113. }
  114. usb_osal_mutex_t usb_osal_mutex_create(void)
  115. {
  116. struct k_mutex *mutex;
  117. mutex = k_malloc(sizeof(struct k_mutex));
  118. if (mutex == NULL) {
  119. USB_LOG_ERR("Create mutex faild\r\n");
  120. return NULL;
  121. }
  122. k_mutex_init(mutex);
  123. return (usb_osal_mutex_t)mutex;
  124. }
  125. void usb_osal_mutex_delete(usb_osal_mutex_t mutex)
  126. {
  127. k_free((struct k_mutex *)mutex);
  128. }
  129. int usb_osal_mutex_take(usb_osal_mutex_t mutex)
  130. {
  131. return (k_mutex_lock((struct k_mutex *)mutex, K_FOREVER) == 0) ? 0 : -USB_ERR_TIMEOUT;
  132. }
  133. int usb_osal_mutex_give(usb_osal_mutex_t mutex)
  134. {
  135. return (k_mutex_unlock((struct k_mutex *)mutex) == 0) ? 0 : -USB_ERR_INVAL;
  136. }
  137. usb_osal_mq_t usb_osal_mq_create(uint32_t max_msgs)
  138. {
  139. struct k_msgq *msgq;
  140. msgq = k_malloc(sizeof(struct k_msgq));
  141. if (msgq == NULL) {
  142. USB_LOG_ERR("Create message queue faild\r\n");
  143. return NULL;
  144. }
  145. if (k_msgq_alloc_init(msgq, sizeof(uintptr_t), max_msgs) != 0) {
  146. return NULL;
  147. }
  148. return (usb_osal_mq_t)msgq;
  149. }
  150. void usb_osal_mq_delete(usb_osal_mq_t mq)
  151. {
  152. struct k_msgq *msgq;
  153. msgq = (struct k_msgq *)mq;
  154. k_msgq_purge(msgq);
  155. k_free(msgq->buffer_start);
  156. k_free(msgq);
  157. }
  158. int usb_osal_mq_send(usb_osal_mq_t mq, uintptr_t addr)
  159. {
  160. struct k_msgq *msgq;
  161. msgq = (struct k_msgq *)mq;
  162. if (k_is_in_isr()) {
  163. return (k_msgq_put(msgq, &addr, K_NO_WAIT) == 0) ? 0 : -USB_ERR_TIMEOUT;
  164. } else {
  165. return (k_msgq_put(msgq, &addr, K_FOREVER) == 0) ? 0 : -USB_ERR_TIMEOUT;
  166. }
  167. }
  168. int usb_osal_mq_recv(usb_osal_mq_t mq, uintptr_t *addr, uint32_t timeout)
  169. {
  170. struct k_msgq *msgq;
  171. msgq = (struct k_msgq *)mq;
  172. if (k_is_in_isr()) {
  173. return (k_msgq_get(msgq, addr, K_NO_WAIT) == 0) ? 0 : -USB_ERR_TIMEOUT;
  174. } else {
  175. if (timeout == USB_OSAL_WAITING_FOREVER) {
  176. return (k_msgq_get(msgq, addr, K_FOREVER) == 0) ? 0 : -USB_ERR_TIMEOUT;
  177. } else {
  178. return (k_msgq_get(msgq, addr, K_MSEC(timeout)) == 0) ? 0 : -USB_ERR_TIMEOUT;
  179. }
  180. }
  181. }
  182. static void zephyr_timer_wrapper(struct k_timer *ktimer)
  183. {
  184. struct usb_osal_timer *timer = (struct usb_osal_timer *)ktimer->user_data;
  185. timer->handler(timer->argument);
  186. }
  187. 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)
  188. {
  189. struct usb_osal_timer *timer;
  190. (void)name;
  191. timer = k_malloc(sizeof(struct usb_osal_timer));
  192. if (timer == NULL) {
  193. USB_LOG_ERR("Create timer faild\r\n");
  194. return NULL;
  195. }
  196. memset(timer, 0, sizeof(struct usb_osal_timer));
  197. timer->timer = k_malloc(sizeof(struct k_timer));
  198. if (timer->timer == NULL) {
  199. USB_LOG_ERR("Create timer faild\r\n");
  200. return NULL;
  201. }
  202. timer->handler = handler;
  203. timer->argument = argument;
  204. timer->is_period = is_period;
  205. timer->timeout_ms = timeout_ms;
  206. k_timer_init(timer->timer, zephyr_timer_wrapper, NULL);
  207. k_timer_user_data_set(timer->timer, timer);
  208. return timer;
  209. }
  210. void usb_osal_timer_delete(struct usb_osal_timer *timer)
  211. {
  212. k_timer_stop(timer->timer);
  213. k_free(timer->timer);
  214. k_free(timer);
  215. }
  216. void usb_osal_timer_start(struct usb_osal_timer *timer)
  217. {
  218. if (timer->is_period) {
  219. k_timer_start(timer->timer, K_MSEC(timer->timeout_ms), K_MSEC(timer->timeout_ms));
  220. } else {
  221. k_timer_start(timer->timer, K_MSEC(timer->timeout_ms), K_NO_WAIT);
  222. }
  223. }
  224. void usb_osal_timer_stop(struct usb_osal_timer *timer)
  225. {
  226. k_timer_stop(timer->timer);
  227. }
  228. size_t usb_osal_enter_critical_section(void)
  229. {
  230. return irq_lock();
  231. }
  232. void usb_osal_leave_critical_section(size_t flag)
  233. {
  234. irq_unlock(flag);
  235. }
  236. void usb_osal_msleep(uint32_t delay)
  237. {
  238. k_sleep(K_MSEC(delay));
  239. }
  240. void *usb_osal_malloc(size_t size)
  241. {
  242. return k_malloc(size);
  243. }
  244. void usb_osal_free(void *ptr)
  245. {
  246. k_free(ptr);
  247. }