2020-11-16 11:25 AM
I'm trying to implement a 10 us delay using TIM2. This delay is inside an EXTI interrupt handler.
I am not getting a return at the end of 10 us.
In the clock configurator, I used the max PLL clock possible, 64 MHz. Is this OK? I don't know what else to use here.
Edited to add: the purpose of this delay is to allow the external pulse to settle before determining its value. I am currently watching for the EXTI interrupt and then giving a delay. Is there a better way to do this with the timer settings?
Thanks,
Priya
void delay_us (uint16_t us)
{
uint32_t start = TIM2->CNT;
while ((TIM2->CNT - start) % 65536U < us); // wait for the counter to reach the us input in the parameter
}
static void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
/* USER CODE END TIM2_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM2_Init 1 */
__HAL_RCC_TIM2_CLK_ENABLE();
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 64-1;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 0xffff;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_Init 2 */
}
Solved! Go to Solution.
2020-11-16 01:16 PM
Did you start the timer in main (code not shown)?
2020-11-16 12:14 PM
Make sure you start the TIM, watch in count in the debugger
Keep the numbers in unsigned 16-bit space, the math works out easier.
void delay_us (uint16_t us)
{
uint16_t start = TIM2->CNT;
while ((TIM2->CNT - start) < us); // wait for the counter to reach the us input in the parameter
}
2020-11-16 12:16 PM
On the G071 TIM2 is 32-bit, perhaps use an 0xFFFFFFFF maximal count, and then 32-bit variables/math, more efficient.
void delay_us (uint32_t us)
{
uint32_t start = TIM2->CNT;
while ((TIM2->CNT - start) < us); // wait for the counter to reach the us input in the parameter
}
2020-11-16 12:26 PM
I made all the delay variables 32 bit and used a 32 bit max timer value. I don't see the timer incrementing at all in the debugger.
Thanks,
Priya
2020-11-16 01:16 PM
Did you start the timer in main (code not shown)?
2020-11-16 01:28 PM
Thank you. It is a long learning curve with a tool generating API code and no MCU specific documentation to use the API. Appreciate the help.
2020-11-16 02:50 PM
> no MCU specific documentation to use the API.
At the top of ever HAL source file, there are instruction on how to use it. In my opinion, those are generally well written and are the best places to look.
The second best place to look are the examples in the repository to see how things are used.
https://github.com/STMicroelectronics/STM32CubeG0/tree/master/Projects
In either case, should be plenty of resources for you to lean on to learn. Agreed the learning curve can be steep.