实现过程:
驱动代码:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/list.h>
#include <linux/stddef.h>
#include <linux/spinlock.h>
#include <asm/current.h>
#include <uapi/linux/wait.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/poll.h>
// 开关灯命令码
#define LED1 _IOW('a', 1, int)
#define LED2 _IOW('a', 2, int)
#define LED3 _IOW('a', 3, int)
struct cdev *cdev;
dev_t devno;
unsigned int major = 0; // 动态态指定主设备号
unsigned int minor = 0;
struct class *cls;
struct device *dev;
struct device_node *led_node, *interrupt_node, *fnode; // 灯节点,中断节点,灯节点的子节点
struct gpio_desc *gpio_num[4] = {0}; // gpio编号
char *led[4] = {"led1", "led2", "led3", "fan"}; // 键名
int irq_num; // 按键中断号
int i;
struct tasklet_struct tasklet;
// 中断处理函数
irqreturn_t irq_handler(int irq_num, void *arg)
{
gpiod_set_value(gpio_num[3], !gpiod_get_value(gpio_num[3]));
return IRQ_HANDLED;
}
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
for (i = 0; i < 3; i++)
{
gpiod_set_value(gpio_num[i], 0);
}
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long args)
{
switch (cmd)
{
case LED1:
gpiod_set_value(gpio_num[0], !gpiod_get_value(gpio_num[0]));
break;
case LED2:
gpiod_set_value(gpio_num[1], !gpiod_get_value(gpio_num[1]));
break;
case LED3:
gpiod_set_value(gpio_num[2], !gpiod_get_value(gpio_num[2]));
break;
default:
break;
}
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
const struct file_operations fops = {
.open = mycdev_open,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
int ret, i;
// 1.分配对象
cdev = cdev_alloc();
if (cdev == NULL)
{
printk("分配对象空间失败\n");
ret = -ENOMEM;
goto ERR1;
}
printk("分配对象空间成功\n");
// 2.初始化驱动对象
cdev_init(cdev, &fops);
// 申请设备号,动态申请
ret = alloc_chrdev_region(&devno, minor, 3, "mycdev");
if (ret)
{
printk("动态申请设备号失败\n");
goto ERR2;
}
major = MAJOR(devno); // 获取主设备号
minor = MINOR(devno); // 获取次设备号
// 3.注册对象
ret = cdev_add(cdev, MKDEV(major, minor), 3);
if (ret)
{
printk("驱动对象注册进内核失败\n");
goto ERR3;
}
printk("驱动对象注册进内核成功\n");
// 向上提交目录
cls = class_create(THIS_MODULE, "mycdev");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
goto ERR4;
}
printk("向上提交目录成功\n");
// 4.向上提交设备节点
for (i = 0; i < 3; i++)
{
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "mycdev%d", i);
if (IS_ERR(dev))
{
printk("向上提交节点信息失败\n");
goto ERR5;
}
}
printk("向上提交设备节点成功\n");
interrupt_node = of_find_node_by_name(NULL, "keyirq");
if (interrupt_node == NULL)
{
printk("解析myirq设备失败\n");
return -ENOENT;
}
printk("解析树myirq设备成功\n");
irq_num = irq_of_parse_and_map(interrupt_node, 0);
if (!irq_num)
{
printk("获取软中断号失败\n");
}
printk("获取软中断号成功\n");
// 注册中断号
request_irq(irq_num, irq_handler, IRQF_TRIGGER_FALLING, "myirq", NULL);
printk("注册中断号成功\n");
// 解析设备树节点
led_node = of_find_node_by_name(NULL, "myleds");
if (led_node == NULL)
{
printk("解析myleds设备失败\n");
return -ENOENT;
}
printk("解析树myleds设备成功\n");
// 解析子节点
fnode = of_get_child_by_name(led_node, "extend-leds");
if (fnode == NULL)
{
printk("解析core_leds设备失败\n");
return -ENOENT;
}
printk("解析core_leds设备成功\n");
for (i = 0; i < 4; i++)
{
// 解析编号
gpio_num[i] = gpiod_get_from_of_node(fnode, led[i], 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpio_num[i]))
{
printk("解析编号gpio_num%d失败\n", i);
return PTR_ERR(gpio_num[i]);
}
printk("解析编号gpio_num%d成功\n", i);
}
return 0;
ERR5:
// 将前面提交成功的设备信息销毁
for (--i; i >= 0; i--)
{
device_destroy(cls, MKDEV(major, i));
}
// 销毁目录
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major, minor), 3);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
static void __exit mycdev_exit(void)
{
for (i = 0; i < 4; i++)
{
gpiod_put(gpio_num[i]);
}
free_irq(irq_num, NULL);
for (i = 0; i < 3; i++)
{
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
cdev_del(cdev);
unregister_chrdev_region(MKDEV(major, minor), 3);
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
应用层代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
// 灯号命令码
#define LED1 _IOW('a', 1, int)
#define LED2 _IOW('a', 2, int)
#define LED3 _IOW('a', 3, int)
int main(int argc, const char *argv[])
{
int fd;
fd = open("/dev/mycdev0", O_RDWR);
if (fd < 0)
{
printf("打开失败\n");
return -1;
}
while (1)
{
ioctl(fd, LED1);
usleep(200000);
ioctl(fd, LED2);
usleep(200000);
ioctl(fd, LED3);
usleep(200000);
};
close(fd);
return 0;
}