This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Simple GPIO Example - nRF9160 DK

Hello,

I'm having difficulty getting the GPIO set up in a way that behaves predictably on the nRF9160 DK. As far as I understand it, the nRF52840 is what controls the GPIO on the board, and the nRF9160 has to send its requests through it to interact. However, some pins on the 9160 are routed directly to things like the LEDs or switches.

My goal is very simple - I need to control pins 2, 4, 6, and 7 during execution. But when I define the pins, the results are not as expected:

struct device *gpio_dev;
gpio_dev = device_get_binding(DT_GPIO_P0_DEV_NAME);
gpio_pin_configure(gpio_dev, 2, GPIO_DIR_OUT);
gpio_pin_write(gpio_dev, 2, 1);

Unfortunately, this does not pull P0.02 high, and instead illuminates one of the LEDs. Any advice would be appreciated!

As an aside, I find that the documentation rarely provides the final answer to my problems. I'm a beginner, but I'm also someone who digs for answers for hours at a time until he solves his problems. I would love to learn about each aspect of programming this board, but I don't know where to begin. A lot of the time I depend on grepping through entire directory trees to find symbols that my IDE could not. I have no idea where to find a guide to using prj.conf, .overlay files, and so on. It doesn't seem that there is a good tutorial that can bring someone up to speed from the beginning. And yes, I have used the getting started tutorials, looked at every example, and read tons of forum posts. It's still necessary for me to make posts here, unfortunately. There should be very simple example given for every feature listed on the product page, even if they're redundant.

Parents
  • Hi,

    P0.02 is connected to LED 1 (Note that there is an error in the pin mapping in that it should state that LED 2 should be connected P0.03 etc..) . You can try P0.17, P0.18, or P0.19 instead. 

    Best regards

    Jared 

  • Hi Jared,

    Thanks for the response.

    I'm not sure how to set those pins. Should I address them as such: gpio_pin_write(gpio_dev, 17, 1);?

    Perhaps I'm doing something else wrong, because this has no effect. Is there some .overlay or prj.conf setting that I'll need to have active so I can access the GPIO from nRF9160 code?

    Justin

  • Hi,

    That should be correct. I've included a hex that toggles some LEDs and P0.17. Could you flash the hex to the board and see if it run?

    If it doesn't, then you are maybe missing secureboot?

    Jared 

    #include <zephyr.h>
    #include <device.h>
    #include <gpio.h>
    
    /* 1000 msec = 1 sec */
    #define SLEEP_TIME 	1000
    
    void main(void)
    {
    	int cnt = 0;
    	struct device *dev;
    
    	dev = device_get_binding("GPIO_0");
    	/* Set LED pin as output */
    	gpio_pin_configure(dev, 3, GPIO_DIR_OUT); //p0.03 == LED2
    	gpio_pin_configure(dev, 4, GPIO_DIR_OUT); //p0.04 == LED3
    	gpio_pin_configure(dev, 17, GPIO_DIR_OUT); //p0.17
    
    	while (1) {
    		/* Set pin to HIGH/LOW every 1 second */
    		gpio_pin_write(dev, 3, cnt % 2);	//p0.03 == LED2
    		gpio_pin_write(dev, 4, cnt % 2);	//p0.04 == LED3
    		gpio_pin_write(dev, 17, cnt % 2);	//p0.17 Toggling pin 17
    		cnt++;
    		k_sleep(SLEEP_TIME);
    	}
    }
    

    Togglepin.hex

Reply
  • Hi,

    That should be correct. I've included a hex that toggles some LEDs and P0.17. Could you flash the hex to the board and see if it run?

    If it doesn't, then you are maybe missing secureboot?

    Jared 

    #include <zephyr.h>
    #include <device.h>
    #include <gpio.h>
    
    /* 1000 msec = 1 sec */
    #define SLEEP_TIME 	1000
    
    void main(void)
    {
    	int cnt = 0;
    	struct device *dev;
    
    	dev = device_get_binding("GPIO_0");
    	/* Set LED pin as output */
    	gpio_pin_configure(dev, 3, GPIO_DIR_OUT); //p0.03 == LED2
    	gpio_pin_configure(dev, 4, GPIO_DIR_OUT); //p0.04 == LED3
    	gpio_pin_configure(dev, 17, GPIO_DIR_OUT); //p0.17
    
    	while (1) {
    		/* Set pin to HIGH/LOW every 1 second */
    		gpio_pin_write(dev, 3, cnt % 2);	//p0.03 == LED2
    		gpio_pin_write(dev, 4, cnt % 2);	//p0.04 == LED3
    		gpio_pin_write(dev, 17, cnt % 2);	//p0.17 Toggling pin 17
    		cnt++;
    		k_sleep(SLEEP_TIME);
    	}
    }
    

    Togglepin.hex

Children
Related