nrf24.h 11 KB

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