From 8a7979378d8dd119b2bc5b014e6b38ec20099c83 Mon Sep 17 00:00:00 2001 From: sakumisu <1203593632@qq.com> Date: Sat, 31 Aug 2024 18:22:33 +0800 Subject: [PATCH] update(osal): add error log and assert when alloc fail, and stop working --- osal/usb_osal_freertos.c | 31 +++++++++++++++++++++++++++---- osal/usb_osal_rtthread.c | 32 +++++++++++++++++++++++++++++--- osal/usb_osal_threadx.c | 34 ++++++++++++++++++++++++++-------- 3 files changed, 82 insertions(+), 15 deletions(-) diff --git a/osal/usb_osal_freertos.c b/osal/usb_osal_freertos.c index 490366a3..20bfa1f2 100644 --- a/osal/usb_osal_freertos.c +++ b/osal/usb_osal_freertos.c @@ -5,6 +5,8 @@ */ #include "usb_osal.h" #include "usb_errno.h" +#include "usb_config.h" +#include "usb_log.h" #include #include "semphr.h" #include "timers.h" @@ -15,6 +17,11 @@ usb_osal_thread_t usb_osal_thread_create(const char *name, uint32_t stack_size, TaskHandle_t htask = NULL; stack_size /= sizeof(StackType_t); xTaskCreate(entry, name, stack_size, args, configMAX_PRIORITIES - 1 - prio, &htask); + if (htask == NULL) { + USB_LOG_ERR("Create thread %s failed\r\n", name); + while (1) { + } + } return (usb_osal_thread_t)htask; } @@ -25,7 +32,13 @@ void usb_osal_thread_delete(usb_osal_thread_t thread) usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count) { - return (usb_osal_sem_t)xSemaphoreCreateCounting(1, initial_count); + usb_osal_sem_t sem = (usb_osal_sem_t)xSemaphoreCreateCounting(1, initial_count); + if (sem == NULL) { + USB_LOG_ERR("Create semaphore failed\r\n"); + while (1) { + } + } + return sem; } void usb_osal_sem_delete(usb_osal_sem_t sem) @@ -66,7 +79,13 @@ void usb_osal_sem_reset(usb_osal_sem_t sem) usb_osal_mutex_t usb_osal_mutex_create(void) { - return (usb_osal_mutex_t)xSemaphoreCreateMutex(); + usb_osal_mutex_t mutex = (usb_osal_mutex_t)xSemaphoreCreateMutex(); + if (mutex == NULL) { + USB_LOG_ERR("Create mutex failed\r\n"); + while (1) { + } + } + return mutex; } void usb_osal_mutex_delete(usb_osal_mutex_t mutex) @@ -131,7 +150,9 @@ struct usb_osal_timer *usb_osal_timer_create(const char *name, uint32_t timeout_ timer = pvPortMalloc(sizeof(struct usb_osal_timer)); if (timer == NULL) { - return NULL; + USB_LOG_ERR("Create usb_osal_timer failed\r\n"); + while (1) { + } } memset(timer, 0, sizeof(struct usb_osal_timer)); @@ -140,7 +161,9 @@ struct usb_osal_timer *usb_osal_timer_create(const char *name, uint32_t timeout_ timer->timer = (void *)xTimerCreate("usb_tim", pdMS_TO_TICKS(timeout_ms), is_period, timer, (TimerCallbackFunction_t)__usb_timeout); if (timer->timer == NULL) { - return NULL; + USB_LOG_ERR("Create timer failed\r\n"); + while (1) { + } } return timer; } diff --git a/osal/usb_osal_rtthread.c b/osal/usb_osal_rtthread.c index e8cbce3a..d355699c 100644 --- a/osal/usb_osal_rtthread.c +++ b/osal/usb_osal_rtthread.c @@ -5,6 +5,8 @@ */ #include "usb_osal.h" #include "usb_errno.h" +#include "usb_config.h" +#include "usb_log.h" #include #include @@ -12,6 +14,11 @@ usb_osal_thread_t usb_osal_thread_create(const char *name, uint32_t stack_size, { rt_thread_t htask; htask = rt_thread_create(name, entry, args, stack_size, prio, 10); + if (htask == NULL) { + USB_LOG_ERR("Create thread %s failed\r\n", name); + while (1) { + } + } rt_thread_startup(htask); return (usb_osal_thread_t)htask; } @@ -27,7 +34,13 @@ void usb_osal_thread_delete(usb_osal_thread_t thread) usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count) { - return (usb_osal_sem_t)rt_sem_create("usbh_sem", initial_count, RT_IPC_FLAG_FIFO); + usb_osal_sem_t sem = (usb_osal_sem_t)rt_sem_create("usbh_sem", initial_count, RT_IPC_FLAG_FIFO); + if (sem == NULL) { + USB_LOG_ERR("Create semaphore failed\r\n"); + while (1) { + } + } + return sem; } void usb_osal_sem_delete(usb_osal_sem_t sem) @@ -68,7 +81,13 @@ void usb_osal_sem_reset(usb_osal_sem_t sem) usb_osal_mutex_t usb_osal_mutex_create(void) { - return (usb_osal_mutex_t)rt_mutex_create("usbh_mutex", RT_IPC_FLAG_FIFO); + usb_osal_mutex_t mutex = (usb_osal_mutex_t)rt_mutex_create("usbh_mutex", RT_IPC_FLAG_FIFO); + if (mutex == NULL) { + USB_LOG_ERR("Create mutex failed\r\n"); + while (1) { + } + } + return mutex; } void usb_osal_mutex_delete(usb_osal_mutex_t mutex) @@ -127,11 +146,18 @@ struct usb_osal_timer *usb_osal_timer_create(const char *name, uint32_t timeout_ struct usb_osal_timer *timer; timer = rt_malloc(sizeof(struct usb_osal_timer)); + if (timer == NULL) { + USB_LOG_ERR("Create usb_osal_timer failed\r\n"); + while (1) { + } + } memset(timer, 0, sizeof(struct usb_osal_timer)); timer->timer = (void *)rt_timer_create(name, handler, argument, timeout_ms, is_period ? (RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER) : (RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER)); if (timer->timer == NULL) { - return NULL; + USB_LOG_ERR("Create timer failed\r\n"); + while (1) { + } } return timer; } diff --git a/osal/usb_osal_threadx.c b/osal/usb_osal_threadx.c index 7a36ea10..b8dffcf6 100644 --- a/osal/usb_osal_threadx.c +++ b/osal/usb_osal_threadx.c @@ -5,6 +5,8 @@ */ #include "usb_osal.h" #include "usb_errno.h" +#include "usb_config.h" +#include "usb_log.h" #include "tx_api.h" /* create bytepool in tx_application_define @@ -22,12 +24,16 @@ usb_osal_thread_t usb_osal_thread_create(const char *name, uint32_t stack_size, tx_byte_allocate(&usb_byte_pool, (VOID **)&thread_ptr, sizeof(TX_THREAD), TX_NO_WAIT); if (thread_ptr == TX_NULL) { - return NULL; + USB_LOG_ERR("Create thread %s failed\r\n", name); + while (1) { + } } tx_byte_allocate(&usb_byte_pool, (VOID **)&pointer, stack_size, TX_NO_WAIT); if (pointer == TX_NULL) { - return NULL; + USB_LOG_ERR("Create thread %s failed\r\n", name); + while (1) { + } } tx_thread_create(thread_ptr, (CHAR *)name, (VOID(*)(ULONG))entry, (uintptr_t)args, @@ -70,7 +76,9 @@ usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count) tx_byte_allocate(&usb_byte_pool, (VOID **)&sem_ptr, sizeof(TX_SEMAPHORE), TX_NO_WAIT); if (sem_ptr == TX_NULL) { - return NULL; + USB_LOG_ERR("Create semaphore failed\r\n"); + while (1) { + } } tx_semaphore_create(sem_ptr, "usbh_sem", initial_count); @@ -115,7 +123,9 @@ usb_osal_mutex_t usb_osal_mutex_create(void) tx_byte_allocate(&usb_byte_pool, (VOID **)&mutex_ptr, sizeof(TX_MUTEX), TX_NO_WAIT); if (mutex_ptr == TX_NULL) { - return NULL; + USB_LOG_ERR("Create mutex failed\r\n"); + while (1) { + } } tx_mutex_create(mutex_ptr, "usbh_mutx", TX_INHERIT); @@ -157,13 +167,17 @@ usb_osal_mq_t usb_osal_mq_create(uint32_t max_msgs) tx_byte_allocate(&usb_byte_pool, (VOID **)&queue_ptr, sizeof(TX_QUEUE), TX_NO_WAIT); if (queue_ptr == TX_NULL) { - return NULL; + USB_LOG_ERR("Create TX_QUEUE failed\r\n"); + while (1) { + } } tx_byte_allocate(&usb_byte_pool, (VOID **)&pointer, sizeof(uintptr_t) * max_msgs, TX_NO_WAIT); if (pointer == TX_NULL) { - return NULL; + USB_LOG_ERR("Create mq failed\r\n"); + while (1) { + } } tx_queue_create(queue_ptr, "usbh_mq", sizeof(uintptr_t) / 4, pointer, sizeof(uintptr_t) * max_msgs); @@ -206,14 +220,18 @@ struct usb_osal_timer *usb_osal_timer_create(const char *name, uint32_t timeout_ tx_byte_allocate(&usb_byte_pool, (VOID **)&timer, sizeof(struct usb_osal_timer), TX_NO_WAIT); if (timer == TX_NULL) { - return NULL; + USB_LOG_ERR("Create usb_osal_timer failed\r\n"); + while (1) { + } } memset(timer, 0, sizeof(struct usb_osal_timer)); tx_byte_allocate(&usb_byte_pool, (VOID **)&timer_ptr, sizeof(TX_TIMER), TX_NO_WAIT); if (timer_ptr == TX_NULL) { - return NULL; + USB_LOG_ERR("Create TX_TIMER failed\r\n"); + while (1) { + } } timer->timer = timer_ptr;