参考源码:x3399_nougat_industry
驱动路径:kernel/drivers/leds/leds-gpio
首先找到leds目录下的Makefile文件
obj-$(CONFIG_LEDS_GPIO) += leds-gpio.o
再找到Kconfig文件
config LEDS_GPIO
tristate "LED Support for GPIO connected LEDs"
depends on LEDS_CLASS
depends on GPIOLIB || COMPILE_TEST
help
This option enables support for the LEDs connected to GPIO
outputs. To be useful the particular board must have LEDs
and they must be connected to the GPIO lines. The LEDs must be
defined as platform devices and/or OpenFirmware platform devices.
The code to use these bindings can be selected below.
再找到配置文件x3399_defconfig:
#
# LED drivers
#
# CONFIG_LEDS_BCM6328 is not set
# CONFIG_LEDS_BCM6358 is not set
# CONFIG_LEDS_LM3530 is not set
# CONFIG_LEDS_LM3642 is not set
# CONFIG_LEDS_PCA9532 is not set
CONFIG_LEDS_GPIO=y
# CONFIG_LEDS_LP3944 is not set
# CONFIG_LEDS_LP5521 is not set
# CONFIG_LEDS_LP5523 is not set
为什么要选 x3399_defconfig配置而不是其他,是因为./mk.sh 脚本固定了的
#
# Target Config
#
BS_CONFIG_BOOTLOADER_UBOOT=x3399_defconfig
BS_CONFIG_KERNEL=x3399_defconfig
BS_CONFIG_KERNEL_DTB=x3399-development-board.img
BS_CONFIG_FILESYSTEM=PRODUCT-rk3399_all-userdebug
交叉编译工具链:找到kernel/Makefile下的文件,搜索CROSS_COMPILE即可找到以下定义。由于ARCH = arm64 支持64位
ARCH ?= arm64
ARCH ?= $(SUBARCH)
ifeq ($(ARCH),arm64)
ifneq ($(wildcard $(srctree)/../prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu),)
CROSS_COMPILE ?= $(srctree)/../prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
endif
ifneq ($(wildcard $(srctree)/../prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9),)
CROSS_COMPILE ?= $(srctree)/../prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/aarch64-linux-android-
endif
endif
ifeq ($(ARCH),arm)
ifneq ($(wildcard $(srctree)/../prebuilts/gcc/linux-x86/arm/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf),)
CROSS_COMPILE ?= $(srctree)/../prebuilts/gcc/linux-x86/arm/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
endif
ifneq ($(wildcard $(srctree)/../prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9),)
CROSS_COMPILE ?= $(srctree)/../prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9/bin/arm-linux-androidkernel-
endif
解读代码可以得知所用的工具链是以下这个:
CROSS_COMPILE ?= $(srctree)/../prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
简单验证:
zdong@ubuntu:~/rockchip/x3399_nougat_industry/kernel/drivers/leds$ mkdir leds
zdong@ubuntu:~/rockchip/x3399_nougat_industry/kernel/drivers/leds$ cp -rf *.c leds/
zdong@ubuntu:~/rockchip/x3399_nougat_industry/kernel/drivers/leds$ cd leds
zdong@ubuntu:~/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds$ ls
dell-led.c leds-asic3.c leds-cobalt-qube.c leds-gpio-register.c leds-lm355x.c leds-lp55xx-common.c leds-mc13783.c leds-pca955x.c leds-sead3.c leds-wm831x-status.c
led-class.c leds-att1272.c leds-cobalt-raq.c leds-hp6xx.c leds-lm3642.c leds-lp8501.c leds-menf21bmc.c leds-pca963x.c leds-ss4200.c leds-wm8350.c
led-class-flash.c leds-bcm6328.c leds-da903x.c leds-ipaq-micro.c leds-locomo.c leds-lp8788.c leds-net48xx.c leds-powernv.c leds-sunfire.c leds-wrap.c
led-core.c leds-bcm6358.c leds-da9052.c leds-is31fl32xx.c leds-lp3944.c leds-lp8860.c leds-netxbig.c leds-pwm.c leds-syscon.c led-triggers.c
leds-88pm860x.c leds-bd2802.c leds-dac124s085.c leds-ktd2692.c leds-lp5521.c leds-lt3593.c leds-ns2.c leds-rb532.c leds-tca6507.c
leds-aat1290.c leds-blinkm.c leds-fsg.c leds-lm3530.c leds-lp5523.c leds-max77693.c leds-ot200.c leds-regulator.c leds-tlc591xx.c
leds-adp5520.c leds-clevo-mail.c leds-gpio.c leds-lm3533.c leds-lp5562.c leds-max8997.c leds-pca9532.c leds-s3c24xx.c leds-versatile.c
zdong@ubuntu:~/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds$
新建一个Makefile文件。 添加以下内容。
#!/bin/bash
export ARCH=arm64 #外部环境变量
obj-m += leds-gpio.o 编译的模块
KDIR := /home/zdong/rockchip/x3399_nougat_industry/kernel #内核路径
PWD ?= $(shell pwd) #当前路径
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) modules clean
编译运行:
zdong@ubuntu:~/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds$ make
make -C /home/zdong/rockchip/x3399_nougat_industry/kernel M=/home/zdong/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds modules
make[1]: Entering directory '/home/zdong/rockchip/x3399_nougat_industry/kernel'
CC [M] /home/zdong/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds/leds-gpio.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/zdong/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds/leds-gpio.mod.o
LD [M] /home/zdong/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds/leds-gpio.ko
make[1]: Leaving directory '/home/zdong/rockchip/x3399_nougat_industry/kernel'
zdong@ubuntu:~/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds$
查看编译的模块:
zdong@ubuntu:~/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds$ ls *.ko
leds-gpio.ko
zdong@ubuntu:~/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds$
删除模块:
zdong@ubuntu:~/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds$ make clean
make -C /home/zdong/rockchip/x3399_nougat_industry/kernel M=/home/zdong/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds modules clean
make[1]: Entering directory '/home/zdong/rockchip/x3399_nougat_industry/kernel'
LD /home/zdong/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds/built-in.o
Building modules, stage 2.
MODPOST 1 modules
CLEAN /home/zdong/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds/.tmp_versions
CLEAN /home/zdong/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds/Module.symvers
make[1]: Leaving directory '/home/zdong/rockchip/x3399_nougat_industry/kernel'
zdong@ubuntu:~/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds$
前面的交叉编译工具链只是为了验证,实际其实是可以不用去管他的。
编写一个简单的测试用例,随意命名为led_gpio_test.c :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char** argv)
{
while(1)
{
system("echo 0 > /sys/devices/platform/leds/leds/led1/brightness");
sleep(1);
system("echo 1 > /sys/devices/platform/leds/leds/led1/brightness");
sleep(1);
}
return 0;
}
这样既可实现LED2不断的交替闪烁。
重写Makefile:
#!/bin/bash
export ARCH=arm64
obj-m += leds-gpio.o
rtos = led_gpio_test
KDIR := /home/zdong/rockchip/x3399_nougat_industry/kernel
CC = /home/zdong/rockchip/x3399_nougat_industry/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc
PWD ?= $(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules
$(CC) $(rtos).c
clean:
make -C $(KDIR) M=$(PWD) modules clean
该路径下会编译生成a.out文件,即为可执行文件。
zdong@ubuntu:~/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds$ make
make -C /home/zdong/rockchip/x3399_nougat_industry/kernel M=/home/zdong/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds modules
make[1]: Entering directory '/home/zdong/rockchip/x3399_nougat_industry/kernel'
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory '/home/zdong/rockchip/x3399_nougat_industry/kernel'
/home/zdong/rockchip/x3399_nougat_industry/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc led_gpio_test.c
zdong@ubuntu:~/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds$ ls
a.out leds-adp5520.c leds-cobalt-qube.c leds-gpio.mod.c leds-lm3530.c leds-lp5562.c leds-mc13783.c leds-pca963x.c leds-sunfire.c led-triggers.c
dell-led.c leds-asic3.c leds-cobalt-raq.c leds-gpio.mod.o leds-lm3533.c leds-lp55xx-common.c leds-menf21bmc.c leds-powernv.c leds-syscon.c Makefile
led-class.c leds-att1272.c leds-da903x.c leds-gpio.o leds-lm355x.c leds-lp8501.c leds-net48xx.c leds-pwm.c leds-tca6507.c modules.order
led-class-flash.c leds-bcm6328.c leds-da9052.c leds-gpio-register.c leds-lm3642.c leds-lp8788.c leds-netxbig.c leds-rb532.c leds-tlc591xx.c Module.symvers
led-core.c leds-bcm6358.c leds-dac124s085.c leds-hp6xx.c leds-locomo.c leds-lp8860.c leds-ns2.c leds-regulator.c leds-versatile.c
led_gpio_test.c leds-bd2802.c leds-fsg.c leds-ipaq-micro.c leds-lp3944.c leds-lt3593.c leds-ot200.c leds-s3c24xx.c leds-wm831x-status.c
leds-88pm860x.c leds-blinkm.c leds-gpio.c leds-is31fl32xx.c leds-lp5521.c leds-max77693.c leds-pca9532.c leds-sead3.c leds-wm8350.c
leds-aat1290.c leds-clevo-mail.c leds-gpio.ko leds-ktd2692.c leds-lp5523.c leds-max8997.c leds-pca955x.c leds-ss4200.c leds-wrap.c
zdong@ubuntu:~/rockchip/x3399_nougat_industry/kernel/drivers/leds/leds$