linux2.22.6内核驱动,应用层跟驱动通讯之----用signal(信号)读取按键值,为驱动主动上报到应用

这篇博客介绍了如何在Linux 2.22.6内核中,通过驱动程序监听按键事件,并使用信号(Signal)机制将按键值主动上报到应用层。应用层通过注册信号处理函数`my_signal_fun`来接收按键值,而驱动层则在接收到中断时更新键值并唤醒等待进程。示例代码包括应用层和内核驱动的实现细节,涉及`open`, `read`, `fcntl`, `signal`等函数以及中断处理和驱动注册。" 112183330,10552837,Rhino 6 细分建模教程:牙刷架设计,"['3D建模', 'Rhino软件', '细分表面', '产品设计', '渲染']
摘要由CSDN通过智能技术生成

应用层:

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

/* fifthdrvtest 

  */

int fd;
void my_signal_fun(int signum)
{
unsigned char key_val;
read(fd, &key_val, 1);
printf("key_val: 0x%x\n", key_val);
}
int main(int argc, char **argv)
{
unsigned char key_val;
int ret;

int Oflags;

signal(SIGIO, my_signal_fun);

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

}

        fcntl(fd, F_SETOWN, getpid());//用来初始化驱动里的 button_async 结构体

Oflags = fcntl(fd, F_GETFL); //用来初始化驱动里的 button_async 结构体

fcntl(fd, F_SETFL, Oflags | FASYNC);//用来初始化驱动里的 button_async 结构体

while (1)
{
sleep(1000);
}

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>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值