您的位置:首页 > 其它

字符设备驱动之中断按键驱动

2015-04-01 09:43 316 查看
1、注册设备描述结构:major = register_chrdev(0,"", file_operation);\

2、自动创建设备节点: class_create();

class_device_create();

注: mdev是busybox自带的一个简化版的udev,作用是在系统启动和热插拔或动态加载驱动程序时,自动产生驱动程序所需的节点文件,在文件系统中的/dev目录下的设备节点都是由mdev创建的。

              在启动脚本文件rcS文件中增加如下启动命令来挂载sys文件系统和tmpfs文件系统,同时启动mdev应用程序。

              /bin/mount  -n  -t tmpfs tmpfs /dev/shm

              /bin/mount  -n  -t sysfs  none /sys

              echo /sbin/mdev > /proc/sys/kernel/hotplug

              /sbin/mdev -s

              /bin/hotplug

3、卸载设备描述结构: unregister_chrdev();

4、卸载设备节点: class_device_unregister()

5、卸载设备类: class_destroy();

6、打开设备节点文件时,注册中断服务:request_irq();

7、close设备节点文件时,释放中断服务:free_irq();

8、read设备节点文件时:判断是否有中断,没有则休眠 wait_event_interruptible(button_waitq,  ev_press);如果有中断,则copy_to_user(),且置ev_press=0;

9、中断服务函数:先用s3c2440_gpio_getpin(pin)判断按键电平,为0则是低电平,从而返回一个键值。设置ev_press = 1;  wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */

10、 DECLARE_WAIT_QUEUE_HEAD(button_waitq);生成一个等待队列头,

感谢魏东山程序#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>

static struct class *thirddrv_class;
static struct class_device *thirddrv_class_dev;

//volatile unsigned long *gpfcon;
//volatile unsigned long *gpfdat;

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/* 中断事件标志, 中断服务程序将它置1,third_drv_read将它清0 */
static volatile int ev_press = 0;

struct pin_desc{
unsigned int pin;
unsigned int key_val;
};

/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val;

/*
* K1,K2,K3,K4对应GPF1、GPF4、GPF2、GPF0
*/

struct pin_desc pins_desc[4] = {
{S3C2410_GPF1, 0x01},
{S3C2410_GPF4, 0x02},
{S3C2410_GPF2, 0x03},
{S3C2410_GPF0, 0x04},
};

/*
* 确定按键值
*/
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc * pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;

pinval = s3c2410_gpio_getpin(pindesc->pin);

if (pinval)
{
/* 松开 */
key_val = 0x80 | pindesc->key_val;
}
else
{
/* 按下 */
key_val = pindesc->key_val;
}

ev_press = 1; /* 表示中断发生了 */
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */

return IRQ_RETVAL(IRQ_HANDLED);
}

static int third_drv_open(struct inode *inode, struct file *file)
{
/* GPF1、GPF4、GPF2、GPF0为中断引脚 */
request_irq(IRQ_EINT1, buttons_irq, IRQT_BOTHEDGE, "K1", &pins_desc[0]);
request_irq(IRQ_EINT4, buttons_irq, IRQT_BOTHEDGE, "K2", &pins_desc[1]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "K3", &pins_desc[2]);
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "K4", &pins_desc[3]);

return 0;
}

ssize_t third_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
if (size != 1)
return -EINVAL;

/* 如果没有按键动作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);

/* 如果有按键动作, 返回键值 */
copy_to_user(buf, &key_val, 1);
ev_press = 0;

return 1;
}

int third_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT1, &pins_desc[0]);
free_irq(IRQ_EINT4, &pins_desc[1]);
free_irq(IRQ_EINT2, &pins_desc[2]);
free_irq(IRQ_EINT0, &pins_desc[3]);
return 0;
}

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

int major;
sta
4000
tic int third_drv_init(void)
{
major = register_chrdev(0, "third_drv", &sencod_drv_fops);

thirddrv_class = class_create(THIS_MODULE, "third_drv");

thirddrv_class_dev = class_device_create(thirddrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */

// gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
// gpfdat = gpfcon + 1;

return 0;
}

static void third_drv_exit(void)
{
unregister_chrdev(major, "third_drv");
class_device_unregister(thirddrv_class_dev);
class_destroy(thirddrv_class);
// iounmap(gpfcon);
return 0;
}

module_init(third_drv_init);

module_exit(third_drv_exit);

MODULE_LICENSE("GPL");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: