fix: queue USB log buffer indices within SDK size limits
This commit is contained in:
@@ -13,6 +13,10 @@ extern int printf(const char *format, ...);
|
||||
#define LOG_RECORD_BYTES 512
|
||||
#define LOG_QUEUE_DEPTH 16
|
||||
struct log_record { unsigned int length; char text[LOG_RECORD_BYTES]; };
|
||||
/* Queue only a 32-bit slot index; full records exceed this SDK's queue
|
||||
* message-size limit. Slots remain owned until the worker finishes writing. */
|
||||
static struct log_record log_pool[LOG_QUEUE_DEPTH];
|
||||
static unsigned char slot_used[LOG_QUEUE_DEPTH];
|
||||
static ql_queue_t usb_queue;
|
||||
static ql_task_t usb_task;
|
||||
static volatile int ready;
|
||||
@@ -25,6 +29,24 @@ static void note_drop(void)
|
||||
ql_rtos_task_sched_unlock();
|
||||
}
|
||||
|
||||
static unsigned int reserve_slot(void)
|
||||
{
|
||||
unsigned int slot;
|
||||
ql_rtos_task_sched_lock();
|
||||
for (slot = 0; slot < LOG_QUEUE_DEPTH; ++slot) {
|
||||
if (!slot_used[slot]) { slot_used[slot] = 1; break; }
|
||||
}
|
||||
ql_rtos_task_sched_unlock();
|
||||
return slot;
|
||||
}
|
||||
|
||||
static void release_slot(unsigned int slot)
|
||||
{
|
||||
ql_rtos_task_sched_lock();
|
||||
slot_used[slot] = 0;
|
||||
ql_rtos_task_sched_unlock();
|
||||
}
|
||||
|
||||
int field_log_printf(const char *format, ...)
|
||||
{
|
||||
struct log_record record;
|
||||
@@ -43,9 +65,18 @@ int field_log_printf(const char *format, ...)
|
||||
}
|
||||
record.length = (unsigned int)strlen(record.text);
|
||||
ret = printf("%s", record.text);
|
||||
if (ready && record.length &&
|
||||
ql_rtos_queue_release(usb_queue, sizeof(record), (u8 *)&record, QL_NO_WAIT) != 0)
|
||||
note_drop();
|
||||
if (ready && record.length) {
|
||||
unsigned int slot = reserve_slot();
|
||||
if (slot == LOG_QUEUE_DEPTH) {
|
||||
note_drop();
|
||||
} else {
|
||||
log_pool[slot] = record;
|
||||
if (ql_rtos_queue_release(usb_queue, sizeof(slot), (u8 *)&slot, QL_NO_WAIT) != 0) {
|
||||
release_slot(slot);
|
||||
note_drop();
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -64,7 +95,7 @@ static int usb_write_all(const char *text, int length)
|
||||
|
||||
static void usb_worker(void *arg)
|
||||
{
|
||||
struct log_record record;
|
||||
unsigned int slot;
|
||||
int opened = 0;
|
||||
unsigned long last_notice = 0, reported = 0;
|
||||
(void)arg;
|
||||
@@ -78,8 +109,11 @@ static void usb_worker(void *arg)
|
||||
}
|
||||
if (!opened) { ql_rtos_task_sleep_ms(1000); continue; }
|
||||
}
|
||||
if (ql_rtos_queue_wait(usb_queue, (u8 *)&record, sizeof(record), 1000) == 0) {
|
||||
if (usb_write_all(record.text, record.length) != 0) {
|
||||
if (ql_rtos_queue_wait(usb_queue, (u8 *)&slot, sizeof(slot), 1000) == 0 &&
|
||||
slot < LOG_QUEUE_DEPTH) {
|
||||
int result = usb_write_all(log_pool[slot].text, log_pool[slot].length);
|
||||
release_slot(slot);
|
||||
if (result != 0) {
|
||||
note_drop();
|
||||
ql_uart_close(QL_USB_CDC_PORT);
|
||||
opened = 0;
|
||||
@@ -106,7 +140,7 @@ int field_log_init(void)
|
||||
{
|
||||
int ret;
|
||||
if (ready) return 0;
|
||||
ret = ql_rtos_queue_create(&usb_queue, sizeof(struct log_record), LOG_QUEUE_DEPTH);
|
||||
ret = ql_rtos_queue_create(&usb_queue, sizeof(unsigned int), LOG_QUEUE_DEPTH);
|
||||
if (ret != 0) { printf("[USB日志] 队列创建失败=%d;仅保留UART日志\r\n", ret); return ret; }
|
||||
ret = ql_rtos_task_create(&usb_task, 4096, 130, "usb_log", usb_worker, NULL);
|
||||
if (ret != 0) {
|
||||
|
||||
Reference in New Issue
Block a user