nrf24.c 12 KB

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