cancel
Showing results for 
Search instead for 
Did you mean: 

Nano second Precision using a hardware timer on STM32L4S5ZI

SJant.1
Associate III

Hi,

I am working with STM32L4S5ZI MCU and I have a custom board with the same.

I am evaluating the hardware timer and as part of my project I need time in nano seconds for some time constrained work. Right now I am using Timer3 and below is my sample code with which I am able to get the counter value between a start and stop of the timer.

Can I derive the time with nano second precision using these timers on the chip? And I need to do this specifically with a timer and not using any other means like a DWT.

Please let me know if there is a way to do this. Thanks a lot.

int i =1;
regValue = TIM3->CNT; 
printf("Timer value %d\n", regValue);
HAL_TIM_Base_Start(&htim3);
 
printf("Timer cycle %d\n", i);
i++; 
 
HAL_TIM_Base_Stop(&htim3);
regValue = TIM3->CNT;
printf("Timer value %d\n\n", regValue);

17 REPLIES 17
Uwe Bonnes
Principal II

Timer resolution is limited by the timer clock Maximum clock is 120 MHz, so you get 8.33 ms resolution.

SJant.1
Associate III

Hi Uwe Bonnes,

Thank you for your response. I figured out the max clock as 120MHz just after posting the above question.

As per your response, the resolution for a 120MHz clock should be 8.33ns rather than 8.33ms right?

Thanks!

Uwe Bonnes
Principal II

Off by one typing...

Javier1
Principal

If what you want to do is measure the time in nanoseconds in between events (for example width of a digital pulse)

you should take a look at the INPUT CAPTURE funcionality

and maybe use the timer with 0 preescaler and max clock speed

Just to confirm what you mean by "Off by one typing" is that you pressed m instead of n right? 🙂

Just wanted to add to the question,

so the max Period that can be set to TIM3 is 65535 since it is a 16bit timer. So once the period is completed I can see that the counter restarts from 0.

Now how can I keep track of the number of times that the period got completed so that at the final calculation I can calculate the entire time?

Sharing something on this would be really helpful. Thanks!

Use Timer overflow interrupt and count overflows. Be carefully when combining overflow count and timer count to a number > 16 Bit!

That sounds good.

order to use the interrupts, I should start and stop the timer with interruption right?

HAL_TIMx_BaseStart_IT()

HAL_TIMx_BaseStop_IT()

Hi Uwe,

Checkout my below test code, with this I dont see that the callback function

void HAL_GetTimerOverflow_Count(struct __TIM_HandleTypeDef *);
 
int main(void)
{
  /* USER CODE BEGIN 1 */
  uint32_t regValue;
  uint8_t cycles;
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
  MX_GPIO_Init();
  MX_TIM3_Init();
  HAL_TIM_Base_Start_IT(&htim3);
  TIM3->DIER = TIM_DIER_UIE_Msk;
  
  while (1)
  {
    
    regValue = TIM3->CNT ;
    printf("Timer value %d\n", regValue);
    for(int i=0; i<100000; i++);
    if(overflowCount_g > 0) {
      HAL_TIM_Base_Stop_IT(&htim3);
      regValue = TIM3->CNT;
      HAL_CalculateTime(regValue);
    }
   // printf("Timer value %d\n\n", regValue);
  }
  /* USER CODE END 3 */
}
 
void HAL_GetTimerOverflow_Count(struct __TIM_HandleTypeDef *htim)
{
  overflowCount_g += 1;
} 

is executed but I can see that the counter finished counting till 65535 and started recounting. I have enabled the Update Interrupt in the DIER register. In the call back function I am incrementing a global variable and checking the value to be more than 0 in while loop. But if I put a breakpoint in callback function, I never see that breakpoint being hit.

I have also set the USE_HAL_TIM_REGISTER_CALLBACKS in stm32l4xx_hal_conf.

Is there anything wrong in what I am doing?

Please let me know what do you think

Thanks