ARM9 2440硬件SPI驱动程序-NRF24L01
扫描二维码
随时随地手机看文章
从开始接触,到驱动编写调试完成,前前后后花费10多天,网上浏览了一下,目前还没有找到硬件SPI控制NRF24L01的驱动程序,绝大多数都是软件SPI,但是软件SPI不好,不稳定,既然都写驱动程序了,肯定要用硬件SPI啦,这样才能学到东西。学习的过程中,通过看韦东山的SPI视频,和参考他写的两个驱动程序。然后花费了四五天,终于将驱动写好了。这个驱动可以通过ioctl切换接收和发送模式,通过read,write选择接收数据还是发送数据,废话少说,上代码
#include#include#include#include#include#include#include#include#include#include#include #include #include#include#include #include#include#include#include#include"nrf.h"/*构造注册spi_driver*/#defineRX_MODE0xf1#defineTX_MODE0xf2staticintmajor;staticstructclass*class;staticintspi_NRF24L01_ce_pin;staticunsignedchar*ker_buf;staticstructspi_device*spi_NRF24L01_dev;staticunsignedcharopencount=0;staticvolatileintint_flag=0;staticDECLARE_WAIT_QUEUE_HEAD(nrf24l01_waitq);/*生成一个等待队列头wait_queue_head_t,名字为nrf24l01_waitq*/staticunsignedcharTX_ADDRESS[TX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x10};//本地地址staticunsignedcharRX_ADDRESS[RX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x10};//接收地址structpin_desc{unsignedintpin;unsignedintkey_val;};/*引脚描述结构体*/structpin_descpins_desc[2]={/*按下时:0x010x02...松开始0x810x82...*/{S3C2410_GPG(0),0x01},};staticuint8TxBuf[TxBufSize]={0x01,0x02,0x03,0x4,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x30,0x31,0x32,};staticuint8RxBuf[RxBufSize]={0};staticvoidNRF24L01_Set_CE(charval){s3c2410_gpio_setpin(spi_NRF24L01_ce_pin,val);}/*寄存器访问函数:用来设置24L01的寄存器的值。基本思路就是通过WRITE_REG命令(也就是0x20+寄存器地址)把要设定的值写到相应的寄存器地址里面去,并读取返回值。对于函数来说也就是把value值写到reg寄存器中*/staticunsignedcharSPI_RW_Reg(unsignedcharreg,unsignedcharvalue){unsignedcharstatus;unsignedchartx_buf[2];unsignedcharrx_buf[2];tx_buf[0]=reg;tx_buf[1]=value;spi_write(spi_NRF24L01_dev,tx_buf,2);status=rx_buf[0];return(status);}/*读取寄存器值的函数:基本思路就是通过READ_REG命令(也就是0x00+寄存器地址),把寄存器中的值读出来。对于函数来说也就是把reg寄存器的值读到reg_val中去*/staticvoidSPI_Read(int*pMID,int*pDID,unsignedcharreg){unsignedchartx_buf[2];unsignedcharrx_buf[2];tx_buf[0]=reg;tx_buf[1]=0x00;spi_write_then_read(spi_NRF24L01_dev,tx_buf,2,rx_buf,2);*pMID=rx_buf[0];*pDID=rx_buf[1];}/*接收缓冲区访问函数:主要用来在接收时读取FIFO缓冲区中的值。基本思路就是通过READ_REG命令把数据从接收FIFO(RD_RX_PLOAD)中读出并存到数组里面去*///staticunsignedcharSPI_Read_Buf(unsignedcharreg,unsignedchar*ker_buf,unsignedcharbytes)staticvoidSPI_Read_Buf(unsignedcharreg,unsignedchar*buf,intlen){/*spi_write_then_read规定了tx_cnt+rx_cnt<32*所以对于大量数据的读取,不能使用该函数*/unsignedchari=0;unsignedchartx_buf[1];unsignedchartx_buf1[len];structspi_transfert[]={{.tx_buf=tx_buf,.len=1,},{.tx_buf=tx_buf1,.rx_buf=buf,.len=len,},};structspi_messagem;for(i=0;i