华清远见嵌入式学习——驱动开发——DAY8

作业要求:

1.使用GPIO子系统编写LED灯驱动,应用程序测试

2.注册三个按键的中断,只需要写内核代码

需要发布到CSDN

作业答案:

GPIO子系统:

代码效果:

应用程序:

cs 复制代码
#include<stdlib.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<unistd.h>
#include<string.h>
#include<sys/ioctl.h>

//创建功能码
#define LED_ON _IOW('l',1,int)  
#define LED_OFF _IOW('l',0,int)
 
int main(int argc, char const *argv[])
{
    int a,b;
    int fd=open("/dev/myled0",O_RDWR);
    if(fd<0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while(1)
    {
        //从终端读取
        printf("请输入要实现的功能\n");
        printf("0(关灯) 1(开灯)\n");
        printf("请输入>");
        scanf("%d",&a);
        printf("请输入要控制的灯\n");
        printf("1(LED1) 2(LED2) 3(LED3)\n");
        printf("请输入>");
        scanf("%d",&b);
        switch(a)
        {
            case 1:
                ioctl(fd,LED_ON,b);
                break;
            case 0:
                ioctl(fd,LED_OFF,b);
                break;
        }
    }
 
    
    close(fd);
 
    return 0;
}

驱动程序:

cs 复制代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/of_irq.h>

int major;
char kbuf[128] = {0};
struct class *cls;
struct device *dev;

struct gpio_desc *gpiono1; // led1设备号
struct gpio_desc *gpiono2; // led2设备号
struct gpio_desc *gpiono3; // led3设备号
struct device_node *dnode; // 保存解析到的设备树节点地址

// 创建功能码
#define LED_ON _IOW('l', 1, int)
#define LED_OFF _IOW('l', 0, int)

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__);
    unsigned long ret;
    // 向用户空间读取拷贝
    if (size > sizeof(kbuf)) // 用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小
        size = sizeof(kbuf);
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret) // 拷贝失败
    {
        printk("copy_to_user filed\n");
        return ret;
    }
    return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
    unsigned long ret;
    // 从用户空间读取数据
    if (size > sizeof(kbuf)) // 用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小
        size = sizeof(kbuf);
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret) // 拷贝失败
    {
        printk("copy_to_user filed\n");
        return ret;
    }

    return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    switch (cmd)
    {
    case LED_ON:
        switch (arg)
        {
        case 1:
            gpiod_set_value(gpiono1, 1);
            break;
        case 2:
            gpiod_set_value(gpiono2, 1);
            break;
        case 3:
            gpiod_set_value(gpiono3, 1);
            break;
        }
        break;
    case LED_OFF:
        switch (arg)
        {
        case 1:
            gpiod_set_value(gpiono1, 0);
            break;
        case 2:
            gpiod_set_value(gpiono2, 0);
            break;
        case 3:
            gpiod_set_value(gpiono3, 0);
            break;
        }

        break;
    }
    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,
    .read = mycdev_read,
    .write = mycdev_write,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};

static int __init mycdev_init(void)
{
    // 字符设备驱动注册
    major = register_chrdev(0, "mychrdev", &fops);
    if (major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功:major=%d\n", major);
    // 向上提交目录
    cls = class_create(THIS_MODULE, "mychrdev");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");
    // 向上提交设备节点信息
    int i; // 向上提交三次设备节点信息
    for (i = 0; i < 3; i++)
    {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
        if (IS_ERR(dev))
        {
            printk("向上提交设备节点失败\n");
            return -PTR_ERR(dev);
        }
    }

    printk("向上提交设备节点成功\n");

    // 解析LED灯设备树节点
    dnode = of_find_node_by_path("/myled");
    if (dnode == NULL)
    {
        printk("解析设备树节点失败\n");
        return -ENXIO;
    }
    printk("解析设备树节点成功\n");
    // 解析LED1的gpio编号
    gpiono1 = gpiod_get_from_of_node(dnode, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);
    if (gpiono1 == NULL)
    {
        printk("解析gpio编号失败\n");
        return -ENXIO;
    }
    printk("解析gpio编号成功\n");
    // 解析LED2的gpio编号
    gpiono2 = gpiod_get_from_of_node(dnode, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);
    if (gpiono2 == NULL)
    {
        printk("解析gpio编号失败\n");
        return -ENXIO;
    }
    printk("解析gpio编号成功\n");
    // 解析LED3的gpio编号
    gpiono3 = gpiod_get_from_of_node(dnode, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);
    if (gpiono3 == NULL)
    {
        printk("解析gpio编号失败\n");
        return -ENXIO;
    }
    printk("解析gpio编号成功\n");
    return 0;
}
static void __exit mycdev_exit(void)
{
    // 销毁设备节点信息
    device_destroy(cls, MKDEV(major, 0));

    // 销毁设备节点信息
    int i;
    for (i = 0; i < 3; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }

    // 释放gpio编号
    gpiod_put(gpiono1);
    gpiod_put(gpiono2);
    gpiod_put(gpiono3);

    // 销毁目录
    class_destroy(cls);
    // 注销字符设备驱动
    unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

中断子系统:

代码效果:

驱动程序:

cs 复制代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/of_irq.h>
/*myirq{
    compatible = "hqyj,myirq";
    interrupt-parent = <&gpiof>;
    interrupts=<9 0>,<7 0>,<8 0>;
};
*/

struct device_node *dnode_led; // 保存解析到的led设备树节点地址
struct device_node *dnode_int; // 保存解析到的中断设备树节点地址
unsigned int irqno1;       // 按键1软中断号
unsigned int irqno2;       // 按键2软中断号
unsigned int irqno3;       // 按键3软中断号
struct gpio_desc *gpiono1; // led1设备号
struct gpio_desc *gpiono2; // led2设备号
struct gpio_desc *gpiono3; // led3设备号

// 中断处理函数1
irqreturn_t myirq_handler1(int irq, void *dev)
{
    printk("key1_intc\n");
    //关灯三 开灯一
    gpiod_set_value(gpiono3, 0);
    gpiod_set_value(gpiono1, 1);
    return IRQ_HANDLED;
}
// 中断处理函数2
irqreturn_t myirq_handler2(int irq, void *dev)
{
    printk("key2_intc\n");
    //关灯一 开灯二
    gpiod_set_value(gpiono1, 0);
    gpiod_set_value(gpiono2, 1);
    return IRQ_HANDLED;
}
// 中断处理函数3
irqreturn_t myirq_handler3(int irq, void *dev)
{
    printk("key3_intc\n");
    //关灯二 开灯三
    gpiod_set_value(gpiono2, 0);
    gpiod_set_value(gpiono3, 1);
    return IRQ_HANDLED;
}

static int __init mycdev_init(void)
{
    // 解析按键的设备树节点
    dnode_int = of_find_compatible_node(NULL, NULL, "hqyj,myirq");
    if (dnode_int == NULL)
    {
        printk("解析设备树节点失败\n");
        return -ENXIO;
    }
    printk("解析设备树节点成功\n");

    // 解析LED灯设备树节点
    dnode_led = of_find_node_by_path("/myled");
    if (dnode_led == NULL)
    {
        printk("解析设备树节点失败\n");
        return -ENXIO;
    }
    printk("解析设备树节点成功\n");

    // 解析按键1的软中断号
    irqno1 = irq_of_parse_and_map(dnode_int, 0);
    if (!irqno1)
    {
        printk("解析按键1软中断号失败\n");
        return -ENXIO;
    }
    printk("解析按键1软中断号成功%d\n", irqno1);
    // 解析按键2的软中断号
    irqno2 = irq_of_parse_and_map(dnode_int, 1);
    if (!irqno2)
    {
        printk("解析按键2软中断号失败\n");
        return -ENXIO;
    }
    printk("解析按键2软中断号成功%d\n", irqno2);
    // 解析按键3的软中断号
    irqno3 = irq_of_parse_and_map(dnode_int, 2);
    if (!irqno3)
    {
        printk("解析按键3软中断号失败\n");
        return -ENXIO;
    }
    printk("解析按键3软中断号成功%d\n", irqno3);
    // 注册按键1中断
    int ret1 = request_irq(irqno1, myirq_handler1, IRQF_TRIGGER_FALLING, "key1", (void *)1);
    if (ret1)
    {
        printk("中断注册失败\n");
        return ret1;
    }
    printk("中断注册成功\n");
    // 注册按键2中断
    int ret2 = request_irq(irqno2, myirq_handler2, IRQF_TRIGGER_FALLING, "key2", (void *)2);
    if (ret2)
    {
        printk("中断注册失败\n");
        return ret2;
    }
    printk("中断注册成功\n");
    // 注册按键3中断
    int ret3 = request_irq(irqno3, myirq_handler3, IRQF_TRIGGER_FALLING, "key3", (void *)3);
    if (ret3)
    {
        printk("中断注册失败\n");
        return ret3;
    }
    printk("中断注册成功\n");

    // 解析LED1的gpio编号
    gpiono1 = gpiod_get_from_of_node(dnode_led, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);
    if (gpiono1 == NULL)
    {
        printk("解析gpio编号失败\n");
        return -ENXIO;
    }
    printk("解析gpio编号成功\n");
    // 解析LED2的gpio编号
    gpiono2 = gpiod_get_from_of_node(dnode_led, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);
    if (gpiono2 == NULL)
    {
        printk("解析gpio编号失败\n");
        return -ENXIO;
    }
    printk("解析gpio编号成功\n");
    // 解析LED3的gpio编号
    gpiono3 = gpiod_get_from_of_node(dnode_led, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);
    if (gpiono3 == NULL)
    {
        printk("解析gpio编号失败\n");
        return -ENXIO;
    }
    printk("解析gpio编号成功\n");

    return 0;
}
static void __exit mycdev_exit(void)
{
    // 释放软中断号
    free_irq(irqno1, (void *)1);
    free_irq(irqno2, (void *)2);
    free_irq(irqno3, (void *)3);

    // 释放gpio编号
    gpiod_put(gpiono1);
    gpiod_put(gpiono2);
    gpiod_put(gpiono3);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
相关推荐
西岸行者12 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
悠哉悠哉愿意12 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码12 天前
嵌入式学习路线
学习
毛小茛12 天前
计算机系统概论——校验码
学习
babe小鑫12 天前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
winfreedoms12 天前
ROS2知识大白话
笔记·学习·ros2
在这habit之下12 天前
Linux Virtual Server(LVS)学习总结
linux·学习·lvs
我想我不够好。12 天前
2026.2.25监控学习
学习
im_AMBER12 天前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode
CodeJourney_J12 天前
从“Hello World“ 开始 C++
c语言·c++·学习