[GPIO] Toggling of GPIO pin (HIGH, LOW)

I am programming with the nRF52840DK. [Toolchain Manager: v1.3.0, IDE: Visual Studio Code (VSCode), SDK: ncs v2.6.0, window11 pro]

I simply want to toggle the 0.18 pin between HIGH and LOW states.
I'm trying to test this, but it gets stuck in either HIGH or LOW state and doesn't toggle. What could be the problem?

Below is my main.c code.

#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/sys/printk.h>
#include <zephyr/device.h>
#define SLEEP_TIME_MS   1000
#define GPIO_PIN        18 
#define GPIO0_NODE DT_NODELABEL(gpio0)
static const struct device *gpio_dev = DEVICE_DT_GET(GPIO0_NODE);
int main(void)
{
    int ret;
    if (!device_is_ready(gpio_dev)) {
        printk("Error: GPIO device is not ready\n");
        return -1;
    }
    ret = gpio_pin_configure(gpio_dev, GPIO_PIN, GPIO_OUTPUT | GPIO_ACTIVE_HIGH);
    if (ret < 0) {
        printk("Error %d: Failed to configure GPIO pin %d\n", ret, GPIO_PIN);
        return -1;
    }
    printk("GPIO toggling demo started on pin %d\n", GPIO_PIN);
    while (1) {
        ret = gpio_pin_toggle(gpio_dev, GPIO_PIN);
        if (ret < 0) {
            printk("Error %d: Failed to toggle GPIO pin %d\n", ret, GPIO_PIN);
            return -1;
        }
        ret = gpio_pin_get(gpio_dev, GPIO_PIN);
        if (ret < 0) {
            printk("Error %d: Failed to read GPIO pin %d\n", ret, GPIO_PIN);
            return -1;
        }
        printk("GPIO pin %d toggled, current state: %s\n", 
               GPIO_PIN, ret ? "HIGH" : "LOW");

        k_msleep(SLEEP_TIME_MS);
    }
    return 0;
}

Below is my prj.conf file.

CONFIG_GPIO=y
CONFIG_SERIAL=y
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y
CONFIG_PRINTK=y

Below is my .overlay file.

&gpio0 {
    custom-gpio {
        gpio-hog;
        gpios = <18 GPIO_ACTIVE_LOW>;
        output-low;
        line-name = "Custom GPIO on P0.18";
    };
};

Below is debugging.

*** Booting nRF Connect SDK v3.5.99-ncs1 ***
GPIO toggling demo started on pin 18
GPIO pin 18 toggled, current state: HIGH
GPIO pin 18 toggled, current state: HIGH
GPIO pin 18 toggled, current state: HIGH
GPIO pin 18 toggled, current state: HIGH
GPIO pin 18 toggled, current state: HIGH
GPIO pin 18 toggled, current state: HIGH
GPIO pin 18 toggled, current state: HIGH
GPIO pin 18 toggled, current state: HIGH
GPIO pin 18 toggled, current state: HIGH
GPIO pin 18 toggled, current state: HIGH
GPIO pin 18 toggled, current state: HIGH
GPIO pin 18 toggled, current state: HIGH
GPIO pin 18 toggled, current state: HIGH
GPIO pin 18 toggled, current state: HIGH

Parents Reply
  • Yes! Looking at the nrf52840dk_nrf52840.dts file, it seems I didn't pay attention to the 'gpio-reserved-ranges' variable below.

    Thank you!


    &gpio0 {
    	status = "okay";
    	gpio-reserved-ranges = <0 2>, <6 1>, <8 3>, <17 7>;
    	gpio-line-names = "XL1", "XL2", "AREF", "A0", "A1", "RTS", "TXD",
    		"CTS", "RXD", "NFC1", "NFC2", "BUTTON1", "BUTTON2", "LED1",
    		"LED2", "LED3", "LED4", "QSPI CS", "RESET", "QSPI CLK",
    		"QSPI DIO0", "QSPI DIO1", "QSPI DIO2", "QSPI DIO3","BUTTON3",
    		"BUTTON4", "SDA", "SCL", "A2", "A3", "A4", "A5";
    };
    

Children
No Data
Related