//文件的写函数
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("OK! \n");
return(10);
}
//的读函数
static ssize_t nrf24l01_read(struct file *filp, char *buffer,
size_t count, loff_t *ppos)
{
nRF24L01_TxPacket(TxBuf);
SPI_RW_Reg(WRITE_REG+STATUS,0XFF);
printk("read \n");
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("jammy_lee@163.com");
MODULE_DESCRIPTION("nrf24l01 driver for TQ2440");
MODULE_LICENSE("GPL");
--------------------------------------------------------------------------------
测试程序:
/************************************************************/
//文件名:test_ds18b20.c
//功能:测试linux下的ds18b20程序
//使用说明: (1)
// (2)
// (3)
// (4)
//作者:jammy-lee
//日期:2010-01-18
/************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
unsigned char TxBuf[32] = {0x00};
int main(void)
{
int fd = -1;
int count = 1;
//fd = open("/dev/nrf24l01", 0);
fd = open("/dev/nrf24l01", O_RDWR); //打开nrf24l01为可读写文件
if(fd < 0)
{
perror("Can't open /dev/nrf24l01 \n");
exit(1);
}
printf("open /dev/nrf24l01 success \n");
while(count <= 5)
{
write(fd, &TxBuf , sizeof(TxBuf));
printf("Sending %d time \n", count);
usleep(100*1000);
count++;
}
close(fd);
}