This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

device_get_binding returning NULL for a custom board (blinky example)

I created a simple custom board (in VS Code 'Create a new board' Wizard) based on nrf9160 secure.

I copied the LED part of the original dts and I want to run blinky.

At first it worked, then somehow somewhere it stopped working. I traced the issue down to the function device_get_binding() that keeps returning NULL for my custom board even though all applicable settings and pins are identical.

I have tried the same with nrf52840 and nrf52832 and I get the same result.

The frustrating part is that it did work and stopped working all of a sudden - Is there some global setting that I could have accidently changed?

  • Hello Eduan, can you share the device tree you are using and show the definition of LED0 ?

  • Hi, this is the dts file for the nrf52832 example:

    // Copyright (c) 2022 Nordic Semiconductor ASA
    // SPDX-License-Identifier: Apache-2.0
    
    /dts-v1/;
    #include <nordic/nrf52832_ciaa.dtsi>
    
    / {
    	model = "act";
    	compatible = "nordic,act";
    
    	chosen {
    		zephyr,sram = &sram0;
    		zephyr,flash = &flash0;
    		zephyr,code-partition = &slot0_partition;
    	};
    
    	leds {
    		compatible = "gpio-leds";
    		led0: led_0 {
    			/* ANNA-B1 GPIO_29 */
    			gpios = <&gpio0 27 GPIO_ACTIVE_LOW>;
    			label = "Red LED";
    		};
    		led1: led_1 {
    			/* ANNA-B1 GPIO_30 */
    			gpios = <&gpio0 25 GPIO_ACTIVE_LOW>;
    			label = "Green LED";
    		};
    		led2: led_2 {
    			/*ANNA-B1 GPIO_31 */
    			gpios = <&gpio0 26 GPIO_ACTIVE_LOW>;
    			label = "Blue LED";
    		};
    	};
    
    	aliases {
    		led0 = &led0;
    		led1 = &led1;
    		led2 = &led2;
    	};
    };
    
    &flash0 {
    	partitions {
    		compatible = "fixed-partitions";
    		#address-cells = <1>;
    		#size-cells = <1>;
    
    		boot_partition: partition@0 {
    			label = "mcuboot";
    			reg = <0x0 0xc000>;
    		};
    		slot0_partition: partition@c000 {
    			label = "image-0";
    			reg = <0xc000 0x32000>;
    		};
    		slot1_partition: partition@3e000 {
    			label = "image-1";
    			reg = <0x3e000 0x32000>;
    		};
    		scratch_partition: partition@70000 {
    			label = "image-scratch";
    			reg = <0x70000 0xa000>;
    		};
    		storage_partition: partition@7a000 {
    			label = "storage";
    			reg = <0x7a000 0x6000>;
    		};
    	};
    };
    
    

  • Here is my main.c:

    /*
     * Copyright (c) 2016 Intel Corporation
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <zephyr.h>
    #include <device.h>
    #include <devicetree.h>
    #include <drivers/gpio.h>
    
    /* 1000 msec = 1 sec */
    #define SLEEP_TIME_MS   1000
    
    /* The devicetree node identifier for the "led0" alias. */
    #define LED0_NODE DT_ALIAS(led0)
    
    #if DT_NODE_HAS_STATUS(LED0_NODE, okay)
    #define LED0	DT_GPIO_LABEL(LED0_NODE, gpios)
    #define PIN	DT_GPIO_PIN(LED0_NODE, gpios)
    #define FLAGS	DT_GPIO_FLAGS(LED0_NODE, gpios)
    #else
    /* A build error here means your board isn't set up to blink an LED. */
    #error "Unsupported board: led0 devicetree alias is not defined"
    #define LED0	""
    #define PIN	0
    #define FLAGS	0
    #endif
    
    void main(void)
    {
    	const struct device *dev;
    	bool led_is_on = true;
    	int ret;
    
    	dev = device_get_binding(LED0);
    	if (dev == NULL) {
    		return;
    	}
    
    	ret = gpio_pin_configure(dev, PIN, GPIO_OUTPUT_ACTIVE | FLAGS);
    	if (ret < 0) {
    		return;
    	}
    
    	while (1) {
    		gpio_pin_set(dev, PIN, (int)led_is_on);
    		led_is_on = !led_is_on;
    		k_msleep(SLEEP_TIME_MS);
    	}
    }

  • Can you try with:

    #define C_LED_LABEL DT_GPIO_LABEL(LED0_NODE, gpios)
    device_get_binding(C_LED_LABEL);
  • Nope, same issue. How can something as simple as this not work? This is really frustrating

Related