nrf24.c 18 KB

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