基于RK3399的LED驱动开发

1、添加设备树
在设备树
arch/arm64/boot/dts/rockchip/rk3399-firefly-linux.dts
中添加

gpio-led{
        status = "okay";
        compatible = "gpio-led";
        gpio-led1 = <&gpio1 8 GPIO_ACTIVE_LOW>;
        gpio-led2 = <&gpio1 9 GPIO_ACTIVE_LOW>;
};

2、驱动代码

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include <linux/io.h>
#include <linux/miscdevice.h>
#include <linux/gpio.h>
#include <linux/device.h>
#include <linux/ioport.h>
#include <linux/ioctl.h>
#include <linux/interrupt.h>
#include <linux/uaccess.h>
#include <linux/miscdevice.h>
#include <linux/wait.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>
#include <linux/of.h>
#include <linux/of_gpio.h>#define     LED_ON       _IO('B',0)
#define     LED_OFF      _IO('B',1)int gpio_led1,gpio_led2;
​
​
static long gec3399_led_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{   
    switch(cmd)
    {
        case LED_ON:
            gpio_set_value(gpio_led1,1);
            gpio_set_value(gpio_led2,1);
            break;
        case LED_OFF:
            gpio_set_value(gpio_led1,0);
            gpio_set_value(gpio_led2,0);
            break;
    }
    return 0;
}static int gec3399_led_release(struct inode *inode, struct file *file) 
{
    gpio_set_value(gpio_led1,0);   //应用程序退出时,关闭蜂鸣器
    gpio_set_value(gpio_led2,0);   //应用程序退出时,关闭蜂鸣器
    return 0;
}static const struct file_operations gec3399_led_fops = {
    .owner = THIS_MODULE,
    .unlocked_ioctl = gec3399_led_ioctl,
    .release = gec3399_led_release,
};   //文件操作集结构体
​
​
static struct miscdevice gec3399_led_misc = {
    .minor = MISC_DYNAMIC_MINOR,
    .fops = &gec3399_led_fops,
    .name   = "led_drv",    
};   //混杂设备结构体定义和初始化
​
​
​
static int gec3399_led_probe(struct platform_device *pdev)
{
    int ret;
    struct device_node *led_node = pdev->dev.of_node;
    //enum of_gpio_flags flag; 
    
    printk(KERN_ALERT"gpio-led math succee\n");
    
​
    gpio_led1 = of_get_named_gpio(led_node,"gpio-led1", 0); //从设备树获取GPIO号
  if (!gpio_is_valid(gpio_led1)) 
  {
    printk("gpio-led1: %d is invalid\n",gpio_led1);
        ret = -ENODEV;
        goto err_get_gpio1;
  } 
    printk("gpio-led1 = %d\n",gpio_led1);
    
    gpio_led2 = of_get_named_gpio(led_node,"gpio-led2", 0); //从设备树获取GPIO号
  if (!gpio_is_valid(gpio_led2)) 
  {
    printk("gpio-led2: %d is invalid\n",gpio_led2);
        ret = -ENODEV;
        goto err_get_gpio1;
  } 
    printk("gpio-led2 = %d\n",gpio_led2);   
    
    
    
    
    gpio_free(gpio_led1);
    ret = gpio_request(gpio_led1,"GPIO_LED1");    //申请gpio_led引脚为GPIO模式
    if(ret < 0){
        printk("gpio_request  gpio = GPIO_LED1 error\n");
        goto err_get_gpio1;
    }
    
    gpio_free(gpio_led2);
    ret = gpio_request(gpio_led2,"GPIO_LED2");    //申请gpio_led引脚为GPIO模式
    if(ret < 0){
        printk("gpio_request  gpio = GPIO_LED1 error\n");
        goto err_get_gpio2;
    }   
    
    
    ret = gpio_direction_output(gpio_led1,0);      //初始化LED1为关闭状态
    if(ret < 0){
        printk("gpio direction input  gpio = ak8963c_DYDR error\n");
        goto err_gpio_direction;
    }
    
    ret = gpio_direction_output(gpio_led2,0);      //初始化LED2为关闭状态
    if(ret < 0){
        printk("gpio direction input  gpio = ak8963c_DYDR error\n");
        goto err_gpio_direction;
    }
​
    ret =  misc_register(&gec3399_led_misc);   //注册字符设备
    if(ret < 0){
        printk("misc register error\n");
        goto err_misc_register;     
    }
    
    printk( KERN_ALERT "led dirve install succee\n");
    return 0;
    
err_misc_register:  
err_gpio_direction:
    gpio_free(gpio_led2);
err_get_gpio2:
    gpio_free(gpio_led1);
err_get_gpio1:
    return ret;
}
​
​
static int gec3399_led_remove(struct platform_device *pdev)
{
    gpio_free(gpio_led1);
    gpio_free(gpio_led2);
    misc_deregister(&gec3399_led_misc);
    printk(KERN_ALERT "led dirve rmove succee\n");
    return 0;
}
​
​
static const struct of_device_id of_gec_leder_match[] = {  
    { .compatible = "gpio-led", },    //compatible 兼容属性名,需与设备树节点的属性一致
    {},  
};static struct platform_driver gec3399_led_driver = {.driver  = {
          .name ="gpio-led",
          .owner = THIS_MODULE,
          .of_match_table = of_gec_leder_match,     //设备树设备匹配
     },
    .probe  = gec3399_led_probe,                 //驱动探测
    .remove  = gec3399_led_remove,   //驱动移除
};module_platform_driver(gec3399_led_driver);//module的描述,不是必需的。#modinfo led_drv.ko
MODULE_DESCRIPTION("led driver for RK3399");
MODULE_LICENSE("GPL"); //符合GPL协议
MODULE_VERSION("V1.0");

3、测试代码

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>//宏定义
#define     LED_ON       _IO('B',0)
#define     LED_OFF      _IO('B',1)int main(void)
{
    int fd_led;
    int ret;
    //第一步:打开设备节点
    fd_led = open("/dev/led_drv", O_WRONLY);
    if(fd_led < 0)
    {
        perror("open led driver");
        return -1;      
    }
  
    while(1)
    {
        //第二步:点亮LED灯    
        ret = ioctl(fd_led,LED_ON);
        if(ret < 0)
            perror("write led driver ");
        sleep(1);
        //第三步:关闭LED灯
        ret = ioctl(fd_led,LED_OFF);
        if(ret < 0 )
            perror("write led driver ");
        sleep(1);
    }

    //第四步:关闭设备节点
    close(fd_led);
    
    return 0;
}

4、Makefile文件

obj-m += led_drv.o
KERNELDIR:=/file/RK3399Pro/rk3399pro_git_repo/kernel
PWD:=$(shell pwd)default:
    $(MAKE)  -C $(KERNELDIR) M=$(PWD) modules
    
test:
    aarch64-linux-gnu-gcc led_test.c -o led_test    
​
clean:
    rm -rf *.o *.order .*.cmd *.ko *.mod.c *.symvers *.tmp_versions led_test

5、测试
insmod led_drv.ko
./led_test
实现LED灯的闪烁

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悠哉无忧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值