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.

  • Anyhow, these are the configuration files for my board as a reference:

    .dts file:

    /dts-v1/;
    #include <nordic/nrf52832_qfaa.dtsi>
    #include "auc_embedkit-pinctrl.dtsi"
    
    
    / {
    	model = "AUC EmbedKit";
    	compatible = "AUC,auc-embedkit";
    
    	chosen {
    		zephyr,sram = &sram0;
    		zephyr,flash = &flash0;
    		zephyr,code-partition = &slot0_partition;
    	};
    };
    
    
    
    /* enable all of the GPIOS tasks and events 
    &gpiote {
    	status = "okay";
    };*/
    
    /* enable GPIO 0 (which is a all gpio pins) 
    &gpio0 {
    	status = "okay";
    }; */
    
    
    
    &flash0 {
    	partitions {
    		compatible = "fixed-partitions";
    		#address-cells = <1>;
    		#size-cells = <1>;
    
    		boot_partition: partition@0 {
    			label = "mcuboot";
    			reg = <0x00000000 DT_SIZE_K(48)>;
    		};
    
    		slot0_partition: partition@c000 {
    			label = "image-0";
    			reg = <0x0000c000 DT_SIZE_K(220)>;
    		};
    
    		slot1_partition: partition@43000 {
    			label = "image-1";
    			reg = <0x00043000 DT_SIZE_K(220)>;
    		};
    
    		storage_partition: partition@7a000 {
    			label = "storage";
    			reg = <0x0007a000 DT_SIZE_K(24)>;
    		};
    	};
    };

    .yml file:

     

    identifier: auc_embedkit/nrf52832
    name: Custom Board auto generated by nRF Connect for VS Code
    vendor: AUC
    type: mcu
    arch: arm
    ram: 64
    flash: 512
    toolchain:
      - zephyr
    supported: []

    defconfig file:

    # Enable MPU
    CONFIG_ARM_MPU=y
    
    # Enable hardware stack protection
    CONFIG_HW_STACK_PROTECTION=y
    
    # Enable GPIO
    # CONFIG_GPIO=y
     

    .dtsi file:

    &pinctrl {
    };
     

    .cmake file:

    board_runner_args(jlink "--device=nRF52832_xxAA" "--speed=4000")
    
    set(OPENOCD_NRF5_SUBFAMILY "nrf52")
    board_runner_args(pyocd "--target=nrf52832" "--frequency=4000000")
    
    include(${ZEPHYR_BASE}/boards/common/nrfjprog.board.cmake)
    include(${ZEPHYR_BASE}/boards/common/nrfutil.board.cmake)
    include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake)
    include(${ZEPHYR_BASE}/boards/common/pyocd.board.cmake)
    include(${ZEPHYR_BASE}/boards/common/openocd-nrf5.board.cmake)
     

    board.yml file:

    board:
      name: auc_embedkit
      vendor: AUC
      socs:
        - name: nrf52832

    Kconfig. file:

    config BOARD_AUC_EMBEDKIT
    	select SOC_NRF52832_QFAA

    Kconfig.defconfig file:

    if BOARD_AUC_EMBEDKIT
    
    config BT_CTLR
    	default BT
    
    endif # BOARD_AUC_EMBEDKIT

    pre_dt_board.cmake file:

    # Suppress "unique_unit_address_if_enabled" to handle the following overlaps:
    # - power@40000000 & clock@40000000 & bprot@40000000
    # - acl@4001e000 & flash-controller@4001e000
    list(APPEND EXTRA_DTC_FLAGS "-Wno-unique_unit_address_if_enabled")

  • what frustrates me is that the "blinky" code example provided by Nordic is working successfully but my code doesn't.

  • The blinky works with the same dts/ overlay file?

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

    Try setting GPIO flag rather than 0.

  • Hi,

    Could it be that you are missing CONFIG_GPIO=y in prj.conf or similar?

Reply Children
No Data
Related