Issue with Configuring GPIO P1.XX on nRF52

Problem Description:

I am trying to configure and control an LED connected to pin P1.07 on my nRF52 device, but I am experiencing issues. When I configure pin P1.07 as an output, I am unable to turn the LED on and off. However, if I use a pin on port P0 (e.g., P0.07), everything works correctly.

I noticed that the ports I had connected to P1 on my hardware were not functioning properly. I had conducted tests with the nRF52840 demo board using pins on P0, but due to layout issues, I had to use the external pins connected to P1 when I built the hardware. Now, I have the circuit and software ready, but the P1 ports are not working.

To isolate the problem, I stripped down all my code and tried to simply turn on an LED connected to pin P1.07.

Current Configuration:

  • SDK: Zephyr 2.6.1
  • Board: Custom board for my hardware
  • Example Project: Nordic BLE HIDS Keyboard
  • GPIO Configuration: I have enabled CONFIG_GPIO_NRF_P1=y in the prj.conf file.

Code Used:

#include <hal/nrf_gpio.h> // Include if using nRF specific features
#define LED_1 NRF_GPIO_PIN_MAP(1, 7)

void main(void)
{
    const struct device *dev = device_get_binding("GPIO_1");
    gpio_pin_configure(dev, LED_1, GPIO_OUTPUT);

    while (1)
    {
        gpio_pin_set(dev, LED_1, 1);
        k_sleep(K_MSEC(500));
        gpio_pin_set(dev, LED_1, 0);
        k_sleep(K_MSEC(500));
    }
}

Checks Performed:

  • I verified that CONFIG_GPIO_NRF_P1=y is enabled in the prj.conf file.
  • I checked the Device Tree to ensure that gpio1 is enabled:

&gpio1 {
    status = "okay";
};


Symptoms:

  • The LED does not turn on when using pin P1.07.
  • The code works correctly when using a pin on port P0 (e.g., P0.07).

Questions:

  1. Are there additional configurations necessary to use the pins on port P1 on nRF52 devices?
  2. Do I need to modify other settings in the Device Tree or elsewhere to properly enable port P1?

Thank you in advance for any help or suggestions!

  • Hi,
    You configuration seems correct. Could you confirm the following too?


    1. Please make sure that the P1.07 is not being used by any other peripherals etc.

    2. Is your LED properly connected to the P1.07? Maybe try changing to another pin on the same port to rule out any hardware issues with P1.07?

    3. You mention that when connecting to P0.07 the LED lights u. Do you use the same LED? Just to rule out that it might be an LED issue.

    Regards,

    Priyanka

  • Hi Priyanka,

    1. Please make sure that the P1.07 is not being used by any other peripherals etc.
    • Not used, the board is custom so there should be no problem.
    1. Is your LED properly connected to the P1.07? Maybe try changing to another pin on the same port to rule out any hardware issues with P1.07?
    • The LED is connected with the ground to GND and the positive to the pin.
    1. You mention that when connecting to P0.07 the LED lights up. Do you use the same LED? Just to rule out that it might be an LED issue.
    • The LED is the same, yes, I just changed the port from zero to one.

    I have tried other solutions and this seems to work, the problem is that it seems to work for the LEDs but not for the GPIOs. Do I need to use other settings or should these be the correct ones?

    I read somewhere that the pins of the GPIO1 port are not configurable in the same way as those of GPIO0. I wanted to understand if this is true. In my project, I was using the dk_buttons_and_leds file to manage LEDs and buttons. I don't think it works properly, or is it enough to make some modifications to the file?

    #include <hal/nrf_gpio.h> // Include if using nRF specific features
    // #define LED_BATTERY_75_100 NRF_GPIO_PIN_MAP(0, 17)
    #define LED_BATTERY_75_100 NRF_GPIO_PIN_MAP(1, 17)
    
    /* -------------------------------------------------------------------------- */
    /*                                     LED                                    */
    /* -------------------------------------------------------------------------- */
    
    void TurnLEDOn(uint32_t led_pin)
    {
        // nrf_gpio_cfg_output(led_pin);
        nrf_gpio_pin_clear(led_pin);
    }
    
    void TurnLEDOff(uint32_t led_pin)
    {
        // nrf_gpio_cfg_output(led_pin);
        nrf_gpio_pin_set(led_pin);
    }
    
    void WriteLEDState(uint32_t led_pin, uint32_t state)
    {
        nrf_gpio_pin_write(led_pin, state);
    }
    
    void ToggleLED(uint32_t led_pin)
    {
        nrf_gpio_cfg_output(led_pin);
        uint32_t state = nrf_gpio_pin_out_read(led_pin);
        if (state == 0)
        {
            nrf_gpio_pin_set(led_pin);
        }
        else
        {
            nrf_gpio_pin_clear(led_pin);
        }
    }
    
    /* -------------------------------------------------------------------------- */
    /*                                    MAIN                                    */
    /* -------------------------------------------------------------------------- */
    
    void configure_gpio(void)
    {
        // Configure GPIOs used by the application
    }
    
    void main(void)
    {
        configure_gpio(); // Configure the GPIOs used by the application
        nrf_gpio_cfg_output(LED_BATTERY_75_100);
    
        while (1)
        {
            TurnLEDOff(LED_BATTERY_75_100);
            k_sleep(K_MSEC(500));
            TurnLEDOn(LED_BATTERY_75_100);
            k_sleep(K_MSEC(500)); // Wait for 500 ms
        }
    }
    

    Thanks for your response.

  • Hi,

    Yes, it's true that the configuration of GPIO1 port pins might not be as straightforward as those of GPIO0. This is because the buttons and LEDs are usually connected to the same GPIO port in our official DKs. If the buttons are connected to a different GPIO port (e.g., GPIO_1) than the LEDs, a new device driver binding or device pointer is required. Please take a look at this: https://academy.nordicsemi.com/courses/nrf-connect-sdk-fundamentals/lessons/lesson-2-reading-buttons-and-controlling-leds/topic/exercise-1-2/ 

    In the dk_buttons_and_leds file, you might need to make some modifications to properly configure the GPIO1 port pins. For instance, you might need to define a new device pointer for the GPIO1 port and use it to initialize and configure the GPIO1 port pins. Please take a look at this thread: https://devzone.nordicsemi.com/f/nordic-q-a/92870/how-to-link-nrfx_gpiote-and-gpio1-port-on-zephyr?ReplyFilter=Answers&ReplySortBy=Answers&ReplySortOrder=Descending 

    -Priyanka

  • Thank you for your responses and sorry for the delay in getting back to you. I've had a few hectic days at work, but I wanted to share that I found a solution to my problem.

    Here's the working code (example):

    /* Define the LED node */
    #define LED_NODE DT_NODELABEL(led_green)
    static struct gpio_dt_spec led = GPIO_DT_SPEC_GET_OR(LED_NODE, gpios, {1});
    
    /* Function to configure the LEDs */
    int configure_leds(void)
    {
        int ret;
    
        /* Check if the LED device is ready */
        if (!gpio_is_ready_dt(&led))
        {
            printk("Error %d: LED device %s is not ready; ignoring it\n", ret, led.port->name);
            return -1;
        }
    
        /* Configure the LED pin as output */
        ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT);
        if (ret != 0)
        {
            printk("Error %d: failed to configure pin %d on LED device %s\n", ret, led.pin, led.port->name);
            return ret;
        }
    
        return 0;
    }
    
    void main(void)
    {
        int ret;
    
        /* Initialize the LEDs */
        ret = configure_leds();
        if (ret != 0)
        {
            printk("Failed to configure LEDs\n");
            return;
        }
    
        /* Main loop */
        while (1)
        {
            /* Toggle the LED state */
            gpio_pin_toggle_dt(&led);
            k_sleep(K_MSEC(1000));  // Sleep for 1 second
        }
    }
    
    /* Additional commands to turn the LED on and off */
    gpio_pin_set_dt(&led, 1); // LED on
    gpio_pin_set_dt(&led, 0); // LED off



    This solution works for both GPIO0 and GPIO1.

    Thank you again for your assistance!

    Best regards.

  • That's great. Happy to hear that the issue is resolved. Closing the ticket. Slight smile

    -Priyanka

Related