[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 Children
  • Hi,

    I don't see any issue with how you toggle the pin, and when I test on a DK (I just changed the pin number to one connected to a LED), it toggles as expected. How did you see that toggling does not work?

  • I am measuring signals with an oscilloscope.

    I connected wires to the pins and directly attached them to the alligator clips of the oscilloscope.

    pin - wire - alligator clip

    The input impedance of the oscilloscope is 1M Ω

  • I see. I expect if you test with pin 13 you will see it work. Pin 18 requiers a few additional changes, as it is the reset pin. First of all you need to configure your project to not enable it as a pin reset, and if using VS code, and you also need to configure VS code to not enable pin reset when programming (or if building from the command line, pass --softreset to west. This is alle explained in this thread.

    Secondly, pin 18 along with other pins are routed to the onboard debugger when debugging, so you need to disconnect the debugger (for intsance with the nRF only switch on the top of the DK in order to be able to access it on the DK (you can see this in the GPIO table on the back side of the DK).

  • 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";
    };
    

Related