nrf24.h 11 KB

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