usb_tx.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 enqueue); the consumer (usbTxDrain) runs from the
  9. // usbtx task AND tud_hid_report_complete_cb, serialized by a guard flag inside usbTxDrain.
  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. void usbTxFlushHid(Adafruit_USBD_HID *hid)
  72. {
  73. if (!hid)
  74. return;
  75. uint32_t pm = __get_PRIMASK();
  76. __disable_irq();
  77. for (int i = 0; i < TX_CHAN_MAX; i++)
  78. if (g_chan[i].hid == hid) {
  79. g_chan[i].head = g_chan[i].tail = 0;
  80. break;
  81. }
  82. __set_PRIMASK(pm);
  83. }
  84. // Send at most one ready report per destination per call. After a send the endpoint stays busy until the host
  85. // polls it, so the next report for that destination goes out from tud_hid_report_complete_cb (the instant the
  86. // host reads it) -- that chaining is what paces each stream to the host's poll rate. The usbtx task calls this
  87. // too, to kick a stream that's idle (no completion pending) and to keep non-busy destinations moving. A busy
  88. // destination is simply skipped; it does not hold up the others.
  89. void usbTxDrain(void)
  90. {
  91. // Re-entrancy guard (defensive): usbTxPump only calls this from loop(), but sendReport below can yield,
  92. // so guard against a nested loop()-context call ever racing a channel's tail. The loser just skips.
  93. static volatile unsigned char draining;
  94. if (__atomic_test_and_set(&draining, __ATOMIC_ACQUIRE))
  95. return;
  96. for (int i = 0; i < TX_CHAN_MAX; i++) {
  97. TxChan *c = &g_chan[i];
  98. if (!c->hid)
  99. continue;
  100. uint8_t t = c->tail;
  101. if (t == c->head)
  102. continue; // empty
  103. if (!c->hid->ready())
  104. continue; // endpoint busy / not mounted -> retry on completion/next tick
  105. TxItem &it = c->ring[t];
  106. // sendReport copies into the endpoint buffer, so releasing the slot right after is safe.
  107. if (c->hid->sendReport(it.rid, it.data, it.len))
  108. c->tail = txNext(t);
  109. }
  110. __atomic_clear(&draining, __ATOMIC_RELEASE);
  111. }
  112. // Per-slot drain callbacks for non-HID senders (XInput endpoint, WebUSB blob). Registered at setup(), invoked
  113. // from usbTxPump() (loop context). Fixed, tiny capacity; registration is setup-only (single-threaded).
  114. #define TX_DRAIN_MAX 4
  115. static usbTxDrainFn g_drainFns[TX_DRAIN_MAX];
  116. static uint8_t g_nDrain;
  117. void usbTxRegisterDrain(usbTxDrainFn fn)
  118. {
  119. if (fn && g_nDrain < TX_DRAIN_MAX)
  120. g_drainFns[g_nDrain++] = fn;
  121. }
  122. // ---- priority-inversion guard (BLACK BOX capture 3, 2026-07-03) --------------------------------------------
  123. // TinyUSB's nrf5x dcd claims the chip's ONE EasyDMA slot (dma_running test_and_set) and only a few
  124. // instructions LATER writes TASKS_STARTEPIN -- and loop-context sends reach that window at TASK_PRIO_LOW.
  125. // Preempted right there during a Steam OUT flood (0x82 haptic storm), the flag is held by a task that never
  126. // runs again: the TASK_PRIO_HIGH usbd task livelocks re-deferring xact_out_dma around the never-started DMA
  127. // (caught live: loopPC=start_dma:139, irqPC=xact_out_dma re-defer) -> USB dead -> watchdog. The fix needs no
  128. // library patch: run loop's USB-TX windows at the usbd task's own priority. Equal priority = no preemption on
  129. // interrupt exit = the claim+start pair completes atomically w.r.t. the usbd task. Depth-counted so nested
  130. // boosted sections (pump -> drain callbacks) stay balanced; only ever used from the loop task.
  131. static UBaseType_t g_boostSaved;
  132. static uint8_t g_boostDepth;
  133. void usbTxBoost(void)
  134. {
  135. if (!g_boostDepth++) {
  136. g_boostSaved = uxTaskPriorityGet(NULL);
  137. vTaskPrioritySet(NULL, TASK_PRIO_HIGH);
  138. }
  139. }
  140. void usbTxUnboost(void)
  141. {
  142. if (g_boostDepth && !--g_boostDepth)
  143. vTaskPrioritySet(NULL, g_boostSaved);
  144. }
  145. // Drain everything from loop() -- NOT from a SOF callback, a dedicated task, or report_complete. All three of
  146. // those were tried and all fight the RF poll, which is a microsecond-precise busy-wait running in loop(): SOF
  147. // got silently disabled by bus resets; the high-priority usbd task (report_complete) preempted the RF RX
  148. // windows -> missed replies + usbd self-blocking on a full event queue -> controllers dropped; a separate
  149. // low-priority task got starved under USB load -> latency. Draining in loop() is what stability-fix did and is
  150. // the only context that does NOT jitter the RF timing: the send and the poll run sequentially in one task, so
  151. // no send ever interrupts an RX window. The cost -- loop() can block if sendReport hits a full usbd event
  152. // queue -- is handled by a deep CFG_TUD_TASK_QUEUE_SZ (so it never fills) rather than by moving sends off loop.
  153. // Call once per loop() iteration.
  154. void usbTxPump(void)
  155. {
  156. // Whole pump (HID sends + vendor/WebUSB drains) runs boosted -- every dcd claim window in here is
  157. // then atomic against the usbd task (see usbTxBoost above).
  158. usbTxBoost();
  159. usbTxDrain();
  160. for (uint8_t i = 0; i < g_nDrain; i++)
  161. g_drainFns[i]();
  162. usbTxUnboost();
  163. }
  164. void usbTxBegin(void)
  165. {
  166. // Nothing to arm: usbTxPump() is driven directly from loop(). Kept for call-site stability.
  167. }