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, RK and Aryan,

    Thanks a lot for your help.

    I'm trying to re-produce the output with a "pure" SDK environment (I use a mixed environment with som function from old SDK and som my own functions before). I'm currently use UART example code in "nRF52_SDK_0.9.1_3639cc9\examples\peripheral\uart", just with a simple modification. The main function is (add timer0 config function and modify the last lines in main function):

    /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
     *
     * The information contained herein is property of Nordic Semiconductor ASA.
     * Terms and conditions of usage are described in detail in NORDIC
     * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
     *
     * Licensees are granted free, non-transferable use of the information. NO
     * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
     * the file.
     *
     */
    
    /** @file
     * @defgroup uart_example_main main.c
     * @{
     * @ingroup uart_example
     * @brief UART Example Application main file.
     *
     * This file contains the source code for a sample application using UART.
     * 
     */
    
    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include "app_uart.h"
    #include "app_error.h"
    #include "nrf_delay.h"
    #include "nrf.h"
    #include "bsp.h"
    
    //#define ENABLE_LOOPBACK_TEST  /**< if defined, then this example will be a loopback test, which means that TX should be connected to RX to get data loopback. */
    
    #define MAX_TEST_DATA_BYTES     (15U)                /**< max number of test bytes to be used for tx and rx. */
    #define UART_TX_BUF_SIZE 256                         /**< UART TX buffer size. */
    #define UART_RX_BUF_SIZE 1                           /**< UART RX buffer size. */
    
    void uart_error_handle(app_uart_evt_t * p_event)
    {
        if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
        {
            APP_ERROR_HANDLER(p_event->data.error_communication);
        }
        else if (p_event->evt_type == APP_UART_FIFO_ERROR)
        {
            APP_ERROR_HANDLER(p_event->data.error_code);
        }
    }
    
    
    
    #ifdef ENABLE_LOOPBACK_TEST
    /** @brief Function for setting the @ref ERROR_PIN high, and then enter an infinite loop.
     */
    static void show_error(void)
    {
        
        LEDS_ON(LEDS_MASK);
        while(true)
        {
            // Do nothing.
        }
    }
    
    
    /** @brief Function for testing UART loop back. 
     *  @details Transmitts one character at a time to check if the data received from the loopback is same as the transmitted data.
     *  @note  @ref TX_PIN_NUMBER must be connected to @ref RX_PIN_NUMBER)
     */
    static void uart_loopback_test()
    {
        uint8_t * tx_data = (uint8_t *)("\n\rLOOPBACK_TEST\n\r");
        uint8_t   rx_data;
    
        // Start sending one byte and see if you get the same
        for (uint32_t i = 0; i < MAX_TEST_DATA_BYTES; i++)
        {
            uint32_t err_code;
            while(app_uart_put(tx_data[i]) != NRF_SUCCESS);
    
            nrf_delay_ms(10);
            err_code = app_uart_get(&rx_data);
    
            if ((rx_data != tx_data[i]) || (err_code != NRF_SUCCESS))
            {
                show_error();
            }
        }
        return;
    }
    
    
    #endif
    
    /********************************************************************************/
    void timer0_initialization(void)
    /*--------------------------------------------------------------------------------
    | TIMER0 initialization
    |
    --------------------------------------------------------------------------------*/
    {
      NRF_TIMER0->TASKS_STOP = 1;
      NRF_TIMER0->TASKS_CLEAR = 1;
      NRF_TIMER0->MODE = TIMER_MODE_MODE_Timer;
      NRF_TIMER0->PRESCALER = 0;
      NRF_TIMER0->BITMODE = TIMER_BITMODE_BITMODE_16Bit;
      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;
    }
    
    
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
        LEDS_CONFIGURE(LEDS_MASK);
        LEDS_OFF(LEDS_MASK);
        uint32_t err_code;
        const app_uart_comm_params_t comm_params =
          {
              RX_PIN_NUMBER,
              TX_PIN_NUMBER,
              RTS_PIN_NUMBER,
              CTS_PIN_NUMBER,
              APP_UART_FLOW_CONTROL_ENABLED,
              false,
              UART_BAUDRATE_BAUDRATE_Baud38400
          };
    
        APP_UART_FIFO_INIT(&comm_params,
                             UART_RX_BUF_SIZE,
                             UART_TX_BUF_SIZE,
                             uart_error_handle,
                             APP_IRQ_PRIORITY_LOW,
                             err_code);
    
        APP_ERROR_CHECK(err_code);
    			
    /*******************************************************************************/
    /*******************************************************************************/
    /*******************************************************************************/
        uint32_t i;
        uint32_t time_capture[10];
    
        timer0_initialization( );
        
        NRF_TIMER0->CC[0] = 1 * 16; 
     
        NRF_TIMER0->TASKS_CLEAR = 1;
        NRF_TIMER0->TASKS_START = 1;
        for (i=1; i<10; i++)
        {
            while (NRF_TIMER0->EVENTS_COMPARE[0] == 0)
            {
            }  
    				NRF_TIMER0->TASKS_CAPTURE[1] = 1;
            time_capture[i] = NRF_TIMER0->CC[1];
            NRF_TIMER0->EVENTS_COMPARE[0] = 0;
            (void)NRF_TIMER0->EVENTS_COMPARE[0];
        }
      
        printf("-------------------------------- Start ------------------------------- \r\n");
        for (i=0; i<10; i++)
        {
            printf("---- i = %ld, time delay = %ld\r\n", i, time_capture[i]); 
    			  nrf_delay_ms(10);
        }
    }
    /** @} */
    

    With smaller CC[0] value “NRF_TIMER0->CC[0] = 1 * 16” and capture timer0 counter and RTC0 is not used. The output of above code is as

    -------------------------------- Start -------------------------------
    ---- i = 0, timer counter = 0
    ---- i = 1, timer counter = 7
    ---- i = 2, timer counter = 8
    ---- i = 3, timer counter = 9
    ---- i = 4, timer counter = 10
    ---- i = 5, timer counter = 11
    ---- i = 6, timer counter = 8
    ---- i = 7, timer counter = 9
    ---- i = 8, timer counter = 10
    ---- i = 9, timer counter = 11
    

    It seems the first event still have different timer number. With some larger CC[0] value, NRF_TIMER0->CC[0] = 1000 * 16 give output

    -------------------------------- Start -------------------------------
    ---- i = 0, timer counter = 0
    ---- i = 1, timer counter = 7
    ---- i = 2, timer counter = 5
    ---- i = 3, timer counter = 7
    ---- i = 4, timer counter = 5
    ---- i = 5, timer counter = 7
    ---- i = 6, timer counter = 5
    ---- i = 7, timer counter = 7
    ---- i = 8, timer counter = 5
    ---- i = 9, timer counter = 7
    

    The code is simple, please help to check the lines of TIMER0 initialization, if some registers have wrong configured.

    I'm using nRF52832 preview DK board (PCA10036), if possible, please repeat the output with above code at your side.

    Thanks again and Best Regards,

    Jiacheng

Reply
  • Hi, RK and Aryan,

    Thanks a lot for your help.

    I'm trying to re-produce the output with a "pure" SDK environment (I use a mixed environment with som function from old SDK and som my own functions before). I'm currently use UART example code in "nRF52_SDK_0.9.1_3639cc9\examples\peripheral\uart", just with a simple modification. The main function is (add timer0 config function and modify the last lines in main function):

    /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
     *
     * The information contained herein is property of Nordic Semiconductor ASA.
     * Terms and conditions of usage are described in detail in NORDIC
     * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
     *
     * Licensees are granted free, non-transferable use of the information. NO
     * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
     * the file.
     *
     */
    
    /** @file
     * @defgroup uart_example_main main.c
     * @{
     * @ingroup uart_example
     * @brief UART Example Application main file.
     *
     * This file contains the source code for a sample application using UART.
     * 
     */
    
    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include "app_uart.h"
    #include "app_error.h"
    #include "nrf_delay.h"
    #include "nrf.h"
    #include "bsp.h"
    
    //#define ENABLE_LOOPBACK_TEST  /**< if defined, then this example will be a loopback test, which means that TX should be connected to RX to get data loopback. */
    
    #define MAX_TEST_DATA_BYTES     (15U)                /**< max number of test bytes to be used for tx and rx. */
    #define UART_TX_BUF_SIZE 256                         /**< UART TX buffer size. */
    #define UART_RX_BUF_SIZE 1                           /**< UART RX buffer size. */
    
    void uart_error_handle(app_uart_evt_t * p_event)
    {
        if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
        {
            APP_ERROR_HANDLER(p_event->data.error_communication);
        }
        else if (p_event->evt_type == APP_UART_FIFO_ERROR)
        {
            APP_ERROR_HANDLER(p_event->data.error_code);
        }
    }
    
    
    
    #ifdef ENABLE_LOOPBACK_TEST
    /** @brief Function for setting the @ref ERROR_PIN high, and then enter an infinite loop.
     */
    static void show_error(void)
    {
        
        LEDS_ON(LEDS_MASK);
        while(true)
        {
            // Do nothing.
        }
    }
    
    
    /** @brief Function for testing UART loop back. 
     *  @details Transmitts one character at a time to check if the data received from the loopback is same as the transmitted data.
     *  @note  @ref TX_PIN_NUMBER must be connected to @ref RX_PIN_NUMBER)
     */
    static void uart_loopback_test()
    {
        uint8_t * tx_data = (uint8_t *)("\n\rLOOPBACK_TEST\n\r");
        uint8_t   rx_data;
    
        // Start sending one byte and see if you get the same
        for (uint32_t i = 0; i < MAX_TEST_DATA_BYTES; i++)
        {
            uint32_t err_code;
            while(app_uart_put(tx_data[i]) != NRF_SUCCESS);
    
            nrf_delay_ms(10);
            err_code = app_uart_get(&rx_data);
    
            if ((rx_data != tx_data[i]) || (err_code != NRF_SUCCESS))
            {
                show_error();
            }
        }
        return;
    }
    
    
    #endif
    
    /********************************************************************************/
    void timer0_initialization(void)
    /*--------------------------------------------------------------------------------
    | TIMER0 initialization
    |
    --------------------------------------------------------------------------------*/
    {
      NRF_TIMER0->TASKS_STOP = 1;
      NRF_TIMER0->TASKS_CLEAR = 1;
      NRF_TIMER0->MODE = TIMER_MODE_MODE_Timer;
      NRF_TIMER0->PRESCALER = 0;
      NRF_TIMER0->BITMODE = TIMER_BITMODE_BITMODE_16Bit;
      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;
    }
    
    
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
        LEDS_CONFIGURE(LEDS_MASK);
        LEDS_OFF(LEDS_MASK);
        uint32_t err_code;
        const app_uart_comm_params_t comm_params =
          {
              RX_PIN_NUMBER,
              TX_PIN_NUMBER,
              RTS_PIN_NUMBER,
              CTS_PIN_NUMBER,
              APP_UART_FLOW_CONTROL_ENABLED,
              false,
              UART_BAUDRATE_BAUDRATE_Baud38400
          };
    
        APP_UART_FIFO_INIT(&comm_params,
                             UART_RX_BUF_SIZE,
                             UART_TX_BUF_SIZE,
                             uart_error_handle,
                             APP_IRQ_PRIORITY_LOW,
                             err_code);
    
        APP_ERROR_CHECK(err_code);
    			
    /*******************************************************************************/
    /*******************************************************************************/
    /*******************************************************************************/
        uint32_t i;
        uint32_t time_capture[10];
    
        timer0_initialization( );
        
        NRF_TIMER0->CC[0] = 1 * 16; 
     
        NRF_TIMER0->TASKS_CLEAR = 1;
        NRF_TIMER0->TASKS_START = 1;
        for (i=1; i<10; i++)
        {
            while (NRF_TIMER0->EVENTS_COMPARE[0] == 0)
            {
            }  
    				NRF_TIMER0->TASKS_CAPTURE[1] = 1;
            time_capture[i] = NRF_TIMER0->CC[1];
            NRF_TIMER0->EVENTS_COMPARE[0] = 0;
            (void)NRF_TIMER0->EVENTS_COMPARE[0];
        }
      
        printf("-------------------------------- Start ------------------------------- \r\n");
        for (i=0; i<10; i++)
        {
            printf("---- i = %ld, time delay = %ld\r\n", i, time_capture[i]); 
    			  nrf_delay_ms(10);
        }
    }
    /** @} */
    

    With smaller CC[0] value “NRF_TIMER0->CC[0] = 1 * 16” and capture timer0 counter and RTC0 is not used. The output of above code is as

    -------------------------------- Start -------------------------------
    ---- i = 0, timer counter = 0
    ---- i = 1, timer counter = 7
    ---- i = 2, timer counter = 8
    ---- i = 3, timer counter = 9
    ---- i = 4, timer counter = 10
    ---- i = 5, timer counter = 11
    ---- i = 6, timer counter = 8
    ---- i = 7, timer counter = 9
    ---- i = 8, timer counter = 10
    ---- i = 9, timer counter = 11
    

    It seems the first event still have different timer number. With some larger CC[0] value, NRF_TIMER0->CC[0] = 1000 * 16 give output

    -------------------------------- Start -------------------------------
    ---- i = 0, timer counter = 0
    ---- i = 1, timer counter = 7
    ---- i = 2, timer counter = 5
    ---- i = 3, timer counter = 7
    ---- i = 4, timer counter = 5
    ---- i = 5, timer counter = 7
    ---- i = 6, timer counter = 5
    ---- i = 7, timer counter = 7
    ---- i = 8, timer counter = 5
    ---- i = 9, timer counter = 7
    

    The code is simple, please help to check the lines of TIMER0 initialization, if some registers have wrong configured.

    I'm using nRF52832 preview DK board (PCA10036), if possible, please repeat the output with above code at your side.

    Thanks again and Best Regards,

    Jiacheng

Children
No Data
Related