nrf24.c 16 KB

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