文章目录
- 系列文章
- 一、前言
- 二、准备工作
- 2.1 内核版本
- 2.2 内核文档:bindings->leds
- 2.3 文档解析: leds-gpio.txt
- 三、编写DTS
- 3.1 查原理图,挑选GPIO
- 3.2 编写DTS文件
- 四、编译测试
- 4.1 编译dt.img
- 4.2 烧录dt.img
- 五、基于fs的测试
- 5.1 测试命令
- 5.2 **点灯效果**
- 六、C语言:编写NDK测试APP
- 6.1 创建文件和目录
- 6.2 Android.mk
- 6.3 test-led.c
- 6.4 编译
- 6.5 执行 test-led
- 七、结束语
系列文章
第1篇 :不写一行代码(一):实现安卓基于GPIO的LED设备驱动
第2篇 :不写一行代码(二):实现安卓基于PWM的LED设备驱动
第3篇:不写一行代码(三):实现安卓基于i2c bus的Slaver设备驱动
一、前言
安卓设备驱动,本质上依旧还是Linux架构的驱动程序,基于Linux Kernel。在做安卓ROM开发的过程中,我们经常要控制设备的LED灯,许多情况下,我们直接就去写了一个LED的字符设备驱动,却不知,这类驱动,在kernel driver大神手下,早就给我们写了通用版本,但凡我们多看它一眼,就可以站在巨人的肩膀上,不写一行代码……,但工资还是要领……
二、准备工作
2.1 内核版本
在板子上执行如下命令,可获知当前设备的内核版本为4.9.113; 这是目前Android 9.0普遍使用的一个版本。另一方面,从2.6版本开始,kernel就已经支持Device Tree(设备树),所以,接下来的工作,重点其实就是设备树文件的书写!
:/ # cat /proc/version
Linux version 4.9.113 (root@d185403d1e6f) (gcc version 6.3.1 20170109 (Linaro GCC 6.3-2017.02) ) #24 SMP PREEMPT Tue Dec 20 15:42:34 UTC 2022
:/ #
2.2 内核文档:bindings->leds
- android9.0_aosp/common 目录,即为我所使用的Amlogic T972平台的安卓内核源代码目录
- 在下图common\drivers\leds 目录中,leds-gpio.txt就是通用GPIO LED驱动的bindings文档,基于GPIO实现
- 同时,我们可以看到leds-pwm.txt,这是PWM实现的另一个版本。
- GPIO LED 版本,对于亮度的控制,只0和1(非0)两个逻辑值,0代表灭灯,1代表开灯;而PWM版本,则可以实现最大256级的亮度控制。
2.3 文档解析: leds-gpio.txt
LEDs connected to GPIO lines
翻译:LED灯需要物理连接到对应的GPIO口上
Required properties:
- compatible : should be "gpio-leds".
翻译:LED设备树节点的 “compatible ”属性,必须是 "gpio-leds"
Each LED is represented as a sub-node of the gpio-leds device. Each
node's name represents the name of the corresponding LED.
翻译:每个LED均为gpio-leds的子节点,这些子节点的name就代表了LED设备的名字
LED sub-node properties:
翻译:在"gpio-leds"节点下面的LED子节点属性说明
- gpios : Should specify the LED's GPIO, see "gpios property" in
Documentation/devicetree/bindings/gpio/gpio.txt. Active low LEDs should be
indicated using flags in the GPIO specifier.
翻译:gpios 用于指定LED所使用的GPIO
备注:(optional)可选项在这里就不做介绍了,有兴趣的自行查阅一下文档
- label : (optional) //如设定,将替代led节点的name属性
see Documentation/devicetree/bindings/leds/common.txt
- linux,default-trigger : (optional)
see Documentation/devicetree/bindings/leds/common.txt
翻译:用于设定LED的初始值
- default-state: (optional) The initial state of the LED.
see Documentation/devicetree/bindings/leds/common.txt
翻译:展开leds/common.txt 关键部分
- default-state : The initial state of the LED. Valid values are "on", "off",
and "keep". If the LED is already on or off and the default-state property is
set the to same value, then no glitch should be produced where the LED
momentarily turns off (or on). The "keep" setting will keep the LED at
whatever its current state is, without producing a glitch. The default is
off if this property is not present.
说明:
(1) default-state 有3个有效值: "on", "off", "keep",用于设定LED的初始值。
(2) 设定为"keep"时,LED GPIO 将保持原状,不做任何处理
(3) LED GPIO的初始状态应保持和default-state值含义一致,以免出现闪烁(glitch )
- retain-state-suspended: (optional) The suspend state can be retained.Such
as charge-led gpio.
- panic-indicator : (optional)
see Documentation/devicetree/bindings/leds/common.txt
例子
Examples:
#include <dt-bindings/gpio/gpio.h>
翻译:在此头文件中,包含了如下两个宏的定义
/* Bit 0 express polarity */
// #define GPIO_ACTIVE_HIGH 0 , 代表GPIO物理线路给高电平时会点亮,即高电平有效
// #define GPIO_ACTIVE_LOW 1 , 即低电平有效
leds {
compatible = "gpio-leds"; //强制要求,要用大神的GPIO LED驱动,暗号得一样
hdd { //led设备名称为hdd,但因为label出现,变成了"Disk Activity"
label = "Disk Activity";
gpios = <&mcu_pio 0 GPIO_ACTIVE_LOW>; //使用mcu_pio控制器下的GPIO 0,且设定低电平有效
linux,default-trigger = "disk-activity";
};
fault {
gpios = <&mcu_pio 1 GPIO_ACTIVE_HIGH>; //使用mcu_pio控制器下的GPIO 1,且设定高电平有效
/* Keep LED on if BIOS detected hardware fault */
default-state = "keep";//将沿用GPIO上电后的状态,不设置初始值
};
};
run-control { // LEDs设备目录名称为run-control
compatible = "gpio-leds";
red { // LEDs子设备名为 red
gpios = <&mpc8572 6 GPIO_ACTIVE_HIGH>;
default-state = "off"; //设定默认关,此PIN为高电平有效,所以开机后会将GPIO 6拉低
};
green {
gpios = <&mpc8572 7 GPIO_ACTIVE_HIGH>;
default-state = "on";
};
};
leds {
compatible = "gpio-leds";
charger-led {
gpios = <&gpio1 2 GPIO_ACTIVE_HIGH>;
linux,default-trigger = "max8903-charger-charging";
retain-state-suspended;
};
};
三、编写DTS
3.1 查原理图,挑选GPIO
- 如下挑选GPIOZ_1作为实验对象, 同时,我们还得选一个GND,可以挑选PIN-48
接线方法如下,因为没有LED灯,我这里用一个普通小灯泡代替,如果是LED灯,注意二极管PN结的方向,别接反了
图一:原理图
图二:接线图
3.2 编写DTS文件
/ {
//…… 前 略 ……
//
leds {
status = "okay"; //设备OK的,或disabled禁用
compatible = "gpio-leds"; //大神的要求,固定
led2{ //设备名称为led2
gpios = <&gpio GPIOZ_1 GPIO_ACTIVE_HIGH>; //使用gpio 控制的GPIOZ_1 ,高电平有效
default-state = "off"; //默认状态是灭灯
};
};
//
//…… 后 略 ……
}
四、编译测试
4.1 编译dt.img
$ source build/envsetup.sh
$ lunch your-board
//1. 如果未做过完整编译,则直接全编译
$ make -j32
//2. 如果已做过完成编译,则可以使用局部编译
$ make dtbimage -j32
编译打印如下:
[100% 1/1] Instaled out/target/product/x301/dt.img
make: Entering directory `/home/builder/android_x301/source/t962x3-t972-android9.0/common'
make[1]: Entering directory `/home/builder/android_x301/source/t962x3-t972-android9.0/out/target/product/x301/obj/KERNEL_OBJ'
CHK scripts/mod/devicetable-offsets.h
DTC arch/arm/boot/dts/amlogic/pro-box-t972.dtb
make[1]: Leaving directory `/home/builder/android_x301/source/t962x3-t972-android9.0/out/target/product/x301/obj/KERNEL_OBJ'
make: Leaving directory `/home/builder/android_x301/source/t962x3-t972-android9.0/common'
Generate Partition Table Xml From common/arch/arm/boot/dts/amlogic//partition_mbox_normal_P_32.dtsi to out/target/product/x301/emmc_burn.xml
part-1 logo 0x800000
part-2 recovery 0x1800000
part-3 misc 0x800000
part-4 dtbo 0x800000
part-5 cri_data 0x800000
part-6 param 0x1000000
part-7 boot 0x1000000
part-8 rsv 0x1000000
part-9 metadata 0x1000000
part-10 vbmeta 0x200000
part-11 tee 0x2000000
part-12 vendor 0x1C000000
part-13 odm 0x8000000
part-14 system 0x50000000
part-15 product 0x8000000
part-17 data 0xffffffff
Generate Partition Table Xml Sucess
#### build completed successfully (01:27 (mm:ss)) ####
Build dts ok!
root@d185403d1e6f:/home/builder/android_x301/source/t962x3-t972-android9.0#
[100% 1/1] Instaled out/target/product/x301/dt.img
编译完后,获取dt.img, 我的平台位于:out/target/product/x301/dt.img
4.2 烧录dt.img
单独分区dt.img的烧录方法,可以参考芯片原厂提供的方法,如下是Amlogic T972的烧录方法,也是安卓的标准方法。
(1)将out/target/product/x301/dt.img 赋值到Burning.bat工具的同级目录下
(2)使用USB双公头线,连接板子OTG USB口, 另一端连接PC USB口
(3)双击运行Burning.bat,选择5,烧录dt
(4)如下图所示,烧录后板子会自动重启
图一:烧录工具目录
烧录打印如下:
adb connecting...
List of devices attached
1234567890 device
****** Burning way by fastboot ******
1: boot ---- boot.img
2: logo ---- logo.img
3: recovery ---- recovery.img
4: system ---- system.img
5: dtb (dts) ---- dt.img
6: uboot (bootloader) ---- uboot.bin
7: vendor ---- vendor.img
8: odm ---- odm.img
9: vendorboot ---- vendor_boot.img
Which number you like?5
dts ---- dt.img
< waiting for any device >
FAILED (Device sent unknown status code: )
fastboot: error: Command failed
OKAY [ 0.045s]
Finished. Total time: 0.046s
fastboot flash dts dt.img
Sending 'dts' (101 KB) OKAY [ 0.021s]
Writing 'dts' OKAY [ 0.155s]
Finished. Total time: 0.297s
OKAY [ 0.043s]
Finished. Total time: 0.047s
OKAY [ 0.045s]
Finished. Total time: 0.047s
Rebooting OKAY [ 0.002s]
Finished. Total time: 0.004s
Press any key to exit...
图二、烧录图示
五、基于fs的测试
5.1 测试命令
//(1)连接USB OTG,板子ADB服务
Z:\>adb usb
restarting in USB mode
//(2)进入板子shell模式
Z:\>adb shell
//(3)通过SYS-FS文件系统,找到我们在dts中创建的设备,可见设备TOP目录名为父节点名leds,子节点名为设备led2
:/ # cd /sys/class/leds/
:/sys/class/leds # ls -al
total 0
drwxr-xr-x 2 root root 0 2022-12-24 11:02 .
drwxr-xr-x 131 root root 0 2022-12-24 11:02 ..
lrwxrwxrwx 1 root root 0 2022-12-24 11:05 led2 -> ../../devices/platform/leds/leds/led2
//(4)进入led2的目录
:/sys/class/leds # cd led2/
:/sys/class/leds/led2 # ls -al
total 0
drwxr-xr-x 3 root root 0 2022-12-24 11:02 .
drwxr-xr-x 3 root root 0 2022-12-24 11:02 ..
-rw-r--r-- 1 root root 4096 2022-12-24 11:06 brightness --> 这是设定亮度的属性,GPIO-LED只有0和1
lrwxrwxrwx 1 root root 0 2022-12-24 11:06 device -> ../../../leds
-r--r--r-- 1 root root 4096 2022-12-24 11:06 max_brightness --> 这是设定亮度的最大值,对于PWM-LED有效,最大255
drwxr-xr-x 2 root root 0 2022-12-24 11:02 power
lrwxrwxrwx 1 root root 0 2022-12-24 11:06 subsystem -> ../../../../../class/leds
-rw-r--r-- 1 root root 4096 2022-12-24 11:06 trigger
-rw-r--r-- 1 root root 4096 2022-12-24 11:02 uevent
//(5)查看亮度默认值,可见如我们的 default-state = "off"; //默认状态是灭灯
:/sys/class/leds/led2 # cat brightness
0
//(6)测试:点灯
:/sys/class/leds/led2 # echo 1 > brightness
//(7)测试:灭灯
:/sys/class/leds/led2 # echo 0 > brightness
5.2 点灯效果
六、C语言:编写NDK测试APP
6.1 创建文件和目录
(1) 找个安卓的子目录,例如 development
szhou@bc04:~$ cd ~/T972/android_x301/source/t962x3-t972-android9.0/development
(2) 创建一个目录,姑且就叫led
szhou@bc04:~/T972/android_x301/source/t962x3-t972-android9.0/development$ mkdir led
(2) 创建 Android.mk 和 test-led.c文件
szhou@bc04:~/T972/android_x301/source/t962x3-t972-android9.0/development$ tree led
led
├── Android.mk
└── test-led.c
0 directories, 2 files
6.2 Android.mk
- 编译后的模块名称为 test-led, 它是一个可执行文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test-led
LOCAL_SRC_FILES := test-led.c
include $(BUILD_EXECUTABLE)
6.3 test-led.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
//led2设备节点的路径,将设置属性brightness
#define LED_DEV_PATH "/sys/class/leds/led2/brightness"
int SetFsLed(char *onOff)
{
int fd;
int ret;
//通过fs系统调用,打开设备文件
fd = open(LED_DEV_PATH, O_WRONLY);
if (fd == -1) {
perror("fsled->open " );
return -1;
}
//通过fs系统调用,向设备文件写入具体的控制值,此处即操作文件 "/sys/class/leds/led2/brightness"
ret = write(fd, onOff, strlen(onOff));
if (ret == -1) {
perror("fsled->write");
close(fd);
return -1;
}
close(fd);
return 0;
}
int main()
{
while (1) {
SetFsLed("1\n");
usleep(500000); //即半秒开,半秒灭,循环闪烁
SetFsLed("0\n");
usleep(500000);
}
}
6.4 编译
- 需要先做一次完整的编译,才可使用局部编译命令
- 生成文件位置:[100% 6/6] Install: out/target/product/x301/system/bin/test-led
(1)准备环境
$ source build/envsetup.sh
$ lunch your-board
(2)局部编译 test-led 模块,源于Android.mk的 LOCAL_MODULE := test-led
$ make test-led
打印如下
root@d185403d1e6f:/home/builder/android_x301/source/t962x3-t972-android9.0# source build/envsetup.sh
// …… 略 ……
root@d185403d1e6f:/home/builder/android_x301/source/t962x3-t972-android9.0# lunch
You're building on Linux
Lunch menu... pick a combo:
1. aosp_arm-eng
// …… 略 ……
84. x301-userdebug
Which would you like? [aosp_arm-eng] 84
// ……略……
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=9
TARGET_PRODUCT=x301
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_ARCH=arm
TARGET_ARCH_VARIANT=armv7-a-neon
TARGET_CPU_VARIANT=cortex-a9
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-5.11.0-49-generic-x86_64-Ubuntu-14.04.6-LTS
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=PPR1.180610.011
OUT_DIR=out
============================================
// ……略……
build/make/core/Makefile:28: warning: overriding commands for target `out/target/product/x301/obj/lib_vendor/mxl661_fe.ko'
device/amlogic/x301/Kernel.mk:178: warning: ignoring old commands for target `out/target/product/x301/obj/lib_vendor/mxl661_fe.ko'
[100% 6/6] Install: out/target/product/x301/system/bin/test-led
#### build completed successfully (01:47 (mm:ss)) ####
root@d185403d1e6f:/home/builder/android_x301/source/t962x3-t972-android9.0#
生成文件位置:[100% 6/6] Install: out/target/product/x301/system/bin/test-led
6.5 执行 test-led
我这里已经将服务器SAMBA挂载到了PC的Z盘
6.5.1 操作命令
(1)切换到服务器的bin生成目录
c:\>cd Z:\T972\android_x301\source\t962x3-t972-android9.0\out\target\product\x301\system\bin
c:\>z:
(2)获取root权限
Z:\T972\android_x301\source\t962x3-t972-android9.0\out\target\product\x301\system\bin>adb root
restarting adbd as root
(3)推送文件到板子的/data目录下
Z:\T972\android_x301\source\t962x3-t972-android9.0\out\target\product\x301\system\bin>adb push test-led /data/
test-led: 1 file pushed, 0 skipped. 1.1 MB/s (16016 bytes in 0.014s)
(4)修改文件属性为可执行文件
Z:\T972\android_x301\source\t962x3-t972-android9.0\out\target\product\x301\system\bin>adb shell chmod 777 /data/test-led
(5)执行 ./data/test-led
Z:\T972\android_x301\source\t962x3-t972-android9.0\out\target\product\x301\system\bin>adb shell ./data/test-led
6.5.2 命令图示
执行后,灯泡将闪烁起来
图二:执行的命令界面
图二:点灯效果如下图 不过这次是闪烁的,0.5秒亮,0.5秒灭
七、结束语
内容虽然简单,但却得周末爆肝才能写完……