微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 字符设备驱动-初级篇按键中断程序驱动

字符设备驱动-初级篇按键中断程序驱动

时间:11-21 来源:互联网 点击:
驱动源码:

#include "linux/module.h"

#include"linux/kernel.h"

#include"linux/fs.h"

#include"linux/init.h"

#include"linux/delay.h"

#include"linux/irq.h"

#include"asm/uaccess.h"

#include"asm/irq.h"

#include"asm/io.h"

#include"asm/arch/regs-gpio.h"

#include"asm/hardware.h"

int major = 0;

static struct class *keydrv_class;

static struct class_device *keydrv_class_dev;

volatile unsigned long *gpfcon;

volatile unsigned long *gpfdat;

volatile unsigned long *gpgcon;

volatile unsigned long *gpgdat;

static irqreturn_t buttons_irq(int irq, void *dev_id)

{

printk("irq = %d\n",irq);

return IRQ_RETVAL(IRQ_HANDLED);

}

static int key_drv_open(struct inode *inode, struct file *file)

{

request_irq(IRQ_EINT0,buttons_irq,IRQT_BOTHEDGE,"S2",1);

request_irq(IRQ_EINT2,buttons_irq,IRQT_BOTHEDGE,"S3",1);

request_irq(IRQ_EINT11,buttons_irq,IRQT_BOTHEDGE,"S4",1);

request_irq(IRQ_EINT19,buttons_irq,IRQT_BOTHEDGE,"S5",1);

return 0;

}

ssize_t key_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)

{

unsigned char key_vals[4];

int regval;

if(size != sizeof(key_vals))

return -EINVAL;

regval = *gpfdat;

key_vals[0] = (regval & (1<0)) ? 1 : 0;

key_vals[1] = (regval & (1<2)) ? 1 : 0;

regval = *gpgdat;

key_vals[2] = (regval & (1<3)) ? 1 : 0;

key_vals[3] = (regval & (1<11)) ? 1 : 0;

copy_to_user(buf, key_vals, sizeof(key_vals));

return sizeof(key_vals);

}

int key_drv_close(struct inode *inode, struct file *file)

{

free_irq(IRQ_EINT0, 1);

free_irq(IRQ_EINT2, 1);

free_irq(IRQ_EINT11, 1);

free_irq(IRQ_EINT19, 1);

return 0;

}

static struct file_operations key_drv_fops = {

.owner = THIS_MODULE,

.open = key_drv_open,

.read = key_drv_read,

.release = key_drv_close,

};

static int key_drv_init(void)

{

major = register_chrdev(0, "key_drv", &key_drv_fops);

keydrv_class = class_create(THIS_MODULE, "key_drv");

keydrv_class_dev = class_device_create(keydrv_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 void key_drv_exit(void)

{

unregister_chrdev(major, "key_drv");

class_device_unregister(keydrv_class_dev);

class_destroy(keydrv_class);

iounmap(gpfcon);

iounmap(gpgcon);

return 0;

}

module_init(key_drv_init);

module_exit(key_drv_exit);

MODULE_LICENSE("GPL");

=================================================================================================

测试命令:

lsmod :检测当前已经安装的驱动程序

insmod ./keydrv.ko :安装keydrv.ko驱动程序

cat /proc/devices :查看当前设备

cat /proc/interrupts :查看当前中断程序

exec 5<|dev/buttons :打开设备(等同于应用程序调用open函数),定位到5

此时再执行:cat /proc/interrupts 命令就可以看到S2、S3、S4、S5四个中断程序

ps :查看所有进程(770 0 3096 S -sh)

top :查看各进程所占CPU比例

ls -l /proc/770/fd :可以查看到定位到5上面(lr-x-- 1 0 0 64 Jan1 00:22 5 -> /dev/buttons)

exec 5<&- :关闭设备(等同于应用程序调用close函数)


Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top