LED implementation on nrF9160 with nRF connect 2.1.2

Hi all ,

I had my code working on nRF connect 1.6.0 and now had to upgrade it to work with nRF connect 2.1.2.

I read the migration guide for  nRF connect 1.x.x to 2.x.x  but still can't fully understand how to port my working code on nRF connect 1.6.0 that controlled the LEDs to nRF connect 2.1.2

Here is the nRF connect 1.6.0:

void init_leds(void)
{
        /* GPIOs:  p0.02 == RED */
        red_gpio = DT_GPIO_PIN(DT_ALIAS(LED_RED_LABEL), gpios);

        /* GPIOs:  p0.03 == BLUE */
        blue_gpio=  DT_GPIO_PIN(DT_ALIAS(LED_BLUE_LABEL), gpios);

   dev_red = device_get_binding(DT_GPIO_LABEL(DT_ALIAS(LED_RED_LABEL), gpios));
        NULL_ERROR_CHECK_RETURN_VOID(dev_red);
        dev_blue = device_get_binding(DT_GPIO_LABEL(DT_ALIAS(LED_BLUE_LABEL), gpios));
   NULL_ERROR_CHECK_RETURN_VOID(dev_blue);
       
        gpio_pin_configure(dev_red, red_gpio, GPIO_OUTPUT_ACTIVE | DT_GPIO_FLAGS(DT_ALIAS(LED_RED_LABEL), gpios));
        gpio_pin_configure(dev_blue, blue_gpio, GPIO_OUTPUT_ACTIVE | DT_GPIO_FLAGS(DT_ALIAS(LED_BLUE_LABEL), gpios));
}

void led_control(int led, int cmd)
{  
    switch (led)
    {
        case LED_RED:
              gpio_pin_set(dev_red, red_gpio, cmd);
              break;
        case LED_BLUE:
              gpio_pin_set(dev_blue, blue_gpio, cmd);
              break;
        default:
              LOG_WRN("GPIO %d is not connected to any LED",led);
            break;
    }  
}
Can someone guide me what to do ?
Thanks in advance ,
Rafalino
Parents Reply Children
  • Hello Einar,

    The main issue that I have is how do I get the device that corresponds to led0, led1.

    I am able to get the GPIOs using : 

          /* GPIOs:  p0.02 == RED */
          red_gpio = DT_GPIO_PIN(DT_NODELABEL(led0), gpios);
     
          /* GPIOs:  p0.03 == BLUE */
          blue_gpio=  DT_GPIO_PIN(DT_NODELABEL(led1), gpios);

    How do I get the relevant device id?

  • Hello Einar,

    I will try to clarify Slight smile

    • I have chosen a nrf9160_ns board.
    • nRF connect 2.1.2.
    • I want to control led0 (Red LED on GPIO 2 ) and led1 (Blue LED on GPIO 3) 
    • I have followed the example here : https://academy.nordicsemi.com/topic/gpio-generic-api/
    • Based on that example I wrote : 
      struct gpio_dt_spec red_led_gpio = GPIO_DT_SPEC_GET(DT_NODELABEL(led0), gpios);
      The error messages that I get during my build are:
      ../src/generic_app.c: In function 'init_leds':
      C:\ncs\v2.1.2\zephyr\include\zephyr\drivers\gpio.h:337:2: error: expected expression before '{' token
      337 | { \
      | ^
      C:\ncs\v2.1.2\zephyr\include\zephyr\drivers\gpio.h:374:2: note: in expansion of macro 'GPIO_DT_SPEC_GET_BY_IDX'
      374 | GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, 0)
      | ^~~~~~~~~~~~~~~~~~~~~~~
      generic_app.c:98:22: note: in expansion of macro 'GPIO_DT_SPEC_GET'
      98 | red_led_gpio = GPIO_DT_SPEC_GET(DT_NODELABEL(led0), gpios);
      | ^~~~~~~~~~~~~~~~
      Any suggestions ?
  • Hi

    Based on your error message here, I would double check for any missing parentheses or semicolons etc in your code.

    If you don't find anything like that missing, could you please share the 'leds' node from your generated devicetree found in build/zephyr/zephyr.dts?

    -Einar

  • Hello Einar ,

    It turned out to be something with parentheses since when I changed 

    red_led_gpio = GPIO_DT_SPEC_GET_BY_IDX(DT_N_S_leds_S_led_0, gpios, 0);

    into :
    red_led_gpio.port = DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(DT_N_S_leds_S_led_0, gpios, 0));
         red_led_gpio.pin = DT_GPIO_PIN_BY_IDX(DT_N_S_leds_S_led_0, gpios, 0);
         red_led_gpio.dt_flags = DT_GPIO_FLAGS_BY_IDX(DT_N_S_leds_S_led_0, gpios, 0);


     It eliminated the erros.
     
    Thanks you for your assistance.
Related