nrf24.c 17 KB

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