Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: nrf24_lib.h
- * Author: Noyel Seth (noyelseth@gamil.com)
- */
- #ifndef NRF24_LIB_H
- #define NRF24_LIB_H
- #include "mcc_generated_files/mcc.h"
- /******************************************************************************/
- // SPI and GPIO Helper Function.
- /******************************************************************************/
- #define NRF24L01_CSN_H() nRF24_CSN_SetHigh()
- #define NRF24L01_CSN_L() nRF24_CSN_SetLow()
- #define NRF24L01_CSN_SetOutput() nRF24_CSN_SetDigitalOutput()
- #define NRF24L01_CSN_SetInput() nRF24_CSN_SetDigitalInput()
- #define NRF24L01_CE_H() nRF24_CE_SetHigh()
- #define NRF24L01_CE_L() nRF24_CE_SetLow()
- #define NRF24L01_CE_SetOutput() nRF24_CE_SetDigitalOutput()
- #define NRF24L01_CE_SetInput() nRF24_CE_SetDigitalInput()
- #define SPI_WRITE_BYTE(dt) SPI1_ExchangeByte(dt)
- #define SPI_READ_BYTE(dt) SPI1_ExchangeByte(dt)
- #define SPI_INIT() SPI1_Open(SPI1_DEFAULT)
- /* Bit Mnemonics */
- /******************************************************************************/
- #define PAYLOAD_BYTES 32 // Sets the bytes to send or read.
- #define EN_DPL 2
- #define EN_ACK_PAY 1
- #define _BV(x) (1<<(x))
- #define IF_SERIAL_DEBUG(x) ({x;})
- #define DPL_P0 0
- #define DPL_P1 1
- #define MIN(x, y) (((x) < (y)) ? (x) : (y))
- #define ARD 4
- #define ARC 0
- // This data type sets the address data pipe 0.
- //unsigned char ADDRESS_DATA_RXPIPE0[5] = {0xCLL,0xCLL,0xCLL,0xCLL,0xCLL};
- //unsigned char ADDRESS_DATA_TXPIPE0[5] = {0xCLL,0xCLL,0xCLL,0xCLL,0xCLL};
- //unsigned char ADDRESS_DATA_RXPIPE0[5] = {0xe1,0xe1,0xe1,0xe1,0xe1};
- //unsigned char ADDRESS_DATA_TXPIPE0[5] = {0xe1,0xe1,0xe1,0xe1,0xe1};
- unsigned char ADDRESS_DATA_TXPIPE0[5] = {0xE7,0xE7,0xE7,0xE7,0xE7};
- unsigned char ADDRESS_DATA_RXPIPE1[5] = {0xE7,0xE7,0xE7,0xE7,0xE7};
- unsigned char ADDRESS_DATA_RXPIPE0[5] = {0xC2,0xC2,0xC2,0xC2,0xC2};
- unsigned char ADDRESS_DATA_RXPIPE2[5] = {0xC3,0xC3,0xC3,0xC3,0xC3};
- unsigned char ADDRESS_DATA_RXPIPE3[5] = {0xC4,0xC4,0xC4,0xC4,0xC4};
- unsigned char ADDRESS_DATA_RXPIPE4[5] = {0xC5,0xC5,0xC5,0xC5,0xC5};
- typedef enum{
- NRF24_INIT_OK = 0,
- NRF24_INIT_FAILED = 1
- }NRF24_INIT_STATUS;
- /******************************************************************************/
- // NRF24L01 Operation Modes
- /******************************************************************************/
- typedef enum {
- RX_MODE = 1,
- TX_MODE = 2
- }NRF24_OPERATION_MODE;
- /******************************************************************************/
- // Register Map.
- /******************************************************************************/
- #define R_REGISTER 0x00
- #define W_REGISTER 0x20
- #define R_RX_PAYLOAD 0x61
- #define W_TX_PAYLOAD 0xA0
- #define FLUSH_TX 0xE1
- #define FLUSH_RX 0xE2
- #define REUSE_TX_PL 0xE3
- #define R_RX_PL_WID 0x60
- #define ACTIVATE 0x73
- #define W_ACK_PAYLOAD 0xA8
- #define NRF24_MEM_CONFIG 0x00
- #define NRF24_MEM_EN_AA 0x01
- #define NRF24_MEM_EN_RXADDR 0x02 //enable data pipe 0 CLL
- #define NRF24_MEM_SETUP_AW 0x03
- #define NRF24_MEM_SETUP_RETR 0x04
- #define NRF24_MEM_RF_CH 0x05
- #define NRF24_MEM_RF_SETUP 0x06
- #define NRF24_MEM_STATUSS 0x07
- #define NRF24_MEM_OBSERVE_TX 0x08
- #define NRF24_MEM_CD 0x09
- #define NRF24_MEM_RX_ADDR_P0 0x0A //data pipe 0 CLL
- #define NRF24_MEM_RX_ADDR_P1 0x0B
- #define NRF24_MEM_RX_ADDR_P2 0x0C
- #define NRF24_MEM_RX_ADDR_P3 0x0D
- #define NRF24_MEM_RX_ADDR_P4 0x0E
- #define NRF24_MEM_RX_ADDR_P5 0x0F
- #define NRF24_MEM_TX_ADDR 0x10 //data pipe 0
- #define NRF24_MEM_RX_PW_P0 0x11 //bytes in the payload 32
- #define NRF24_MEM_RX_PW_P1 0x12
- #define NRF24_MEM_RX_PW_P2 0x13
- #define NRF24_MEM_RX_PW_P3 0x14
- #define NRF24_MEM_RX_PW_P4 0x15
- #define NRF24_MEM_RX_PW_P5 0x16
- #define NRF24_MEM_FIFO_STATUS 0x17
- #define NRF24_MEM_CMD_NOP 0xFF // No operation (used for reading status register)
- #define NRF24_MEM_FEATURE 0x1D
- #define NRF24_MEM_REGISTER_MASK 0x1F
- #define NRF24_MEM_DYNPD 0x1C
- /******************************************************************************/
- // NRF24L01 Functions.
- /******************************************************************************/
- /**
- * @brief Write or update value into the Memory Address of NRF24L01
- *
- * @param[in] mnemonic_addr NRF24L01 Memory Address
- * @param[in] value NRF24L01 Memory Address's value
- *
- */
- void nrf24_write_register(unsigned char mnemonic_addr, unsigned char value);
- /**
- * @brief Read value from the Memory Address of NRF24L01
- *
- * @param[in] mnemonic_addr NRF24L01 Memory Address
- *
- * @return current value of the read NRF24L01 Memory Address
- *
- */
- unsigned char nrf24_read_register(unsigned char mnemonic_addr);
- /**
- * @brief Write or update buffer into the Memory Address of NRF24L01
- *
- * @param[in] mnemonic_addr NRF24L01 Memory Address
- * @param[in] buffer write buffer data for NRF24L01 Memory Address's value
- * @param[in] bytes size of the write buffer
- *
- */
- void nrf24_write_buff(unsigned char mnemonic_addr, unsigned char *buffer, unsigned char bytes);
- /**
- * @brief Read buffer data from the Memory Address of NRF24L01
- *
- * @param[in] mnemonic_addr NRF24L01 Memory Address
- * @param[out] buffer read buffer for read NRF24L01 Memory Address's data
- * @param[in] bytes size of the read buffer
- *
- */
- void nrf24_read_buff(unsigned char mnemonic_addr, unsigned char *buffer, unsigned char bytes);
- /**
- * @brief Initialize NRF24L01 in Tx Operation or Rx Operation Mode
- *
- * @param[in] mode NRF24L01 Tx/Rx Operation Mode
- * @param[in] rf_channel NRF24L01 RF channel - radio frequency channel, value from 0 to 127
- *
- * @return
- * NRF24_INIT_OK succesfully Initialize the nRF24
- * NRF24_INIT_FAILED failed Initialize the nRF24
- *
- * @Note: NRF24L01 frequency will be (2400 + channel)MHz
- *
- */
- NRF24_INIT_STATUS nrf24_rf_init(unsigned char mode, unsigned char rf_channel);
- /**
- * @brief Set the NRF24L01 Operation Mode Tx/Rx
- *
- * @param[in] mode NRF24L01 Tx/Rx Operation Mode
- *
- */
- void nrf24_set_rf_mode(NRF24_OPERATION_MODE mode);
- /**
- * @brief Send Payload to NRF24L01
- *
- * @param[in] buffer NRF24L01 Send Payload buffer pointer
- *
- */
- void nrf24_send_rf_data(unsigned char *buffer);
- /**
- * @brief Receive data availability check
- *
- * @return 1 if data present
- * 0 if data not present
- *
- */
- unsigned char nrf24_is_rf_data_available(void);
- /**
- * @brief Read payload from NRF24L01
- *
- * @param[out] buffer pointer buffer for receive payload
- *
- */
- void nrf24_read_rf_data(unsigned char *buffer);
- /**
- * @brief Set NRF24L01 RF Channel Frequency
- *
- * @param[in] rf_channel NRF24L01 RF channel - radio frequency channel, value from 0 to 127
- *
- * @Note: NRF24L01 frequency will be (2400 + rf_channel)GHz
- *
- */
- void nrf24_set_channel_frq(unsigned char rf_channel);
- /**
- * @brief Get NRF24L01 RF Channel Frequency
- *
- * @return rf_channel NRF24L01 RF channel - radio frequency channel, value from 0 to 127
- *
- * @Note: For actual NRF24L01 frequency will be (2400 + rf_channel)GHz
- *
- */
- unsigned char nrf24_get_channel_frq(void);
- /**
- * @brief Set NRF24L01 into StandBy-I
- *
- */
- void nrf24_standby_I(void);
- /**
- * @brief Flush the Tx an Rx
- *
- */
- void nrf24_flush_tx_rx(void);
- /**
- * @brief Print The NRF24L01 Current Configuration
- *
- */
- void nrf24_printf_rf_config(void);
- /*
- unsigned char getDynamicPayloadSize(void);
- /**
- * Enable custom payloads on the acknowledge packets
- *
- * Ack payloads are a handy way to return data back to senders without
- * manually changing the radio modes on both units.
- *
- * @warning Do note, multicast payloads will not trigger ack payloads.
- *
- * @see examples/pingpair_pl/pingpair_pl.pde
- */
- /*
- void enableAckPayload(void);
- /**
- * Enable dynamically-sized payloads
- *
- * This way you don't always have to send large packets just to send them
- * once in a while. This enables dynamic payloads on ALL pipes.
- *
- * @see examples/pingpair_pl/pingpair_dyn.pde
- */
- void toggle_features(void);
- /**@}*/
- /*
- void enableDynamicPayloads(void);
- /**
- * Determine whether the hardware is an nRF24L01+ or not.
- *
- * @return true if the hardware is nRF24L01+ (or compatible) and false
- * if its not.
- */
- /*
- void openWritingPipe(unsigned long long address);
- /**
- * Open a pipe for reading
- *
- * Up to 6 pipes can be open for reading at once. Open all the
- * reading pipes, and then call startListening().
- *
- * @see openWritingPipe
- *
- * @warning Pipes 1-5 should share the first 32 bits.
- * Only the least significant byte should be unique, e.g.
- * @code
- * openReadingPipe(1,0xF0F0F0F0AA);
- * openReadingPipe(2,0xF0F0F0F066);
- * @endcode
- *
- * @warning Pipe 0 is also used by the writing pipe. So if you open
- * pipe 0 for reading, and then startListening(), it will overwrite the
- * writing pipe. Ergo, do an openWritingPipe() again before write().
- *
- * @warning Pipe 0 is also used as the multicast address pipe. Pipe 1
- * is the unicast pipe address.
- *
- * @todo Enforce the restriction that pipes 1-5 must share the top 32 bits
- *
- * @param number Which pipe# to open, 0-5.
- * @param address The 40-bit address of the pipe to open.
- */
- void writeAckPayload(unsigned char pipe, const void* buf, unsigned char len);
- /**
- * Determine if an ack payload was received in the most recent call to
- * write().
- *
- * Call read() to retrieve the ack payload.
- *
- * @warning Calling this function clears the internal flag which indicates
- * a payload is available. If it returns true, you must read the packet
- * out as the very next interaction with the radio, or the results are
- * undefined.
- *
- * @return True if an ack payload is available.
- */
- #endif /* NRF24_LIB_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement