【Linux驱动】六、设备树-点亮LED
【Linux驱动】六、设备树-点亮LED
本文最后更新于332 天前,其中的信息可能已经过时,如有错误请发送邮件到273925452@qq.com
Avatar
GPIO 的地位跟其他模块,比如 I2C、UART 的地方是一样的,要使用某个引脚,需要先把引脚配置为 GPIO 功能,这要使用 Pinctrl 子系统,只需要在设备树里指定就可以。在驱动代码上不需要我们做任何事情。
GPIO 本身需要确定引脚,这也需要在设备树里指定。

LED原理图


设备树生成工具i.MX Pins Tool v6

pinctrl_tsc_reset: tscresetgrp  {        /*!< Function assigned for the core: Cortex-A7[ca7] */
            fsl,pins = <
                MX6ULL_PAD_SNVS_TAMPER2__GPIO5_IO02        0x000110A0
            >;
        };

在设备树中添加pin controller组

pin controller
1.路径:
vi /home/book/100ask_imx6ull-sdk/Linux-4.9.88/arch/arm/boot/dts/100ask_imx6ull-14×14.dts
2.在设备树里面搜索(按下 / ): &iomuxc_snv
3.复制上面设备树工具生成的片段进去


在设备树根目录下创建子节点

子节点
跳转到指定行数: 按下esc然后 按:1
  myled{
        compatible = "100ask,leddrv" ;   //指定了设备与哪个驱动程序兼容,要与驱动程序里面对应
        pinctrl-names = "default";       
        pinctrl-0 = <&pinctrl_leds>;     //指向了名为pinctrl_leds的pin controller配置组
        led-gpios = <&gpio5 3 GPIO_ACTIVE_LOW>;  //指定了与LED设备关联的GPIO线,这里的led是<label>,要与驱动程序里面对应
       };

注意
因为IMX6ULL_PRO已经使用GPIO5_3作为CPU指示灯,所以要找到该节点的地方去禁止掉
搜索: /MX6ULL_PAD_SNVS_TAMPER3__GPIO5_IO03得到CPU状态灯的子节点
注意
在搜索: /pinctrl_leds
在里面添加 status = “disabled”;
按下ESC :wq (保存退出)

编译设备树

路径: /home/book/100ask_imx6ull-sdk/Linux-4.9.88

make dtbs


程序

leddrv.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 *led_class;
static struct gpio_desc *led_gpio;   //GPIO操作结构体指针
static int err_g = 0;

/* 设备名 设备类 设备节点全局变量 */
static const char *device_name = "100ask_led";           //字符设备名
static const char *class_name = "100ask_led_class";      //设备类名
static const char *device_node_pattern = "100ask_led0";   //设备节点





/* 3. 实现对应的open/read/write等函数,填入file_operations结构体                   */
static ssize_t led_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    return 0;
}



/* write(fd, &val, 1); */
static ssize_t led_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
    int err;
    char status;
    //struct inode *inode = file_inode(file);
    //int minor = iminor(inode);

    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    err = copy_from_user(&status, buf, 1);

    /* 根据次设备号和status控制LED */
    gpiod_set_value(led_gpio, status);
    
    return 1;
}

static int led_drv_open (struct inode *node, struct file *file)
{
    // int minor = iminor(node);

    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    /* 根据次设备号初始化LED */
    gpiod_direction_output(led_gpio, 1);  //根据原理图低电平亮
    
    return 0;
}

static int led_drv_close (struct inode *node, struct file *file)
{
    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    return 0;
}

/* 定义自己的file_operations结构体                                              */
static struct file_operations led_drv = {
    .owner     = THIS_MODULE,
    .open    = led_drv_open,
    .read    = led_drv_read,
    .write   = led_drv_write,
    .release = led_drv_close,
};


/* 4. 从platform_device获得GPIO
 *    把file_operations结构体告诉内核:注册驱动程序
 */static int chip_demo_gpio_probe(struct platform_device *pdev)
{
    //int err;
    err_g = 1;

    /* 4.1 设备树中定义有: led-gpios=<...>;    */
    /*
        该行代码的作用:从设备树中获取与 pdev 关联的设备上的 "led" GPIO 线。
        &pdev->dev: 指向 struct device 结构体的指针,这个结构体描述了一个设备
        gpiod_get():从设备树(Device Tree)中查找并获取一个 GPIO 控制器上的 GPIO 线。
    */
    led_gpio = gpiod_get(&pdev->dev, "led", 0);  //"led"对应设备树里面的<label>
    if (IS_ERR(led_gpio)) {
        dev_err(&pdev->dev, "Failed to get GPIO for led\n");
        err_g = 2;
        return PTR_ERR(led_gpio);
        
    }

    /* 4.2 注册file_operations     */
    major = register_chrdev(0, device_name, &led_drv); /* /dev/100ask_led0 */

    led_class = class_create(THIS_MODULE, class_name);
    if (IS_ERR(led_class))
    {
        printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
        unregister_chrdev(major, "led"); // 卸载驱动
        gpiod_put(led_gpio);             // 释放GPIO
        err_g = 3;
        return PTR_ERR(led_class);
        
    }

    /* /dev/100ask_led */
    device_create(led_class, NULL, MKDEV(major, 0), NULL, device_node_pattern);

    return 0;
    
}

static int chip_demo_gpio_remove(struct platform_device *pdev)
{
    device_destroy(led_class, MKDEV(major, 0));
    class_destroy(led_class);
    unregister_chrdev(major, device_name);
    gpiod_put(led_gpio);
    
    return 0;
}


/* 定义设备树匹配表,用于识别和支持特定的LED驱动器 */
static const struct of_device_id ask100_leds[] = {
    /* 匹配字符串 "100ask,leddrv" 用于标识100ASK系列LED驱动器 */
    { .compatible = "100ask,leddrv" },
    /* 空项作为匹配表的结束标志 */
    { },
};

/* 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_led", // 字符设备名
    /* 设置设备树匹配表,用于设备的匹配 */
        .of_match_table = ask100_leds,
    },
};

/* 2. 在入口函数注册platform_driver */
static int __init led_init(void)
{
    int err;
    err = platform_driver_register(&chip_demo_gpio_driver);

    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 led_exit(void)
{
    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    device_destroy(led_class, MKDEV(major, 0));
    class_destroy(led_class);
    unregister_chrdev(major, "100ask_led");
    gpiod_put(led_gpio);
    
    platform_driver_unregister(&chip_demo_gpio_driver);
}


/* 7. 其他完善:提供设备信息,自动创建设备节点                                     */

module_init(led_init);
module_exit(led_exit);

MODULE_LICENSE("GPL");


ledtest.c


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

/*
 * ./ledtest /dev/100ask_led0 on
 * ./ledtest /dev/100ask_led0 off
 */
int main(int argc, char **argv)
{
    int fd;
    char status;
    
    /* 1. 判断参数 */
    if (argc != 3) 
    {
        printf("Usage: %s <dev> <on | off>\n", argv[0]);
        return -1;
    }

    /* 2. 打开文件 */
    fd = open(argv[1], O_RDWR);
    if (fd == -1)
    {
        printf("can not open file %s\n", argv[1]);
        return -1;
    }

    /* 3. 写文件 */
    if (0 == strcmp(argv[2], "on"))
    {
        status = 1;
        write(fd, &status, 1);
    }
    else
    {
        status = 0;
        write(fd, &status, 0);
    }
    
    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 ledtest ledtest.c 

clean:
    make -C $(KERN_DIR) M=`pwd` modules clean
    rm -rf modules.order
    rm -f ledtest

# 参考内核源码drivers/char/ipmi/Makefile
# 要想把a.c, b.c编译成ab.ko, 可以这样指定:
# ab-y := a.o b.o
# obj-m += ab.o



obj-m += leddrv.o

上机

注意
复制 100ask_imx6ull-14×14.dtb 到自己要挂载的网络文件系统:
cp arch/arm/boot/dts/100ask_imx6ull-14×14.dtb ~/nfs_rootfs/Study_Li

启动开发板,挂载网络文件系统,复制设备树到boot目录:
cp /mnt/Study_Li/100ask_imx6ull-14×14.dtb /boot/

重启开发板:
reboot

挂载系统,安装驱动,列出节点(ls /dev/100ask_led0),执行程序(./ledtest /dev/100ask_led0 on/off)
💡商业转载请联系作者获得授权,非商业转载请注明出处。
协议(License):署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)。
使用这些免费资源的时候应遵守版权意识,切勿非法利用,售卖,尊重原创内容知识产权。未经允许严禁转载。

评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇