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

nrf52832 system on sleep from rtc

Hi,

    i am working with nRF52832, i need to us sleep.it may be do somthing and sleep 40ms,rtc wake up it and do somthing and cycle.

    i see the rtc example but it may be some thing wrong.i do not have 32.768Khz crystal oscillator. do you have the same example?

    the code

    

const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(0); /**< Declaring an instance of nrf_drv_rtc for RTC0. */
static int mcuInSleep = 0;

/** @brief: Function for handling the RTC0 interrupts.
 * Triggered on TICK and COMPARE0 match.
 */
static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
{
    if (int_type == NRF_DRV_RTC_INT_COMPARE0)
    {
        //nrf_gpio_pin_toggle(COMPARE_EVENT_OUTPUT);
    }
    else if (int_type == NRF_DRV_RTC_INT_TICK)
    {
        //nrf_gpio_pin_toggle(TICK_EVENT_OUTPUT);
		mcuInSleep = false;
    }
}

/** @brief Function configuring gpio for pin toggling.
 */
static void leds_config(void)
{
    bsp_board_init(BSP_INIT_LEDS);
}

/** @brief Function starting the internal LFCLK XTAL oscillator.
 */
static void lfclk_config(void)
{
    ret_code_t err_code = nrf_drv_clock_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_clock_lfclk_request(NULL);
}

/** @brief Function initialization and configuration of RTC driver instance.
 */
static void rtc_config(void)
{
    uint32_t err_code;

    //Initialize RTC instance
    nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
    config.prescaler = 1309;
    err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler);
    APP_ERROR_CHECK(err_code);

    //Enable tick event & interrupt
    nrf_drv_rtc_tick_enable(&rtc,true);

    //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
    //err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 8,true);
    //APP_ERROR_CHECK(err_code);

    //Power on RTC instance
    nrf_drv_rtc_disable(&rtc);
}

/**
 * @brief Function for application main entry.
 */

void rtc_uint(void)
{
	nrf_drv_rtc_counter_clear(&rtc);
	nrf_drv_rtc_disable(&rtc);
}

void rtc_enable(void)
{
	nrf_drv_rtc_counter_clear(&rtc);
	nrf_drv_rtc_enable(&rtc);
	mcuInSleep = true;
}

int main(void)
{
	lfclk_config();

    rtc_config();
	rtc_uint();
	nrf_gpio_cfg_output(BUTTON1_PIN);
    while (true)
    {
		//do something
		rtc_enable();
		while(mcuInSleep){
			
        __WFE();__SEV();
        __WFE();
		}        
		nrf_gpio_pin_toggle(BUTTON1_PIN);
		rtc_uint();
		
    }
}

Related