目录
- 字符设备驱动(五)按键优化
- 按键值读取
- 休眠读取
- 程序设计
- 测试
title: 字符设备驱动(五)按键优化
tags: linux
date: 2018-11-23 17:56:57
toc: true
---
字符设备驱动(五)按键优化
按键值读取
Linux内部有系统函数s3c2410_gpio_getpin
能够读取GPIO
的值
unsigned int s3c2410_gpio_getpin(unsigned int pin)
{
void __iomem *base = S3C24XX_GPIO_BASE(pin);
unsigned long offs = S3C2410_GPIO_OFFSET(pin);
return __raw_readl(base + 0x04) & (1<< offs);
}
在中断处理函数中有个参数是dev_id
,这个是由request_irq
初始化的,处理函数可以用这个结构体传递参数
static irqreturn_t buttons_irq(int irq, void *dev_id)
也就是说一个中断绑定了一个dev_id
,可用作传递参数,卸载中断函数
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
free_irq(IRQ_EINT0, &pins_desc[0]);
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); //这里就读取到了
}
休眠读取
程序设计
程序设计目的:App
去读取按键值,如果有按键中断触发(键值有改变)则打印,否则休眠.
read(key)
if change
printf
else
sleep
也就是说App
函数依然不变,但是需要修改驱动的读取函数,驱动的读取函数中有休眠的动作
// app.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int fd;
unsigned char key_val;
fd = open("/dev/xyz0", O_RDWR);
if (fd < 0)
{
printf("can‘t open!\n");
}
while (1)
{
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
}
return 0;
}
驱动程序中需要设计休眠,中断发生来唤醒首先定义一个等待队列,下述是一个宏
//生成一个等待队列头wait_queue_head_t,名字为name
DECLARE_WAIT_QUEUE_HEAD(name)
// 定义一个名为`button_waitq`的队列
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
休眠函数如下,condition=0
才休眠,定义在include/linux/wait.h
#define wait_event_interruptible(wq, condition) ({ int __ret = 0; if (!(condition)) __wait_event_interruptible(wq, condition, __ret); __ret; })
唤醒也是一个宏,参数是等待队列,放置在中断函数处理中,定义在include/linux/wait.h
wake_up_interruptible(&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>
//#include <linux/interrupt.h>
volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;
static struct class *drv_class;
static struct class_device *drv_class_dev;
// 定义一个名为`button_waitq`的队列
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
// flag=1 means irq happened and need to update
int flag=0;
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val;
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04},
};
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
printk("irq%d\r\n",irq);
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;
}
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
flag=1;
return IRQ_RETVAL(IRQ_HANDLED);
}
static int drv_open(struct inode *inode, struct file *file)
{
/* 配置GPF0,2为输入引脚 */
/* 配置GPG3,11为输入引脚 */
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);
return 0;
}
int drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
free_irq(IRQ_EINT11,&pins_desc[2]);
free_irq(IRQ_EINT19,&pins_desc[3]);
return 0;
}
static ssize_t drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
//int minor = MINOR(file->f_dentry->d_inode->i_rdev);
//printk("drv_write=%d\n",minor);
return 0;
}
static ssize_t drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
if (size != 1)
return -EINVAL;
/* 如果没有按键动作, 休眠 */
wait_event_interruptible(button_waitq, flag);
/* 如果有按键动作, 返回键值 */
copy_to_user(buf, &key_val, 1);
flag = 0;
return 1;
}
static struct file_operations drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = drv_open,
.write = drv_write,
.read = drv_read,
.release = drv_close,
};
static int major;
static int drv_init(void)
{
int minor=0;
major=register_chrdev(0, "drv", &drv_fops); // 注册, 告诉内核
drv_class = class_create(THIS_MODULE, "drv");
drv_class_dev = class_device_create(drv_class, NULL, MKDEV(major, 0), NULL, "xyz%d", minor);
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
return 0;
}
static void drv_exit(void)
{
unregister_chrdev(major, "drv"); // 卸载
class_device_unregister(drv_class_dev);
class_destroy(drv_class);
iounmap(gpfcon);
iounmap(gpgcon);
}
module_init(drv_init);
module_exit(drv_exit);
MODULE_AUTHOR("xxx");
MODULE_VERSION("0.1.0");
MODULE_DESCRIPTION("S3C2410/S3C2440 LED Driver");
MODULE_LICENSE("GPL");
测试
测试运行./text /dev/xyz0 &
后台运行
# ./test /dev/xyz0 &
# irq55
key_val = 0x3
irq55
key_val = 0x83
irq18
key_val = 0x2
irq18
key_val = 0x82
irq16
key_val = 0x1
irq16
key_val = 0x81
irq63
key_val = 0x4
irq63
key_val = 0x84
使用top
查看占用
Load average: 0.00 0.01 0.00
PID PPID USER STAT VSZ %MEM %CPU COMMAND
782 770 0 R 3096 5% 0% top
770 1 0 S 3096 5% 0% -sh
781 770 0 S 1312 2% 0% ./test /dev/xyz0
使用ps
查看任务为S
状态 休眠状态
# ps
PID Uid VSZ Stat Command
1 0 3092 S init
781 0 1312 S ./test /dev/xyz0
783 0 3096 R ps
原文地址:https://www.cnblogs.com/zongzi10010/p/10009182.html
时间: 2024-11-05 11:01:16