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