This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

TIMER counting micros

Hi

I am struggling with timer in counter mode. What i need is the counter to calculate time between each loop in microseconds. I do not want time interrupts i just need a function which will return me time in microseconds (like on arduino). 

Greetings

Parents
  • Hello,

    The easiest thing to do here would be to just read out the value of the TIMER CC register at the different places and then do a comparison between these to see how long time has passed (you will need to convert from ticks to µs).
    If you intend to keep this for more than debugging purposes you will need to make sure to handle potential wrap-arounds of the CC register (depending on the application and the TIMER frequency and BITMODE configuration).

    Best regards,
    Karl

  • uint32_t counter = 0 ;
    
    void start_timer(void)
    {		
    
    	NRF_TIMER1->TASKS_START = 1;	
            NRF_TIMER1->MODE = 2;
            NRF_TIMER1->PRESCALER = 4;
    	NRF_TIMER1->BITMODE = (TIMER_BITMODE_BITMODE_24Bit << TIMER_BITMODE_BITMODE_Pos);
    	NRF_TIMER1->TASKS_CLEAR = 1;
    }
    		
    
    static void read_TIM(void)
    {
     NRF_TIMER1->TASKS_COUNT = 1;
    NRF_TIMER1->TASKS_CAPTURE[1] = 1 ; 
    counter = NRF_TIMER1->CC[1];
    while(counter < 10000)
    {
    NRF_TIMER1->TASKS_COUNT=1;
    NRF_TIMER1->TASKS_CAPTURE[1] = 1 ; 
    counter = NRF_TIMER1->CC[1];
    }
    
    NRF_TIMER1->TASKS_CLEAR = 1;
    }
    
    int main(void)
    {
    	
      start_timer();                            
    	NRF_TIMER1->TASKS_START = 1;
    	while(true) 
    	{
    		 read_TIM();
                     NRF_LOG_INFO(counter);
                     NRF_LOG_FLUSH();;
    	}
    }

    I tried to use this code but the value is always 10000 and i do not know what is wrong with it.

  • Hello,

    It seems like you are configuring the TIMER instance after having started it - this may not work as expected.
    The simplest way for you to do this would be to instance and configure the timer as demonstrated in some of the example, and then either read the CC register value directly, or using the drivers nrfx_timer_capture function. I would perhaps recommend just using the nrfx driver, so you do not have to work with the registers directly.

    In this case, it could look something like this:

    static const nrfx_timer_t m_timer = NRFX_TIMER_INSTANCE(0);
    ..
    void timer_handler(nrf_timer_event_t event_type, void * p_context)
    {
    
    }
    ..
    void timer_init_and_start()
    {
        nrfx_timer_config_t timer_cfg = NRFX_TIMER_DEFAULT_CONFIG;
        timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;
        err_code = nrfx_timer_init(&m_timer, &timer_cfg, timer_handler);
        APP_ERROR_CHECK(err_code);
    
        /* Setting the CC to trigger at an arbitrary value of 2000 ms, since I do not know your application's requirements */
        uint32_t ticks = nrfx_timer_ms_to_ticks(&m_timer, 2000);
        nrfx_timer_compare(&m_timer,
                            NRF_TIMER_CC_CHANNEL0,
                            ticks,
                            false);
        nrfx_timer_enable(&m_timer);
    }
    ..
    uint32_t ticks = nrfx_timer_capture(&m_timer, NRF_TIMER_CC_CHANNEL0);
    ..
    


    Then compare the outputted ticks values and covert the difference into ms / µs to see how long the execution took.

    Best regards,
    Karl

  • My application need to calculate quaterions based on the time between each cycle (getting data from sensor and update function) and i already know - better time resolution and more precise then the position is better. It will be in use after button click and it stops after certain event after 5 to 20 mins. it cannot be based on time interrupt due to sensor refrest rate and etc. This alghoritm works well on arduino nano 33 ble and using micros() function (in arduino IDE). I want ot use 1 us Resolution (timer with 1 Mhz clock) but i do not know how to do it because of misleading informations on forum and not working programs. I just need to calculate time betweend each cycle i can start and stop timer each time and read the value.

  • Thank you for elaborating.

    Ceranthor said:
    I want ot use 1 us Resolution (timer with 1 Mhz clock) but i do not know how to do it because of misleading informations on forum and not working programs.

    What misleading information are you here referring to, and which non-working programs?

    Ceranthor said:
    I just need to calculate time betweend each cycle i can start and stop timer each time and read the value.

    Please try the approach I described in my earlier comment, and call nrfx_timer_capture whenever you would like to capture the current ticks of the timer.
    If you have configured the timer to use a 1 MHz frequency, each tick will indeed represent 1 µs, so you will not need to do any ticks -> µs conversion in that case.
    You might need to change the configured frequency of the TIMER as well.

    Best regards,
    Karl

  • I just did it and it is not working for me i have an error:


    Compiling ‘main.c’
    14> In file included from ../../../../../../modules/nrfx/nrfx.h:45,
    14> from ../../../../../../modules/nrfx/hal/nrf_gpio.h:44,
    14> from ../../../../../../components/boards/boards.h:43,
    14> from C:\Users\koron\Desktop\nRF5_SDK_17.1.0_ddde560\examples\peripheral\twi_scanner\main.c:2:
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:72:39: error: 'NRFX_TIMER0_INST_IDX' undeclared here (not in a function); did you mean 'NRFX_TWIM0_INST_IDX'?
    14> ../../../../../../modules/nrfx/drivers/nrfx_common.h:113:37: note: in definition of macro 'NRFX_CONCAT_3_'
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:72:25: note: in expansion of macro 'NRFX_CONCAT_3'
    14> C:\Users\koron\Desktop\nRF5_SDK_17.1.0_ddde560\examples\peripheral\twi_scanner\main.c:46:37: note: in expansion of macro 'NRFX_TIMER_INSTANCE'
    14> In file included from C:\Users\koron\Desktop\nRF5_SDK_17.1.0_ddde560\examples\peripheral\twi_scanner\main.c:14:
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:110:50: error: 'NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY' undeclared (first use in this function); did you mean 'NRFX_TWIM_DEFAULT_CONFIG_FREQUENCY'?
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:110:50: note: in definition of macro 'NRFX_TIMER_DEFAULT_CONFIG'
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:110:50: note: each undeclared identifier is reported only once for each function it appears in
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:110:50: note: in definition of macro 'NRFX_TIMER_DEFAULT_CONFIG'
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:111:45: error: 'NRFX_TIMER_DEFAULT_CONFIG_MODE' undeclared (first use in this function); did you mean 'NRFX_TIMER_DEFAULT_CONFIG'?
    14> C:\Users\koron\Desktop\nRF5_SDK_17.1.0_ddde560\examples\peripheral\twi_scanner\main.c:55:37: note: in expansion of macro 'NRFX_TIMER_DEFAULT_CONFIG'
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:112:50: error: 'NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH' undeclared (first use in this function); did you mean 'NRFX_TIMER_DEFAULT_CONFIG'?
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:112:50: note: in definition of macro 'NRFX_TIMER_DEFAULT_CONFIG'
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:113:27: error: 'NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY' undeclared (first use in this function); did you mean 'NRFX_TWIM_DEFAULT_CONFIG_IRQ_PRIORITY'?
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:113:27: note: in definition of macro 'NRFX_TIMER_DEFAULT_CONFIG'
    Build failed

    And i do not know how to fix this 
    I attached "nrfx_timer.h"

Reply
  • I just did it and it is not working for me i have an error:


    Compiling ‘main.c’
    14> In file included from ../../../../../../modules/nrfx/nrfx.h:45,
    14> from ../../../../../../modules/nrfx/hal/nrf_gpio.h:44,
    14> from ../../../../../../components/boards/boards.h:43,
    14> from C:\Users\koron\Desktop\nRF5_SDK_17.1.0_ddde560\examples\peripheral\twi_scanner\main.c:2:
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:72:39: error: 'NRFX_TIMER0_INST_IDX' undeclared here (not in a function); did you mean 'NRFX_TWIM0_INST_IDX'?
    14> ../../../../../../modules/nrfx/drivers/nrfx_common.h:113:37: note: in definition of macro 'NRFX_CONCAT_3_'
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:72:25: note: in expansion of macro 'NRFX_CONCAT_3'
    14> C:\Users\koron\Desktop\nRF5_SDK_17.1.0_ddde560\examples\peripheral\twi_scanner\main.c:46:37: note: in expansion of macro 'NRFX_TIMER_INSTANCE'
    14> In file included from C:\Users\koron\Desktop\nRF5_SDK_17.1.0_ddde560\examples\peripheral\twi_scanner\main.c:14:
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:110:50: error: 'NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY' undeclared (first use in this function); did you mean 'NRFX_TWIM_DEFAULT_CONFIG_FREQUENCY'?
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:110:50: note: in definition of macro 'NRFX_TIMER_DEFAULT_CONFIG'
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:110:50: note: each undeclared identifier is reported only once for each function it appears in
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:110:50: note: in definition of macro 'NRFX_TIMER_DEFAULT_CONFIG'
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:111:45: error: 'NRFX_TIMER_DEFAULT_CONFIG_MODE' undeclared (first use in this function); did you mean 'NRFX_TIMER_DEFAULT_CONFIG'?
    14> C:\Users\koron\Desktop\nRF5_SDK_17.1.0_ddde560\examples\peripheral\twi_scanner\main.c:55:37: note: in expansion of macro 'NRFX_TIMER_DEFAULT_CONFIG'
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:112:50: error: 'NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH' undeclared (first use in this function); did you mean 'NRFX_TIMER_DEFAULT_CONFIG'?
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:112:50: note: in definition of macro 'NRFX_TIMER_DEFAULT_CONFIG'
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:113:27: error: 'NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY' undeclared (first use in this function); did you mean 'NRFX_TWIM_DEFAULT_CONFIG_IRQ_PRIORITY'?
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:113:27: note: in definition of macro 'NRFX_TIMER_DEFAULT_CONFIG'
    Build failed

    And i do not know how to fix this 
    I attached "nrfx_timer.h"

Children
  • Have you enabled the TIMER instance you are going to use in the sdk_config.h file?
    Please also remove or comment out any legacy definitions, such as TIMER_ENABLED. You should have NRFX_TIMER_ENABLED defined in your sdk_config, and TIMER_ENABLED should not be present as this may cause issues.

    Please also note that some peripheral instances are blocked or restricted by the SoftDevice.

    Ceranthor said:
    I attached "nrfx_timer.h"

    I do not see any attached file - but you should not modify the driver directly, in any case, since this might break it.

    Best regards,
    Karl

  • I do not have this option in sdk_config.h but i have it on another example "simple timer" and i not know how to add this part to sdk config.

  • You can add the entire nrfx_timer_* section from the other project into your sdk_config for your main project. Make sure to look through the configurations to ensure that they are configured to your requirements.

    // <e> NRFX_TIMER_ENABLED - nrfx_timer - TIMER periperal driver
    //==========================================================
    #ifndef NRFX_TIMER_ENABLED
    #define NRFX_TIMER_ENABLED 0
    #endif
    // <q> NRFX_TIMER0_ENABLED  - Enable TIMER0 instance
     
    
    #ifndef NRFX_TIMER0_ENABLED
    #define NRFX_TIMER0_ENABLED 0
    #endif
    
    // <q> NRFX_TIMER1_ENABLED  - Enable TIMER1 instance
     
    
    #ifndef NRFX_TIMER1_ENABLED
    #define NRFX_TIMER1_ENABLED 0
    #endif
    
    // <q> NRFX_TIMER2_ENABLED  - Enable TIMER2 instance
     
    
    #ifndef NRFX_TIMER2_ENABLED
    #define NRFX_TIMER2_ENABLED 0
    #endif
    
    // <q> NRFX_TIMER3_ENABLED  - Enable TIMER3 instance
     
    
    #ifndef NRFX_TIMER3_ENABLED
    #define NRFX_TIMER3_ENABLED 0
    #endif
    
    // <q> NRFX_TIMER4_ENABLED  - Enable TIMER4 instance
     
    
    #ifndef NRFX_TIMER4_ENABLED
    #define NRFX_TIMER4_ENABLED 0
    #endif
    
    // <o> NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY  - Timer frequency if in Timer mode
     
    // <0=> 16 MHz 
    // <1=> 8 MHz 
    // <2=> 4 MHz 
    // <3=> 2 MHz 
    // <4=> 1 MHz 
    // <5=> 500 kHz 
    // <6=> 250 kHz 
    // <7=> 125 kHz 
    // <8=> 62.5 kHz 
    // <9=> 31.25 kHz 
    
    #ifndef NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY
    #define NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY 0
    #endif
    
    // <o> NRFX_TIMER_DEFAULT_CONFIG_MODE  - Timer mode or operation
     
    // <0=> Timer 
    // <1=> Counter 
    
    #ifndef NRFX_TIMER_DEFAULT_CONFIG_MODE
    #define NRFX_TIMER_DEFAULT_CONFIG_MODE 0
    #endif
    
    // <o> NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH  - Timer counter bit width
     
    // <0=> 16 bit 
    // <1=> 8 bit 
    // <2=> 24 bit 
    // <3=> 32 bit 
    
    #ifndef NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH
    #define NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH 0
    #endif
    
    // <o> NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY  - Interrupt priority
     
    // <0=> 0 (highest) 
    // <1=> 1 
    // <2=> 2 
    // <3=> 3 
    // <4=> 4 
    // <5=> 5 
    // <6=> 6 
    // <7=> 7 
    
    #ifndef NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY
    #define NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY 6
    #endif
    
    // <e> NRFX_TIMER_CONFIG_LOG_ENABLED - Enables logging in the module.
    //==========================================================
    #ifndef NRFX_TIMER_CONFIG_LOG_ENABLED
    #define NRFX_TIMER_CONFIG_LOG_ENABLED 0
    #endif
    // <o> NRFX_TIMER_CONFIG_LOG_LEVEL  - Default Severity level
     
    // <0=> Off 
    // <1=> Error 
    // <2=> Warning 
    // <3=> Info 
    // <4=> Debug 
    
    #ifndef NRFX_TIMER_CONFIG_LOG_LEVEL
    #define NRFX_TIMER_CONFIG_LOG_LEVEL 3
    #endif
    
    // <o> NRFX_TIMER_CONFIG_INFO_COLOR  - ANSI escape code prefix.
     
    // <0=> Default 
    // <1=> Black 
    // <2=> Red 
    // <3=> Green 
    // <4=> Yellow 
    // <5=> Blue 
    // <6=> Magenta 
    // <7=> Cyan 
    // <8=> White 
    
    #ifndef NRFX_TIMER_CONFIG_INFO_COLOR
    #define NRFX_TIMER_CONFIG_INFO_COLOR 0
    #endif
    
    // <o> NRFX_TIMER_CONFIG_DEBUG_COLOR  - ANSI escape code prefix.
     
    // <0=> Default 
    // <1=> Black 
    // <2=> Red 
    // <3=> Green 
    // <4=> Yellow 
    // <5=> Blue 
    // <6=> Magenta 
    // <7=> Cyan 
    // <8=> White 
    
    #ifndef NRFX_TIMER_CONFIG_DEBUG_COLOR
    #define NRFX_TIMER_CONFIG_DEBUG_COLOR 0
    #endif
    
    // </e>


    Best regards,
    Karl

  • Now it works perfectly i changed freq to 1 Mhz and to 32 bit. Thank you for help 

  • Great, I am happy to hear that it now works perfectly! Slight smile

    Please do not hesitate to open another ticket if you should encounter any other issues or questions in the future.

    Good luck with your development!

    Best regards,
    Karl

Related