I\'m new to using HAL functions. The description of the function HAL_GetTick()
says that it \"provides a tick value in millisecond
yI needed a timestamp at 1uS precision, and using TIM5 as described above worked, but a few tweaks were necessary. Here's what I came up with.
/* Initialization */
__HAL_RCC_TIM5_CLK_ENABLE();
TIM5->PSC = HAL_RCC_GetPCLK1Freq() / 500000;
TIM5->CR1 = TIM_CR1_CEN;
TIM5->CNT = -10;
/* Reading the time */
uint32_t microseconds = TIM5->CNT << 1;
I did not fully explore why I had to do what I did. But I realized two things very quickly. (1) The prescalar scaling was not working, although it looked right. This was one of several things I tried to get it to work (basically a half-us clock, and divide the result by 2). (2) The clock was already running, and gave strange results at first. I tried several unsuccessful things to stop, reprogram and restart it, and setting the count to -10 was a crude but effective way to just let it complete its current cycle, then very quickly start working as desired. There are certainly better ways of achieving this. But overall this is a simple way of getting an accurate event timestamp with very low overhead.