应用层:
#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>