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

BSP and GPIOTE [NRF_ERROR_INVALID_STATE]

Combinig a simplified version of the bsp example and the pin_change_int example I noticed that I'm running into an "[NRF_ERROR_INVALID_STATE]" error caused by nrf_drv_gpiote_init() function when the function is called after bsp_init(). When I do it vice versa It works fine. In another DevZone post I read that it should also work fine in the initial order when the pin used for the GPIOTE event is not a LED or BUTTON pin. So I'm using PIN 27 for the GPIOTE Event but still running into the error.

Does this mean that nrf_drv_gpiote_init() can never be called after bsp_init() or am I still doing something wrong?

#include <stdbool.h>
#include <stdint.h>
#include "boards.h"
#include "bsp.h"
#include "app_timer.h"
#include "nordic_common.h"
#include "nrf_error.h"
#include "nrf_drv_gpiote.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#define PIN_IN 	27

/**@brief Function for handling bsp events.
 */
void bsp_evt_handler(bsp_event_t evt)
{
    NRF_LOG_INFO("BSP Event");
}

void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
    NRF_LOG_INFO("GPIOTE Event.");
}

static void gpio_init(void)
{
    ret_code_t err_code;

    err_code = nrf_drv_gpiote_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
    in_config.pull = NRF_GPIO_PIN_PULLUP;

    err_code = nrf_drv_gpiote_in_init(PIN_IN, &in_config, in_pin_handler);
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_event_enable(PIN_IN, true);
}

/**@brief Function for initializing low frequency clock.
 */
void clock_initialization()
{
    NRF_CLOCK->LFCLKSRC            = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos);
    NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_LFCLKSTART    = 1;

    while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
    {
        // Do nothing.
    }
}

/**@brief Function for initializing bsp module.
 */
void bsp_configuration()
{
    uint32_t err_code;

    err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_evt_handler);
    APP_ERROR_CHECK(err_code);
}

void log_init()
{
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();	
}


/**
 * @brief Function for application main entry.
 */
int main(void)
{
    clock_initialization();
    app_timer_init();
		log_init();
		NRF_LOG_INFO("BSP example started.");
	
		//gpio_init() before bsp_configuration() works
		bsp_configuration();
		gpio_init();

    while (true)
    {
        NRF_LOG_FLUSH();
        __SEV();
        __WFE();
        __WFE();
    }
}

Parents
  • Hi!

    The bsp_init() has already initialized the GPIOTE driver, so the reason it's not working is because when you call nrf_drv_gpiote_init() after calling bsp_init(), the function returns the error code NRF_ERROR_INVALID_STATE, which means the driver has already been initialized. You can read more about this here.

    Does this mean that nrf_drv_gpiote_init() can never be called after bsp_init() or am I still doing something wrong?

    You can, as long as you don't call APP_ERROR_CHECK(err_code) afterwards. 

    Best regards,

    Heidi

Reply
  • Hi!

    The bsp_init() has already initialized the GPIOTE driver, so the reason it's not working is because when you call nrf_drv_gpiote_init() after calling bsp_init(), the function returns the error code NRF_ERROR_INVALID_STATE, which means the driver has already been initialized. You can read more about this here.

    Does this mean that nrf_drv_gpiote_init() can never be called after bsp_init() or am I still doing something wrong?

    You can, as long as you don't call APP_ERROR_CHECK(err_code) afterwards. 

    Best regards,

    Heidi

Children
Related