Advertisement
Mukmin039

nrfl .h

Jul 21st, 2022 (edited)
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.30 KB | None | 0 0
  1. /*
  2.  * File:   nrf24_lib.h
  3.  * Author: Noyel Seth (noyelseth@gamil.com)
  4.  */
  5. #ifndef NRF24_LIB_H
  6. #define NRF24_LIB_H
  7.  
  8. #include "mcc_generated_files/mcc.h"
  9.  
  10. /******************************************************************************/
  11. // SPI and GPIO Helper Function.
  12. /******************************************************************************/
  13.  
  14. #define NRF24L01_CSN_H()            nRF24_CSN_SetHigh()
  15. #define NRF24L01_CSN_L()            nRF24_CSN_SetLow()
  16. #define NRF24L01_CSN_SetOutput()    nRF24_CSN_SetDigitalOutput()
  17. #define NRF24L01_CSN_SetInput()     nRF24_CSN_SetDigitalInput()
  18.  
  19. #define NRF24L01_CE_H()             nRF24_CE_SetHigh()
  20. #define NRF24L01_CE_L()             nRF24_CE_SetLow()
  21. #define NRF24L01_CE_SetOutput()     nRF24_CE_SetDigitalOutput()
  22. #define NRF24L01_CE_SetInput()      nRF24_CE_SetDigitalInput()
  23.  
  24. #define SPI_WRITE_BYTE(dt)          SPI1_ExchangeByte(dt)
  25. #define SPI_READ_BYTE(dt)           SPI1_ExchangeByte(dt)
  26. #define SPI_INIT()                  SPI1_Open(SPI1_DEFAULT)
  27.  
  28. /* Bit Mnemonics */
  29. /******************************************************************************/
  30.  
  31.  
  32. #define PAYLOAD_BYTES      32 // Sets the bytes to send or read.
  33. #define EN_DPL      2
  34. #define EN_ACK_PAY  1
  35. #define _BV(x) (1<<(x))
  36. #define IF_SERIAL_DEBUG(x) ({x;})
  37. #define DPL_P0      0        
  38. #define DPL_P1      1
  39. #define MIN(x, y) (((x) < (y)) ? (x) : (y))
  40. #define ARD         4
  41. #define ARC         0
  42.  
  43. // This data type sets the address data pipe 0.
  44. //unsigned char ADDRESS_DATA_RXPIPE0[5] = {0xCLL,0xCLL,0xCLL,0xCLL,0xCLL};
  45. //unsigned char ADDRESS_DATA_TXPIPE0[5] = {0xCLL,0xCLL,0xCLL,0xCLL,0xCLL};
  46. //unsigned char ADDRESS_DATA_RXPIPE0[5] = {0xe1,0xe1,0xe1,0xe1,0xe1};
  47. //unsigned char ADDRESS_DATA_TXPIPE0[5] = {0xe1,0xe1,0xe1,0xe1,0xe1};
  48. unsigned char ADDRESS_DATA_TXPIPE0[5] = {0xE7,0xE7,0xE7,0xE7,0xE7};
  49. unsigned char ADDRESS_DATA_RXPIPE1[5] = {0xE7,0xE7,0xE7,0xE7,0xE7};
  50. unsigned char ADDRESS_DATA_RXPIPE0[5] = {0xC2,0xC2,0xC2,0xC2,0xC2};
  51. unsigned char ADDRESS_DATA_RXPIPE2[5] = {0xC3,0xC3,0xC3,0xC3,0xC3};
  52. unsigned char ADDRESS_DATA_RXPIPE3[5] = {0xC4,0xC4,0xC4,0xC4,0xC4};
  53. unsigned char ADDRESS_DATA_RXPIPE4[5] = {0xC5,0xC5,0xC5,0xC5,0xC5};
  54.  
  55. typedef enum{
  56.     NRF24_INIT_OK = 0,
  57.     NRF24_INIT_FAILED = 1
  58. }NRF24_INIT_STATUS;
  59.  
  60. /******************************************************************************/
  61. // NRF24L01 Operation Modes
  62. /******************************************************************************/
  63. typedef enum {
  64.     RX_MODE = 1,
  65.     TX_MODE = 2
  66. }NRF24_OPERATION_MODE;
  67.  
  68.  
  69. /******************************************************************************/
  70. // Register Map.
  71. /******************************************************************************/
  72.  
  73. #define R_REGISTER          0x00  
  74. #define W_REGISTER          0x20  
  75. #define R_RX_PAYLOAD        0x61  
  76. #define W_TX_PAYLOAD        0xA0  
  77. #define FLUSH_TX            0xE1  
  78. #define FLUSH_RX            0xE2  
  79. #define REUSE_TX_PL         0xE3
  80. #define R_RX_PL_WID         0x60
  81. #define ACTIVATE            0x73
  82. #define W_ACK_PAYLOAD       0xA8
  83.  
  84.  
  85. #define NRF24_MEM_CONFIG              0x00  
  86. #define NRF24_MEM_EN_AA               0x01  
  87. #define NRF24_MEM_EN_RXADDR           0x02  //enable data pipe 0 CLL
  88. #define NRF24_MEM_SETUP_AW            0x03  
  89. #define NRF24_MEM_SETUP_RETR          0x04  
  90. #define NRF24_MEM_RF_CH               0x05  
  91. #define NRF24_MEM_RF_SETUP            0x06  
  92. #define NRF24_MEM_STATUSS             0x07  
  93. #define NRF24_MEM_OBSERVE_TX          0x08  
  94. #define NRF24_MEM_CD                  0x09  
  95. #define NRF24_MEM_RX_ADDR_P0          0x0A  //data pipe 0 CLL
  96. #define NRF24_MEM_RX_ADDR_P1          0x0B  
  97. #define NRF24_MEM_RX_ADDR_P2          0x0C  
  98. #define NRF24_MEM_RX_ADDR_P3          0x0D  
  99. #define NRF24_MEM_RX_ADDR_P4          0x0E  
  100. #define NRF24_MEM_RX_ADDR_P5          0x0F  
  101. #define NRF24_MEM_TX_ADDR             0x10  //data pipe 0
  102. #define NRF24_MEM_RX_PW_P0            0x11  //bytes in the payload  32
  103. #define NRF24_MEM_RX_PW_P1            0x12
  104. #define NRF24_MEM_RX_PW_P2            0x13
  105. #define NRF24_MEM_RX_PW_P3            0x14
  106. #define NRF24_MEM_RX_PW_P4            0x15
  107. #define NRF24_MEM_RX_PW_P5            0x16
  108. #define NRF24_MEM_FIFO_STATUS         0x17
  109. #define NRF24_MEM_CMD_NOP             0xFF // No operation (used for reading status register)
  110. #define NRF24_MEM_FEATURE             0x1D
  111. #define NRF24_MEM_REGISTER_MASK     0x1F
  112. #define NRF24_MEM_DYNPD             0x1C
  113.  
  114.  
  115. /******************************************************************************/
  116. // NRF24L01 Functions.
  117. /******************************************************************************/
  118.  
  119. /**
  120.  * @brief  Write or update value into the Memory Address of NRF24L01
  121.  *
  122.  * @param[in]   mnemonic_addr NRF24L01 Memory Address
  123.  * @param[in]   value NRF24L01 Memory Address's value
  124.  *
  125.  */
  126. void nrf24_write_register(unsigned char mnemonic_addr, unsigned char value);
  127.  
  128. /**
  129.  * @brief  Read value from the Memory Address of NRF24L01
  130.  *
  131.  * @param[in]   mnemonic_addr NRF24L01 Memory Address
  132.  *
  133.  * @return      current value of the read NRF24L01 Memory Address
  134.  *
  135.  */
  136. unsigned char nrf24_read_register(unsigned char mnemonic_addr);
  137.  
  138. /**
  139.  * @brief  Write or update buffer into the Memory Address of NRF24L01
  140.  *
  141.  * @param[in]   mnemonic_addr NRF24L01 Memory Address
  142.  * @param[in]   buffer write buffer data for NRF24L01 Memory Address's value
  143.  * @param[in]   bytes size of the write buffer
  144.  *
  145.  */
  146. void nrf24_write_buff(unsigned char mnemonic_addr, unsigned char *buffer, unsigned char bytes);
  147.  
  148. /**
  149.  * @brief  Read buffer data from the Memory Address of NRF24L01
  150.  *
  151.  * @param[in]   mnemonic_addr NRF24L01 Memory Address
  152.  * @param[out]  buffer read buffer for read NRF24L01 Memory Address's data
  153.  * @param[in]   bytes size of the read buffer
  154.  *
  155.  */
  156. void nrf24_read_buff(unsigned char mnemonic_addr, unsigned char *buffer, unsigned char bytes);
  157.  
  158. /**
  159.  * @brief  Initialize NRF24L01 in Tx Operation or Rx Operation Mode
  160.  *
  161.  * @param[in]   mode NRF24L01 Tx/Rx Operation Mode
  162.  * @param[in]   rf_channel NRF24L01 RF channel - radio frequency channel, value from 0 to 127
  163.  *
  164.  * @return
  165.  *          NRF24_INIT_OK succesfully Initialize the nRF24
  166.  *          NRF24_INIT_FAILED failed Initialize the nRF24
  167.  *
  168.  * @Note: NRF24L01 frequency will be (2400 + channel)MHz
  169.  *
  170.  */
  171. NRF24_INIT_STATUS nrf24_rf_init(unsigned char mode, unsigned char rf_channel);
  172.  
  173. /**
  174.  * @brief  Set the NRF24L01 Operation Mode Tx/Rx
  175.  *
  176.  * @param[in]   mode NRF24L01 Tx/Rx Operation Mode
  177.  *
  178.  */
  179. void nrf24_set_rf_mode(NRF24_OPERATION_MODE mode);
  180.  
  181. /**
  182.  * @brief  Send Payload to NRF24L01
  183.  *
  184.  * @param[in]   buffer NRF24L01 Send Payload buffer pointer
  185.  *
  186.  */
  187. void nrf24_send_rf_data(unsigned char *buffer);
  188.  
  189. /**
  190.  * @brief  Receive data availability check
  191.  *
  192.  * @return  1 if data present
  193.  *          0 if data not present
  194.  *
  195.  */
  196. unsigned char nrf24_is_rf_data_available(void);
  197.  
  198. /**
  199.  * @brief  Read payload from NRF24L01
  200.  *
  201.  * @param[out]  buffer pointer buffer for receive payload
  202.  *
  203.  */
  204. void nrf24_read_rf_data(unsigned char *buffer);
  205.  
  206. /**
  207.  * @brief  Set NRF24L01 RF Channel Frequency
  208.  *
  209.  * @param[in]   rf_channel NRF24L01 RF channel - radio frequency channel, value from 0 to 127
  210.  *
  211.  * @Note: NRF24L01 frequency will be (2400 + rf_channel)GHz
  212.  *
  213.  */
  214. void nrf24_set_channel_frq(unsigned char rf_channel);
  215.  
  216. /**
  217.  * @brief  Get NRF24L01 RF Channel Frequency
  218.  *
  219.  * @return rf_channel NRF24L01 RF channel - radio frequency channel, value from 0 to 127
  220.  *
  221.  * @Note: For actual NRF24L01 frequency will be (2400 + rf_channel)GHz
  222.  *
  223.  */
  224. unsigned char nrf24_get_channel_frq(void);
  225.  
  226. /**
  227.  * @brief  Set NRF24L01 into StandBy-I
  228.  *
  229.  */
  230. void nrf24_standby_I(void);
  231.  
  232. /**
  233.  * @brief  Flush the Tx an Rx
  234.  *
  235.  */
  236. void nrf24_flush_tx_rx(void);
  237.  
  238. /**
  239.  * @brief  Print The NRF24L01 Current Configuration
  240.  *
  241.  */
  242. void nrf24_printf_rf_config(void);
  243.  
  244. /*
  245. unsigned char getDynamicPayloadSize(void);
  246.  /**
  247.    * Enable custom payloads on the acknowledge packets
  248.    *
  249.    * Ack payloads are a handy way to return data back to senders without
  250.    * manually changing the radio modes on both units.
  251.    *
  252.    * @warning Do note, multicast payloads will not trigger ack payloads.
  253.    *
  254.    * @see examples/pingpair_pl/pingpair_pl.pde
  255.    */
  256. /*
  257. void enableAckPayload(void);
  258.  
  259.   /**
  260.    * Enable dynamically-sized payloads
  261.    *
  262.    * This way you don't always have to send large packets just to send them
  263.    * once in a while.  This enables dynamic payloads on ALL pipes.
  264.    *
  265.    * @see examples/pingpair_pl/pingpair_dyn.pde
  266.    */
  267.  
  268.  
  269.  void toggle_features(void);
  270.   /**@}*/
  271.  /*
  272.  void enableDynamicPayloads(void);
  273.  
  274.   /**
  275.    * Determine whether the hardware is an nRF24L01+ or not.
  276.    *
  277.    * @return true if the hardware is nRF24L01+ (or compatible) and false
  278.    * if its not.
  279.    */
  280.  /*
  281.   void openWritingPipe(unsigned long long address);
  282.  
  283.   /**
  284.    * Open a pipe for reading
  285.    *
  286.    * Up to 6 pipes can be open for reading at once.  Open all the
  287.    * reading pipes, and then call startListening().
  288.    *
  289.    * @see openWritingPipe
  290.    *
  291.    * @warning Pipes 1-5 should share the first 32 bits.
  292.    * Only the least significant byte should be unique, e.g.
  293.    * @code
  294.    *   openReadingPipe(1,0xF0F0F0F0AA);
  295.    *   openReadingPipe(2,0xF0F0F0F066);
  296.    * @endcode
  297.    *
  298.    * @warning Pipe 0 is also used by the writing pipe.  So if you open
  299.    * pipe 0 for reading, and then startListening(), it will overwrite the
  300.    * writing pipe.  Ergo, do an openWritingPipe() again before write().
  301.    *
  302.    * @warning Pipe 0 is also used as the multicast address pipe. Pipe 1
  303.    * is the unicast pipe address.
  304.    *
  305.    * @todo Enforce the restriction that pipes 1-5 must share the top 32 bits
  306.    *
  307.    * @param number Which pipe# to open, 0-5.
  308.    * @param address The 40-bit address of the pipe to open.
  309.    */
  310.   void writeAckPayload(unsigned char pipe, const void* buf, unsigned char len);
  311.  
  312.   /**
  313.    * Determine if an ack payload was received in the most recent call to
  314.    * write().
  315.    *
  316.    * Call read() to retrieve the ack payload.
  317.    *
  318.    * @warning Calling this function clears the internal flag which indicates
  319.    * a payload is available.  If it returns true, you must read the packet
  320.    * out as the very next interaction with the radio, or the results are
  321.    * undefined.
  322.    *
  323.    * @return True if an ack payload is available.
  324.    */
  325.  
  326.  
  327. #endif  /* NRF24_LIB_H */
  328.  
  329.  
  330.  
  331.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement