usb_osal_liteos_m.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright (c) 2025, HPMicro
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. #include "usb_osal.h"
  6. #include "usb_errno.h"
  7. #include "usb_config.h"
  8. #include "usb_log.h"
  9. #include "los_interrupt.h"
  10. #include "los_task.h"
  11. #include "los_queue.h"
  12. #include "los_sem.h"
  13. #include "los_mux.h"
  14. #include "los_memory.h"
  15. #include "los_swtmr.h"
  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. UINT32 task_id;
  19. UINT32 ret;
  20. TSK_INIT_PARAM_S task_init_param = {
  21. .usTaskPrio = prio,
  22. .pfnTaskEntry = (TSK_ENTRY_FUNC)entry,
  23. .uwStackSize = stack_size,
  24. .uwArg = (UINT32)args,
  25. .pcName = name
  26. };
  27. ret = LOS_TaskCreate(&task_id, &task_init_param);
  28. if (ret != LOS_OK) {
  29. USB_LOG_ERR("Create task[%s] failed code[%u]\r\n", name, ret);
  30. while (1) {
  31. }
  32. }
  33. return (usb_osal_thread_t)task_id;
  34. }
  35. void usb_osal_thread_delete(usb_osal_thread_t thread)
  36. {
  37. UINT32 task_id = (UINT32)thread;
  38. UINT32 ret;
  39. if (thread == NULL) {
  40. task_id = LOS_CurTaskIDGet();
  41. }
  42. ret = LOS_TaskDelete(task_id);
  43. if (ret != LOS_OK) {
  44. USB_LOG_ERR("Delete task id[%u] failed code[%u]\r\n", task_id, ret);
  45. while (1) {
  46. }
  47. }
  48. }
  49. void usb_osal_thread_schedule_other(void)
  50. {
  51. UINT32 task_id = LOS_CurTaskIDGet();
  52. const UINT16 old_priority = LOS_TaskPriGet(task_id);
  53. LOS_TaskPriSet(task_id, OS_TASK_PRIORITY_LOWEST);
  54. LOS_TaskYield();
  55. LOS_TaskPriSet(task_id, old_priority);
  56. }
  57. usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count)
  58. {
  59. UINT32 sem_handle;
  60. UINT32 ret;
  61. ret = LOS_SemCreate(initial_count, &sem_handle);
  62. if (ret != LOS_OK) {
  63. USB_LOG_ERR("Create Sem failed code[%u]\r\n", ret);
  64. while (1) {
  65. }
  66. }
  67. return (usb_osal_sem_t)sem_handle;
  68. }
  69. void usb_osal_sem_delete(usb_osal_sem_t sem)
  70. {
  71. UINT32 sem_handle = (UINT32)sem;
  72. UINT32 ret;
  73. ret = LOS_SemDelete(sem_handle);
  74. if (ret != LOS_OK) {
  75. USB_LOG_ERR("Delete sem handle[%u] failed code[%u]\r\n",
  76. sem_handle, ret);
  77. while (1) {
  78. }
  79. }
  80. }
  81. int usb_osal_sem_take(usb_osal_sem_t sem, uint32_t timeout)
  82. {
  83. UINT32 sem_handle = (UINT32)sem;
  84. UINT32 ret;
  85. ret = LOS_SemPend(sem_handle,
  86. timeout == USB_OSAL_WAITING_FOREVER ?
  87. LOS_WAIT_FOREVER :
  88. timeout);
  89. return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
  90. }
  91. int usb_osal_sem_give(usb_osal_sem_t sem)
  92. {
  93. UINT32 sem_handle = (UINT32)sem;
  94. UINT32 ret;
  95. ret = LOS_SemPost(sem_handle);
  96. return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
  97. }
  98. void usb_osal_sem_reset(usb_osal_sem_t sem)
  99. {
  100. }
  101. usb_osal_mutex_t usb_osal_mutex_create(void)
  102. {
  103. UINT32 mux_handle;
  104. UINT32 ret;
  105. ret = LOS_MuxCreate(&mux_handle);
  106. if (ret != LOS_OK) {
  107. USB_LOG_ERR("Create mux failed code[%u]\r\n", ret);
  108. while (1) {
  109. }
  110. }
  111. return (usb_osal_mutex_t)mux_handle;
  112. }
  113. void usb_osal_mutex_delete(usb_osal_mutex_t mutex)
  114. {
  115. UINT32 mux_handle = (UINT32)mutex;
  116. UINT32 ret;
  117. ret = LOS_MuxDelete(mux_handle);
  118. if (ret != LOS_OK) {
  119. USB_LOG_ERR("Delete mux handle[%u] failed code[%u]\r\n",
  120. mux_handle, ret);
  121. while (1) {
  122. }
  123. }
  124. }
  125. int usb_osal_mutex_take(usb_osal_mutex_t mutex)
  126. {
  127. UINT32 mux_handle = (UINT32)mutex;
  128. UINT32 ret;
  129. ret = LOS_MuxPend(mux_handle, LOS_WAIT_FOREVER);
  130. return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
  131. }
  132. int usb_osal_mutex_give(usb_osal_mutex_t mutex)
  133. {
  134. UINT32 mux_handle = (UINT32)mutex;
  135. UINT32 ret;
  136. ret = LOS_MuxPost(mux_handle);
  137. return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
  138. }
  139. usb_osal_mq_t usb_osal_mq_create(uint32_t max_msgs)
  140. {
  141. UINT32 queue_id;
  142. UINT32 ret;
  143. ret = LOS_QueueCreate(NULL, max_msgs, &queue_id, 0, sizeof(uintptr_t));
  144. if (ret != LOS_OK) {
  145. USB_LOG_ERR("Create queue failed code[%u]\r\n", ret);
  146. while (1) {
  147. }
  148. }
  149. return (usb_osal_mq_t)queue_id;
  150. }
  151. void usb_osal_mq_delete(usb_osal_mq_t mq)
  152. {
  153. UINT32 queue_id = (UINT32)mq;
  154. UINT32 ret;
  155. ret = LOS_QueueDelete(queue_id);
  156. if (ret != LOS_OK) {
  157. USB_LOG_ERR("Delete queue id[%u] failed code[%u]\r\n",
  158. queue_id, ret);
  159. while (1) {
  160. }
  161. }
  162. }
  163. int usb_osal_mq_send(usb_osal_mq_t mq, uintptr_t addr)
  164. {
  165. UINT32 queue_id = (UINT32)mq;
  166. UINT32 ret;
  167. UINT32 timeout;
  168. if (OS_INT_ACTIVE)
  169. timeout = LOS_NO_WAIT;
  170. else
  171. timeout = LOS_WAIT_FOREVER;
  172. ret = LOS_QueueWrite(queue_id, addr, sizeof(uintptr_t), timeout);
  173. return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
  174. }
  175. int usb_osal_mq_recv(usb_osal_mq_t mq, uintptr_t *addr, uint32_t timeout)
  176. {
  177. UINT32 queue_id = (UINT32)mq;
  178. UINT32 ret;
  179. UINT32 _timeout;
  180. if (OS_INT_ACTIVE)
  181. _timeout = LOS_NO_WAIT;
  182. else
  183. _timeout = timeout == USB_OSAL_WAITING_FOREVER ? LOS_WAIT_FOREVER : timeout;
  184. ret = LOS_QueueRead(queue_id, addr, sizeof(uintptr_t), _timeout);
  185. return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
  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. UINT32 timer_id;
  190. UINT32 ret;
  191. struct usb_osal_timer *timer_handle;
  192. timer_handle = usb_osal_malloc(sizeof(struct usb_osal_timer));
  193. if (timer_handle == NULL) {
  194. USB_LOG_ERR("Malloc usb_osal_timer failed\r\n");
  195. while (1) {
  196. }
  197. }
  198. memset(timer_handle, 0x00, sizeof(struct usb_osal_timer));
  199. ret = LOS_SwtmrCreate(timeout_ms,
  200. is_period ? LOS_SWTMR_MODE_PERIOD : LOS_SWTMR_MODE_NO_SELFDELETE,
  201. (SWTMR_PROC_FUNC)handler, &timer_id, (UINT32)argument
  202. #if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
  203. ,
  204. OS_SWTMR_ROUSES_IGNORE, OS_SWTMR_ALIGN_INSENSITIVE
  205. #endif
  206. );
  207. if (ret != LOS_OK) {
  208. USB_LOG_ERR("Create software timer failed code[%u]\r\n", ret);
  209. while (1) {
  210. }
  211. }
  212. timer_handle->handler = handler;
  213. timer_handle->argument = argument;
  214. timer_handle->is_period = is_period;
  215. timer_handle->timeout_ms = timeout_ms;
  216. timer_handle->timer = (void *)timer_id;
  217. return timer_handle;
  218. }
  219. void usb_osal_timer_delete(struct usb_osal_timer *timer)
  220. {
  221. UINT32 timer_id = (UINT32)timer->timer;
  222. UINT32 ret;
  223. ret = LOS_SwtmrDelete(timer_id);
  224. if (ret != LOS_OK) {
  225. USB_LOG_ERR("Delete software timer id[%u] failed code[%u]\r\n",
  226. timer_id, ret);
  227. while (1) {
  228. }
  229. }
  230. usb_osal_free(timer);
  231. }
  232. void usb_osal_timer_start(struct usb_osal_timer *timer)
  233. {
  234. UINT32 timer_id = (UINT32)timer->timer;
  235. UINT32 ret;
  236. ret = LOS_SwtmrStart(timer_id);
  237. if (ret != LOS_OK) {
  238. USB_LOG_ERR("Start software timer id[%u] failed code[%u]\r\n",
  239. timer_id, ret);
  240. while (1) {
  241. }
  242. }
  243. }
  244. void usb_osal_timer_stop(struct usb_osal_timer *timer)
  245. {
  246. UINT32 timer_id = (UINT32)timer->timer;
  247. UINT32 ret;
  248. ret = LOS_SwtmrStop(timer_id);
  249. if (ret != LOS_OK) {
  250. USB_LOG_ERR("Stop software timer id[%u] failed code[%u]\r\n",
  251. timer_id, ret);
  252. while (1) {
  253. }
  254. }
  255. }
  256. size_t usb_osal_enter_critical_section(void)
  257. {
  258. return LOS_IntLock();
  259. }
  260. void usb_osal_leave_critical_section(size_t flag)
  261. {
  262. LOS_IntRestore(flag);
  263. }
  264. void usb_osal_msleep(uint32_t delay)
  265. {
  266. LOS_Msleep(delay);
  267. }
  268. void *usb_osal_malloc(size_t size)
  269. {
  270. return LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, size);
  271. }
  272. void usb_osal_free(void *ptr)
  273. {
  274. if (ptr != NULL) {
  275. LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, ptr);
  276. }
  277. }