/************************************************************/
//文件名:nrf24l01.c
//功能:linux下的nrf24l01驱动程序
/************************************************************/
#include
#include
#include
#include
#include
#include
#include
//#include
#include
#include
#include
typedef unsigned int uint16 ;
typedef unsigned char uint8 ;
//和引脚相关的宏定义
#define CSN S3C2410_GPG2
#define CSN_OUTP S3C2410_GPG2_OUTP
#define MOSI S3C2410_GPG3
#define MOSI_OUTP S3C2410_GPG3_OUTP
#define IRQ S3C2410_GPG7
#define IRQ_INP S3C2410_GPG7_INP
#define MISO S3C2410_GPG11
#define MISO_INP S3C2410_GPG11_INP
#define SCK S3C2410_GPG5
#define SCK_OUTP S3C2410_GPG5_OUTP
#define CE S3C2410_GPG6
#define CE_OUTP S3C2410_GPG6_OUTP
#define DEVICE_NAME "NRF24L01" //设备名称,在可以 /proc/devices 查看
#define NRF24L01_MAJOR 241 //主设备号
#define TxBufSize 32
#define RxBufSize 32
uint8 TxBuf[TxBufSize]={0X00};
uint8 RxBuf[RxBufSize]={0x00};
//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_UP s3c2410_gpio_pullup(SCK, 1) //打开上拉电阻
#define SCK_L s3c2410_gpio_setpin(SCK, 0) //拉低数据线电平
#define SCK_H s3c2410_gpio_setpin(SCK, 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) //拉高数据线电平
#define MISO_IN s3c2410_gpio_cfgpin(MISO, MISO_INP) //数据线设置为input
#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) //拉高数据线电平
//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 const TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //本地地址
uint8 const 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栈入栈出状态寄存器设置
uint8 init_NRF24L01(void);
uint8 SPI_RW(uint8 tmp);
uint8 SPI_Read(uint8 reg);
void SetRX_Mode(void);
uint8 SPI_RW_Reg(uint8 reg, uint8 value);
uint8 SPI_Read_Buf(uint8 reg, uint8 *pBuf, uint8 uchars);
uint8 SPI_Write_Buf(uint8 reg, uint8 *pBuf, uint8 uchars);
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf);
void nRF24L01_TxPacket(unsigned char * tx_buf);
//全局变量
uint8 opencount = 0;
//
uint8 sta; //状态标志
#define RX_DR 6
#define TX_DS 5
#define MAX_RT 4
//NRF24L01初始化
uint8 init_NRF24L01(void)
{
MISO_UP;
udelay(20);
CE_OUT;
udelay(20);
CSN_OUT;
udelay(20);
SCK_OUT;
udelay(20);
MOSI_OUT;
udelay(20);
MISO_IN;
udelay(20);
IRQ_IN;
udelay(600);
CE_L; // chip enable
udelay(20);
CSN_H; // Spi disable
udelay(20);
SCK_L; // Spi clock line init high
udelay(20);
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // 写本地地址
udelay(10);
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // 写接收端地址
udelay(10);
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // 频道0自动 ACK应答允许
udelay(10);
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // 允许接收地址只有频道0,如果需要多频道可以参考Page21
SPI_RW_Reg(WRITE_REG + RF_CH, 0); // 设置信道工作为2.4GHZ,收发必须一致
udelay(10);
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节
//SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //设置发射速率为1MHZ,发射功率为最大值0dB
udelay(10);
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f); //设置发射速率为1MHZ,发射功率为最大值0dB
mdelay(1000);
return (1);
}
//函数:uint8 SPI_RW(uint8 tmp)
//功能:NRF24L01的SPI写时序tmp
uint8 SPI_RW(uint8 tmp)
{
uint8 bit_ctr;
for(bit_ctr=0 ;bit_ctr<8 ;bit_ctr++) // output 8-bit
{
if(tmp & 0x80) // output 'tmp', MSB to MOSI
MOSI_H;
else
MOSI_L;
udelay(10);
tmp <<= 1; // shift next bit into MSB..
udelay(5);
SCK_H; // Set SCK high..
udelay(20);
if(MISO_STU)
tmp |= 0x01;
udelay(5);
SCK_L; // ..then set SCK low again
ndelay(20);
}
return(tmp); // return read tmp
}
//函数:uint8 SPI_Read(uint8 reg)
//功能:NRF24L01的SPI时序
uint8 SPI_Read(uint8 reg)
{
uint8 reg_val;
CSN_L; // CSN low, initialize SPI communication...
udelay(60);
SPI_RW(reg); // Select register to read from..
reg_val = SPI_RW(0); // ..then read registervalue
CSN_H; // CSN high, terminate SPI communication
udelay(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
// udelay(60);
status = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it..
CSN_H; // CSN high again
// udelay(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
udelay(20);
status = SPI_RW(reg); // Select register to write to and read status uint8
for(uint8_ctr=0;uint8_ctr
pBuf[uint8_ctr] = SPI_RW(0); //
//udelay(10);
}
CSN_H;
udelay(20);
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使能
udelay(20);
status = SPI_RW(reg);
for(uint8_ctr=0; uint8_ctr
SPI_RW(*pBuf++);
udelay(10);
}
CSN_H; //关闭SPI
udelay(20);
return(status); //
}
//函数:void SetRX_Mode(void)
//功能:数据接收配置
void SetRX_Mode(void)
{
CE_L;
udelay(20);
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收发完成中断响应,16位CRC ,主接收
//udelay(100);
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;
sta=SPI_Read(STATUS); // 读取状态寄存其来判断数据接收状况
udelay(10);
if(sta & (1<
CE_L; //SPI使能
udelay(100);
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,sta); //接收到数据后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)
{
CE_L; //StandBy I模式
udelay(20);
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 装载接收端地址
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // 装载数据
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,主发送
CE_H; //置高CE,激发数据发送
udelay(20);
}
//文件的写函数
static ssize_t nrf24l01_write(struct file *filp, const char *buffer,
size_t count, loff_t *ppos)
{
int i =0;
copy_from_user( &TxBuf, buffer, count );
nRF24L01_TxPacket(TxBuf);
udelay(100);
SPI_RW_Reg(FLUSH_TX,0x00);
SPI_RW_Reg(WRITE_REG+STATUS,0x7F);
memset(TxBuf,0,sizeof(TxBuf));
return(10);
}
//的读函数
static ssize_t nrf24l01_read(struct file *filp, char *buffer,
size_t count, loff_t *ppos)
{
int i =0;
SetRX_Mode();
udelay(1000);
nRF24L01_RxPacket(RxBuf);
udelay(1000);
if(copy_to_user(buffer,RxBuf, count))
{
printk("can not recv data\n");
return -EFAULT;
}
return (10);
}
static int nrf24l01_open(struct inode *node, struct file *file)
{
uint8 flag = 0;
if(opencount == 1)
return -EBUSY;
flag = init_NRF24L01();
mdelay(100);
if(flag == 0)
{
printk("uable to open device!\n");
return -1;
}
else
{
opencount++;
printk("device opened !\n");
return 0;
}
}
static int nrf24l01_release(struct inode *node, struct file *file)
{
opencount--;
printk(DEVICE_NAME " released !\n");
return 0;
}
static struct file_operations nrf24l01_fops = {
.owner = THIS_MODULE,
.open = nrf24l01_open,
.write = nrf24l01_write,
.read = nrf24l01_read,
.release = nrf24l01_release,
};
static int __init nrf24l01_init(void)
{
int ret;
printk("Initial driver for NRF24L01......................\n");
ret = register_chrdev(NRF24L01_MAJOR, DEVICE_NAME, &nrf24l01_fops);
mdelay(10);
if (ret < 0)
{
printk(DEVICE_NAME " can't register major number\n");
return ret;
}
else
{
printk(DEVICE_NAME " register success\n");
return 0;
}
}
static void __exit nrf24l01_exit(void)
{
unregister_chrdev(NRF24L01_MAJOR, DEVICE_NAME);
printk("NRF24L01 unregister success \n");
}
module_init(nrf24l01_init);
module_exit(nrf24l01_exit);
MODULE_AUTHOR("phoenixTree");
MODULE_DESCRIPTION("nrf24l01 driver for TQ2440");
MODULE_LICENSE("GPL");