#ifndef __24L01_H
#define __24L01_H   
#include <stdbool.h>
#include <stdint.h>

//NRF24L01寄存器操作命令
#define NRF_READ_REG    0x00  //读配置寄存器,低5位为寄存器地址
#define NRF_WRITE_REG   0x20  //写配置寄存器,低5位为寄存器地址
#define RD_RX_PLOAD     0x61  //读RX有效数据,1~32字节
#define WR_TX_PLOAD     0xA0  //写TX有效数据,1~32字节
#define FLUSH_TX        0xE1  //清除TX FIFO寄存器.发射模式下用
#define FLUSH_RX        0xE2  //清除RX FIFO寄存器.接收模式下用
#define REUSE_TX_PL     0xE3  //重新使用上一包数据,CE为高,数据包被不断发送.
#define NOP             0xFF  //空操作,可以用来读状态寄存器 
//SPI(NRF24L01)寄存器地址
#define CONFIG          0x00  //配置寄存器地址;bit0:1接收模式,0发射模式;bit1:电选择;bit2:CRC模式;bit3:CRC使能;
                              //bit4:中断MAX_RT(达到最大重发次数中断)使能;bit5:中断TX_DS使能;bit6:中断RX_DR使能
#define EN_AA           0x01  //使能自动应答功能  bit0~5,对应通道0~5
#define EN_RXADDR       0x02  //接收地址允许,bit0~5,对应通道0~5
#define SETUP_AW        0x03  //设置地址宽度(所有数据通道):bit1,0:00,3字节;01,4字节;02,5字节;
#define SETUP_RETR      0x04  //建立自动重发;bit3:0,自动重发计数器;bit7:4,自动重发延时 250*x+86us
#define RF_CH           0x05  //RF通道,bit6:0,工作通道频率;2400+RF_CH【MHZ】
#define RF_SETUP        0x06  //RF寄存器;bit3:传输速率(0:1Mbps,1:2Mbps);bit2:1,发射功率;bit0:低噪声放大器增益
#define STATUS          0x07  //状态寄存器;bit0:TX FIFO满标志;bit3:1,接收数据通道号(最大:6);bit4,达到最多次重发
                              //bit5:数据发送完成中断;bit6:接收数据中断;
#define MAX_TX  		0x10  //达到最大发送次数中断
#define TX_OK   		0x20  //TX发送完成中断
#define RX_OK   		0x40  //接收到数据中断

#define OBSERVE_TX      0x08  //发送检测寄存器,bit7:4,数据包丢失计数器;bit3:0,重发计数器
#define CD              0x09  //载波检测寄存器,bit0,载波检测;
#define RX_ADDR_P0      0x0A  //数据通道0接收地址,最大长度5个字节,低字节在前
#define RX_ADDR_P1      0x0B  //数据通道1接收地址,最大长度5个字节,低字节在前
#define RX_ADDR_P2      0x0C  //数据通道2接收地址,最低字节可设置,高字节,必须同RX_ADDR_P1[39:8]相等;
#define RX_ADDR_P3      0x0D  //数据通道3接收地址,最低字节可设置,高字节,必须同RX_ADDR_P1[39:8]相等;
#define RX_ADDR_P4      0x0E  //数据通道4接收地址,最低字节可设置,高字节,必须同RX_ADDR_P1[39:8]相等;
#define RX_ADDR_P5      0x0F  //数据通道5接收地址,最低字节可设置,高字节,必须同RX_ADDR_P1[39:8]相等;
#define TX_ADDR         0x10  //发送地址(低字节在前),ShockBurstTM模式下,RX_ADDR_P0与此地址相等
#define RX_PW_P0        0x11  //接收数据通道0有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P1        0x12  //接收数据通道1有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P2        0x13  //接收数据通道2有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P3        0x14  //接收数据通道3有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P4        0x15  //接收数据通道4有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P5        0x16  //接收数据通道5有效数据宽度(1~32字节),设置为0则非法
#define NRF_FIFO_STATUS 0x17  //FIFO状态寄存器;bit0,RX FIFO寄存器空标志;bit1,RX FIFO满标志;bit2,3,保留
                              //bit4,TX FIFO空标志;bit5,TX FIFO满标志;bit6,1,循环发送上一数据包.0,不循环;
#define FEATURE         0x1D    
#define DYNDP           0x1C

#define MASK_CONFIG_RX_IRQ 0x40
#define CONFIG_RX_IRQ_PIN 0x40

#define MASK_CONFIG_TX_IRQ 0x20
#define CONFIG_TX_IRQ_PIN 0x20

#define MASK_CONFIG_MAX_IRQ 0x10
#define CONFIG_MAX_IRQ_PIN 0x10

#define MASK_CONFIG_EN_CRC 0x08
#define CONFIG_EN_CRC 0x08
#define CONFIG_DISEN_CRC 0x00

#define MASK_CONFIG_CRC_BYTES 0x04
#define CONFIG_CRC_BYTES_2BYTE 0x04
#define CONFIG_CRC_BYTES_1BYTE 0x00

#define MASK_CONFIG_PWR_UP 0x02
#define CONFIG_PWR_UP_ON 0x02
#define CONFIG_PWR_UP_OFF 0x00

#define MASK_CONFIG_RF_MODE 0x01
#define CONFIG_RF_MODE_RX 0x01
#define CONFIG_RF_MODE_TX 0x00
typedef enum
{
    RF_PRIM_RX = 1,
    RF_PRIM_TX = 0,
}rf_mode_te;
#define MASK_SETUP_RETR_DELAY 0xf0
#define MASK_SETUP_RETR_COUNT 0x0f

#define MASK_RF_CH 0x7f

#define MASK_RF_SETUP_CONT_WAVE 0x80
#define MASK_RF_SETUP_RF_DR_LOW 0x20
#define MASK_RF_SETUP_RF_DR_HIGH 0x08
#define RF_SETUP_RF_DR_1Mbps 0x00
#define RF_SETUP_RF_DR_2Mbps 0x08
#define RF_SETUP_RF_DR_250Mbps 0x28

#define MASK_RF_SETUP_RF_PWR 0x03
#define RF_PWR_18N 0x00
#define RF_PWR_12N 0x01
#define RF_PWR_6N 0x02
#define RF_PWR_0 0x03

#define MASK_FEATURE 0x03
#define FEATURE_EN_DPL 0x04
#define FEATURE_EN_ACK_PAY 0x02
#define FEATURE_EN_DYN_ACK 0x01

//24L01发送接收数据宽度定义
#define TX_ADR_WIDTH    5   	//5字节的地址宽度
#define RX_ADR_WIDTH    5   	//5字节的地址宽度
#define TX_PLOAD_WIDTH  32  	//32字节的用户数据宽度
#define RX_PLOAD_WIDTH  32  	//32字节的用户数据宽度

typedef void (*rf_callBack)(uint8_t *buf, uint16_t count);
typedef union 
{
    uint8_t value;
    struct 
    {
        /**
         * bit0
         * ...
         * bit7
        */
        // uint8_t obsolete : 1;   //Don’t care
        uint8_t rf_pwr : 3;     //R/W Set RF output power in TX mode
                                // '00' – -18dBm
                                // '01' – -12dBm
                                // '10' – -6dBm
                                // '11' – 0dBm
                                // 111: 7dBm 110: 4dBm 
                                // 101: 3dBm 100: 1dBm 
                                // 011: 0dBm 010:-4dBm 
                                // 001:-6dBm 000:-12dBm
        uint8_t rf_dr_high : 1;    //Select between the high speed data rates. This bit
                                    // is don’t care if RF_DR_LOW is set.
                                    // Encoding:
                                    // [RF_DR_LOW, RF_DR_HIGH]:
                                    // ‘00’ – 1Mbps
                                    // ‘01’ – 2Mbps
                                    // ‘10’ – 250kbps
                                    // ‘11’ – Reserved
        uint8_t pll_lock : 1;       //Force PLL lock signal. Only used in test
        uint8_t rf_dr_low : 1;      //R/W Set RF Data Rate to 250kbps. See RF_DR_HIGH
                                    // for encoding.
        uint8_t reserved : 1;       //R/W Only '0' allowed
        uint8_t cont_wave : 1;      //R/W Enables continuous carrier transmit when high
    }bits;
}rf_setup_tu;
typedef union 
{
    uint8_t value;
    struct 
    {
        /**
         * bit0
         * ...
         * bit7
        */
        rf_mode_te prim_rx : 1;     //RX/TX control
                                    // 1: PRX, 0: PTX
        uint8_t pwr_up : 1;     //1: POWER UP, 0:POWER DOWN
        uint8_t crco : 1;       //CRC encoding scheme
                                // '0' - 1 byte
                                // '1' – 2 bytes
        uint8_t enCrc : 1;      //Enable CRC. Forced high if one of the bits in the
                                // EN_AA is high
        uint8_t mask_max_rt : 1;    //Mask interrupt caused by MAX_RT
                                    // 1: Interrupt not reflected on the IRQ pin
                                    // 0: Reflect MAX_RT as active low interrupt on the
                                    // IRQ pin
        uint8_t mask_tx_ds : 1;     //Mask interrupt caused by TX_DS
                                    // 1: Interrupt not reflected on the IRQ pin
                                    // 0: Reflect TX_DS as active low interrupt on the IRQ
                                    // pin
        uint8_t mask_rx_dr : 1;     // Mask interrupt caused by RX_DR
                                    // 1: Interrupt not reflected on the IRQ pin
                                    // 0: Reflect RX_DR as active low interrupt on the
                                    // IRQ pin
        uint8_t reserved : 1;       //R/W Only '0' allowed
    }bits;
}config_tu;
typedef union 
{
    uint8_t value;
    struct 
    {
        /**
         * bit0
         * ...
         * bit7
        */
        uint8_t tx_full : 1;    //R TX FIFO full flag.
                                // 1: TX FIFO full.
                                // 0: Available locations in TX FIFO
        uint8_t rx_p_no : 3;     //R Data pipe number for the payload available for
                                // reading from RX_FIFO
                                // 000-101: Data Pipe Number
                                // 110: Not Used
                                // 111: RX FIFO Empty
        uint8_t max_rt : 1;     //R/W Maximum number of TX retransmits interrupt
                                // Write 1 to clear bit. If MAX_RT is asserted it must
                                // be cleared to enable further communication
        uint8_t tx_ds : 1;      // Data Sent TX FIFO interrupt. Asserted when
                                // packet transmitted on TX. If AUTO_ACK is activated, 
                                // this bit is set high only when ACK is
                                // received.
                                // Write 1 to clear bit.
        uint8_t rx_dr : 1;    //Data Ready RX FIFO interrupt. Asserted when
                            // new data arrives RX FIFOc.
                            // Write 1 to clear bit
        uint8_t reserved : 1;    //R/W Only '0' allowed
    }statusUint;
}status_tu;
typedef union 
{
    uint8_t setupRetrValue;
    struct 
    {
        /**
         * bit0
         * ...
         * bit7
        */
        uint8_t arc : 4;    //R/W Auto Retransmit Count
                            // ‘0000’ –Re-Transmit disabled
                            // ‘0001’ – Up to 1 Re-Transmit on fail of AA
                            // ……
                            // ‘1111’ – Up to 15 Re-Transmit on fail of AA
        uint8_t ard : 4;    //Auto Retransmit Delay
                            // ‘0000’ – Wait 250?S
                            // ‘0001’ – Wait 500?S
                            // ‘0010’ – Wait 750?S
                            // ……..
                            // ‘1111’ – Wait 4000?S
                            // (Delay defined from end of transmission to start of
                            // next transmission)b
    }bits;
}setupRetr_tu;
typedef union 
{
    uint8_t futureValue;
    struct 
    {
        /**
         * bit0
         * ...
         * bit7
        */
        uint8_t en_dyn_ack : 1;    //R/W Enables the W_TX_PAYLOAD_NOACK command
        uint8_t en_ack_pay : 1;    //R/W Enables Payload with ACK
        uint8_t en_dpl : 1;    //R/W Enables Dynamic Payload Length
        uint8_t receved : 5;    //R/W Only ‘00000’ allowed
    }bits;
}future_tu;

typedef struct 
{
    uint8_t isValid;
    uint8_t len;
    uint8_t package[RX_PLOAD_WIDTH];
}rfPackage_ts;
      
int NRF24L01_Init(void);

uint8_t NRF24L01_Check(void);
void NRF24L01_TxPacket(uint8_t *txbuf, uint16_t len);
void NRF24L01_getRxPacket(uint8_t *rxbuf, uint16_t len);
void NRF24L01_WriteStrobe(uint8_t strobe);

void NRF24L01_setChannl(uint8_t ch);
void NRF24L01_setRfPower(uint8_t power);
void NRF24L01_setRfBaudrate(uint32_t br);

void NRF24L01_RfMode(uint8_t mode);
void NRF24L01_carrierWave(void);
void NRF24L01_powerDown(void);
status_tu NRF24L01_readStatus(void);
int8_t NRF24L01_getRssi(void);
#endif











