i.MX6ULL Linux开发板 :pinctrl 子系统----按键中断控制实验
Linux 设备驱动理论看了有一段时间了,上一节 我记录了LED 控制闪烁实验 这次记录一下另一个 ----gpio中断
硬件了解
硬件原理图
可以看到SW2可以用为key中断 为 GPIO5_1.默认低电平 按下高电平
在imx6ul-pinfunc.h中 查找得知
KEY引脚 | 配置地址别名 |
---|---|
GPIO5_1 | MX6UL_PAD_SNVS_TAMPER1__GPIO5_IO01 |
添加子系统 设备树
要知道 中断是非常复杂的东西,原理我不深究 ,我们只要将key的设备树加入子系统
button_interrupt {
compatible = "button_interrupt";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_button>;
button_gpio = <&gpio5 1 GPIO_ACTIVE_LOW>; //默认低电平,按键按下高电平
status = "okay";
interrupt-parent = <&gpio5>; //父中断gpio5
interrupts = <1 IRQ_TYPE_EDGE_RISING>; // 指定中断,触发方式为上升沿触发。
//在 gpio5 节点中定义使用两个 cells 来描述我们的按键信息,‘ 1’表示的是我们按键 GPIO5 中引脚编号,“IRQ_TYPE_EDGE_RISING”表示的是触发方式
};
#define IRQ_TYPE_NONE 0
#define IRQ_TYPE_EDGE_RISING 1
#define IRQ_TYPE_EDGE_FALLING 2
#define IRQ_TYPE_EDGE_BOTH (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING)
#define IRQ_TYPE_LEVEL_HIGH 4
#define IRQ_TYPE_LEVEL_LOW 8
之前我们直接在 底层增加 设备树节点与子系统 ,这次我们实验设备树插件方式来完成设备树实现 imx-fire-button-interrupt-overlay.dts
#include "../imx6ul-pinfunc.h"
#include "dt-bindings/interrupt-controller/irq.h"
#include "dt-bindings/gpio/gpio.h"
/ {
fragment@0 {
target-path = "/";
__overlay__ {
button_interrupt {
compatible = "button_interrupt";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_button>;
button_gpio = <&gpio5 1 GPIO_ACTIVE_LOW>; //默认低电平,按键按下高电平
status = "okay";
interrupt-parent = <&gpio5>;
interrupts = <1 IRQ_TYPE_EDGE_RISING>; // 指定中断,触发方式为上升沿触发。
};
};
};
fragment@1 {
target = <&iomuxc>;
__overlay__ {
pinctrl_button: buttongrp {
fsl,pins = <
MX6UL_PAD_SNVS_TAMPER1__GPIO5_IO01 0x10b0
>;
};
};
};
};
将写好的 imx-fire-button-interrupt-overlay.dts
放到 ebf_linux_kernel/arch/arm/boot/dts/overlays
目录然后 将Makefile添加imx-fire-button-interrupt.dtbo
注意名称 注意名称注意名称
//运行以下命令:
make distclean
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- npi_v7_defconfig
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- dtbs
复制到开发板的 /usr/lib/linux-image-4.19.35-imx6/overlays/ 目录下:
我们将设备树设置为 上电启动并注释有冲突的设备树插件:vi /boot/uEnv.txt
至此 设备树已经添加完成了。重启后查看 ls /proc/device-tree
设备驱动编写与编译
//interrupt.c
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <asm/mach/map.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/io.h>
#include <linux/device.h>
#include <linux/irq.h>
#include <linux/of_irq.h>
#include <linux/types.h>
/*------------------字符设备内容----------------------*/
#define DEV_NAME "button"
#define DEV_CNT (1)
static dev_t button_devno; //定义字符设备的设备号
static struct cdev button_chr_dev; //定义字符设备结构体chr_dev
struct class *class_button; //保存创建的类
struct device *device_button; // 保存创建的设备
struct device_node *button_device_node = NULL; //定义按键设备节点结构体
unsigned button_GPIO_number = 0; //保存button使用的GPIO引脚编号
u32 interrupt_number = 0; // button 引脚中断编号
atomic_t button_status = ATOMIC_INIT(0); //定义整型原子变量,保存按键状态 ,设置初始值为0
static irqreturn_t button_irq_hander(int irq, void *dev_id)
{
// printk_green("button on \n");
/*按键状态加一*/
atomic_inc(&button_status);
return IRQ_HANDLED;
}
static int button_open(struct inode *inode, struct file *filp)
{
int error = -1;
/*添加初始化代码*/
// printk_green("button_open");
/*获取按键 设备树节点*/
button_device_node = of_find_node_by_path("/button_interrupt");
if(NULL == button_device_node)
{
printk("of_find_node_by_path error!");
return -1;
}
/*获取按键使用的GPIO*/
button_GPIO_number = of_get_named_gpio(button_device_node ,"button_gpio", 0);
if(0 == button_GPIO_number)
{
printk("of_get_named_gpio error");
return -1;
}
/*申请GPIO , 记得释放*/
error = gpio_request(button_GPIO_number, "button_gpio");
if(error < 0)
{
printk("gpio_request error");
gpio_free(button_GPIO_number);
return -1;
}
error = gpio_direction_input(button_GPIO_number);//设置引脚为输入模式
/*获取中断号*/
interrupt_number = irq_of_parse_and_map(button_device_node, 0);
printk("\n irq_of_parse_and_map! = %d \n",interrupt_number);
/*申请中断, 记得释放*/
error = request_irq(interrupt_number,button_irq_hander,IRQF_TRIGGER_RISING,"button_interrupt",device_button);
if(error != 0)
{
printk("request_irq error");
free_irq(interrupt_number, device_button);
return -1;
}
/*申请之后已经开启了,切记不要再次打开,否则运行时报错*/
// // enable_irq(interrupt_number);
return 0;
}
static int button_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
int error = -1;
int button_countervc = 0;
/*读取按键状态值*/
button_countervc = atomic_read(&button_status);
/*结果拷贝到用户空间*/
error = copy_to_user(buf, &button_countervc, sizeof(button_countervc));
if(error < 0)
{
printk("copy_to_user error");
return -1;
}
/*清零按键状态值*/
atomic_set(&button_status,0);
return 0;
}
/*字符设备操作函数集,.release函数实现*/
static int button_release(struct inode *inode, struct file *filp)
{
/*释放申请的引脚,和中断*/
gpio_free(button_GPIO_number);
free_irq(interrupt_number, device_button);
return 0;
}
/*字符设备操作函数集*/
static struct file_operations button_chr_dev_fops = {
.owner = THIS_MODULE,
.open = button_open,
.read = button_read,
.release = button_release};
/*
*驱动初始化函数
*/
static int __init button_driver_init(void)
{
int error = -1;
/*采用动态分配的方式,获取设备编号,次设备号为0,*/
error = alloc_chrdev_region(&button_devno, 0, DEV_CNT, DEV_NAME);
if (error < 0)
{
printk("fail to alloc button_devno\n");
goto alloc_err;
}
/*关联字符设备结构体cdev与文件操作结构体file_operations*/
button_chr_dev.owner = THIS_MODULE;
cdev_init(&button_chr_dev, &button_chr_dev_fops);
/*添加设备至cdev_map散列表中*/
error = cdev_add(&button_chr_dev, button_devno, DEV_CNT);
if (error < 0)
{
printk("fail to add cdev\n");
goto add_err;
}
class_button = class_create(THIS_MODULE, DEV_NAME); //创建类
device_button = device_create(class_button, NULL, button_devno, NULL, DEV_NAME);//创建设备 DEV_NAME 指定设备名,
return 0;
add_err:
unregister_chrdev_region(button_devno, DEV_CNT); // 添加设备失败时,需要注销设备号
printk("\n error! \n");
alloc_err:
return -1;
}
/*
*驱动注销函数
*/
static void __exit button_driver_exit(void)
{
pr_info("button_driver_exit\n");
/*删除设备*/
device_destroy(class_button, button_devno); //清除设备
class_destroy(class_button); //清除类
cdev_del(&button_chr_dev); //清除设备号
unregister_chrdev_region(button_devno, DEV_CNT); //取消注册字符设备
}
module_init(button_driver_init);
module_exit(button_driver_exit);
MODULE_LICENSE("GPL");
//makefile
KERNEL_DIR=../../ebf_linux_kernel/build_image/build #内核地址,正确修改
ARCH=arm
CROSS_COMPILE=arm-linux-gnueabihf-
export ARCH CROSS_COMPILE
obj-m := interrupt.o
all:
$(MAKE) EXTRA_CFLAGS=-fno-pic -C $(KERNEL_DIR) M=$(CURDIR) modules
.PHONY:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean
make
编译后 有了interrupt.ko
就是我们的驱动
测试进程
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int error = -20;
int button_status = 0;
/*打开文件*/
int fd = open("/dev/button", O_RDWR);
if (fd < 0)
{
printf("open file : /dev/button error!\n");
return -1;
}
printf("等待按键按下 \n");
do
{
/*读取按键状态*/
error = read(fd, &button_status, sizeof(button_status));
if (error < 0)
{
printf("read file error! \n");
}
usleep(1000 * 100); //延时100毫秒
} while (0 == button_status);
printf("按键按下了,结束进程 !\n");
/*关闭文件*/
error = close(fd);
if (error < 0)
{
printf("close file error! \n");
}
return 0;
}
//makefile
out_file_name = "test_app"
all: test_app.c
arm-linux-gnueabihf-gcc $^ -o $(out_file_name)
.PHONY: clean
clean:
rm $(out_file_name)
移动到开发板
到此实验结束。