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)>;
	};
};

Parents
  • Hi

    I feel like I am getting closer to a solution, but…

    So here’s the code from my overlay:

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

    My defines:

    #define CUS_PIN0_NODE	DT_NODELABEL(gpiocus0)
    #define CUS_PIN0		DT_GPIO_PIN(CUS_PIN0_NODE, gpios)

    And the code in main()

    	const struct device *dev_gpio0;
    
    	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);
    

    Resulting in this error:

    C:\dev\myapp\src\main.c:296: undefined reference to `__device_dts_ord_19'

  • Hello,
    I am sorry, but we are short staffed this week due to Public Holidays in Norway. We will be back on Monday 22nd and hope to be able to answer all incoming requests within a couple of days, depending on the backlog. I am sorry for the inconvenience.

    Best regards,
    Priyanka

  • 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

  • Hi Priyanka

    Near the start of main() - see code below - although I just tried to further down, just before the forever loop, with the same result - build fail.

    void main(void)
    {
    	int blink_status = 0;
    	int err = 0;
    	const struct device *const my_dev = DEVICE_DT_GET(MODE_BUTTON);		// !io!
    
    	configure_gpio();
    
    	err = uart_init();
    	if (err) {
    		error();
    	}
    
    	if (!device_is_ready(my_dev)) {										// !io!
    	  /* Not ready, do not use */
    	  return;
    	  //return -ENODEV;
    	}
    
    	if (IS_ENABLED(CONFIG_BT_NUS_SECURITY_ENABLED)) {
    		err = bt_conn_auth_cb_register(&conn_auth_callbacks);
    		if (err) {
    			printk("Failed to register authorization callbacks.\n");
    			return;
    		}
    
    		err = bt_conn_auth_info_cb_register(&conn_auth_info_callbacks);
    		if (err) {
    			printk("Failed to register authorization info callbacks.\n");
    			return;
    		}
    	}
    
    	err = bt_enable(NULL);
    	if (err) {
    		error();
    	}
    
    	LOG_INF("Bluetooth initialized");
    
    	k_sem_give(&ble_init_ok);
    
    	if (IS_ENABLED(CONFIG_SETTINGS)) {
    		settings_load();
    	}
    
    	err = bt_nus_init(&nus_cb);
    	if (err) {
    		LOG_ERR("Failed to initialize UART service (err: %d)", err);
    		return;
    	}
    
    	err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd,
    			      ARRAY_SIZE(sd));
    	if (err) {
    		LOG_ERR("Advertising failed to start (err %d)", err);
    		return;
    	}
    
    	
    	//if (!device_is_ready(my_dev)) {										// !io!
    	//  /* Not ready, do not use */
    	//  return;
    	//  //return -ENODEV;
    	//}
    
    	for (;;) {
    		dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);
    		k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
    	}
    }

    I can zip the project and send it to you, if that helps. Just tell me how I can send a zipped project.

    Regards

    Garrett

  • Hi Garrett,

    You can just right click on your project folder and select "compress to ZIP folder" and then share the zip folder here.

    Regards,

    Priyanka

  • Hi Garrett,

    I can see the error. I placed the snippet at the end of main () and was able to override the error. Please try this and let me know how it goes. 

    Also, _device_dts_ord_20 is quite puzzling. Maybe you can refer this blog for details.

    Another suggestion would be that instead of directly modifying in the original app.overlay, you can create another overlay file nrf52840dk_nrf52840.overlay and put you dts modifications there.

    Regards,

    Priyanka

Reply
  • Hi Garrett,

    I can see the error. I placed the snippet at the end of main () and was able to override the error. Please try this and let me know how it goes. 

    Also, _device_dts_ord_20 is quite puzzling. Maybe you can refer this blog for details.

    Another suggestion would be that instead of directly modifying in the original app.overlay, you can create another overlay file nrf52840dk_nrf52840.overlay and put you dts modifications there.

    Regards,

    Priyanka

Children
  • Hi Priyanka

    Thank you for your reply.
    I have a few points.

    Point 1

    What do you mean: : I placed the snippet at the end of main () and was able to override the error.:
    I tried placing the code here:

    	if (!device_is_ready(my_dev)) {										// !io!
    	  /* Not ready, do not use */
    	  return;
    
    	}
    
    	for (;;) {
    		dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);
    		k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
    	}

    And I also tried this:

    	try_this();																// !io!
    
    	for (;;) {
    		dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);
    		k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
    	}
    
    void try_this() {															// !io!
    	const struct device *const my_dev = DEVICE_DT_GET(MODE_BUTTON);
    	
    	if (!device_is_ready(my_dev)) {
    	  /* Not ready, do not use */
    	  return;
    	}	
    }

    And both failed, with the same `__device_dts_ord_20' error.


    Point 2

    I tried putting the overlay definition in this file:

    nrf52840dk_nrf52840.overlay

    However, whether I placed this file in the application root, or in the boards sub-folder, the application did NOT find / recognise the file.

    Point 3

    I had a look at the blog you mentioned.
    I found a similar file to the build/include/generated/devicetree_generated.h file the blog mentions.
    I found it here:
    build\zephyr\include\generated\devicetree_unfixed.h

    In this file, I can see the Node dependency ordering list.

    And I can see node 20, which corresponds to the `__device_dts_ord_20' error is listed:

     *   17  /cpus/cpu@0
     *   18  /cpus/cpu@0/itm@e0000000
     *   19  /gpiocustom
     *   20  /gpiocustom/gpiocus_0
     *   21  /leds
     *   22  /leds/led_0
     *   23  /leds/led_1
     *   24  /leds/led_2
     *   25  /leds/led_3

    So I know it exists.

    I also looked in the build/zephyr/zephyr.dts file, and can see the node mentioned there:

    	aliases {
    		led0 = &led0;
    		led1 = &led1;
    		led2 = &led2;
    		led3 = &led3;
    		:
    		:
    		gpiocus0 = &gpiocus0;
    	};

    And at the end of the file,

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

    In the blog, the issue was that the status was disabled.
    I read somewhere that with zephyr, if the status is not mentioned, then it is enabled.

    However, I decided to include it anyway, in the app.overlay file:

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

    But this did not make any difference.

    Point 4

    The overlay files are meant to re-define / re-purpose peripherals, correct?

    The only mention of gpio0.28 I can find in the files are as follows...

    In the nrf52840dk_nrf52840.dts file:

    	arduino_header: connector {
    		compatible = "arduino-header-r3";
    		#gpio-cells = <2>;
    		gpio-map-mask = <0xffffffff 0xffffffc0>;
    		gpio-map-pass-thru = <0 0x3f>;
    		gpio-map = <0 0 &gpio0 3 0>,	/* A0 */
    			   <1 0 &gpio0 4 0>,	/* A1 */
    			   <2 0 &gpio0 28 0>,	/* A2 */
    			   <3 0 &gpio0 29 0>,	/* A3 */

    and

    	arduino_adc: analog-connector {
    		compatible = "arduino,uno-adc";
    		#io-channel-cells = <1>;
    		io-channel-map = <0 &adc 1>,	/* A0 = P0.3 = AIN1 */
    				 <1 &adc 2>,	/* A1 = P0.4 = AIN2 */
    				 <2 &adc 4>,	/* A2 = P0.28 = AIN4 */

    Bu there is no mention of 0.28 in nrf52840dk_nrf52840-pinctrl.dtsi

    Does this mean that this pin is not defined, and that it needs to be defined somewhere before it is "overlayed"?

    Point 5

    Instead of using gpio0.28, I tried using gpio0.20 - which is already defined (spi2: SPIM_MOSI) in nrf52840dk_nrf52840-pinctrl.dtsi

    But I get the exact same error.

    Regards

    Garrett

  • Hi Garrett,

    This is how I placed the snippet and was able to get away with the error:

    	for (;;) {
    		dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);
    		k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
    	}
    	
    		if (!device_is_ready(my_dev)) {										// !io!
    	  /* Not ready, do not use */
    	  return;
    
    	}
    

    It's also strange that the nrf52840dk_nrf52840.overlay is not being recognize for you. Because I placed it inside the peripheral_uart project folder directly and it detects for me.

    I will cross check your project zip file once again.

    -Priyanka

  • When I do the same, the projects build successfully.

    However, I don't see the point in putting the code there, as it will never be run, because of the forever loop. Which is why it probably builds - because the compiler probably recognises that it is redundant, so omits it.

  • I can't believe it is proving so difficult (and so far impossible) for me just to use one i/o pin. Can someone else help out on this please?

  • Hey Garrett,

    I pulled a fresh peripheral_uart sample and built it on the nRF52840DK.

    I added your overlay for adding the gpiocustom node to re-use port 0 pin 28 from the ARD header.

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

    I then added that i/o pin as an input that triggers an interrupt callback that prints "MODE button pressed" when the pin is driven low from high. (Tested with my logic analyzer)

    All changes I used for being able to use that pin as an input are highlighted here in this PR.

    Custom gpio by droidecahedron · Pull Request #1 · droidecahedron/nrf_peripheral_uart_custom_gpio (github.com)

    I hope this sheds some light on how to use that I/O pin within peripheral UART sample. (I added the interrupt callback because I assumed since you named it button that would have been the intention. If not, let me know and we can sync up.)

    Best regards,

Related