Com_usb_cdc.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "Com_usb_cdc.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "FreeRTOS.h"
  5. #include "task.h"
  6. #include "stream_buffer.h"
  7. #include "queue.h"
  8. #include "usbd_cdc_if.h"
  9. /* usb_device.c 中定义的 USB 设备句柄 */
  10. extern USBD_HandleTypeDef hUsbDeviceFS;
  11. StreamBufferHandle_t xUsbTxStream = NULL;
  12. StreamBufferHandle_t xUsbRxStream = NULL;
  13. TaskHandle_t xUsbSendTaskHandle = NULL;
  14. /* 首次挂载宽限标志:设备未 CONFIGURED 时重置,CONFIGURED 后等宽限才发首包 */
  15. static uint8_t s_attach_hold_ok = 0;
  16. /**
  17. * @brief USB CDC 发送任务
  18. * 从 TX 流缓冲取出数据,串行调用 CDC_Transmit_FS 发送。
  19. * 单发任务保证 CDC 端点不会并发访问。
  20. *
  21. * 任务A printf → fputc ─┐
  22. * 任务B printf → fputc ─┼→ TX stream buffer(环形) → [usbSendTask 单发任务] → CDC_Transmit_FS()
  23. * 任务C printf → fputc ─┘
  24. */
  25. static void Task_Usb_Send(void *argument)
  26. {
  27. (void)argument;
  28. uint8_t buf[64];
  29. size_t got = 0;
  30. for (;;)
  31. {
  32. if (hUsbDeviceFS.dev_state != USBD_STATE_CONFIGURED)
  33. {
  34. /* 未枚举:重置宽限并低频轮询,不空转 */
  35. s_attach_hold_ok = 0;
  36. vTaskDelay(pdMS_TO_TICKS(10));
  37. }
  38. else
  39. {
  40. if (s_attach_hold_ok == 0)
  41. {
  42. /* CONFIGURED 后先等终端(COM_B)重连再发首包,
  43. 避免第一包掉进主机重枚举窗口被丢弃 */
  44. vTaskDelay(pdMS_TO_TICKS(COM_USB_BOOT_HOLDOFF_MS));
  45. s_attach_hold_ok = 1;
  46. }
  47. got = xStreamBufferReceive(xUsbTxStream, buf, sizeof(buf), portMAX_DELAY);
  48. if (got > 0)
  49. {
  50. uint32_t sent = 0;
  51. while (sent < got)
  52. {
  53. uint16_t chunk = (uint16_t)(got - sent);
  54. uint8_t ret = CDC_Transmit_FS(&buf[sent], chunk);
  55. if (ret == USBD_OK)
  56. {
  57. sent += chunk;
  58. }
  59. else if (ret == USBD_BUSY)
  60. {
  61. /* 端点忙,稍后重试 */
  62. vTaskDelay(pdMS_TO_TICKS(1));
  63. }
  64. else /* USBD_FAIL: 未枚举/已掉出 CONFIGURED, 重试无意义,
  65. 丢弃余下数据并回外层重新检查设备状态 */
  66. {
  67. break;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. /**
  75. * @brief 初始化 USB CDC 通信层:创建流缓冲与发送任务
  76. * 注意!注意!要放在Com_parameter_init初始化后面,不然会出现卡死现象
  77. */
  78. void Com_usb_cdc_init(void)
  79. {
  80. if (xUsbTxStream == NULL)
  81. {
  82. xUsbTxStream = xStreamBufferCreate(COM_USB_TX_BUFFER_SIZE, 1);
  83. }
  84. if (xUsbRxStream == NULL)
  85. {
  86. xUsbRxStream = xStreamBufferCreate(COM_USB_RX_BUFFER_SIZE, 1);
  87. }
  88. if (xUsbSendTaskHandle == NULL)
  89. {
  90. xTaskCreate(Task_Usb_Send, "usbSendTask", COM_USB_SEND_TASK_STACK,
  91. NULL, COM_USB_SEND_TASK_PRIORITY, &xUsbSendTaskHandle);
  92. }
  93. }
  94. /**
  95. * @brief printf 重定向:把字符写入 TX 流缓冲(多任务安全)
  96. */
  97. int fputc(int ch, FILE *f)
  98. {
  99. (void)f;
  100. if (xUsbTxStream != NULL)
  101. {
  102. xStreamBufferSend(xUsbTxStream, (uint8_t *)&ch, 1, 0);
  103. }
  104. return ch;
  105. }
  106. /**
  107. * @brief CDC 接收回调桥接:把收到的数据存入 RX 流缓冲
  108. */
  109. void Com_usb_cdc_rx_handler(uint8_t *buf, uint32_t len)
  110. {
  111. if (xUsbRxStream != NULL)
  112. {
  113. xStreamBufferSendFromISR(xUsbRxStream, buf, len, NULL);
  114. }
  115. }
  116. /**
  117. * @brief 读取应用层接收到的数据(非阻塞)
  118. */
  119. uint32_t Com_usb_cdc_read(uint8_t *buf, uint32_t len)
  120. {
  121. if (xUsbRxStream == NULL || buf == NULL)
  122. {
  123. return 0;
  124. }
  125. return (uint32_t)xStreamBufferReceive(xUsbRxStream, buf, len, 0);
  126. }
  127. /**
  128. * @brief 查询当前可读字节数
  129. */
  130. uint32_t Com_usb_cdc_bytes_available(void)
  131. {
  132. if (xUsbRxStream == NULL)
  133. {
  134. return 0;
  135. }
  136. return (uint32_t)xStreamBufferBytesAvailable(xUsbRxStream);
  137. }