nrf24.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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(momentum_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(momentum_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(momentum_settings.spi_nrf24_handle == SpiDefault) {
  30. furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeAnalog);
  31. } else if(momentum_settings.spi_nrf24_handle == SpiExtra) {
  32. furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeAnalog);
  33. }
  34. }
  35. void nrf24_spi_trx(FuriHalSpiBusHandle* handle, uint8_t* tx, uint8_t* rx, uint8_t size) {
  36. furi_hal_gpio_write(handle->cs, false);
  37. furi_hal_spi_bus_trx(handle, tx, rx, size, nrf24_TIMEOUT);
  38. furi_hal_gpio_write(handle->cs, true);
  39. }
  40. uint8_t nrf24_write_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t data) {
  41. uint8_t buf[] = {W_REGISTER | (REGISTER_MASK & reg), data};
  42. nrf24_spi_trx(handle, buf, buf, 2);
  43. //FURI_LOG_D("NRF_WR", " #%02X=%02X", reg, data);
  44. return buf[0];
  45. }
  46. uint8_t
  47. nrf24_write_buf_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* data, uint8_t size) {
  48. uint8_t buf[size + 1];
  49. buf[0] = W_REGISTER | (REGISTER_MASK & reg);
  50. memcpy(&buf[1], data, size);
  51. nrf24_spi_trx(handle, buf, buf, size + 1);
  52. //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] );
  53. return buf[0];
  54. }
  55. uint8_t nrf24_read_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* data, uint8_t size) {
  56. uint8_t buf[size + 1];
  57. memset(buf, 0, size + 1);
  58. buf[0] = R_REGISTER | (REGISTER_MASK & reg);
  59. nrf24_spi_trx(handle, buf, buf, size + 1);
  60. memcpy(data, &buf[1], size);
  61. return buf[0];
  62. }
  63. uint8_t nrf24_read_register(FuriHalSpiBusHandle* handle, uint8_t reg) {
  64. uint8_t buf[] = {R_REGISTER | (REGISTER_MASK & reg), 0};
  65. nrf24_spi_trx(handle, buf, buf, 2);
  66. return buf[1];
  67. }
  68. uint8_t nrf24_flush_rx(FuriHalSpiBusHandle* handle) {
  69. uint8_t tx[] = {FLUSH_RX};
  70. uint8_t rx[] = {0};
  71. nrf24_spi_trx(handle, tx, rx, 1);
  72. return rx[0];
  73. }
  74. uint8_t nrf24_flush_tx(FuriHalSpiBusHandle* handle) {
  75. uint8_t tx[] = {FLUSH_TX};
  76. uint8_t rx[] = {0};
  77. nrf24_spi_trx(handle, tx, rx, 1);
  78. return rx[0];
  79. }
  80. uint8_t nrf24_get_maclen(FuriHalSpiBusHandle* handle) {
  81. uint8_t maclen;
  82. nrf24_read_reg(handle, REG_SETUP_AW, &maclen, 1);
  83. maclen &= 3;
  84. return maclen + 2;
  85. }
  86. uint8_t nrf24_set_maclen(FuriHalSpiBusHandle* handle, uint8_t maclen) {
  87. assert(maclen > 1 && maclen < 6);
  88. uint8_t status = 0;
  89. status = nrf24_write_reg(handle, REG_SETUP_AW, maclen - 2);
  90. return status;
  91. }
  92. uint8_t nrf24_status(FuriHalSpiBusHandle* handle) {
  93. uint8_t tx = RF24_NOP;
  94. nrf24_spi_trx(handle, &tx, &tx, 1);
  95. return tx;
  96. }
  97. uint32_t nrf24_get_rate(FuriHalSpiBusHandle* handle) {
  98. uint8_t setup = 0;
  99. uint32_t rate = 0;
  100. nrf24_read_reg(handle, REG_RF_SETUP, &setup, 1);
  101. setup &= 0x28;
  102. if(setup == 0x20)
  103. rate = 250000; // 250kbps
  104. else if(setup == 0x08)
  105. rate = 2000000; // 2Mbps
  106. else if(setup == 0x00)
  107. rate = 1000000; // 1Mbps
  108. return rate;
  109. }
  110. uint8_t nrf24_set_rate(FuriHalSpiBusHandle* handle, uint32_t rate) {
  111. uint8_t r6 = 0;
  112. uint8_t status = 0;
  113. if(!rate) rate = 2000000;
  114. nrf24_read_reg(handle, REG_RF_SETUP, &r6, 1); // RF_SETUP register
  115. r6 = r6 & (~0x28); // Clear rate fields.
  116. if(rate == 2000000)
  117. r6 = r6 | 0x08;
  118. else if(rate == 1000000)
  119. r6 = r6;
  120. else if(rate == 250000)
  121. r6 = r6 | 0x20;
  122. status = nrf24_write_reg(handle, REG_RF_SETUP, r6); // Write new rate.
  123. return status;
  124. }
  125. uint8_t nrf24_get_chan(FuriHalSpiBusHandle* handle) {
  126. uint8_t channel = 0;
  127. nrf24_read_reg(handle, REG_RF_CH, &channel, 1);
  128. return channel;
  129. }
  130. uint8_t nrf24_set_chan(FuriHalSpiBusHandle* handle, uint8_t chan) {
  131. uint8_t status;
  132. status = nrf24_write_reg(handle, REG_RF_CH, chan);
  133. return status;
  134. }
  135. uint8_t nrf24_get_src_mac(FuriHalSpiBusHandle* handle, uint8_t* mac) {
  136. uint8_t size = 0;
  137. uint8_t status = 0;
  138. size = nrf24_get_maclen(handle);
  139. status = nrf24_read_reg(handle, REG_RX_ADDR_P0, mac, size);
  140. return status;
  141. }
  142. uint8_t nrf24_set_src_mac(FuriHalSpiBusHandle* handle, uint8_t* mac, uint8_t size) {
  143. uint8_t status = 0;
  144. uint8_t clearmac[] = {0, 0, 0, 0, 0};
  145. nrf24_set_maclen(handle, size);
  146. nrf24_write_buf_reg(handle, REG_RX_ADDR_P0, clearmac, 5);
  147. status = nrf24_write_buf_reg(handle, REG_RX_ADDR_P0, mac, size);
  148. return status;
  149. }
  150. uint8_t nrf24_get_dst_mac(FuriHalSpiBusHandle* handle, uint8_t* mac) {
  151. uint8_t size = 0;
  152. uint8_t status = 0;
  153. size = nrf24_get_maclen(handle);
  154. status = nrf24_read_reg(handle, REG_TX_ADDR, mac, size);
  155. return status;
  156. }
  157. uint8_t nrf24_set_dst_mac(FuriHalSpiBusHandle* handle, uint8_t* mac, uint8_t size) {
  158. uint8_t status = 0;
  159. uint8_t clearmac[] = {0, 0, 0, 0, 0};
  160. nrf24_set_maclen(handle, size);
  161. nrf24_write_buf_reg(handle, REG_TX_ADDR, clearmac, 5);
  162. status = nrf24_write_buf_reg(handle, REG_TX_ADDR, mac, size);
  163. return status;
  164. }
  165. uint8_t nrf24_get_packetlen(FuriHalSpiBusHandle* handle, uint8_t pipe) {
  166. uint8_t len = 0;
  167. if(pipe > 5) pipe = 0;
  168. nrf24_read_reg(handle, RX_PW_P0 + pipe, &len, 1);
  169. return len;
  170. }
  171. uint8_t nrf24_set_packetlen(FuriHalSpiBusHandle* handle, uint8_t len) {
  172. uint8_t status = 0;
  173. status = nrf24_write_reg(handle, RX_PW_P0, len);
  174. return status;
  175. }
  176. // packet_size: 0 - dyn payload (read from PL_WID), 1 - read from pipe size, >1 - override
  177. // Return STATUS reg + additional: RX_DR - new data available, 0x80 - NRF24 hardware error
  178. uint8_t nrf24_rxpacket(
  179. FuriHalSpiBusHandle* handle,
  180. uint8_t* packet,
  181. uint8_t* ret_packetsize,
  182. uint8_t packet_size) {
  183. uint8_t status = 0;
  184. uint8_t buf[33]; // 32 max payload size + 1 for command
  185. status = nrf24_status(handle);
  186. if(!(status & RX_DR)) {
  187. uint8_t st = nrf24_read_register(handle, REG_FIFO_STATUS);
  188. if(st == 0xFF || st == 0) return 0x80; // hardware error
  189. if((st & 1) == 0) {
  190. FURI_LOG_D("NRF", "FIFO PKT");
  191. status |= RX_DR; // packet in FIFO buffer
  192. }
  193. }
  194. if(status & RX_DR) {
  195. if(status & 0x80) return 0x80; // hardware error
  196. if(packet_size == 1)
  197. packet_size = nrf24_get_packetlen(handle, (status >> 1) & 7);
  198. else if(packet_size == 0) {
  199. buf[0] = R_RX_PL_WID;
  200. buf[1] = 0xFF;
  201. nrf24_spi_trx(handle, buf, buf, 2);
  202. packet_size = buf[1];
  203. }
  204. if(packet_size > 32 || packet_size == 0) packet_size = 32;
  205. memset(buf, 0, packet_size + 1);
  206. buf[0] = R_RX_PAYLOAD;
  207. nrf24_spi_trx(handle, buf, buf, packet_size + 1);
  208. memcpy(packet, &buf[1], packet_size);
  209. nrf24_write_reg(handle, REG_STATUS, RX_DR); // clear RX_DR
  210. }
  211. if(status & (MAX_RT)) { // MAX_RT
  212. nrf24_write_reg(handle, REG_STATUS, (MAX_RT)); // clear MAX_RT.
  213. }
  214. *ret_packetsize = packet_size;
  215. return status;
  216. }
  217. // Return 0 when error
  218. uint8_t nrf24_txpacket(FuriHalSpiBusHandle* handle, uint8_t* payload, uint8_t size, bool ack) {
  219. uint8_t status = 0;
  220. uint8_t buf[size + 1];
  221. buf[0] = ack ? W_TX_PAYLOAD : W_TX_PAYLOAD_NOACK;
  222. memcpy(&buf[1], payload, size);
  223. nrf24_set_tx_mode(handle);
  224. nrf24_spi_trx(handle, buf, buf, size + 1);
  225. uint32_t start_time = furi_get_tick();
  226. do {
  227. furi_delay_us(100);
  228. status = nrf24_status(handle);
  229. } while(!(status & (TX_DS | MAX_RT)) && furi_get_tick() - start_time < 100UL);
  230. if(status & MAX_RT) {
  231. if(furi_log_get_level() == FuriLogLevelDebug)
  232. FURI_LOG_D(
  233. "NRF", "MAX RT: %X (%X)", nrf24_read_register(handle, REG_OBSERVE_TX), status);
  234. nrf24_flush_tx(handle);
  235. }
  236. furi_hal_gpio_write(nrf24_CE_PIN, false);
  237. //nrf24_set_idle(handle);
  238. if(status & (TX_DS | MAX_RT)) nrf24_write_reg(handle, REG_STATUS, TX_DS | MAX_RT);
  239. return status & TX_DS;
  240. }
  241. uint8_t nrf24_power_up(FuriHalSpiBusHandle* handle) {
  242. uint8_t status = 0;
  243. uint8_t cfg = 0;
  244. nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
  245. cfg = cfg | 2;
  246. status = nrf24_write_reg(handle, REG_CONFIG, cfg);
  247. //furi_delay_ms(1000);
  248. return status;
  249. }
  250. uint8_t nrf24_set_idle(FuriHalSpiBusHandle* handle) {
  251. uint8_t status = 0;
  252. uint8_t cfg = 0;
  253. nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
  254. cfg &= 0xfc; // clear bottom two bits to power down the radio
  255. status = nrf24_write_reg(handle, REG_CONFIG, cfg);
  256. furi_hal_gpio_write(nrf24_CE_PIN, false);
  257. return status;
  258. }
  259. uint8_t nrf24_set_rx_mode(FuriHalSpiBusHandle* handle) {
  260. uint8_t cfg = 0;
  261. cfg = nrf24_read_register(handle, REG_CONFIG);
  262. cfg |= 0x03; // PWR_UP, and PRIM_RX
  263. cfg = nrf24_write_reg(handle, REG_CONFIG, cfg);
  264. furi_hal_gpio_write(nrf24_CE_PIN, true);
  265. return cfg;
  266. }
  267. uint8_t nrf24_set_tx_mode(FuriHalSpiBusHandle* handle) {
  268. uint8_t reg;
  269. furi_hal_gpio_write(nrf24_CE_PIN, false);
  270. //nrf24_write_reg(handle, REG_STATUS, TX_DS | MAX_RT);
  271. reg = nrf24_read_register(handle, REG_CONFIG);
  272. reg &= ~0x01; // disable PRIM_RX
  273. reg |= 0x02; // PWR_UP
  274. reg = nrf24_write_reg(handle, REG_CONFIG, reg);
  275. furi_hal_gpio_write(nrf24_CE_PIN, true);
  276. return reg;
  277. }
  278. void hexlify(uint8_t* in, uint8_t size, char* out) {
  279. memset(out, 0, size * 2);
  280. for(int i = 0; i < size; i++)
  281. snprintf(out + strlen(out), sizeof(out + strlen(out)), "%02X", in[i]);
  282. }
  283. uint64_t bytes_to_int64(uint8_t* bytes, uint8_t size, bool bigendian) {
  284. uint64_t ret = 0;
  285. for(int i = 0; i < size; i++)
  286. if(bigendian)
  287. ret |= bytes[i] << ((size - 1 - i) * 8);
  288. else
  289. ret |= bytes[i] << (i * 8);
  290. return ret;
  291. }
  292. void int64_to_bytes(uint64_t val, uint8_t* out, bool bigendian) {
  293. for(int i = 0; i < 8; i++) {
  294. if(bigendian)
  295. out[i] = (val >> ((7 - i) * 8)) & 0xff;
  296. else
  297. out[i] = (val >> (i * 8)) & 0xff;
  298. }
  299. }
  300. uint32_t bytes_to_int32(uint8_t* bytes, bool bigendian) {
  301. uint32_t ret = 0;
  302. for(int i = 0; i < 4; i++)
  303. if(bigendian)
  304. ret |= bytes[i] << ((3 - i) * 8);
  305. else
  306. ret |= bytes[i] << (i * 8);
  307. return ret;
  308. }
  309. void int32_to_bytes(uint32_t val, uint8_t* out, bool bigendian) {
  310. for(int i = 0; i < 4; i++) {
  311. if(bigendian)
  312. out[i] = (val >> ((3 - i) * 8)) & 0xff;
  313. else
  314. out[i] = (val >> (i * 8)) & 0xff;
  315. }
  316. }
  317. uint64_t bytes_to_int16(uint8_t* bytes, bool bigendian) {
  318. uint16_t ret = 0;
  319. for(int i = 0; i < 2; i++)
  320. if(bigendian)
  321. ret |= bytes[i] << ((1 - i) * 8);
  322. else
  323. ret |= bytes[i] << (i * 8);
  324. return ret;
  325. }
  326. void int16_to_bytes(uint16_t val, uint8_t* out, bool bigendian) {
  327. for(int i = 0; i < 2; i++) {
  328. if(bigendian)
  329. out[i] = (val >> ((1 - i) * 8)) & 0xff;
  330. else
  331. out[i] = (val >> (i * 8)) & 0xff;
  332. }
  333. }
  334. uint8_t nrf24_set_mac(uint8_t mac_addr, uint8_t* mac, uint8_t mlen) {
  335. uint8_t addr[5];
  336. for(int i = 0; i < mlen; i++) addr[i] = mac[mlen - i - 1];
  337. return nrf24_write_buf_reg(nrf24_HANDLE, mac_addr, addr, mlen);
  338. }