本文最后更新于331 天前,其中的信息可能已经过时,如有错误请发送邮件到273925452@qq.com
按键原理图
平时按键电平为高,按下按键后电平为低。
设备树生成工具i.MX Pins Tool v6
BOARD_InitPinsSnvs: BOARD_InitPinsSnvsGrp { /*!< Function assigned for the core: Cortex-A7[ca7] */
fsl,pins = <
MX6ULL_PAD_SNVS_TAMPER1__GPIO5_IO01 0x000110A0
>;
};
在设备树中添加pin controller组
pinctrl-0 = <&pinctrl_hog_2>; /* default */
pinctrl_hog_2: hoggrp-2 {
fsl,pins = <
MX6ULL_PAD_SNVS_TAMPER9__GPIO5_IO09 0x1b0b0 /* enet1 reset */
MX6ULL_PAD_SNVS_TAMPER6__GPIO5_IO06 0x1b0b0 /* enet2 reset */
MX6ULL_PAD_SNVS_TAMPER1__GPIO5_IO01 0x000110A0 /*key 1*/
>;
};
在设备树根目录下创建子节点
myButton{
compatible = "100ask,Button_drv";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_hog_2>;
Button-gpios = <&gpio5 1 GPIO_ACTIVE_LOW>;
};
编译拷贝设备树
具体操作和上一节一样
程序
Button_drv.c
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/of.h>
#include <linux/gpio.h>
/* 1. 确定主设备号 */
static int major = 0;
static struct class *Button_class;
static struct gpio_desc *Button_gpio; // GPIO操作结构体指针
static int err_g = 0;
/* 设备名 设备类 设备节点全局变量 */
static const char *device_name = "100ask_Button"; // 字符设备名
static const char *class_name = "100ask_Button_class"; // 设备类名
static const char *device_node_pattern = "100ask_Button0"; // 设备节点
/* 3. 实现对应的open/read/write等函数,填入file_operations结构体 */
static ssize_t Button_drv_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
int state;
state = gpiod_get_value(Button_gpio);
if (state < 0)
{
printk("Failed to get value for Button\n");
return state;
}
state = copy_to_user(buf, &state, 1);
return 1;
}
/* write(fd, &val, 1); */
static ssize_t Button_drv_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
// int err;
// char status;
// printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
// err = copy_from_user(&status, buf, 1);
return 0;
}
static int Button_drv_open(struct inode *node, struct file *file)
{
printk("open file is ok! \n");
return 0;
}
static int Button_drv_close(struct inode *node, struct file *file)
{
printk("close file is ok! \n");
return 0;
}
/* 定义自己的file_operations结构体 */
static struct file_operations Button_drv = {
.owner = THIS_MODULE,
.open = Button_drv_open,
.read = Button_drv_read,
.write = Button_drv_write,
.release = Button_drv_close,
};
/* 4. 从platform_device获得GPIO
* 把file_operations结构体告诉内核:注册驱动程序
*/
static int chip_demo_gpio_probe(struct platform_device *pdev)
{
// int err;
err_g = 1;
printk("err_g:%d\n", err_g);
/* 4.1 设备树中定义有: led-gpios=<...>; */
/*
该行代码的作用:从设备树中获取与 pdev 关联的设备上的 "led" GPIO 线。
&pdev->dev: 指向 struct device 结构体的指针,这个结构体描述了一个设备
gpiod_get():从设备树(Device Tree)中查找并获取一个 GPIO 控制器上的 GPIO 线。
*/
Button_gpio = gpiod_get(&pdev->dev, "Button", 0); //"led"对应设备树里面的<label>
if (IS_ERR(Button_gpio))
{
dev_err(&pdev->dev, "Failed to get GPIO for Button\n");
err_g = 2;
return PTR_ERR(Button_gpio);
}
gpiod_direction_input(Button_gpio);
/* 4.2 注册file_operations */
major = register_chrdev(0, device_name, &Button_drv); /* /dev/100ask_led0 */
Button_class = class_create(THIS_MODULE, class_name);
if (IS_ERR(Button_class))
{
unregister_chrdev(major, device_name); // 卸载驱动
gpiod_put(Button_gpio); // 释放GPIO
err_g = 3;
return PTR_ERR(Button_class);
}
/* /dev/100ask_led */
device_create(Button_class, NULL, MKDEV(major, 0), NULL, device_node_pattern);
return 0;
}
static int chip_demo_gpio_remove(struct platform_device *pdev)
{
device_destroy(Button_class, MKDEV(major, 0));
class_destroy(Button_class);
unregister_chrdev(major, device_name);
gpiod_put(Button_gpio);
return 0;
}
/* 定义设备树匹配表,用于识别和支持特定的LED驱动器 */
static const struct of_device_id ask100_Buttons[] = {
/* 匹配字符串 "100ask,leddrv" 用于标识100ASK系列LED驱动器 */
{.compatible = "100ask,Button_drv"},
/* 空项作为匹配表的结束标志 */
{},
};
/* 1. 定义platform_driver */
/* 定义一个平台驱动结构体,用于LED芯片的演示 */
static struct platform_driver chip_demo_gpio_driver = {
/* 设置探测函数,当设备被探测到时调用 */
.probe = chip_demo_gpio_probe,
/* 设置移除函数,当设备被移除时调用 */
.remove = chip_demo_gpio_remove,
/* 设置<驱动程序的名称>和<设备树匹配表> */
.driver = {
/* 设置驱动程序的名称 */
.name = "100ask_Button", // 字符设备名
/* 设置设备树匹配表,用于设备的匹配 */
.of_match_table = ask100_Buttons,
},
};
/* 2. 在入口函数注册platform_driver */
static int __init Button_init(void)
{
int err;
err = platform_driver_register(&chip_demo_gpio_driver);
printk("err_g:%d init_err:%d\n", err_g, err);
// printk("__FILE__:%s line:%d err:%d led_class:%d err_g:%d\n", __FILE__, __LINE__, err, led_class, err_g);
return err;
}
/* 3. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
* 卸载platform_driver
*/
static void __exit Button_exit(void)
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
device_destroy(Button_class, MKDEV(major, 0));
class_destroy(Button_class);
unregister_chrdev(major, device_name);
// gpiod_put(button_gpio);
platform_driver_unregister(&chip_demo_gpio_driver);
}
/* 7. 其他完善:提供设备信息,自动创建设备节点 */
module_init(Button_init);
module_exit(Button_exit);
MODULE_LICENSE("GPL");
Button_test.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <poll.h>
#include <signal.h>
#define BUTTON_DEV "/dev/100ask_Button0"
/*
* ./Button_test /dev/100ask_Button0
*/
int main(int argc, char **argv)
{
int fd;
char val;
struct pollfd fds[1];
const int timeout = 10000;
/* 1. 判断参数 */
if (argc != 2)
{
printf("Usage: %s /dev/<dev> \n", argv[0]);
return -1;
}
/* 2. 打开文件 */
fd = open(argv[1], O_RDWR | O_NONBLOCK);
if (fd == -1)
{
printf("can not open file %s\n", argv[1]);
return -1;
}
/* 3. 读文件 */
read(fd, &val, 1);
printf("get button : %d\n", val);
fds[0].fd = fd;
fds[0].events = POLLIN;
while(1)
{
if(poll(fds, 1, -1) == -1)
{
printf("poll error\n");
break;
}
if(fds[0].revents & POLLIN)
{
read(fd, &val, 1);
printf("get button : %d\n", val);
}
usleep(timeout);
}
close(fd);
return 0;
}
Makefile
# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
# 2.1 ARCH, 比如: export ARCH=arm64
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
# 2.3 PATH, 比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
# 请参考各开发板的高级用户使用手册
KERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.9.88 # 板子所用内核源码的目录
all:
make -C $(KERN_DIR) M=`pwd` modules
$(CROSS_COMPILE)gcc -o Button_test Button_test.c
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
rm -f Button_test
# 参考内核源码drivers/char/ipmi/Makefile
# 要想把a.c, b.c编译成ab.ko, 可以这样指定:
# ab-y := a.o b.o
# obj-m += ab.o
obj-m += Button_drv.o
评论