Com_usb_cdc.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include "Com_usb_cdc.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include "FreeRTOS.h"
  6. #include "task.h"
  7. #include "stream_buffer.h"
  8. #include "queue.h"
  9. #include "usbd_cdc_if.h"
  10. #include "usart.h" /* huart1: 日志环未建立时的直发兜底 */
  11. #include "Com_pc_usart.h" /* Com_pc_usart_log_write: USART1 日志环 */
  12. /* usb_device.c 中定义的 USB 设备句柄 */
  13. extern USBD_HandleTypeDef hUsbDeviceFS;
  14. StreamBufferHandle_t xUsbTxStream = NULL;
  15. StreamBufferHandle_t xUsbRxStream = NULL;
  16. TaskHandle_t xUsbSendTaskHandle = NULL;
  17. /* 首次挂载宽限标志:设备未 CONFIGURED 时重置,CONFIGURED 后等宽限才发首包 */
  18. static uint8_t s_attach_hold_ok = 0;
  19. /**
  20. * @brief USB CDC 发送任务
  21. * 从 TX 流缓冲取出数据,串行调用 CDC_Transmit_FS 发送。
  22. * 单发任务保证 CDC 端点不会并发访问。
  23. *
  24. * 任务A printf → fputc ─┐
  25. * 任务B printf → fputc ─┼→ TX stream buffer(环形) → [usbSendTask 单发任务] → CDC_Transmit_FS()
  26. * 任务C printf → fputc ─┘
  27. */
  28. static void Task_Usb_Send(void *argument)
  29. {
  30. (void)argument;
  31. uint8_t buf[64];
  32. size_t got = 0;
  33. for (;;)
  34. {
  35. if (hUsbDeviceFS.dev_state != USBD_STATE_CONFIGURED)
  36. {
  37. /* 未枚举:重置宽限并低频轮询,不空转 */
  38. s_attach_hold_ok = 0;
  39. vTaskDelay(pdMS_TO_TICKS(10));
  40. }
  41. else
  42. {
  43. if (s_attach_hold_ok == 0)
  44. {
  45. /* CONFIGURED 后先等终端(COM_B)重连再发首包,
  46. 避免第一包掉进主机重枚举窗口被丢弃 */
  47. vTaskDelay(pdMS_TO_TICKS(COM_USB_BOOT_HOLDOFF_MS));
  48. s_attach_hold_ok = 1;
  49. }
  50. got = xStreamBufferReceive(xUsbTxStream, buf, sizeof(buf), portMAX_DELAY);
  51. if (got > 0)
  52. {
  53. uint32_t sent = 0;
  54. while (sent < got)
  55. {
  56. uint16_t chunk = (uint16_t)(got - sent);
  57. uint8_t ret = CDC_Transmit_FS(&buf[sent], chunk);
  58. if (ret == USBD_OK)
  59. {
  60. sent += chunk;
  61. }
  62. else if (ret == USBD_BUSY)
  63. {
  64. /* 端点忙,稍后重试 */
  65. vTaskDelay(pdMS_TO_TICKS(1));
  66. }
  67. else /* USBD_FAIL: 未枚举/已掉出 CONFIGURED, 重试无意义,
  68. 丢弃余下数据并回外层重新检查设备状态 */
  69. {
  70. break;
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. /**
  78. * @brief 初始化 USB CDC 通信层:创建流缓冲与发送任务
  79. * 注意!注意!要放在Com_parameter_init初始化后面,不然会出现卡死现象
  80. * 调度器启动前创建 RTOS 对象(xTaskCreate/xStreamBufferCreate、printf 经
  81. * fputc 写流缓冲)都会进临界区: uxCriticalNesting 初值是哨兵 0xaaaaaaaa,
  82. * vPortExitCritical 要等计数减到 0 才恢复中断, 调度器启动前到不了 0,
  83. * BASEPRI 会被一直压在 0x50, 屏蔽掉 TIM2(HAL tick) 等优先级>=5 的中断,
  84. * 此后任何 HAL_Delay 都会死等。调度器启动时 SVC 会恢复 BASEPRI,
  85. * 这里提前恢复是安全的
  86. */
  87. void Com_usb_cdc_init(void)
  88. {
  89. if (xUsbTxStream == NULL)
  90. {
  91. xUsbTxStream = xStreamBufferCreate(COM_USB_TX_BUFFER_SIZE, 1);
  92. }
  93. if (xUsbRxStream == NULL)
  94. {
  95. xUsbRxStream = xStreamBufferCreate(COM_USB_RX_BUFFER_SIZE, 1);
  96. }
  97. if (xUsbSendTaskHandle == NULL)
  98. {
  99. xTaskCreate(Task_Usb_Send, "usbSendTask", COM_USB_SEND_TASK_STACK,
  100. NULL, COM_USB_SEND_TASK_PRIORITY, &xUsbSendTaskHandle);
  101. }
  102. }
  103. /* 日志分发: 将一条日志送入所有已启用的输出通道。
  104. * 本函数不套用 COM_LOG_ENABLE: fputc/printf 不受日志总开关约束, 便于随时
  105. * 插入 printf 输出临时调试信息; 两个通道宏均为 0 时, 数据被丢弃。
  106. * USART1 与 PC 协议共用同一外设, 但两者在同一发送任务内排队, 不会字节级穿插。 */
  107. static void log_output(const uint8_t *d, size_t n)
  108. {
  109. if ((d == NULL) || (n == 0))
  110. {
  111. return;
  112. }
  113. #if COM_LOG_USART1_ENABLE
  114. if (Com_pc_usart_log_write((const uint8_t *)d, (uint32_t)n) == 0)
  115. {
  116. /* 日志环尚未建立(Com_pc_usart_init() 之前): 退化为阻塞直发,
  117. * 保证启动阶段的输出不丢失。此时协议通道亦未工作, 无穿插风险。 */
  118. if (huart1.Instance == USART1)
  119. {
  120. (void)HAL_UART_Transmit(&huart1, (uint8_t *)d, (uint16_t)n,
  121. COM_LOG_USART1_TIMEOUT_MS);
  122. }
  123. }
  124. #endif
  125. #if COM_LOG_USB_ENABLE
  126. if (xUsbTxStream != NULL) /* 调度器启动前该流缓冲不存在 */
  127. {
  128. (void)xStreamBufferSend(xUsbTxStream, d, n, 0);
  129. }
  130. #endif
  131. }
  132. /**
  133. * @brief printf 重定向: 经与日志相同的通道分发输出
  134. * @note 输出通道由 COM_LOG_USART1_ENABLE / COM_LOG_USB_ENABLE 决定,
  135. * 不受 COM_LOG_ENABLE 约束; 逐字符入队, 仅保证字符级原子,
  136. * 整行打印请使用 Com_log_printf
  137. */
  138. int fputc(int ch, FILE *f)
  139. {
  140. uint8_t c = (uint8_t)ch;
  141. (void)f;
  142. log_output(&c, 1);
  143. return ch;
  144. }
  145. /**
  146. * @brief 行原子日志输出: 先格式化到行缓冲, 再整行送入已启用的输出通道
  147. * @note 整行单次入环, 行内不会被其他任务打印穿插;
  148. * 超时为0, 环满时丢弃不阻塞调用任务; 单行超长截断。
  149. * @param fmt : 格式串 (同 printf)
  150. * @return vsnprintf 返回的字符数 (截断时为原长度), 出错返回 0
  151. */
  152. int Com_log_printf(const char *fmt, ...)
  153. {
  154. char buf[COM_LOG_LINE_BUF_SIZE];
  155. va_list ap;
  156. int n;
  157. size_t len;
  158. if (fmt == NULL)
  159. {
  160. return 0;
  161. }
  162. va_start(ap, fmt);
  163. n = vsnprintf(buf, sizeof(buf), fmt, ap);
  164. va_end(ap);
  165. if (n < 0)
  166. {
  167. /* 非C99语义的库截断时返回负值: 缓冲区已有截断内容,
  168. * 强制封尾后按实际长度发送 */
  169. buf[sizeof(buf) - 1] = '\0';
  170. len = strlen(buf);
  171. }
  172. else
  173. {
  174. if (n == 0)
  175. {
  176. return 0;
  177. }
  178. len = (size_t)n;
  179. if (len >= sizeof(buf))
  180. {
  181. len = sizeof(buf) - 1;
  182. }
  183. }
  184. if (len == 0)
  185. {
  186. return 0;
  187. }
  188. log_output((const uint8_t *)buf, len);
  189. return n;
  190. }
  191. /**
  192. * @brief 自检打印: 不受任何日志宏约束, 恒输出
  193. * @note 总开关/两通道/遥测四个宏都对它无效 —— 开机自检这类结果必须始终可见,
  194. * 发货固件把日志全关掉它照样出字。
  195. * 仍走日志环排队(与协议帧不会字节级穿插); 环未建立时退化为阻塞直发。
  196. * @param fmt : 格式串 (同 printf)
  197. * @return vsnprintf 返回的字符数 (截断时为原长度), 出错返回 0
  198. */
  199. int Com_selftest_printf(const char *fmt, ...)
  200. {
  201. char buf[COM_LOG_LINE_BUF_SIZE];
  202. va_list ap;
  203. int n;
  204. size_t len;
  205. if (fmt == NULL)
  206. {
  207. return 0;
  208. }
  209. va_start(ap, fmt);
  210. n = vsnprintf(buf, sizeof(buf), fmt, ap);
  211. va_end(ap);
  212. if (n < 0)
  213. {
  214. /* 非C99语义的库截断时返回负值: 缓冲区已有截断内容, 强制封尾 */
  215. buf[sizeof(buf) - 1] = '\0';
  216. len = strlen(buf);
  217. }
  218. else
  219. {
  220. if (n == 0)
  221. {
  222. return 0;
  223. }
  224. len = (size_t)n;
  225. if (len >= sizeof(buf))
  226. {
  227. len = sizeof(buf) - 1;
  228. }
  229. }
  230. if (len == 0)
  231. {
  232. return 0;
  233. }
  234. /* 不走 log_output(): 那条路受 COM_LOG_USART1_ENABLE / COM_LOG_USB_ENABLE 管 */
  235. if (Com_pc_usart_log_write((const uint8_t *)buf, (uint32_t)len) == 0)
  236. {
  237. if (huart1.Instance == USART1)
  238. {
  239. (void)HAL_UART_Transmit(&huart1, (uint8_t *)buf, (uint16_t)len,
  240. COM_LOG_USART1_TIMEOUT_MS);
  241. }
  242. }
  243. return n;
  244. }
  245. /**
  246. * @brief CDC 接收回调桥接:把收到的数据存入 RX 流缓冲
  247. */
  248. void Com_usb_cdc_rx_handler(uint8_t *buf, uint32_t len)
  249. {
  250. if (xUsbRxStream != NULL)
  251. {
  252. xStreamBufferSendFromISR(xUsbRxStream, buf, len, NULL);
  253. }
  254. }
  255. /**
  256. * @brief 读取应用层接收到的数据(非阻塞)
  257. */
  258. uint32_t Com_usb_cdc_read(uint8_t *buf, uint32_t len)
  259. {
  260. if (xUsbRxStream == NULL || buf == NULL)
  261. {
  262. return 0;
  263. }
  264. return (uint32_t)xStreamBufferReceive(xUsbRxStream, buf, len, 0);
  265. }
  266. /**
  267. * @brief 查询当前可读字节数
  268. */
  269. uint32_t Com_usb_cdc_bytes_available(void)
  270. {
  271. if (xUsbRxStream == NULL)
  272. {
  273. return 0;
  274. }
  275. return (uint32_t)xStreamBufferBytesAvailable(xUsbRxStream);
  276. }