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
  • Try on this program, But I am using SEGGER SDK 1.7.1. Hear I have blinked P0.02 & P0.12. I hope this will help.

    #include <stdio.h>
    #include <zephyr.h>
    #include <device.h>
    #include <devicetree.h>
    #include <drivers/gpio.h>
    
    //sleep time
    #define SLEEP_TIME_MS   100
    
    void main(void)
    {
    	int cnt = 0;
    	const struct device *dev;
    
    	dev = device_get_binding("GPIO_0");
    	/* Set led0 pin as output */
    
    
    	gpio_pin_configure(dev, 2, GPIO_OUTPUT); //GPIO p0.03 == LED2
        gpio_pin_configure(dev,12, GPIO_OUTPUT); //GPIO P0.12
    
    	while (1) {
    		/* Set pin to HIGH/LOW every 1 second */
    		gpio_pin_set(dev, 2,cnt % 2);	//GPIO p0.03 == LED2
            gpio_pin_set(dev, 12, cnt % 2);
    
    		cnt++;
    		k_msleep(SLEEP_TIME_MS);
    	}
    }

Reply
  • Try on this program, But I am using SEGGER SDK 1.7.1. Hear I have blinked P0.02 & P0.12. I hope this will help.

    #include <stdio.h>
    #include <zephyr.h>
    #include <device.h>
    #include <devicetree.h>
    #include <drivers/gpio.h>
    
    //sleep time
    #define SLEEP_TIME_MS   100
    
    void main(void)
    {
    	int cnt = 0;
    	const struct device *dev;
    
    	dev = device_get_binding("GPIO_0");
    	/* Set led0 pin as output */
    
    
    	gpio_pin_configure(dev, 2, GPIO_OUTPUT); //GPIO p0.03 == LED2
        gpio_pin_configure(dev,12, GPIO_OUTPUT); //GPIO P0.12
    
    	while (1) {
    		/* Set pin to HIGH/LOW every 1 second */
    		gpio_pin_set(dev, 2,cnt % 2);	//GPIO p0.03 == LED2
            gpio_pin_set(dev, 12, cnt % 2);
    
    		cnt++;
    		k_msleep(SLEEP_TIME_MS);
    	}
    }

Children
No Data
Related