cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G071 Nucleo 64 microsecond delay timer

PNada.1
Associate II

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 */

}

1 ACCEPTED SOLUTION

Accepted Solutions

Did you start the timer in main (code not shown)?

View solution in original post

6 REPLIES 6

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

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

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

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
PNada.1
Associate II

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

Did you start the timer in main (code not shown)?

PNada.1
Associate II

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.

TDK
Guru

> 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.

https://github.com/STMicroelectronics/STM32CubeG0/blob/master/Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim.c

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.

If you feel a post has answered your question, please click "Accept as Solution".