linux2.22.6内核驱动,应用层跟驱动通讯之----用poll,中断,休眠,唤醒机制读取按键值

本文介绍了如何在Linux内核2.22.6中编写驱动程序,通过poll、中断、休眠和唤醒机制实现应用层与驱动的通讯,从而读取按键值。应用层代码通过poll函数等待中断事件,驱动部分配置GPIO引脚为输入,并注册中断处理函数,当按键按下或松开时触发中断并更新按键值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

应用层:



#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>




/* forthdrvtest 
  */
int main(int argc, char **argv)
{
int fd;
unsigned char key_val;
int ret;


struct pollfd fds[1];

fd = open("/dev/buttons", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}


fds[0].fd     = fd;
fds[0].events = POLLIN;
while (1)
{
ret = poll(fds, 1, 5000);
if (ret == 0)
{
printf("time out\n");
}
else
{
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
}
}

return 0;
}

--------------------------------------------------------------------------------------------------------------------

linux内核2.22.6驱动部份:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/ir

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值