radio.cpp 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "radio.h"
  2. // Pairing rendezvous bytes from IBEX rodata. The 0x91A2A793 key that precedes the "ibex" string is a
  3. // DIFFERENT key, NOT the discovery address (confirmed via PTR_s_ibexesb_common=0x56f99 -> "ibex...").
  4. const uint8_t PAIR_BASE[4] = {0x69, 0x62, 0x65, 0x78}; // "ibex"
  5. uint8_t g_rfPrefix = 0x10;
  6. uint8_t g_rfCh = 2;
  7. uint8_t g_rfBase[4] = {0x69, 0x62, 0x65, 0x78}; // "ibex"
  8. // ch18 measured clean (RSSI 95/90, PER 0) on the REAL PUCK's own channel scan and is in its active hop set
  9. // {18,2,80}; trackpad data bursts collide on bad channels -> reply-rate crash. Tunable 'C'.
  10. uint8_t g_sessCh = 18;
  11. // pre-init to "ibex"/0x10 as a safe default; rfGenSessionAddr() overwrites with the per-device value at boot.
  12. uint8_t g_sessBase[4] = {0x69, 0x62, 0x65, 0x78};
  13. uint8_t g_sessPrefix = 0x10;
  14. void rfGenSessionAddr(){
  15. // Mix the 64-bit FICR DEVICEID into a 4-byte base + a prefix. Deterministic per chip (stable across reboots,
  16. // debuggable) and unique across chips (DEVICEID is unique) -> two OpenPucks never collide on the connected
  17. // session. Avoid degenerate address bytes (0x00/0xFF) for clean preamble correlation, and never reproduce
  18. // the shared discovery base "ibex" (else the isolation is lost).
  19. uint32_t h = NRF_FICR->DEVICEID[0] * 0x9E3779B1u ^ NRF_FICR->DEVICEID[1];
  20. uint32_t h2 = NRF_FICR->DEVICEID[1] * 0x85EBCA6Bu ^ NRF_FICR->DEVICEID[0];
  21. for(int i=0;i<4;i++){ uint8_t b=(uint8_t)(h>>(i*8)); if(b==0x00||b==0xFF) b^=0x5A; g_sessBase[i]=b; }
  22. uint8_t p=(uint8_t)(h2>>16); if(p==0x00||p==0xFF) p=0x5C; g_sessPrefix=p;
  23. if(g_sessBase[0]==g_rfBase[0] && g_sessBase[1]==g_rfBase[1] &&
  24. g_sessBase[2]==g_rfBase[2] && g_sessBase[3]==g_rfBase[3]) g_sessBase[0]^=0x80; // collided with "ibex"
  25. }
  26. uint8_t rfrx[80], rftx[80];
  27. uint32_t g_rfRxCount = 0;
  28. // HYBRID puck-link config (firmware-derived, definitive). DECODED from real-puck capture: puck TX = Ble_2Mbit,
  29. // ENDIAN=Big (MSB-first), NO whitening, addr "ibex", CRC16 0x11021/0xFFFF; frame = FUN_00027e9a (static length).
  30. uint32_t g_crcinit = 0xFFFF;
  31. uint8_t g_whiteiv = 37;
  32. uint8_t g_mode = RADIO_MODE_MODE_Ble_2Mbit; // <-- was the bug (1Mbit). Real puck = 2Mbit.
  33. uint32_t g_pcnf0 = 0x00030008UL; // S0LEN0, LFLEN8, S1LEN3 (ESB DPL) - CRC-VALIDATED
  34. uint8_t g_statlen = 0x20;
  35. uint32_t g_pcnf1 = 0x01040040; // ENDIAN=Big, BALEN4, MAXLEN64 - CRC-VALIDATED
  36. uint32_t g_crcpoly = 0x11021UL;
  37. uint16_t g_crccnf = 0x2; // CRC16, address included
  38. uint8_t g_pid = 0;
  39. uint8_t g_balen = 4; // ESB 5-byte addr
  40. bool g_prefixRaw = false;
  41. uint8_t rfBitrev8(uint8_t x){ uint8_t y=0; for(int i=0;i<8;i++){y=(y<<1)|(x&1);x>>=1;} return y; }
  42. void rfSetAddr(const uint8_t b4[4], uint8_t prefix){
  43. uint8_t b[4]; for(int i=0;i<4;i++) b[i]=rfBitrev8(b4[i]);
  44. NRF_RADIO->BASE0 = ((uint32_t)b[0]<<24)|((uint32_t)b[1]<<16)|((uint32_t)b[2]<<8)|b[3];
  45. NRF_RADIO->PREFIX0 = g_prefixRaw ? prefix : rfBitrev8(prefix);
  46. NRF_RADIO->TXADDRESS = 0; NRF_RADIO->RXADDRESSES = 1u<<0;
  47. }
  48. void rfConfig(uint8_t ch){
  49. NRF_RADIO->TASKS_DISABLE = 1; RWAIT_DISABLED(); NRF_RADIO->EVENTS_DISABLED = 0;
  50. NRF_RADIO->MODE = ((uint32_t)g_mode << RADIO_MODE_MODE_Pos); // Ble_2Mbit (tunable 'M')
  51. NRF_RADIO->FREQUENCY = ch;
  52. NRF_RADIO->TXPOWER = (RADIO_TXPOWER_TXPOWER_0dBm << RADIO_TXPOWER_TXPOWER_Pos);
  53. #if defined(RADIO_MODECNF0_RU_Fast)
  54. NRF_RADIO->MODECNF0 = (RADIO_MODECNF0_RU_Fast<<RADIO_MODECNF0_RU_Pos);
  55. #endif
  56. NRF_RADIO->PCNF0 = g_pcnf0; // static length (tunable '0'); g_pcnf1=0 -> build from g_statlen/g_balen
  57. NRF_RADIO->PCNF1 = g_pcnf1 ? g_pcnf1 :
  58. ((1u<<RADIO_PCNF1_ENDIAN_Pos) // ENDIAN=Big (puck transmits MSB-first)
  59. |((uint32_t)g_balen<<RADIO_PCNF1_BALEN_Pos)
  60. |((uint32_t)g_statlen<<RADIO_PCNF1_STATLEN_Pos)
  61. |((uint32_t)g_statlen<<RADIO_PCNF1_MAXLEN_Pos)); // WHITEEN=0
  62. NRF_RADIO->CRCCNF = g_crccnf; // CRC16, address included
  63. NRF_RADIO->CRCPOLY = g_crcpoly; NRF_RADIO->CRCINIT = g_crcinit;
  64. NRF_RADIO->DATAWHITEIV = g_whiteiv;
  65. NRF_RADIO->PACKETPTR = (uint32_t)rfrx;
  66. }