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
  • capture the RTC->COUNTER value after you get the event

      for (i=0; i<10; i++)
         {  
            NRF_TIMER0->EVENTS_COMPARE[0] = 0;
        while (NRF_TIMER0->EVENTS_COMPARE[0] == 0)
        {    
           /* delay 1ms */    
        }
        time_capture[i] = NRF_RTC0->COUNTER;  // <-- this is changed
      }
    

    you are not using interrupts to sense the TIMER0 events, so if you add nrf_delay_ms(some_number), then you are just ignoring the events happening within that time. After that when you capture the RTC->COUNTER value then it will give you different numbers because of the initial ignored events which caused many overflows in RTC counter.

  • The CC register doesn't take any time at all so that's not the problem. This output makes no sense to me currently, I can't see how you could be getting this result. As long as TIMER0 is started from 0 when you start it, the first event will occur 16,000 ticks later.

    As well as recording the RTC0 counts, how about recording the TIMER0 count at the same moments to just confirm they make sense. ie add a timer_capture[] variable and after each of the time_capture[x]=RTC0->COUNTER lines add something which captures the TIMER0 count to CC[1] and stores that - then print them out too.

    Take that printf() at the start out too, just to eliminate that, I don't know what printf() library you have in there and whether it's asynchronous, uses timers or anything else.

Reply
  • The CC register doesn't take any time at all so that's not the problem. This output makes no sense to me currently, I can't see how you could be getting this result. As long as TIMER0 is started from 0 when you start it, the first event will occur 16,000 ticks later.

    As well as recording the RTC0 counts, how about recording the TIMER0 count at the same moments to just confirm they make sense. ie add a timer_capture[] variable and after each of the time_capture[x]=RTC0->COUNTER lines add something which captures the TIMER0 count to CC[1] and stores that - then print them out too.

    Take that printf() at the start out too, just to eliminate that, I don't know what printf() library you have in there and whether it's asynchronous, uses timers or anything else.

Children
No Data
Related