nrf24.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. // Modified by vad7, 25.11.2022
  2. //
  3. #include "nrf24.h"
  4. #include <furi.h>
  5. #include <furi_hal.h>
  6. #include <furi_hal_resources.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. void nrf24_init() {
  10. // this is needed if multiple SPI devices are connected to the same bus but with different CS pins
  11. if(momentum_settings.spi_nrf24_handle == SpiDefault) {
  12. furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeOutputPushPull);
  13. furi_hal_gpio_write(&gpio_ext_pc3, true);
  14. } else if(momentum_settings.spi_nrf24_handle == SpiExtra) {
  15. furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeOutputPushPull);
  16. furi_hal_gpio_write(&gpio_ext_pa4, true);
  17. }
  18. furi_hal_spi_bus_handle_init(nrf24_HANDLE);
  19. furi_hal_spi_acquire(nrf24_HANDLE);
  20. furi_hal_gpio_init(nrf24_CE_PIN, GpioModeOutputPushPull, GpioPullUp, GpioSpeedVeryHigh);
  21. furi_hal_gpio_write(nrf24_CE_PIN, false);
  22. }
  23. void nrf24_deinit() {
  24. furi_hal_spi_release(nrf24_HANDLE);
  25. furi_hal_spi_bus_handle_deinit(nrf24_HANDLE);
  26. furi_hal_gpio_write(nrf24_CE_PIN, false);
  27. furi_hal_gpio_init(nrf24_CE_PIN, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  28. // resetting the CS pins to floating
  29. if(momentum_settings.spi_nrf24_handle == SpiDefault) {
  30. furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeAnalog);
  31. } else if(momentum_settings.spi_nrf24_handle == SpiExtra) {
  32. furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeAnalog);
  33. }
  34. }
  35. void nrf24_spi_trx(
  36. const FuriHalSpiBusHandle* handle,
  37. uint8_t* tx,
  38. uint8_t* rx,
  39. uint8_t size,
  40. uint32_t timeout) {
  41. UNUSED(timeout);
  42. furi_hal_gpio_write(handle->cs, false);
  43. furi_hal_spi_bus_trx(handle, tx, rx, size, nrf24_TIMEOUT);
  44. furi_hal_gpio_write(handle->cs, true);
  45. }
  46. uint8_t nrf24_write_reg(const FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t data) {
  47. uint8_t tx[2] = {W_REGISTER | (REGISTER_MASK & reg), data};
  48. uint8_t rx[2] = {0};
  49. nrf24_spi_trx(handle, tx, rx, 2, nrf24_TIMEOUT);
  50. //FURI_LOG_D("NRF_WR", " #%02X=%02X", reg, data);
  51. return rx[0];
  52. }
  53. uint8_t nrf24_write_buf_reg(const FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* data, uint8_t size) {
  54. uint8_t tx[size + 1];
  55. uint8_t rx[size + 1];
  56. memset(rx, 0, size + 1);
  57. tx[0] = W_REGISTER | (REGISTER_MASK & reg);
  58. memcpy(&tx[1], data, size);
  59. nrf24_spi_trx(handle, tx, rx, size + 1, nrf24_TIMEOUT);
  60. //FURI_LOG_D("NRF_WR", " #%02X(%02X)=0x%02X%02X%02X%02X%02X", reg, size, data[0], data[1], data[2], data[3], data[4] );
  61. return rx[0];
  62. }
  63. uint8_t nrf24_read_reg(const FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* data, uint8_t size) {
  64. uint8_t tx[size + 1];
  65. uint8_t rx[size + 1];
  66. memset(rx, 0, size + 1);
  67. tx[0] = R_REGISTER | (REGISTER_MASK & reg);
  68. memset(&tx[1], 0, size);
  69. nrf24_spi_trx(handle, tx, rx, size + 1, nrf24_TIMEOUT);
  70. memcpy(data, &rx[1], size);
  71. return rx[0];
  72. }
  73. uint8_t nrf24_flush_rx(const FuriHalSpiBusHandle* handle) {
  74. uint8_t tx[] = {FLUSH_RX};
  75. uint8_t rx[] = {0};
  76. nrf24_spi_trx(handle, tx, rx, 1, nrf24_TIMEOUT);
  77. return rx[0];
  78. }
  79. uint8_t nrf24_flush_tx(const FuriHalSpiBusHandle* handle) {
  80. uint8_t tx[] = {FLUSH_TX};
  81. uint8_t rx[] = {0};
  82. nrf24_spi_trx(handle, tx, rx, 1, nrf24_TIMEOUT);
  83. return rx[0];
  84. }
  85. uint8_t nrf24_get_maclen(const FuriHalSpiBusHandle* handle) {
  86. uint8_t maclen;
  87. nrf24_read_reg(handle, REG_SETUP_AW, &maclen, 1);
  88. maclen &= 3;
  89. return maclen + 2;
  90. }
  91. uint8_t nrf24_set_maclen(const FuriHalSpiBusHandle* handle, uint8_t maclen) {
  92. assert(maclen > 1 && maclen < 6);
  93. uint8_t status = 0;
  94. status = nrf24_write_reg(handle, REG_SETUP_AW, maclen - 2);
  95. return status;
  96. }
  97. uint8_t nrf24_status(const FuriHalSpiBusHandle* handle) {
  98. uint8_t status;
  99. uint8_t tx[] = {R_REGISTER | (REGISTER_MASK & REG_STATUS)};
  100. nrf24_spi_trx(handle, tx, &status, 1, nrf24_TIMEOUT);
  101. return status;
  102. }
  103. uint32_t nrf24_get_rate(const FuriHalSpiBusHandle* handle) {
  104. uint8_t setup = 0;
  105. uint32_t rate = 0;
  106. nrf24_read_reg(handle, REG_RF_SETUP, &setup, 1);
  107. setup &= 0x28;
  108. if(setup == 0x20)
  109. rate = 250000; // 250kbps
  110. else if(setup == 0x08)
  111. rate = 2000000; // 2Mbps
  112. else if(setup == 0x00)
  113. rate = 1000000; // 1Mbps
  114. return rate;
  115. }
  116. uint8_t nrf24_set_rate(const FuriHalSpiBusHandle* handle, uint32_t rate) {
  117. uint8_t r6 = 0;
  118. uint8_t status = 0;
  119. if(!rate) rate = 2000000;
  120. nrf24_read_reg(handle, REG_RF_SETUP, &r6, 1); // RF_SETUP register
  121. r6 = r6 & (~0x28); // Clear rate fields.
  122. if(rate == 2000000)
  123. r6 = r6 | 0x08;
  124. else if(rate == 1000000)
  125. r6 = r6;
  126. else if(rate == 250000)
  127. r6 = r6 | 0x20;
  128. status = nrf24_write_reg(handle, REG_RF_SETUP, r6); // Write new rate.
  129. return status;
  130. }
  131. uint8_t nrf24_get_chan(const FuriHalSpiBusHandle* handle) {
  132. uint8_t channel = 0;
  133. nrf24_read_reg(handle, REG_RF_CH, &channel, 1);
  134. return channel;
  135. }
  136. uint8_t nrf24_set_chan(const FuriHalSpiBusHandle* handle, uint8_t chan) {
  137. uint8_t status;
  138. status = nrf24_write_reg(handle, REG_RF_CH, chan);
  139. return status;
  140. }
  141. uint8_t nrf24_get_src_mac(const FuriHalSpiBusHandle* handle, uint8_t* mac) {
  142. uint8_t size = 0;
  143. uint8_t status = 0;
  144. size = nrf24_get_maclen(handle);
  145. status = nrf24_read_reg(handle, REG_RX_ADDR_P0, mac, size);
  146. return status;
  147. }
  148. uint8_t nrf24_set_src_mac(const FuriHalSpiBusHandle* handle, uint8_t* mac, uint8_t size) {
  149. uint8_t status = 0;
  150. uint8_t clearmac[] = {0, 0, 0, 0, 0};
  151. nrf24_set_maclen(handle, size);
  152. nrf24_write_buf_reg(handle, REG_RX_ADDR_P0, clearmac, 5);
  153. status = nrf24_write_buf_reg(handle, REG_RX_ADDR_P0, mac, size);
  154. return status;
  155. }
  156. uint8_t nrf24_get_dst_mac(const FuriHalSpiBusHandle* handle, uint8_t* mac) {
  157. uint8_t size = 0;
  158. uint8_t status = 0;
  159. size = nrf24_get_maclen(handle);
  160. status = nrf24_read_reg(handle, REG_TX_ADDR, mac, size);
  161. return status;
  162. }
  163. uint8_t nrf24_set_dst_mac(const FuriHalSpiBusHandle* handle, uint8_t* mac, uint8_t size) {
  164. uint8_t status = 0;
  165. uint8_t clearmac[] = {0, 0, 0, 0, 0};
  166. nrf24_set_maclen(handle, size);
  167. nrf24_write_buf_reg(handle, REG_TX_ADDR, clearmac, 5);
  168. status = nrf24_write_buf_reg(handle, REG_TX_ADDR, mac, size);
  169. return status;
  170. }
  171. uint8_t nrf24_get_packetlen(const FuriHalSpiBusHandle* handle, uint8_t pipe) {
  172. uint8_t len = 0;
  173. if(pipe > 5) pipe = 0;
  174. nrf24_read_reg(handle, RX_PW_P0 + pipe, &len, 1);
  175. return len;
  176. }
  177. uint8_t nrf24_set_packetlen(const FuriHalSpiBusHandle* handle, uint8_t len) {
  178. uint8_t status = 0;
  179. status = nrf24_write_reg(handle, RX_PW_P0, len);
  180. return status;
  181. }
  182. uint8_t nrf24_rxpacket(const FuriHalSpiBusHandle* handle, uint8_t* packet, uint8_t* ret_packetsize, uint8_t packet_size) {
  183. uint8_t status = 0;
  184. uint8_t tx_cmd[33] = {0}; // 32 max payload size + 1 for command
  185. uint8_t tmp_packet[33] = {0};
  186. status = nrf24_status(handle);
  187. if(!(status & RX_DR)) {
  188. tx_cmd[0] = R_REGISTER | (REGISTER_MASK & REG_FIFO_STATUS);
  189. nrf24_spi_trx(handle, tx_cmd, tmp_packet, 2, nrf24_TIMEOUT);
  190. if((tmp_packet[1] & 1) == 0) status |= RX_DR; // packet in FIFO buffer
  191. }
  192. if(status & RX_DR) {
  193. if(packet_size == 1)
  194. packet_size = nrf24_get_packetlen(handle, (status >> 1) & 7);
  195. else if(packet_size == 0) {
  196. tx_cmd[0] = R_RX_PL_WID;
  197. tx_cmd[1] = 0;
  198. nrf24_spi_trx(handle, tx_cmd, tmp_packet, 2, nrf24_TIMEOUT);
  199. packet_size = tmp_packet[1];
  200. }
  201. if(packet_size > 32 || packet_size == 0) packet_size = 32;
  202. tx_cmd[0] = R_RX_PAYLOAD;
  203. tx_cmd[1] = 0;
  204. nrf24_spi_trx(handle, tx_cmd, tmp_packet, packet_size + 1, nrf24_TIMEOUT);
  205. memcpy(packet, &tmp_packet[1], packet_size);
  206. nrf24_write_reg(handle, REG_STATUS, RX_DR); // clear RX_DR
  207. } else if(status & (TX_DS | MAX_RT)) { // MAX_RT, TX_DS
  208. nrf24_write_reg(handle, REG_STATUS, (TX_DS | MAX_RT)); // clear RX_DR, MAX_RT.
  209. }
  210. *ret_packetsize = packet_size;
  211. return status;
  212. }
  213. // Return 0 when error
  214. uint8_t nrf24_txpacket(const FuriHalSpiBusHandle* handle, uint8_t* payload, uint8_t size, bool ack) {
  215. uint8_t status = 0;
  216. uint8_t tx[size + 1];
  217. uint8_t rx[size + 1];
  218. memset(tx, 0, size + 1);
  219. memset(rx, 0, size + 1);
  220. if(!ack)
  221. tx[0] = W_TX_PAYLOAD_NOACK;
  222. else
  223. tx[0] = W_TX_PAYLOAD;
  224. memcpy(&tx[1], payload, size);
  225. nrf24_spi_trx(handle, tx, rx, size + 1, nrf24_TIMEOUT);
  226. nrf24_set_tx_mode(handle);
  227. uint32_t start_time = furi_get_tick();
  228. while(!(status & (TX_DS | MAX_RT)) && furi_get_tick() - start_time < 2000UL)
  229. status = nrf24_status(handle);
  230. if(status & MAX_RT) nrf24_flush_tx(handle);
  231. nrf24_set_idle(handle);
  232. nrf24_write_reg(handle, REG_STATUS, TX_DS | MAX_RT);
  233. return status & TX_DS;
  234. }
  235. uint8_t nrf24_power_up(const FuriHalSpiBusHandle* handle) {
  236. uint8_t status = 0;
  237. uint8_t cfg = 0;
  238. nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
  239. cfg = cfg | 2;
  240. status = nrf24_write_reg(handle, REG_CONFIG, cfg);
  241. furi_delay_ms(1000);
  242. return status;
  243. }
  244. uint8_t nrf24_set_idle(const FuriHalSpiBusHandle* handle) {
  245. uint8_t status = 0;
  246. uint8_t cfg = 0;
  247. nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
  248. cfg &= 0xfc; // clear bottom two bits to power down the radio
  249. status = nrf24_write_reg(handle, REG_CONFIG, cfg);
  250. //nr204_write_reg(handle, REG_EN_RXADDR, 0x0);
  251. furi_hal_gpio_write(nrf24_CE_PIN, false);
  252. return status;
  253. }
  254. uint8_t nrf24_set_rx_mode(const FuriHalSpiBusHandle* handle) {
  255. uint8_t status = 0;
  256. uint8_t cfg = 0;
  257. //status = nrf24_write_reg(handle, REG_CONFIG, 0x0F); // enable 2-byte CRC, PWR_UP, and PRIM_RX
  258. nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
  259. cfg |= 0x03; // PWR_UP, and PRIM_RX
  260. status = nrf24_write_reg(handle, REG_CONFIG, cfg);
  261. //nr204_write_reg(REG_EN_RXADDR, 0x03) // Set RX Pipe 0 and 1
  262. furi_hal_gpio_write(nrf24_CE_PIN, true);
  263. furi_delay_ms(2);
  264. return status;
  265. }
  266. uint8_t nrf24_set_tx_mode(const FuriHalSpiBusHandle* handle) {
  267. uint8_t status = 0;
  268. uint8_t cfg = 0;
  269. furi_hal_gpio_write(nrf24_CE_PIN, false);
  270. nrf24_write_reg(handle, REG_STATUS, 0x30);
  271. //status = nrf24_write_reg(handle, REG_CONFIG, 0x0E); // enable 2-byte CRC, PWR_UP
  272. nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
  273. cfg &= 0xfe; // disable PRIM_RX
  274. cfg |= 0x02; // PWR_UP
  275. status = nrf24_write_reg(handle, REG_CONFIG, cfg);
  276. furi_hal_gpio_write(nrf24_CE_PIN, true);
  277. furi_delay_ms(2);
  278. return status;
  279. }
  280. void nrf24_configure(
  281. const FuriHalSpiBusHandle* handle,
  282. uint8_t rate,
  283. uint8_t* srcmac,
  284. uint8_t* dstmac,
  285. uint8_t maclen,
  286. uint8_t channel,
  287. bool noack,
  288. bool disable_aa) {
  289. assert(channel <= 125);
  290. assert(rate == 1 || rate == 2);
  291. if(rate == 2)
  292. rate = 8; // 2Mbps
  293. else
  294. rate = 0; // 1Mbps
  295. nrf24_write_reg(handle, REG_CONFIG, 0x00); // Stop nRF
  296. nrf24_set_idle(handle);
  297. nrf24_write_reg(handle, REG_STATUS, 0x70); // clear interrupts
  298. if(disable_aa)
  299. nrf24_write_reg(handle, REG_EN_AA, 0x00); // Disable Shockburst
  300. else
  301. nrf24_write_reg(handle, REG_EN_AA, 0x1F); // Enable Shockburst
  302. nrf24_write_reg(handle, REG_DYNPD, 0x3F); // enable dynamic payload length on all pipes
  303. if(noack)
  304. nrf24_write_reg(handle, REG_FEATURE, 0x05); // disable payload-with-ack, enable noack
  305. else {
  306. nrf24_write_reg(handle, REG_CONFIG, 0x0C); // 2 byte CRC
  307. nrf24_write_reg(handle, REG_FEATURE, 0x07); // enable dyn payload and ack
  308. nrf24_write_reg(
  309. handle, REG_SETUP_RETR, 0x1f); // 15 retries for AA, 500us auto retransmit delay
  310. }
  311. nrf24_set_idle(handle);
  312. nrf24_flush_rx(handle);
  313. nrf24_flush_tx(handle);
  314. if(maclen) nrf24_set_maclen(handle, maclen);
  315. if(srcmac) nrf24_set_src_mac(handle, srcmac, maclen);
  316. if(dstmac) nrf24_set_dst_mac(handle, dstmac, maclen);
  317. nrf24_write_reg(handle, REG_RF_CH, channel);
  318. nrf24_write_reg(handle, REG_RF_SETUP, rate);
  319. furi_delay_ms(200);
  320. }
  321. void nrf24_init_promisc_mode(const FuriHalSpiBusHandle* handle, uint8_t channel, uint8_t rate) {
  322. //uint8_t preamble[] = {0x55, 0x00}; // little endian
  323. uint8_t preamble[] = {0xAA, 0x00}; // little endian
  324. //uint8_t preamble[] = {0x00, 0x55}; // little endian
  325. //uint8_t preamble[] = {0x00, 0xAA}; // little endian
  326. nrf24_write_reg(handle, REG_CONFIG, 0x00); // Stop nRF
  327. nrf24_write_reg(handle, REG_STATUS, 0x70); // clear interrupts
  328. nrf24_write_reg(handle, REG_DYNPD, 0x0); // disable shockburst
  329. nrf24_write_reg(handle, REG_EN_AA, 0x00); // Disable Shockburst
  330. nrf24_write_reg(handle, REG_FEATURE, 0x05); // disable payload-with-ack, enable noack
  331. nrf24_set_maclen(handle, 2); // shortest address
  332. nrf24_set_src_mac(handle, preamble, 2); // set src mac to preamble bits to catch everything
  333. nrf24_set_packetlen(handle, 32); // set max packet length
  334. nrf24_set_idle(handle);
  335. nrf24_flush_rx(handle);
  336. nrf24_flush_tx(handle);
  337. nrf24_write_reg(handle, REG_RF_CH, channel);
  338. nrf24_write_reg(handle, REG_RF_SETUP, rate);
  339. // prime for RX, no checksum
  340. nrf24_write_reg(handle, REG_CONFIG, 0x03); // PWR_UP and PRIM_RX, disable AA and CRC
  341. furi_hal_gpio_write(nrf24_CE_PIN, true);
  342. furi_delay_ms(100);
  343. }
  344. void hexlify(uint8_t* in, uint8_t size, char* out) {
  345. memset(out, 0, size * 2);
  346. for(int i = 0; i < size; i++)
  347. snprintf(out + strlen(out), sizeof(out + strlen(out)), "%02X", in[i]);
  348. }
  349. uint64_t bytes_to_int64(uint8_t* bytes, uint8_t size, bool bigendian) {
  350. uint64_t ret = 0;
  351. for(int i = 0; i < size; i++)
  352. if(bigendian)
  353. ret |= bytes[i] << ((size - 1 - i) * 8);
  354. else
  355. ret |= bytes[i] << (i * 8);
  356. return ret;
  357. }
  358. void int64_to_bytes(uint64_t val, uint8_t* out, bool bigendian) {
  359. for(int i = 0; i < 8; i++) {
  360. if(bigendian)
  361. out[i] = (val >> ((7 - i) * 8)) & 0xff;
  362. else
  363. out[i] = (val >> (i * 8)) & 0xff;
  364. }
  365. }
  366. uint32_t bytes_to_int32(uint8_t* bytes, bool bigendian) {
  367. uint32_t ret = 0;
  368. for(int i = 0; i < 4; i++)
  369. if(bigendian)
  370. ret |= bytes[i] << ((3 - i) * 8);
  371. else
  372. ret |= bytes[i] << (i * 8);
  373. return ret;
  374. }
  375. void int32_to_bytes(uint32_t val, uint8_t* out, bool bigendian) {
  376. for(int i = 0; i < 4; i++) {
  377. if(bigendian)
  378. out[i] = (val >> ((3 - i) * 8)) & 0xff;
  379. else
  380. out[i] = (val >> (i * 8)) & 0xff;
  381. }
  382. }
  383. uint64_t bytes_to_int16(uint8_t* bytes, bool bigendian) {
  384. uint16_t ret = 0;
  385. for(int i = 0; i < 2; i++)
  386. if(bigendian)
  387. ret |= bytes[i] << ((1 - i) * 8);
  388. else
  389. ret |= bytes[i] << (i * 8);
  390. return ret;
  391. }
  392. void int16_to_bytes(uint16_t val, uint8_t* out, bool bigendian) {
  393. for(int i = 0; i < 2; i++) {
  394. if(bigendian)
  395. out[i] = (val >> ((1 - i) * 8)) & 0xff;
  396. else
  397. out[i] = (val >> (i * 8)) & 0xff;
  398. }
  399. }
  400. // handle iffyness with preamble processing sometimes being a bit (literally) off
  401. void alt_address_old(uint8_t* packet, uint8_t* altaddr) {
  402. uint8_t macmess_hi_b[4];
  403. uint8_t macmess_lo_b[2];
  404. uint32_t macmess_hi;
  405. uint16_t macmess_lo;
  406. uint8_t preserved;
  407. // get first 6 bytes into 32-bit and 16-bit variables
  408. memcpy(macmess_hi_b, packet, 4);
  409. memcpy(macmess_lo_b, packet + 4, 2);
  410. macmess_hi = bytes_to_int32(macmess_hi_b, true);
  411. //preserve least 7 bits from hi that will be shifted down to lo
  412. preserved = macmess_hi & 0x7f;
  413. macmess_hi >>= 7;
  414. macmess_lo = bytes_to_int16(macmess_lo_b, true);
  415. macmess_lo >>= 7;
  416. macmess_lo = (preserved << 9) | macmess_lo;
  417. int32_to_bytes(macmess_hi, macmess_hi_b, true);
  418. int16_to_bytes(macmess_lo, macmess_lo_b, true);
  419. memcpy(altaddr, &macmess_hi_b[1], 3);
  420. memcpy(altaddr + 3, macmess_lo_b, 2);
  421. }
  422. bool validate_address(uint8_t* addr) {
  423. uint8_t bad[][3] = {{0x55, 0x55}, {0xAA, 0xAA}, {0x00, 0x00}, {0xFF, 0xFF}};
  424. for(int i = 0; i < 4; i++)
  425. for(int j = 0; j < 2; j++)
  426. if(!memcmp(addr + j * 2, bad[i], 2)) return false;
  427. return true;
  428. }
  429. bool nrf24_sniff_address(const FuriHalSpiBusHandle* handle, uint8_t maclen, uint8_t* address) {
  430. bool found = false;
  431. uint8_t packet[32] = {0};
  432. uint8_t packetsize;
  433. //char printit[65];
  434. uint8_t status = 0;
  435. status = nrf24_rxpacket(handle, packet, &packetsize, true);
  436. if(status & 0x40) {
  437. if(validate_address(packet)) {
  438. for(int i = 0; i < maclen; i++)
  439. address[i] = packet[maclen - 1 - i];
  440. /*
  441. alt_address(packet, packet);
  442. for(i = 0; i < maclen; i++)
  443. address[i + 5] = packet[maclen - 1 - i];
  444. */
  445. //memcpy(address, packet, maclen);
  446. //hexlify(packet, packetsize, printit);
  447. found = true;
  448. }
  449. }
  450. return found;
  451. }
  452. uint8_t nrf24_find_channel(
  453. const FuriHalSpiBusHandle* handle,
  454. uint8_t* srcmac,
  455. uint8_t* dstmac,
  456. uint8_t maclen,
  457. uint8_t rate,
  458. uint8_t min_channel,
  459. uint8_t max_channel,
  460. bool autoinit) {
  461. uint8_t ping_packet[] = {0x0f, 0x0f, 0x0f, 0x0f}; // this can be anything, we just need an ack
  462. uint8_t ch = max_channel + 1; // means fail
  463. nrf24_configure(handle, rate, srcmac, dstmac, maclen, 2, false, false);
  464. for(ch = min_channel; ch <= max_channel + 1; ch++) {
  465. nrf24_write_reg(handle, REG_RF_CH, ch);
  466. if(nrf24_txpacket(handle, ping_packet, 4, true)) break;
  467. }
  468. if(autoinit) {
  469. FURI_LOG_D("nrf24", "initializing radio for channel %d", ch);
  470. nrf24_configure(handle, rate, srcmac, dstmac, maclen, ch, false, false);
  471. return ch;
  472. }
  473. return ch;
  474. }
  475. uint8_t nrf24_set_mac(uint8_t mac_addr, uint8_t* mac, uint8_t mlen) {
  476. uint8_t addr[5];
  477. for(int i = 0; i < mlen; i++)
  478. addr[i] = mac[mlen - i - 1];
  479. return nrf24_write_buf_reg(nrf24_HANDLE, mac_addr, addr, mlen);
  480. }