编译系统 :ubuntu 16.04
内 核 :linux-2.6.22.6
硬件平台 :jz2240
交叉编译器:arm-linux-gcc 3.4.5
上一篇具体的分析了linux中断框架,这里贴出来一个具体事例
驱动程序如下:
#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 <linux/interrupt.h>
#include <linux/wait.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <asm/io.h>
/* 这是一个按键驱动
* 如果没有按键按下,就进行休眠
* 为什么要休眠,因为应用程序会一直在读或者等到按键被按下来,
* 如果说一直没有按下按键,那么久需要让用户空间进行休眠,不然会一直处于读阻塞
*/
static struct class *g_buttons_class;
static struct class_device *g_my_buttons_classdevice;
struct buttondesc
{
unsigned int key_gpio; //引脚
unsigned int irq ; //中断号
unsigned int flag; //触发条件
const char *name; //名字
char keyval;//自定义键值
};
static struct buttondesc g_buttons[4] =
{
{S3C2410_GPF0, IRQ_EINT0, IRQT_FALLING, "S2",0x11},
{S3C2410_GPF2, IRQ_EINT2, IRQT_FALLING, "S3",0x12},
{S3C2410_GPG3, IRQ_EINT11, IRQT_FALLING, "S4",0x13},
{S3C2410_GPG11, IRQ_EINT19, IRQT_FALLING, "S5",0x14},
};
static char g_keyval;//用于返回给用户空间
/* ------------------------------------------------------------------------- */
/* ################## 休眠相关变量 ################## */
#define SLEEP 1
#define NO_SLEEP 0
static DECLARE_WAIT_QUEUE_HEAD(g_buttons_waitq);
static char g_ev_press = SLEEP;//用于标志中断休眠
/* ------------------------------------------------------------------------- */
/* ################## 原子操作相关变量 ################## */
static atomic_t g_atomic = ATOMIC_INIT(1);//定义一个原子,并且初始化为1
//实际的中断处理函数
static irq_handler_t buttons_handler(int irq, void *dev_id)
{
struct buttondesc *temp_desc = (struct buttondesc *)dev_id;//根据传递参数可以获得对应中断号数组
unsigned int temp_key = s3c2410_gpio_getpin(temp_desc->key_gpio);//读取gpio引脚状态
if(!temp_key)//如果有按键按下就是0
g_keyval = 0x80 | temp_desc->keyval;
else
g_keyval = temp_desc->keyval;
//访问全局变量这里要进行原子操作
if(!atomic_dec_and_test(&g_atomic))//测试其是否为0,为 0 则返回 true,否则返回 false
{
//已经打开,不能再次打开
atomic_inc(&g_atomic);
return -1;
}
g_ev_press = NO_SLEEP;//唤醒休眠
atomic_inc(&g_atomic);//恢复原子值
wake_up_interruptible(&g_buttons_waitq);
return 0;
}
//驱动读函数
static ssize_t buttons_read(struct file *fp, char __user *buf, size_t size, loff_t *fops)
{
wait_event_interruptible(g_buttons_waitq, (g_ev_press == NO_SLEEP));//当g_ev_press == NO_SLEEP条件符合是 会被唤醒,继续执行
if(copy_to_user(buf,&g_keyval, 1))//返回到用户空间
printk("copy_to_user not complete\n");
//访问全局变量这里要进行原子操作
if(!atomic_dec_and_test(&g_atomic))//测试其是否为0,为 0 则返回 true,否则返回 false
{
//已经打开,不能再次打开
atomic_inc(&g_atomic);
return -1;
}
g_ev_press = SLEEP;//重新进行休眠
atomic_inc(&g_atomic);//恢复原子值
return 0;
}
//驱动打开函数
static int buttons_open(struct inode *inodep, struct file *fp)
{
int i;
//注册中断
for(i = 0; i < sizeof(g_buttons)/sizeof(struct buttondesc); i++)
request_irq(g_buttons[i].irq,buttons_handler,g_buttons[i].flag,(const char *)g_buttons[i].name,(void *)(&g_buttons[i]));
//访问全局变量这里要进行原子操作
if(!atomic_dec_and_test(&g_atomic))//测试其是否为0,为 0 则返回 true,否则返回 false
{
//已经打开,不能再次打开
atomic_inc(&g_atomic);
return -1;
}
g_ev_press = SLEEP;//进行休眠
atomic_inc(&g_atomic);//恢复原子值
printk(KERN_INFO "open buttons success\n");
return 0;
}
//关闭
static int buttons_close(struct inode *inodep, struct file *fp)
{
int i;
for(i = 0; i < sizeof(g_buttons)/sizeof(struct buttondesc); i++)
free_irq(g_buttons[i].irq,&g_buttons[i]);
return 0;
}
//操作函数集
static struct file_operations g_buttons_op =
{
.owner = THIS_MODULE,
.open = buttons_open,
.read = buttons_read,
.release = buttons_close,
};
int g_buttons_major;
static int __init buttons_init(void)
{
g_buttons_major = register_chrdev(0, "my_buttons", &g_buttons_op); //注册设备,在/proc/device下可以看到
g_buttons_class = class_create(THIS_MODULE, "HBUT_class"); //创建类
g_my_buttons_classdevice = class_device_create(g_buttons_class, NULL, MKDEV(g_buttons_major, 0), NULL, "buttons"); // 可以看到/dev/button
return 0;
}
static void __exit buttons_exit(void)
{
unregister_chrdev(g_buttons_major,"my_buttons");
class_device_destroy(g_buttons_class,MKDEV(g_buttons_major, 0));
class_destroy(g_buttons_class);
}
MODULE_AUTHOR("大白菜");
MODULE_DESCRIPTION("buttons drive with sleep and interrupt");
MODULE_LICENSE("GPL");
module_init(buttons_init);
module_exit(buttons_exit);
用户程序如下:
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <error.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
/*
ifconfig eth0 192.168.0.11
mount -t nfs -o nolock,vers=2 192.168.0.104:/home/book/wangruo /mnt
cd /mnt/led
*/
int g_fd;//文件描述符
void my_handler(int sign_num)
{
printf("\n ---------press Ctrl + c ---------\n");
close(g_fd);
exit(0);
}
int main(int argc,int **argv)
{
char keyval = 0;
int count=0;
g_fd = open("/dev/buttons",O_RDWR);
if(g_fd < 0)
{
perror("open /dev/buttons");
return -1;
}
//接收到信号
signal(SIGINT,my_handler);
while(1)
{
read(g_fd,&keyval,sizeof(char));//没有中断时,休眠
printf("keyval = 0x%x count = %d\n",keyval,++count);
}
close(g_fd);
return 0;
}
应用程序运行结果: