| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- /*
- * Copyright (c) 2025, HPMicro
- * SPDX-License-Identifier: Apache-2.0
- */
- #include "usb_osal.h"
- #include "usb_errno.h"
- #include "usb_config.h"
- #include "usb_log.h"
- #include "los_interrupt.h"
- #include "los_task.h"
- #include "los_queue.h"
- #include "los_sem.h"
- #include "los_mux.h"
- #include "los_memory.h"
- #include "los_swtmr.h"
- 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)
- {
- UINT32 task_id;
- UINT32 ret;
- TSK_INIT_PARAM_S task_init_param = {
- .usTaskPrio = prio,
- .pfnTaskEntry = (TSK_ENTRY_FUNC)entry,
- .uwStackSize = stack_size,
- .uwArg = (UINT32)args,
- .pcName = name
- };
- ret = LOS_TaskCreate(&task_id, &task_init_param);
- if (ret != LOS_OK) {
- USB_LOG_ERR("Create task[%s] failed code[%u]\r\n", name, ret);
- while (1) {
- }
- }
- return (usb_osal_thread_t)task_id;
- }
- void usb_osal_thread_delete(usb_osal_thread_t thread)
- {
- UINT32 task_id = (UINT32)thread;
- UINT32 ret;
- if (thread == NULL) {
- task_id = LOS_CurTaskIDGet();
- }
- ret = LOS_TaskDelete(task_id);
- if (ret != LOS_OK) {
- USB_LOG_ERR("Delete task id[%u] failed code[%u]\r\n", task_id, ret);
- while (1) {
- }
- }
- }
- void usb_osal_thread_schedule_other(void)
- {
- UINT32 task_id = LOS_CurTaskIDGet();
- const UINT16 old_priority = LOS_TaskPriGet(task_id);
- LOS_TaskPriSet(task_id, OS_TASK_PRIORITY_LOWEST);
- LOS_TaskYield();
- LOS_TaskPriSet(task_id, old_priority);
- }
- usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count)
- {
- UINT32 sem_handle;
- UINT32 ret;
- ret = LOS_SemCreate(initial_count, &sem_handle);
- if (ret != LOS_OK) {
- USB_LOG_ERR("Create Sem failed code[%u]\r\n", ret);
- while (1) {
- }
- }
- return (usb_osal_sem_t)sem_handle;
- }
- void usb_osal_sem_delete(usb_osal_sem_t sem)
- {
- UINT32 sem_handle = (UINT32)sem;
- UINT32 ret;
- ret = LOS_SemDelete(sem_handle);
- if (ret != LOS_OK) {
- USB_LOG_ERR("Delete sem handle[%u] failed code[%u]\r\n",
- sem_handle, ret);
- while (1) {
- }
- }
- }
- int usb_osal_sem_take(usb_osal_sem_t sem, uint32_t timeout)
- {
- UINT32 sem_handle = (UINT32)sem;
- UINT32 ret;
- ret = LOS_SemPend(sem_handle,
- timeout == USB_OSAL_WAITING_FOREVER ?
- LOS_WAIT_FOREVER :
- timeout);
- return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
- }
- int usb_osal_sem_give(usb_osal_sem_t sem)
- {
- UINT32 sem_handle = (UINT32)sem;
- UINT32 ret;
- ret = LOS_SemPost(sem_handle);
- return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
- }
- void usb_osal_sem_reset(usb_osal_sem_t sem)
- {
- }
- usb_osal_mutex_t usb_osal_mutex_create(void)
- {
- UINT32 mux_handle;
- UINT32 ret;
- ret = LOS_MuxCreate(&mux_handle);
- if (ret != LOS_OK) {
- USB_LOG_ERR("Create mux failed code[%u]\r\n", ret);
- while (1) {
- }
- }
- return (usb_osal_mutex_t)mux_handle;
- }
- void usb_osal_mutex_delete(usb_osal_mutex_t mutex)
- {
- UINT32 mux_handle = (UINT32)mutex;
- UINT32 ret;
- ret = LOS_MuxDelete(mux_handle);
- if (ret != LOS_OK) {
- USB_LOG_ERR("Delete mux handle[%u] failed code[%u]\r\n",
- mux_handle, ret);
- while (1) {
- }
- }
- }
- int usb_osal_mutex_take(usb_osal_mutex_t mutex)
- {
- UINT32 mux_handle = (UINT32)mutex;
- UINT32 ret;
- ret = LOS_MuxPend(mux_handle, LOS_WAIT_FOREVER);
- return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
- }
- int usb_osal_mutex_give(usb_osal_mutex_t mutex)
- {
- UINT32 mux_handle = (UINT32)mutex;
- UINT32 ret;
- ret = LOS_MuxPost(mux_handle);
- return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
- }
- usb_osal_mq_t usb_osal_mq_create(uint32_t max_msgs)
- {
- UINT32 queue_id;
- UINT32 ret;
- ret = LOS_QueueCreate(NULL, max_msgs, &queue_id, 0, sizeof(uintptr_t));
- if (ret != LOS_OK) {
- USB_LOG_ERR("Create queue failed code[%u]\r\n", ret);
- while (1) {
- }
- }
- return (usb_osal_mq_t)queue_id;
- }
- void usb_osal_mq_delete(usb_osal_mq_t mq)
- {
- UINT32 queue_id = (UINT32)mq;
- UINT32 ret;
- ret = LOS_QueueDelete(queue_id);
- if (ret != LOS_OK) {
- USB_LOG_ERR("Delete queue id[%u] failed code[%u]\r\n",
- queue_id, ret);
- while (1) {
- }
- }
- }
- int usb_osal_mq_send(usb_osal_mq_t mq, uintptr_t addr)
- {
- UINT32 queue_id = (UINT32)mq;
- UINT32 ret;
- UINT32 timeout;
- if (OS_INT_ACTIVE)
- timeout = LOS_NO_WAIT;
- else
- timeout = LOS_WAIT_FOREVER;
- ret = LOS_QueueWrite(queue_id, addr, sizeof(uintptr_t), timeout);
- return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
- }
- int usb_osal_mq_recv(usb_osal_mq_t mq, uintptr_t *addr, uint32_t timeout)
- {
- UINT32 queue_id = (UINT32)mq;
- UINT32 ret;
- UINT32 _timeout;
- if (OS_INT_ACTIVE)
- _timeout = LOS_NO_WAIT;
- else
- _timeout = timeout == USB_OSAL_WAITING_FOREVER ? LOS_WAIT_FOREVER : timeout;
- ret = LOS_QueueRead(queue_id, addr, sizeof(uintptr_t), _timeout);
- return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
- }
- 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)
- {
- UINT32 timer_id;
- UINT32 ret;
- struct usb_osal_timer *timer_handle;
- timer_handle = usb_osal_malloc(sizeof(struct usb_osal_timer));
- if (timer_handle == NULL) {
- USB_LOG_ERR("Malloc usb_osal_timer failed\r\n");
- while (1) {
- }
- }
- memset(timer_handle, 0x00, sizeof(struct usb_osal_timer));
- ret = LOS_SwtmrCreate(timeout_ms,
- is_period ? LOS_SWTMR_MODE_PERIOD : LOS_SWTMR_MODE_NO_SELFDELETE,
- (SWTMR_PROC_FUNC)handler, &timer_id, (UINT32)argument
- #if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
- ,
- OS_SWTMR_ROUSES_IGNORE, OS_SWTMR_ALIGN_INSENSITIVE
- #endif
- );
- if (ret != LOS_OK) {
- USB_LOG_ERR("Create software timer failed code[%u]\r\n", ret);
- while (1) {
- }
- }
- timer_handle->handler = handler;
- timer_handle->argument = argument;
- timer_handle->is_period = is_period;
- timer_handle->timeout_ms = timeout_ms;
- timer_handle->timer = (void *)timer_id;
- return timer_handle;
- }
- void usb_osal_timer_delete(struct usb_osal_timer *timer)
- {
- UINT32 timer_id = (UINT32)timer->timer;
- UINT32 ret;
- ret = LOS_SwtmrDelete(timer_id);
- if (ret != LOS_OK) {
- USB_LOG_ERR("Delete software timer id[%u] failed code[%u]\r\n",
- timer_id, ret);
- while (1) {
- }
- }
- usb_osal_free(timer);
- }
- void usb_osal_timer_start(struct usb_osal_timer *timer)
- {
- UINT32 timer_id = (UINT32)timer->timer;
- UINT32 ret;
- ret = LOS_SwtmrStart(timer_id);
- if (ret != LOS_OK) {
- USB_LOG_ERR("Start software timer id[%u] failed code[%u]\r\n",
- timer_id, ret);
- while (1) {
- }
- }
- }
- void usb_osal_timer_stop(struct usb_osal_timer *timer)
- {
- UINT32 timer_id = (UINT32)timer->timer;
- UINT32 ret;
- ret = LOS_SwtmrStop(timer_id);
- if (ret != LOS_OK) {
- USB_LOG_ERR("Stop software timer id[%u] failed code[%u]\r\n",
- timer_id, ret);
- while (1) {
- }
- }
- }
- size_t usb_osal_enter_critical_section(void)
- {
- return LOS_IntLock();
- }
- void usb_osal_leave_critical_section(size_t flag)
- {
- LOS_IntRestore(flag);
- }
- void usb_osal_msleep(uint32_t delay)
- {
- LOS_Msleep(delay);
- }
- void *usb_osal_malloc(size_t size)
- {
- return LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, size);
- }
- void usb_osal_free(void *ptr)
- {
- if (ptr != NULL) {
- LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, ptr);
- }
- }
|