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

nRF52 standby with sd_app_evt_wait doesn't work

I write a source code to test led and button driver. it's work with a loop but when add sd_app_evt_wait() to use low power mode of nRF52. I suppose that I forgot something but I can't find. Someone knows this issue ?

Thanks

My source code :

#include <stdbool.h>
#include <stdint.h>

#include "nrf_drv_gpiote.h"

#include "config/nrf_drv_config.h"
#include "bsp.h"
#include "app_timer.h"
#include "nrf_drv_clock.h"
#include "nordic_common.h"
#include "softdevice_handler.h"

#define GPIO_PIN_1		1
#define GPIO_PIN_2		2
#define GPIO_PIN_3		3
#define GPIO_PIN_4		4
#define GPIO_PIN_5		5
#define GPIO_PIN_6		6
#define GPIO_PIN_7		7
#define GPIO_PIN_8		8
#define GPIO_PIN_9		9
#define GPIO_PIN_10		10
#define GPIO_PIN_11		11
#define GPIO_PIN_12		12
#define GPIO_PIN_13		13
#define GPIO_PIN_14		14
#define GPIO_PIN_15		15
#define GPIO_PIN_16		16
#define GPIO_PIN_17		17
#define GPIO_PIN_18		18
#define GPIO_PIN_19		19
#define GPIO_PIN_20		20


#define APP_TIMER_PRESCALER      		0                           /**< Value of the RTC1 PRESCALER register. */
#define APP_TIMER_MAX_TIMERS            6                           /**< Maximum number of simultaneously created timers. */
#define APP_TIMER_OP_QUEUE_SIZE         4

#define BUTTON_DETECTION_DELAY          APP_TIMER_TICKS(50, APP_TIMER_PRESCALER)    /**< Delay from a GPIOTE event until a button is reported as pushed (in number of timer ticks). */


/**@brief Function for doing power management.
 */
static void power_manage(void)
{
    uint32_t err_code = sd_app_evt_wait();
    APP_ERROR_CHECK(err_code);
}

/**@brief Function for handling events from the button handler module.
 *
 * @param[in] pin_no        The pin that the event applies to.
 * @param[in] button_action The button action (press/release).
 */
static void button_event_handler(uint8_t pin_no, uint8_t button_action)
{
	if (pin_no == GPIO_PIN_13)
	{
		if (button_action == APP_BUTTON_PUSH)
			nrf_drv_gpiote_out_toggle(GPIO_PIN_17);
	}
}

/**@brief Function for the Timer initialization.
 *
 * @details Initializes the timer module.
 */
static void timers_init(void)
{
    // Initialize timer module, making it use the scheduler
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
}

/**@brief Function for initializing the button handler module.
 */
static void buttons_init(void)
{
    uint32_t err_code;

    //The array must be static because a pointer to it will be saved in the button handler module.
    static app_button_cfg_t buttons[] =
    {
        {GPIO_PIN_13, false, BUTTON_PULL, button_event_handler}
    };

    err_code = app_button_init(buttons, sizeof(buttons) / sizeof(buttons[0]),
                               BUTTON_DETECTION_DELAY);
    APP_ERROR_CHECK(err_code);
}

/**@brief Function for initialization oscillators.
 */
void clock_initialization()
{
	/* Start 16 MHz crystal oscillator */
	NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
	NRF_CLOCK->TASKS_HFCLKSTART    = 1;

	/* Wait for the external oscillator to start up */
	while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
	{
		// Do nothing.
	}

	/* Start low frequency crystal oscillator for app_timer(used by bsp)*/
	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 application main entry.
 */
int main(void)
{
	uint32_t err_code=0;

	err_code = nrf_drv_clock_init();
	APP_ERROR_CHECK(err_code);

    nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC;

    // Initialize the SoftDevice handler module.
    SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL);
    err_code = sd_softdevice_disable();
    APP_ERROR_CHECK(err_code);

	timers_init();
	buttons_init();

	//Clock Init
	clock_initialization();

	if(!nrf_drv_gpiote_is_init())
	{
	    err_code = nrf_drv_gpiote_init();
	}

	nrf_drv_gpiote_out_config_t config_gpio_out = GPIOTE_CONFIG_OUT_SIMPLE(NRF_GPIOTE_INITIAL_VALUE_LOW);
	config_gpio_out.init_state = NRF_GPIOTE_INITIAL_VALUE_LOW;
	err_code = nrf_drv_gpiote_out_init(GPIO_PIN_17, &config_gpio_out);

	err_code = app_button_enable();
	APP_ERROR_CHECK(err_code);
    
    //Enter main loop.
    for (;; )
    {
        power_manage();
    }
	return (int)err_code;
}


/**
 * @}
 */
Related