日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LED驱动框架

發布時間:2025/4/5 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LED驱动框架 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 LED驅動框架

1.1 回顧字符設備驅動程序框架

1.2 對于 LED 驅動,我們想要什么樣的接口?

1.3 LED 驅動要怎么寫,才能支持多個板子?分層

  • 把驅動拆分為通用的框架(leddrv.c)、具體的硬件操作(board_X.c):
  • 以面向對象的思想,改進代碼:
    抽象出一個結構體:

    每個單板相關的 board_X.c 實現自己的 led_operations 結構體,供上層的 leddrv.c 調用:

  • 2 代碼實現

    首先看下文件:

    led_opr.h:

    #ifndef _LED_OPR_H #define _LED_OPR_Hstruct led_operations {int (*init) (int which); /* 初始化LED, which-哪個LED */ int (*ctl) (int which, char status); /* 控制LED, which-哪個LED, status:1-亮,0-滅 */ };struct led_operations *get_board_led_opr(void);#endif

    board_demo.c:

    #include <linux/module.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 "led_opr.h"static int board_demo_led_init (int which) /* 初始化LED, which-哪個LED */ {printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);return 0; }static int board_demo_led_ctl (int which, char status) /* 控制LED, which-哪個LED, status:1-亮,0-滅 */ {printk("%s %s line %d, led %d, %s\n", __FILE__, __FUNCTION__, __LINE__, which, status ? "on" : "off");return 0; }static struct led_operations board_demo_led_opr = {.init = board_demo_led_init,.ctl = board_demo_led_ctl, };struct led_operations *get_board_led_opr(void) {return &board_demo_led_opr; }

    leddrv.c:

    #include <linux/module.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 "led_opr.h"#define LED_NUM 2/* 1. 確定主設備號 */ static int major = 0; static struct class *led_class; struct led_operations *p_led_opr;#define MIN(a, b) (a < b ? a : b)/* 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 */p_led_opr->ctl(minor, 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 */p_led_opr->init(minor);return 0; }static int led_drv_close (struct inode *node, struct file *file) {printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);return 0; }/* 2. 定義自己的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. 把file_operations結構體告訴內核:注冊驅動程序 */ /* 5. 誰來注冊驅動程序啊?得有一個入口函數:安裝驅動程序時,就會去調用這個入口函數 */ static int __init led_init(void) {int err;int i;printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);major = register_chrdev(0, "100ask_led", &led_drv); /* /dev/led */led_class = class_create(THIS_MODULE, "100ask_led_class");err = PTR_ERR(led_class);if (IS_ERR(led_class)) {printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);unregister_chrdev(major, "100ask_led");return -1;}for (i = 0; i < LED_NUM; i++)device_create(led_class, NULL, MKDEV(major, i), NULL, "100ask_led%d", i); /* /dev/100ask_led0,1,... */p_led_opr = get_board_led_opr();return 0; }/* 6. 有入口函數就應該有出口函數:卸載驅動程序時,就會去調用這個出口函數 */ static void __exit led_exit(void) {int i;printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);for (i = 0; i < LED_NUM; i++)device_destroy(led_class, MKDEV(major, i)); /* /dev/100ask_led0,1,... */device_destroy(led_class, MKDEV(major, 0));class_destroy(led_class);unregister_chrdev(major, "100ask_led"); }/* 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, 1);}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_roc-rk3399-pc/linux-4.4all:make -C $(KERN_DIR) M=`pwd` modules $(CROSS_COMPILE)gcc -o ledtest ledtest.c clean:make -C $(KERN_DIR) M=`pwd` modules cleanrm -rf modules.orderrm -f ledtest# 參考內核源碼drivers/char/ipmi/Makefile # 要想把a.c, b.c編譯成ab.ko, 可以這樣指定: # ab-y := a.o b.o # obj-m += ab.o# leddrv.c board_demo.c 編譯成 100ask.ko 100ask_led-y := leddrv.o board_demo.o obj-m += 100ask_led.o

    總結

    以上是生活随笔為你收集整理的LED驱动框架的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。