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

Change the state of a LED when there is a rising edge of a button

Hi, 

I am trying to change the state of a LED whenever there is a rising edge of a button

Here is my code :

/*
 * Copyright (c) 2016 Open-RnD Sp. z o.o.
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdbool.h>
#include "nrf.h"
#include "nrfx_gpiote.h"
#include <drivers/gpio.h>


#include <zephyr.h>
#include <device.h>
#include <drivers/gpio.h>
#include <sys/util.h>
#include <sys/printk.h>
#include <inttypes.h>

#include <devicetree.h>

#define SLEEP_TIME_MS	1000

/* The devicetree node identifier for the "led0" alias.*/ 
#define LED0_NODE DT_ALIAS(led0)

#if DT_NODE_HAS_STATUS(LED0_NODE, okay)
#define LED0	DT_GPIO_LABEL(LED0_NODE, gpios)
#define PIN	DT_GPIO_PIN(LED0_NODE, gpios)
#define FLAGS	DT_GPIO_FLAGS(LED0_NODE, gpios)
#else
/* A build error here means your board isn't set up to blink an LED. */
#error "Unsupported board: led0 devicetree alias is not defined"
#define LED0	""
#define PIN	0
#define FLAGS	0
#endif 

#define SW0_NODE	DT_ALIAS(sw0)

#if DT_NODE_HAS_STATUS(SW0_NODE, okay)
#define SW0_GPIO_LABEL	DT_GPIO_LABEL(SW0_NODE, gpios)
#define SW0_GPIO_PIN	DT_GPIO_PIN(SW0_NODE, gpios)
#define SW0_GPIO_FLAGS	(GPIO_INPUT | DT_GPIO_FLAGS(SW0_NODE, gpios))
#else
#error "Unsupported board: sw0 devicetree alias is not defined"
#define SW0_GPIO_LABEL	""
#define SW0_GPIO_PIN	0
#define SW0_GPIO_FLAGS	0
#endif


void Initialize_Led();
void Initialize_Button();
const struct device *devled;
const struct device *devbutton;



nrfx_gpiote_in_config_t  configinterruption = NRFX_GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
configinterruption.pull =  NRF_GPIO_PIN_PULLUP;

void input_pin_handle(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{   
  bool led_is_on = true;

  gpio_pin_set(devled, PIN, (int)led_is_on);
  led_is_on = !led_is_on;
  
}


int main (void)
{ 
    
    Initialize_Led();
    Initialize_Button(); 
    nrfx_gpiote_in_init(SW0_GPIO_PIN, &configinterruption , input_pin_handle);
    nrfx_gpiote_in_event_enable(SW0_GPIO_PIN, true);

    while(1)
    {
     
    }
}

void Initialize_Led()
{
        int ret1;

	devled = device_get_binding(LED0);
	if (devled == NULL) {
		return;
	}

	ret1 = gpio_pin_configure(devled, PIN, GPIO_OUTPUT_ACTIVE | FLAGS); 
	if (ret1 < 0) {
		return;
	}
}

void Initialize_Button()
{
        int ret2;

	devbutton = device_get_binding(SW0_GPIO_LABEL);
	if (devbutton == NULL) {
		return;
	}

	ret2 = gpio_pin_configure(devbutton, SW0_GPIO_PIN, SW0_GPIO_FLAGS);
	if (ret2 != 0) {
		return;
	}

        ret2 = gpio_pin_interrupt_configure(devbutton, SW0_GPIO_PIN, GPIO_INT_EDGE_RISING);
	if (ret2 != 0) {
		return;
	}

}

When I build, I have this error :

By the way, I am not sure if 

nrfx_gpiote_in_init(SW0_GPIO_PIN, &configinterruption , input_pin_handle);
nrfx_gpiote_in_event_enable(SW0_GPIO_PIN, true);

is a good solution for what I want.

I took example from \ncs\v1.5.1\zephyr\samples\basic\button but what I want to do is : LED off -> Button pressed and released -> LED on -> Button pressed and released -> LED off

Anyone has any suggestion or example of code? I am working on nRF52840 and NCS

  • The C programming language does not allow executable statements outside functions:

    nrfx_gpiote_in_config_t  configinterruption = NRFX_GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
    configinterruption.pull =  NRF_GPIO_PIN_PULLUP;  <<--- THIS!
    
    void input_pin_handle(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {   
    

  • As suggested by , I think code won't even compile.

    Here is a code you can try. You may not need nrf_gpio_pin_read since gpiote it is configured for rising edge, see what works.

    #define BUTTON 4
    
    
    void input_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
    	if (pin == BUTTON)
        {
    		if (nrf_gpio_pin_read(BUTTON))
    		{
    			// High state
    		}
    		else
    		{
    			// Low state
    		}
    	}
    }
    
    
    static void gpiote_init(void)
    {
        ret_code_t err_code;
    
        if (!nrf_drv_gpiote_is_init())
        {
            err_code = nrf_drv_gpiote_init();
            APP_ERROR_CHECK(err_code);
        }
    
    	nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_LOTOHI(true);
        in_config = NRF_GPIO_PIN_PULLDOWN;
    
        err_code = nrf_drv_gpiote_in_init(BUTTON, &in_config, in_pin_handler);
        APP_ERROR_CHECK(err_code);
    	
    	nrf_drv_gpiote_in_event_enable(BUTTON, true);
    }
    
    int main(void)
    {
        ..
        ..
        gpiote_init();
        ..
        ..
    }

Related