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.

  • Hi, Aryan,

    Thanks a lot for your quick response.

    The point is that the first event time (after timer0 task tarted) is different. To record the time of first event (and remove the delay right after timer0 started), the code is modified as:

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

    the result is as

       ---------------------------------- 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 = 33, 
        i = 6, time delay = 34, 
        i = 7, time delay = 34, 
        i = 8, time delay = 34, 
        i = 9, time delay = 34, 
    

    How can I get the same time of the first event as the other events?

    Thanks again.

Reply
  • Hi, Aryan,

    Thanks a lot for your quick response.

    The point is that the first event time (after timer0 task tarted) is different. To record the time of first event (and remove the delay right after timer0 started), the code is modified as:

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

    the result is as

       ---------------------------------- 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 = 33, 
        i = 6, time delay = 34, 
        i = 7, time delay = 34, 
        i = 8, time delay = 34, 
        i = 9, time delay = 34, 
    

    How can I get the same time of the first event as the other events?

    Thanks again.

Children
No Data
Related