Com_usb_cdc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. StreamBufferHandle_t xUsbTxStream = NULL;
  10. StreamBufferHandle_t xUsbRxStream = NULL;
  11. TaskHandle_t xUsbSendTaskHandle = NULL;
  12. /**
  13. * @brief USB CDC 发送任务
  14. * 从 TX 流缓冲取出数据,串行调用 CDC_Transmit_FS 发送。
  15. * 单发任务保证 CDC 端点不会并发访问。
  16. *
  17. * 任务A printf → fputc ─┐
  18. * 任务B printf → fputc ─┼→ TX stream buffer(环形) → [usbSendTask 单发任务] → CDC_Transmit_FS()
  19. * 任务C printf → fputc ─┘
  20. */
  21. static void Task_Usb_Send(void *argument)
  22. {
  23. (void)argument;
  24. uint8_t buf[64];
  25. size_t got = 0;
  26. for (;;)
  27. {
  28. got = xStreamBufferReceive(xUsbTxStream, buf, sizeof(buf), portMAX_DELAY);
  29. if (got > 0)
  30. {
  31. uint32_t sent = 0;
  32. while (sent < got)
  33. {
  34. uint16_t chunk = (uint16_t)(got - sent);
  35. if (CDC_Transmit_FS(&buf[sent], chunk) == USBD_OK)
  36. {
  37. sent += chunk;
  38. }
  39. else
  40. {
  41. /* 端点忙,稍后重试 */
  42. vTaskDelay(pdMS_TO_TICKS(1));
  43. }
  44. }
  45. }
  46. }
  47. }
  48. /**
  49. * @brief 初始化 USB CDC 通信层:创建流缓冲与发送任务
  50. */
  51. void Com_usb_cdc_init(void)
  52. {
  53. if (xUsbTxStream == NULL)
  54. {
  55. xUsbTxStream = xStreamBufferCreate(COM_USB_TX_BUFFER_SIZE, 1);
  56. }
  57. if (xUsbRxStream == NULL)
  58. {
  59. xUsbRxStream = xStreamBufferCreate(COM_USB_RX_BUFFER_SIZE, 1);
  60. }
  61. if (xUsbSendTaskHandle == NULL)
  62. {
  63. xTaskCreate(Task_Usb_Send, "usbSendTask", COM_USB_SEND_TASK_STACK,
  64. NULL, COM_USB_SEND_TASK_PRIORITY, &xUsbSendTaskHandle);
  65. }
  66. }
  67. /**
  68. * @brief printf 重定向:把字符写入 TX 流缓冲(多任务安全)
  69. */
  70. int fputc(int ch, FILE *f)
  71. {
  72. (void)f;
  73. if (xUsbTxStream != NULL)
  74. {
  75. xStreamBufferSend(xUsbTxStream, (uint8_t *)&ch, 1, 0);
  76. }
  77. return ch;
  78. }
  79. /**
  80. * @brief CDC 接收回调桥接:把收到的数据存入 RX 流缓冲
  81. */
  82. void Com_usb_cdc_rx_handler(uint8_t *buf, uint32_t len)
  83. {
  84. if (xUsbRxStream != NULL)
  85. {
  86. xStreamBufferSendFromISR(xUsbRxStream, buf, len, NULL);
  87. }
  88. }
  89. /**
  90. * @brief 读取应用层接收到的数据(非阻塞)
  91. */
  92. uint32_t Com_usb_cdc_read(uint8_t *buf, uint32_t len)
  93. {
  94. if (xUsbRxStream == NULL || buf == NULL)
  95. {
  96. return 0;
  97. }
  98. return (uint32_t)xStreamBufferReceive(xUsbRxStream, buf, len, 0);
  99. }
  100. /**
  101. * @brief 查询当前可读字节数
  102. */
  103. uint32_t Com_usb_cdc_bytes_available(void)
  104. {
  105. if (xUsbRxStream == NULL)
  106. {
  107. return 0;
  108. }
  109. return (uint32_t)xStreamBufferBytesAvailable(xUsbRxStream);
  110. }