nrf24l01.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * @author Tilen MAJERLE
  3. * @email tilen@majerle.eu
  4. * @website http://stm32f4-discovery.net
  5. * @link http://stm32f4-discovery.net/2015/09/hal-library-25-nrf24l01-for-stm32fxxx/
  6. * @version v1.0
  7. * @ide Keil uVision
  8. * @license MIT
  9. * @brief Library template
  10. *
  11. \verbatim
  12. ----------------------------------------------------------------------
  13. Copyright (c) 2016 Tilen MAJERLE
  14. Permission is hereby granted, free of charge, to any person
  15. obtaining a copy of this software and associated documentation
  16. files (the "Software"), to deal in the Software without restriction,
  17. including without limitation the rights to use, copy, modify, merge,
  18. publish, distribute, sublicense, and/or sell copies of the Software,
  19. and to permit persons to whom the Software is furnished to do so,
  20. subject to the following conditions:
  21. The above copyright notice and this permission notice shall be
  22. included in all copies or substantial portions of the Software.
  23. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  25. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  26. AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  27. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  28. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  30. OTHER DEALINGS IN THE SOFTWARE.
  31. ----------------------------------------------------------------------
  32. \endverbatim
  33. */
  34. /**
  35. * @defgroup TM_NRF24L01P
  36. * @brief nRF24L01+ library for STM32xxx devices - http://stm32f4-discovery.net/2015/09/hal-library-25-nrf24l01-for-stm32fxxx/
  37. * @{
  38. *
  39. * This library allows you to work with nRF24L01+ modules.
  40. *
  41. * You can send and receive data from nRF24L01+ modules.
  42. *
  43. * \par Default pinout
  44. *
  45. \verbatim
  46. NRF24L01+ STM32Fxxx DESCRIPTION
  47. GND GND Ground
  48. VCC 3.3V 3.3V
  49. CE PD8 RF activated pin
  50. CSN PD7 Chip select pin for SPI
  51. SCK PC10 SCK pin for SPI
  52. MOSI PC12 MOSI pin for SPI
  53. MISO PC11 MISO pin for SPI
  54. IRQ Not used Interrupt pin. Goes low when active. Pin functionality is active, but not used in library
  55. \endverbatim
  56. *
  57. * IRQ pin is not used in this library, but its functionality is enabled by this software.
  58. *
  59. * You can still set any pin on Fxxx to be an external interrupt and handle interrupts from nRF24L01+ module.
  60. *
  61. * The easiest way to that is to use @ref TM_EXTI library and attach interrupt functionality to this pin
  62. *
  63. * \par Custom pinout
  64. *
  65. * Add lines below in your defines.h file if you want to change default pinout:
  66. */
  67. #ifndef _NRF_24L01_H_
  68. #define _NRF_24L01_H_
  69. /* Interrupt masks */
  70. #define NRF24L01_IRQ_DATA_READY 0x40 /*!< Data ready for receive */
  71. #define NRF24L01_IRQ_TRAN_OK 0x20 /*!< Transmission went OK */
  72. #define NRF24L01_IRQ_MAX_RT 0x10 /*!< Max retransmissions reached, last transmission failed */
  73. /* NRF24L01+ registers*/
  74. #define NRF24L01_REG_CONFIG 0x00 //Configuration Register
  75. #define NRF24L01_REG_EN_AA 0x01 //Enable ‘Auto Acknowledgment’ Function
  76. #define NRF24L01_REG_EN_RXADDR 0x02 //Enabled RX Addresses
  77. #define NRF24L01_REG_SETUP_AW 0x03 //Setup of Address Widths (common for all data pipes)
  78. #define NRF24L01_REG_SETUP_RETR 0x04 //Setup of Automatic Retransmission
  79. #define NRF24L01_REG_RF_CH 0x05 //RF Channel
  80. #define NRF24L01_REG_RF_SETUP 0x06 //RF Setup Register
  81. #define NRF24L01_REG_STATUS 0x07 //Status Register
  82. #define NRF24L01_REG_OBSERVE_TX 0x08 //Transmit observe registerf
  83. #define NRF24L01_REG_RPD 0x09
  84. #define NRF24L01_REG_RX_ADDR_P0 0x0A //Receive address data pipe 0. 5 Bytes maximum length.
  85. #define NRF24L01_REG_RX_ADDR_P1 0x0B //Receive address data pipe 1. 5 Bytes maximum length.
  86. #define NRF24L01_REG_RX_ADDR_P2 0x0C //Receive address data pipe 2. Only LSB
  87. #define NRF24L01_REG_RX_ADDR_P3 0x0D //Receive address data pipe 3. Only LSB
  88. #define NRF24L01_REG_RX_ADDR_P4 0x0E //Receive address data pipe 4. Only LSB
  89. #define NRF24L01_REG_RX_ADDR_P5 0x0F //Receive address data pipe 5. Only LSB
  90. #define NRF24L01_REG_TX_ADDR 0x10 //Transmit address. Used for a PTX device only
  91. #define NRF24L01_REG_RX_PW_P0 0x11
  92. #define NRF24L01_REG_RX_PW_P1 0x12
  93. #define NRF24L01_REG_RX_PW_P2 0x13
  94. #define NRF24L01_REG_RX_PW_P3 0x14
  95. #define NRF24L01_REG_RX_PW_P4 0x15
  96. #define NRF24L01_REG_RX_PW_P5 0x16
  97. #define NRF24L01_REG_FIFO_STATUS 0x17 //FIFO Status Register
  98. #define NRF24L01_REG_DYNPD 0x1C //Enable dynamic payload length
  99. #define NRF24L01_REG_FEATURE 0x1D
  100. /* Registers default values */
  101. #define NRF24L01_REG_DEFAULT_VAL_CONFIG 0x08
  102. #define NRF24L01_REG_DEFAULT_VAL_EN_AA 0x3F
  103. #define NRF24L01_REG_DEFAULT_VAL_EN_RXADDR 0x03
  104. #define NRF24L01_REG_DEFAULT_VAL_SETUP_AW 0x03
  105. #define NRF24L01_REG_DEFAULT_VAL_SETUP_RETR 0x03
  106. #define NRF24L01_REG_DEFAULT_VAL_RF_CH 0x02
  107. #define NRF24L01_REG_DEFAULT_VAL_RF_SETUP 0x0E
  108. #define NRF24L01_REG_DEFAULT_VAL_STATUS 0x0E
  109. #define NRF24L01_REG_DEFAULT_VAL_OBSERVE_TX 0x00
  110. #define NRF24L01_REG_DEFAULT_VAL_RPD 0x00
  111. #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P0_0 0xE7
  112. #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P0_1 0xE7
  113. #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P0_2 0xE7
  114. #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P0_3 0xE7
  115. #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P0_4 0xE7
  116. #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P1_0 0xC2
  117. #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P1_1 0xC2
  118. #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P1_2 0xC2
  119. #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P1_3 0xC2
  120. #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P1_4 0xC2
  121. #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P2 0xC3
  122. #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P3 0xC4
  123. #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P4 0xC5
  124. #define NRF24L01_REG_DEFAULT_VAL_RX_ADDR_P5 0xC6
  125. #define NRF24L01_REG_DEFAULT_VAL_TX_ADDR_0 0xE7
  126. #define NRF24L01_REG_DEFAULT_VAL_TX_ADDR_1 0xE7
  127. #define NRF24L01_REG_DEFAULT_VAL_TX_ADDR_2 0xE7
  128. #define NRF24L01_REG_DEFAULT_VAL_TX_ADDR_3 0xE7
  129. #define NRF24L01_REG_DEFAULT_VAL_TX_ADDR_4 0xE7
  130. #define NRF24L01_REG_DEFAULT_VAL_RX_PW_P0 0x00
  131. #define NRF24L01_REG_DEFAULT_VAL_RX_PW_P1 0x00
  132. #define NRF24L01_REG_DEFAULT_VAL_RX_PW_P2 0x00
  133. #define NRF24L01_REG_DEFAULT_VAL_RX_PW_P3 0x00
  134. #define NRF24L01_REG_DEFAULT_VAL_RX_PW_P4 0x00
  135. #define NRF24L01_REG_DEFAULT_VAL_RX_PW_P5 0x00
  136. #define NRF24L01_REG_DEFAULT_VAL_FIFO_STATUS 0x11
  137. #define NRF24L01_REG_DEFAULT_VAL_DYNPD 0x00
  138. #define NRF24L01_REG_DEFAULT_VAL_FEATURE 0x00
  139. /* Configuration register*/
  140. #define NRF24L01_MASK_RX_DR 6
  141. #define NRF24L01_MASK_TX_DS 5
  142. #define NRF24L01_MASK_MAX_RT 4
  143. #define NRF24L01_EN_CRC 3
  144. #define NRF24L01_CRCO 2
  145. #define NRF24L01_PWR_UP 1
  146. #define NRF24L01_PRIM_RX 0
  147. /* Enable auto acknowledgment*/
  148. #define NRF24L01_ENAA_P5 5
  149. #define NRF24L01_ENAA_P4 4
  150. #define NRF24L01_ENAA_P3 3
  151. #define NRF24L01_ENAA_P2 2
  152. #define NRF24L01_ENAA_P1 1
  153. #define NRF24L01_ENAA_P0 0
  154. /* Enable rx addresses */
  155. #define NRF24L01_ERX_P5 5
  156. #define NRF24L01_ERX_P4 4
  157. #define NRF24L01_ERX_P3 3
  158. #define NRF24L01_ERX_P2 2
  159. #define NRF24L01_ERX_P1 1
  160. #define NRF24L01_ERX_P0 0
  161. /* Setup of address width */
  162. #define NRF24L01_AW 0 //2 bits
  163. /* Setup of auto re-transmission*/
  164. #define NRF24L01_ARD 4 //4 bits
  165. #define NRF24L01_ARC 0 //4 bits
  166. /* RF setup register*/
  167. #define NRF24L01_PLL_LOCK 4
  168. #define NRF24L01_RF_DR_LOW 5
  169. #define NRF24L01_RF_DR_HIGH 3
  170. #define NRF24L01_RF_DR 3
  171. #define NRF24L01_RF_PWR 1 //2 bits
  172. /* General status register */
  173. #define NRF24L01_RX_DR 6
  174. #define NRF24L01_TX_DS 5
  175. #define NRF24L01_MAX_RT 4
  176. #define NRF24L01_RX_P_NO 1 //3 bits
  177. #define NRF24L01_TX_FULL 0
  178. /* Transmit observe register */
  179. #define NRF24L01_PLOS_CNT 4 //4 bits
  180. #define NRF24L01_ARC_CNT 0 //4 bits
  181. /* FIFO status*/
  182. #define NRF24L01_TX_REUSE 6
  183. #define NRF24L01_FIFO_FULL 5
  184. #define NRF24L01_TX_EMPTY 4
  185. #define NRF24L01_RX_FULL 1
  186. #define NRF24L01_RX_EMPTY 0
  187. //Dynamic length
  188. #define NRF24L01_DPL_P0 0
  189. #define NRF24L01_DPL_P1 1
  190. #define NRF24L01_DPL_P2 2
  191. #define NRF24L01_DPL_P3 3
  192. #define NRF24L01_DPL_P4 4
  193. #define NRF24L01_DPL_P5 5
  194. /* Transmitter power*/
  195. #define NRF24L01_M18DBM 0 //-18 dBm
  196. #define NRF24L01_M12DBM 1 //-12 dBm
  197. #define NRF24L01_M6DBM 2 //-6 dBm
  198. #define NRF24L01_0DBM 3 //0 dBm
  199. /* Data rates */
  200. #define NRF24L01_2MBPS 0
  201. #define NRF24L01_1MBPS 1
  202. #define NRF24L01_250KBPS 2
  203. /* Configuration */
  204. #define NRF24L01_CONFIG ((1 << NRF24L01_EN_CRC) | (0 << NRF24L01_CRCO))
  205. /* Instruction Mnemonics */
  206. #define NRF24L01_REGISTER_MASK 0x1F
  207. #define NRF24L01_READ_REGISTER_MASK(reg) (0x00 | (NRF24L01_REGISTER_MASK & reg)) //Last 5 bits will indicate reg. address
  208. #define NRF24L01_WRITE_REGISTER_MASK(reg) (0x20 | (NRF24L01_REGISTER_MASK & reg)) //Last 5 bits will indicate reg. address
  209. #define NRF24L01_R_RX_PAYLOAD_MASK 0x61
  210. #define NRF24L01_W_TX_PAYLOAD_MASK 0xA0
  211. #define NRF24L01_FLUSH_TX_MASK 0xE1
  212. #define NRF24L01_FLUSH_RX_MASK 0xE2
  213. #define NRF24L01_REUSE_TX_PL_MASK 0xE3
  214. #define NRF24L01_ACTIVATE_MASK 0x50
  215. #define NRF24L01_R_RX_PL_WID_MASK 0x60
  216. #define NRF24L01_NOP_MASK 0xFF
  217. /* Flush FIFOs */
  218. #define NRF24L01_FLUSH_TX do { NRF24L01_CSN_LOW; TM_SPI_Send(NRF24L01_SPI, NRF24L01_FLUSH_TX_MASK); NRF24L01_CSN_HIGH; } while (0)
  219. #define NRF24L01_FLUSH_RX do { NRF24L01_CSN_LOW; TM_SPI_Send(NRF24L01_SPI, NRF24L01_FLUSH_RX_MASK); NRF24L01_CSN_HIGH; } while (0)
  220. #define NRF24L01_TRANSMISSON_OK 0
  221. #define NRF24L01_MESSAGE_LOST 1
  222. #ifdef __cplusplus
  223. extern "C" {
  224. #endif
  225. #define NRF24L01_CHECK_BIT(reg, bit) (reg & (1 << bit))
  226. typedef struct {
  227. uint8_t PayloadSize; //Payload size
  228. uint8_t Channel; //Channel selected
  229. TM_NRF24L01_OutputPower_t OutPwr; //Output power
  230. TM_NRF24L01_DataRate_t DataRate; //Data rate
  231. } TM_NRF24L01_t;
  232. /**
  233. * @brief Interrupt structure
  234. */
  235. typedef union _TM_NRF24L01_IRQ_t {
  236. struct {
  237. uint8_t reserved0:4;
  238. uint8_t MaxRT:1; /*!< Set to 1 if MAX retransmissions flag is set */
  239. uint8_t DataSent:1; /*!< Set to 1 if last transmission is OK */
  240. uint8_t DataReady:1; /*!< Set to 1 if data are ready to be read */
  241. uint8_t reserved1:1;
  242. } F;
  243. uint8_t Status; /*!< NRF status register value */
  244. } TM_NRF24L01_IRQ_t;
  245. /**
  246. * @brief Data rate enumeration
  247. */
  248. typedef enum _TM_NRF24L01_DataRate_t {
  249. TM_NRF24L01_DataRate_2M = 0x00, /*!< Data rate set to 2Mbps */
  250. TM_NRF24L01_DataRate_1M, /*!< Data rate set to 1Mbps */
  251. TM_NRF24L01_DataRate_250k /*!< Data rate set to 250kbps */
  252. } TM_NRF24L01_DataRate_t;
  253. /**
  254. * @brief Output power enumeration
  255. */
  256. typedef enum _TM_NRF24L01_OutputPower_t {
  257. TM_NRF24L01_OutputPower_M18dBm = 0x00,/*!< Output power set to -18dBm */
  258. TM_NRF24L01_OutputPower_M12dBm, /*!< Output power set to -12dBm */
  259. TM_NRF24L01_OutputPower_M6dBm, /*!< Output power set to -6dBm */
  260. TM_NRF24L01_OutputPower_0dBm /*!< Output power set to 0dBm */
  261. } TM_NRF24L01_OutputPower_t;
  262. #ifdef __cplusplus
  263. }
  264. #endif
  265. #endif /* _NRF_24L01_H_ */