nrf24.c 11 KB

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