update(osal): add error log and assert when alloc fail, and stop working

This commit is contained in:
sakumisu 2024-08-31 18:22:33 +08:00
parent a8a5d95f8f
commit 8a7979378d
3 changed files with 82 additions and 15 deletions

View File

@ -5,6 +5,8 @@
*/ */
#include "usb_osal.h" #include "usb_osal.h"
#include "usb_errno.h" #include "usb_errno.h"
#include "usb_config.h"
#include "usb_log.h"
#include <FreeRTOS.h> #include <FreeRTOS.h>
#include "semphr.h" #include "semphr.h"
#include "timers.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; TaskHandle_t htask = NULL;
stack_size /= sizeof(StackType_t); stack_size /= sizeof(StackType_t);
xTaskCreate(entry, name, stack_size, args, configMAX_PRIORITIES - 1 - prio, &htask); 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; 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) 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) 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) 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) 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)); timer = pvPortMalloc(sizeof(struct usb_osal_timer));
if (timer == NULL) { 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)); 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); timer->timer = (void *)xTimerCreate("usb_tim", pdMS_TO_TICKS(timeout_ms), is_period, timer, (TimerCallbackFunction_t)__usb_timeout);
if (timer->timer == NULL) { if (timer->timer == NULL) {
return NULL; USB_LOG_ERR("Create timer failed\r\n");
while (1) {
}
} }
return timer; return timer;
} }

View File

@ -5,6 +5,8 @@
*/ */
#include "usb_osal.h" #include "usb_osal.h"
#include "usb_errno.h" #include "usb_errno.h"
#include "usb_config.h"
#include "usb_log.h"
#include <rtthread.h> #include <rtthread.h>
#include <rthw.h> #include <rthw.h>
@ -12,6 +14,11 @@ usb_osal_thread_t usb_osal_thread_create(const char *name, uint32_t stack_size,
{ {
rt_thread_t htask; rt_thread_t htask;
htask = rt_thread_create(name, entry, args, stack_size, prio, 10); 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); rt_thread_startup(htask);
return (usb_osal_thread_t)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) 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) 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) 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) 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; struct usb_osal_timer *timer;
timer = rt_malloc(sizeof(struct usb_osal_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)); 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)); 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) { if (timer->timer == NULL) {
return NULL; USB_LOG_ERR("Create timer failed\r\n");
while (1) {
}
} }
return timer; return timer;
} }

View File

@ -5,6 +5,8 @@
*/ */
#include "usb_osal.h" #include "usb_osal.h"
#include "usb_errno.h" #include "usb_errno.h"
#include "usb_config.h"
#include "usb_log.h"
#include "tx_api.h" #include "tx_api.h"
/* create bytepool in tx_application_define /* 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); tx_byte_allocate(&usb_byte_pool, (VOID **)&thread_ptr, sizeof(TX_THREAD), TX_NO_WAIT);
if (thread_ptr == TX_NULL) { 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); tx_byte_allocate(&usb_byte_pool, (VOID **)&pointer, stack_size, TX_NO_WAIT);
if (pointer == TX_NULL) { 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, 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); tx_byte_allocate(&usb_byte_pool, (VOID **)&sem_ptr, sizeof(TX_SEMAPHORE), TX_NO_WAIT);
if (sem_ptr == TX_NULL) { 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); 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); tx_byte_allocate(&usb_byte_pool, (VOID **)&mutex_ptr, sizeof(TX_MUTEX), TX_NO_WAIT);
if (mutex_ptr == TX_NULL) { 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); 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); tx_byte_allocate(&usb_byte_pool, (VOID **)&queue_ptr, sizeof(TX_QUEUE), TX_NO_WAIT);
if (queue_ptr == TX_NULL) { 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); tx_byte_allocate(&usb_byte_pool, (VOID **)&pointer, sizeof(uintptr_t) * max_msgs, TX_NO_WAIT);
if (pointer == TX_NULL) { 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); 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); tx_byte_allocate(&usb_byte_pool, (VOID **)&timer, sizeof(struct usb_osal_timer), TX_NO_WAIT);
if (timer == TX_NULL) { 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)); memset(timer, 0, sizeof(struct usb_osal_timer));
tx_byte_allocate(&usb_byte_pool, (VOID **)&timer_ptr, sizeof(TX_TIMER), TX_NO_WAIT); tx_byte_allocate(&usb_byte_pool, (VOID **)&timer_ptr, sizeof(TX_TIMER), TX_NO_WAIT);
if (timer_ptr == TX_NULL) { if (timer_ptr == TX_NULL) {
return NULL; USB_LOG_ERR("Create TX_TIMER failed\r\n");
while (1) {
}
} }
timer->timer = timer_ptr; timer->timer = timer_ptr;