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

Question about strange behavior of Timer0

I want to use timer0 to get exact time delay (without soft device used).

Timer0 configuration is as

void timer0_initialization(void)
{
  NRF_TIMER0->MODE = TIMER_MODE_MODE_Timer;
  NRF_TIMER0->PRESCALER = 0;
  NRF_TIMER0->BITMODE = TIMER_BITMODE_BITMODE_32Bit;
  NRF_TIMER0->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos;
  NRF_TIMER0->INTENSET = TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos;
  NRF_TIMER0->TASKS_STOP = 1;
}

the test main function is as

int main(void)
{
  uint32_t i;
  uint32_t time_capture[10];
  
 /* UART and RTC0 initialization */
  
  printf ("---------------------------------- Start ---------------------------------- \r\n");
  timer0_initialization( );      
  
  NRF_TIMER0->CC[0] = 1000 * 16;
  NRF_TIMER0->TASKS_START = 1;
        
  for (i=0; i<10; i++)
  {  
    NRF_TIMER0->EVENTS_COMPARE[0] = 0;
    time_capture[i] = NRF_RTC0->COUNTER;
    while (NRF_TIMER0->EVENTS_COMPARE[0] == 0)
    {    
       /* delay 1ms */    
    }
  }
  for (i=1; i<10; i++)
  {
    printf("i = %ld, time delay = %ld, \r\n", i, time_capture[i] - time_capture[i - 1]); 
  }
  
  while(1);
}

Since RTC0's frequency is 32K, so the output of "printf" time delay should be about 30 (1000/32.768), moreover, each time delay should be the same. However, the first time delay is much difference than others. the output of above code is like

---------------------------------- Start --------------------------------------- 
i = 1, time delay = 8, 
i = 2, time delay = 34, 
i = 3, time delay = 34, 
i = 4, time delay = 34, 
i = 5, time delay = 34, 
i = 6, time delay = 34, 
i = 7, time delay = 34, 
i = 8, time delay = 34, 
i = 9, time delay = 34, 

If add some time daly right after timer0 started like

  NRF_TIMER0->CC[0] = 1000 * 16;
  NRF_TIMER0->TASKS_START = 1;
      
/* add some time delay */
  nrf_delay_ms(10);
  
  for (i=0; i<10; i++)
  {  
    NRF_TIMER0->EVENTS_COMPARE[0] = 0;
    time_capture[i] = NRF_RTC0->COUNTER;
    while (NRF_TIMER0->EVENTS_COMPARE[0] == 0)
    {        
    }
  }

The program output is like

---------------------------------- Start --------------------------------------- 
i = 1, time delay = 0, 
i = 2, time delay = 34, 
i = 3, time delay = 34, 
i = 4, time delay = 34, 
i = 5, time delay = 34, 
i = 6, time delay = 34, 
i = 7, time delay = 34, 
i = 8, time delay = 34, 
i = 9, time delay = 34, 

The first time delay is 0, means no any time delay.

For "nrf_delay_ms(100);", "nrf_delay_ms(1000);", "nrf_delay_ms(2000);", the output are like following:

---------------------------------- Start --------------------------------------- 
i = 1, time delay = 2, 
i = 2, time delay = 34, 
i = 3, time delay = 34, 
i = 4, time delay = 34, 
i = 5, time delay = 34, 
i = 6, time delay = 34, 
i = 7, time delay = 34, 
i = 8, time delay = 34, 
i = 9, time delay = 34, 
---------------------------------- Start --------------------------------------- 
i = 1, time delay = 27, 
i = 2, time delay = 32, 
i = 3, time delay = 33, 
i = 4, time delay = 32, 
i = 5, time delay = 33, 
i = 6, time delay = 32, 
i = 7, time delay = 33, 
i = 8, time delay = 32, 
i = 9, time delay = 33, 
---------------------------------- Start --------------------------------------- 
i = 1, time delay = 21, 
i = 2, time delay = 33, 
i = 3, time delay = 32, 
i = 4, time delay = 33, 
i = 5, time delay = 32, 
i = 6, time delay = 33, 
i = 7, time delay = 32, 
i = 8, time delay = 33, 
i = 9, time delay = 32, 

It seems the first time delay is a random number. These result is obtained from nRF52832 preview DK board, the similar result is obtain with nRF51822 board.

How to understand this results of timer0? is there any bug in my code?

Thanks a lot!

Parents
  • There is a bug in my previous code.

    Inside the for loop, the EVENTS_COMPARE register is cleared after the event. For MDK, since all avriable is initialized with 0, each event has the same timer number. but for GCC, the initial value is a random number (I use GCC), so the first event has a different timer number.

    The correct code should be like:

    time_capture[0] = NRF_RTC0->COUNTER;
    NRF_TIMER0->TASKS_CLEAR = 1;
    for (i=1; i<10; i++)
    {
        NRF_TIMER0->EVENTS_COMPARE[0] = 0;
        (void)NRF_TIMER0->EVENTS_COMPARE[0];
        while (NRF_TIMER0->EVENTS_COMPARE[0] == 0)
        {
        }  
        time_capture[i] = NRF_RTC0->COUNTER;
    }
    

    This code will give the same timer number for each event.

  • Hi, Aryan,

    The first value of RTC0->COUNTER save in time_capture[0] is just used to record the initial time of waiting for the first event, and second value saved in time_capture[1] (in for loop, start form i=1) to record the time of the first event and the starting time for waiting second event, and .......

    The value captured from RTC0 has no difference of unconditional/conditional, just record the time. The time used for waiting the first event is (time_capture[1] - time_capture[0]), and second event time is (time_capture[2] - time_capture[1]). The output code is

    for (i=1; i<10; i++)
    {
        printf("---- i = %ld, timer counter = %ld\r\n", i, time_capture[i] - time_capture[i - 1]); 
        nrf_delay_ms(100);
    }
    

    for NRF_TIMER0->CC[0] = 16 * 1000, I get the output as ---------------------------------------------------------------- ---- i = 1, timer counter = 34 ---- i = 2, timer counter = 34 ---- i = 3, timer counter = 34 ---- i = 4, timer counter = 34 ---- i = 5, timer counter = 34 ---- i = 6, timer counter = 34 ---- i = 7, timer counter = 34 ---- i = 8, timer counter = 34 ---- i = 9, timer counter = 34

    Thanks a lot,

    Jiacheng

Reply
  • Hi, Aryan,

    The first value of RTC0->COUNTER save in time_capture[0] is just used to record the initial time of waiting for the first event, and second value saved in time_capture[1] (in for loop, start form i=1) to record the time of the first event and the starting time for waiting second event, and .......

    The value captured from RTC0 has no difference of unconditional/conditional, just record the time. The time used for waiting the first event is (time_capture[1] - time_capture[0]), and second event time is (time_capture[2] - time_capture[1]). The output code is

    for (i=1; i<10; i++)
    {
        printf("---- i = %ld, timer counter = %ld\r\n", i, time_capture[i] - time_capture[i - 1]); 
        nrf_delay_ms(100);
    }
    

    for NRF_TIMER0->CC[0] = 16 * 1000, I get the output as ---------------------------------------------------------------- ---- i = 1, timer counter = 34 ---- i = 2, timer counter = 34 ---- i = 3, timer counter = 34 ---- i = 4, timer counter = 34 ---- i = 5, timer counter = 34 ---- i = 6, timer counter = 34 ---- i = 7, timer counter = 34 ---- i = 8, timer counter = 34 ---- i = 9, timer counter = 34

    Thanks a lot,

    Jiacheng

Children
No Data
Related