usb_tx.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "usb_tx.h"
  2. #include <Adafruit_TinyUSB.h>
  3. #include <Arduino.h>
  4. #include <string.h>
  5. // One outbound ring per active HID destination. Only one USB mode is live at a time, and a mode presents at
  6. // most CFG_TUD_HID HID interfaces plus the wake mouse, so a handful of channels covers every case. Channels are
  7. // allocated lazily on first send to a given Adafruit_USBD_HID* and never freed (the object set is fixed for the
  8. // session). The producer side runs under PRIMASK (loop AND the RF-decode path enqueue); the consumer is the
  9. // single usbd task (tud_sof_cb), so head/tail need no further locking beyond the enqueue critical section.
  10. #define TX_CHAN_MAX 8
  11. #define TX_RING_N \
  12. 4 // depth per destination; 250 Hz produced, ~1 kHz SOF drain -> stays shallow
  13. #define TX_DATA_MAX \
  14. 64 // CFG_TUD_HID_EP_BUFSIZE -- the largest report any mode sends (63 B + id)
  15. struct TxItem {
  16. uint8_t rid;
  17. uint8_t len;
  18. uint8_t data[TX_DATA_MAX];
  19. };
  20. struct TxChan {
  21. Adafruit_USBD_HID *hid; // nullptr = free slot
  22. TxItem ring[TX_RING_N];
  23. volatile uint8_t head,
  24. tail; // head=next write, tail=next read; empty when equal
  25. };
  26. static TxChan g_chan[TX_CHAN_MAX];
  27. static inline uint8_t txNext(uint8_t i)
  28. {
  29. return (uint8_t)((i + 1) % TX_RING_N);
  30. }
  31. void usbTxHid(Adafruit_USBD_HID *hid, uint8_t rid, const void *data,
  32. uint16_t len)
  33. {
  34. if (!hid)
  35. return;
  36. if (len > TX_DATA_MAX)
  37. len = TX_DATA_MAX;
  38. uint32_t pm = __get_PRIMASK();
  39. __disable_irq();
  40. // find this destination's channel, or claim a free one
  41. TxChan *c = nullptr;
  42. for (int i = 0; i < TX_CHAN_MAX; i++) {
  43. if (g_chan[i].hid == hid) {
  44. c = &g_chan[i];
  45. break;
  46. }
  47. }
  48. if (!c) {
  49. for (int i = 0; i < TX_CHAN_MAX; i++) {
  50. if (!g_chan[i].hid) {
  51. c = &g_chan[i];
  52. c->hid = hid;
  53. c->head = c->tail = 0;
  54. break;
  55. }
  56. }
  57. }
  58. if (c) {
  59. uint8_t h = c->head, nx = txNext(h);
  60. // full -> drop the oldest (advance tail), never the newest
  61. if (nx == c->tail)
  62. c->tail = txNext(c->tail);
  63. c->ring[h].rid = rid;
  64. c->ring[h].len = (uint8_t)len;
  65. if (len)
  66. memcpy(c->ring[h].data, data, len);
  67. c->head = nx;
  68. }
  69. __set_PRIMASK(pm);
  70. }
  71. // usbd task only (tud_sof_cb). Send at most one ready report per destination per SOF: after a send the
  72. // endpoint stays busy until the host polls it (~1 ms), and SOF fires every ~1 ms, so this naturally paces each
  73. // stream to the host's poll rate without ever blocking. A destination whose endpoint is busy is simply left
  74. // for the next frame; it does not hold up the others.
  75. void usbTxDrain(void)
  76. {
  77. for (int i = 0; i < TX_CHAN_MAX; i++) {
  78. TxChan *c = &g_chan[i];
  79. if (!c->hid)
  80. continue;
  81. uint8_t t = c->tail;
  82. if (t == c->head)
  83. continue; // empty
  84. if (!c->hid->ready())
  85. continue; // endpoint busy / not mounted -> retry next SOF
  86. TxItem &it = c->ring[t];
  87. // sendReport copies into the endpoint buffer, so releasing the slot right after is safe.
  88. if (c->hid->sendReport(it.rid, it.data, it.len))
  89. c->tail = txNext(t);
  90. }
  91. }
  92. // Per-frame drain callbacks for non-HID senders (XInput endpoint, WebUSB blob). Registered at setup(),
  93. // invoked from tud_sof_cb on the usbd task. Fixed, tiny capacity; registration is setup-only (single-threaded)
  94. // so no locking is needed and the SOF reader sees a stable list.
  95. #define TX_DRAIN_MAX 4
  96. static usbTxDrainFn g_drainFns[TX_DRAIN_MAX];
  97. static uint8_t g_nDrain;
  98. void usbTxRegisterDrain(usbTxDrainFn fn)
  99. {
  100. if (fn && g_nDrain < TX_DRAIN_MAX)
  101. g_drainFns[g_nDrain++] = fn;
  102. }
  103. // Drain trigger: a DEDICATED FreeRTOS task -- NOT tud_sof_cb. SOF proved unusable here: TinyUSB gates the SOF
  104. // callback on `sof_consumer`, which configuration_reset() wipes on every bus reset (tu_varclr(&_usbd_dev)), so
  105. // after the host's first reset tud_sof_cb silently stops firing and NO device->host data flows (0x45 input,
  106. // the 0x79 connect report, streamed reports, the WebUSB blob -- all dead, while RF keeps working). A plain task
  107. // is ungated and always runs. Crucially it is a SEPARATE task from loop(): if a sendReport ever blocks on a
  108. // full usbd event queue, this task just blocks/yields -- loop() keeps running and feeding the ~8 s watchdog,
  109. // which is the whole reason we moved sends off loop() in the first place.
  110. static void usbTxTask(void *arg)
  111. {
  112. (void)arg;
  113. for (;;) {
  114. usbTxDrain();
  115. for (uint8_t i = 0; i < g_nDrain; i++)
  116. g_drainFns[i]();
  117. vTaskDelay(1); // ~1 ms, matching the USB frame cadence
  118. }
  119. }
  120. void usbTxBegin(void)
  121. {
  122. // 512 words (2 KB): covers the deepest drain path (webusbSendBlob's 115 B frame + the tud_* call chain).
  123. // TASK_PRIO_LOW = same as loop(): cooperative, won't preempt the RF poll timing; vTaskDelay yields each ms.
  124. xTaskCreate(usbTxTask, "usbtx", 512, NULL, TASK_PRIO_LOW, NULL);
  125. }