为了和jihuaLi 完结智能家居体系,我移植了这个驱动,不论写的怎么,总结一下是很有必要的。
NRF24L01运用SPI总线与主机通讯,没有SPI端口的设备能够运用IO口进行模仿。
关于SPI:
SPI是一种四线串行总线,
SCLK: 串行时钟线
MOSI: 总线主机输出/ 从机输入
MISO: 总线主机输入/ 从机输出;
SS: 从机使能数据传输办法
通讯是经过数据交换完结的,这儿先要知道SPI是串行通讯协议,也便是说数据是一位一位的传输的。这便是SCLK时钟线存在的原因,由SCK供给时钟脉冲,SDI,SDO则基于此脉冲完结数据传输。数据输出经过 SDO线,数据在时钟上升沿或下降沿时改动,在紧接着的下降沿或上升沿被读取。完结一位数据传输,输入也运用相同原理。这样,在至少8次时钟信号的改动(上沿和下沿为一次),就能够完结8位数据的传输。
NRF24L01存放器阐明:
仍是看阐明书吧,dbank驱动源码鄙人一页。驱动源码:
#include #include #include #include #include #include
#include#include#include#include#include#include#include#include#include#include
typedef unsigned int uint16;
typedef unsigned char uint8;
/* 引脚相关界说 */
#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) //数据线设置为输出
#define CE_UP s3c2410_gpio_pullup(CE, 1) //翻开上拉电阻
#define CE_L s3c2410_gpio_setpin(CE, 0) //拉低数据线电平
#define CE_H s3c2410_gpio_setpin(CE, 1) //拉高数据线电平
#define SCK_OUT s3c2410_gpio_cfgpin(SCK, SCK_OUTP) //数据线设置为输出
#define SCK_H s3c2410_gpio_setpin(SCK, 1) //拉高数据线电平
#define SCK_L s3c2410_gpio_setpin(SCK, 0) //拉高数据线电平
#define MISO_IN s3c2410_gpio_cfgpin(MISO, MISO_INP) //数据线设置为输出
#define MISO_UP s3c2410_gpio_pullup(MISO, 1) //翻开上拉电阻
#define MISO_STU s3c2410_gpio_getpin(MISO) //数据情况
#define IRQ_IN s3c2410_gpio_cfgpin(IRQ, IRQ_INP) //数据线设置为输出
#define IRQ_UP s3c2410_gpio_pullup(IRQ, 1) //翻开上拉电阻
#define IRQ_L s3c2410_gpio_setpin(IRQ, 0) //拉低数据线电平
#define IRQ_H s3c2410_gpio_setpin(IRQ, 1) //拉高数据线电平
#define MOSI_OUT s3c2410_gpio_cfgpin(MOSI, MOSI_OUTP) //数据线设置为输出
#define MOSI_UP s3c2410_gpio_pullup(MOSI, 1) //翻开上拉电阻
#define MOSI_L s3c2410_gpio_setpin(MOSI, 0) //拉低数据线电平
#define MOSI_H s3c2410_gpio_setpin(MOSI, 1) //拉高数据线电平
#define CSN_OUT s3c2410_gpio_cfgpin(CSN, CSN_OUTP) //数据线设置为输出
#define CSN_UP s3c2410_gpio_pullup(CSN, 1) //翻开上拉电阻
#define CSN_L s3c2410_gpio_setpin(CSN, 0) //拉低数据线电平
#define CSN_H s3c2410_gpio_setpin(CSN, 1) //拉高数据线电平
//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 // 读取接纳数据指令
#define WR_TX_PLOAD 0xA0 // 写待发数据指令
#define FLUSH_TX 0xE1 // 冲刷发送 FIFO指令
#define FLUSH_RX 0xE2 // 冲刷接纳 FIFO指令
#define REUSE_TX_PL 0xE3 // 界说重复装载数据指令
#define NOP 0xFF // 保存
//SPI(nRF24L01)存放器地址
#define CONFIG 0x00 // 装备收发情况,CRC校验形式以及收发情况呼应办法
#define EN_AA 0x01 // 主动应对功用设置
#define EN_RXADDR 0x02 // 可用信道设置
#define SETUP_AW 0x03 // 收发地址宽度设置
#define SETUP_RETR 0x04 // 主动重发功用设置
#define RF_CH 0x05 // 作业频率设置
#define RF_SETUP 0x06 // 发射速率、功耗功用设置
#define STATUS 0x07 // 情况存放器
#define OBSERVE_TX 0x08 // 发送监测功用
#define CD 0x09 // 地址检测
#define RX_ADDR_P0 0x0A // 频道0接纳数据地址
#define RX_ADDR_P1 0x0B // 频道1接纳数据地址
#define RX_ADDR_P2 0x0C // 频道2接纳数据地址
#define RX_ADDR_P3 0x0D // 频道3接纳数据地址
#define RX_ADDR_P4 0x0E // 频道4接纳数据地址
#define RX_ADDR_P5 0x0F // 频道5接纳数据地址
#define TX_ADDR 0x10 // 发送地址存放器
#define RX_PW_P0 0x11 // 接纳频道0接纳数据长度
#define RX_PW_P1 0x12 // 接纳频道0接纳数据长度
#define RX_PW_P2 0x13 // 接纳频道0接纳数据长度
#define RX_PW_P3 0x14 // 接纳频道0接纳数据长度
#define RX_PW_P4 0x15 // 接纳频道0接纳数据长度
#define RX_PW_P5 0x16 // 接纳频道0接纳数据长度
#define FIFO_STATUS 0x17 // FIFO栈入栈出情况存放器设置
/* 翻开计数 */
uint open_count = 0;
/* 情况标识 */
uint8 receive_state;
int get_data=0;
wait_queue_head_t read_queue; //读取等候行列
#define RX_DR 6
#define TX_DS 5
#define MAX_RT 4
/* unit8 SPI_RW(uint8 tmp)
* SPI写时序,写一个字节到MOSI一起从MISO中读取一个字节 */
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;
}
/*
* 函数: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;
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
}
//函数:uint8 SPI_Read_Buf(uint8 reg, uint8 *pBuf, uint8 uchars)
//功用: 用于读数据,reg:为存放器地址,pBuf:为待读出数据地址,uchars:读出数据的个数
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
}
//函数:uint8 SPI_Write_Buf(uint8 reg, uint8 *pBuf, uint8 uchars)
//功用: 用于写数据:为存放器地址,pBuf:为待写入数据地址,uchars:写入数据的个数
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; //封闭SPI
ndelay(60);
return (status); //
}
//函数:void SetRX_Mode(void)
//功用:数据接纳装备
void SetRX_Mode(void)
{
CE_L;
ndelay(60);
// SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收发完结中止呼应,16位CRC ,主接纳
//udelay(1);
CE_H;
udelay(130);
}
//函数:unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
//功用:数据读取后放如rx_buf接纳缓冲区中
unsigned char nRF24L01_RxPacket(unsigned char *rx_buf)
{ unsigned char revale = 0;
receive_state = SPI_Read(STATUS); // 读取情况存放其来判别数据接纳情况
if (receive_state & (1 << RX_DR)) // 判别是否接纳到数据
{
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; //读取数据完结标志
}
SPI_RW_Reg(WRITE_REG + STATUS, receive_state); //接纳到数据后RX_DR,TX_DS,MAX_PT都置高为1,经过写1来清楚中止标志
return revale;
}
//函数:void nRF24L01_TxPacket(unsigned char * tx_buf)
//功用:发送 tx_buf中数据
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); // 装载数据
printk(“ret=%cn”,ret);
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完结中止呼应,16位CRC,主发送
CE_H; //置高CE,激起数据发送
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); //假如是重发中止则写回铲除中止
}else if(state & 0x20){
SPI_RW_Reg(WRITE_REG + STATUS , state); //铲除发送中止
}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主动 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);
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_L%&&&&&%ENSE(“GPL”);补白:不同管脚的界说能够自行修正。
我增加了poll办法 和接纳中止的处理
我们MSP的程序没有调好,这个驱动还没有进行测验。在内核中布置驱动:
将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 驱动即可
测验程序:仅测验了发送
#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);
}