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

timer in microsecond on nRF52840

hello , i'am working actually on the timer of nRF52840 S140  

my timer look like this: 

#define COMPTEUR_INTERVAL     APP_TIMER_TICKS(1)                   /**< Tick / Ms */

APP_TIMER_DEF(sd_timer_id);

static void lfclk_request(void)
{
    ret_code_t err_code;// = nrf_drv_clock_init();
    APP_ERROR_CHECK(err_code);
    nrf_drv_clock_lfclk_request(NULL);
	NRF_LOG_INFO("LFCLK: OFF");
    
    if(nrf_drv_clock_lfclk_is_running()){
		NRF_LOG_INFO("LFCLK: ON");
			
	}
}

void test(){
	
	ret_code_t err_code;
	
	// Create timers
	err_code = app_timer_create(&sd_timer_id,APP_TIMER_MODE_REPEATED,timer_sd_handler);
	lfclk_request();
	APP_ERROR_CHECK(err_code);
	NRF_LOG_INFO("Timer_sd_start");
	err_code = app_timer_start(sd_timer_id, COMPTEUR_INTERVAL, NULL);
	NRF_LOG_INFO("%d",err_code);
	APP_ERROR_CHECK(err_code);
}

static void timer_sd_handler(void * p_context){
	
	compteur++;
	
}

but  how to switch from millisecond resolution to microsecond resolution ? ( APP_TIMER_TICKS) 

thank's

  • Hi,

    The application timer library runs using the RTC peripheral, which again use the LFCLK as the clock source. This will give you a maximum frequency of 32.768 kHz, or a resolution of ~30 µs. If you want better resolution, you need to use the TIMER peripheral. There is a driver and an example available in the SDK. Please have a look at this thread for more details.

    Best regards,
    Jørgen

  • 30 us is enough for me, same 1 us  

    what I want :

    -> make a break every 62.5ms, how to do this.? (when my compteur equal( 62.5ms) but to do this i will to replace this 

     -> #define COMPTEUR_INTERVAL     APP_TIMER_TICKS(1)                   /**< Tick / Ms */

    by 

    #define COMPTEUR_INTERVAL     APP_TIMER_TICKS(1)                   /**< Tick / Us */

    I want my counter ("compteur")  to increment every microsecond 

  • You are not required to use the MACRO defined in the app_timer library. You can calculate the ticks value yourself, or create your own MACRO to calculate ticks from microseconds:

    #define APP_TIMER_TICKS_US(US)                                \
                ((uint32_t)ROUNDED_DIV(                        \
                (US) * (uint64_t)APP_TIMER_CLOCK_FREQ,         \
                1000000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1)))

  • i have defined the MACRO on app_timer.h like this 

    #ifndef FREERTOS
    #define APP_TIMER_TICKS(MS)                                \
                ((uint32_t)ROUNDED_DIV(                        \
                (MS) * (uint64_t)APP_TIMER_CLOCK_FREQ,         \
                1000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1)))
    
    #define APP_TIMER_TICKS_US(US)                                \
                ((uint32_t)ROUNDED_DIV(                        \
                (US) * (uint64_t)APP_TIMER_CLOCK_FREQ,         \
                1000000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1)))
    #else
    #include "FreeRTOSConfig.h"
    #define APP_TIMER_TICKS(MS) (uint32_t)ROUNDED_DIV((MS)*configTICK_RATE_HZ,1000)
    #endif

    and on my main : 

    #define COMPTEUR_INTERVAL     APP_TIMER_TICKS_US(1)                   /**< Tick / Us */void test(){
    	
    	ret_code_t err_code;
    	//Create timers
    	err_code = app_timer_create(&timer_id,APP_TIMER_MODE_REPEATED,timer_sd_handler);
    	lfclk_request();
    	APP_ERROR_CHECK(err_code);
    	NRF_LOG_INFO("Timer_sd_start");
    	err_code = app_timer_start(timer_id, COMPTEUR_INTERVAL, NULL);
    	NRF_LOG_INFO("%d",err_code);
    	APP_ERROR_CHECK(err_code);
    }

    but i have a fatal error when i define like this : 

    -> #define COMPTEUR_INTERVAL     APP_TIMER_TICKS_US(1)                   /**< Tick / Us */ 

    but no fatal error with this  : 

    -> #define COMPTEUR_INTERVAL     APP_TIMER_TICKS_US(1000)                   /**< Tick / Us */

    i don't know why 

  • I told you the resolution is 30 µs. You try to calculate ticks for 1 µs. Of course that will not work. App_timer have a minimum tick configuration of 5: APP_TIMER_MIN_TIMEOUT_TICKS, which means you cannot get a timeout of less than ~150 µs.

Related