nrf24.h 11 KB

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