rf_link.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // rf_link.h -- the operational puck<->controller RF protocol (the "dongle" role).
  2. //
  3. // Built on the radio HW layer (radio.cpp), this is what makes the device act as a puck once a controller is
  4. // bonded: it transmits the host frame the bonded controller reconnects on, runs the connected-mode poll loop
  5. // (E7 awake announce -> E3 + GET-report-0x45 each cycle), and decodes the controller's 0xF1 input reply. The
  6. // decode fills the shared g_in (triton.h) and dispatches to the active IController; it also handles the
  7. // mode-switch chord, USB remote-wakeup on a Steam short-press, adaptive channel hopping (QoS), and the
  8. // host-frame keepalive beacons. RE/calibration scaffolding lives separately in rf_diag.cpp.
  9. //
  10. // RECIPE (IBEX disasm @0x18d80): the controller streams 0xF1 ONLY when (a) connstate==3 [host frame E1
  11. // establishes it], (b) we mark host-awake via E7 [00][00], and (c) each E3 poll carries a GET-report-0x45
  12. // sub-TLV. F1 reply = type 0xF1 then TLV type4(analog/buttons)+type6(per-module gyro/accel/sticks).
  13. #pragma once
  14. #include <stdint.h>
  15. // ---- operational toggles + tunables (set from the CDC console / WebUSB; read here + by webusb status) ----
  16. // auto-start host beacon on boot (resumes puck role after a USB replug)
  17. extern bool g_rfHost;
  18. extern bool g_connOn; // auto-start connected-mode poll on boot
  19. extern uint8_t g_connType; // start packet type (E7 handshake, then E3)
  20. // E7 payload B-byte: 0=current(slow/awake), 1=protocol-version-1
  21. extern uint8_t g_e7b;
  22. extern uint8_t g_connLen;
  23. extern uint8_t g_getParam; // GET report 0x45 param byte
  24. // E3-poll PID/S1 mode (0=fixed07, 1=cyclePID+noack1, 2=cyclePID+noack0)
  25. extern uint8_t g_e3mode;
  26. // full multi-line debug dump vs compact "I45 <hex>" stream
  27. extern bool g_connVerbose;
  28. extern uint32_t g_rxWin; // poll RX-window (us): caps poll rate (~1e6/rxWin)
  29. // set on 0xF2 disconnect; pauses beacon+poll so a powering-off controller can sleep
  30. extern unsigned long g_connCooldown;
  31. // connected-mode state (reset by the 'k' console toggle)
  32. extern uint8_t g_connSt, g_connStep;
  33. extern uint16_t g_connPoll;
  34. extern uint32_t g_connF1; // count of 0xF1 input reports seen
  35. // last protocol version the controller reported in an F3 reply (0xFF=none)
  36. extern uint8_t g_connF3v;
  37. // QoS adaptive channel hopping
  38. extern uint8_t g_qos; // 0=off (static g_sessCh), 1=auto-hop on degradation
  39. extern uint8_t g_hopIdx;
  40. extern volatile uint16_t g_qosBad;
  41. extern unsigned long g_qosCheckMs, g_qosLastHopMs;
  42. // per-second rate readouts for the WebUSB status blob
  43. extern uint16_t g_f1ps; // last completed second's F1 rate
  44. // genuine new-report rate (report 0x45 seq byte changes)
  45. extern uint16_t g_newps;
  46. // last second's poll TX count (GET+relay) -- vs F1 tells starvation from reply-loss
  47. extern uint16_t g_pollsps;
  48. // MEASURED avg us between GET-poll fires (compare to the intended g_pollUs=4000)
  49. extern uint16_t g_pollPeriodUs;
  50. // Smoothed controller->puck signal strength, sampled by the radio (RSSISAMPLE) on each CRC-good controller
  51. // reply during the poll. Stored as the dBm MAGNITUDE (35 = -35dBm); 0 = no sample yet. puck_hid reports it
  52. // to Steam in status report 0x7B byte 8 (signed dBm), after subtracting RSSI_DBM_OFFSET.
  53. extern volatile uint8_t g_linkRssi;
  54. // dB to subtract from our raw RSSI magnitude before reporting, to match the real puck's close-range -35dBm
  55. // (compensates the Pro Micro antenna vs Valve's front-end). Tune against one known-distance reading.
  56. #define RSSI_DBM_OFFSET 20
  57. // Battery percent from the controller's periodic report 0x43 (body[1]); 0 = none yet. Surfaced in the WebUSB
  58. // panel; the raw 0x43 report is also forwarded to Steam verbatim (puck_hid onAuxReport) so Steam reads it itself.
  59. extern volatile uint8_t g_battery;
  60. // TX one connected packet [LEN][S1][payload] on channel ch, then RX the reply into rfrx; decodes 0xF1.
  61. // rxWinUs overrides the reply-wait window (0 = use g_rxWin). Pass a tiny value for NO-ACK relays that expect
  62. // no reply, so they don't burn a full ~1.2ms window of dead air per haptic.
  63. uint8_t rfConnTx(uint8_t ch, uint8_t s1, const uint8_t *payload, uint8_t plen,
  64. uint16_t rxWinUs = 0);
  65. // Mid-session channel hop: advertise newCh on the current channel a few times, then move the poll to newCh.
  66. void rfHopTo(uint8_t newCh);
  67. // Per-loop: host-frame beacons + connected-mode poll + remote-wakeup + QoS hop + per-second stats.
  68. void rfLinkTask();