Toggle LED in custom application

I'm trying to do something extremely simple but can't get it to work. Through the dev academy, I successfully loaded up and executed exercise 1. However, when I then try to "Create a New Application" (rather than "Add and existing appliction), running the exact same main.c code, I am not able to toggle the LEDs and none of the GPIOs seem to be configured. I'm guessing this has something to do with the generated files, but I don't know where to start looking. Please help!

BTW - I am able to attach with the debugger and see that the initialization seems to be completing without error and function call to the gpio toggle also returns with no error. main.c code for both applications is the following:

const struct device *dev0;
    const int PIN=29;
    bool led_is_on = true;
    int ret;

    dev0 = DEVICE_DT_GET(DT_NODELABEL(gpio0));

    if (dev0==NULL) {
        printk("device not found");
        return;
    }

    if (!device_is_ready(dev0)) {
        printk("GPIO controller not ready");
        return -ENODEV;
    }
    gpio_pin_configure(dev0, PIN, GPIO_OUTPUT);
    while(1) {
        gpio_pin_toggle(dev0, PIN);
        k_msleep(SLEEP_TIME_MS);
    }
  • Update - I have recreated the application three different ways using different Application templates.

    1. Created with nrf/samples/nrf5340/empty_app_core - LED toggle does not work.
    2. Created with zephry/samples/hello_world - LED does not work.
    3. Created with zephyr/samples/basic/blinky - LED works.

    I should note that it is not just the LED that doesn't toggle, I also have SPI transciever code and I can see that the GPIOs associated with clock, chip select, and data lines also do not work (i.e. they are always held low) in 1 and 2 above, but work in 3 above. 

    So my pins are under control now, which is great. But so that I can understand, what is going on "behind the scenes" that causes the first two applications to fail and the third to work? What options or configurations do I need to enable to make 1 and 2 work?

    Thanks,

    Jamie

  • I applied the following modifications to the hello world sample in NCS v2.1.0:

    diff --git a/samples/hello_world/prj.conf b/samples/hello_world/prj.conf
    index b2a4ba5910..d63ffc4391 100644
    --- a/samples/hello_world/prj.conf
    +++ b/samples/hello_world/prj.conf
    @@ -1 +1,2 @@
     # nothing here
    +CONFIG_GPIO=y
    diff --git a/samples/hello_world/src/main.c b/samples/hello_world/src/main.c
    index 8676d8940b..0e61e7a84d 100644
    --- a/samples/hello_world/src/main.c
    +++ b/samples/hello_world/src/main.c
    @@ -5,8 +5,33 @@
      */
     
     #include <zephyr/zephyr.h>
    +#include <zephyr/drivers/gpio.h>
    +
    +#define SLEEP_TIME_MS 500
     
     void main(void)
     {
     	printk("Hello World! %s\n", CONFIG_BOARD);
    +	const struct device *dev0;
    +    const int PIN=29;
    +    int ret;
    +
    +    dev0 = DEVICE_DT_GET(DT_NODELABEL(gpio0));
    +
    +    if (dev0==NULL) {
    +        printk("device not found");
    +    }
    +
    +    if (!device_is_ready(dev0)) {
    +        printk("GPIO controller not ready");
    +    }
    +
    +    ret = gpio_pin_configure(dev0, PIN, GPIO_OUTPUT);
    +    if(ret){
    +        printk("gpio_pin configure failed: %d\n", ret);
    +    }
    +    while(1) {
    +        gpio_pin_toggle(dev0, PIN);
    +        k_msleep(SLEEP_TIME_MS);
    +    }
     }
    

    Then I programmed it onto the board nrf5340dk_nrf5340_cpuapp, and LED2 blinked as expected.

    Here is the complete sample:

    2248.hello_world_blinky.zip

    Best regards,

    Simon

  • Hi. Going back to the application generated from the hello world template, I have verified that the prj.conf has the following:

    CONFIG_GPIO=y
    CONFIG_SPI=y
    main.c has the following includes:
    #include <zephyr.h>
    #include <drivers/gpio.h>
    #include <drivers/spi.h>
    Also, I added the code snippet above but still no LED. I went into debug mode to add a breakpoint at line 27 (from your code snippet above) but it would not let me add a breakpoint there. In fact, if I click to add a breakpoint there, it automatically inserts it at line 34 above, indicating that it must have optimized out the lines for getting the device tree reference. Any ideas?
    -Jamie
  • Hmm.. that is strange. What nRF5340 DK version do you have?

    Are you able to get any output from any other pins? Try toggling P0.25 or P0.26 for example and connect a logic analyzer to the header and check if you can see any activity.

    jmilliken said:
    I went into debug mode to add a breakpoint at line 27 (from your code snippet above) but it would not let me add a breakpoint there. In fact, if I click to add a breakpoint there, it automatically inserts it at line 34 above, indicating that it must have optimized out the lines for getting the device tree reference. Any ideas?

    Try setting CONFIG_DEBUG_OPTIMIZATIONS=y in the prj.conf and do a pristine build.

    Best regards,

    Simon

  • Okay, I will try this tonight and let you know. To answer your question, I am using the NORA-B106 EVK from U-blox:

    www.digikey.com/.../13534375

Related