这个代码是通过晕哥那个中断驱动的帖子该了
#include <linux/major.h>
#include <linux/slab.h>
#include <asm/fb.h>
#include <linux/uaccess.h>
#define DEVICE "myKey"
#define PB3_GPIO 35 //PB3 35=32*1+3
#define GPIO_INT_NAME "PB3_int"
#define GPIO_HIGH gpio_get_value(PB3_GPIO)
#define GPIO_LOW (gpio_get_value(PB3_GPIO) == 0)
short int irq_any_gpio = 0;
int count =0;
struct class *key_dev_class = NULL;
struct class_device *key_dev_class_dev = NULL;
enum { falling, rising } type;
static irqreturn_t r_irq_handler(int irq, void *dev_id)
{
count++;
printk(KERN_DEBUG "interrupt received (irq: %d)\n", irq);
if (irq == gpio_to_irq(PB3_GPIO))
{
type = GPIO_LOW ? falling : rising;
if(type == falling)
{
printk("gpio pin is low\n");
}
else
printk("gpio pin is high\n");
}
return IRQ_HANDLED;
}
int led_open(struct inode *inode, struct file *filp)
{
int ret;
int err;
if (gpio_request(PB3_GPIO, GPIO_INT_NAME ))
{
printk("PB3_GPIO request failure: %s\n", GPIO_INT_NAME );
return -1;
}
if ( (irq_any_gpio = gpio_to_irq(PB3_GPIO)) < 0 ) {
printk("PB3_GPIO to IRQ mapping failure %s\n",GPIO_INT_NAME );
return -1;
}
printk(KERN_NOTICE "Mapped int %d\n", irq_any_gpio);
if (request_irq(irq_any_gpio,(irq_handler_t ) r_irq_handler, IRQF_TRIGGER_FALLING , GPIO_INT_NAME, NULL))
{
printk("Irq Request failure\n");
return -1;
}
printk("led_open == ok\n");
return 0;
}
static int key_dev_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
unsigned long err ;
printk("Enter key_dev_read == %d\n",type);
buff[0] = type;
//err = copy_to_user(buff,&type,1,1);
printk("copy_to_user == %d\n",buff[0]);
//return err ? -EFAULT : 0;
return 0;
}
int r_int_release(struct inode *inode, struct
free_irq(gpio_to_irq(PB3_GPIO), NULL);
gpio_free(PB3_GPIO);
return 0;
}
struct file_operations led_fops ={
.owner = THIS_MODULE,
.open = led_open,
.release = r_int_release,
.read = key_dev_read
};
struct miscdevice led_dev ={
.minor = MISC_DYNAMIC_MINOR,//minor是这个混杂设备的次设备号,由系统自动配置,
.name = DEVICE, //.name 设备名
.fops = &led_fops
};
void r_int_config(void)
{
int ret;
ret = misc_register(&led_dev);//注册驱动程序时采用misc_register函数注册,此函数中会自动创建设备节点,即设备文件
if(ret)
{
printk("error: cannot register misc.\n");
return ret;
}
return 0;
}
static void __exit Key_exit(void)
{
misc_deregister(&led_dev);//杂项设备的注销过程,其实就是注册过程的逆向过程
printk("mise-deregister %s\n", DEVICE);
}
int init_module(void)
{
printk("<1>Hello World\n");
r_int_config();
return 0;
}
module_init(init_module); // Do some better naming
module_exit(Key_exit);
MODULE_AUTHOR("ysc201995"); // 驱动程序的作者
MODULE_DESCRIPTION("Key")
离线
主要是在
int led_open(struct inode *inode, struct file *filp)
{
int ret;
int err;
if (gpio_request(PB3_GPIO, GPIO_INT_NAME ))
{
printk("PB3_GPIO request failure: %s\n", GPIO_INT_NAME );
return -1;
}
if ( (irq_any_gpio = gpio_to_irq(PB3_GPIO)) < 0 ) {
printk("PB3_GPIO to IRQ mapping failure %s\n",GPIO_INT_NAME );
return -1;
}
printk(KERN_NOTICE "Mapped int %d\n", irq_any_gpio);
if (request_irq(irq_any_gpio,(irq_handler_t ) r_irq_handler, IRQF_TRIGGER_FALLING , GPIO_INT_NAME, NULL))
{
printk("Irq Request failure\n");
return -1;
}
printk("led_open == ok\n");
return 0;
}
我在Qt上面去Open设备的时候一到这里就就就进去了 if (gpio_request(PB3_GPIO, GPIO_INT_NAME )) 就返回负一了
离线
和这个对比测试一下: https://whycan.cn/t_1261.html
就是按着你那个去写的 只是加了一个字符设备 通过读取字符设备获取状态
离线
PB3 有没有被其他功能占用?
没有被其他的占用 但是我按下去的时候有进到中断里面 ,就是在QT层去读取的时候有错误
[ 224.167890] Mapped int 44
[ 224.170614] genirq: Flags mismatch irq 44. 00000002 (PB3_int) vs. 00000002 (PB3_int)
[ 224.178386] Irq Request failure
open event0 error
[ 226.711062] interrupt received (irq: 44)
[ 226.715010] gpio pin is low
[ 227.053878] interrupt received (irq: 44)
[ 227.057826] gpio pin is low
离线
离线
你这是一个 GPIO 的 按键驱动?
是的 就是GPIO按键驱动 然后我在QT层去读取字符 可以获取到按键是按下还是弹起 现在在/dev下面有一个字符设备 了
离线
离线
GPIO KEY 按键驱动(轮询): https://whycan.cn/t_2393.html#p18258
GPIO KEY 按键驱动(中断): https://whycan.cn/t_2393.html#p18270
根本不需要自己动手, Linux自带光环
是啊 就想把这个按键驱动通过代码形式,可以运行起来,晕哥你能帮我看看那个代码吗
离线