nrf24.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // Modified by vad7, 24.02.2023
  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. // this is needed if multiple SPI devices are connected to the same bus but with different CS pins
  11. if(xtreme_settings.spi_nrf24_handle == SpiDefault) {
  12. furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeOutputPushPull);
  13. furi_hal_gpio_write(&gpio_ext_pc3, true);
  14. } else if(xtreme_settings.spi_nrf24_handle == SpiExtra) {
  15. furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeOutputPushPull);
  16. furi_hal_gpio_write(&gpio_ext_pa4, true);
  17. }
  18. furi_hal_spi_bus_handle_init(nrf24_HANDLE);
  19. furi_hal_spi_acquire(nrf24_HANDLE);
  20. furi_hal_gpio_init(nrf24_CE_PIN, GpioModeOutputPushPull, GpioPullUp, GpioSpeedVeryHigh);
  21. furi_hal_gpio_write(nrf24_CE_PIN, false);
  22. }
  23. void nrf24_deinit() {
  24. furi_hal_spi_release(nrf24_HANDLE);
  25. furi_hal_spi_bus_handle_deinit(nrf24_HANDLE);
  26. furi_hal_gpio_write(nrf24_CE_PIN, false);
  27. furi_hal_gpio_init(nrf24_CE_PIN, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  28. // resetting the CS pins to floating
  29. if(xtreme_settings.spi_nrf24_handle == SpiDefault) {
  30. furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeAnalog);
  31. } else if(xtreme_settings.spi_nrf24_handle == SpiExtra) {
  32. furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeAnalog);
  33. }
  34. }
  35. void nrf24_spi_trx(
  36. FuriHalSpiBusHandle* handle,
  37. uint8_t* tx,
  38. uint8_t* rx,
  39. uint8_t size) {
  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 buf[] = {W_REGISTER | (REGISTER_MASK & reg), data};
  46. nrf24_spi_trx(handle, buf, buf, 2);
  47. //FURI_LOG_D("NRF_WR", " #%02X=%02X", reg, data);
  48. return buf[0];
  49. }
  50. uint8_t nrf24_write_buf_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* data, uint8_t size) {
  51. uint8_t buf[size + 1];
  52. buf[0] = W_REGISTER | (REGISTER_MASK & reg);
  53. memcpy(&buf[1], data, size);
  54. nrf24_spi_trx(handle, buf, buf, size + 1);
  55. //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] );
  56. return buf[0];
  57. }
  58. uint8_t nrf24_read_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* data, uint8_t size) {
  59. uint8_t buf[size + 1];
  60. memset(buf, 0, size + 1);
  61. buf[0] = R_REGISTER | (REGISTER_MASK & reg);
  62. nrf24_spi_trx(handle, buf, buf, size + 1);
  63. memcpy(data, &buf[1], size);
  64. return buf[0];
  65. }
  66. uint8_t nrf24_read_register(FuriHalSpiBusHandle* handle, uint8_t reg) {
  67. uint8_t buf[] = { R_REGISTER | (REGISTER_MASK & reg), 0 };
  68. nrf24_spi_trx(handle, buf, buf, 2);
  69. return buf[1];
  70. }
  71. uint8_t nrf24_flush_rx(FuriHalSpiBusHandle* handle) {
  72. uint8_t tx[] = {FLUSH_RX};
  73. uint8_t rx[] = {0};
  74. nrf24_spi_trx(handle, tx, rx, 1);
  75. return rx[0];
  76. }
  77. uint8_t nrf24_flush_tx(FuriHalSpiBusHandle* handle) {
  78. uint8_t tx[] = {FLUSH_TX};
  79. uint8_t rx[] = {0};
  80. nrf24_spi_trx(handle, tx, rx, 1);
  81. return rx[0];
  82. }
  83. uint8_t nrf24_get_maclen(FuriHalSpiBusHandle* handle) {
  84. uint8_t maclen;
  85. nrf24_read_reg(handle, REG_SETUP_AW, &maclen, 1);
  86. maclen &= 3;
  87. return maclen + 2;
  88. }
  89. uint8_t nrf24_set_maclen(FuriHalSpiBusHandle* handle, uint8_t maclen) {
  90. assert(maclen > 1 && maclen < 6);
  91. uint8_t status = 0;
  92. status = nrf24_write_reg(handle, REG_SETUP_AW, maclen - 2);
  93. return status;
  94. }
  95. uint8_t nrf24_status(FuriHalSpiBusHandle* handle) {
  96. uint8_t tx = RF24_NOP;
  97. nrf24_spi_trx(handle, &tx, &tx, 1);
  98. return tx;
  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, uint8_t pipe) {
  169. uint8_t len = 0;
  170. if(pipe > 5) pipe = 0;
  171. nrf24_read_reg(handle, RX_PW_P0 + pipe, &len, 1);
  172. return len;
  173. }
  174. uint8_t nrf24_set_packetlen(FuriHalSpiBusHandle* handle, uint8_t len) {
  175. uint8_t status = 0;
  176. status = nrf24_write_reg(handle, RX_PW_P0, len);
  177. return status;
  178. }
  179. // packet_size: 0 - dyn payload (read from PL_WID), 1 - read from pipe size, >1 - override
  180. // Return STATUS reg + additional: RX_DR - new data available, 0x80 - NRF24 hardware error
  181. uint8_t nrf24_rxpacket(FuriHalSpiBusHandle* handle, uint8_t* packet, uint8_t* ret_packetsize, uint8_t packet_size) {
  182. uint8_t status = 0;
  183. uint8_t buf[33]; // 32 max payload size + 1 for command
  184. status = nrf24_status(handle);
  185. if(!(status & RX_DR)) {
  186. uint8_t st = nrf24_read_register(handle, REG_FIFO_STATUS);
  187. if(st == 0xFF || st == 0) return 0x80; // hardware error
  188. if((st & 1) == 0) {
  189. FURI_LOG_D("NRF", "FIFO PKT");
  190. status |= RX_DR; // packet in FIFO buffer
  191. }
  192. }
  193. if(status & RX_DR) {
  194. if(status & 0x80) return 0x80; // hardware error
  195. if(packet_size == 1)
  196. packet_size = nrf24_get_packetlen(handle, (status >> 1) & 7);
  197. else if(packet_size == 0){
  198. buf[0] = R_RX_PL_WID; buf[1] = 0xFF;
  199. nrf24_spi_trx(handle, buf, buf, 2);
  200. packet_size = buf[1];
  201. }
  202. if(packet_size > 32 || packet_size == 0) packet_size = 32;
  203. memset(buf, 0, packet_size + 1);
  204. buf[0] = R_RX_PAYLOAD;
  205. nrf24_spi_trx(handle, buf, buf, packet_size + 1);
  206. memcpy(packet, &buf[1], packet_size);
  207. nrf24_write_reg(handle, REG_STATUS, RX_DR); // clear RX_DR
  208. }
  209. if(status & (MAX_RT)) { // MAX_RT
  210. nrf24_write_reg(handle, REG_STATUS, (MAX_RT)); // clear MAX_RT.
  211. }
  212. *ret_packetsize = packet_size;
  213. return status;
  214. }
  215. // Return 0 when error
  216. uint8_t nrf24_txpacket(FuriHalSpiBusHandle* handle, uint8_t* payload, uint8_t size, bool ack) {
  217. uint8_t status = 0;
  218. uint8_t buf[size + 1];
  219. buf[0] = ack ? W_TX_PAYLOAD : W_TX_PAYLOAD_NOACK;
  220. memcpy(&buf[1], payload, size);
  221. nrf24_set_tx_mode(handle);
  222. nrf24_spi_trx(handle, buf, buf, size + 1);
  223. uint32_t start_time = furi_get_tick();
  224. do {
  225. furi_delay_us(100);
  226. status = nrf24_status(handle);
  227. } while(!(status & (TX_DS | MAX_RT)) && furi_get_tick() - start_time < 100UL);
  228. if(status & MAX_RT) {
  229. if(furi_log_get_level() == FuriLogLevelDebug) FURI_LOG_D("NRF", "MAX RT: %X (%X)", nrf24_read_register(handle, REG_OBSERVE_TX), status);
  230. nrf24_flush_tx(handle);
  231. }
  232. furi_hal_gpio_write(nrf24_CE_PIN, false);
  233. //nrf24_set_idle(handle);
  234. if(status & (TX_DS | MAX_RT)) nrf24_write_reg(handle, REG_STATUS, TX_DS | MAX_RT);
  235. return status & TX_DS;
  236. }
  237. uint8_t nrf24_power_up(FuriHalSpiBusHandle* handle) {
  238. uint8_t status = 0;
  239. uint8_t cfg = 0;
  240. nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
  241. cfg = cfg | 2;
  242. status = nrf24_write_reg(handle, REG_CONFIG, cfg);
  243. //furi_delay_ms(1000);
  244. return status;
  245. }
  246. uint8_t nrf24_set_idle(FuriHalSpiBusHandle* handle) {
  247. uint8_t status = 0;
  248. uint8_t cfg = 0;
  249. nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
  250. cfg &= 0xfc; // clear bottom two bits to power down the radio
  251. status = nrf24_write_reg(handle, REG_CONFIG, cfg);
  252. furi_hal_gpio_write(nrf24_CE_PIN, false);
  253. return status;
  254. }
  255. uint8_t nrf24_set_rx_mode(FuriHalSpiBusHandle* handle) {
  256. uint8_t cfg = 0;
  257. cfg = nrf24_read_register(handle, REG_CONFIG);
  258. cfg |= 0x03; // PWR_UP, and PRIM_RX
  259. cfg = nrf24_write_reg(handle, REG_CONFIG, cfg);
  260. furi_hal_gpio_write(nrf24_CE_PIN, true);
  261. return cfg;
  262. }
  263. uint8_t nrf24_set_tx_mode(FuriHalSpiBusHandle* handle) {
  264. uint8_t reg;
  265. furi_hal_gpio_write(nrf24_CE_PIN, false);
  266. //nrf24_write_reg(handle, REG_STATUS, TX_DS | MAX_RT);
  267. reg = nrf24_read_register(handle, REG_CONFIG);
  268. reg &= ~0x01; // disable PRIM_RX
  269. reg |= 0x02; // PWR_UP
  270. reg = nrf24_write_reg(handle, REG_CONFIG, reg);
  271. furi_hal_gpio_write(nrf24_CE_PIN, true);
  272. return reg;
  273. }
  274. void hexlify(uint8_t* in, uint8_t size, char* out) {
  275. memset(out, 0, size * 2);
  276. for(int i = 0; i < size; i++)
  277. snprintf(out + strlen(out), sizeof(out + strlen(out)), "%02X", in[i]);
  278. }
  279. uint64_t bytes_to_int64(uint8_t* bytes, uint8_t size, bool bigendian) {
  280. uint64_t ret = 0;
  281. for(int i = 0; i < size; i++)
  282. if(bigendian)
  283. ret |= bytes[i] << ((size - 1 - i) * 8);
  284. else
  285. ret |= bytes[i] << (i * 8);
  286. return ret;
  287. }
  288. void int64_to_bytes(uint64_t val, uint8_t* out, bool bigendian) {
  289. for(int i = 0; i < 8; i++) {
  290. if(bigendian)
  291. out[i] = (val >> ((7 - i) * 8)) & 0xff;
  292. else
  293. out[i] = (val >> (i * 8)) & 0xff;
  294. }
  295. }
  296. uint32_t bytes_to_int32(uint8_t* bytes, bool bigendian) {
  297. uint32_t ret = 0;
  298. for(int i = 0; i < 4; i++)
  299. if(bigendian)
  300. ret |= bytes[i] << ((3 - i) * 8);
  301. else
  302. ret |= bytes[i] << (i * 8);
  303. return ret;
  304. }
  305. void int32_to_bytes(uint32_t val, uint8_t* out, bool bigendian) {
  306. for(int i = 0; i < 4; i++) {
  307. if(bigendian)
  308. out[i] = (val >> ((3 - i) * 8)) & 0xff;
  309. else
  310. out[i] = (val >> (i * 8)) & 0xff;
  311. }
  312. }
  313. uint64_t bytes_to_int16(uint8_t* bytes, bool bigendian) {
  314. uint16_t ret = 0;
  315. for(int i = 0; i < 2; i++)
  316. if(bigendian)
  317. ret |= bytes[i] << ((1 - i) * 8);
  318. else
  319. ret |= bytes[i] << (i * 8);
  320. return ret;
  321. }
  322. void int16_to_bytes(uint16_t val, uint8_t* out, bool bigendian) {
  323. for(int i = 0; i < 2; i++) {
  324. if(bigendian)
  325. out[i] = (val >> ((1 - i) * 8)) & 0xff;
  326. else
  327. out[i] = (val >> (i * 8)) & 0xff;
  328. }
  329. }
  330. uint8_t nrf24_set_mac(uint8_t mac_addr, uint8_t *mac, uint8_t mlen)
  331. {
  332. uint8_t addr[5];
  333. for(int i = 0; i < mlen; i++) addr[i] = mac[mlen - i - 1];
  334. return nrf24_write_buf_reg(nrf24_HANDLE, mac_addr, addr, mlen);
  335. }