nrf24.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. #include "nrf24.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <furi_hal_resources.h>
  5. #include <assert.h>
  6. #include <string.h>
  7. void nrf24_init() {
  8. // this is needed if multiple SPI devices are connected to the same bus but with different CS pins
  9. if(momentum_settings.spi_nrf24_handle == SpiDefault) {
  10. furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeOutputPushPull);
  11. furi_hal_gpio_write(&gpio_ext_pc3, true);
  12. } else if(momentum_settings.spi_nrf24_handle == SpiExtra) {
  13. furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeOutputPushPull);
  14. furi_hal_gpio_write(&gpio_ext_pa4, true);
  15. }
  16. furi_hal_spi_bus_handle_init(nrf24_HANDLE);
  17. furi_hal_spi_acquire(nrf24_HANDLE);
  18. furi_hal_gpio_init(nrf24_CE_PIN, GpioModeOutputPushPull, GpioPullUp, GpioSpeedVeryHigh);
  19. furi_hal_gpio_write(nrf24_CE_PIN, false);
  20. }
  21. void nrf24_deinit() {
  22. furi_hal_spi_release(nrf24_HANDLE);
  23. furi_hal_spi_bus_handle_deinit(nrf24_HANDLE);
  24. furi_hal_gpio_write(nrf24_CE_PIN, false);
  25. furi_hal_gpio_init(nrf24_CE_PIN, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  26. // resetting the CS pins to floating
  27. if(momentum_settings.spi_nrf24_handle == SpiDefault) {
  28. furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeAnalog);
  29. } else if(momentum_settings.spi_nrf24_handle == SpiExtra) {
  30. furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeAnalog);
  31. }
  32. }
  33. void nrf24_spi_trx(
  34. const FuriHalSpiBusHandle* handle,
  35. uint8_t* tx,
  36. uint8_t* rx,
  37. uint8_t size,
  38. uint32_t timeout) {
  39. UNUSED(timeout);
  40. furi_hal_gpio_write(handle->cs, false);
  41. furi_hal_spi_bus_trx(handle, tx, rx, size, nrf24_TIMEOUT);
  42. furi_hal_gpio_write(handle->cs, true);
  43. }
  44. uint8_t nrf24_write_reg(const FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t data) {
  45. uint8_t tx[2] = {W_REGISTER | (REGISTER_MASK & reg), data};
  46. uint8_t rx[2] = {0};
  47. nrf24_spi_trx(handle, tx, rx, 2, nrf24_TIMEOUT);
  48. return rx[0];
  49. }
  50. uint8_t nrf24_write_buf_reg(
  51. const FuriHalSpiBusHandle* handle,
  52. uint8_t reg,
  53. uint8_t* data,
  54. 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. return rx[0];
  62. }
  63. uint8_t
  64. nrf24_read_reg(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const FuriHalSpiBusHandle* handle) {
  173. uint8_t len = 0;
  174. nrf24_read_reg(handle, RX_PW_P0, &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(
  183. const FuriHalSpiBusHandle* handle,
  184. uint8_t* packet,
  185. uint8_t* packetsize,
  186. bool full) {
  187. uint8_t status = 0;
  188. uint8_t size = 0;
  189. uint8_t tx_pl_wid[] = {R_RX_PL_WID, 0};
  190. uint8_t rx_pl_wid[] = {0, 0};
  191. uint8_t tx_cmd[33] = {0}; // 32 max payload size + 1 for command
  192. uint8_t tmp_packet[33] = {0};
  193. status = nrf24_status(handle);
  194. if(status & 0x40) {
  195. if(full)
  196. size = nrf24_get_packetlen(handle);
  197. else {
  198. nrf24_spi_trx(handle, tx_pl_wid, rx_pl_wid, 2, nrf24_TIMEOUT);
  199. size = rx_pl_wid[1];
  200. }
  201. tx_cmd[0] = R_RX_PAYLOAD;
  202. nrf24_spi_trx(handle, tx_cmd, tmp_packet, size + 1, nrf24_TIMEOUT);
  203. nrf24_write_reg(handle, REG_STATUS, 0x40); // clear bit.
  204. memcpy(packet, &tmp_packet[1], size);
  205. } else if(status == 0) {
  206. nrf24_flush_rx(handle);
  207. nrf24_write_reg(handle, REG_STATUS, 0x40); // clear bit.
  208. }
  209. *packetsize = size;
  210. return status;
  211. }
  212. uint8_t
  213. nrf24_txpacket(const FuriHalSpiBusHandle* handle, uint8_t* payload, uint8_t size, bool ack) {
  214. uint8_t status = 0;
  215. uint8_t tx[size + 1];
  216. uint8_t rx[size + 1];
  217. memset(tx, 0, size + 1);
  218. memset(rx, 0, size + 1);
  219. if(!ack)
  220. tx[0] = W_TX_PAYLOAD_NOACK;
  221. else
  222. tx[0] = W_TX_PAYLOAD;
  223. memcpy(&tx[1], payload, size);
  224. nrf24_spi_trx(handle, tx, rx, size + 1, nrf24_TIMEOUT);
  225. nrf24_set_tx_mode(handle);
  226. while(!(status & (TX_DS | MAX_RT)))
  227. status = nrf24_status(handle);
  228. if(status & MAX_RT) nrf24_flush_tx(handle);
  229. nrf24_set_idle(handle);
  230. nrf24_write_reg(handle, REG_STATUS, TX_DS | MAX_RT);
  231. return status & TX_DS;
  232. }
  233. uint8_t nrf24_power_up(const FuriHalSpiBusHandle* handle) {
  234. uint8_t status = 0;
  235. uint8_t cfg = 0;
  236. nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
  237. cfg = cfg | 2;
  238. status = nrf24_write_reg(handle, REG_CONFIG, cfg);
  239. furi_delay_ms(5000);
  240. return status;
  241. }
  242. uint8_t nrf24_set_idle(const FuriHalSpiBusHandle* handle) {
  243. uint8_t status = 0;
  244. uint8_t cfg = 0;
  245. nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
  246. cfg &= 0xfc; // clear bottom two bits to power down the radio
  247. status = nrf24_write_reg(handle, REG_CONFIG, cfg);
  248. //nr204_write_reg(handle, REG_EN_RXADDR, 0x0);
  249. furi_hal_gpio_write(nrf24_CE_PIN, false);
  250. return status;
  251. }
  252. uint8_t nrf24_set_rx_mode(const FuriHalSpiBusHandle* handle) {
  253. uint8_t status = 0;
  254. uint8_t cfg = 0;
  255. //status = nrf24_write_reg(handle, REG_CONFIG, 0x0F); // enable 2-byte CRC, PWR_UP, and PRIM_RX
  256. nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
  257. cfg |= 0x03; // PWR_UP, and PRIM_RX
  258. status = nrf24_write_reg(handle, REG_CONFIG, cfg);
  259. //nr204_write_reg(REG_EN_RXADDR, 0x03) // Set RX Pipe 0 and 1
  260. furi_hal_gpio_write(nrf24_CE_PIN, true);
  261. furi_delay_ms(2000);
  262. return status;
  263. }
  264. uint8_t nrf24_set_tx_mode(const FuriHalSpiBusHandle* handle) {
  265. uint8_t status = 0;
  266. uint8_t cfg = 0;
  267. furi_hal_gpio_write(nrf24_CE_PIN, false);
  268. nrf24_write_reg(handle, REG_STATUS, 0x30);
  269. //status = nrf24_write_reg(handle, REG_CONFIG, 0x0E); // enable 2-byte CRC, PWR_UP
  270. nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
  271. cfg &= 0xfe; // disable PRIM_RX
  272. cfg |= 0x02; // PWR_UP
  273. status = nrf24_write_reg(handle, REG_CONFIG, cfg);
  274. furi_hal_gpio_write(nrf24_CE_PIN, true);
  275. furi_delay_ms(2);
  276. return status;
  277. }
  278. void nrf24_configure(
  279. const FuriHalSpiBusHandle* handle,
  280. uint8_t rate,
  281. uint8_t* srcmac,
  282. uint8_t* dstmac,
  283. uint8_t maclen,
  284. uint8_t channel,
  285. bool noack,
  286. bool disable_aa) {
  287. assert(channel <= 125);
  288. assert(rate == 1 || rate == 2);
  289. if(rate == 2)
  290. rate = 8; // 2Mbps
  291. else
  292. rate = 0; // 1Mbps
  293. nrf24_write_reg(handle, REG_CONFIG, 0x00); // Stop nRF
  294. nrf24_set_idle(handle);
  295. nrf24_write_reg(handle, REG_STATUS, 0x1c); // clear interrupts
  296. if(disable_aa)
  297. nrf24_write_reg(handle, REG_EN_AA, 0x00); // Disable Shockburst
  298. else
  299. nrf24_write_reg(handle, REG_EN_AA, 0x1F); // Enable Shockburst
  300. nrf24_write_reg(handle, REG_DYNPD, 0x3F); // enable dynamic payload length on all pipes
  301. if(noack)
  302. nrf24_write_reg(handle, REG_FEATURE, 0x05); // disable payload-with-ack, enable noack
  303. else {
  304. nrf24_write_reg(handle, REG_CONFIG, 0x0C); // 2 byte CRC
  305. nrf24_write_reg(handle, REG_FEATURE, 0x07); // enable dyn payload and ack
  306. nrf24_write_reg(
  307. handle, REG_SETUP_RETR, 0x1f); // 15 retries for AA, 500us auto retransmit delay
  308. }
  309. nrf24_set_idle(handle);
  310. nrf24_flush_rx(handle);
  311. nrf24_flush_tx(handle);
  312. if(maclen) nrf24_set_maclen(handle, maclen);
  313. if(srcmac) nrf24_set_src_mac(handle, srcmac, maclen);
  314. if(dstmac) nrf24_set_dst_mac(handle, dstmac, maclen);
  315. nrf24_write_reg(handle, REG_RF_CH, channel);
  316. nrf24_write_reg(handle, REG_RF_SETUP, rate);
  317. furi_delay_ms(200);
  318. }
  319. void nrf24_init_promisc_mode(const FuriHalSpiBusHandle* handle, uint8_t channel, uint8_t rate) {
  320. //uint8_t preamble[] = {0x55, 0x00}; // little endian
  321. uint8_t preamble[] = {0xAA, 0x00}; // little endian
  322. //uint8_t preamble[] = {0x00, 0x55}; // little endian
  323. //uint8_t preamble[] = {0x00, 0xAA}; // little endian
  324. nrf24_write_reg(handle, REG_CONFIG, 0x00); // Stop nRF
  325. nrf24_write_reg(handle, REG_STATUS, 0x1c); // clear interrupts
  326. nrf24_write_reg(handle, REG_DYNPD, 0x0); // disable shockburst
  327. nrf24_write_reg(handle, REG_EN_AA, 0x00); // Disable Shockburst
  328. nrf24_write_reg(handle, REG_FEATURE, 0x05); // disable payload-with-ack, enable noack
  329. nrf24_set_maclen(handle, 2); // shortest address
  330. nrf24_set_src_mac(handle, preamble, 2); // set src mac to preamble bits to catch everything
  331. nrf24_set_packetlen(handle, 32); // set max packet length
  332. nrf24_set_idle(handle);
  333. nrf24_flush_rx(handle);
  334. nrf24_flush_tx(handle);
  335. nrf24_write_reg(handle, REG_RF_CH, channel);
  336. nrf24_write_reg(handle, REG_RF_SETUP, rate);
  337. // prime for RX, no checksum
  338. nrf24_write_reg(handle, REG_CONFIG, 0x03); // PWR_UP and PRIM_RX, disable AA and CRC
  339. furi_hal_gpio_write(nrf24_CE_PIN, true);
  340. furi_delay_ms(100);
  341. }
  342. void hexlify(uint8_t* in, uint8_t size, char* out) {
  343. memset(out, 0, size * 2);
  344. for(int i = 0; i < size; i++)
  345. snprintf(out + strlen(out), sizeof(out + strlen(out)), "%02X", in[i]);
  346. }
  347. uint64_t bytes_to_int64(uint8_t* bytes, uint8_t size, bool bigendian) {
  348. uint64_t ret = 0;
  349. for(int i = 0; i < size; i++)
  350. if(bigendian)
  351. ret |= bytes[i] << ((size - 1 - i) * 8);
  352. else
  353. ret |= bytes[i] << (i * 8);
  354. return ret;
  355. }
  356. void int64_to_bytes(uint64_t val, uint8_t* out, bool bigendian) {
  357. for(int i = 0; i < 8; i++) {
  358. if(bigendian)
  359. out[i] = (val >> ((7 - i) * 8)) & 0xff;
  360. else
  361. out[i] = (val >> (i * 8)) & 0xff;
  362. }
  363. }
  364. uint32_t bytes_to_int32(uint8_t* bytes, bool bigendian) {
  365. uint32_t ret = 0;
  366. for(int i = 0; i < 4; i++)
  367. if(bigendian)
  368. ret |= bytes[i] << ((3 - i) * 8);
  369. else
  370. ret |= bytes[i] << (i * 8);
  371. return ret;
  372. }
  373. void int32_to_bytes(uint32_t val, uint8_t* out, bool bigendian) {
  374. for(int i = 0; i < 4; i++) {
  375. if(bigendian)
  376. out[i] = (val >> ((3 - i) * 8)) & 0xff;
  377. else
  378. out[i] = (val >> (i * 8)) & 0xff;
  379. }
  380. }
  381. uint64_t bytes_to_int16(uint8_t* bytes, bool bigendian) {
  382. uint16_t ret = 0;
  383. for(int i = 0; i < 2; i++)
  384. if(bigendian)
  385. ret |= bytes[i] << ((1 - i) * 8);
  386. else
  387. ret |= bytes[i] << (i * 8);
  388. return ret;
  389. }
  390. void int16_to_bytes(uint16_t val, uint8_t* out, bool bigendian) {
  391. for(int i = 0; i < 2; i++) {
  392. if(bigendian)
  393. out[i] = (val >> ((1 - i) * 8)) & 0xff;
  394. else
  395. out[i] = (val >> (i * 8)) & 0xff;
  396. }
  397. }
  398. // handle iffyness with preamble processing sometimes being a bit (literally) off
  399. void alt_address_old(uint8_t* packet, uint8_t* altaddr) {
  400. uint8_t macmess_hi_b[4];
  401. uint8_t macmess_lo_b[2];
  402. uint32_t macmess_hi;
  403. uint16_t macmess_lo;
  404. uint8_t preserved;
  405. // get first 6 bytes into 32-bit and 16-bit variables
  406. memcpy(macmess_hi_b, packet, 4);
  407. memcpy(macmess_lo_b, packet + 4, 2);
  408. macmess_hi = bytes_to_int32(macmess_hi_b, true);
  409. //preserve least 7 bits from hi that will be shifted down to lo
  410. preserved = macmess_hi & 0x7f;
  411. macmess_hi >>= 7;
  412. macmess_lo = bytes_to_int16(macmess_lo_b, true);
  413. macmess_lo >>= 7;
  414. macmess_lo = (preserved << 9) | macmess_lo;
  415. int32_to_bytes(macmess_hi, macmess_hi_b, true);
  416. int16_to_bytes(macmess_lo, macmess_lo_b, true);
  417. memcpy(altaddr, &macmess_hi_b[1], 3);
  418. memcpy(altaddr + 3, macmess_lo_b, 2);
  419. }
  420. bool validate_address(uint8_t* addr) {
  421. uint8_t bad[][3] = {{0x55, 0x55}, {0xAA, 0xAA}, {0x00, 0x00}, {0xFF, 0xFF}};
  422. for(int i = 0; i < 4; i++)
  423. for(int j = 0; j < 2; j++)
  424. if(!memcmp(addr + j * 2, bad[i], 2)) return false;
  425. return true;
  426. }
  427. bool nrf24_sniff_address(const FuriHalSpiBusHandle* handle, uint8_t maclen, uint8_t* address) {
  428. bool found = false;
  429. uint8_t packet[32] = {0};
  430. uint8_t packetsize;
  431. //char printit[65];
  432. uint8_t status = 0;
  433. status = nrf24_rxpacket(handle, packet, &packetsize, true);
  434. if(status & 0x40) {
  435. if(validate_address(packet)) {
  436. for(int i = 0; i < maclen; i++)
  437. address[i] = packet[maclen - 1 - i];
  438. /*
  439. alt_address(packet, packet);
  440. for(i = 0; i < maclen; i++)
  441. address[i + 5] = packet[maclen - 1 - i];
  442. */
  443. //memcpy(address, packet, maclen);
  444. //hexlify(packet, packetsize, printit);
  445. found = true;
  446. }
  447. }
  448. return found;
  449. }
  450. uint8_t nrf24_find_channel(
  451. const FuriHalSpiBusHandle* handle,
  452. uint8_t* srcmac,
  453. uint8_t* dstmac,
  454. uint8_t maclen,
  455. uint8_t rate,
  456. uint8_t min_channel,
  457. uint8_t max_channel,
  458. bool autoinit) {
  459. uint8_t ping_packet[] = {0x0f, 0x0f, 0x0f, 0x0f}; // this can be anything, we just need an ack
  460. uint8_t ch = max_channel + 1; // means fail
  461. nrf24_configure(handle, rate, srcmac, dstmac, maclen, 2, false, false);
  462. for(ch = min_channel; ch <= max_channel + 1; ch++) {
  463. nrf24_write_reg(handle, REG_RF_CH, ch);
  464. if(nrf24_txpacket(handle, ping_packet, 4, true)) break;
  465. }
  466. if(autoinit) {
  467. FURI_LOG_D("nrf24", "initializing radio for channel %d", ch);
  468. nrf24_configure(handle, rate, srcmac, dstmac, maclen, ch, false, false);
  469. return ch;
  470. }
  471. return ch;
  472. }
  473. bool nrf24_check_connected(const FuriHalSpiBusHandle* handle) {
  474. uint8_t status = nrf24_status(handle);
  475. if(status != 0x00) {
  476. return true;
  477. } else {
  478. return false;
  479. }
  480. }