Getting started with gpio overlay

Hi

I am really struggling to get started with overlay files. I have read through a lot of documentation, watched a few hours of YouTube tutorials, and yet I can’t even get going with one gpio.


I have an existing project for the nRF5340_DK, based I think on Nordic’s BLE UART Peripheral example.
I had to change the pins used for the i2c bus, so I managed to hack an overlay fiel for that, which works – but probably isn’t correct.
So now I want to try using one of the GPIO, as an output.


The examples I found were here and here.

I couldn't get either to build.

Any help / guidance / pointers would be appreciated, thank you.

DETAILS

I decided to pick gpio0.25, and then gpio0.26, since they seem to be free.

Here is my overlay file – you will see both attempts at the end of the file – one commented out:

/*
 * Copyright (c) 2022 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

/ {
	chosen {
		nordic,nus-uart = &uart0;
	};
};


&pinctrl {
	i2c2_default: i2c2_default {
		group1 {
			psels = <NRF_PSEL(TWIM_SCL, 0, 6)>, <NRF_PSEL(TWIM_SDA, 0, 7)>;
		};
	};

	i2c2_sleep: i2c2_sleep {
		group1 {
			psels = <NRF_PSEL(TWIM_SDA, 0, 7)>,
				<NRF_PSEL(TWIM_SCL, 0, 6)>;
			low-power-enable;
		};
	};
};

&i2c2 {
	compatible = "nordic,nrf-twim";
	status = "okay";


	pinctrl-0 = <&i2c2_default>;
	pinctrl-1 = <&i2c2_default>;
	pinctrl-names = "default", "sleep";
};


&uart0_default {
	group2 {
		psels = <NRF_PSEL(UART_RX, 0, 22)>, <NRF_PSEL(UART_CTS, 0, 21)>;
	};
};

&uart0_default {
	group1 {
		psels = <NRF_PSEL(UART_RTS, 0, 19)>;
	};
};

// !gpio! - start
// From example here: https://devzone.nordicsemi.com/f/nordic-q-a/94963/need-simple-devicetree-overlay-example-for-gpio
/*
/{

	custom_gpios {
 
	   compatible = "gpio-keys";
 
	   cusgpio0: cusgpio_0 {
 
		  gpios = <&gpio0 25 GPIO_ACTIVE_HIGH>;
 
		  label = "My custom GPIO";
 
	   };
 
	};
 
	aliases {
 
	mycusgpio = &cusgpio0;
	};
 
 };

// From example here: https://devzone.nordicsemi.com/f/nordic-q-a/72618/extra-gpios-in-board-overlay-for-nrf9160dk
*/
/ {
    gpiocustom {
        compatible = "gpio-keys";
        gpiocus0: gpiocus_0 {
            gpios = <&gpio0 25 GPIO_ACTIVE_LOW>;
            label = "Custom gpio 25";
        };
        gpiocus1: gpiocus_1 {
            gpios = <&gpio0 26 GPIO_ACTIVE_LOW>;
            label = "Custom gpio 26";
        };
    };
    aliases {
        gpiocus0 = &gpiocus0;
        gpiocus1 = &gpiocus1;
    };
};
// !gpio! – end

And in main.c, again – two attempts, one commented out:

// !gpio! - start
//#define GPIO025_NODE  DT_ALIAS(mycusgpio)
//#define GPIO025           DT_GPIO_LABEL(GPIO025_NODE, gpios)
#define GPIO025_NODE    DT_ALIAS(gpiocus0)
#define GPIO025         DT_GPIO_LABEL(GPIO025_NODE, gpios)
// !gpio! - end

And in main():

    const struct device *dev_gpio0;                 // !gpio!

    dev_gpio0 = device_get_binding(GPIO025);        // !gpio!

Here are the warnings and errors I get for the first build (for gpio0.25 only – which is the attempt commented out above):

src\main.c:213:13: warning: Macro is deprecated
  213 |  dev_gpio0 = device_get_binding(GPIO025);
      |             ^~~~~~~~~~~~~~~~~~~~~
 build\zephyr\include\generated\devicetree_unfixed.h:1749:58: error: 'DT_N_S_soc_S_peripheral_40000000_S_gpio_842500_P_label' undeclared (first use in this function); did you mean 'DT_N_S_soc_S_peripheral_40000000_S_gpio_842500_P_reg'?
 1749 | #define DT_N_S_custom_gpios_S_cusgpio_0_P_gpios_IDX_0_PH DT_N_S_soc_S_peripheral_40000000_S_gpio_842500
      |                                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:3068:24: note: in definition of macro 'DT_CAT'
 3068 | #define DT_CAT(a1, a2) a1 ## a2
      |                        ^~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree\gpio.h:106:2: note: in expansion of macro 'DT_PROP'
  106 |  DT_PROP(DT_GPIO_CTLR_BY_IDX(node_id, gpio_pha, idx), label) __DEPRECATED_MACRO
      |  ^~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:3076:41: note: in expansion of macro 'DT_N_S_custom_gpios_S_cusgpio_0_P_gpios_IDX_0_PH'
 3076 | #define DT_CAT6(a1, a2, a3, a4, a5, a6) a1 ## a2 ## a3 ## a4 ## a5 ## a6
      |                                         ^~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:1247:2: note: in expansion of macro 'DT_CAT6'
 1247 |  DT_CAT6(node_id, _P_, prop, _IDX_, idx, _PH)
      |  ^~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree\gpio.h:54:2: note: in expansion of macro 'DT_PHANDLE_BY_IDX'
   54 |  DT_PHANDLE_BY_IDX(node_id, gpio_pha, idx)
      |  ^~~~~~~~~~~~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree\gpio.h:106:10: note: in expansion of macro 'DT_GPIO_CTLR_BY_IDX'
  106 |  DT_PROP(DT_GPIO_CTLR_BY_IDX(node_id, gpio_pha, idx), label) __DEPRECATED_MACRO
      |          ^~~~~~~~~~~~~~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree\gpio.h:120:2: note: in expansion of macro 'DT_GPIO_LABEL_BY_IDX'
  120 |  DT_GPIO_LABEL_BY_IDX(node_id, gpio_pha, 0) __DEPRECATED_MACRO
      |  ^~~~~~~~~~~~~~~~~~~~
 src\main.c:64:19: note: in expansion of macro 'DT_GPIO_LABEL'
   64 | #define GPIO025   DT_GPIO_LABEL(GPIO025_NODE, gpios)
      |                   ^~~~~~~~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:3068:24: note: in expansion of macro 'DT_N_ALIAS_mycusgpio'
 3068 | #define DT_CAT(a1, a2) a1 ## a2
      |                        ^~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:213:25: note: in expansion of macro 'DT_CAT'
  213 | #define DT_ALIAS(alias) DT_CAT(DT_N_ALIAS_, alias)
      |                         ^~~~~~
 src\main.c:63:22: note: in expansion of macro 'DT_ALIAS'
   63 | #define GPIO025_NODE DT_ALIAS(mycusgpio)
      |                      ^~~~~~~~
 src\main.c:64:33: note: in expansion of macro 'GPIO025_NODE'
   64 | #define GPIO025   DT_GPIO_LABEL(GPIO025_NODE, gpios)
      |                                 ^~~~~~~~~~~~
 src\main.c:213:33: note: in expansion of macro 'GPIO025'
  213 |  dev_gpio0 = device_get_binding(GPIO025);   
      |                                 ^~~~~~~
 build\zephyr\include\generated\devicetree_unfixed.h:1749:58: note: each undeclared identifier is reported only once for each function it appears in
 1749 | #define DT_N_S_custom_gpios_S_cusgpio_0_P_gpios_IDX_0_PH DT_N_S_soc_S_peripheral_40000000_S_gpio_842500
      |                                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:3068:24: note: in definition of macro 'DT_CAT'
 3068 | #define DT_CAT(a1, a2) a1 ## a2
      |                        ^~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree\gpio.h:106:2: note: in expansion of macro 'DT_PROP'
  106 |  DT_PROP(DT_GPIO_CTLR_BY_IDX(node_id, gpio_pha, idx), label) __DEPRECATED_MACRO
      |  ^~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:3076:41: note: in expansion of macro 'DT_N_S_custom_gpios_S_cusgpio_0_P_gpios_IDX_0_PH'
 3076 | #define DT_CAT6(a1, a2, a3, a4, a5, a6) a1 ## a2 ## a3 ## a4 ## a5 ## a6
      |                                         ^~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:1247:2: note: in expansion of macro 'DT_CAT6'
 1247 |  DT_CAT6(node_id, _P_, prop, _IDX_, idx, _PH)
      |  ^~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree\gpio.h:54:2: note: in expansion of macro 'DT_PHANDLE_BY_IDX'
   54 |  DT_PHANDLE_BY_IDX(node_id, gpio_pha, idx)
      |  ^~~~~~~~~~~~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree\gpio.h:106:10: note: in expansion of macro 'DT_GPIO_CTLR_BY_IDX'
  106 |  DT_PROP(DT_GPIO_CTLR_BY_IDX(node_id, gpio_pha, idx), label) __DEPRECATED_MACRO
      |          ^~~~~~~~~~~~~~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree\gpio.h:120:2: note: in expansion of macro 'DT_GPIO_LABEL_BY_IDX'
  120 |  DT_GPIO_LABEL_BY_IDX(node_id, gpio_pha, 0) __DEPRECATED_MACRO
      |  ^~~~~~~~~~~~~~~~~~~~
 src\main.c:64:19: note: in expansion of macro 'DT_GPIO_LABEL'
   64 | #define GPIO025   DT_GPIO_LABEL(GPIO025_NODE, gpios)
      |                   ^~~~~~~~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:3068:24: note: in expansion of macro 'DT_N_ALIAS_mycusgpio'
 3068 | #define DT_CAT(a1, a2) a1 ## a2
      |                        ^~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:213:25: note: in expansion of macro 'DT_CAT'
  213 | #define DT_ALIAS(alias) DT_CAT(DT_N_ALIAS_, alias)
      |                         ^~~~~~
 src\main.c:63:22: note: in expansion of macro 'DT_ALIAS'
   63 | #define GPIO025_NODE DT_ALIAS(mycusgpio)
      |                      ^~~~~~~~
 src\main.c:64:33: note: in expansion of macro 'GPIO025_NODE'
   64 | #define GPIO025   DT_GPIO_LABEL(GPIO025_NODE, gpios)
      |                                 ^~~~~~~~~~~~
 src\main.c:213:33: note: in expansion of macro 'GPIO025'
  213 |  dev_gpio0 = device_get_binding(GPIO025);   
      |                                 ^~~~~~~
[27/221] Building C object zephyr/drivers/mbox/CMakeFiles/drivers__mbox.dir/mbox_nrfx_ipc.c.obj
[29/219] Building C object modules/nrf/lib/dk_buttons_and_leds/CMakeFiles/..__nrf__lib__dk_buttons_and_leds.dir/dk_buttons_and_leds.c.obj
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1

And the error and warning for my second attempt:

../src/main.c: In function 'main':
src\main.c:215:13: warning: Macro is deprecated
  215 |  dev_gpio0 = device_get_binding(GPIO025);  // !gpio!
      |             ^~~~~~~~~~~~~~~~~~~~~
build\zephyr\include\generated\devicetree_unfixed.h:1752:56: error: 'DT_N_S_soc_S_peripheral_40000000_S_gpio_842500_P_label' undeclared (first use in this function); did you mean 'DT_N_S_soc_S_peripheral_40000000_S_gpio_842500_P_reg'?
 1752 | #define DT_N_S_gpiocustom_S_gpiocus_0_P_gpios_IDX_0_PH DT_N_S_soc_S_peripheral_40000000_S_gpio_842500
      |                                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:3068:24: note: in definition of macro 'DT_CAT'
 3068 | #define DT_CAT(a1, a2) a1 ## a2
      |                        ^~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree\gpio.h:106:2: note: in expansion of macro 'DT_PROP'
  106 |  DT_PROP(DT_GPIO_CTLR_BY_IDX(node_id, gpio_pha, idx), label) __DEPRECATED_MACRO
      |  ^~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:3076:41: note: in expansion of macro 'DT_N_S_gpiocustom_S_gpiocus_0_P_gpios_IDX_0_PH'
 3076 | #define DT_CAT6(a1, a2, a3, a4, a5, a6) a1 ## a2 ## a3 ## a4 ## a5 ## a6
      |                                         ^~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:1247:2: note: in expansion of macro 'DT_CAT6'
 1247 |  DT_CAT6(node_id, _P_, prop, _IDX_, idx, _PH)
      |  ^~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree\gpio.h:54:2: note: in expansion of macro 'DT_PHANDLE_BY_IDX'
   54 |  DT_PHANDLE_BY_IDX(node_id, gpio_pha, idx)
      |  ^~~~~~~~~~~~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree\gpio.h:106:10: note: in expansion of macro 'DT_GPIO_CTLR_BY_IDX'
  106 |  DT_PROP(DT_GPIO_CTLR_BY_IDX(node_id, gpio_pha, idx), label) __DEPRECATED_MACRO
      |          ^~~~~~~~~~~~~~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree\gpio.h:120:2: note: in expansion of macro 'DT_GPIO_LABEL_BY_IDX'
  120 |  DT_GPIO_LABEL_BY_IDX(node_id, gpio_pha, 0) __DEPRECATED_MACRO
      |  ^~~~~~~~~~~~~~~~~~~~
src\main.c:66:19: note: in expansion of macro 'DT_GPIO_LABEL'
   66 | #define GPIO025   DT_GPIO_LABEL(GPIO025_NODE, gpios)
      |                   ^~~~~~~~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:3068:24: note: in expansion of macro 'DT_N_ALIAS_gpiocus0'
 3068 | #define DT_CAT(a1, a2) a1 ## a2
      |                        ^~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:213:25: note: in expansion of macro 'DT_CAT'
  213 | #define DT_ALIAS(alias) DT_CAT(DT_N_ALIAS_, alias)
      |                         ^~~~~~
src\main.c:65:22: note: in expansion of macro 'DT_ALIAS'
   65 | #define GPIO025_NODE DT_ALIAS(gpiocus0)
      |                      ^~~~~~~~
src\main.c:66:33: note: in expansion of macro 'GPIO025_NODE'
   66 | #define GPIO025   DT_GPIO_LABEL(GPIO025_NODE, gpios)
      |                                 ^~~~~~~~~~~~
src\main.c:215:33: note: in expansion of macro 'GPIO025'
  215 |  dev_gpio0 = device_get_binding(GPIO025);  // !gpio!
      |                                 ^~~~~~~
build\zephyr\include\generated\devicetree_unfixed.h:1752:56: note: each undeclared identifier is reported only once for each function it appears in
 1752 | #define DT_N_S_gpiocustom_S_gpiocus_0_P_gpios_IDX_0_PH DT_N_S_soc_S_peripheral_40000000_S_gpio_842500
      |                                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:3068:24: note: in definition of macro 'DT_CAT'
 3068 | #define DT_CAT(a1, a2) a1 ## a2
      |                        ^~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree\gpio.h:106:2: note: in expansion of macro 'DT_PROP'
  106 |  DT_PROP(DT_GPIO_CTLR_BY_IDX(node_id, gpio_pha, idx), label) __DEPRECATED_MACRO
      |  ^~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:3076:41: note: in expansion of macro 'DT_N_S_gpiocustom_S_gpiocus_0_P_gpios_IDX_0_PH'
 3076 | #define DT_CAT6(a1, a2, a3, a4, a5, a6) a1 ## a2 ## a3 ## a4 ## a5 ## a6
      |                                         ^~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:1247:2: note: in expansion of macro 'DT_CAT6'
 1247 |  DT_CAT6(node_id, _P_, prop, _IDX_, idx, _PH)
      |  ^~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree\gpio.h:54:2: note: in expansion of macro 'DT_PHANDLE_BY_IDX'
   54 |  DT_PHANDLE_BY_IDX(node_id, gpio_pha, idx)
      |  ^~~~~~~~~~~~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree\gpio.h:106:10: note: in expansion of macro 'DT_GPIO_CTLR_BY_IDX'
  106 |  DT_PROP(DT_GPIO_CTLR_BY_IDX(node_id, gpio_pha, idx), label) __DEPRECATED_MACRO
      |          ^~~~~~~~~~~~~~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree\gpio.h:120:2: note: in expansion of macro 'DT_GPIO_LABEL_BY_IDX'
  120 |  DT_GPIO_LABEL_BY_IDX(node_id, gpio_pha, 0) __DEPRECATED_MACRO
      |  ^~~~~~~~~~~~~~~~~~~~
src\main.c:66:19: note: in expansion of macro 'DT_GPIO_LABEL'
   66 | #define GPIO025   DT_GPIO_LABEL(GPIO025_NODE, gpios)
      |                   ^~~~~~~~~~~~~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:3068:24: note: in expansion of macro 'DT_N_ALIAS_gpiocus0'
 3068 | #define DT_CAT(a1, a2) a1 ## a2
      |                        ^~
C:\Users\me\ncs\v2.1.0\zephyr\include\zephyr\devicetree.h:213:25: note: in expansion of macro 'DT_CAT'
  213 | #define DT_ALIAS(alias) DT_CAT(DT_N_ALIAS_, alias)
      |                         ^~~~~~
src\main.c:65:22: note: in expansion of macro 'DT_ALIAS'
   65 | #define GPIO025_NODE DT_ALIAS(gpiocus0)
      |                      ^~~~~~~~
src\main.c:66:33: note: in expansion of macro 'GPIO025_NODE'
   66 | #define GPIO025   DT_GPIO_LABEL(GPIO025_NODE, gpios)
      |                                 ^~~~~~~~~~~~
src\main.c:215:33: note: in expansion of macro 'GPIO025'
  215 |  dev_gpio0 = device_get_binding(GPIO025);  // !gpio!
      |                                 ^~~~~~~
[27/222] Building C object zephyr/drivers/pinctrl/CMakeFiles/drivers__pinctrl.dir/pinctrl_nrf.c.obj
[29/220] Building C object zephyr/drivers/mbox/CMakeFiles/drivers__mbox.dir/mbox_nrfx_ipc.c.obj
[30/220] Building C object zephyr/drivers/entropy/CMakeFiles/drivers__entropy.dir/C_/Users/garrett/ncs/v2.1.0/nrf/drivers/entropy/entropy_cc3xx.c.obj
[31/220] Building C object modules/nrf/lib/dk_buttons_and_leds/CMakeFiles/..__nrf__lib__dk_buttons_and_leds.dir/dk_buttons_and_leds.c.obj
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1

By the way, I don’t know where this came from, in the overlay file – I suspect is was from when I was playing with the Device Tree GUI:

&uart0_default {
	group2 {
		psels = <NRF_PSEL(UART_RX, 0, 22)>, <NRF_PSEL(UART_CTS, 0, 21)>;
	};
};

&uart0_default {
	group1 {
		psels = <NRF_PSEL(UART_RTS, 0, 19)>;
	};
};

  • Hi Priyanka

    The line is the closing curly brace for the main() routine.

    void main(void)
    {
      const struct device *dev_gpio0;
    
      // - start
      dev_gpio0 = DEVICE_DT_GET(DT_NODELABEL(gpiocus0));
    
      if (!device_is_ready(dev_gpio0)) {
        printk("main: gpio not ready\n");
      }
    
      gpio_pin_configure(dev_gpio0, CUS_PIN0,  GPIO_OUTPUT);
      gpio_pin_set(dev_gpio0, CUS_PIN0, 0);
      // - end
    
      :
      :
    
    }

    Essentially if I comment out the above code (between the -start and -end comments), the code builds successfully, so it obviously does not like/agree with this line:

      dev_gpio0 = DEVICE_DT_GET(DT_NODELABEL(gpiocus0));
    

    As, any subsequent reference to dev_gpio0 results in a failed build.

  • Some more information:

    The code builds if I use device_get_binding() instead of DEVICE_DT_GET(DT_NODELABEL( )).

    I don't know if I will be able to actually manipulate the output, but it does build...

    	//dev_gpio0 = DEVICE_DT_GET(DT_NODELABEL(gpiocus0));
    	dev_gpio0 = device_get_binding("Custom gpio 28");
    	if (!device_is_ready(dev_gpio0)) {
    		printk("main: gpio not ready\n");
    	}
    	gpio_pin_configure(dev_gpio0, CUS_PIN0,  GPIO_OUTPUT);
    	gpio_pin_set(dev_gpio0, CUS_PIN0, 0);

  • That's interesting, because it's generally recommended to use DEVICE_DT_GET() instead of device_get_binding() . Please take a look here: https://docs.zephyrproject.org/latest/releases/release-notes-3.2.html#devicetree 

    -Priyanka

  • Hi

    I am revisiting this, and I have yet to get this working.
    And now the matter is becoming highly urgent.

    I have read so many posts, documentation, tutorials, and am going around in circles.

    I am obviously missing one simple piece of information... please help thanks.

    So, I started again, from scratch...

    I am using nRF Connect SDK 2.1.0
    My board is the nRF52840 DK

    Step 1
    I create a new application, from nrf samples: peripheral uart
    Pristine build --> success

    Step 2
    I add the following to the app.overlay file:

    / {
        gpiocustom {
            compatible = "gpio-keys";
            gpiocus0: gpiocus_0 {
                gpios = <&gpio0 28 GPIO_ACTIVE_LOW>;
                label = "Custom gpio 28";
            };
        };
        aliases {
            gpiocus0 = &gpiocus0;
        };
    };

    so now the entire overlay file is as follows:

    /*
     * Copyright (c) 2022 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
     */
    
    / {
    	chosen {
    		nordic,nus-uart = &uart0;
    	};
    };
    
    / {
        gpiocustom {
            compatible = "gpio-keys";
            gpiocus0: gpiocus_0 {
                gpios = <&gpio0 28 GPIO_ACTIVE_LOW>;
                label = "Custom gpio 28";
            };
        };
        aliases {
            gpiocus0 = &gpiocus0;
        };
    };

    Pristine build --> success

    In main.c, I added the definition:

    #define MODE_BUTTON DT_ALIAS(gpiocus0)
    

    Pristine build --> success

    In main(), I added:

    const struct device *const my_dev = DEVICE_DT_GET(MODE_BUTTON);
    

    Pristine build --> success

    In main(), I added:

    if (!device_is_ready(my_dev)) {
      /* Not ready, do not use */
      return;
    }

    Pristine build --> error

    undefined reference to `__device_dts_ord_20'
    

    When I look at zephyr_final.map, from the last successful build, it does not include any mention of `__device_dts_ord_20'

    When I look at the zephyr.dts file - after the build fail, it does include the gpiocus0 alias, and the overlay info:

    	gpiocustom {
    		compatible = "gpio-keys";
    		gpiocus0: gpiocus_0 {
    			gpios = < &gpio0 0x1c 0x1 >;
    			label = "Custom gpio 28";
    		};
    	};

    Note: I get the same buil derror whether I use node label or alias definition:

    #define MODE_BUTTON DT_ALIAS(gpiocus0)
    #define MODE_BUTTON DT_NODELABEL(gpiocus0)

    #define MODE_BUTTON DT_ALIAS(gpiocus0)
    #define MODE_BUTTON DT_NODELABEL(gpiocus0)

  • Hi,

    Where in main() did you add this

    if (!device_is_ready(my_dev)) {
    /* Not ready, do not use */
    return;
    }

    I tried this and did not get any build errors.

    Regards,

    Priyanka

Related