nrf24.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <furi_hal_spi.h>
  5. #include <momentum/momentum.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #define R_REGISTER 0x00
  10. #define W_REGISTER 0x20
  11. #define REGISTER_MASK 0x1F
  12. #define ACTIVATE 0x50
  13. #define R_RX_PL_WID 0x60
  14. #define R_RX_PAYLOAD 0x61
  15. #define W_TX_PAYLOAD 0xA0
  16. #define W_TX_PAYLOAD_NOACK 0xB0
  17. #define W_ACK_PAYLOAD 0xA8
  18. #define FLUSH_TX 0xE1
  19. #define FLUSH_RX 0xE2
  20. #define REUSE_TX_PL 0xE3
  21. #define RF24_NOP 0xFF
  22. #define REG_CONFIG 0x00
  23. #define REG_EN_AA 0x01
  24. #define REG_EN_RXADDR 0x02
  25. #define REG_SETUP_AW 0x03
  26. #define REG_SETUP_RETR 0x04
  27. #define REG_DYNPD 0x1C
  28. #define REG_FEATURE 0x1D
  29. #define REG_RF_SETUP 0x06
  30. #define REG_STATUS 0x07
  31. #define REG_RX_ADDR_P0 0x0A
  32. #define REG_RX_ADDR_P1 0x0B
  33. #define REG_RX_ADDR_P2 0x0C
  34. #define REG_RX_ADDR_P3 0x0D
  35. #define REG_RX_ADDR_P4 0x0E
  36. #define REG_RX_ADDR_P5 0x0F
  37. #define REG_RF_CH 0x05
  38. #define REG_TX_ADDR 0x10
  39. #define REG_FIFO_STATUS 0x17
  40. #define REG_OBSERVE_TX 0x08
  41. #define RX_PW_P0 0x11
  42. #define RX_PW_P1 0x12
  43. #define RX_PW_P2 0x13
  44. #define RX_PW_P3 0x14
  45. #define RX_PW_P4 0x15
  46. #define RX_PW_P5 0x16
  47. #define RX_DR 0x40
  48. #define TX_DS 0x20
  49. #define MAX_RT 0x10
  50. #define NRF24_EN_DYN_ACK 0x01
  51. #define nrf24_TIMEOUT 500
  52. #define nrf24_CE_PIN &gpio_ext_pb2
  53. #define nrf24_HANDLE \
  54. (momentum_settings.spi_nrf24_handle == SpiDefault ? &furi_hal_spi_bus_handle_external : \
  55. &furi_hal_spi_bus_handle_external_extra)
  56. /* Low level API */
  57. /** Write device register
  58. *
  59. * @param handle - pointer to FuriHalSpiHandle
  60. * @param reg - register
  61. * @param data - data to write
  62. *
  63. * @return device status
  64. */
  65. uint8_t nrf24_write_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t data);
  66. /** Write buffer to device register
  67. *
  68. * @param handle - pointer to FuriHalSpiHandle
  69. * @param reg - register
  70. * @param data - data to write
  71. * @param size - size of data to write
  72. *
  73. * @return device status
  74. */
  75. uint8_t nrf24_write_buf_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* data, uint8_t size);
  76. /** Read device register
  77. *
  78. * @param handle - pointer to FuriHalSpiHandle
  79. * @param reg - register
  80. * @param[out] data - pointer to data
  81. *
  82. * @return device status
  83. */
  84. uint8_t nrf24_read_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* data, uint8_t size);
  85. // Read single register (1 byte)
  86. uint8_t nrf24_read_register(FuriHalSpiBusHandle* handle, uint8_t reg);
  87. /** Power up the radio for operation
  88. *
  89. * @param handle - pointer to FuriHalSpiHandle
  90. *
  91. * @return device status
  92. */
  93. uint8_t nrf24_power_up(FuriHalSpiBusHandle* handle);
  94. /** Power down the radio
  95. *
  96. * @param handle - pointer to FuriHalSpiHandle
  97. *
  98. * @return device status
  99. */
  100. uint8_t nrf24_set_idle(FuriHalSpiBusHandle* handle);
  101. /** Sets the radio to RX mode
  102. *
  103. * @param handle - pointer to FuriHalSpiHandle
  104. *
  105. * @return device status
  106. */
  107. uint8_t nrf24_set_rx_mode(FuriHalSpiBusHandle* handle);
  108. /** Sets the radio to TX mode
  109. *
  110. * @param handle - pointer to FuriHalSpiHandle
  111. *
  112. * @return device status
  113. */
  114. uint8_t nrf24_set_tx_mode(FuriHalSpiBusHandle* handle);
  115. /*=============================================================================================================*/
  116. /* High level API */
  117. /** Must call this before using any other nrf24 API
  118. *
  119. */
  120. void nrf24_init();
  121. /** Must call this when we end using nrf24 device
  122. *
  123. */
  124. void nrf24_deinit();
  125. /** Send flush rx command
  126. *
  127. * @param handle - pointer to FuriHalSpiHandle
  128. *
  129. * @return device status
  130. */
  131. uint8_t nrf24_flush_rx(FuriHalSpiBusHandle* handle);
  132. /** Send flush tx command
  133. *
  134. * @param handle - pointer to FuriHalSpiHandle
  135. *
  136. * @return device status
  137. */
  138. uint8_t nrf24_flush_tx(FuriHalSpiBusHandle* handle);
  139. /** Gets the RX packet length in data pipe 0
  140. *
  141. * @param handle - pointer to FuriHalSpiHandle
  142. * pipe - pipe index (0..5)
  143. * @return packet length in data pipe 0
  144. */
  145. uint8_t nrf24_get_packetlen(FuriHalSpiBusHandle* handle, uint8_t pipe);
  146. /** Sets the RX packet length in data pipe 0
  147. *
  148. * @param handle - pointer to FuriHalSpiHandle
  149. * @param len - length to set
  150. *
  151. * @return device status
  152. */
  153. uint8_t nrf24_set_packetlen(FuriHalSpiBusHandle* handle, uint8_t len);
  154. /** Gets configured length of MAC address
  155. *
  156. * @param handle - pointer to FuriHalSpiHandle
  157. *
  158. * @return MAC address length
  159. */
  160. uint8_t nrf24_get_maclen(FuriHalSpiBusHandle* handle);
  161. /** Sets configured length of MAC address
  162. *
  163. * @param handle - pointer to FuriHalSpiHandle
  164. * @param maclen - length to set MAC address to, must be greater than 1 and less than 6
  165. *
  166. * @return MAC address length
  167. */
  168. uint8_t nrf24_set_maclen(FuriHalSpiBusHandle* handle, uint8_t maclen);
  169. /** Gets the current status flags from the STATUS register
  170. *
  171. * @param handle - pointer to FuriHalSpiHandle
  172. *
  173. * @return status flags
  174. */
  175. uint8_t nrf24_status(FuriHalSpiBusHandle* handle);
  176. /** Gets the current transfer rate
  177. *
  178. * @param handle - pointer to FuriHalSpiHandle
  179. *
  180. * @return transfer rate in bps
  181. */
  182. uint32_t nrf24_get_rate(FuriHalSpiBusHandle* handle);
  183. /** Sets the transfer rate
  184. *
  185. * @param handle - pointer to FuriHalSpiHandle
  186. * @param rate - the transfer rate in bps
  187. *
  188. * @return device status
  189. */
  190. uint8_t nrf24_set_rate(FuriHalSpiBusHandle* handle, uint32_t rate);
  191. /** Gets the current channel
  192. * In nrf24, the channel number is multiplied times 1MHz and added to 2400MHz to get the frequency
  193. *
  194. * @param handle - pointer to FuriHalSpiHandle
  195. *
  196. * @return channel
  197. */
  198. uint8_t nrf24_get_chan(FuriHalSpiBusHandle* handle);
  199. /** Sets the channel
  200. *
  201. * @param handle - pointer to FuriHalSpiHandle
  202. * @param frequency - the frequency in hertz
  203. *
  204. * @return device status
  205. */
  206. uint8_t nrf24_set_chan(FuriHalSpiBusHandle* handle, uint8_t chan);
  207. /** Gets the source mac address
  208. *
  209. * @param handle - pointer to FuriHalSpiHandle
  210. * @param[out] mac - the source mac address
  211. *
  212. * @return device status
  213. */
  214. uint8_t nrf24_get_src_mac(FuriHalSpiBusHandle* handle, uint8_t* mac);
  215. /** Sets the source mac address
  216. *
  217. * @param handle - pointer to FuriHalSpiHandle
  218. * @param mac - the mac address to set
  219. * @param size - the size of the mac address (2 to 5)
  220. *
  221. * @return device status
  222. */
  223. uint8_t nrf24_set_src_mac(FuriHalSpiBusHandle* handle, uint8_t* mac, uint8_t size);
  224. /** Gets the dest mac address
  225. *
  226. * @param handle - pointer to FuriHalSpiHandle
  227. * @param[out] mac - the source mac address
  228. *
  229. * @return device status
  230. */
  231. uint8_t nrf24_get_dst_mac(FuriHalSpiBusHandle* handle, uint8_t* mac);
  232. /** Sets the dest mac address
  233. *
  234. * @param handle - pointer to FuriHalSpiHandle
  235. * @param mac - the mac address to set
  236. * @param size - the size of the mac address (2 to 5)
  237. *
  238. * @return device status
  239. */
  240. uint8_t nrf24_set_dst_mac(FuriHalSpiBusHandle* handle, uint8_t* mac, uint8_t size);
  241. /** Reads RX packet
  242. *
  243. * @param handle - pointer to FuriHalSpiHandle
  244. * @param[out] packet - the packet contents
  245. * @param[out] ret_packetsize - size of the received packet
  246. * @param packet_size: >1 - size, 1 - packet length is determined by RX_PW_P0 register, 0 - it is determined by dynamic payload length command
  247. *
  248. * @return device status
  249. */
  250. uint8_t nrf24_rxpacket(
  251. FuriHalSpiBusHandle* handle,
  252. uint8_t* packet,
  253. uint8_t* ret_packetsize,
  254. uint8_t packet_size_flag);
  255. /** Sends TX packet
  256. *
  257. * @param handle - pointer to FuriHalSpiHandle
  258. * @param packet - the packet contents
  259. * @param size - packet size
  260. * @param ack - boolean to determine whether an ACK is required for the packet or not
  261. *
  262. * @return device status
  263. */
  264. uint8_t nrf24_txpacket(FuriHalSpiBusHandle* handle, uint8_t* payload, uint8_t size, bool ack);
  265. /** Configure the radio
  266. * This is not comprehensive, but covers a lot of the common configuration options that may be changed
  267. * @param handle - pointer to FuriHalSpiHandle
  268. * @param rate - transfer rate in Mbps (1 or 2)
  269. * @param srcmac - source mac address
  270. * @param dstmac - destination mac address
  271. * @param maclen - length of mac address
  272. * @param channel - channel to tune to
  273. * @param noack - if true, disable auto-acknowledge
  274. * @param disable_aa - if true, disable ShockBurst
  275. *
  276. */
  277. void nrf24_configure(
  278. FuriHalSpiBusHandle* handle,
  279. uint8_t rate,
  280. uint8_t* srcmac,
  281. uint8_t* dstmac,
  282. uint8_t maclen,
  283. uint8_t channel,
  284. bool noack,
  285. bool disable_aa);
  286. // Set mac address (MSB first), Return: Status
  287. uint8_t nrf24_set_mac(uint8_t mac_addr, uint8_t* mac, uint8_t mlen);
  288. /** Configures the radio for "promiscuous mode" and primes it for rx
  289. * This is not an actual mode of the nrf24, but this function exploits a few bugs in the chip that allows it to act as if it were.
  290. * See http://travisgoodspeed.blogspot.com/2011/02/promiscuity-is-nrf24l01s-duty.html for details.
  291. * @param handle - pointer to FuriHalSpiHandle
  292. * @param channel - channel to tune to
  293. * @param rate - transfer rate in Mbps (1 or 2)
  294. */
  295. void nrf24_init_promisc_mode(FuriHalSpiBusHandle* handle, uint8_t channel, uint8_t rate);
  296. /** Listens for a packet and returns first possible address sniffed
  297. * Call this only after calling nrf24_init_promisc_mode
  298. * @param handle - pointer to FuriHalSpiHandle
  299. * @param maclen - length of target mac address
  300. * @param[out] addresses - sniffed address
  301. *
  302. * @return success
  303. */
  304. bool nrf24_sniff_address(FuriHalSpiBusHandle* handle, uint8_t maclen, uint8_t* address);
  305. /** Sends ping packet on each channel for designated tx mac looking for ack
  306. *
  307. * @param handle - pointer to FuriHalSpiHandle
  308. * @param srcmac - source address
  309. * @param dstmac - destination address
  310. * @param maclen - length of address
  311. * @param rate - transfer rate in Mbps (1 or 2)
  312. * @param min_channel - channel to start with
  313. * @param max_channel - channel to end at
  314. * @param autoinit - if true, automatically configure radio for this channel
  315. *
  316. * @return channel that the address is listening on, if this value is above the max_channel param, it failed
  317. */
  318. uint8_t nrf24_find_channel(
  319. FuriHalSpiBusHandle* handle,
  320. uint8_t* srcmac,
  321. uint8_t* dstmac,
  322. uint8_t maclen,
  323. uint8_t rate,
  324. uint8_t min_channel,
  325. uint8_t max_channel,
  326. bool autoinit);
  327. /** Converts 64 bit value into uint8_t array
  328. * @param val - 64-bit integer
  329. * @param[out] out - bytes out
  330. * @param bigendian - if true, convert as big endian, otherwise little endian
  331. */
  332. void int64_to_bytes(uint64_t val, uint8_t* out, bool bigendian);
  333. /** Converts 32 bit value into uint8_t array
  334. * @param val - 32-bit integer
  335. * @param[out] out - bytes out
  336. * @param bigendian - if true, convert as big endian, otherwise little endian
  337. */
  338. void int32_to_bytes(uint32_t val, uint8_t* out, bool bigendian);
  339. /** Converts uint8_t array into 32 bit value
  340. * @param bytes - uint8_t array
  341. * @param bigendian - if true, convert as big endian, otherwise little endian
  342. *
  343. * @return 32-bit value
  344. */
  345. uint32_t bytes_to_int32(uint8_t* bytes, bool bigendian);
  346. #ifdef __cplusplus
  347. }
  348. #endif