usb_osal_liteos_m.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. usb_osal_sem_t usb_osal_sem_create_counting(uint32_t max_count)
  70. {
  71. return usb_osal_sem_create(0);
  72. }
  73. void usb_osal_sem_delete(usb_osal_sem_t sem)
  74. {
  75. UINT32 sem_handle = (UINT32)sem;
  76. UINT32 ret;
  77. ret = LOS_SemDelete(sem_handle);
  78. if (ret != LOS_OK) {
  79. USB_LOG_ERR("Delete sem handle[%u] failed code[%u]\r\n",
  80. sem_handle, ret);
  81. while (1) {
  82. }
  83. }
  84. }
  85. int usb_osal_sem_take(usb_osal_sem_t sem, uint32_t timeout)
  86. {
  87. UINT32 sem_handle = (UINT32)sem;
  88. UINT32 ret;
  89. ret = LOS_SemPend(sem_handle,
  90. timeout == USB_OSAL_WAITING_FOREVER ?
  91. LOS_WAIT_FOREVER :
  92. timeout);
  93. return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
  94. }
  95. int usb_osal_sem_give(usb_osal_sem_t sem)
  96. {
  97. UINT32 sem_handle = (UINT32)sem;
  98. UINT32 ret;
  99. ret = LOS_SemPost(sem_handle);
  100. return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
  101. }
  102. void usb_osal_sem_reset(usb_osal_sem_t sem)
  103. {
  104. }
  105. usb_osal_mutex_t usb_osal_mutex_create(void)
  106. {
  107. UINT32 mux_handle;
  108. UINT32 ret;
  109. ret = LOS_MuxCreate(&mux_handle);
  110. if (ret != LOS_OK) {
  111. USB_LOG_ERR("Create mux failed code[%u]\r\n", ret);
  112. while (1) {
  113. }
  114. }
  115. return (usb_osal_mutex_t)mux_handle;
  116. }
  117. void usb_osal_mutex_delete(usb_osal_mutex_t mutex)
  118. {
  119. UINT32 mux_handle = (UINT32)mutex;
  120. UINT32 ret;
  121. ret = LOS_MuxDelete(mux_handle);
  122. if (ret != LOS_OK) {
  123. USB_LOG_ERR("Delete mux handle[%u] failed code[%u]\r\n",
  124. mux_handle, ret);
  125. while (1) {
  126. }
  127. }
  128. }
  129. int usb_osal_mutex_take(usb_osal_mutex_t mutex)
  130. {
  131. UINT32 mux_handle = (UINT32)mutex;
  132. UINT32 ret;
  133. ret = LOS_MuxPend(mux_handle, LOS_WAIT_FOREVER);
  134. return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
  135. }
  136. int usb_osal_mutex_give(usb_osal_mutex_t mutex)
  137. {
  138. UINT32 mux_handle = (UINT32)mutex;
  139. UINT32 ret;
  140. ret = LOS_MuxPost(mux_handle);
  141. return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
  142. }
  143. usb_osal_mq_t usb_osal_mq_create(uint32_t max_msgs)
  144. {
  145. UINT32 queue_id;
  146. UINT32 ret;
  147. ret = LOS_QueueCreate(NULL, max_msgs, &queue_id, 0, sizeof(uintptr_t));
  148. if (ret != LOS_OK) {
  149. USB_LOG_ERR("Create queue failed code[%u]\r\n", ret);
  150. while (1) {
  151. }
  152. }
  153. return (usb_osal_mq_t)queue_id;
  154. }
  155. void usb_osal_mq_delete(usb_osal_mq_t mq)
  156. {
  157. UINT32 queue_id = (UINT32)mq;
  158. UINT32 ret;
  159. ret = LOS_QueueDelete(queue_id);
  160. if (ret != LOS_OK) {
  161. USB_LOG_ERR("Delete queue id[%u] failed code[%u]\r\n",
  162. queue_id, ret);
  163. while (1) {
  164. }
  165. }
  166. }
  167. int usb_osal_mq_send(usb_osal_mq_t mq, uintptr_t addr)
  168. {
  169. UINT32 queue_id = (UINT32)mq;
  170. UINT32 ret;
  171. UINT32 timeout;
  172. if (OS_INT_ACTIVE)
  173. timeout = LOS_NO_WAIT;
  174. else
  175. timeout = LOS_WAIT_FOREVER;
  176. ret = LOS_QueueWrite(queue_id, addr, sizeof(uintptr_t), timeout);
  177. return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
  178. }
  179. int usb_osal_mq_recv(usb_osal_mq_t mq, uintptr_t *addr, uint32_t timeout)
  180. {
  181. UINT32 queue_id = (UINT32)mq;
  182. UINT32 ret;
  183. UINT32 _timeout;
  184. if (OS_INT_ACTIVE)
  185. _timeout = LOS_NO_WAIT;
  186. else
  187. _timeout = timeout == USB_OSAL_WAITING_FOREVER ? LOS_WAIT_FOREVER : timeout;
  188. ret = LOS_QueueRead(queue_id, addr, sizeof(uintptr_t), _timeout);
  189. return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
  190. }
  191. 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)
  192. {
  193. UINT32 timer_id;
  194. UINT32 ret;
  195. struct usb_osal_timer *timer_handle;
  196. timer_handle = usb_osal_malloc(sizeof(struct usb_osal_timer));
  197. if (timer_handle == NULL) {
  198. USB_LOG_ERR("Malloc usb_osal_timer failed\r\n");
  199. while (1) {
  200. }
  201. }
  202. memset(timer_handle, 0x00, sizeof(struct usb_osal_timer));
  203. ret = LOS_SwtmrCreate(timeout_ms,
  204. is_period ? LOS_SWTMR_MODE_PERIOD : LOS_SWTMR_MODE_NO_SELFDELETE,
  205. (SWTMR_PROC_FUNC)handler, &timer_id, (UINT32)argument
  206. #if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
  207. ,
  208. OS_SWTMR_ROUSES_IGNORE, OS_SWTMR_ALIGN_INSENSITIVE
  209. #endif
  210. );
  211. if (ret != LOS_OK) {
  212. USB_LOG_ERR("Create software timer failed code[%u]\r\n", ret);
  213. while (1) {
  214. }
  215. }
  216. timer_handle->handler = handler;
  217. timer_handle->argument = argument;
  218. timer_handle->is_period = is_period;
  219. timer_handle->timeout_ms = timeout_ms;
  220. timer_handle->timer = (void *)timer_id;
  221. return timer_handle;
  222. }
  223. void usb_osal_timer_delete(struct usb_osal_timer *timer)
  224. {
  225. UINT32 timer_id = (UINT32)timer->timer;
  226. UINT32 ret;
  227. ret = LOS_SwtmrDelete(timer_id);
  228. if (ret != LOS_OK) {
  229. USB_LOG_ERR("Delete software timer id[%u] failed code[%u]\r\n",
  230. timer_id, ret);
  231. while (1) {
  232. }
  233. }
  234. usb_osal_free(timer);
  235. }
  236. void usb_osal_timer_start(struct usb_osal_timer *timer)
  237. {
  238. UINT32 timer_id = (UINT32)timer->timer;
  239. UINT32 ret;
  240. ret = LOS_SwtmrStart(timer_id);
  241. if (ret != LOS_OK) {
  242. USB_LOG_ERR("Start software timer id[%u] failed code[%u]\r\n",
  243. timer_id, ret);
  244. while (1) {
  245. }
  246. }
  247. }
  248. void usb_osal_timer_stop(struct usb_osal_timer *timer)
  249. {
  250. UINT32 timer_id = (UINT32)timer->timer;
  251. UINT32 ret;
  252. ret = LOS_SwtmrStop(timer_id);
  253. if (ret != LOS_OK) {
  254. USB_LOG_ERR("Stop software timer id[%u] failed code[%u]\r\n",
  255. timer_id, ret);
  256. while (1) {
  257. }
  258. }
  259. }
  260. size_t usb_osal_enter_critical_section(void)
  261. {
  262. return LOS_IntLock();
  263. }
  264. void usb_osal_leave_critical_section(size_t flag)
  265. {
  266. LOS_IntRestore(flag);
  267. }
  268. void usb_osal_msleep(uint32_t delay)
  269. {
  270. LOS_Msleep(delay);
  271. }
  272. void *usb_osal_malloc(size_t size)
  273. {
  274. return LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, size);
  275. }
  276. void usb_osal_free(void *ptr)
  277. {
  278. if (ptr != NULL) {
  279. LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, ptr);
  280. }
  281. }