基于RK3399的LED驱动开发
生活随笔
收集整理的這篇文章主要介紹了
基于RK3399的LED驱动开发
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、添加設備樹
在設備樹
arch/arm64/boot/dts/rockchip/rk3399-firefly-linux.dts
中添加
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) modulestest:aarch64-linux-gnu-gcc led_test.c -o led_test ? clean:rm -rf *.o *.order .*.cmd *.ko *.mod.c *.symvers *.tmp_versions led_test5、測試
insmod led_drv.ko
./led_test
實現LED燈的閃爍
總結
以上是生活随笔為你收集整理的基于RK3399的LED驱动开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 返回链表的中间结点,若中间有两个结点,则
- 下一篇: 官宣 | 效率源文档修复神器正式出道:超