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


//
uint8     sta;   //状态标志
#define   RX_DR    6
#define   TX_DS    5
#define   MAX_RT   4

//NRF24L01初始化
uint8 init_NRF24L01(void)
{
/*
    CE_UP;
    SCK_UP;
    MISO_UP;
    IRQ_UP;
    MOSI_UP;
    CSN_UP;  
*/

MISO_UP;

CE_OUT;
    CSN_OUT;
    SCK_OUT;
    MOSI_OUT;
    MISO_IN;
    IRQ_IN;

udelay(500);

CE_L;    // chip enable
    ndelay(60);
    CSN_H;   // Spi disable
    ndelay(60);
    SCK_L;   // Spi clock line init high
    ndelay(60);
    SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);    // 写本地地址
    SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // 写接收端地址
    SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      //  频道0自动 ACK应答允许
    SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  //  允许接收地址只有频道0,如果需要多频道可以参考Page21 
    SPI_RW_Reg(WRITE_REG + RF_CH, 0);        //   设置信道工作为2.4GHZ,收发必须一致
    SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节
    SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);         //设置发射速率为1MHZ,发射功率为最大值0dB
    SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);           // IRQ收发完成中断响应,16位CRC ,主接收

mdelay(1000);
    nRF24L01_TxPacket(TxBuf);
    SPI_RW_Reg(WRITE_REG+STATUS,0XFF);
    printk("test 1 \n");
    mdelay(1000);
/*
    nRF24L01_TxPacket(TxBuf);
    SPI_RW_Reg(WRITE_REG+STATUS,0XFF);
    printk("test 2 \n");
    mdelay(1000);
    nRF24L01_TxPacket(TxBuf);
    SPI_RW_Reg(WRITE_REG+STATUS,0XFF);
    printk("test 3 \n");
    mdelay(1000);
    nRF24L01_TxPacket(TxBuf);
    SPI_RW_Reg(WRITE_REG+STATUS,0XFF);
    printk("test 4 \n");
    mdelay(1000);
*/
    return (1);
}


//函数:uint8 SPI_RW(uint8 tmp)
//功能:NRF24L01的SPI写时序tmp
uint8 SPI_RW(uint8 tmp)
{
    uint8 bit_ctr;

for(bit_ctr=0 ;bit_ctr<8 ;bit_ctr++) // output 8-bit
    {
 if(tmp & 0x80)         // output 'tmp', MSB to MOSI
     MOSI_H;
 else
     MOSI_L;
 
        tmp <<= 1;           // shift next bit into MSB..

SCK_H;                   // Set SCK high..
 ndelay(60);

tmp |= MISO_STU;          // capture current MISO bit
        SCK_L;                   // ..then set SCK low again
 ndelay(60);
    }
    return(tmp);                    // return read tmp
}

//函数:uint8 SPI_Read(uint8 reg)
//功能:NRF24L01的SPI时序
uint8 SPI_Read(uint8 reg)
{
    uint8 reg_val;

CSN_L;                // CSN low, initialize SPI communication...
    ndelay(60);
    SPI_RW(reg);             // Select register to read from..
    reg_val = SPI_RW(0);     // ..then read registervalue
    CSN_H;               // CSN high, terminate SPI communication
    ndelay(60);
   
    return(reg_val);           // return register value
}

//功能:NRF24L01读写寄存器函数
uint8 SPI_RW_Reg(uint8 reg, uint8 value)
{
    uint8 status;  

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

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