2.5. 添加驱动

2.5.1. 添加外设

本文以 GT911 电容屏驱动添加为例,介绍外设驱动添加方法。

2.5.1.1. 获取源码

GT911 的 RT-Thread 驱动源码位于:https://github.com/RiceChen/gt911 下载后,将源码解压至 bsp/peripheral/touch 目录下,源码结构如下:

touch$ tree
.
├── gt911
│   ├── inc
│   │   └── gt911.h
│   ├── Kconfig
│   └── src
│       └── gt911.c
└── SConscript

2.5.1.2. SConscript 修改

bsp/peripheral/touch/SConscript 文件内容如下:

Import('AIC_ROOT')
Import('PRJ_KERNEL')
from building import *

cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = []

if GetDepend('AIC_TOUCH_PANEL_GT911'):
    CPPPATH.append(cwd + '/gt911/inc')
    src += Glob('gt911/src/*.c')

group = DefineGroup('gt911', src, depend = [''], CPPPATH = CPPPATH)

Return('group')

如果后续添加其他 touch 的驱动,可在此文件中,以 GT911 为参考模板添加。

2.5.1.3. pinmux 修改

每个板卡的 pinmux.c 都定义了各个端口的 GPIO 引脚及功能,新增接口前应检查。 根据原理图:

  • GT911 驱动使用的是 I2C3 接口;

  • 除了 I2C 的 SCK 和 SDA引脚,还需要用到 RST 和 INT 引脚;

因此,需要在 target/<cpu>/<board>/pinmux.c 中,添加如下内容:

#ifdef AIC_USING_I2C3
    {1, PIN_PULL_DIS, 3, "PA.8"},  // RST
    {1, PIN_PULL_DIS, 3, "PA.9"},  // INT
    {4, PIN_PULL_DIS, 3, "PA.10"}, // SCK
    {4, PIN_PULL_DIS, 3, "PA.11"}, // SDA
#endif

2.5.1.4. Kconfig 配置

2.5.1.4.1. 源码 Kconfig

驱动源码的 Kconfig bsp/peripheral/touch/gt911/Kconfig 内容如下:

gt911$ cat Kconfig

menu "Gt911 touch panel options"

config AIC_TOUCH_PANEL_GT911
    bool "Using touch panel gt911"
    default n
    select AIC_I2C_DRV

endmenu

2.5.1.4.2. 外设驱动 Kconfig

bsp/peripheral/Kconfig 中,添加配置驱动源码的 Kconfig 路径

...
#--------------------------------------------
# touch panel driver global option
#--------------------------------------------

menu "Touch Panel Support"
source "bsp/peripheral/touch/gt911/Kconfig"
endmenu

...

2.5.1.6. 编译

完成驱动添加和配置后,对 SDK 进行编译,使用命令 scons --menuconfigm ( OneStep 命令 )

2.5.1.7. 验证

烧写镜像,系统启动之后,通过 list_device 命令, 查看设备节点是否已成功枚举,如下所示:

aic /> list_device
device           type         ref count
-------- -------------------- ----------
...
gt911    Touch Device         1
...

看到 gt911 设备,表示已成功添加该设备。