從開始接觸,到驅(qū)動編寫調(diào)試完成,前前后后花費(fèi)10多天,網(wǎng)上瀏覽了一下,目前還沒有找到硬件SPI控制NRF24L01的驅(qū)動程序,絕大多數(shù)都是軟件SPI,但是軟件SPI不好,不穩(wěn)定,既然都寫驅(qū)動程序了,肯定要用硬件SPI啦,這樣才能學(xué)到東西。學(xué)習(xí)的過程中,通過看韋東山的SPI視頻,和參考他寫的兩個驅(qū)動程序。然后花費(fèi)了四五天,終于將驅(qū)動寫好了。這個驅(qū)動可以通過ioctl切換接收和發(fā)送模式,通過read,write選擇接收數(shù)據(jù)還是發(fā)送數(shù)據(jù),廢話少說,上代碼
#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include"nrf.h"/*構(gòu)造注冊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);/*生成一個等待隊(duì)列頭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;};/*引腳描述結(jié)構(gòu)體*/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);}/*寄存器訪問函數(shù):用來設(shè)置24L01的寄存器的值?;舅悸肪褪峭ㄟ^WRITE_REG命令(也就是0x20+寄存器地址)把要設(shè)定的值寫到相應(yīng)的寄存器地址里面去,并讀取返回值。對于函數(shù)來說也就是把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);}/*讀取寄存器值的函數(shù):基本思路就是通過READ_REG命令(也就是0x00+寄存器地址),把寄存器中的值讀出來。對于函數(shù)來說也就是把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];}/*接收緩沖區(qū)訪問函數(shù):主要用來在接收時讀取FIFO緩沖區(qū)中的值。基本思路就是通過READ_REG命令把數(shù)據(jù)從接收FIFO(RD_RX_PLOAD)中讀出并存到數(shù)組里面去*///staticunsignedcharSPI_Read_Buf(unsignedcharreg,unsignedchar*ker_buf,unsignedcharbytes)staticvoidSPI_Read_Buf(unsignedcharreg,unsignedchar*buf,intlen){/*spi_write_then_read規(guī)定了tx_cnt+rx_cnt<32*所以對于大量數(shù)據(jù)的讀取,不能使用該函數(shù)*/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