nrf24.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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_RF_CH 0x05
  33. #define REG_TX_ADDR 0x10
  34. #define RX_PW_P0 0x11
  35. #define TX_DS 0x20
  36. #define MAX_RT 0x10
  37. #define nrf24_TIMEOUT 500
  38. #define nrf24_CE_PIN &gpio_ext_pb2
  39. #define nrf24_HANDLE \
  40. (momentum_settings.spi_nrf24_handle == SpiDefault ? &furi_hal_spi_bus_handle_external : \
  41. &furi_hal_spi_bus_handle_external_extra)
  42. /* Low level API */
  43. /** Write device register
  44. *
  45. * @param handle - pointer to FuriHalSpiHandle
  46. * @param reg - register
  47. * @param data - data to write
  48. *
  49. * @return device status
  50. */
  51. uint8_t nrf24_write_reg(const FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t data);
  52. /** Write buffer to device register
  53. *
  54. * @param handle - pointer to FuriHalSpiHandle
  55. * @param reg - register
  56. * @param data - data to write
  57. * @param size - size of data to write
  58. *
  59. * @return device status
  60. */
  61. uint8_t nrf24_write_buf_reg(const FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* data, uint8_t size);
  62. /** Read device register
  63. *
  64. * @param handle - pointer to FuriHalSpiHandle
  65. * @param reg - register
  66. * @param[out] data - pointer to data
  67. *
  68. * @return device status
  69. */
  70. uint8_t nrf24_read_reg(const FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* data, uint8_t size);
  71. /** Power up the radio for operation
  72. *
  73. * @param handle - pointer to FuriHalSpiHandle
  74. *
  75. * @return device status
  76. */
  77. uint8_t nrf24_power_up(const FuriHalSpiBusHandle* handle);
  78. /** Power down the radio
  79. *
  80. * @param handle - pointer to FuriHalSpiHandle
  81. *
  82. * @return device status
  83. */
  84. uint8_t nrf24_set_idle(const FuriHalSpiBusHandle* handle);
  85. /** Sets the radio to RX mode
  86. *
  87. * @param handle - pointer to FuriHalSpiHandle
  88. *
  89. * @return device status
  90. */
  91. uint8_t nrf24_set_rx_mode(const FuriHalSpiBusHandle* handle);
  92. /** Sets the radio to TX mode
  93. *
  94. * @param handle - pointer to FuriHalSpiHandle
  95. *
  96. * @return device status
  97. */
  98. uint8_t nrf24_set_tx_mode(const FuriHalSpiBusHandle* handle);
  99. /*=============================================================================================================*/
  100. /* High level API */
  101. /** Must call this before using any other nrf24 API
  102. *
  103. */
  104. void nrf24_init();
  105. /** Must call this when we end using nrf24 device
  106. *
  107. */
  108. void nrf24_deinit();
  109. /** Send flush rx command
  110. *
  111. * @param handle - pointer to FuriHalSpiHandle
  112. *
  113. * @return device status
  114. */
  115. uint8_t nrf24_flush_rx(const FuriHalSpiBusHandle* handle);
  116. /** Send flush tx command
  117. *
  118. * @param handle - pointer to FuriHalSpiHandle
  119. *
  120. * @return device status
  121. */
  122. uint8_t nrf24_flush_tx(const FuriHalSpiBusHandle* handle);
  123. /** Gets the RX packet length in data pipe 0
  124. *
  125. * @param handle - pointer to FuriHalSpiHandle
  126. *
  127. * @return packet length in data pipe 0
  128. */
  129. uint8_t nrf24_get_packetlen(const FuriHalSpiBusHandle* handle);
  130. /** Sets the RX packet length in data pipe 0
  131. *
  132. * @param handle - pointer to FuriHalSpiHandle
  133. * @param len - length to set
  134. *
  135. * @return device status
  136. */
  137. uint8_t nrf24_set_packetlen(const FuriHalSpiBusHandle* handle, uint8_t len);
  138. /** Gets configured length of MAC address
  139. *
  140. * @param handle - pointer to FuriHalSpiHandle
  141. *
  142. * @return MAC address length
  143. */
  144. uint8_t nrf24_get_maclen(const FuriHalSpiBusHandle* handle);
  145. /** Sets configured length of MAC address
  146. *
  147. * @param handle - pointer to FuriHalSpiHandle
  148. * @param maclen - length to set MAC address to, must be greater than 1 and less than 6
  149. *
  150. * @return MAC address length
  151. */
  152. uint8_t nrf24_set_maclen(const FuriHalSpiBusHandle* handle, uint8_t maclen);
  153. /** Gets the current status flags from the STATUS register
  154. *
  155. * @param handle - pointer to FuriHalSpiHandle
  156. *
  157. * @return status flags
  158. */
  159. uint8_t nrf24_status(const FuriHalSpiBusHandle* handle);
  160. /** Gets the current transfer rate
  161. *
  162. * @param handle - pointer to FuriHalSpiHandle
  163. *
  164. * @return transfer rate in bps
  165. */
  166. uint32_t nrf24_get_rate(const FuriHalSpiBusHandle* handle);
  167. /** Sets the transfer rate
  168. *
  169. * @param handle - pointer to FuriHalSpiHandle
  170. * @param rate - the transfer rate in bps
  171. *
  172. * @return device status
  173. */
  174. uint8_t nrf24_set_rate(const FuriHalSpiBusHandle* handle, uint32_t rate);
  175. /** Gets the current channel
  176. * In nrf24, the channel number is multiplied times 1MHz and added to 2400MHz to get the frequency
  177. *
  178. * @param handle - pointer to FuriHalSpiHandle
  179. *
  180. * @return channel
  181. */
  182. uint8_t nrf24_get_chan(const FuriHalSpiBusHandle* handle);
  183. /** Sets the channel
  184. *
  185. * @param handle - pointer to FuriHalSpiHandle
  186. * @param frequency - the frequency in hertz
  187. *
  188. * @return device status
  189. */
  190. uint8_t nrf24_set_chan(const FuriHalSpiBusHandle* handle, uint8_t chan);
  191. /** Gets the source mac address
  192. *
  193. * @param handle - pointer to FuriHalSpiHandle
  194. * @param[out] mac - the source mac address
  195. *
  196. * @return device status
  197. */
  198. uint8_t nrf24_get_src_mac(const FuriHalSpiBusHandle* handle, uint8_t* mac);
  199. /** Sets the source mac address
  200. *
  201. * @param handle - pointer to FuriHalSpiHandle
  202. * @param mac - the mac address to set
  203. * @param size - the size of the mac address (2 to 5)
  204. *
  205. * @return device status
  206. */
  207. uint8_t nrf24_set_src_mac(const FuriHalSpiBusHandle* handle, uint8_t* mac, uint8_t size);
  208. /** Gets the dest mac address
  209. *
  210. * @param handle - pointer to FuriHalSpiHandle
  211. * @param[out] mac - the source mac address
  212. *
  213. * @return device status
  214. */
  215. uint8_t nrf24_get_dst_mac(const FuriHalSpiBusHandle* handle, uint8_t* mac);
  216. /** Sets the dest mac address
  217. *
  218. * @param handle - pointer to FuriHalSpiHandle
  219. * @param mac - the mac address to set
  220. * @param size - the size of the mac address (2 to 5)
  221. *
  222. * @return device status
  223. */
  224. uint8_t nrf24_set_dst_mac(const FuriHalSpiBusHandle* handle, uint8_t* mac, uint8_t size);
  225. /** Reads RX packet
  226. *
  227. * @param handle - pointer to FuriHalSpiHandle
  228. * @param[out] packet - the packet contents
  229. * @param[out] packetsize - size of the received packet
  230. * @param full - boolean set to true, packet length is determined by RX_PW_P0 register, false it is determined by dynamic payload length command
  231. *
  232. * @return device status
  233. */
  234. uint8_t
  235. nrf24_rxpacket(const FuriHalSpiBusHandle* handle, uint8_t* packet, uint8_t* packetsize, bool full);
  236. /** Sends TX packet
  237. *
  238. * @param handle - pointer to FuriHalSpiHandle
  239. * @param packet - the packet contents
  240. * @param size - packet size
  241. * @param ack - boolean to determine whether an ACK is required for the packet or not
  242. *
  243. * @return device status
  244. */
  245. uint8_t nrf24_txpacket(const FuriHalSpiBusHandle* handle, uint8_t* payload, uint8_t size, bool ack);
  246. /** Configure the radio
  247. * This is not comprehensive, but covers a lot of the common configuration options that may be changed
  248. * @param handle - pointer to FuriHalSpiHandle
  249. * @param rate - transfer rate in Mbps (1 or 2)
  250. * @param srcmac - source mac address
  251. * @param dstmac - destination mac address
  252. * @param maclen - length of mac address
  253. * @param channel - channel to tune to
  254. * @param noack - if true, disable auto-acknowledge
  255. * @param disable_aa - if true, disable ShockBurst
  256. *
  257. */
  258. void nrf24_configure(
  259. const FuriHalSpiBusHandle* handle,
  260. uint8_t rate,
  261. uint8_t* srcmac,
  262. uint8_t* dstmac,
  263. uint8_t maclen,
  264. uint8_t channel,
  265. bool noack,
  266. bool disable_aa);
  267. /** Configures the radio for "promiscuous mode" and primes it for rx
  268. * 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.
  269. * See http://travisgoodspeed.blogspot.com/2011/02/promiscuity-is-nrf24l01s-duty.html for details.
  270. * @param handle - pointer to FuriHalSpiHandle
  271. * @param channel - channel to tune to
  272. * @param rate - transfer rate in Mbps (1 or 2)
  273. */
  274. void nrf24_init_promisc_mode(const FuriHalSpiBusHandle* handle, uint8_t channel, uint8_t rate);
  275. /** Listens for a packet and returns first possible address sniffed
  276. * Call this only after calling nrf24_init_promisc_mode
  277. * @param handle - pointer to FuriHalSpiHandle
  278. * @param maclen - length of target mac address
  279. * @param[out] addresses - sniffed address
  280. *
  281. * @return success
  282. */
  283. bool nrf24_sniff_address(const FuriHalSpiBusHandle* handle, uint8_t maclen, uint8_t* address);
  284. /** Sends ping packet on each channel for designated tx mac looking for ack
  285. *
  286. * @param handle - pointer to FuriHalSpiHandle
  287. * @param srcmac - source address
  288. * @param dstmac - destination address
  289. * @param maclen - length of address
  290. * @param rate - transfer rate in Mbps (1 or 2)
  291. * @param min_channel - channel to start with
  292. * @param max_channel - channel to end at
  293. * @param autoinit - if true, automatically configure radio for this channel
  294. *
  295. * @return channel that the address is listening on, if this value is above the max_channel param, it failed
  296. */
  297. uint8_t nrf24_find_channel(
  298. const FuriHalSpiBusHandle* handle,
  299. uint8_t* srcmac,
  300. uint8_t* dstmac,
  301. uint8_t maclen,
  302. uint8_t rate,
  303. uint8_t min_channel,
  304. uint8_t max_channel,
  305. bool autoinit);
  306. /** Converts 64 bit value into uint8_t array
  307. * @param val - 64-bit integer
  308. * @param[out] out - bytes out
  309. * @param bigendian - if true, convert as big endian, otherwise little endian
  310. */
  311. void int64_to_bytes(uint64_t val, uint8_t* out, bool bigendian);
  312. /** Converts 32 bit value into uint8_t array
  313. * @param val - 32-bit integer
  314. * @param[out] out - bytes out
  315. * @param bigendian - if true, convert as big endian, otherwise little endian
  316. */
  317. void int32_to_bytes(uint32_t val, uint8_t* out, bool bigendian);
  318. /** Converts uint8_t array into 32 bit value
  319. * @param bytes - uint8_t array
  320. * @param bigendian - if true, convert as big endian, otherwise little endian
  321. *
  322. * @return 32-bit value
  323. */
  324. uint32_t bytes_to_int32(uint8_t* bytes, bool bigendian);
  325. /** Check if the nrf24 is connected
  326. * @param handle - pointer to FuriHalSpiHandle
  327. *
  328. * @return true if connected, otherwise false
  329. */
  330. bool nrf24_check_connected(const FuriHalSpiBusHandle* handle);
  331. #ifdef __cplusplus
  332. }
  333. #endif