radio.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // radio.h -- bare-metal nRF52 RADIO (ESB-style) hardware layer.
  2. //
  3. // *** VERBATIM COPY of OpenPuck/radio.h -- keep byte-identical. ***
  4. // The controller emulator speaks the SAME 2.4 GHz PHY/CRC/address layer as the puck; only the
  5. // roles are inverted (we answer polls instead of issuing them). Arduino compiles a sketch folder
  6. // flat, so this file is duplicated rather than shared. If you change the radio layer in OpenPuck,
  7. // mirror it here (and vice-versa) -- the two MUST agree on the air.
  8. //
  9. // Register-confirmed config (decoded from real-puck capture): PHY=Ble_2Mbit, ENDIAN=Big (MSB-first),
  10. // whitening off, addr "ibex", CRC16 poly 0x11021 init 0xFFFF (address included). On-air address =
  11. // bitrev8 of stored bytes.
  12. #pragma once
  13. #include <stdint.h>
  14. #include <Arduino.h>
  15. // Bounded radio-disable wait: NEVER spin forever. A wedged RADIO would hang the main loop (USB stops
  16. // being serviced -> device "dies" until replug). On timeout we bail; the next rfConfig re-inits.
  17. #define RWAIT_DISABLED() \
  18. do { \
  19. uint32_t _w = micros(); \
  20. while (!NRF_RADIO->EVENTS_DISABLED && \
  21. (uint32_t)(micros() - _w) < 3000) { \
  22. } \
  23. } while (0)
  24. // Discovery/reconnect address (IBEX FUN_00019000, firmware-derived, definitive):
  25. // base = "ibex" (69 62 65 78), prefix = 0x10, channel = 2.
  26. extern const uint8_t PAIR_BASE[4]; // "ibex"
  27. extern uint8_t g_rfPrefix; // discovery prefix
  28. extern uint8_t g_rfCh; // current TX/RX channel
  29. extern uint8_t g_rfBase[4]; // "ibex"
  30. // connected-session channel: a CLEAN data channel off the congested adv ch2
  31. extern uint8_t g_sessCh;
  32. // Session address (base+prefix). On the puck this is per-device and advertised; on the controller
  33. // emulator it is ADOPTED from the puck's E1 host frame (the puck advertises it; we follow).
  34. extern uint8_t g_sessBase[4];
  35. extern uint8_t g_sessPrefix;
  36. void rfGenSessionAddr();
  37. // RADIO DMA buffers (>= MAXLEN+2; the controller's 0x43-augmented F1 is ~66B)
  38. extern uint8_t rfrx[100], rftx[100];
  39. extern uint32_t g_rfRxCount;
  40. // ---- tunable radio parameters ----
  41. extern uint32_t g_crcinit;
  42. extern uint8_t g_whiteiv;
  43. extern uint8_t g_mode; // RADIO_MODE_MODE_* (real puck = Ble_2Mbit)
  44. extern uint32_t g_pcnf0; // S0LEN0, LFLEN8, S1LEN3 (ESB DPL) -- CRC-VALIDATED
  45. extern uint8_t g_statlen; // static-mode payload length
  46. extern uint32_t g_pcnf1; // ENDIAN=Big, BALEN4, MAXLEN96 -- CRC-VALIDATED
  47. extern uint32_t g_crcpoly;
  48. extern uint16_t g_crccnf; // CRC16, address included
  49. extern uint8_t g_pid;
  50. extern uint8_t g_balen; // ESB 5-byte addr
  51. // false = bitrev8 prefix (ESB addr_conv); true = raw prefix byte
  52. extern bool g_prefixRaw;
  53. uint8_t rfBitrev8(uint8_t x);
  54. // Access address (IBEX FUN_00037530): BASE0 = bitrev8 each base byte packed big-endian; PREFIX0 = raw
  55. // or bitrev8 prefix per g_prefixRaw. "ibex" 69 62 65 78 -> BASE0=0x9646A61E, PREFIX0=0x10.
  56. void rfSetAddr(const uint8_t b4[4], uint8_t prefix);
  57. // Program MODE/FREQUENCY/PCNF/CRC/whitening from the tunables above onto channel ch (radio left disabled).
  58. void rfConfig(uint8_t ch);