#include "24l01.h"
#include "myRadio_gpio.h"
		
const uint8_t TX_ADDRESS[5]={0x34,0x43,0x10,0x10,0x01}; //发送地址
const uint8_t RX_ADDRESS[5]={0x34,0x43,0x10,0x10,0x01}; //发送地址

static uint8_t rf_mode = 0x55;//初始化值，保证第一次能执行 
//初始化24L01的IO口

static bool ext3_flag=false;
rfPackage_ts rfRxPackage;
static void delay_us(uint32_t delay_us)
{
    uint32_t i = 0;
    uint32_t j = 0;
    for (i = 0; i < delay_us; i++)
    {
        for (j = 0; j < 10; j++)
        {
            ;
        }
        
    }
    
}
void NRF24L01_WriteStrobe(uint8_t strobe) 
{
   	RF_SPI_NSS_L();                  //使能SPI传输
  	SpiReadWrite(strobe);//发送寄存器号 
  	SpiReadWrite(0);//发送寄存器号 
  	RF_SPI_NSS_H();                //禁止SPI传输	   
}
//SPI写寄存器
//reg:指定寄存器地址
//value:写入的值
uint8_t NRF24L01_Write_Reg(uint8_t reg,uint8_t value)
{
	uint8_t status;	
   	RF_SPI_NSS_L();                 //使能SPI传输
  	status = SpiReadWrite(reg);//发送寄存器号 
  	SpiReadWrite(value);      //写入寄存器的值
  	RF_SPI_NSS_H();                 //禁止SPI传输	   
  	return(status);       			//返回状态值
}
//读取SPI寄存器值
//reg:要读的寄存器
uint8_t NRF24L01_Read_Reg(uint8_t reg)
{
	uint8_t reg_val;	    
 	RF_SPI_NSS_L();          //使能SPI传输		
  	SpiReadWrite(reg);   //发送寄存器号
  	reg_val= SpiReadWrite(0XFF);//读取寄存器内容
  	RF_SPI_NSS_H();          //禁止SPI传输		    
  	return(reg_val);           //返回状态值
}	
//在指定位置读出指定长度的数据
//reg:寄存器(位置)
//*pBuf:数据指针
//len:数据长度
//返回值,此次读到的状态寄存器值 
uint8_t NRF24L01_Read_Buf(uint8_t reg,uint8_t *pBuf,uint8_t len)
{
	uint8_t status,uint8_t_ctr;	       
  	RF_SPI_NSS_L();           //使能SPI传输
  	status= SpiReadWrite(reg);//发送寄存器值(位置),并读取状态值   	   
 	for(uint8_t_ctr=0;uint8_t_ctr<len;uint8_t_ctr++)
     pBuf[uint8_t_ctr]= SpiReadWrite(0XFF);//读出数据
  	RF_SPI_NSS_H();       //关闭SPI传输
  	return status;        //返回读到的状态值
}
//在指定位置写指定长度的数据
//reg:寄存器(位置)
//*pBuf:数据指针
//len:数据长度
//返回值,此次读到的状态寄存器值
uint8_t NRF24L01_Write_Buf(uint8_t reg, uint8_t *pBuf, uint8_t len)
{
	uint8_t status,uint8_t_ctr;	    
   	RF_SPI_NSS_L();          //使能SPI传输
  	status = SpiReadWrite(reg);//发送寄存器值(位置),并读取状态值
  	for(uint8_t_ctr=0; uint8_t_ctr<len; uint8_t_ctr++)
      SpiReadWrite(*pBuf++); //写入数据	 
  	RF_SPI_NSS_H();       //关闭SPI传输
  	return status;          //返回读到的状态值
}			
//检测24L01是否存在
//返回值:0，成功;1，失败	
uint8_t NRF24L01_Check(void)
{
	uint8_t buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
	uint8_t i;
	// SPI1_SetSpeed(SPI_BaudRatePrescaler_8); //spi速度为9Mhz（24L01的最大SPI时钟为10Mhz）   	 
	NRF24L01_Write_Buf(NRF_WRITE_REG+TX_ADDR,buf,5);//写入5个字节的地址.	
	memset(buf,0, 5);
	NRF24L01_Read_Buf(TX_ADDR,buf,5); //读出写入的地址  
	for(i=0;i<5;i++)if(buf[i]!=0XA5)break;	 							   
	if(i!=5)return 1;//检测24L01错误	
	return 0;		 //检测到24L01
}	   
//启动NRF24L01发送一次数据
//txbuf:待发送数据首地址
//返回值:发送完成状况
void NRF24L01_TxPacket(uint8_t *txbuf, uint16_t len)
{
    RF_NRF_CE_L();   
    NRF24L01_Write_Buf(WR_TX_PLOAD, txbuf, TX_PLOAD_WIDTH);//写数据到TX BUF  32个字节	
    RF_NRF_CE_H(); //启动发送	
}
//启动NRF24L01发送一次数据
//txbuf:待发送数据首地址
void NRF24L01_getRxPacket(uint8_t *rxbuf, uint16_t len)
{
    NRF24L01_Read_Buf(RD_RX_PLOAD, rxbuf, len);//读取数据
    NRF24L01_WriteStrobe(FLUSH_RX);//清除RX FIFO寄存器 
}			
	 
/**
 * 
 * 设置无线工作模式
 * mode
 * 		=CONFIG_RF_MODE_TX，发送
 * 		=CONFIG_RF_MODE_RX，接收
*/
void NRF24L01_RfMode(uint8_t mode)//
{
    config_tu config;
    RF_NRF_CE_L();
    config.value = NRF24L01_Read_Reg(NRF_READ_REG + CONFIG);//读取配置寄存器
    config.bits.prim_rx = (rf_mode_te)mode;
    config.bits.mask_rx_dr = 0;
    config.bits.mask_tx_ds = 0;
    config.bits.mask_max_rt = 0;
    config.bits.pwr_up = 1;
    NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG, config.value);
    if (mode == RF_PRIM_RX)
    {
        RF_NRF_CE_H();
    }
    
}
/**
 * 
 * 
*/
status_tu NRF24L01_readStatus(void)
{
    status_tu sta;
	sta.value = NRF24L01_Read_Reg(STATUS);
    NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS, sta.value);
    return sta;
}
/**
 * 
 * 设置无线发射功率
 * 
*/
void NRF24L01_setRfPower(uint8_t power)
{
    rf_setup_tu rf_setup;
    RF_NRF_CE_L();
    rf_setup.value = NRF24L01_Read_Reg(NRF_READ_REG + RF_SETUP);//读取配置寄存器
    rf_setup.bits.rf_pwr = power;
    NRF24L01_Write_Reg(NRF_WRITE_REG+RF_SETUP, rf_setup.value); 
}
/**
 * 
 * 设置无线发射功率
 * 
*/
void NRF24L01_setRfBaudrate(uint32_t br)
{
    rf_setup_tu rf_setup;
    RF_NRF_CE_L();
    rf_setup.value = NRF24L01_Read_Reg(NRF_READ_REG + RF_SETUP);//读取配置寄存器
    rf_setup.bits.rf_dr_low = (br & 0x02) >> 1;
    rf_setup.bits.rf_dr_high = br & 0x01;
    NRF24L01_Write_Reg(NRF_WRITE_REG+RF_SETUP, rf_setup.value); 
}
/**
 * 
 * 设置无线信道
 * 具体频点：2400MHz+ch MHz
*/
void NRF24L01_setChannl(uint8_t ch)
{
    NRF24L01_Write_Reg(NRF_WRITE_REG+RF_CH, ch);
}
/**
 * 
 * 进入stb1 mode
*/
void NRF24L01_powerDown(void)
{
	config_tu config;

    RF_NRF_CE_L();
    config.value = NRF24L01_Read_Reg(NRF_READ_REG + CONFIG);//读取配置寄存器
    config.bits.pwr_up = 0;
    NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG, config.value);
}
/**
 * 
 * 获取信号强度
*/
int8_t NRF24L01_getRssi(void)
{
	int8_t rssi = 0;

    // RF_NRF_CE_L();
    rssi = NRF24L01_Read_Reg(NRF_READ_REG+CD)&0x01;
    // RF_NRF_CE_H();
    return rssi;
}
/**
 * 
 * 单载波测试
*/
void NRF24L01_carrierWave(void)
{
    rf_setup_tu rf_setup;
	config_tu config;

    RF_NRF_CE_L();
    config.value = NRF24L01_Read_Reg(NRF_READ_REG + CONFIG);//读取配置寄存器
    rf_setup.value = NRF24L01_Read_Reg(NRF_READ_REG + RF_SETUP);
    config.bits.pwr_up = 1;
//    rf_setup.bits.obsolete = 0;
    // rf_setup.bits.rf_pwr = 0x03;
    rf_setup.bits.rf_dr_low = 0;
    rf_setup.bits.rf_dr_high = 0;
    rf_setup.bits.pll_lock = 1;
    rf_setup.bits.reserved = 0;
    rf_setup.bits.cont_wave = 1;
    NRF24L01_Write_Reg(NRF_WRITE_REG+RF_SETUP,rf_setup.value); 
    NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG, config.value);
    // NRF24L01_Write_Reg(NRF_WRITE_REG+RF_CH,50);	     //设置RF通信频率
    RF_NRF_CE_H();
}

int NRF24L01_Init(void)
{  
	int sta = 0;
	int errorCnt=0;
    rf_setup_tu rf_setup;
    setupRetr_tu setupRetr;
    future_tu future;
    config_tu config;
	//测试NRF24L01是否存在
	while(NRF24L01_Check())
	{
		errorCnt ++;
		if (errorCnt > 100)
		{
			break;
		}
		
	}
	if (errorCnt < 10)
	{
		NRF24L01_Write_Buf(NRF_WRITE_REG+TX_ADDR,(uint8_t*)TX_ADDRESS,TX_ADR_WIDTH);//写TX节点地址 
		NRF24L01_Write_Buf(NRF_WRITE_REG+RX_ADDR_P0,(uint8_t*)RX_ADDRESS,RX_ADR_WIDTH); //设置TX节点地址,主要为了使能ACK	
		NRF24L01_Write_Reg(NRF_WRITE_REG+SETUP_AW,0x03); 
		NRF24L01_Write_Reg(NRF_WRITE_REG+EN_AA,0x00);     //使能通道0的自动应答    
		NRF24L01_Write_Reg(NRF_WRITE_REG+EN_RXADDR,0x01); //使能通道0的接收地址  
		NRF24L01_Write_Reg(NRF_WRITE_REG+FEATURE,0x00);
		NRF24L01_Write_Reg(NRF_WRITE_REG+DYNDP,0x00); 
		NRF24L01_Write_Reg(NRF_WRITE_REG+RF_CH,0);       //设置RF通道
		NRF24L01_Write_Reg(NRF_WRITE_REG+RX_PW_P0,RX_PLOAD_WIDTH);
        setupRetr.bits.ard = 0x00;
        setupRetr.bits.arc = 0x00;
		NRF24L01_Write_Reg(NRF_WRITE_REG+SETUP_RETR,setupRetr.setupRetrValue);
//        rf_setup.bits.obsolete = 0;
        rf_setup.bits.rf_pwr = 0x00;
        rf_setup.bits.rf_dr_low = 1;
        rf_setup.bits.rf_dr_high = 0;
        rf_setup.bits.pll_lock = 0;
        rf_setup.bits.reserved = 0;
        rf_setup.bits.cont_wave = 0;
		NRF24L01_Write_Reg(NRF_WRITE_REG+RF_SETUP,rf_setup.value);  //设置TX发射参数,0db增益,1Mbps,低噪声增益开启   
        config.bits.pwr_up = 1;
        config.bits.crco = 1;
        config.bits.enCrc = 1;
        config.bits.mask_max_rt = 0;
        config.bits.mask_tx_ds = 0;
        config.bits.mask_rx_dr = 0;
        config.bits.reserved = 0;
		NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG,config.value);    //配置基本工作模式的参数;PWR_UP,EN_CRC,16BIT_CRC,接收模式,开启所有中断
        future.bits.en_dyn_ack = 0x00;
        future.bits.en_ack_pay = 0x00;
        future.bits.en_dpl = 0x00;
        future.bits.receved = 0x00;
		NRF24L01_Write_Reg(NRF_WRITE_REG+FEATURE,future.futureValue);    //配置基本工作模式的参数;PWR_UP,EN_CRC,16BIT_CRC,接收模式,开启所有中断
        NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS, 0x70);
        NRF24L01_WriteStrobe(FLUSH_RX);
	}
	else
	{
		sta = 1;
	}
	return sta;
}


