radio.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "radio.h"
  2. // *** VERBATIM COPY of OpenPuck/radio.cpp -- keep byte-identical (see radio.h header note). ***
  3. // On the controller emulator rfGenSessionAddr() only seeds a safe default; the operational session
  4. // address is ADOPTED from the puck's E1 host frame in ctrl_link.cpp.
  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}. Tunable.
  10. uint8_t g_sessCh = 18;
  11. // Safe default; adopted from the puck's E1 host frame at connect time.
  12. uint8_t g_sessBase[4] = { 0x69, 0x62, 0x65, 0x78 };
  13. uint8_t g_sessPrefix = 0x10;
  14. void rfGenSessionAddr()
  15. {
  16. uint32_t h = NRF_FICR->DEVICEID[0] * 0x9E3779B1u ^
  17. NRF_FICR->DEVICEID[1];
  18. uint32_t h2 = NRF_FICR->DEVICEID[1] * 0x85EBCA6Bu ^
  19. NRF_FICR->DEVICEID[0];
  20. for (int i = 0; i < 4; i++) {
  21. uint8_t b = (uint8_t)(h >> (i * 8));
  22. if (b == 0x00 || b == 0xFF)
  23. b ^= 0x5A;
  24. g_sessBase[i] = b;
  25. }
  26. uint8_t p = (uint8_t)(h2 >> 16);
  27. if (p == 0x00 || p == 0xFF)
  28. p = 0x5C;
  29. g_sessPrefix = p;
  30. if (g_sessBase[0] == g_rfBase[0] && g_sessBase[1] == g_rfBase[1] &&
  31. g_sessBase[2] == g_rfBase[2] && g_sessBase[3] == g_rfBase[3])
  32. g_sessBase[0] ^= 0x80;
  33. }
  34. uint8_t rfrx[100], rftx[100];
  35. uint32_t g_rfRxCount = 0;
  36. // Puck-link config decoded from real-puck capture: Ble_2Mbit, ENDIAN=Big, NO whitening, addr "ibex",
  37. // CRC16 0x11021/0xFFFF; static-length frame.
  38. uint32_t g_crcinit = 0xFFFF;
  39. uint8_t g_whiteiv = 37;
  40. uint8_t g_mode = RADIO_MODE_MODE_Ble_2Mbit;
  41. // S0LEN0, LFLEN8, S1LEN3 (ESB DPL) - CRC-VALIDATED
  42. uint32_t g_pcnf0 = 0x00030008UL;
  43. uint8_t g_statlen = 0x20;
  44. // ENDIAN=Big, BALEN4, MAXLEN=96.
  45. uint32_t g_pcnf1 = 0x01040060;
  46. uint32_t g_crcpoly = 0x11021UL;
  47. uint16_t g_crccnf = 0x2; // CRC16, address included
  48. uint8_t g_pid = 0;
  49. uint8_t g_balen = 4; // ESB 5-byte addr
  50. bool g_prefixRaw = false;
  51. uint8_t rfBitrev8(uint8_t x)
  52. {
  53. uint8_t y = 0;
  54. for (int i = 0; i < 8; i++) {
  55. y = (y << 1) | (x & 1);
  56. x >>= 1;
  57. }
  58. return y;
  59. }
  60. void rfSetAddr(const uint8_t b4[4], uint8_t prefix)
  61. {
  62. uint8_t b[4];
  63. for (int i = 0; i < 4; i++)
  64. b[i] = rfBitrev8(b4[i]);
  65. NRF_RADIO->BASE0 = ((uint32_t)b[0] << 24) | ((uint32_t)b[1] << 16) |
  66. ((uint32_t)b[2] << 8) | b[3];
  67. NRF_RADIO->PREFIX0 = g_prefixRaw ? prefix : rfBitrev8(prefix);
  68. NRF_RADIO->TXADDRESS = 0;
  69. NRF_RADIO->RXADDRESSES = 1u << 0;
  70. }
  71. void rfConfig(uint8_t ch)
  72. {
  73. NRF_RADIO->TASKS_DISABLE = 1;
  74. RWAIT_DISABLED();
  75. NRF_RADIO->EVENTS_DISABLED = 0;
  76. NRF_RADIO->MODE = ((uint32_t)g_mode << RADIO_MODE_MODE_Pos);
  77. NRF_RADIO->FREQUENCY = ch;
  78. NRF_RADIO->TXPOWER =
  79. (RADIO_TXPOWER_TXPOWER_0dBm << RADIO_TXPOWER_TXPOWER_Pos);
  80. #if defined(RADIO_MODECNF0_RU_Fast)
  81. NRF_RADIO->MODECNF0 = (RADIO_MODECNF0_RU_Fast << RADIO_MODECNF0_RU_Pos);
  82. #endif
  83. NRF_RADIO->PCNF0 = g_pcnf0;
  84. NRF_RADIO->PCNF1 =
  85. g_pcnf1 ? g_pcnf1 :
  86. ((1u << RADIO_PCNF1_ENDIAN_Pos) |
  87. ((uint32_t)g_balen << RADIO_PCNF1_BALEN_Pos) |
  88. ((uint32_t)g_statlen << RADIO_PCNF1_STATLEN_Pos) |
  89. ((uint32_t)g_statlen << RADIO_PCNF1_MAXLEN_Pos));
  90. NRF_RADIO->CRCCNF = g_crccnf; // CRC16, address included
  91. NRF_RADIO->CRCPOLY = g_crcpoly;
  92. NRF_RADIO->CRCINIT = g_crcinit;
  93. NRF_RADIO->DATAWHITEIV = g_whiteiv;
  94. NRF_RADIO->PACKETPTR = (uint32_t)rfrx;
  95. }