一、字符设备驱动程序介绍
app里面用 open、read、write等等函数出来操作底层硬件。驱动程序中也有对应的xxx_open等函数。怎么找到驱动程序中的函数依赖于驱动程序框架。
二、搭建驱动程序框架
2.1 初步框架
2.1.1 Makefile
2.1.2 jz2440_led.c
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/regmap.h>
#include <linux/fs.h> #define JZ2440_LEDS_MAJOR 111 /* 主设备号 */ static int jz2440_drv_open(struct inode *inode, struct file *file)
{
printk("jz2440_drv_open");
return ;
} static int jz2440_drv_close(struct inode *inode, struct file *file)
{
printk("jz2440_drv_close");
return ;
} static ssize_t jz2440__drv_read(struct file *file, char __user * buf, size_t count, loff_t * ppos)
{
printk("jz2440__drv_read");
return ;
} static ssize_t jz2440_drv_write(struct file *file, const char __user * buf, size_t count, loff_t * ppos)
{
printk("jz2440_drv_write");
return ;
} static struct file_operations jz2440_leds_ops = {
.owner = THIS_MODULE,
.read = jz2440__drv_read,
.open = jz2440_drv_open,
.write = jz2440_drv_write,
.release = jz2440_drv_close,
}; static int __init jz2440_drv_leds_init(void)
{
register_chrdev(JZ2440_LEDS_MAJOR,"jz2440_drv_leds",&jz2440_leds_ops);//注册驱动程序,即告诉内核
return ;
} static void __exit jz2440_drv_leds_exit(void)
{
unregister_chrdev(JZ2440_LEDS_MAJOR,"jz2440_drv_leds");
} //指向入口函数 jz2440_drv_leds_init
module_init(jz2440_drv_leds_init);
//出口函数 卸载驱动
module_exit(jz2440_drv_leds_exit); MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("jz2440 leds driver");
编译:
烧写运行:
进入开发板u-boot界面:
设置命令行启动参数
set bootargs noinitrd root=/dev/nfs nfsroot=192.168.0.192:/home/ubuntu/work/nfs_root/fs_mini ip=192.168.0.191:192.168.0.192:192.168.0.1:255.255.255.0::eth0:off init=/linuxrc console=ttySAC0,115200
启动内核:boot
nfs目录挂载成功。
查看proc目录:
设备号可用。
加载模块:
查看是否加载进去:
测试程序
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h> int main(int argc, char **argv)
{
int fd;
int val = ; fd = open("/dev/xxx", O_RDWR);
if(fd < )
{
printf("can't open !\n");
} write(fd, &val, );
return ;
}
编译烧写进板子,执行
创建设备节点;
执行 a.out
卸载驱动 rmmod jz2440_led
删除节点: rm -rf /dev/xxx