Advertisement
phillip_bourdon234

NRF24L01_Driver.h

Feb 28th, 2021
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.95 KB | None | 0 0
  1. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  2. //                     THIS DRIVER DOES NOT ALLOW THE USE OF THE IRQ PIN
  3. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  4.  
  5. #ifndef NRF_Driver_H_
  6. #define NRF_Driver_H_
  7.  
  8. #include  <stdint.h>
  9. #include <stdbool.h>
  10. #include "GPIO_Driver.h"
  11.  
  12. //=====================================================================================================
  13. //                                                  NRF COMMANDS
  14. //=====================================================================================================
  15.  
  16. #define R_REGISTER                  0x00  
  17. #define W_REGISTER                  0x20  
  18. #define R_RX_PAYLOAD                0x61
  19. #define W_TX_PAYLOAD                0xA0
  20. #define FLUSH_TX                    0xE1
  21. #define FLUSH_RX                    0xE2
  22. #define REUSE_TX_PL                 0xE3
  23. #define R_RX_PL_WID                 0x60
  24. #define W_ACK_PAYLOAD               0xA8
  25. #define NOP                         0xFF
  26. #define W_TX_PAYLOAD_NACK           0xB0
  27. #define ACTIVATE                    0x50
  28. #define ACTIVATE_BYTE               0x73  
  29.  
  30.  
  31. //=====================================================================================================
  32. //                              NRF REGISTERS AND SPECIFIC BITS
  33. //=====================================================================================================
  34.  
  35. #define REGISTER_MASK 0x1F
  36. #define NRF_CONFIG      0x00
  37. #define EN_AA           0x01
  38. #define EN_RXADDR       0x02
  39. #define SETUP_AW        0x03
  40. #define SETUP_RETR      0x04
  41. #define RF_CH           0x05
  42. #define RF_SETUP        0x06
  43. #define NRF_STATUS      0x07
  44. #define OBSERVE_TX      0x08
  45. #define CD              0x09
  46. #define RX_ADDR_P0      0x0A
  47. #define RX_ADDR_P1      0x0B
  48. #define RX_ADDR_P2      0x0C
  49. #define RX_ADDR_P3      0x0D
  50. #define RX_ADDR_P4      0x0E
  51. #define RX_ADDR_P5      0x0F
  52. #define TX_ADDR         0x10
  53. #define RX_PW_P0        0x11
  54. #define RX_PW_P1        0x12
  55. #define RX_PW_P2        0x13
  56. #define RX_PW_P3        0x14
  57. #define RX_PW_P4        0x15
  58. #define RX_PW_P5        0x16
  59. #define FIFO_STATUS     0x17
  60. #define DYNPD           0x1C
  61. #define FEATURE         0x1D
  62.  
  63.  
  64. //=====================================================================================================
  65. //                                                                                      NRF BITS
  66. //=====================================================================================================
  67.  
  68. //CONFIG Register : CONFIG
  69. #define MASK_RX_DR  6
  70. #define MASK_TX_DS  5
  71. #define MASK_MAX_RT 4
  72. #define EN_CRC      3
  73. #define CRCO        2
  74. #define PWR_UP      1
  75. #define PRIM_RX     0
  76.  
  77. //ENABLE AUTO ACK Register : EN_AA
  78. #define ENAA_P5     5
  79. #define ENAA_P4     4
  80. #define ENAA_P3     3
  81. #define ENAA_P2     2
  82. #define ENAA_P1     1
  83. #define ENAA_P0     0
  84.  
  85. //Enabled RX Addresses Register : EN_RXADDR
  86. #define ERX_P5      5
  87. #define ERX_P4      4
  88. #define ERX_P3      3
  89. #define ERX_P2      2
  90. #define ERX_P1      1
  91. #define ERX_P0      0
  92.  
  93. //Setup of Address Widths Register : SETUP_AW
  94. #define AW          0
  95.  
  96. //Setup of Automatic Retransmission Register : SETUP_RETR
  97. #define ARD         4
  98. #define ARC         0
  99.  
  100. //RF Channel Register :RF_CH
  101. #define RF_CH_BITS  0
  102.  
  103. //RF Setup Register : RF_SETUP
  104. #define PLL_LOCK    4
  105. #define RF_DR       3
  106. #define RF_PWR      6
  107. #define LNA_HCURR   0
  108.  
  109. //STATUS register : STATUS
  110. #define RX_DR       0x40
  111. #define TX_DS       5
  112. #define MAX_RT      4
  113. #define RX_P_NO     1
  114. #define TX_FULL     0
  115.  
  116. //Transmit observe register : OBSERVE_TX
  117. #define PLOS_CNT    4
  118. #define ARC_CNT     0
  119. // CD
  120. #define CD_BIT      0
  121.  
  122. //FIFO Status Register : FIFO_STATUS
  123. #define TX_REUSE    6
  124. #define FIFO_FULL   5
  125. #define TX_EMPTY    4
  126. #define RX_FULL     1
  127. #define RX_EMPTY    0
  128.  
  129. //Enable dynamic payload length Register : DYNPD
  130. #define DPL_P5      5
  131. #define DPL_P4      4
  132. #define DPL_P3      3
  133. #define DPL_P2      2
  134. #define DPL_P1      1
  135. #define DPL_P0      0
  136.  
  137. //Feature Register : FEATURE
  138. #define EN_DPL      2
  139. #define EN_ACK_PAY  1
  140. #define EN_DYN_ACK  0
  141.  
  142. #define PIPE_0  0x00
  143. #define PIPE_1  0x01
  144. #define PIPE_2  0x02
  145. #define PIPE_3  0x03
  146. #define PIPE_4  0x04
  147. #define PIPE_5  0x05
  148.  
  149. //offsets that help with writing to registers
  150. #define RX_ADDR_OFFSET  0x0A
  151. #define RX_PW_OFFSET    0x11
  152.  
  153. //SETUP_AW byte codes
  154. #define THREE_BYTES 0b01
  155. #define FOUR_BYTES  0b10
  156. #define FIVE_BYTES  0b11
  157. #define ENCODING_SCHEME_1_BYTE  0x00
  158. #define ENCODING_SCHEME_2_BYTE  0x01
  159. #define DUMMYBYTE  0xF1
  160.  
  161.  
  162.  
  163.  
  164. //*****************************************************************************************************
  165. void nrf_init_pins(GPIO_TypeDef *CE_port, uint32_t CE_pin, GPIO_TypeDef *CSN_port, uint32_t CSN_pin);
  166.  
  167. //      The nrf_init_pins function configures the CE and CSN pins of the NRF module and inits the SPI
  168. //*****************************************************************************************************
  169.  
  170.  
  171.  
  172.  
  173. //*****************************************************************************************************
  174. void nrf_write_reg(char reg, char data);
  175. //      The nrf_write_reg function allows you to write to an 8 bit register in the NRF24 module
  176. //*****************************************************************************************************
  177.  
  178.  
  179. //*****************************************************************************************************
  180. uint8_t nrf_read_reg(char reg);
  181. //      The nrf_read_reg function returns the bits of an 8 bit register in the NRF24 module
  182. //*****************************************************************************************************
  183.  
  184.  
  185.  
  186. //*****************************************************************************************************
  187. void nrf_init_transmiter(uint32_t addr_high, uint8_t addr_low, uint8_t channel, char en_auot_ack, char en_dpl);
  188.  
  189. //      The nrf_init_transmiter function writes the appropriate bits to the NRF24 for transmit mode.
  190. //    The user must pass into the function the last 4 bits of the address to addr_high, and the first bit of the
  191. //      address to addr_low. The user must also send their desired channel, a 1 if they would like to
  192. //    enable auto ack, and a 1 to enable dynamic payload. The rest of the settings are fixed as follows:
  193. //     
  194. //    CONFIG register = 0x0A (turns the nrf on, dissables interrupts, and sets the nrf into TX mode)
  195. //      EN_AA register = 0x3F (enables auto ack for all the pipes by default)
  196. //      SETUP_AW register = 0x03 (sets address width to 5)
  197. //      SETUP_RETR register = 0xFF (retransmits the data up to 15 times on a delay of 4000uS)
  198. //      RF_SETUP register = 0x0F (sets the air data rate to 2Mbps and the output power to 0dBm)
  199. //
  200. //*****************************************************************************************************
  201.  
  202.  
  203.  
  204. //*****************************************************************************************************
  205. void nrf_init_reciever(uint8_t pipe, uint32_t addr_high, uint8_t addr_low, uint8_t channel, char en_auto_ack, char en_dpl, uint8_t dpl_w);
  206.  
  207. //      The nrf_init_reciever function writes the appropriate bits to the NRF24 for recieve mode.
  208. //      The user must pass into the function their desired pipe ([1,5], pipe 0 is reserved for TX mode).
  209. //      The user must also pass the last 4 bits of the address to addr_high, and the first bit of the
  210. //      address to addr_low. The user must also send their desired channel, a 1 if they would like to
  211. //    enable auto ack, and a 1 to enable dynamic payload. The rest of the settings are fixed as follows:
  212. //
  213. //    CONFIG register = 0x7B (turns the nrf on, dissables interrupts, and sets the nrf into RX mode)
  214. //      EN_AA register = 0x3F (enables auto ack for all the pipes by default)
  215. //      SETUP_AW register = 0x03 (sets address width to 5)
  216. //      SETUP_RETR register = 0xFF (retransmits the data up to 15 times on a delay of 4000uS)
  217. //      RF_SETUP register = 0x0F (sets the air data rate to 2Mbps and the output power to 0dBm)
  218. //
  219. //*****************************************************************************************************
  220.  
  221.  
  222. //****************************************************************************************************
  223. void nrf_write_reg_specific_bit(uint8_t reg, uint8_t bit, uint8_t value);
  224.  
  225. //      The nrf_write_reg_specific_bit allows the user to change just one bit in their register of choice
  226. //****************************************************************************************************
  227.  
  228.  
  229. //****************************************************************************************************
  230. void nrf_read_reg_multiple_bytes(uint8_t reg, uint8_t *data_buff);
  231.  
  232. //      The nrf_read_reg_multiple_bytes allows the user to read a reister with more than one byte
  233. //      Such as the address registers
  234. //****************************************************************************************************
  235.  
  236.  
  237.  
  238. //****************************************************************************************************
  239. void nrf_cmd_listen(void);
  240.                                                            
  241. //      The nrf_cmd_listen function simply pulls the CE pin high
  242. //****************************************************************************************************                                                             
  243.  
  244.  
  245.  
  246. //****************************************************************************************************
  247. uint8_t nrf_cmd_get_status(void);
  248.  
  249. //      The nrf_cmd_get_status function returns the value in the status register
  250. //****************************************************************************************************
  251.  
  252.  
  253.  
  254. //****************************************************************************************************
  255. void nrf_cmd_clear_interrupts(void);
  256.                                                    
  257. //      The nrf_cmd_clear_interrupts function clears the interrupts in the NRF module
  258. //****************************************************************************************************
  259.  
  260.  
  261.  
  262.  
  263. //****************************************************************************************************
  264. void nrf_cmd_flush_tx(void);
  265.  
  266. //      The nrf_cmd_flush_tx function clears the TX_FIFO value
  267. //****************************************************************************************************
  268.  
  269.  
  270.  
  271. //****************************************************************************************************
  272. void nrf_cmd_flush_rx(void);
  273.  
  274. //      The nrf_cmd_flush_rx function clears the RX_FIFO register
  275. //****************************************************************************************************
  276.  
  277.  
  278.  
  279. //****************************************************************************************************
  280. void nrf_cmd_act_as_RX(void);
  281.                                                            
  282. //      The nrf_cmd_act_as_RX function puts the NRF in RX mode
  283. //****************************************************************************************************
  284.  
  285.  
  286.  
  287. //****************************************************************************************************
  288. void nrf_cmd_act_as_TX(void);
  289.  
  290. //      The nrf_cmd_act_as_TX function puts the NRF in TX mode
  291. //****************************************************************************************************
  292.  
  293.  
  294.  
  295. //****************************************************************************************************
  296. void nrf_cmd_activate(void);
  297.  
  298. //      The nrf_cmd_activate function enables the use of the following features:
  299. //          • R_RX_PL_WID
  300. //          • W_ACK_PAYLOAD
  301. //          • W_TX_PAYLOAD_NOACK
  302. //    Note that this function only enables the use of these features.
  303. //****************************************************************************************************
  304.  
  305.  
  306.  
  307. //****************************************************************************************************
  308. void nrf_set_tx_addr(uint32_t addr_high, uint8_t addr_low, uint8_t auto_ack);
  309.  
  310. //      The nrf_set_tx_addr function sets the address for the transmitter given the last 4
  311. //    bytes (addr_high) and the first byte (addr_low) of the address. You can also send
  312. //      a 1 for the auto_ack to enable auto ack on the transmitter.
  313. //****************************************************************************************************
  314.  
  315.  
  316. //****************************************************************************************************
  317. void nrf_set_rx_addr(uint8_t rx_pipe, uint32_t addr_high, uint8_t addr_low);
  318.  
  319. //      The nrf_set_rx_addr function sets the address for the reciever given the last 4
  320. //      bytes (addr_high) and the first byte (addr_low) of the address. You must also specify
  321. //      the pipe of the reciever [1-5]
  322. //****************************************************************************************************
  323.  
  324.  
  325.  
  326. //****************************************************************************************************
  327. void nrf_read_rx_payload(uint8_t *payload);
  328.  
  329. //      The nrf_read_rx_payload function reads the paylod from the RX and puts the value into
  330. //      a pointer
  331. //****************************************************************************************************
  332.  
  333.  
  334.  
  335. //****************************************************************************************************
  336. void nrf_write_tx_payload(uint8_t *data, uint8_t len);
  337.  
  338. //      The nrf_write_tx_payload function writes the given payload [*data] to the transmitter
  339. //****************************************************************************************************
  340.  
  341.  
  342.  
  343. //****************************************************************************************************
  344. char nrf_send(uint8_t *data, uint8_t length);
  345.  
  346. //      The nrf_send function sends the payload [*data] to a reciever. Returns a 1 if succssfully sent
  347. //****************************************************************************************************
  348.  
  349.  
  350.  
  351. //****************************************************************************************************
  352. char nrf_data_available(void);
  353.  
  354. //      The nrfh_data_available function returns a 1 if there is a payload in the RX
  355. //****************************************************************************************************
  356.  
  357.  
  358.  
  359. //****************************************************************************************************
  360. void nrf_read(uint8_t *dataBuffer);
  361.  
  362. //      The nrf_read function reads the payload from the RX and puts the value into a pointer
  363. //****************************************************************************************************
  364.  
  365.  
  366.  
  367. #endif
  368.  
  369.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement