nrf24.c 18 KB

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