GPIO pin value are read incorrectly from the .overlay file

I generated an overlay file with the following info:

// To get started, press Ctrl+Space to bring up the completion menu and view the available nodes.

// You can also use the buttons in the sidebar to perform actions on nodes.
// Actions currently available include:

// * Enabling / disabling the node
// * Adding the bus to a bus
// * Removing the node
// * Connecting ADC channels

// For more help, browse the DeviceTree documentation at https://docs.zephyrproject.org/latest/guides/dts/index.html
// You can also visit the nRF DeviceTree extension documentation at https://docs.nordicsemi.com/bundle/nrf-connect-vscode/page/guides/ncs_configure_app.html#devicetree-support-in-the-extension

&gpio0 {
	status = "okay";
};

&gpiote {
	status = "okay";
};

/ {
	leds {
		compatible = "gpio-leds";
		yellow_led: led {
			label = "led";
			gpios = <&gpio0 6 0>;
		};
	};
};

and there is my main.c file:

// have some useful utility functions provided by zeyphr kernal.
#include <zephyr/kernel.h>

// include the GPIO driver
#include <zephyr/drivers/gpio.h>

// get the gpio pin properties from the .overlay file
static struct gpio_dt_spec led = GPIO_DT_SPEC_GET(DT_NODELABEL(yellow_led), gpios);

int main(void)
{
        // check if the pin is ready to use (the function returns 0 if everything is ok)
        if(!gpio_is_ready_dt(&led))
        {
                // end the program
                return 0;
        }

        // check if the pin is configured successfully (the function returns 0 if everything is ok)
        if(gpio_pin_configure_dt(&led, GPIO_OUTPUT_INACTIVE) < 0)
        {
                // end the program
                return 0;
        }

        // loop infinitely
        while(1)
        {
                // toggle the led and check if it was toggled successfully 
                if(gpio_pin_toggle_dt(&led) < 0)
                {
                        return 0;
                }

                // sleep for 1 second 
                k_msleep(1000);
        }

        return 0;
}

However, when I tried to debug my code to understand what was wrong, I got the following value of the led variable at the start of the main:

$1 = {port = 0x395f <notify_monitors+40>, pin = 0 '\000', dt_flags = 0}

which is incorrect because the pin value should be 6 not 0.

Parents
  • Hi,

    I do not see any issues with this, and when I test the code on my end, I see pin 6 toggling. Did you measure with alogic analyzer? Also, if you are testing on a DK (?) you may face problems as pin 6 is connected to the onboard debugger as UART TXD.

  • actually, it isn't working on my end and I am not using one of the Nordic development kits, I developed my kit and tried to run this code on it. here are the schematics:

    but I am pretty sure it has nothing to do with the hardware. I started a debugging session using the gdb server with the help of my black magic probe, and here is the CPU halted at the first line.

     

    before executing the first line, the variable called "led" is initialized to have a pin value equal to 0 which is incorrect because it's supposed to be 6. which means that the line 

    static struct gpio_dt_spec led = GPIO_DT_SPEC_GET(DT_NODELABEL(yellow_led), gpios);
    

    initializes the variable led with wrong values. I don't know why is that.

  • when I hovered over the `GPIO_DT_SPEC_GET` macro to see what it expands to, it showed me this:

    which shows that according to the expansion that pin the shall be assigned the value 6. 

Reply Children
No Data
Related