LED设备驱动开发实验—源码代码详解

来源:互联网 发布:随机抽取名字软件 编辑:程序博客网 时间:2024/05/16 05:27

如题,因学校课程要求写驱动开发要求。无奈Linux内核机制不清楚,很是痛苦。所以将led的源代码看了一遍,从中窥探Linux的冰山一角。


实验要求参考:嵌入式Linux移植驱动及应用开发.pdf 第16章(如下)





实验书上的环境和实验步骤写的很详细,废话不多说,上代码:

led.h

#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <stdlib.h>#include <sys/ioctl.h>#include "fs4412_led.h"int main(int argc, char **argv){int fd;int i = 1;int last=1;int r=1;//fd是一个设备描述符,open是Linux下打开设备的一个函数(其屏蔽了具体的硬件基本操作细节),返回整数类型,返回-1说明不能开打//后期对设备的操作就是对fd的操作//第一个参数是串口号,第二个参数是读写的控制权(可读可写)fd = open("/dev/led", O_RDWR);if (fd < 0) {perror("open");exit(1);}    while(1){    scanf("%d",&r);    //该函数设备驱动程序中对设备的i/o通道进行管理的函数。fd是open 函数返回的参数    //LED_OFF是cmd命令,在头文件led.h定义的命令:_IOW(LED_MAGIC, 0, int)    //,后面的补充参数不知道是干什么的(好像不是很重要)ioctl(fd, LED_OFF, &last);ioctl(fd, LED_ON, &r);//usleep(500000);//usleep(500000);last=r;}/*while(1){ioctl(fd, LED_ON, &i);usleep(500000);ioctl(fd, LED_OFF, &i);usleep(500000);if(++i == 5)i = 1;}*/return 0;}


led.c

/*字符设备驱动模型:驱动初始化:1.分配cdev(动态/静态分配);2.初始化cdev(init函数);3.注册cdev(cdev_add);4.硬件初始化;*/#include <linux/kernel.h>#include <linux/module.h>#include <linux/fs.h>#include <linux/cdev.h>#include <asm/io.h>#include <asm/uaccess.h>#include "fs4412_led.h"//块设备MODULE_LICENSE("Dual BSD/GPL");#define LED_MA 500#define LED_MI 0#define LED_NUM 1//物理地址#define FS4412_GPF3CON0x114001E0#define FS4412_GPF3DAT0x114001E4#define FS4412_GPX1CON0x11000C20#define FS4412_GPX1DAT0x11000C24#define FS4412_GPX2CON0x11000C40#define FS4412_GPX2DAT0x11000C44//逻辑地址static unsigned int *gpf3con;static unsigned int *gpf3dat;static unsigned int *gpx1con;static unsigned int *gpx1dat;static unsigned int *gpx2con;static unsigned int *gpx2dat;//字符设备struct cdev cdev;void fs4412_led_on(int nr){//led灯打开操作,先读再移位再写?switch(nr) {case 1: writel(readl(gpx2dat) | 1 << 7, gpx2dat);break;case 2: writel(readl(gpx1dat) | 1 << 0, gpx1dat);break;case 3: writel(readl(gpf3dat) | 1 << 4, gpf3dat);break;case 4: writel(readl(gpf3dat) | 1 << 5, gpf3dat);break;}}void fs4412_led_off(int nr){switch(nr) {case 1: writel(readl(gpx2dat) & ~(1 << 7), gpx2dat);break;case 2: writel(readl(gpx1dat) & ~(1 << 0), gpx1dat);break;case 3: writel(readl(gpf3dat) & ~(1 << 4), gpf3dat);break;case 4: writel(readl(gpf3dat) & ~(1 << 5), gpf3dat);break;}}static int s5pv210_led_open(struct inode *inode, struct file *file){return 0;}static int s5pv210_led_release(struct inode *inode, struct file *file){return 0;}static long s5pv210_led_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg){int nr;//此函数是用户态陷入内核态if(copy_from_user((void *)&nr, (void *)arg, sizeof(nr)))return -EFAULT;if (nr < 1 || nr > 4)return -EINVAL;switch (cmd) {case LED_ON:fs4412_led_on(nr);break;case LED_OFF:fs4412_led_off(nr);break;default:printk("Invalid argument");return -EINVAL;}return 0;}/*一般来说,在系统运行时,外设的I/O内存资源的物理地址是已知的,由硬件的设计决定。但是CPU通常并没有为这些已知的外设I/O内存资源的物理地址预定义虚拟地址范围,驱动程序并不能直接通过物理地址访问I/O内存资源,而必须将它们映射到核心虚地址空间内(通过页表),然后才能根据映射所得到的核心虚地址范围,通过访内指令访问这些I/O内存资源。Linux在io.h头文件中声明了函数ioremap(),用来将I/O内存资源的物理地址映射到核心虚地址空间(3GB-4GB)中*///简单点说就是将led灯的物理地址映射到Linux内核可以用的虚拟地址int fs4412_led_ioremap(void){int ret;gpf3con = ioremap(FS4412_GPF3CON, 4);if (gpf3con == NULL) {printk("ioremap gpf3con\n");ret = -ENOMEM;return ret;}gpf3dat = ioremap(FS4412_GPF3DAT, 4);if (gpf3dat == NULL) {printk("ioremap gpx2dat\n");ret = -ENOMEM;return ret;}gpx1con = ioremap(FS4412_GPX1CON, 4);if (gpx1con == NULL) {printk("ioremap gpx2con\n");ret = -ENOMEM;return ret;}gpx1dat = ioremap(FS4412_GPX1DAT, 4);if (gpx1dat == NULL) {printk("ioremap gpx2dat\n");ret = -ENOMEM;return ret;}gpx2con = ioremap(FS4412_GPX2CON, 4);if (gpx2con == NULL) {printk("ioremap gpx2con\n");ret = -ENOMEM;return ret;}gpx2dat = ioremap(FS4412_GPX2DAT, 4);if (gpx2dat == NULL) {printk("ioremap gpx2dat\n");ret = -ENOMEM;return ret;}return 0;}//iounmap函数是取消ioremap函数生成映射void fs4412_led_iounmap(void){iounmap(gpf3con);iounmap(gpf3dat);iounmap(gpx1con);iounmap(gpx1dat);iounmap(gpx2con);iounmap(gpx2dat);}//led的初始化void fs4412_led_io_init(void){//原型:ssize_t (*read) (struct file * filp, char __user * buffer, size_t    size , loff_t * p); //filp :为进行读取信息的目标文件, //buffer :为对应放置信息的缓冲区(即用户空间内存地址); //size :为要读取的信息长度; //p :为读的位置相对于文件开头的偏移,在读取信息后,这个指针一般都会移动,//             移动的值为要读取信息的长度值////read函数是 从设备中读取数据;//write函数是 发送数据给设备;//////write/read函数可以百度该函数详解;writel((readl(gpf3con) & ~(0xff << 16)) | (0x11 << 16), gpf3con);writel(readl(gpx2dat) & ~(0x3<<4), gpf3dat);writel((readl(gpx1con) & ~(0xf << 0)) | (0x1 << 0), gpx1con);writel(readl(gpx1dat) & ~(0x1<<0), gpx1dat);writel((readl(gpx2con) & ~(0xf << 28)) | (0x1 << 28), gpx2con);writel(readl(gpx2dat) & ~(0x1<<7), gpx2dat);}//应用程序与驱动的映射struct file_operations s5pv210_led_fops = {.owner = THIS_MODULE,.open = s5pv210_led_open,.release = s5pv210_led_release,.unlocked_ioctl = s5pv210_led_unlocked_ioctl,};static int s5pv210_led_init(void){//分配->初始化->注册////定一个设备表dev_t devno = MKDEV(LED_MA, LED_MI); int ret;//内核中所有已分配的字符设备编号都记录在一个名为 chrdevs 散列表里。该散列表中的每一个元素是一个 char_device_struct 结构/*内核提供了三个函数来注册一组字符设备编号,这三个函数分别是 register_chrdev_region()、alloc_chrdev_region() 和register_chrdev()。这三个函数都会调用一个共用的__register_chrdev_region() 函数来注册一组设备编号范围(即一个 char_device_struct 结构)int register_chrdev_region(dev_t from, unsigned count, const char *name)from :要分配的设备编号范围的初始值(次设备号常设为0);Count:连续编号范围.name:编号相关联的设备名称. (/proc/devices);*/ret = register_chrdev_region(devno, LED_NUM, "newled");if (ret < 0) {printk("register_chrdev_region\n");return ret;}//初始化cdev_init(&cdev, &s5pv210_led_fops);cdev.owner = THIS_MODULE;//块设备//注册ret = cdev_add(&cdev, devno, LED_NUM);if (ret < 0) {printk("cdev_add\n");goto err1;}ret = fs4412_led_ioremap();if (ret < 0)goto err2;//硬件初始化fs4412_led_io_init();printk("Led init\n");return 0;err2:cdev_del(&cdev);err1:unregister_chrdev_region(devno, LED_NUM);return ret;}static void s5pv210_led_exit(void){dev_t devno = MKDEV(LED_MA, LED_MI);fs4412_led_iounmap();cdev_del(&cdev);unregister_chrdev_region(devno, LED_NUM);printk("Led exit\n");}//初始化;module_init(s5pv210_led_init);module_exit(s5pv210_led_exit);

text.c

#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <stdlib.h>#include <sys/ioctl.h>#include "fs4412_led.h"int main(int argc, char **argv){int fd;int i = 1;int last=1;int r=1;//fd是一个设备描述符,open是Linux下打开设备的一个函数(其屏蔽了具体的硬件基本操作细节),返回整数类型,返回-1说明不能开打//后期对设备的操作就是对fd的操作//第一个参数是串口号,第二个参数是读写的控制权(可读可写)fd = open("/dev/led", O_RDWR);if (fd < 0) {perror("open");exit(1);}    while(1){    scanf("%d",&r);    //该函数设备驱动程序中对设备的i/o通道进行管理的函数。fd是open 函数返回的参数    //LED_OFF是cmd命令,在头文件led.h定义的命令:_IOW(LED_MAGIC, 0, int)    //,后面的补充参数不知道是干什么的(好像不是很重要)ioctl(fd, LED_OFF, &last);ioctl(fd, LED_ON, &r);//usleep(500000);//usleep(500000);last=r;}/*while(1){ioctl(fd, LED_ON, &i);usleep(500000);ioctl(fd, LED_OFF, &i);usleep(500000);if(++i == 5)i = 1;}*/return 0;}

注意:在驱动程序的中一开始运行的不是main函数,这个与平时写的小代码不同,想要了解更加详细的过程自己百度下吧!我贴出我引用部分的帖子(顺便感谢下那些大佬):

http://blog.csdn.net/l1315925504/article/details/51178342

http://www.linuxidc.com/Linux/2011-04/34295.htm

http://blog.csdn.net/zqixiao_09/article/details/50858946

http://blog.csdn.net/tigerjibo/article/details/6412672

http://blog.csdn.net/zqixiao_09/article/details/50839042


欢迎大家评论交流!!!



阅读全文
0 0
原创粉丝点击