韦东山嵌入式linux系列-驱动设计的思想(面向对象/分层/分离)
1 面向对象

字符设备驱动程序抽象出一个 file_operations 结构体;
我们写的程序针对硬件部分抽象出 led_operations 结构体。
2 分层
上下分层,比如我们前面写的 LED 驱动程序就分为 2 层:
① 上层实现硬件无关的操作,比如注册字符设备驱动: led_drv.c
② 下层实现硬件相关的操作,比如 board_A.c 实现单板 A 的 LED 操作

3 分离
还能不能改进? 分离
在 board_A.c 中,实现了一个 led_operations,为 LED 引脚实现了初始化函数、控制函数:
static struct led_operations board_demo_led_opr = {.num = 1,.init = board_demo_led_init,.ctl = board_demo_led_ctl,
};
如果硬件上更换一个引脚来控制 LED 怎么办?你要去修改上面结构体中的init、 ctl 函数。
实际情况是,每一款芯片它的 GPIO 操作都是类似的。比如: GPIO1_3、GPIO5_4 这 2 个引脚接到 LED:
① GPIO1_3 属于第 1 组,即 GPIO1。
a) 有方向寄存器 DIR、数据寄存器DR等,基础地址是addr_base_addr_gpio1。
b) 设置为output引脚:修改GPIO1的DIR寄存器的bit3。
c) 设置输出电平:修改GPIO1的DR寄存器的 bit3。
② GPIO5_4 属于第 5 组,即 GPIO5。
a) 有方向寄存器 DIR、数据寄存器DR等,基础地址是addr_base_addr_gpio5。
b) 设置为 output 引脚:修改GPIO5的DIR寄存器的bit4
c) 设置输出电平:修改GPIO5 的DR寄存器的 bit4。
既然引脚操作那么有规律,并且这是跟主芯片相关的,那可以针对该芯片写出比较通用的硬件操作代码。
比如 board_A.c 使用芯片 chipY,那就可以写出: chipY_gpio.c,它实现芯片 Y 的 GPIO 操作,适用于芯片 Y 的所有 GPIO 引脚。
使用时,我们只需要在 board_A_led.c 中指定使用哪一个引脚即可。程序结构如下:

以面向对象的思想,在 board_A_led.c 中实现 led_resouce 结构体,它定义“资源”──要用哪一个引脚。
在 chipY_gpio.c 中仍是实现 led_operations 结构体,它要写得更完善,支持所有 GPIO。
上下分层,左右分离
4 实例代码
board_A_led.c
#include "led_resource.h"// gpio3_1
// 3(11)左移16位 -- 11 0000 0000 0000 0000
static struct led_resource board_A_led = {.pin = GROUP_PIN(3, 1),
};// 返回指针
struct led_resource* get_led_resource(void)
{return &board_A_led;
}
chip_demo_gpio.c
#include <linux/gfp.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include "led_resource.h"
#include "led_operations.h"static struct led_resource* p_led_reource;// init函数
static int board_demo_led_init(int which)
{printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);if (!p_led_reource){p_led_reource = get_led_resource();}printk("init gpio: group: %d, pin: %d\n", GROUP(p_led_reource->pin), PIN(p_led_reource->pin));switch(GROUP(p_led_reource->pin)){case 0:{printk("init pin of group 0...\n");break;}case 1:{printk("init pin of group 1...\n");break;}case 2:{printk("init pin of group 2...\n");break;}case 3:{printk("init pin of group 3...\n");break;}}return 0;
}// ctl函数
static int board_demo_led_ctl(int which, char status)
{printk("set led: %s: group: %d, pin: %d\n", status ? "on" : "off",GROUP(p_led_reource->pin), PIN(p_led_reource->pin));switch(GROUP(p_led_reource->pin)){case 0:{printk("init pin of group 0...\n");break;}case 1:{printk("init pin of group 1...\n");break;}case 2:{printk("init pin of group 2...\n");break;}case 3:{printk("init pin of group 3...\n");break;}}return 0;
}static struct led_operations board_demo_led_operations = {.init = board_demo_led_init,.ctl = board_demo_led_ctl,
};// 返回结构体
struct led_operations* get_board_led_operations(void)
{return &board_demo_led_operations;
}
led_resource.h
#ifndef LED_RESOURCE_H
#define LED_RESOURCE_H// gpio3_0
// bit[31:16] = group
// bit[15:0] = which pin
#define GROUP(x) ((x) >> 16)
#define PIN(x) ((x) & 0xFFFF)
#define GROUP_PIN(g, p) (((g) << 16) | (p))struct led_resource {int pin;
};// 声明函数
struct led_resource* get_led_resource(void);#endif
led_operations.h
#ifndef LED_OPERATIONS_H
#define LED_OPERATIONS_Hstruct led_operations {int (*init) (int which); // 初始化LED,which是哪一个LEDint (*ctl) (int which, char status); // 控制LED,which-哪一个LED,status-1亮,0灭
};// 返回结构体指针
struct led_operations* get_board_led_operations(void);#endif
led_drv.c
/*************************************************************************> File Name: led.drv.c> Author: Winter> Created Time: Sun 07 Jul 2024 12:35:19 AM EDT************************************************************************/#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_operations.h"#define LED_NUM 2// 1确定主设备号,也可以让内核分配
static int major = 0; // 让内核分配
static struct class *led_class;
struct led_operations* p_led_operations;#define MIN(a, b) (a < b ? a : b)// 3 实现对应的 drv_open/drv_read/drv_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* node;int minor;printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);// 把用户区的数据buf拷贝到内核区status,即向写到内核status中写数据err = copy_from_user(&status, buf, 1);// 根据次设备号和status控制LEDnode = file_inode(file);minor = iminor(node);p_led_operations->ctl(minor, status);return 1;
}static int led_drv_open (struct inode *node, struct file *file)
{int minor;printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);// 得到次设备号minor = iminor(node);// 根据次设备号初始化LEDp_led_operations->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 结构体告诉内核: register_chrdev
// 5谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数
static int __init led_init(void)
{int err, i; printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);// 注册led_drv,返回主设备号major = register_chrdev(0, "winter_led", &led_drv); /* /dev/led */// 创建classled_class = class_create(THIS_MODULE, "led_class");err = PTR_ERR(led_class);if (IS_ERR(led_class)) {printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);unregister_chrdev(major, "led_class");return -1;}// 创建device// 根据次设备号访问多个LED
// device_create(led_class, NULL, MKDEV(major, 0), NULL, "winter_led0"); /* /dev/winter_led0 */
// device_create(led_class, NULL, MKDEV(major, 1), NULL, "winter_led1"); /* /dev/winter_led1 */for (i = 0; i < LED_NUM; i++){device_create(led_class, NULL, MKDEV(major, i), NULL, "winter_led%d", i);}// 入口函数获得结构体指针p_led_operations = get_board_led_operations();return 0;
}// 6有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdev
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));}device_destroy(led_class, MKDEV(major, 0));class_destroy(led_class);// 卸载unregister_chrdev(major, "winter_led");
}// 7其他完善:提供设备信息,自动创建设备节点: class_create,device_create
module_init(led_init);
module_exit(led_exit);MODULE_LICENSE("GPL");
led_drv_test.c
/*************************************************************************> File Name: hello_test.c> Author: Winter> Created Time: Sun 07 Jul 2024 01:39:39 AM EDT************************************************************************/#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>/** ./led_drv /dev/winter_led0 on* ./led_drv /dev/winter_led0 off*/
int main(int argc, char **argv)
{int fd;char status;/* 1. 判断参数 */if (argc < 2) {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;}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_stm32mp157_pro-sdk/Linux-5.4all:make -C $(KERN_DIR) M=`pwd` modules $(CROSS_COMPILE)gcc -o led_drv_test led_drv_test.c clean:make -C $(KERN_DIR) M=`pwd` modules cleanrm -rf modules.orderrm -f led_drv_test# 参考内核源码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
winter_led-y := led_drv.o chip_demo_gpio.o board_A_led.o
obj-m += winter_led.o
编译

5 测试
在开发板挂载 Ubuntu 的NFS目录
mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs/ /mnt
将ko文件和测试代码拷贝到挂载目录,安装驱动
insmod winter_led.ko

执行测试程序
./led_drv_test /dev/winter_led0 on
./led_drv_test /dev/winter_led0 off

相关文章:
韦东山嵌入式linux系列-驱动设计的思想(面向对象/分层/分离)
1 面向对象 字符设备驱动程序抽象出一个 file_operations 结构体; 我们写的程序针对硬件部分抽象出 led_operations 结构体。 2 分层 上下分层,比如我们前面写的 LED 驱动程序就分为 2 层: ① 上层实现硬件无关的操作,比如注册…...
0/1背包
0/1背包 背包问题是DP最经典的类型之一,而0/1背包是最经典最基础的背包问题。 背包体积为 V V V, n n n种物品,每种物品只有1个,第 i i i种物品对应体积为 c i c_i ci,价值为 w i w_i wi,怎样装填能使…...
Linux的进程和权限的基本命令
目录 基本命令 man find date cal du ln exit grep 基本命令-帮助查询: wc cat more less head tail echo alias unalias 基本命令-进程管理: ps kill top 操作系统负载查看 用户分类: 程序用户 普通用户&#x…...
鼠标录制工具怎么挑选?9款电脑鼠标录制工具分享(2024)
你知道鼠标录制工具吗?鼠标录制工具通过记录和回放用户的操作,帮助自动化重复性任务,提高工作效率和精确性。它可以帮助用户简化很多繁琐的操作步骤,非常适合运用在电脑自动化任务、游戏自动化中,给大家整理了2024年9款…...
C1W4.LAB.Vector manipulation+Hash functions and multiplanes
理论课:C1W4.Machine Translation and Document Search 文章目录 Python 中的矢量操作Transforming vectorsExample 1Example 2 Frobenius Norm Hash functions and multiplanesBasic Hash tablesPlanesHash Function with multiple planesRandom PlanesDocument v…...
YOLOv8改进 | 检测头 | 融合渐进特征金字塔的检测头【AFPN4】
秋招面试专栏推荐 :深度学习算法工程师面试问题总结【百面算法工程师】——点击即可跳转 💡💡💡本专栏所有程序均经过测试,可成功执行💡💡💡 专栏目录 :《YOLOv8改进有效…...
数据采集监控平台:挖掘数据价值 高效高速生产!
在当今数字化的时代,数据已成为企业非常宝贵的资产之一。然而,要充分发挥数据的潜力,离不开一个强大的数据采集监控平台,尤其是生产制造行业。它不仅是数据的收集者,更是洞察生产的智慧之眼,高效高速处理产…...
【算法笔记自学】第 9 章 提高篇(3)——数据结构专题(2)
9.1树与二叉树 #include <cstdio>int main() {int n, m;scanf("%d%d", &n, &m);printf(n m 1 ? "Yes" : "No");return 0; } 9.2二叉树的遍历 #include <cstdio> #include <vector> using namespace std;const int…...
Objective-C 中字符串的保存位置
在 Objective-C 中,字符串常量和动态创建的字符串(例如通过 stringWithFormat:、initWithString: 等方法创建的字符串)在内存中保存的位置一样么 ? 在 Objective-C 中,字符串常量和动态创建的字符串在内存中的保存位置…...
git 想要创建一个新的本地分支并检出远程分支的内容
如果你想要创建一个新的本地分支并检出远程分支的内容: git checkout -b feature-branch origin/feature-branch feature-branch 是你在本地创建的新分支名,origin/feature-branch 是远程分支的引用。 根据你检出的远程分支的名字而定 不知道名称的时…...
C语言学习笔记[24]:循环语句while②
getchar()的使用场景 int main() {char password[20] {0};printf("请输入密码:");//输入 123456 后回车scanf("%s", passwoed);//数组名本身就是数组地址printf("请确认密码:Y/N");int ch getchar();if(Y ch)printf(&…...
安全运营概述
安全运营概述 概述安全运营的工作对内安全运营工作对外安全运营工作品牌建设 概述 安全是一个过程,安全是靠运营出来的,公司会不断的有新业务的变更,新产品的发布,新版本的升级,技术架构的升级,底层系统的…...
spring-cloud和spring-cloud-alibaba的关系
首先Spring Cloud 是什么? Spring Cloud是一系列框架的有序集合,它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发。Spring Cloud提供了微服务架构开发所需的多种组件和工具,如服务发现注册、配置中心、消息总线、负载均…...
持续集成06--Jenkins构建触发器
前言 在持续集成(CI)的实践中,构建触发器是自动化流程中不可或缺的一环。它决定了何时启动构建过程,从而确保代码变更能够及时地得到验证和反馈。Jenkins,作为业界领先的CI/CD工具,提供了多种构建触发器选项…...
根据视图矩阵, 恢复相机的世界空间的位置
根据视图矩阵, 恢复相机的世界空间的位置 一、方法1 glsl 实现: // 从本地局部坐标系(相机空间) 到 世界空间的旋转变换 mat3 getLocal2WorldRotation() {mat3 world2localRotation mat3(viewMatrix[0].xyz,viewMatrix[1].xyz,viewMatrix[2].xyz);return inverse(world2loca…...
使用pytest-playwright截图和录制视频并添加到Allure报告
一、依赖环境 python, version==3.9.5 pytest-playwright, version==0.5.1 allure-pytest, version==2.13.5 pytest, version==6.2.5 allure, version==2.22.0pytest-playwright支持如下命令行参数: Playwright:--browser={chromium,firefox,webkit}Browser engine which …...
pytorch GPU cuda 使用 报错 整理
GPU 使用、报错整理 1. 使用指定GPU(单卡)1.1 方法1:os.environ[CUDA_VISIBLE_DEVICES]1.2 方法2:torch.device(cuda:2)1.3 报错1:RuntimeError: CUDA error: invalid device ordinal CUDA kernel errors might be asy…...
python + Pytest + requests 的接口自动化步骤
pythonpytestrequestallureyaml接口自动化测试项目实战 开发环境准备 1. jdk 下载 Java官网下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 安装: https://blog.csdn.net/VA_AV/article/details/138…...
基于若依的ruoyi-nbcio流程管理系统修正自定义业务表单的回写bug
更多ruoyi-nbcio功能请看演示系统 gitee源代码地址 前后端代码: https://gitee.com/nbacheng/ruoyi-nbcio 演示地址:RuoYi-Nbcio后台管理系统 http://218.75.87.38:9666/ 更多nbcio-boot功能请看演示系统 gitee源代码地址 后端代码: h…...
GD32 MCU上电跌落导致启动异常如何解决
大家是否碰到过MCU上电过程中存在电源波动或者电压跌落导致MCU启动异常的问题?本视频将会为大家讲解可能的原因以及解决方法: GD32 MCU上下电复位波形如下图所示,上电过程中如果存在吃电的模块,比如wifi模块/4G模块/开启某块电路…...
stm32G473的flash模式是单bank还是双bank?
今天突然有人stm32G473的flash模式是单bank还是双bank?由于时间太久,我真忘记了。搜搜发现,还真有人和我一样。见下面的链接:https://shequ.stmicroelectronics.cn/forum.php?modviewthread&tid644563 根据STM32G4系列参考手…...
理解 MCP 工作流:使用 Ollama 和 LangChain 构建本地 MCP 客户端
🌟 什么是 MCP? 模型控制协议 (MCP) 是一种创新的协议,旨在无缝连接 AI 模型与应用程序。 MCP 是一个开源协议,它标准化了我们的 LLM 应用程序连接所需工具和数据源并与之协作的方式。 可以把它想象成你的 AI 模型 和想要使用它…...
【git】把本地更改提交远程新分支feature_g
创建并切换新分支 git checkout -b feature_g 添加并提交更改 git add . git commit -m “实现图片上传功能” 推送到远程 git push -u origin feature_g...
Spring AI 入门:Java 开发者的生成式 AI 实践之路
一、Spring AI 简介 在人工智能技术快速迭代的今天,Spring AI 作为 Spring 生态系统的新生力量,正在成为 Java 开发者拥抱生成式 AI 的最佳选择。该框架通过模块化设计实现了与主流 AI 服务(如 OpenAI、Anthropic)的无缝对接&…...
Map相关知识
数据结构 二叉树 二叉树,顾名思义,每个节点最多有两个“叉”,也就是两个子节点,分别是左子 节点和右子节点。不过,二叉树并不要求每个节点都有两个子节点,有的节点只 有左子节点,有的节点只有…...
Device Mapper 机制
Device Mapper 机制详解 Device Mapper(简称 DM)是 Linux 内核中的一套通用块设备映射框架,为 LVM、加密磁盘、RAID 等提供底层支持。本文将详细介绍 Device Mapper 的原理、实现、内核配置、常用工具、操作测试流程,并配以详细的…...
SiFli 52把Imagie图片,Font字体资源放在指定位置,编译成指定img.bin和font.bin的问题
分区配置 (ptab.json) img 属性介绍: img 属性指定分区存放的 image 名称,指定的 image 名称必须是当前工程生成的 binary 。 如果 binary 有多个文件,则以 proj_name:binary_name 格式指定文件名, proj_name 为工程 名&…...
Go 语言并发编程基础:无缓冲与有缓冲通道
在上一章节中,我们了解了 Channel 的基本用法。本章将重点分析 Go 中通道的两种类型 —— 无缓冲通道与有缓冲通道,它们在并发编程中各具特点和应用场景。 一、通道的基本分类 类型定义形式特点无缓冲通道make(chan T)发送和接收都必须准备好࿰…...
【Post-process】【VBA】ETABS VBA FrameObj.GetNameList and write to EXCEL
ETABS API实战:导出框架元素数据到Excel 在结构工程师的日常工作中,经常需要从ETABS模型中提取框架元素信息进行后续分析。手动复制粘贴不仅耗时,还容易出错。今天我们来用简单的VBA代码实现自动化导出。 🎯 我们要实现什么? 一键点击,就能将ETABS中所有框架元素的基…...
React父子组件通信:Props怎么用?如何从父组件向子组件传递数据?
系列回顾: 在上一篇《React核心概念:State是什么?》中,我们学习了如何使用useState让一个组件拥有自己的内部数据(State),并通过一个计数器案例,实现了组件的自我更新。这很棒&#…...
