STM32G071 Nucleo 64 microsecond delay timer
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 */
}