编写LED灯的驱动,使用GPIO子系统,里面添加按键的中断处理
1.应用程序发送指令控制LED亮灭
2.按键1 按下,led1电位反转 按键2按下,led2电位反转 按键3 按下,led3电位反转
驱动代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/device.h>
/* leds{
led1-gpios=<&gpioe 10 0>;//10表示引脚编号 0表示默认
led2-gpios=<&gpiof 10 0>;
led3-gpios=<&gpioe 8 0>;
};*/
unsigned int major; // 定义一个变量保存主设备号
struct class *cls;
struct device *cdev;
char kbuf[128]={0};
struct device_node *led_dev;
struct device_node *dev;
unsigned int irqno1;
unsigned int irqno2;
unsigned int irqno3;
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
irqreturn_t myirq_handler_led1(int irq, void *dev)
{
gpiod_set_value(gpiono1, !gpiod_get_value(gpiono1));
printk("KEY1_INTERRUPT\n");
return IRQ_HANDLED;
}
irqreturn_t myirq_handler_led2(int irq, void *dev)
{
gpiod_set_value(gpiono2, !gpiod_get_value(gpiono2));
printk("KEY2_INTERRUPT\n");
return IRQ_HANDLED;
}
irqreturn_t myirq_handler_led3(int irq, void *dev)
{
gpiod_set_value(gpiono3, !gpiod_get_value(gpiono3));
printk("KEY3_INTERRUPT\n");
return IRQ_HANDLED;
}
// 封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
long ret;
ret = copy_to_user(ubuf, kbuf, size);
if (ret)
{
printk("copy_to_user filed\n");
return -EIO;
}
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
long ret;
ret = copy_from_user(kbuf, ubuf, size);
if (ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
// 判断用户输入的数据,进行不同的硬件逻辑执行
if (kbuf[0] == '1') // 操作led1
{
// 关灯逻辑
if (kbuf[1] == '0')
gpiod_set_value(gpiono1, 0);
else if (kbuf[1] == '1')
// 开灯逻辑
gpiod_set_value(gpiono1, 1);
}
if (kbuf[0] == '2') // 操作led2
{
// 关灯逻辑
if (kbuf[1] == '0')
gpiod_set_value(gpiono2, 0);
else if (kbuf[1] == '1')
// 开灯逻辑
gpiod_set_value(gpiono2, 1);
}
if (kbuf[0] == '3') // 操作led3
{
// 关灯逻辑
if (kbuf[1] == '0')
gpiod_set_value(gpiono3, 0);
else if (kbuf[1] == '1')
// 开灯逻辑
gpiod_set_value(gpiono3, 1);
}
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
// 定义一个操作方法结构体变量并且初始化
struct file_operations fops = {
.open = mycdev_open,
.release = mycdev_close,
.read = mycdev_read,
.write = mycdev_write,
};
static int __init mycdev_init(void)
{
// 根据设备树节点的路径解析设备树信息
led_dev = of_find_node_by_path("/myleds");
if (led_dev == NULL)
{
printk("解析设备树节点失败\n");
return -EFAULT;
}
printk("解析设备树节点成功\n");
// 申请gpio_desc对象并设置输出为低电平
gpiono1 = gpiod_get_from_of_node(led_dev, "led1", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono1))
{
printk("申请gpio对象失败\n");
return -PTR_ERR(gpiono1);
}
printk("led1申请gpio对象成功\n");
gpiono2 = gpiod_get_from_of_node(led_dev, "led2", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono2))
{
printk("申请gpio对象失败\n");
return -PTR_ERR(gpiono2);
}
printk("led2申请gpio对象成功\n");
gpiono3 = gpiod_get_from_of_node(led_dev, "led3", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono3))
{
printk("申请gpio对象失败\n");
return -PTR_ERR(gpiono3);
}
printk("led3申请gpio对象成功\n");
int ret;
// 解析按键的设备树节点
dev = of_find_node_by_path("/myirq");
if (dev == NULL)
{
printk("解析设备树节点失败\n");
return -EFAULT;
}
printk("解析设备树节点成功\n");
// 根据设备树节点解析出软中断号
irqno1 = irq_of_parse_and_map(dev, 0); // 按键1索引号为0
if (!irqno1)
{
printk("解析key1软中断号失败\n");
return -ENXIO;
}
irqno2 = irq_of_parse_and_map(dev, 1); // 按键1索引号为0
if (!irqno2)
{
printk("解析key2软中断号失败\n");
return -ENXIO;
}
irqno3 = irq_of_parse_and_map(dev, 2); // 按键1索引号为0
if (!irqno3)
{
printk("解析key3软中断号失败\n");
return -ENXIO;
}
printk("解析软中断号成功 irqn1=%d irqn2=%d irqn3=%d\n", irqno1, irqno2, irqno3);
// 注册中断
ret = request_irq(irqno1, myirq_handler_led1, IRQF_TRIGGER_FALLING, "key1", NULL);
if (ret)
{
printk("注册key1中断失败\n");
return ret;
}
printk("注册key1中断成功\n");
ret = request_irq(irqno2, myirq_handler_led2, IRQF_TRIGGER_FALLING, "key2", NULL);
if (ret)
{
printk("注册key2中断失败\n");
return ret;
}
printk("注册key2中断成功\n");
ret = request_irq(irqno3, myirq_handler_led3, IRQF_TRIGGER_FALLING, "key3", NULL);
if (ret)
{
printk("注册key3中断失败\n");
return ret;
}
printk("注册key3中断成功\n");
// 注册字符设备驱动
major = register_chrdev(0, "mychrdev", &fops);
if (major < 0)
{
printk("注册字符设备驱动失败\n");
return major;
}
printk("注册字符设备驱动成功major=%d\n", major);
// 向上提交目录
cls = class_create(THIS_MODULE, "myled");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
// 向上提交设备节点信息
cdev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myleds");
if (IS_ERR(cdev))
{
printk("向上提交节点失败\n");
return -PTR_ERR(cdev);
}
printk("向上提交节点成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
// 灭灯
gpiod_set_value(gpiono1, 0);
gpiod_set_value(gpiono2, 0);
gpiod_set_value(gpiono3, 0);
// 释放gpio编号
gpiod_put(gpiono1);
gpiod_put(gpiono2);
gpiod_put(gpiono3);
// 注销中断
free_irq(irqno1, NULL);
free_irq(irqno2, NULL);
free_irq(irqno3, NULL);
// 销毁节点信息
device_destroy(cls, MKDEV(major, 0));
// 销毁目录信息
class_destroy(cls);
// 注销字符设备驱动
unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
应用程序
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char buf[128] = {0};
int fd = open("/dev/myleds", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
printf("请输入控制命令:(11)led1亮 (10)led1灭\n(21)led2亮 (20)led2灭\n(31)led1亮 (30)led3灭\n>>>>");
fgets(buf, sizeof(buf), stdin); // 从终端输入数据到buf
buf[strlen(buf) - 1] = '\0'; // 将buf末尾的'\n'切换为'\0'
write(fd, buf, sizeof(buf));
}
close(fd);
return 0;
}