<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > Mini2440 NRF24L01無線模塊驅(qū)動(dòng)

          Mini2440 NRF24L01無線模塊驅(qū)動(dòng)

          作者: 時(shí)間:2016-11-19 來源:網(wǎng)絡(luò) 收藏
          為了和jihuaLi 完成智能家居系統(tǒng),我移植了這個(gè)驅(qū)動(dòng),不管寫的如何,總結(jié)一下是很有必要的。

          NRF24L01使用SPI總線與主機(jī)通信,沒有SPI端口的設(shè)備可以使用IO口進(jìn)行模擬。

          本文引用地址:http://www.ex-cimer.com/article/201611/318111.htm

          關(guān)于SPI:
          SPI是一種四線串行總線,
          SCLK: 串行時(shí)鐘線
          MOSI: 總線主機(jī)輸出/ 從機(jī)輸入
          MISO: 總線主機(jī)輸入/ 從機(jī)輸出;
          SS: 從機(jī)使能數(shù)據(jù)傳輸方式
          通訊是通過數(shù)據(jù)交換完成的,這里先要知道SPI是串行通訊協(xié)議,也就是說數(shù)據(jù)是一位一位的傳輸?shù)?。這就是SCLK時(shí)鐘線存在的原因,由SCK提供時(shí)鐘脈沖,SDI,SDO則基于此脈沖完成數(shù)據(jù)傳輸。數(shù)據(jù)輸出通過 SDO線,數(shù)據(jù)在時(shí)鐘上升沿或下降沿時(shí)改變,在緊接著的下降沿或上升沿被讀取。完成一位數(shù)據(jù)傳輸,輸入也使用同樣原理。這樣,在至少8次時(shí)鐘信號(hào)的改變(上沿和下沿為一次),就可以完成8位數(shù)據(jù)的傳輸。

          NRF24L01寄存器說明:
          還是看說明書吧,dbank驅(qū)動(dòng)源碼在下一頁。驅(qū)動(dòng)源碼:

          #include #include #include #include #include #include 

          #include#include#include#include#include#include#include#include#include#include

          typedef unsigned int uint16;
          typedef unsigned char uint8;

          /* 引腳相關(guān)定義 */
          #define CSN S3C2410_GPF(4)
          #define CSN_OUTP S3C2410_GPIO_OUTPUT
          #define MOSI S3C2410_GPG(0)
          #define MOSI_OUTP S3C2410_GPIO_OUTPUT
          #define IRQ S3C2410_GPG(1)
          #define IRQ_INP S3C2410_GPIO_INPUT
          #define MISO S3C2410_GPG(6)
          #define MISO_INP S3C2410_GPIO_INPUT
          #define SCK S3C2410_GPG(7)
          #define SCK_OUTP S3C2410_GPIO_OUTPUT
          #define CE S3C2410_GPG(11)
          #define CE_OUTP S3C2410_GPIO_OUTPUT

          #define DEVICE_NAME “NRF24L01”

          #define TxBufSize 32

          uint8 TxBuf[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,
          };

          //NRF24L01端口定義

          #define CE_OUT s3c2410_gpio_cfgpin(CE, CE_OUTP) //數(shù)據(jù)線設(shè)置為輸出

          #define CE_UP s3c2410_gpio_pullup(CE, 1) //打開上拉電阻

          #define CE_L s3c2410_gpio_setpin(CE, 0) //拉低數(shù)據(jù)線電平

          #define CE_H s3c2410_gpio_setpin(CE, 1) //拉高數(shù)據(jù)線電平

          #define SCK_OUT s3c2410_gpio_cfgpin(SCK, SCK_OUTP) //數(shù)據(jù)線設(shè)置為輸出

          #define SCK_H s3c2410_gpio_setpin(SCK, 1) //拉高數(shù)據(jù)線電平

          #define SCK_L s3c2410_gpio_setpin(SCK, 0) //拉高數(shù)據(jù)線電平

          #define MISO_IN s3c2410_gpio_cfgpin(MISO, MISO_INP) //數(shù)據(jù)線設(shè)置為輸出

          #define MISO_UP s3c2410_gpio_pullup(MISO, 1) //打開上拉電阻

          #define MISO_STU s3c2410_gpio_getpin(MISO) //數(shù)據(jù)狀態(tài)

          #define IRQ_IN s3c2410_gpio_cfgpin(IRQ, IRQ_INP) //數(shù)據(jù)線設(shè)置為輸出

          #define IRQ_UP s3c2410_gpio_pullup(IRQ, 1) //打開上拉電阻

          #define IRQ_L s3c2410_gpio_setpin(IRQ, 0) //拉低數(shù)據(jù)線電平

          #define IRQ_H s3c2410_gpio_setpin(IRQ, 1) //拉高數(shù)據(jù)線電平

          #define MOSI_OUT s3c2410_gpio_cfgpin(MOSI, MOSI_OUTP) //數(shù)據(jù)線設(shè)置為輸出

          #define MOSI_UP s3c2410_gpio_pullup(MOSI, 1) //打開上拉電阻

          #define MOSI_L s3c2410_gpio_setpin(MOSI, 0) //拉低數(shù)據(jù)線電平

          #define MOSI_H s3c2410_gpio_setpin(MOSI, 1) //拉高數(shù)據(jù)線電平

          #define CSN_OUT s3c2410_gpio_cfgpin(CSN, CSN_OUTP) //數(shù)據(jù)線設(shè)置為輸出

          #define CSN_UP s3c2410_gpio_pullup(CSN, 1) //打開上拉電阻

          #define CSN_L s3c2410_gpio_setpin(CSN, 0) //拉低數(shù)據(jù)線電平

          #define CSN_H s3c2410_gpio_setpin(CSN, 1) //拉高數(shù)據(jù)線電平

          //NRF24L01

          #define TX_ADR_WIDTH 5 // 5 uint8s TX address width

          #define RX_ADR_WIDTH 5 // 5 uint8s RX address width

          #define TX_PLOAD_WIDTH 32 // 20 uint8s TX payload

          #define RX_PLOAD_WIDTH 32 // 20 uint8s TX payload

          uint8 TX_ADDRESS[TX_ADR_WIDTH] = { 0x34, 0x43, 0x10, 0x10, 0x01 }; //本地地址

          uint8 RX_ADDRESS[RX_ADR_WIDTH] = { 0x34, 0x43, 0x10, 0x10, 0x01 }; //接收地址

          //NRF24L01寄存器指令

          #define READ_REG 0x00 // 讀寄存器指令

          #define WRITE_REG 0x20 // 寫寄存器指令

          #define RD_RX_PLOAD 0x61 // 讀取接收數(shù)據(jù)指令

          #define WR_TX_PLOAD 0xA0 // 寫待發(fā)數(shù)據(jù)指令

          #define FLUSH_TX 0xE1 // 沖洗發(fā)送 FIFO指令

          #define FLUSH_RX 0xE2 // 沖洗接收 FIFO指令

          #define REUSE_TX_PL 0xE3 // 定義重復(fù)裝載數(shù)據(jù)指令

          #define NOP 0xFF // 保留

          //SPI(nRF24L01)寄存器地址

          #define CONFIG 0x00 // 配置收發(fā)狀態(tài),CRC校驗(yàn)?zāi)J揭约笆瞻l(fā)狀態(tài)響應(yīng)方式

          #define EN_AA 0x01 // 自動(dòng)應(yīng)答功能設(shè)置

          #define EN_RXADDR 0x02 // 可用信道設(shè)置

          #define SETUP_AW 0x03 // 收發(fā)地址寬度設(shè)置

          #define SETUP_RETR 0x04 // 自動(dòng)重發(fā)功能設(shè)置

          #define RF_CH 0x05 // 工作頻率設(shè)置

          #define RF_SETUP 0x06 // 發(fā)射速率、功耗功能設(shè)置

          #define STATUS 0x07 // 狀態(tài)寄存器

          #define OBSERVE_TX 0x08 // 發(fā)送監(jiān)測功能

          #define CD 0x09 // 地址檢測

          #define RX_ADDR_P0 0x0A // 頻道0接收數(shù)據(jù)地址

          #define RX_ADDR_P1 0x0B // 頻道1接收數(shù)據(jù)地址

          #define RX_ADDR_P2 0x0C // 頻道2接收數(shù)據(jù)地址

          #define RX_ADDR_P3 0x0D // 頻道3接收數(shù)據(jù)地址

          #define RX_ADDR_P4 0x0E // 頻道4接收數(shù)據(jù)地址

          #define RX_ADDR_P5 0x0F // 頻道5接收數(shù)據(jù)地址

          #define TX_ADDR 0x10 // 發(fā)送地址寄存器

          #define RX_PW_P0 0x11 // 接收頻道0接收數(shù)據(jù)長度

          #define RX_PW_P1 0x12 // 接收頻道0接收數(shù)據(jù)長度

          #define RX_PW_P2 0x13 // 接收頻道0接收數(shù)據(jù)長度

          #define RX_PW_P3 0x14 // 接收頻道0接收數(shù)據(jù)長度

          #define RX_PW_P4 0x15 // 接收頻道0接收數(shù)據(jù)長度

          #define RX_PW_P5 0x16 // 接收頻道0接收數(shù)據(jù)長度

          #define FIFO_STATUS 0x17 // FIFO棧入棧出狀態(tài)寄存器設(shè)置

          /* 打開計(jì)數(shù) */
          uint open_count = 0;

          /* 狀態(tài)標(biāo)識(shí) */
          uint8 receive_state;

          int get_data=0;

          wait_queue_head_t read_queue; //讀取等待隊(duì)列

          #define RX_DR 6
          #define TX_DS 5
          #define MAX_RT 4

          /* unit8 SPI_RW(uint8 tmp)
          * SPI寫時(shí)序,寫一個(gè)字節(jié)到MOSI同時(shí)從MISO中讀取一個(gè)字節(jié) */
          uint8 SPI_RW(uint8 tmp)
          { uint8 bit_ctl;

          for (bit_ctl = 0; bit_ctl < 8;bit_ctl++){
          if(tmp & 0x80)
          MOSI_H;
          else
          MOSI_L;

          tmp = tmp << 1; //Shift next bit into MSB

          SCK_H; //Set SCK high

          ndelay(60);

          tmp |= MISO_STU; //Capture current MISO bit

          SCK_L;

          ndelay(60);
          }
          return tmp;
          }

          /*
          * 函數(shù):uint8 SPI_Read(uint8 reg)
          * 功能:NRF24L01的SPI時(shí)序
          */
          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讀寫寄存器函數(shù)

          uint8 SPI_RW_Reg(uint8 reg, uint8 value)
          { uint8 status;

          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

          }

          //函數(shù):uint8 SPI_Read_Buf(uint8 reg, uint8 *pBuf, uint8 uchars)

          //功能: 用于讀數(shù)據(jù),reg:為寄存器地址,pBuf:為待讀出數(shù)據(jù)地址,uchars:讀出數(shù)據(jù)的個(gè)數(shù)

          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

          }

          //函數(shù):uint8 SPI_Write_Buf(uint8 reg, uint8 *pBuf, uint8 uchars)

          //功能: 用于寫數(shù)據(jù):為寄存器地址,pBuf:為待寫入數(shù)據(jù)地址,uchars:寫入數(shù)據(jù)的個(gè)數(shù)

          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; //關(guān)閉SPI

          ndelay(60);
          return (status); //
          }

          //函數(shù):void SetRX_Mode(void)

          //功能:數(shù)據(jù)接收配置

          void SetRX_Mode(void)
          {

          CE_L;
          ndelay(60);
          // SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收發(fā)完成中斷響應(yīng),16位CRC ,主接收

          //udelay(1);

          CE_H;
          udelay(130);
          }

          //函數(shù):unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)

          //功能:數(shù)據(jù)讀取后放如rx_buf接收緩沖區(qū)中

          unsigned char nRF24L01_RxPacket(unsigned char *rx_buf)
          { unsigned char revale = 0;

          receive_state = SPI_Read(STATUS); // 讀取狀態(tài)寄存其來判斷數(shù)據(jù)接收狀況

          if (receive_state & (1 << RX_DR)) // 判斷是否接收到數(shù)據(jù)

          {
          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; //讀取數(shù)據(jù)完成標(biāo)志

          }
          SPI_RW_Reg(WRITE_REG + STATUS, receive_state); //接收到數(shù)據(jù)后RX_DR,TX_DS,MAX_PT都置高為1,通過寫1來清楚中斷標(biāo)志

          return revale;
          }

          //函數(shù):void nRF24L01_TxPacket(unsigned char * tx_buf)

          //功能:發(fā)送 tx_buf中數(shù)據(jù)

          void nRF24L01_TxPacket(unsigned char *tx_buf)
          { uint8 ret;
          CE_L; //StandBy I模式

          ndelay(60);
          ret=SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 裝載接收端地址
          printk(“ret=%cn”,ret);
          ret=SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // 裝載數(shù)據(jù)
          printk(“ret=%cn”,ret);

          SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收發(fā)完成中斷響應(yīng),16位CRC,主發(fā)送

          CE_H; //置高CE,激發(fā)數(shù)據(jù)發(fā)送

          udelay(10);
          }

          static irqreturn_t nrf24l01_interrupt(int irq,void *dev_id)
          { uint8 state ;
          state = SPI_Read(STATUS);
          if(state & 0x10){
          SPI_RW_Reg(WRITE_REG + STATUS , state); //如果是重發(fā)中斷則寫回清除中斷
          }else if(state & 0x20){
          SPI_RW_Reg(WRITE_REG + STATUS , state); //清除發(fā)送中斷
          }else if ( state & 0x40){
          get_data = 1;
          nRF24L01_RxPacket(TxBuf);
          wake_up_interruptible(&read_queue);
          }
          return IRQ_RETVAL(IRQ_HANDLED);
          }

          uint8 init_NRF24L01(void)
          { 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自動(dòng) ACK應(yīng)答允許

          SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // 允許接收地址只有頻道0,如果需要多頻道可以參考Page21

          SPI_RW_Reg(WRITE_REG + RF_CH, 0); // 設(shè)置信道工作為2.4GHZ,收發(fā)必須一致

          SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //設(shè)置接收數(shù)據(jù)長度,本次設(shè)置為32字節(jié)

          SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //設(shè)置發(fā)射速率為1MHZ,發(fā)射功率為最大值0dB

          SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收發(fā)完成中斷響應(yīng),16位CRC ,主接收
          mdelay(1000);
          nRF24L01_TxPacket(TxBuf);
          SPI_RW_Reg(WRITE_REG + STATUS, 0XFF);
          printk(“test 1 n”);
          mdelay(1000);
          return 1;
          }

          static uint16 nrf24l01_poll(struct file *filp,struct poll_table_struct *wait)
          { uint16 mask = 0;
          poll_wait(filp,&read_queue,wait);
          if(get_data){
          mask |= POLLIN|POLLRDNORM;
          }

          return mask;
          } static ssize_t nrf24l01_write(struct file *filp,const char *buffer, size_t count,loff_t *ppos)
          { if(copy_from_user(TxBuf,buffer,count))
          {
          printk(“Can’t Send Data !”);
          return -EFAULT;
          }

          nRF24L01_TxPacket(TxBuf);
          SPI_RW_Reg(WRITE_REG + STATUS,0XFF);
          printk(“Send OK n”);
          return 10;
          }

          static ssize_t nrf24l01_read(struct file * filp,char *buffer,size_t count,loff_t *ppos)
          { unsigned long err;
          if(!get_data){
          if(filp->f_flags & O_NONBLOCK)
          return -EAGAIN;
          else
          wait_event_interruptible(read_queue,get_data);
          }
          get_data = 0;
          err = copy_to_user(buffer,TxBuf,min(TxBufSize,count));

          printk(“read okn”);
          return err ? -EFAULT : min(TxBufSize,count);
          }

          static int nrf24l01_open(struct inode *node, struct file *file)
          { uint8 flag = 0;
          unsigned long err;
          if (open_count == 1)
          return -EBUSY;

          flag = init_NRF24L01();

          mdelay(100);
          init_waitqueue_head(&read_queue);

          err = request_irq(IRQ_EINT9,nrf24l01_interrupt,IRQ_TYPE_EDGE_FALLING,DEVICE_NAME,NULL);
          if(err){
          disable_irq(IRQ_EINT9);
          free_irq(IRQ_EINT9,NULL);
          }
          if (flag == 0) {
          printk(“uable to open device!n”);
          return -1;
          } else {
          open_count++;
          printk(“device opened !n”);
          return 0;
          }
          }

          static int nrf24l01_release(struct inode *node, struct file *file)
          { free_irq(IRQ_EINT9,NULL);
          open_count–;
          printk(DEVICE_NAME ” released !n”);
          return 0;
          }

          static struct file_operations nrf24l01_fops = {
          .owner = THIS_MODULE,
          .open = nrf24l01_open,
          .write = nrf24l01_write,
          .poll = nrf24l01_poll,
          .read = nrf24l01_read,
          .release = nrf24l01_release,
          };

          static struct miscdevice nrf24l01_dev = {
          .minor = MISC_DYNAMIC_MINOR,
          .name = DEVICE_NAME,
          .fops = &nrf24l01_fops,
          };

          static int __init nrf24l01_init(void)
          { int ret;

          printk(“Initial driver for NRF24L01.n”);
          ret = misc_register(&nrf24l01_dev);
          mdelay(10);
          if (ret < 0) {
          printk(DEVICE_NAME ” can’t register major numbern”);
          return ret;

          } else {
          printk(DEVICE_NAME ” register successn”);
          return 0;
          }
          }

          static void __exit nrf24l01_exit(void)
          { misc_deregister(&nrf24l01_dev);
          printk(“NRF24L01 unregister success n”);
          }

          module_init(nrf24l01_init);
          module_exit(nrf24l01_exit);
          MODULE_AUTHOR(“Issac”);
          MODULE_DESCRIPTION(“NRF24L01 Driver”);
          MODULE_LICENSE(“GPL”);備注:不同管腳的定義可以自行修改。
          我添加了poll方法 和接收中斷的處理
          由于MSP的程序沒有調(diào)好,這個(gè)驅(qū)動(dòng)還沒有進(jìn)行測試。在內(nèi)核中部署驅(qū)動(dòng):
          將nrf24l01.c文件到源碼下driver/misc/ 目錄下
          修改該目錄下的Kconfig文件
          在合適位置添加

          config NRF24L01tristate "NRF24L01 Single Chip 2.4 GHz Radio Transceiver"helpDriver for NRF24L01

          修改Makefile添加

          obj-$(CONFIG_NRF24L01)		+= nrf24l01.o
          然后make menuconfig
          選上NRF24L01 驅(qū)動(dòng)即可

          測試程序:僅測試了發(fā)送

          #include 

          #include

          #include

          #include

          #include

          #include

          #include

          #include

          #include

          #include

          unsigned char TxBuf[32] = {
          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,
          };
          int main(void)

          {

          int fd = -1;
          int ret;
          int count = 1;

          fd = open(“/dev/NRF24L01”, O_RDWR);

          if(fd < 0)

          {

          perror(“Can’t open /dev/nrf24l01 n”);

          exit(1);

          }

          printf(“open /dev/nrf24l01 success n”);

          while(count <= 5)
          {

          ret = write(fd, TxBuf , sizeof(TxBuf));

          char *mesg = strerror(errno);

          printf(“Sending %d time %d n”, count,ret);
          printf(“Errno:%dn,Mesg:%sn”,errno,mesg);
          usleep(100*1000);

          count++;
          }

          close(fd);
          }



          評(píng)論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();