字符设备驱动-轮询方式操控按键

来源:互联网 发布:程序员书籍 编辑:程序博客网 时间:2024/06/04 23:30

一般驱动都分为两个部分:

一:写出框架

二:硬件操作

框架搭建:

①头文件的拷贝

#include <linux/module.h>  #include <linux/kernel.h>  #include <linux/fs.h>  #include <linux/init.h>  #include <linux/delay.h>  #include <asm/uaccess.h>  #include <asm/irq.h>  #include <asm/io.h>  #include <asm/arch/regs-gpio.h>  #include <asm/hardware.h>

②将open、read驱动函数框架写出

static int second_drv_open(struct inode *inode,struct file *file){    return 0;}

③定义fileoperation

static struct file_operations second_drv_fops = {    .owner = THIS_MODULE,   /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */    .open  = second_drv_open,    .read = second_drv_read,};

入口函数编写(注册驱动程序)

int major; static int second_drv_init(void){        major = register_chrdev(0,"second_drv",&second_drv_fops);        return 0;}

出口函数编写(卸载驱动程序)

static int second_drv_exit(void){        unregister_chrdev(major,"second_drv");        return 0;}

入口函数和出口函数也只是普通的函数,何以达到入口出口的功能!需要通过module_来修饰。

module_init(second_drv_init);module_exit(second_drv_exit);

⑤给sysfs提供更多信息,利用udev机制创建设备节点

Ⅰ定义两个结构体

static struct class *seconddrv_class;static struct class_device *seconddrv_class_dev;

Ⅱ 入口函数中创建一个类=>类下面创建一个设备

int major;static int second_drv_init(void){        major = register_chrdev(0,"second_drv",&second_drv_fops);        seconddrv_class = class_create(THIS_MODULE,"seconddrv");        seconddrv_class_dev = class_device_create(seconddrv_class,NULL,MKDEV(major,0),NULL,"buttons");}

补: mdev是udev的简化版本,mdev应用程序会被内核来调用,根据⑤提供的信息来创建/dev/buttons 设备节点。

Ⅲ 出口函数中卸载类

static int second_drv_exit(void){        unregister_chrdev(major,"second_drv");        class_device_unregister(seconddrv_class_dev);        class_destroy(seconddrv_class);        return 0;}

MODULE_LICENSE("GPL");

通过 “GPL” 指明 这是GNU General Public License的任意版本

测试:

修改Makefile
cp second_drv.ko /work/nfs_root/czg
insmod ./second_drv.ko
lsmod
cat /proc/devices
ls /dev/buttons -l

硬件操作

① 看原理图-确定哪些引脚

② 看2440手册-确定引脚状态

编程 Ⅰ单片机:物理地址 Ⅱ 驱动:虚拟地址

VA = ioremap(PA,size)

原理图:

这里写图片描述

2440手册:

这里写图片描述
这里写图片描述

编程:

① 定义寄存器

volatile unsigned long *gpfcon;volatile unsigned long *gpfdat;volatile unsigned long *gpgcon;volatile unsigned long *gpgdat;

② 地址映射建立和解除

入口函数中建立:

static int second_drv_init(void){        major = register_chrdev(0,"second_drv",&second_drv_fops);        seconddrv_class = class_create(THIS_MODULE,"seconddrv");        seconddrv_class_dev = class_device_create(seconddrv_class,NULL,MKDEV(major,0),NULL,"buttons");        gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);        gpfdat = gpfcon + 1;        gpgcon = (volatile unsigned long *)ioremap(0x56000060,16);        gpgdat = gpgcon + 1;        return 0;}

出口函数中解除:

static int second_drv_exit(void){        unregister_chrdev(major,"second_drv");        class_device_unregister(seconddrv_class_dev);        class_destroy(seconddrv_class);        iounmap(gpfcon);        iounmap(gpgcon);        return 0;}

③ open函数中配置引脚

static int second_drv_open(struct inode *inode,struct file *file){    /* 配置GPF0,2为输入引脚 */    *gpfcon &= ~((0x3<<(0*2)) | (0x3<<(2*2)));    /* 配置GPG3为输入引脚 */    *gpgcon &= ~((0x3<<(3*2)));    return 0;}

④ read函数中返回引脚状态

static ssize_t second_drv_read (struct file *file, char __user *buf, size_t count, loff_t *ppos){    /* 返回4个引脚的电平状态 */    unsigned char key_vals[3];    int regval;    //看用户需要读取的空间,和这里的是否相同    if(count != sizeof(key_vals))        return -EINVAL;    /* 读GPF0,2 */    regval = *gpfdat;    key_vals[0] = (regval & (1<<0)) ? 1 : 0;    key_vals[1] = (regval & (1<<2)) ? 1 : 0;    /* 读GPF3 */    regval = *gpgdat;    key_vals[2] = (regval & (1<<3)) ? 1 : 0;    //将值返回给用户程序    copy_to_user(buf,key_vals,sizeof(key_vals));    return sizeof(key_vals);}

完整代码:

Makefile

KERN_DIR = /work/system/linux-2.6.22.6all:    make -C $(KERN_DIR) M=`pwd` modules clean:    make -C $(KERN_DIR) M=`pwd` modules clean    rm -rf modules.orderobj-m += second_drv.o

驱动程序:second_drv.c

#include <linux/module.h>  #include <linux/kernel.h>  #include <linux/fs.h>  #include <linux/init.h>  #include <linux/delay.h>  #include <asm/uaccess.h>  #include <asm/irq.h>  #include <asm/io.h>  #include <asm/arch/regs-gpio.h>  #include <asm/hardware.h>static struct class *seconddrv_class;static struct class_device *seconddrv_class_dev;volatile unsigned long *gpfcon;volatile unsigned long *gpfdat;volatile unsigned long *gpgcon;volatile unsigned long *gpgdat;static int second_drv_open(struct inode *inode,struct file *file){    /* 配置GPF0,2为输入引脚 */    *gpfcon &= ~((0x3<<(0*2)) | (0x3<<(2*2)));    /* 配置GPG3为输入引脚 */    *gpgcon &= ~((0x3<<(3*2)));    return 0;}static ssize_t second_drv_read (struct file *file, char __user *buf, size_t count, loff_t *ppos){    /* 返回4个引脚的电平状态 */    unsigned char key_vals[3];    int regval;    //看用户需要读取的空间,和这里的是否相同    if(count != sizeof(key_vals))        return -EINVAL;    /* 读GPF0,2 */    regval = *gpfdat;    key_vals[0] = (regval & (1<<0)) ? 1 : 0;    key_vals[1] = (regval & (1<<2)) ? 1 : 0;    /* 读GPF3 */    regval = *gpgdat;    key_vals[2] = (regval & (1<<3)) ? 1 : 0;    //将值返回给用户程序    copy_to_user(buf,key_vals,sizeof(key_vals));    return sizeof(key_vals);}static struct file_operations second_drv_fops = {    .owner = THIS_MODULE,   /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */    .open  = second_drv_open,    .read = second_drv_read,};int major; static int second_drv_init(void){        major = register_chrdev(0,"second_drv",&second_drv_fops);        seconddrv_class = class_create(THIS_MODULE,"seconddrv");        seconddrv_class_dev = class_device_create(seconddrv_class,NULL,MKDEV(major,0),NULL,"buttons");        gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);        gpfdat = gpfcon + 1;        gpgcon = (volatile unsigned long *)ioremap(0x56000060,16);        gpgdat = gpgcon + 1;        return 0;}static int second_drv_exit(void){        unregister_chrdev(major,"second_drv");        class_device_unregister(seconddrv_class_dev);        class_destroy(seconddrv_class);        iounmap(gpfcon);        iounmap(gpgcon);        return 0;}module_init(second_drv_init);module_exit(second_drv_exit);MODULE_LICENSE("GPL");

驱动测试程序:seconddrvtest.c

#include <sys/types.h>  #include <sys/stat.h>  #include <fcntl.h>  #include <stdio.h>  /*  * firstdrvtest */int main(int argc, char **argv){    int fd;    unsigned char key_vals[3];    int cnt = 0;    fd = open("/dev/buttons",O_RDWR);    if(fd < 0)    {        printf("can't open!\n");    }    while(1)    {        read(fd,key_vals,sizeof(key_vals));        if(!key_vals[0] || !key_vals[1] || !key_vals[2])        {            printf("%04d key pressed: %d %d %d.\n",cnt++,key_vals[0],key_vals[1],key_vals[2]);        }    }    return 0;}

测试:

后台运行:./seconddrvtest &
查看后台程序: top
放在前台:fg

insmod second_drv.ko
lsmod
cat /proc/devices
ls /dev/buttons -l
./seconddrvtest
这里写图片描述
这里写图片描述

原创粉丝点击