当前位置:首页 > 单片机 > 单片机
[导读]#include #include #include #include #include #include #include #include #include #include #include #include ccess.h>#include #include #include #include static struct timer_list buttons_timer;static st

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include ccess.h>
#include
#include
#include
#include


static struct timer_list buttons_timer;


static struct class *sixthdrv_class;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static struct fasync_struct *button_async;
//static atomic_t canopen = ATOMIC_INIT(1); //定义原子变量并初始化为1
static DECLARE_MUTEX(button_lock); //定义互斥锁


/* 中断事件标志, 中断服务程序将它置1,buttons_timer_read将它清0 */
static volatile int ev_press = 0;//等于0的话就会一直等待
static unsigned char key_val;


struct pin_desc {
unsigned int pin;
unsigned int val;
};
struct pin_desc pins [] = {
{S3C2410_GPG(0),1},
{S3C2410_GPG(3),2},
{S3C2410_GPG(5),3},
{S3C2410_GPG(6),4}
};

static struct pin_desc *irq_pd;

static irqreturn_t buttons_irq(int irq, void *dev_id)
{
/* 10ms后启动定时器 */
/*
原理当触发中断的时候,进入此函数,然后修改定时器的值10ms后
去执行定时器指定的函数,读取键值
*/
irq_pd = (struct pin_desc *)dev_id;//这里为参数传递,因为
//buttons_timer_function里没法得到传入引脚值
mod_timer(&buttons_timer, jiffies+HZ/100);
return IRQ_RETVAL(IRQ_HANDLED);
}

static void buttons_timer_function(unsigned long data)
{
//printk("irqno = %dn" , irqno);
struct pin_desc *pings = irq_pd;

if (pings==NULL)
return -EFAULT;

if(s3c2410_gpio_getpin(pings->pin))//传入pin值
{
/* 松开 */
key_val = 0x80 | pings->val;
}
else
{
/* 按下 */
key_val = pings->val;
}
ev_press = 1; /* 表示中断发生了 */
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
/*
通过系统函数kill_fasync向进程发送异步IO信号
POLL_IN表示数据可读
*/
kill_fasync (&button_async, SIGIO, POLL_IN);
//return IRQ_RETVAL(IRQ_HANDLED);//这里是一个宏,里面是一个条件判断,最终返回1
}

static int buttons_timer_open(struct inode *inode, struct file *file)
{//S3C2410_GPG(0)

/*
* K1,K2,K3,K4对应GPG0,GPG3,GPG5,GPG6
*/
#if 0
if (!atomic_dec_and_test(&canopen))//自减并判断是否为0,为0返回true,不然返回false
{
atomic_inc(&canopen);//自增
return -EBUSY;
}
#endif

if (file->f_flags & O_NONBLOCK)//以非阻塞方式打开,不成功还是返回
{

//printk(KERN_EMERG "open failedn");
//down_trylock获取信号量不成功不会睡眠而是正常退出
if (down_trylock(&button_lock))//申请信号量,如果成功申请则返回0,不然返回非0
return -EBUSY;//不会导致睡眠
//return 100;
}
else
{
/* 获取信号量 */
down(&button_lock);//如果获取不到信号量,将进入不可中断的睡眠
// down_killable(&button_lock)//如果获取不到信号量,可以杀
// down_interruptible(&button_lock)//如果获取不到信号量,可以中断方式唤醒
}

request_irq(IRQ_EINT8, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K1", &pins[0]);
request_irq(IRQ_EINT11, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K2", &pins[1]);
request_irq(IRQ_EINT13, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K3", &pins[2]);
request_irq(IRQ_EINT14, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K4", &pins[3]);

return 0;
}

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

if (size != 8)//格式控制,如果用户程序传过来的不是1长度,则直接返回
return -EINVAL;
if (file->f_flags & O_NONBLOCK)//非阻塞方式读,没有键值,返回,不会等待
{
if (!ev_press)
return -EAGAIN;
}
else
{
/* 如果没有按键动作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);
}

/* 如果没有按键动作, 休眠 在此就使应用程序休眠,直到有中断来唤醒*/
//wait_event_interruptible(button_waitq, ev_press);//ev_press = 0时休眠
//执行到这,说明已被唤醒,ev_press = 1,所以要清零
ev_press = 0;
copy_to_user(buf, &key_val, 1);

return 1;
}
int buttons_timer_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT8, &pins[0]);
free_irq(IRQ_EINT11, &pins[1]);
free_irq(IRQ_EINT13, &pins[2]);
free_irq(IRQ_EINT14, &pins[3]);
//atomic_inc(&canopen);
up(&button_lock);
return 0;
}
static unsigned buttons_timer_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0; //为0则poll_wait加入队列后进入休眠
poll_wait(file, &button_waitq, wait); // 先加入队列,不会立即休眠

if (ev_press)
mask |= POLLIN | POLLRDNORM;

return mask;//返回非零直接唤醒进程
}
static int buttons_timer_fasync (int fd, struct file *filp, int on)
{
printk("driver: buttons_timer_fasyncn");
return fasync_helper (fd, filp, on, &button_async);//此函数会初始化button_async结构体
}


static struct file_operations buttons_timer_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = buttons_timer_open,
.read=buttons_timer_read,
.release = buttons_timer_close,
.poll = buttons_timer_poll,
.fasync = buttons_timer_fasync,//注册异步处理函数
};//当应用程序一打开异步模式,就会调用此函数


int major;
static int buttons_timer_init(void)
{

init_timer(&buttons_timer);
buttons_timer.function = buttons_timer_function;
//buttons_timer.expires = 0;
add_timer(&buttons_timer);

major = register_chrdev(0, "buttons_timer", &buttons_timer_fops);

sixthdrv_class = class_create(THIS_MODULE, "buttons_timer");

device_create(sixthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons_timer"); /* /dev/buttons */

return 0;
}

static void buttons_timer_exit(void)
{
unregister_chrdev(major, "buttons_timer");
device_destroy(sixthdrv_class,MKDEV(major, 0));
class_destroy(sixthdrv_class);

}


module_init(buttons_timer_init);

module_exit(buttons_timer_exit);

MODULE_LICENSE("GPL");

这里有一点需要注意,就是关于空指针的问题如:

这里定义static struct pin_desc *irq_pd;指针,但是没给它赋值的话,虽然定时器函数中irq_pd = (struct pin_desc *)dev_id;好像赋了值,但是在struct pin_desc *pings = irq_pd;中再次使用的时候,也会有空指针问题热报错 所以要加入

if (pings==NULL)
return -EFAULT;


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

May 14, 2024 ---- 夏普(Sharp)今(14)日正式宣布位于日本的堺10代线工厂将于今年第三季度开始停止生产。TrendForce集邦咨询认为,夏普堺10代的关闭对于今年的供给量影响甚微,但因为该厂10...

关键字: 液晶电视面板 彩虹光电

为增进大家对氧传感器的认识,本文将对氧传感器的检查方法进行详细的介绍。

关键字: 传感器 指数 氧传感器

为增进大家对氧传感器的认识,本文将对氧传感器的作用、氧传感器坏了的表现以及氧传感器常见故障予以介绍。

关键字: 传感器 指数 氧传感器

为增进大家对氧传感器的认识,本文将对氧传感器的原理、氧传感器的作用予以介绍。

关键字: 传感器 指数 氧传感器

为增进大家对PCB电路板的认识,本文将对PCB电路板翘曲标准、PCB电路板翘曲的原因予以介绍。

关键字: PCB 指数 电路板

为增进大家对PCB电路板的认识,本文将对PCB电路板覆铜的作用、PCB电路板干膜种类予以介绍。

关键字: PCB 指数 电路板

为增进大家对PCB电路板的认识,本文将对PCB电路板扇孔、PCB设计对PCB扇孔的要求予以介绍。

关键字: PCB 指数 电路板

May 14, 2024 ---- TrendForce集邦咨询研究最新显示,OLED桌上型显示器(Monitor)2024年第一季出货总量约为20万台,年成长率121%。第二季在品牌新机陆续上市后,当季成长幅度预估将达...

关键字: OLED 显示器

为增进大家对有源滤波器的认识,本文将对有源滤波器的选型方法、环路滤波器对有源、无源滤波器的选择予以介绍。

关键字: 滤波器 指数 有源滤波器

为增进大家对滤波器的认识,本文将对切比雪夫滤波器阶数以及LC滤波器设计优化流程予以介绍。

关键字: 滤波器 指数 切比雪夫滤波器
关闭
关闭