Linux内核在3.0以上引入了设备树概念(具体哪个版本不清楚)在编译内核后需要将与之对应的dtb文件也下载人板子上才能使内核与硬件关联起来。

dtb文件是有dts文件编译后生成的:例如

/*
* Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/ /*
* AM335x Starter Kit
* http://www.ti.com/tool/tmdssk3358
*/ /dts-v1/; #include "am33xx.dtsi"
#include <dt-bindings/pwm/pwm.h> / {
model = "TI AM335x EVM-SK";
compatible = "ti,am335x-evmsk", "ti,am33xx"; cpus {
cpu@ {
cpu0-supply = <&vdd1_reg>;
};
}; memory {
device_type = "memory";
reg = <0x80000000 0x10000000>; /* 256 MB */
}; vbat: fixedregulator@ {
compatible = "regulator-fixed";
regulator-name = "vbat";
regulator-min-microvolt = <>;
regulator-max-microvolt = <>;
regulator-boot-on;
}; lis3_reg: fixedregulator@ {
compatible = "regulator-fixed";
regulator-name = "lis3_reg";
regulator-boot-on;
}; leds {
pinctrl-names = "default";
pinctrl- = <&user_leds_s0>; compatible = "gpio-leds"; led@ {
label = "zyrD1:green:usr0";
gpios = <&gpio1 GPIO_ACTIVE_HIGH>;
default-state = "off";
}; led@ {
label = "zyrD2:green:usr1";
gpios = <&gpio1 GPIO_ACTIVE_HIGH>;
default-state = "off";
}; led@ {
label = "zyrD4:green:mmc0";
gpios = <&gpio1 GPIO_ACTIVE_HIGH>;
linux,default-trigger = "mmc0";
default-state = "off";
}; led@ {
label = "zyrD3:green:heartbeat";
gpios = <&gpio1 GPIO_ACTIVE_HIGH>;
linux,default-trigger = "heartbeat";
default-state = "off";
};
}; gpio_buttons: gpio_buttons@ {
compatible = "gpio-keys";
#address-cells = <>;
#size-cells = <>; switch@ {
label = "button0";
linux,code = <0x100>;
gpios = <&gpio2 GPIO_ACTIVE_HIGH>;
}; switch@ {
label = "button1";
linux,code = <0x101>;
gpios = <&gpio2 GPIO_ACTIVE_HIGH>;
}; switch@ {
label = "button2";
linux,code = <0x102>;
gpios = <&gpio0 GPIO_ACTIVE_HIGH>;
gpio-key,wakeup;
}; switch@ {
label = "button3";
linux,code = <0x103>;
gpios = <&gpio2 GPIO_ACTIVE_HIGH>;
};
}; backlight {
compatible = "pwm-backlight";
pwms = <&ecap2 PWM_POLARITY_INVERTED>;
brightness-levels = < >;
default-brightness-level = <>;
}; sound {
compatible = "ti,da830-evm-audio";
ti,model = "AM335x-EVMSK";
ti,audio-codec = <&tlv320aic3106>;
ti,mcasp-controller = <&mcasp1>;
ti,codec-clock-rate = <>;
ti,audio-routing =
"Headphone Jack", "HPLOUT",
"Headphone Jack", "HPROUT";
};
}; &am33xx_pinmux {
pinctrl-names = "default";
pinctrl- = <&gpio_keys_s0 &clkout2_pin>; user_leds_s0: user_leds_s0 {
pinctrl-single,pins = <
0x10 (PIN_OUTPUT_PULLDOWN | MUX_MODE7) /* gpmc_ad4.gpio1_4 */
0x14 (PIN_OUTPUT_PULLDOWN | MUX_MODE7) /* gpmc_ad5.gpio1_5 */
0x18 (PIN_OUTPUT_PULLDOWN | MUX_MODE7) /* gpmc_ad6.gpio1_6 */
0x1c (PIN_OUTPUT_PULLDOWN | MUX_MODE7) /* gpmc_ad7.gpio1_7 */
>;
}; gpio_keys_s0: gpio_keys_s0 {
pinctrl-single,pins = <
0x94 (PIN_INPUT_PULLDOWN | MUX_MODE7) /* gpmc_oen_ren.gpio2_3 */
0x90 (PIN_INPUT_PULLDOWN | MUX_MODE7) /* gpmc_advn_ale.gpio2_2 */
0x70 (PIN_INPUT_PULLDOWN | MUX_MODE7) /* gpmc_wait0.gpio0_30 */
0x9c (PIN_INPUT_PULLDOWN | MUX_MODE7) /* gpmc_ben0_cle.gpio2_5 */
>;
}; i2c0_pins: pinmux_i2c0_pins {
pinctrl-single,pins = <
0x188 (PIN_INPUT_PULLUP | MUX_MODE0) /* i2c0_sda.i2c0_sda */
0x18c (PIN_INPUT_PULLUP | MUX_MODE0) /* i2c0_scl.i2c0_scl */
>;
}; uart0_pins: pinmux_uart0_pins {
pinctrl-single,pins = <
0x170 (PIN_INPUT_PULLUP | MUX_MODE0) /* uart0_rxd.uart0_rxd */
0x174 (PIN_OUTPUT_PULLDOWN | MUX_MODE0) /* uart0_txd.uart0_txd */
>;
}; clkout2_pin: pinmux_clkout2_pin {
pinctrl-single,pins = <
0x1b4 (PIN_OUTPUT_PULLDOWN | MUX_MODE3) /* xdma_event_intr1.clkout2 */
>;
}; ecap2_pins: backlight_pins {
pinctrl-single,pins = <
0x19c 0x4 /* mcasp0_ahclkr.ecap2_in_pwm2_out MODE4 */
>;
}; cpsw_default: cpsw_default {
pinctrl-single,pins = <
/* Slave 1 */
0x114 (PIN_OUTPUT_PULLDOWN | MUX_MODE2) /* mii1_txen.rgmii1_tctl */
0x118 (PIN_INPUT_PULLDOWN | MUX_MODE2) /* mii1_rxdv.rgmii1_rctl */
0x11c (PIN_OUTPUT_PULLDOWN | MUX_MODE2) /* mii1_txd3.rgmii1_td3 */
0x120 (PIN_OUTPUT_PULLDOWN | MUX_MODE2) /* mii1_txd2.rgmii1_td2 */
0x124 (PIN_OUTPUT_PULLDOWN | MUX_MODE2) /* mii1_txd1.rgmii1_td1 */
0x128 (PIN_OUTPUT_PULLDOWN | MUX_MODE2) /* mii1_txd0.rgmii1_td0 */
0x12c (PIN_OUTPUT_PULLDOWN | MUX_MODE2) /* mii1_txclk.rgmii1_tclk */
0x130 (PIN_INPUT_PULLDOWN | MUX_MODE2) /* mii1_rxclk.rgmii1_rclk */
0x134 (PIN_INPUT_PULLDOWN | MUX_MODE2) /* mii1_rxd3.rgmii1_rd3 */
0x138 (PIN_INPUT_PULLDOWN | MUX_MODE2) /* mii1_rxd2.rgmii1_rd2 */
0x13c (PIN_INPUT_PULLDOWN | MUX_MODE2) /* mii1_rxd1.rgmii1_rd1 */
0x140 (PIN_INPUT_PULLDOWN | MUX_MODE2) /* mii1_rxd0.rgmii1_rd0 */ /* Slave 2 */
0x40 (PIN_OUTPUT_PULLDOWN | MUX_MODE2) /* gpmc_a0.rgmii2_tctl */
0x44 (PIN_INPUT_PULLDOWN | MUX_MODE2) /* gpmc_a1.rgmii2_rctl */
0x48 (PIN_OUTPUT_PULLDOWN | MUX_MODE2) /* gpmc_a2.rgmii2_td3 */
0x4c (PIN_OUTPUT_PULLDOWN | MUX_MODE2) /* gpmc_a3.rgmii2_td2 */
0x50 (PIN_OUTPUT_PULLDOWN | MUX_MODE2) /* gpmc_a4.rgmii2_td1 */
0x54 (PIN_OUTPUT_PULLDOWN | MUX_MODE2) /* gpmc_a5.rgmii2_td0 */
0x58 (PIN_OUTPUT_PULLDOWN | MUX_MODE2) /* gpmc_a6.rgmii2_tclk */
0x5c (PIN_INPUT_PULLDOWN | MUX_MODE2) /* gpmc_a7.rgmii2_rclk */
0x60 (PIN_INPUT_PULLDOWN | MUX_MODE2) /* gpmc_a8.rgmii2_rd3 */
0x64 (PIN_INPUT_PULLDOWN | MUX_MODE2) /* gpmc_a9.rgmii2_rd2 */
0x68 (PIN_INPUT_PULLDOWN | MUX_MODE2) /* gpmc_a10.rgmii2_rd1 */
0x6c (PIN_INPUT_PULLDOWN | MUX_MODE2) /* gpmc_a11.rgmii2_rd0 */
>;
}; cpsw_sleep: cpsw_sleep {
pinctrl-single,pins = <
/* Slave 1 reset value */
0x114 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x118 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x11c (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x120 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x124 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x128 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x12c (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x130 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x134 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x138 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x13c (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x140 (PIN_INPUT_PULLDOWN | MUX_MODE7) /* Slave 2 reset value*/
0x40 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x44 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x48 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x4c (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x50 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x54 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x58 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x5c (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x60 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x64 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x68 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x6c (PIN_INPUT_PULLDOWN | MUX_MODE7)
>;
}; davinci_mdio_default: davinci_mdio_default {
pinctrl-single,pins = <
/* MDIO */
0x148 (PIN_INPUT_PULLUP | SLEWCTRL_FAST | MUX_MODE0) /* mdio_data.mdio_data */
0x14c (PIN_OUTPUT_PULLUP | MUX_MODE0) /* mdio_clk.mdio_clk */
>;
}; davinci_mdio_sleep: davinci_mdio_sleep {
pinctrl-single,pins = <
/* MDIO reset value */
0x148 (PIN_INPUT_PULLDOWN | MUX_MODE7)
0x14c (PIN_INPUT_PULLDOWN | MUX_MODE7)
>;
}; mmc1_pins: pinmux_mmc1_pins {
pinctrl-single,pins = <
0x160 (PIN_INPUT | MUX_MODE7) /* spi0_cs1.gpio0_6 */
>;
}; mcasp1_pins: mcasp1_pins {
pinctrl-single,pins = <
0x10c (PIN_INPUT_PULLDOWN | MUX_MODE4) /* mii1_crs.mcasp1_aclkx */
0x110 (PIN_INPUT_PULLDOWN | MUX_MODE4) /* mii1_rxerr.mcasp1_fsx */
0x108 (PIN_OUTPUT_PULLDOWN | MUX_MODE4) /* mii1_col.mcasp1_axr2 */
0x144 (PIN_INPUT_PULLDOWN | MUX_MODE4) /* rmii1_ref_clk.mcasp1_axr3 */
>;
};
}; &uart0 {
pinctrl-names = "default";
pinctrl- = <&uart0_pins>; status = "okay";
}; &i2c0 {
pinctrl-names = "default";
pinctrl- = <&i2c0_pins>; status = "okay";
clock-frequency = <>; tps: tps@2d {
reg = <0x2d>;
}; lis331dlh: lis331dlh@ {
compatible = "st,lis331dlh", "st,lis3lv02d";
reg = <0x18>;
Vdd-supply = <&lis3_reg>;
Vdd_IO-supply = <&lis3_reg>; st,click-single-x;
st,click-single-y;
st,click-single-z;
st,click-thresh-x = <>;
st,click-thresh-y = <>;
st,click-thresh-z = <>;
st,irq1-click;
st,irq2-click;
st,wakeup-x-lo;
st,wakeup-x-hi;
st,wakeup-y-lo;
st,wakeup-y-hi;
st,wakeup-z-lo;
st,wakeup-z-hi;
st,min-limit-x = <>;
st,min-limit-y = <>;
st,min-limit-z = <>;
st,max-limit-x = <>;
st,max-limit-y = <>;
st,max-limit-z = <>;
}; tlv320aic3106: tlv320aic3106@1b {
compatible = "ti,tlv320aic3106";
reg = <0x1b>;
status = "okay"; /* Regulators */
AVDD-supply = <&vaux2_reg>;
IOVDD-supply = <&vaux2_reg>;
DRVDD-supply = <&vaux2_reg>;
DVDD-supply = <&vbat>;
};
}; &usb {
status = "okay"; control@44e10620 {
status = "okay";
}; usb-phy@ {
status = "okay";
}; usb@ {
status = "okay";
};
}; &epwmss2 {
status = "okay"; ecap2: ecap@ {
status = "okay";
pinctrl-names = "default";
pinctrl- = <&ecap2_pins>;
};
}; #include "tps65910.dtsi" &tps {
vcc1-supply = <&vbat>;
vcc2-supply = <&vbat>;
vcc3-supply = <&vbat>;
vcc4-supply = <&vbat>;
vcc5-supply = <&vbat>;
vcc6-supply = <&vbat>;
vcc7-supply = <&vbat>;
vccio-supply = <&vbat>; regulators {
vrtc_reg: regulator@ {
regulator-always-on;
}; vio_reg: regulator@ {
regulator-always-on;
}; vdd1_reg: regulator@ {
/* VDD_MPU voltage limits 0.95V - 1.26V with +/-4% tolerance */
regulator-name = "vdd_mpu";
regulator-min-microvolt = <>;
regulator-max-microvolt = <>;
regulator-boot-on;
regulator-always-on;
}; vdd2_reg: regulator@ {
/* VDD_CORE voltage limits 0.95V - 1.1V with +/-4% tolerance */
regulator-name = "vdd_core";
regulator-min-microvolt = <>;
regulator-max-microvolt = <>;
regulator-boot-on;
regulator-always-on;
}; vdd3_reg: regulator@ {
regulator-always-on;
}; vdig1_reg: regulator@ {
regulator-always-on;
}; vdig2_reg: regulator@ {
regulator-always-on;
}; vpll_reg: regulator@ {
regulator-always-on;
}; vdac_reg: regulator@ {
regulator-always-on;
}; vaux1_reg: regulator@ {
regulator-always-on;
}; vaux2_reg: regulator@ {
regulator-always-on;
}; vaux33_reg: regulator@ {
regulator-always-on;
}; vmmc_reg: regulator@ {
regulator-min-microvolt = <>;
regulator-max-microvolt = <>;
regulator-always-on;
};
};
}; &mac {
pinctrl-names = "default", "sleep";
pinctrl- = <&cpsw_default>;
pinctrl- = <&cpsw_sleep>;
}; &davinci_mdio {
pinctrl-names = "default", "sleep";
pinctrl- = <&davinci_mdio_default>;
pinctrl- = <&davinci_mdio_sleep>;
}; &cpsw_emac0 {
phy_id = <&davinci_mdio>, <>;
phy-mode = "rgmii-txid";
}; &cpsw_emac1 {
phy_id = <&davinci_mdio>, <>;
phy-mode = "rgmii-txid";
}; &mmc1 {
status = "okay";
vmmc-supply = <&vmmc_reg>;
bus-width = <>;
pinctrl-names = "default";
pinctrl- = <&mmc1_pins>;
cd-gpios = <&gpio0 GPIO_ACTIVE_HIGH>;
}; &sham {
status = "okay";
}; &aes {
status = "okay";
}; &gpio0 {
ti,no-reset-on-init;
}; &mcasp1 {
pinctrl-names = "default";
pinctrl- = <&mcasp1_pins>; status = "okay"; op-mode = <>; /* MCASP_IIS_MODE */
tdm-slots = <>;
/* 4 serializers */
serial-dir = < /* 0: INACTIVE, 1: TX, 2: RX */ >;
tx-num-evt = <>;
rx-num-evt = <>;
}; &tscadc {
status = "okay";
tsc {
ti,wires = <>;
ti,x-plate-resistance = <>;
ti,coordinate-readouts = <>;
ti,wire-config = <0x00 0x11 0x22 0x33>;
};
};

红色部分为LED子系统调用硬件配置。dtb文件和Linux内核是怎么关联的呢?主要是通过这个参数:compatible = "gpio-leds";

在leds-gpio.c中有相同的参数传递,具体是怎么操作的,没有详细的去了解。

    return priv;

err:
for (count = priv->num_leds - ; count >= ; count--)
delete_gpio_led(&priv->leds[count]);
return ERR_PTR(-ENODEV);
} static const struct of_device_id of_gpio_leds_match[] = {
{ .compatible = "gpio-leds", },
{},
};
#else /* CONFIG_OF_GPIO */
static struct gpio_leds_priv *gpio_leds_create_of(struct platform_device *pdev)
{
return ERR_PTR(-ENODEV);
}
#endif /* CONFIG_OF_GPIO */ static int gpio_led_probe(struct platform_device *pdev)
{
struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
struct gpio_leds_priv *priv;
int i, ret = ;

进入内核xxxx/drivers/leds/将led-class.c、led-core.c、leds.h、leds-gpio.c文件拷出来以模块的方法编译出leds-gpio.ko加载入模块中

编译模块的Makfile:

ifneq ($(KERNELRELEASE),)
obj-m := leds-gpio.o
else
KDIR :=/home/zyr/Source_code/linux-3.14./
all:
make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j8
clean:
rm -f *.ko *.o *.mod.o *.mod.c *.symvers *.order
endif

编写led子系统的测试:

1、点亮熄灭LED

[root@zyr-am335x ]#cd /sys/class
[root@zyr-am335x class]#ls
bdi hwmon mem regulator tty
block i2c-adapter misc rtc ubi
dma i2c-dev mmc_host scsi_device udc
extcon input mtd scsi_disk usbmon
firmware lcd net scsi_host vc
gpio leds phy spi_master vtconsole
graphics mdio_bus power_supply thermal watchdog
[root@zyr-am335x class]#cd leds
[root@zyr-am335x leds]#ls
zyrD1:green:usr0 zyrD3:green:heartbeat
zyrD2:green:usr1 zyrD4:green:mmc0
[root@zyr-am335x leds]#cd zyrD1\:green\:usr0/
[root@zyr-am335x zyrD1:green:usr0]#echo > brightness
[root@zyr-am335x zyrD1:green:usr0]#echo > brightness
[root@zyr-am335x zyrD1:green:usr0]#

2、也可以编写测试程序LED_zixitong.c

/********************************************************************
* copyright (C) 2014 all rights reserved
* @file: 子系统_led.c
* @Created: 2017-6-27 14:00nd
* @Author: Yinrui Zhu
* @Description: test user leds
* @Modify Date: 2017-6-27 14:00
*********************************************************************/ #include <stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/ioctl.h>
#include<sys/stat.h>
#include<sys/select.h>
#include<sys/time.h>
#include<fcntl.h>
#include<errno.h>
#include<linux/input.h> int main(int argc, char *argv[])
{
int ledn;
char *tmp;
int keys_fd;
//char timer=6; tmp = (unsigned char*)malloc(); while()
{
sprintf(tmp, "echo 1 > /sys/class/leds/zyrD1:green:usr0/brightness");
printf("status led4%d on \n", ledn);
system(tmp);
usleep(*);
sprintf(tmp, "echo 0 > /sys/class/leds/zyrD1:green:usr0/brightness");
printf("status led4%d on \n", ledn);
system(tmp);
usleep(*); sprintf(tmp, "echo 1 > /sys/class/leds/zyrD2:green:usr1/brightness");
printf("status led3%d on \n", ledn);
system(tmp);
usleep(*);
sprintf(tmp, "echo 0 > /sys/class/leds/zyrD2:green:usr1/brightness");
printf("status led3%d on \n", ledn);
system(tmp);
usleep(*); sprintf(tmp, "echo 1 > /sys/class/leds/zyrD3:green:heartbeat/brightness");
printf("status led1%d on \n", ledn);
system(tmp);
usleep(*);
sprintf(tmp, "echo 0 > /sys/class/leds/zyrD3:green:heartbeat/brightness");
printf("status led1%d on \n", ledn);
system(tmp);
usleep(*); //sprintf(tmp, "echo 1 > /sys/class/leds/evmsk:green:usr0/brightness");
printf("status led2%d on \n", ledn);
system("echo 1 > /sys/class/leds/zyrD4:green:mmc0/brightness");
usleep(*);
//sprintf(tmp, "echo 0 > /sys/class/leds/evmsk:green:usr0/brightness");
printf("status led2%d on \n", ledn);
system("echo 0 > /sys/class/leds/zyrD4:green:mmc0/brightness");
usleep(*); } }

测试文件的Makefile;

#----------------------------
CC = arm-linux-gnueabihf-gcc
CFLAGS =
main : LED_zixitong.c
$(CC) $(CFLAGS) LED_zixitong.c -o LED_zixitong

将生成可执行文件LED_zixitong copy到挂载配置好的nfs目录中执行既可以依次点亮4个LED了。

[root@zyr-am335x zyrD1:green:usr0]#cd
[root@zyr-am335x ]#cd /mnt/
[root@zyr-am335x mnt]#ls
LED_zixitong led_test leds-gpio.ko zleds-gpio.ko zyr-hello.ko
[root@zyr-am335x mnt]#./LED_zixitong
status led40 on
status led40 on
status led30 on
status led30 on
status led10 on
05-11 11:02