nrf24.c 16 KB

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