Linux下驱动2.4G无线模块(NRF24L01)(4)

CSN_L;                   // CSN low, init SPI transaction
    ndelay(60);
    status = SPI_RW(reg);      // select register
    SPI_RW(value);             // ..and write value to it..
    CSN_H;                   // CSN high again
    ndelay(60);
   
    return(status);            // return nRF24L01 status uint8
}

//函数:uint8 SPI_Read_Buf(uint8 reg, uint8 *pBuf, uint8 uchars)
//功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数
uint8 SPI_Read_Buf(uint8 reg, uint8 *pBuf, uint8 uchars)
{
    uint8 status,uint8_ctr;
   
    CSN_L;                            // Set CSN low, init SPI tranaction
    ndelay(60);
    status = SPI_RW(reg);               // Select register to write to and read status uint8
   
    for(uint8_ctr=0;uint8_ctr<uchars;uint8_ctr++)
    {
        pBuf[uint8_ctr] = SPI_RW(0);    //
        ndelay(20);
    }
   
    CSN_H;
    ndelay(60);
   
    return(status);                    // return nRF24L01 status uint8
}


//函数:uint8 SPI_Write_Buf(uint8 reg, uint8 *pBuf, uint8 uchars)
//功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数
uint8 SPI_Write_Buf(uint8 reg, uint8 *pBuf, uint8 uchars)
{
    uint8 status,uint8_ctr;
   
    CSN_L;            //SPI使能 
    ndelay(60);
    status = SPI_RW(reg);  
    for(uint8_ctr=0; uint8_ctr<uchars; uint8_ctr++) //
    {
        SPI_RW(*pBuf++);
     ndelay(20);
    }
    CSN_H;           //关闭SPI
    ndelay(60);
    return(status);    //
}


//函数:void SetRX_Mode(void)
//功能:数据接收配置
void SetRX_Mode(void)
{

CE_L;
    ndelay(60);
//  SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);           // IRQ收发完成中断响应,16位CRC ,主接收
    //udelay(1);
    CE_H;
    udelay(130);
}

//函数:unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
//功能:数据读取后放如rx_buf接收缓冲区中
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
{
    unsigned char revale=0;

sta=SPI_Read(STATUS);   // 读取状态寄存其来判断数据接收状况
    if(sta & (1<<RX_DR))     // 判断是否接收到数据
    {
        CE_L;             //SPI使能
     udelay(50);
        SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
        revale =1;          //读取数据完成标志
    }
    SPI_RW_Reg(WRITE_REG+STATUS,sta);   //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志
    return revale;
}


//函数:void nRF24L01_TxPacket(unsigned char * tx_buf)
//功能:发送 tx_buf中数据
void nRF24L01_TxPacket(unsigned char * tx_buf)
{
    CE_L;           //StandBy I模式
    ndelay(60);
    SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 装载接收端地址
    SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH);              // 装载数据
    SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);            // IRQ收发完成中断响应,16位CRC,主发送
    CE_H;        //置高CE,激发数据发送
    udelay(10);
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwfdgp.html