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(
  36. const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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;
  199. buf[1] = 0xFF;
  200. nrf24_spi_trx(handle, buf, buf, 2);
  201. packet_size = buf[1];
  202. }
  203. if(packet_size > 32 || packet_size == 0) packet_size = 32;
  204. memset(buf, 0, packet_size + 1);
  205. buf[0] = R_RX_PAYLOAD;
  206. nrf24_spi_trx(handle, buf, buf, packet_size + 1);
  207. memcpy(packet, &buf[1], packet_size);
  208. nrf24_write_reg(handle, REG_STATUS, RX_DR); // clear RX_DR
  209. }
  210. if(status & (MAX_RT)) { // MAX_RT
  211. nrf24_write_reg(handle, REG_STATUS, (MAX_RT)); // clear MAX_RT.
  212. }
  213. *ret_packetsize = packet_size;
  214. return status;
  215. }
  216. // Return 0 when error
  217. uint8_t nrf24_txpacket(const FuriHalSpiBusHandle* handle, uint8_t* payload, uint8_t size, bool ack) {
  218. uint8_t status = 0;
  219. uint8_t buf[size + 1];
  220. buf[0] = ack ? W_TX_PAYLOAD : W_TX_PAYLOAD_NOACK;
  221. memcpy(&buf[1], payload, size);
  222. nrf24_set_tx_mode(handle);
  223. nrf24_spi_trx(handle, buf, buf, size + 1);
  224. uint32_t start_time = furi_get_tick();
  225. do {
  226. furi_delay_us(100);
  227. status = nrf24_status(handle);
  228. } while(!(status & (TX_DS | MAX_RT)) && furi_get_tick() - start_time < 100UL);
  229. if(status & MAX_RT) {
  230. if(furi_log_get_level() == FuriLogLevelDebug)
  231. FURI_LOG_D(
  232. "NRF", "MAX RT: %X (%X)", nrf24_read_register(handle, REG_OBSERVE_TX), status);
  233. nrf24_flush_tx(handle);
  234. }
  235. furi_hal_gpio_write(nrf24_CE_PIN, false);
  236. //nrf24_set_idle(handle);
  237. if(status & (TX_DS | MAX_RT)) nrf24_write_reg(handle, REG_STATUS, TX_DS | MAX_RT);
  238. return status & TX_DS;
  239. }
  240. uint8_t nrf24_power_up(const FuriHalSpiBusHandle* handle) {
  241. uint8_t status = 0;
  242. uint8_t cfg = 0;
  243. nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
  244. cfg = cfg | 2;
  245. status = nrf24_write_reg(handle, REG_CONFIG, cfg);
  246. //furi_delay_ms(1000);
  247. return status;
  248. }
  249. uint8_t nrf24_set_idle(const FuriHalSpiBusHandle* handle) {
  250. uint8_t status = 0;
  251. uint8_t cfg = 0;
  252. nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
  253. cfg &= 0xfc; // clear bottom two bits to power down the radio
  254. status = nrf24_write_reg(handle, REG_CONFIG, cfg);
  255. furi_hal_gpio_write(nrf24_CE_PIN, false);
  256. return status;
  257. }
  258. uint8_t nrf24_set_rx_mode(const FuriHalSpiBusHandle* handle) {
  259. uint8_t cfg = 0;
  260. cfg = nrf24_read_register(handle, REG_CONFIG);
  261. cfg |= 0x03; // PWR_UP, and PRIM_RX
  262. cfg = nrf24_write_reg(handle, REG_CONFIG, cfg);
  263. furi_hal_gpio_write(nrf24_CE_PIN, true);
  264. return cfg;
  265. }
  266. uint8_t nrf24_set_tx_mode(const FuriHalSpiBusHandle* handle) {
  267. uint8_t reg;
  268. furi_hal_gpio_write(nrf24_CE_PIN, false);
  269. //nrf24_write_reg(handle, REG_STATUS, TX_DS | MAX_RT);
  270. reg = nrf24_read_register(handle, REG_CONFIG);
  271. reg &= ~0x01; // disable PRIM_RX
  272. reg |= 0x02; // PWR_UP
  273. reg = nrf24_write_reg(handle, REG_CONFIG, reg);
  274. furi_hal_gpio_write(nrf24_CE_PIN, true);
  275. return reg;
  276. }
  277. void hexlify(uint8_t* in, uint8_t size, char* out) {
  278. memset(out, 0, size * 2);
  279. for(int i = 0; i < size; i++)
  280. snprintf(out + strlen(out), sizeof(out + strlen(out)), "%02X", in[i]);
  281. }
  282. uint64_t bytes_to_int64(uint8_t* bytes, uint8_t size, bool bigendian) {
  283. uint64_t ret = 0;
  284. for(int i = 0; i < size; i++)
  285. if(bigendian)
  286. ret |= bytes[i] << ((size - 1 - i) * 8);
  287. else
  288. ret |= bytes[i] << (i * 8);
  289. return ret;
  290. }
  291. void int64_to_bytes(uint64_t val, uint8_t* out, bool bigendian) {
  292. for(int i = 0; i < 8; i++) {
  293. if(bigendian)
  294. out[i] = (val >> ((7 - i) * 8)) & 0xff;
  295. else
  296. out[i] = (val >> (i * 8)) & 0xff;
  297. }
  298. }
  299. uint32_t bytes_to_int32(uint8_t* bytes, bool bigendian) {
  300. uint32_t ret = 0;
  301. for(int i = 0; i < 4; i++)
  302. if(bigendian)
  303. ret |= bytes[i] << ((3 - i) * 8);
  304. else
  305. ret |= bytes[i] << (i * 8);
  306. return ret;
  307. }
  308. void int32_to_bytes(uint32_t val, uint8_t* out, bool bigendian) {
  309. for(int i = 0; i < 4; i++) {
  310. if(bigendian)
  311. out[i] = (val >> ((3 - i) * 8)) & 0xff;
  312. else
  313. out[i] = (val >> (i * 8)) & 0xff;
  314. }
  315. }
  316. uint64_t bytes_to_int16(uint8_t* bytes, bool bigendian) {
  317. uint16_t ret = 0;
  318. for(int i = 0; i < 2; i++)
  319. if(bigendian)
  320. ret |= bytes[i] << ((1 - i) * 8);
  321. else
  322. ret |= bytes[i] << (i * 8);
  323. return ret;
  324. }
  325. void int16_to_bytes(uint16_t val, uint8_t* out, bool bigendian) {
  326. for(int i = 0; i < 2; i++) {
  327. if(bigendian)
  328. out[i] = (val >> ((1 - i) * 8)) & 0xff;
  329. else
  330. out[i] = (val >> (i * 8)) & 0xff;
  331. }
  332. }
  333. uint8_t nrf24_set_mac(uint8_t mac_addr, uint8_t* mac, uint8_t mlen) {
  334. uint8_t addr[5];
  335. for(int i = 0; i < mlen; i++)
  336. addr[i] = mac[mlen - i - 1];
  337. return nrf24_write_buf_reg(nrf24_HANDLE, mac_addr, addr, mlen);
  338. }