radio.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "radio.h"
  2. #include "bonds.h" // g_slot[]
  3. // Pairing rendezvous bytes from IBEX rodata. The 0x91A2A793 key preceding "ibex" is a DIFFERENT key,
  4. // NOT the discovery address.
  5. const uint8_t PAIR_BASE[4] = { 0x69, 0x62, 0x65, 0x78 }; // "ibex"
  6. uint8_t g_rfPrefix = 0x10;
  7. uint8_t g_rfCh = 2;
  8. uint8_t g_rfBase[4] = { 0x69, 0x62, 0x65, 0x78 }; // "ibex"
  9. // ch18: clean channel in the real puck's active hop set {18,2,80}; bad channels collide with trackpad
  10. // data bursts -> reply-rate crash. Tunable 'C'.
  11. uint8_t g_sessCh = 18;
  12. // Safe defaults; rfGenSessionAddr(slot) overwrites each used slot at boot. Discovery base "ibex" as a
  13. // degenerate starting point means an UN-bonded slot (and a not-yet-initialized one) won't accidentally
  14. // cross-talk with itself before the real address is set.
  15. uint8_t g_sessBase[NSLOT][4];
  16. uint8_t g_sessPrefix[NSLOT];
  17. static void sessSetDefault(int slot)
  18. {
  19. for (int i = 0; i < 4; i++)
  20. g_sessBase[slot][i] = g_rfBase[i];
  21. g_sessPrefix[slot] = g_rfPrefix;
  22. }
  23. void rfGenSessionAddr(int slot)
  24. {
  25. if (slot < 0 || slot >= NSLOT) {
  26. sessSetDefault(0);
  27. return;
  28. }
  29. sessSetDefault(slot);
  30. // Per-bond UUID (rec[0..8]) + FICR DEVICEID (8 bytes) -> 5-byte on-air address. Mixing the bond UUID in
  31. // makes the address deterministic per (puck, controller) pair: a slot's address is stable across reboots
  32. // without storing anything new in bonds.bin, and the same controller bonded to two OpenPucks gets two
  33. // different session addresses (DEVICEID differs) so the two pucks never collide on-air. The original
  34. // generator (UUID-free) is preserved as the no-UUID fallback for an empty/zero bond record.
  35. const uint8_t *uuid = g_slot[slot].rec;
  36. bool uuidLive = false;
  37. for (int i = 0; i < 8; i++)
  38. if (uuid[i]) {
  39. uuidLive = true;
  40. break;
  41. }
  42. uint32_t h, h2;
  43. if (uuidLive) {
  44. h = (uint32_t)uuid[0] * 0x9E3779B1u ^
  45. (uint32_t)uuid[2] * 0x85EBCA6Bu ^
  46. (uint32_t)uuid[4] * 0xC2B2AE35u ^ (uint32_t)uuid[6] ^
  47. NRF_FICR->DEVICEID[0];
  48. h2 = (uint32_t)uuid[1] * 0x27D4EB2Fu ^
  49. (uint32_t)uuid[3] * 0x165667B1u ^ (uint32_t)uuid[5] ^
  50. (uint32_t)uuid[7] * 0x9E3779B1u ^ NRF_FICR->DEVICEID[1];
  51. } else {
  52. h = NRF_FICR->DEVICEID[0] * 0x9E3779B1u ^ NRF_FICR->DEVICEID[1];
  53. h2 = NRF_FICR->DEVICEID[1] * 0x85EBCA6Bu ^
  54. NRF_FICR->DEVICEID[0];
  55. }
  56. // Scrub degenerate bytes (0x00/0xFF) for clean preamble correlation.
  57. for (int i = 0; i < 4; i++) {
  58. uint8_t b = (uint8_t)(h >> (i * 8));
  59. if (b == 0x00 || b == 0xFF)
  60. b ^= 0x5A;
  61. g_sessBase[slot][i] = b;
  62. }
  63. uint8_t p = (uint8_t)(h2 >> 16);
  64. if (p == 0x00 || p == 0xFF)
  65. p = 0x5C;
  66. g_sessPrefix[slot] = p;
  67. // Never reproduce the shared discovery base "ibex" (else the on-air address is shared with every other
  68. // controller scanning the rendezvous -> isolation is lost).
  69. if (g_sessBase[slot][0] == g_rfBase[0] &&
  70. g_sessBase[slot][1] == g_rfBase[1] &&
  71. g_sessBase[slot][2] == g_rfBase[2] &&
  72. g_sessBase[slot][3] == g_rfBase[3])
  73. g_sessBase[slot][0] ^= 0x80;
  74. }
  75. uint8_t rfrx[100], rftx[100];
  76. uint32_t g_rfRxCount = 0;
  77. // Puck-link config decoded from real-puck capture: puck TX = Ble_2Mbit, ENDIAN=Big (MSB-first), NO whitening,
  78. // addr "ibex", CRC16 0x11021/0xFFFF; static-length frame.
  79. uint32_t g_crcinit = 0xFFFF;
  80. uint8_t g_whiteiv = 37;
  81. uint8_t g_mode = RADIO_MODE_MODE_Ble_2Mbit;
  82. // S0LEN0, LFLEN8, S1LEN3 (ESB DPL) - CRC-VALIDATED
  83. uint32_t g_pcnf0 = 0x00030008UL;
  84. uint8_t g_statlen = 0x20;
  85. // ENDIAN=Big, BALEN4, MAXLEN=96. MAXLEN must be >=66 or the controller's 0x43-battery-augmented F1 (66B)
  86. // truncates -> CRC-fail -> battery never arrives.
  87. uint32_t g_pcnf1 = 0x01040060;
  88. uint32_t g_crcpoly = 0x11021UL;
  89. uint16_t g_crccnf = 0x2; // CRC16, address included
  90. uint8_t g_pid = 0;
  91. uint8_t g_balen = 4; // ESB 5-byte addr
  92. bool g_prefixRaw = false;
  93. uint8_t rfBitrev8(uint8_t x)
  94. {
  95. uint8_t y = 0;
  96. for (int i = 0; i < 8; i++) {
  97. y = (y << 1) | (x & 1);
  98. x >>= 1;
  99. }
  100. return y;
  101. }
  102. void rfSetAddr(const uint8_t b4[4], uint8_t prefix)
  103. {
  104. uint8_t b[4];
  105. for (int i = 0; i < 4; i++)
  106. b[i] = rfBitrev8(b4[i]);
  107. NRF_RADIO->BASE0 = ((uint32_t)b[0] << 24) | ((uint32_t)b[1] << 16) |
  108. ((uint32_t)b[2] << 8) | b[3];
  109. NRF_RADIO->PREFIX0 = g_prefixRaw ? prefix : rfBitrev8(prefix);
  110. NRF_RADIO->TXADDRESS = 0;
  111. NRF_RADIO->RXADDRESSES = 1u << 0;
  112. }
  113. void rfConfig(uint8_t ch)
  114. {
  115. NRF_RADIO->TASKS_DISABLE = 1;
  116. RWAIT_DISABLED();
  117. NRF_RADIO->EVENTS_DISABLED = 0;
  118. NRF_RADIO->MODE = ((uint32_t)g_mode << RADIO_MODE_MODE_Pos);
  119. NRF_RADIO->FREQUENCY = ch;
  120. // +8dBm = the nRF52840's max TX power (was 0dBm). The link margin is thin on
  121. // the Pro Micro's PCB-trace antenna (RSSI reads ~20dB below a real puck), and
  122. // 2.4GHz is crowded, so weak polls the controller can't hear -> no reply ->
  123. // the reply gaps that read as intermittent disconnects. This only lifts the
  124. // puck->controller side (poll/beacon reach); the controller's own TX (the F1
  125. // reply) is fixed -- but the poll MUST land for the controller to answer, so
  126. // the extra 8dB of headroom directly cuts missed-poll dropouts. USB-powered,
  127. // so the higher current draw is free.
  128. NRF_RADIO->TXPOWER =
  129. (RADIO_TXPOWER_TXPOWER_Pos8dBm << RADIO_TXPOWER_TXPOWER_Pos);
  130. #if defined(RADIO_MODECNF0_RU_Fast)
  131. NRF_RADIO->MODECNF0 = (RADIO_MODECNF0_RU_Fast << RADIO_MODECNF0_RU_Pos);
  132. #endif
  133. NRF_RADIO->PCNF0 = g_pcnf0;
  134. // g_pcnf1=0 -> build static-length config from g_statlen/g_balen (ENDIAN=Big, WHITEEN=0)
  135. NRF_RADIO->PCNF1 =
  136. g_pcnf1 ? g_pcnf1 :
  137. ((1u << RADIO_PCNF1_ENDIAN_Pos) |
  138. ((uint32_t)g_balen << RADIO_PCNF1_BALEN_Pos) |
  139. ((uint32_t)g_statlen << RADIO_PCNF1_STATLEN_Pos) |
  140. ((uint32_t)g_statlen << RADIO_PCNF1_MAXLEN_Pos));
  141. NRF_RADIO->CRCCNF = g_crccnf; // CRC16, address included
  142. NRF_RADIO->CRCPOLY = g_crcpoly;
  143. NRF_RADIO->CRCINIT = g_crcinit;
  144. NRF_RADIO->DATAWHITEIV = g_whiteiv;
  145. NRF_RADIO->PACKETPTR = (uint32_t)rfrx;
  146. }