Linux 字符设备驱动开发基础(一)—— 编写简单 LED 设备驱动
现在,我们来编写自己第一个字符设备驱动 —— 点亮LED。(不完善,后面再完善)
硬件平台:Exynos4412(FS4412)
编写驱动分下面几步:
a -- 查看原理图、数据手册,了解设备的操作方法;
b -- 在内核中找到相近的驱动程序,以它为模板进行开发,有时候需要从零开始;
c -- 实现驱动程序的初始化:比如向内核注册这个驱动程序,这样应用程序传入文件名,内核才能找到相应的驱动程序;
d -- 设计所要实现的操作,比如 open、close、read、write 等函数;
e -- 实现中断服务(中断不是每个设备驱动所必须的);
f -- 编译该驱动程序到内核中,或者用 insmod 命令加载;
g-- 测试驱动程序;
下面是一个点亮LED 的驱动:
第一步,当然是查看手册,查看原理图,找到相应寄存器;
查看手册,四个LED 所用寄存器为:
led2
GPX2CON 0x11000c40
GPX2DAT 0x11000c44
led3
GPX1CON 0x11000c20
GPX1DAT 0x11000c24
led4 3-4 3-5
GPF3CON 0x114001e0
GPF3DAT 0x114001e4
这里要注意:arm体系架构是io内存,必须要映射 ioremap( ); 其作用是物理内存向虚拟内存的映射。 用到 writel readl这两个函数,详细解释会在后面不上,先看一下简单用法:
以LED2为例,下面是地址映射及读写:
- int *pgpx2con ;
- int *pgpx2dat;
- pgpx2con = ioremap( GPX2CON, 4);
- pgpx2dat = ioremap(GPX2DAT,4);
- readl(pgpx2con);
- writel(0x01, pgpx2dat );
下面是驱动程序,后面会更完善
- #include <linux/module.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/device.h>
- #include <asm/io.h>
- #include <asm/uaccess.h>
- static int major = 250;
- static int minor=0;
- static dev_t devno;
- static struct class *cls;
- static struct device *test_device;
- #define GPX2CON 0x11000c40
- #define GPX2DAT 0x11000c44
- #define GPX1CON 0x11000c20
- #define GPX1DAT 0x11000c24
- #define GPF3CON 0x114001e0
- #define GPF3DAT 0x114001e4
- static int *pgpx2con ;
- static int *pgpx2dat;
- static int *pgpx1con ;
- static int *pgpx1dat;
- static int *pgpf3con ;
- static int *pgpf3dat;
- void fs4412_led_off(int num);
- void fs4412_led_on(int num)
- {
- switch(num)
- {
- case 1:
- writel(readl(pgpx2dat) |(0x1<<7), pgpx2dat);
- break;
- case 2:
- writel(readl(pgpx1dat) |(0x1<<0), pgpx1dat);
- break;
- case 3:
- writel(readl(pgpf3dat) |(0x1<<4), pgpf3dat);
- break;
- case 4:
- writel(readl(pgpf3dat) |(0x1<<5), pgpf3dat);
- break;
- default:
- fs4412_led_off(1);
- fs4412_led_off(2);
- fs4412_led_off(3);
- fs4412_led_off(4);
- break;
- }
- }
- void fs4412_led_off(int num)
- {
- switch(num)
- {
- case 1:
- writel(readl(pgpx2dat) &(~(0x1<<7)), pgpx2dat);
- break;
- case 2:
- writel(readl(pgpx1dat)&(~(0x1<<0)), pgpx1dat);
- break;
- case 3:
- writel(readl(pgpf3dat) &(~(0x1<<4)), pgpf3dat);
- break;
- case 4:
- writel(readl(pgpf3dat) &(~(0x1<<5)), pgpf3dat);
- break;
- }
- }
- static int led_open (struct inode *inode, struct file *filep)
- {//open
- fs4412_led_off(1);
- fs4412_led_off(2);
- fs4412_led_off(3);
- fs4412_led_off(4);
- return 0;
- }
- static int led_release(struct inode *inode, struct file *filep)
- {//close
- fs4412_led_off(1);
- fs4412_led_off(2);
- fs4412_led_off(3);
- fs4412_led_off(4);
- return 0;
- }
- static ssize_t led_read(struct file *filep, char __user *buf, size_t len, loff_t *pos)
- {
- return 0;
- }
- static ssize_t led_write(struct file *filep, const char __user *buf, size_t len, loff_t *pos)
- {
- int led_num;
- if(len !=4)
- {
- return -EINVAL;
- }
- if(copy_from_user(&led_num,buf,len))
- {
- return -EFAULT;
- }
- fs4412_led_on(led_num);
- printk("led_num =%d \n",led_num);
- return 0;
- }
- static struct file_operations hello_ops=
- {
- .open = led_open,
- .release = led_release,
- .read = led_read,
- .write = led_write,
- };
- static void fs4412_led_init(void)
- {
- pgpx2con = ioremap(GPX2CON,4);
- pgpx2dat = ioremap(GPX2DAT,4);
- pgpx1con = ioremap(GPX1CON,4);
- pgpx1dat =ioremap(GPX1DAT,4);
- pgpf3con = ioremap(GPF3CON,4);
- pgpf3dat =ioremap(GPF3DAT,4);
- writel((readl(pgpx2con)& ~(0xf<<28)) |(0x1<<28),pgpx2con) ;
- writel((readl(pgpx1con)& ~(0xf<<0)) |(0x1<<0),pgpx1con) ;
- writel((readl(pgpf3con)& ~(0xff<<16)) |(0x11<<16),pgpf3con) ;
- }
- static int led_init(void)
- {
- int ret;
- devno = MKDEV(major,minor);
- ret = register_chrdev(major,"led",&hello_ops);
- cls = class_create(THIS_MODULE, "myclass");
- if(IS_ERR(cls))
- {
- unregister_chrdev(major,"led");
- return -EBUSY;
- }
- test_device = device_create(cls,NULL,devno,NULL,"led");//mknod /dev/hello
- if(IS_ERR(test_device))
- {
- class_destroy(cls);
- unregister_chrdev(major,"led");
- return -EBUSY;
- }
- fs4412_led_init();
- return 0;
- }
- void fs4412_led_unmap(void)
- {
- iounmap(pgpx2con);
- iounmap(pgpx2dat );
- iounmap(pgpx1con);
- iounmap(pgpx1dat );
- iounmap(pgpf3con );
- iounmap(pgpf3dat );
- }
- static void led_exit(void)
- {
- fs4412_led_unmap();
- device_destroy(cls,devno);
- class_destroy(cls);
- unregister_chrdev(major,"led");
- printk("led_exit \n");
- }
- MODULE_LICENSE("GPL");
- module_init(led_init);
- module_exit(led_exit);
测试程序:
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdio.h>
- main()
- {
- int fd,i,lednum;
- fd = open("/dev/led",O_RDWR);
- if(fd<0)
- {
- perror("open fail \n");
- return ;
- }
- for(i=0;i<100;i++)
- {
- lednum=0;
- write(fd,&lednum,sizeof(int));
- lednum = i%4+1;
- write(fd,&lednum,sizeof(int));
- sleep(1);
- }
- close(fd);
- }
makefile:
- ifneq ($(KERNELRELEASE),)
- obj-m:=hello.o
- $(info "2nd")
- else
- #KDIR := /lib/modules/$(shell uname -r)/build
- KDIR := /home/xiaoming/linux-3.14-fs4412
- PWD:=$(shell pwd)
- all:
- $(info "1st")
- make -C $(KDIR) M=$(PWD) modules
- arm-none-linux-gnueabi-gcc test.c
- sudo cp hello.ko a.out /rootfs/test/
- clean:
- rm -f *.ko *.o *.symvers *.mod.c *.mod.o *.order
- endif
编译结束后,将a.out 和 hello.ko 拷贝到开发板中:
# insmod hello.ko
#mknod /dev/hello c 250 0
#./a.out
会看到跑马灯效果。
后面会对该驱动完善。
Linux 字符设备驱动开发基础(一)—— 编写简单 LED 设备驱动相关推荐
- 树莓派底层IO驱动开发示例(一个简单io口驱动的开发)
一.驱动代码的开发 1.树莓派寄存器的介绍 点击查看:树莓派(bcm2835芯片手册) GPFSEL0 GPIO Function Select 0: 功能选择 输入/输出 GPSET0 GPIO P ...
- 【嵌入式Linux】嵌入式Linux驱动开发基础知识之LED模板驱动程序的改造:设备树
文章目录 前言 1.驱动的三种编写方法 2.怎么使用设备树写驱动程序 2.1.设备树节点要与platform_driver能匹配 2.2.修改platform_driver的源码 3.实验和调试技巧 ...
- 【嵌入式Linux】嵌入式Linux驱动开发基础知识之设备树模型
文章目录 前言 1.设备树的作用 2.设备树的语法 2.1.设备树的逻辑图和dts文件.dtb文件 2.1.1.1Devicetree格式 1DTS文件的格式 node的格式 properties的格 ...
- 【嵌入式Linux】嵌入式Linux驱动开发基础知识之总线设备驱动模型
文章目录 前言 1.驱动编写的三种方法 1.1.传统写法 1.2.总线驱动模型 1.3.设备树驱动模型 2.Linux实现分离:Bus/Dev/Drv模型 2.1.Bus/Dev/Drv模型 2.2. ...
- 【嵌入式Linux】嵌入式Linux驱动开发基础知识之第一个驱动
文章目录 前言 1.Hello驱动 1.1.APP打开的文件在内核中如何表示? 1.2.打开字符设备节点时,内核中也有对应的struct file 1.3.如何编写驱动程序? 1.4.驱动程序代码 1 ...
- linux驱动开发篇(三)—— 总线设备驱动模型
linux系列目录: linux基础篇(一)--GCC和Makefile编译过程 linux基础篇(二)--静态和动态链接 ARM裸机篇(一)--i.MX6ULL介绍 ARM裸机篇(二)--i.MX6 ...
- 驱动开发基础知识——设备树
BSP开发工程师[原来BSP就是那些被指臃肿的文件啊 BSP的出生 Linux经过不断的发展,原先嵌入式系统的三层结构逐步演化成为一种四层结构. 这个新增加的中间层次位于操作系统和硬件之间,包含了系统 ...
- linux课程_【课程完结】嵌入式Linux应用/驱动开发基础知识两大篇章已全部录制完毕 共72集...
完结撒花 <第四篇嵌入式Linux应用开发基础知识> <第五篇嵌入式Linux驱动开发基础知识> 两大篇章已全部录制完毕 共计 72 集 01 嵌入式Linux应用开发基础知识 ...
- 【嵌入式Linux】嵌入式Linux驱动开发基础知识之Pinctrl子系统和GPIO子系统的使用
文章目录 前言 1.Pinctrl子系统 1.1.为什么有Pinctrl子系统 1.2.重要的概念 1.3.代码中怎么引用pinctrl 2.GPIO子系统 2.1.为什么有GPIO子系统 2.2.在 ...
最新文章
- 自动驾驶感知系统盘点
- 服务器报错:“/usr/local/var/run/nginx.pid”failed
- 如果用神经网络分类处于纠缠态的一对粒子?
- 如何判断基因组的重复区域_Nat Comm. | 15万人类基因组中多核苷酸变异(MNV)的起源及功能研究...
- iphone-common-codes-ccteam源代码 CCUIKit.m
- python缓存技术_Python中整数的缓存机制讲解
- html表格中绑定显示xml文档内容的简单实例,JS读取XML文件数据并以table形式显示数据的方法(兼容IE与火狐)...
- 转:ORA-01126: 数据库必须已装载到此实例并且不在任何实例中打开
- python调用scp上传目录_Python调用scp向服务器上传文件示例
- WIndow Document
- 预编译头文件来自编译器的早期版本,或者预编译头为 C++ 而在 C 中使用它(或相反)...
- SEO工具,站长必备
- codeforce 985C Liebig's Barrels
- Mac废纸篓无法清空怎么办?
- 计算机设备耗材管理系统,实验室耗材管理方法、系统、计算机设备和存储介质与流程...
- Maven -- dependency详解
- pygame交换式拼图设计
- iOS13的暗黑模式
- MySql的flush用法
- Gram矩阵与卷积网络中的卷积的直观理解