| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- #include "Com_usb_cdc.h"
- #include <stdio.h>
- #include <string.h>
- #include <stdarg.h>
- #include "FreeRTOS.h"
- #include "task.h"
- #include "stream_buffer.h"
- #include "queue.h"
- #include "usbd_cdc_if.h"
- #include "usart.h" /* huart1: 日志环未建立时的直发兜底 */
- #include "Com_pc_usart.h" /* Com_pc_usart_log_write: USART1 日志环 */
- /* usb_device.c 中定义的 USB 设备句柄 */
- extern USBD_HandleTypeDef hUsbDeviceFS;
- StreamBufferHandle_t xUsbTxStream = NULL;
- StreamBufferHandle_t xUsbRxStream = NULL;
- TaskHandle_t xUsbSendTaskHandle = NULL;
- /* 首次挂载宽限标志:设备未 CONFIGURED 时重置,CONFIGURED 后等宽限才发首包 */
- static uint8_t s_attach_hold_ok = 0;
- /**
- * @brief USB CDC 发送任务
- * 从 TX 流缓冲取出数据,串行调用 CDC_Transmit_FS 发送。
- * 单发任务保证 CDC 端点不会并发访问。
- *
- * 任务A printf → fputc ─┐
- * 任务B printf → fputc ─┼→ TX stream buffer(环形) → [usbSendTask 单发任务] → CDC_Transmit_FS()
- * 任务C printf → fputc ─┘
- */
- static void Task_Usb_Send(void *argument)
- {
- (void)argument;
- uint8_t buf[64];
- size_t got = 0;
- for (;;)
- {
- if (hUsbDeviceFS.dev_state != USBD_STATE_CONFIGURED)
- {
- /* 未枚举:重置宽限并低频轮询,不空转 */
- s_attach_hold_ok = 0;
- vTaskDelay(pdMS_TO_TICKS(10));
- }
- else
- {
- if (s_attach_hold_ok == 0)
- {
- /* CONFIGURED 后先等终端(COM_B)重连再发首包,
- 避免第一包掉进主机重枚举窗口被丢弃 */
- vTaskDelay(pdMS_TO_TICKS(COM_USB_BOOT_HOLDOFF_MS));
- s_attach_hold_ok = 1;
- }
- got = xStreamBufferReceive(xUsbTxStream, buf, sizeof(buf), portMAX_DELAY);
- if (got > 0)
- {
- uint32_t sent = 0;
- while (sent < got)
- {
- uint16_t chunk = (uint16_t)(got - sent);
- uint8_t ret = CDC_Transmit_FS(&buf[sent], chunk);
- if (ret == USBD_OK)
- {
- sent += chunk;
- }
- else if (ret == USBD_BUSY)
- {
- /* 端点忙,稍后重试 */
- vTaskDelay(pdMS_TO_TICKS(1));
- }
- else /* USBD_FAIL: 未枚举/已掉出 CONFIGURED, 重试无意义,
- 丢弃余下数据并回外层重新检查设备状态 */
- {
- break;
- }
- }
- }
- }
- }
- }
- /**
- * @brief 初始化 USB CDC 通信层:创建流缓冲与发送任务
- * 注意!注意!要放在Com_parameter_init初始化后面,不然会出现卡死现象
- * 调度器启动前创建 RTOS 对象(xTaskCreate/xStreamBufferCreate、printf 经
- * fputc 写流缓冲)都会进临界区: uxCriticalNesting 初值是哨兵 0xaaaaaaaa,
- * vPortExitCritical 要等计数减到 0 才恢复中断, 调度器启动前到不了 0,
- * BASEPRI 会被一直压在 0x50, 屏蔽掉 TIM2(HAL tick) 等优先级>=5 的中断,
- * 此后任何 HAL_Delay 都会死等。调度器启动时 SVC 会恢复 BASEPRI,
- * 这里提前恢复是安全的
- */
- void Com_usb_cdc_init(void)
- {
- if (xUsbTxStream == NULL)
- {
- xUsbTxStream = xStreamBufferCreate(COM_USB_TX_BUFFER_SIZE, 1);
- }
- if (xUsbRxStream == NULL)
- {
- xUsbRxStream = xStreamBufferCreate(COM_USB_RX_BUFFER_SIZE, 1);
- }
- if (xUsbSendTaskHandle == NULL)
- {
- xTaskCreate(Task_Usb_Send, "usbSendTask", COM_USB_SEND_TASK_STACK,
- NULL, COM_USB_SEND_TASK_PRIORITY, &xUsbSendTaskHandle);
- }
- }
- /* 日志分发: 将一条日志送入所有已启用的输出通道。
- * 本函数不套用 COM_LOG_ENABLE: fputc/printf 不受日志总开关约束, 便于随时
- * 插入 printf 输出临时调试信息; 两个通道宏均为 0 时, 数据被丢弃。
- * USART1 与 PC 协议共用同一外设, 但两者在同一发送任务内排队, 不会字节级穿插。 */
- static void log_output(const uint8_t *d, size_t n)
- {
- if ((d == NULL) || (n == 0))
- {
- return;
- }
- #if COM_LOG_USART1_ENABLE
- if (Com_pc_usart_log_write((const uint8_t *)d, (uint32_t)n) == 0)
- {
- /* 日志环尚未建立(Com_pc_usart_init() 之前): 退化为阻塞直发,
- * 保证启动阶段的输出不丢失。此时协议通道亦未工作, 无穿插风险。 */
- if (huart1.Instance == USART1)
- {
- (void)HAL_UART_Transmit(&huart1, (uint8_t *)d, (uint16_t)n,
- COM_LOG_USART1_TIMEOUT_MS);
- }
- }
- #endif
- #if COM_LOG_USB_ENABLE
- if (xUsbTxStream != NULL) /* 调度器启动前该流缓冲不存在 */
- {
- (void)xStreamBufferSend(xUsbTxStream, d, n, 0);
- }
- #endif
- }
- /**
- * @brief printf 重定向: 经与日志相同的通道分发输出
- * @note 输出通道由 COM_LOG_USART1_ENABLE / COM_LOG_USB_ENABLE 决定,
- * 不受 COM_LOG_ENABLE 约束; 逐字符入队, 仅保证字符级原子,
- * 整行打印请使用 Com_log_printf
- */
- int fputc(int ch, FILE *f)
- {
- uint8_t c = (uint8_t)ch;
- (void)f;
- log_output(&c, 1);
- return ch;
- }
- /**
- * @brief 行原子日志输出: 先格式化到行缓冲, 再整行送入已启用的输出通道
- * @note 整行单次入环, 行内不会被其他任务打印穿插;
- * 超时为0, 环满时丢弃不阻塞调用任务; 单行超长截断。
- * @param fmt : 格式串 (同 printf)
- * @return vsnprintf 返回的字符数 (截断时为原长度), 出错返回 0
- */
- int Com_log_printf(const char *fmt, ...)
- {
- char buf[COM_LOG_LINE_BUF_SIZE];
- va_list ap;
- int n;
- size_t len;
- if (fmt == NULL)
- {
- return 0;
- }
- va_start(ap, fmt);
- n = vsnprintf(buf, sizeof(buf), fmt, ap);
- va_end(ap);
- if (n < 0)
- {
- /* 非C99语义的库截断时返回负值: 缓冲区已有截断内容,
- * 强制封尾后按实际长度发送 */
- buf[sizeof(buf) - 1] = '\0';
- len = strlen(buf);
- }
- else
- {
- if (n == 0)
- {
- return 0;
- }
- len = (size_t)n;
- if (len >= sizeof(buf))
- {
- len = sizeof(buf) - 1;
- }
- }
- if (len == 0)
- {
- return 0;
- }
- log_output((const uint8_t *)buf, len);
- return n;
- }
- /**
- * @brief 自检打印: 不受任何日志宏约束, 恒输出
- * @note 总开关/两通道/遥测四个宏都对它无效 —— 开机自检这类结果必须始终可见,
- * 发货固件把日志全关掉它照样出字。
- * 仍走日志环排队(与协议帧不会字节级穿插); 环未建立时退化为阻塞直发。
- * @param fmt : 格式串 (同 printf)
- * @return vsnprintf 返回的字符数 (截断时为原长度), 出错返回 0
- */
- int Com_selftest_printf(const char *fmt, ...)
- {
- char buf[COM_LOG_LINE_BUF_SIZE];
- va_list ap;
- int n;
- size_t len;
- if (fmt == NULL)
- {
- return 0;
- }
- va_start(ap, fmt);
- n = vsnprintf(buf, sizeof(buf), fmt, ap);
- va_end(ap);
- if (n < 0)
- {
- /* 非C99语义的库截断时返回负值: 缓冲区已有截断内容, 强制封尾 */
- buf[sizeof(buf) - 1] = '\0';
- len = strlen(buf);
- }
- else
- {
- if (n == 0)
- {
- return 0;
- }
- len = (size_t)n;
- if (len >= sizeof(buf))
- {
- len = sizeof(buf) - 1;
- }
- }
- if (len == 0)
- {
- return 0;
- }
- /* 不走 log_output(): 那条路受 COM_LOG_USART1_ENABLE / COM_LOG_USB_ENABLE 管 */
- if (Com_pc_usart_log_write((const uint8_t *)buf, (uint32_t)len) == 0)
- {
- if (huart1.Instance == USART1)
- {
- (void)HAL_UART_Transmit(&huart1, (uint8_t *)buf, (uint16_t)len,
- COM_LOG_USART1_TIMEOUT_MS);
- }
- }
- return n;
- }
- /**
- * @brief CDC 接收回调桥接:把收到的数据存入 RX 流缓冲
- */
- void Com_usb_cdc_rx_handler(uint8_t *buf, uint32_t len)
- {
- if (xUsbRxStream != NULL)
- {
- xStreamBufferSendFromISR(xUsbRxStream, buf, len, NULL);
- }
- }
- /**
- * @brief 读取应用层接收到的数据(非阻塞)
- */
- uint32_t Com_usb_cdc_read(uint8_t *buf, uint32_t len)
- {
- if (xUsbRxStream == NULL || buf == NULL)
- {
- return 0;
- }
- return (uint32_t)xStreamBufferReceive(xUsbRxStream, buf, len, 0);
- }
- /**
- * @brief 查询当前可读字节数
- */
- uint32_t Com_usb_cdc_bytes_available(void)
- {
- if (xUsbRxStream == NULL)
- {
- return 0;
- }
- return (uint32_t)xStreamBufferBytesAvailable(xUsbRxStream);
- }
|