This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] CC2340R5:Zephyr 如何向器件树中添加新的 GPIO 引脚

Guru**** 2805935 points
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/wireless-connectivity/bluetooth-group/bluetooth/f/bluetooth-forum/1624764/cc2340r5-zephyr-how-to-add-new-gpio-pin-to-device-tree

器件型号: CC2340R5

您好:

我正在尝试向 lp_em_cc2340r53.dts 添加一个新的 GPIO、我想我已正确定义了覆盖、但收到了一个看似不相关的构建错误。

我的 lp_em_cc2340r53.overlay 如下所示:

/{
	aliases {
		led2 = &led2;
	};
	
	leds {
    	led2: led_2 {
			gpios = <&gpio0 19 GPIO_ACTIVE_HIGH>;
			label = "Munan LED";
		};
	};
};

这样会将 LED2 节点添加到生成的 Zephyr.dts:

/dts-v1/;

/ {
	#address-cells = < 0x1 >;
	#size-cells = < 0x1 >;
	model = "LP_EM_CC2340R53";
	compatible = "ti,lp_em_cc2340r53";
	chosen {
		zephyr,flash-controller = &flash_controller;
		zephyr,bt-hci = &ti_bt_hci;
		zephyr,entropy = &rng;
		zephyr,sram = &sram0;
		zephyr,flash = &flash0;
		zephyr,console = &uart0;
		zephyr,shell-uart = &uart0;
		zephyr,bt_hci = &ti_bt_hci;
		zephyr,bt-c2h-uart = &uart0;
		zephyr,code-partition = &slot0_partition;
		zephyr,uart-mcumgr = &uart0;
	};
	aliases {
		led0 = &led0;
		led1 = &led1;
		sw0 = &btn0;
		sw1 = &btn1;
		pwm-led0 = &pwm_green;
		pwm-led1 = &pwm_red;
		watchdog0 = &wdt0;
		mcuboot-led0 = &led1;
		mcuboot-button0 = &btn1;
		led2 = &led2;
	};

...

	leds {
		compatible = "gpio-leds";
		led0: led_0 {
			gpios = < &gpio0 0xf 0x0 >;
			label = "Green LED";
		};
		led1: led_1 {
			gpios = < &gpio0 0xe 0x0 >;
			label = "Red LED";
		};
		led2: led_2 {
			gpios = < &gpio0 0x13 0x0 >;
			label = "Munan LED";
		};
	};

我可以成功地在代码中引用此节点:

/* Locate led2 as alias or label by that name */
#if DT_NODE_EXISTS(DT_ALIAS(led2))
#define LED2 DT_ALIAS(led2)
#elif DT_NODE_EXISTS(DT_NODELABEL(led2))
#define LED2 DT_NODELABEL(led2)
#else
#define LED2 DT_INVALID_NODE
#endif

#if DT_NODE_EXISTS(LED2)
#define LED2_DEV DT_PHANDLE(LED2, gpios)
#define LED2_PIN DT_PHA(LED2, gpios, pin)
#define LED2_FLAGS DT_PHA(LED2, gpios, flags)

static const struct device *const led_dev = DEVICE_DT_GET(LED2_DEV);
#endif /* LED2 */

...
main() {
	ret = gpio_pin_configure(led_dev, LED2_PIN,
				 LED2_FLAGS | GPIO_OUTPUT_INACTIVE);

但在链接步骤中、我收到以下构建错误:

[415/420] Linking C executable zephyr\zephyr_pre0.elf; Logical command for additional byproducts on target: zephyr_pre0
FAILED: [code=1] zephyr/zephyr_pre0.elf zephyr/zephyr_pre0.map C:/zephyr-simplelink/zephyr/build/zephyr/zephyr_pre0.map
C:\WINDOWS\system32\cmd.exe /C "cd . && C:\Users\a0227237\zephyr-sdk-0.16.8\arm-zephyr-eabi\bin\arm-zephyr-eabi-gcc.exe  -gdwarf-4 zephyr/CMakeFiles/zephyr_pre0.dir/misc/empty_file.c.obj -o zephyr\zephyr_pre0.elf  zephyr/CMakeFiles/offsets.dir/./arch/arm/core/offsets/offsets.c.obj  -fuse-ld=bfd  -T  zephyr/linker_zephyr_pre0.cmd  -Wl,-Map=C:/zephyr-simplelink/zephyr/build/zephyr/zephyr_pre0.map  -Wl,--whole-archive  app/libapp.a  zephyr/libzephyr.a  zephyr/arch/common/libarch__common.a  zephyr/arch/arch/arm/core/libarch__arm__core.a  zephyr/arch/arch/arm/core/cortex_m/libarch__arm__core__cortex_m.a  zephyr/lib/libc/picolibc/liblib__libc__picolibc.a  zephyr/lib/libc/common/liblib__libc__common.a  zephyr/subsys/random/libsubsys__random.a  zephyr/subsys/bluetooth/common/libsubsys__bluetooth__common.a  zephyr/subsys/bluetooth/host/libsubsys__bluetooth__host.a  zephyr/subsys/bluetooth/mesh/libsubsys__bluetooth__mesh.a  zephyr/subsys/net/libsubsys__net.a  zephyr/drivers/bluetooth/libdrivers__bluetooth.a  zephyr/drivers/console/libdrivers__console.a  zephyr/drivers/crypto/libdrivers__crypto.a  zephyr/drivers/dma/libdrivers__dma.a  zephyr/drivers/entropy/libdrivers__entropy.a  zephyr/drivers/flash/libdrivers__flash.a  zephyr/drivers/gpio/libdrivers__gpio.a  zephyr/drivers/pinctrl/libdrivers__pinctrl.a  zephyr/drivers/pwm/libdrivers__pwm.a  zephyr/drivers/serial/libdrivers__serial.a  zephyr/drivers/spi/libdrivers__spi.a  zephyr/drivers/timer/libdrivers__timer.a  modules/ti/simplelink_lpf3/source/ti/devices/cc23x0r5/lib..__modules__hal__ti__simplelink_lpf3__source__ti__devices__cc23x0r5.a  modules/ti/simplelink_lpf3/source/ti/ble/lib..__modules__hal__ti__simplelink_lpf3__source__ti__ble.a  modules/ti/simplelink_lpf3/lib..__modules__hal__ti__simplelink_lpf3.a  modules/mbedtls/libmbedTLSBase.a  modules/mbedtls/libmbedTLSCrypto.a  modules/mbedtls/libmbedTLSX509.a  modules/mbedtls/libmodules__mbedtls.a  -Wl,--no-whole-archive  zephyr/kernel/libkernel.a  -L"c:/users/a0227237/zephyr-sdk-0.16.8/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/thumb/v6-m/nofp"  -LC:/zephyr-simplelink/zephyr/build/zephyr  -lgcc  zephyr/arch/common/libisr_tables.a  modules/mbedtls/libmbedTLSBase.a  modules/mbedtls/libmbedTLSCrypto.a  modules/mbedtls/libmbedTLSX509.a  -mcpu=cortex-m0plus  -mthumb  -mabi=aapcs  -mfp16-format=ieee  -mtp=soft  -Wl,--gc-sections  -Wl,--build-id=none  -Wl,--sort-common=descending  -Wl,--sort-section=alignment  -Wl,-u,_OffsetAbsSyms  -Wl,-u,_ConfigAbsSyms  -nostdlib  -static  -Wl,-X  -Wl,-N  -Wl,--orphan-handling=warn  -Wl,-no-pie  -DPICOLIBC_LONG_LONG_PRINTF_SCANF  C:/zephyr-simplelink/modules/hal/ti/simplelink_lpf3/source/ti/ble/lib/m0p/ble_controller.a  C:/zephyr-simplelink/modules/hal/ti/simplelink_lpf3/source/third_party/ecc/lib/gcc/m0p/ecc.a  --specs=picolibc.specs  -lc  -lgcc && C:\WINDOWS\system32\cmd.exe /C "cd /D C:\zephyr-simplelink\zephyr\build\zephyr && "C:\Program Files\CMake\bin\cmake.exe" -E true""
c:/users/a0227237/zephyr-sdk-0.16.8/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd.exe: C:/zephyr-simplelink/modules/hal/ti/simplelink_lpf3/source/ti/ble/lib/m0p/ble_controller.a(crypto_port_zephyr.c.obj): in function `CryptoP_AESInit':
/git/zephyrproject/ble_ti/source/ti/ble/controller/port/src/crypto_port_zephyr.c:106: undefined reference to `__device_dts_ord_40'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1: 'C:\Program Files\CMake\bin\cmake.EXE' --build 'C:\zephyr-simplelink\zephyr\build'
(.venv) PS C:\zephyr-simplelink\zephyr>

我的 devicetree_generated.h 中的__device_dts_ord_40 似乎指的是 ADC? GPIO 为 ORD 15。

我们知道其中包含 GPIO 驱动程序、因为代码链接成功、没有 LED2 定义...

有什么建议吗?

Munan

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Munan:

    感谢您发送编修。 我将研究这个问题、并提供更多的调查结果。 但现在、您是否可以在非 BLE 样片中尝试同样的覆盖文件? 我怀疑此问题可能与 BLE 控制器(作为预编译库)有关。

    此致、

    Ging

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Ging:

    您的理论似乎是正确的;我构建并运行了此设备树、以 FADE_LED 示例叠加、并按预期在 DIO19 上添加了 GPIO 切换。  

    Munan

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    Munan、

    感谢您试用。 我自己也已验证了问题、我会将此问题提交给研发团队。 作为目前的解决方法、您可以执行以下操作:

    #include <zephyr/drivers/gpio.h>
    
    #define CUSTOM_LED_PORT "gpio0"
    #define CUSTOM_LED_PIN  19
    
    static const struct device *gpio_dev;
    
    int main(void) {
        ....
        gpio_dev = DEVICE_DT_GET(DT_NODELABEL(gpio_0));
    	if (!device_is_ready(gpio_dev)) {
    		printk("GPIO device %s is not ready!\n", gpio_dev->name);
    		return 0;
    	}
    
    	ret = gpio_pin_configure(gpio_dev, CUSTOM_LED_PIN, GPIO_OUTPUT);
    	if (ret) {
    		printk("Error %d: failed to configure custom LED pin\n", ret);
    		return 0;
    	}
    
    	gpio_pin_set(gpio_dev, CUSTOM_LED_PIN, 1);
        ....	
    }

    它本质上只是直接使用 GPIO 驱动程序来设置 GPIO 引脚。

    此致、

    Ging