nrf24.c 17 KB

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