nrf_delay_ms clock issues

I'm using nrf_delay_ms in main on "ble_app_beacon", but the delay time is much longer than I set.

I have set 2000ms delay to log, but it seems 4s or longer than I set.

int main(void)
{
	uint32_t num=0;
    // Initialize.
    log_init();
    timers_init();
    leds_init();
    power_management_init();
    ble_stack_init();
    advertising_init();

    // Start execution.
    NRF_LOG_INFO("Beacon example started.");
		NRF_LOG_FLUSH();
    advertising_start();

    // Enter main loop.
    for (;; )
    {
			nrf_delay_ms(2000);
			NRF_LOG_INFO("beacon test %d",num);
			NRF_LOG_FLUSH();
			num++;
        idle_state_handle();
    }
}

Parents Reply Children
  • Thanks!

    I'll delete the NRFX_COREDEP_DELAY_US_LOOP_CYCLES in preprocessor symbols. And DEVELOP_IN_NRF52832  should be deleted too? I have not find issues because of that.

  • Hi Vidar,

    I have tested after deleted the NRFX_COREDEP_DELAY_US_LOOP_CYCLES  and DEVELOP_IN_NRF52832, the clock seems worked normally, but there is another problem.

    In my code, LED will change the status for every 2 seconds, but the LED just changed two times and then never change.

    int main(void)
    {
    	  uint8_t  rcv = 0;
    		const char * txbuff = "AnyTek";
    		uint8_t rcvbuff[ 32 ] = { 0 };
        // Initialize.
    		log_init();
        timers_init();
    //    leds_init();
        power_management_init();
        ble_stack_init();
        advertising_init();
    
        // Start execution.
        NRF_LOG_INFO("Beacon example started.");
    		NRF_LOG_FLUSH();
        advertising_start();
    		platform_gpio_initialize();
    		platform_gpio_int_initialize();
    		twi_master_init();
    		m_sensor_work_init();
    //		m_CC1101_Init(0xAA);
    //		CC1101_Init();
    //		NRF_LOG_FLUSH();
        // Enter main loop.
        for (;; )
        {
    //			if(GSENSOR_Get_Transient_Detection_Flag() == true)
    //			{
    //				nrf_gpio_pin_toggle(GREEN_PIN);
    //				nrf_delay_ms(200);
    //			}
    //			NRF_LOG_INFO("Test");
    //					CC1101_Tx_Packet((uint8_t *)txbuff,6,ADDRESS_CHECK);
    //			NRF_LOG_FLUSH();
    //			CC1101_Tx_Packet((uint8_t *)txbuff,6,ADDRESS_CHECK);
    //				CC1101_Clear_RxBuffer( );
    //				CC1101_Set_Mode( RX_MODE );
    //				rcv = CC1101_Rx_Packet( rcvbuff );
    				nrf_delay_ms(2000);
    				nrf_gpio_pin_toggle(GREEN_PIN);
    //			NRF_LOG_FLUSH();
          idle_state_handle();
        }
    }

  • Hi,

    The device will stay inside the idle_state_handle() function in sleep (System ON mode) unless you have a periodic application interrupt to wake up it up again. This is probably why the the LED stops toggling.

    That said, busy waiting with nrf_delay_ms() should generally be avoided on battery powered devices as it keeps the CPU active. I would instead recommend you use a low power app timer interrupt for toggling the LED. I posted an example showing how you can add a timer instance in the beacon example in this thread here: https://devzone.nordicsemi.com/f/nordic-q-a/71940/low-power-mode-using-saadc-with-ble_app_beacon

  • Hi,

    In SDK_CONFIG there are NRX_TIMER_ENABLED and TIMER_ENABLED  APP_TIMER_ENABLE, should I select them all? APP_TIMER_ENABLE is already seleted in example. I had created timer in SDK14.02, but I don't know SDK17.2 well.

  • Hi,

    Only APP_TIMER_ENABLE should be needed. TIMER_ENABLED  is for the high frequency timer which is not low power. You can use my example as a reference.

Related