Skip to content

Commit 87d05ab

Browse files
feat: key drivers and optimize the namings (#53)
1 parent c8b99dd commit 87d05ab

118 files changed

Lines changed: 9060 additions & 7 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

document/tutorial/17.output

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
~ # home/key_demo gpio
2+
正在监控按键事件 (GPIO轮询(无消抖) 模式)
3+
设备: /dev/imxaes_ke[ 65.865833] key_open: device opened
4+
y
5+
按 Ctrl+C 退出
6+
----------------------------------------
7+
[000001] [830101563000 ns] 按键: 按下
8+
[000002] [830252029000 ns] 按键: 释放
9+
[000003] [830945189000 ns] 按键: 按下
10+
[000004] [831193486000 ns] 按键: 释放
11+
[000005] [831684800000 ns] 按键: 按下
12+
[000006] [832027980000 ns] 按键: 释放
13+
[000007] [832505140000 ns] 按键: 按下
14+
[000008] [832897211000 ns] 按键: 释放
15+
[000009] [833494241000 ns] 按键: 按下
16+
[000010] [833717739000 ns] 按键: 释放
17+
[000011] [834098115000 ns] 按键: 按下
18+
[000012] [834250662000 ns] 按键: 释放
19+
[000013] [834694503000 ns] 按键: 按下
20+
[000014] [834882351000 ns] 按键: 释放
21+
[000015] [835245839000 ns] 按键: 按下
22+
[000016] [836477746000 ns] 按键: 释放
23+
[000017] [836478088000 ns] 按键: 释放
24+
[000018] [836478214000 ns] 按键: 释放
25+
[000019] [837889397000 ns] 按键: 按下
26+
[000020] [838676903000 ns] 按键: 释放
27+
[000021] [839514856000 ns] 按键: 按下
28+
[000022] [840103740000 ns] 按键: 释放
29+
[000023] [840103871000 ns] 按键: 释放
30+
[000024] [840104051000 ns] 按键: 按下
31+
[000025] [840104108000 ns] 按键: 释放
32+
[000026] [840104180000 ns] 按键: 释放

document/tutorial/18.output

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
~ # home/key_demo debounce
2+
正在监控按键事件 (中断噠 34.750879] key_open: device opened
3+
¶抖(20ms) 模式)
4+
设备: /dev/imxaes_key_debounce
5+
按 Ctrl+C 退出
6+
----------------------------------------
7+
[000001] [1011893639000 ns] 按键: 按下
8+
[000002] [1012063432000 ns] 按键: 释放
9+
[000003] [1012563404000 ns] 按键: 按下
10+
[000004] [1012691287000 ns] 按键: 释放
11+
[000005] [1013253539000 ns] 按键: 按下
12+
[000006] [1013863432000 ns] 按键: 释放
13+
[000007] [1014433555000 ns] 按键: 按下
14+
[000008] [1014883433000 ns] 按键: 释放
15+
[000009] [1015403547000 ns] 按键: 按下
16+
[000010] [1015813431000 ns] 按键: 释放
17+
[000011] [1016273421000 ns] 按键: 按下
18+
[000012] [1016653439000 ns] 按键: 释放
19+
[000013] [1017173540000 ns] 按键: 按下
20+
[000014] [1017563493000 ns] 按键: 释放
21+
[000015] [1018073555000 ns] 按键: 按下
22+
[000016] [1018413403000 ns] 按键: 释放
23+
[000017] [1018983552000 ns] 按键: 按下
24+
[000018] [1019343424000 ns] 按键: 释放
25+
[000019] [1019813414000 ns] 按键: 按下
26+
[000020] [1020253415000 ns] 按键: 释放
27+
[000021] [1020713503000 ns] 按键: 按下
28+
[000022] [1021073416000 ns] 按键: 释放
29+
[000023] [1021553504000 ns] 按键: 按下
30+
[000024] [1021823431000 ns] 按键: 释放
31+
[000025] [1022343557000 ns] 按键: 按下
32+
[000026] [1022573416000 ns] 按键: 释放

document/tutorial/19.output

Whitespace-only changes.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Platform LED 驱动 - 架构概览
2+
3+
## 前言:为什么这是第一个实战驱动
4+
5+
在之前的字符设备基础教程里,我们已经学会了如何写一个最小化的字符设备驱动——注册设备号、实现 `file_operations`、创建设备节点。但说实话,那些都是"硬编码"的驱动,设备和驱动的绑定关系不清晰,代码结构也比较粗糙。
6+
7+
现在我们要进入真实的嵌入式驱动开发世界。这个教程会带你完成一个**生产级别**的 Platform LED 驱动,完整展示从框架设计到代码实现的全过程。
8+
9+
::: tip 学习目标
10+
掌握 Platform 驱动框架的完整开发流程:从设备树匹配到 probe/remove 函数,从 HAL 层设计到用户接口实现。学会用 `devm_gpiod_get()` 等 GPIO Descriptor API 操作硬件,理解设备树和驱动的严格对齐关系。
11+
:::
12+
13+
## 教程结构
14+
15+
本教程分为六个章节:
16+
17+
### 第一阶段:框架理解
18+
19+
1. **[02_platform_framework](02_platform_framework)** - Platform 驱动框架详解
20+
- Platform 总线的工作原理
21+
- platform_driver 结构体解析
22+
- probe/remove 函数的工作流程
23+
24+
2. **[03_hal_layer](03_hal_layer)** - HAL 层设计思想
25+
- 为什么需要硬件抽象层
26+
- HAL 接口设计
27+
- GPIO Descriptor API 使用
28+
29+
### 第二阶段:实现与集成
30+
31+
3. **[04_driver_layer](04_driver_layer)** - 驱动层实现
32+
- 设备结构体设计
33+
- file_operations 实现
34+
- 驱动层与 HAL 层的协作
35+
36+
4. **[05_device_tree](05_device_tree)** - 设备树配置
37+
- 设备树语法
38+
- compatible 属性匹配
39+
- GPIO 配置与极性
40+
41+
### 第三阶段:实战验证
42+
43+
5. **[06_build_and_test](06_build_and_test)** - 编译测试
44+
- 驱动编译
45+
- 设备树编译
46+
- 功能测试与调试
47+
48+
## 硬件抽象层(HAL)设计思想
49+
50+
LED 驱动的代码被分成了两个文件:`platform_led_13_driver_main.c` 是驱动层,`led_hw.c``led_hw.h` 是硬件抽象层。为什么要这样拆分?
51+
52+
假设你的系统里有多个 LED:一个是板载的电源指示灯,一个是用户可编程的状态灯,还有一个通过 GPIO 扩展芯片控制的灯。这三个 LED 的控制方式可能不同:第一个直接用 GPIO 控制,第二个也是 GPIO 但有特殊初始化要求,第三个需要通过 I2C 扩展芯片。如果在每个地方都写一遍 GPIO 操作代码,会有大量重复。
53+
54+
HAL 的思路是:**定义一套统一的接口,把硬件相关的细节封装起来**
55+
56+
```c
57+
int led_hw_init(struct device *dev, struct led_hw_ctx *ctx);
58+
void led_hw_deinit(struct led_hw_ctx *ctx);
59+
void led_set_status(struct led_hw_ctx *ctx, bool status);
60+
bool led_get_status(struct led_hw_ctx *ctx);
61+
```
62+
63+
驱动层只调用这些接口,不需要知道底层是 GPIO 还是 I2C 扩展芯片。如果以后要支持 PWM 调光,只需要修改 `led_hw.c` 的实现,驱动层的代码完全不用动。
64+
65+
::: tip HAL 设计的价值
66+
HAL 层的核心价值在于"隔离变化"。硬件变了,改 HAL 层;接口变了,才需要改驱动层。大部分硬件改动都发生在 HAL 层,驱动代码可以保持稳定。
67+
:::
68+
69+
## 代码结构
70+
71+
```
72+
driver/16_tutorial_platform_led/
73+
├── platform_led_13_driver_main.c # 驱动层
74+
│ ├── platform_driver 定义
75+
│ ├── probe/remove 函数
76+
│ └── file_operations 实现
77+
78+
└── led_hw.c / led_hw.h # HAL 层
79+
├── GPIO 获取/释放
80+
└── LED 状态控制
81+
```
82+
83+
## 小结
84+
85+
本节介绍了教程的整体结构和 HAL 设计思想。接下来我们将深入 Platform 框架,理解设备和驱动是如何匹配的。
86+
87+
---
88+
89+
<ChapterNav variant="sub">
90+
<ChapterLink href="index.md" variant="sub">← 返回目录</ChapterLink>
91+
<ChapterLink href="02_platform_framework.md" variant="sub">Platform 驱动框架 →</ChapterLink>
92+
</ChapterNav>

0 commit comments

Comments
 (0)