[原创]基于Blackfin系列的字符设备驱动开发
0赞目的:在pc机上编写简单的虚拟硬件驱动程序并进行调试,实验Blackfin驱动的各个接口函数的实现,分析并理解驱动与应用程序的交互过程.字符设备驱动的整个过程写下来:
1 字符设备驱动需要的头文件
#include <uclinux/config.h>
#include <uclinux/module.h>
#include <uclinux/kernel.h>
#include <uclinux/init.h>
#include <uclinux/miscdevice.h>
#include <uclinux/sched.h>
#include <uclinux/delay.h>
#include <uclinux/poll.h>
#include <uclinux/spinlock.h>
#include <uclinux/irq.h>
#include <uclinux/delay.h>
#include <asm/hardware.h>
2 初始化函数
static int __init Blackfin_test_init(void)
{
int ret;
printk("hello world!\n");
printk("insmod !\n");
}
3 退出函数
static void __exit Blackfin_test_exit(void)
{
unregister_chrdev(testMajor,DEVICE_NAME);
printk("rmmod!\n");
}
4 fops结构体的定义
static struct file_operationsBlackfin_test_fops = {
owner: THIS_MODULE,
open: Blackfin_test_open,
read: Blackfin_test_read,
release: Blackfin_test_release,
};
5 open函数
static int Blackfin_test_open(struct inode *inode, struct file *filp)
{
MOD_INC_USE_COUNT;
return 0;
}
6 release函数
static int sBlackfin_test_release(struct inode *inode, struct file *filp)
{
MOD_DEC_USE_COUNT;
return 0;
}
7 read函数
static ssize_t Blackfin_test_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)
{
char * test;
int count = 20;
test = testread;
copy_to_user(buffer,test,count);
return count;
}
为了将此字符设备模块编译成模块需要编辑Config.in,Makefile这两个文件
- 在Config.in文件中字符设备部分添加
dep_tristate 'Support S3C2410 Test' CONFIG_BLACKFIN_TEST $CONFIG_ARCH_BLACKFIN
- 在Makefile文件中添加
obj-$(CONFIG_Blackfin_TEST) +=blackfin-test.o
编辑修改之就可以开始进行模块的编译,当编译完成后需要通过网络或串口将编译好的模块通过insmod命令插入到系统中,最后使用一个应用程序,通过字符设备驱动中读取其中的内容,下面附上测试应用程序代码。
#include <stdio.h>
#include <fcntl.h>
int main(void)
{
int fd;
char buffer[20] = "this is test";
fd = open("/dev/test",O_RDONLY);
if(fd == -1)
{
printf("unable to open the test device\n");
return 0;
}
printf("the fd is %d \n",fd);
while(1)
{
read(fd,buffer,20);
printf("read from driver:%s\n",buffer);
sleep(5);
}
}
