cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G0B1 Timer 6 - program crash when timer started with interrupt

PPate.1
Associate II

Hi,

I am using STM32G0B1VET6 on my custom board. I want a delay of 0.2 and 0.3us. Hence I have used timer 6 to achieve this delay. But my program leads to crash when API - HAL_TIM_Base_Start_IT(&htim6) is executed.

My settings for Timer 6 are as follows:

PPate1_0-1741181859622.png

PPate1_2-1741182082811.png

 

My clock configuration is as given below:

PPate1_1-1741182015801.png

Generated code:

static void MX_TIM6_Init(void)

{

 

/* USER CODE BEGIN TIM6_Init 0 */

 

/* USER CODE END TIM6_Init 0 */

 

TIM_MasterConfigTypeDef sMasterConfig = {0};

 

/* USER CODE BEGIN TIM6_Init 1 */

 

/* USER CODE END TIM6_Init 1 */

htim6.Instance = TIM6;

htim6.Init.Prescaler = 0;

htim6.Init.CounterMode = TIM_COUNTERMODE_UP;

htim6.Init.Period = 13;

htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

if (HAL_TIM_Base_Init(&htim6) != HAL_OK)

{

Error_Handler();

}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN TIM6_Init 2 */

 

/* USER CODE END TIM6_Init 2 */

 

}

 

void TIM6_DAC_LPTIM1_IRQHandler(void)

{

/* USER CODE BEGIN TIM6_DAC_LPTIM1_IRQn 0 */

 

/* USER CODE END TIM6_DAC_LPTIM1_IRQn 0 */

HAL_TIM_IRQHandler(&htim6);

/* USER CODE BEGIN TIM6_DAC_LPTIM1_IRQn 1 */

delay_flag = true;

/* USER CODE END TIM6_DAC_LPTIM1_IRQn 1 */

}

My code for delay function:

void delay_us(uint16_t delay)

{

delay_flag = false;

htim6.Init.Period = delay;

if (HAL_TIM_Base_Start_IT(&htim6) != HAL_OK) //this function crash here

return;

 

while(!delay_flag);

}

 

 

Please guide.

Thanks,

pradeep

5 REPLIES 5
TDK
Guru

What does HAL_TIM_Base_Start_IT return?

 

You're going to have a tough time getting a 0.3 us delay with HAL IT functions. Lots of overhead in there that are going to add up to more than 0.3 us of time.

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

Program crash and hangup. There is no return value I can check.

I am ok with timing becomes 0.3us to 0.5us.

What does "program crash and hangup" mean? Are you getting an error message? Show it in full.

Since this is a custom board, consider showing your schematic. Ensure decoupling caps are present and rails are steady.

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

I tried debugging to check what exactly is happening when program hang up. During my step by step debugging in main() function, i have following observation:

If i set breakpoint then I get cross line over breakpoint and execution does not stop at breakpoint. My compiler optimization level is set as -0.

When I debug from HAL_Init() function in main(), it goes through all functions successfully (F6 - step over) but when it comes to function delay_us() and if F6 - step over is done then debug cursor (green highlight over function name) gets lost. No error message and screen just remains at that point till I suspend debugging. 

If I do F5 - step Into on delay_us() function then it does goes inside the function and goes through all lines of code successfully and does come out of this function too. But then when it goes to next function - Test_Display() after delay_us(), then again If I do step over or step into, debugging cursor gets lost i.e. green highlight over function name is lost and screen just remains at that point without any error message till I suspend debugging.

PPate1_0-1741194213461.png

I have one more timer - TIM14 which is triggering interrupt every 1ms and this is executing good without any issue.

Schematic for power section of microcontroller is as below:

PPate1_1-1741194349747.png

 

 

TDK
Guru

You are not allowed to return from main(). Put a "while (1);" loop at the end.

> ...and if F6 - step over is done then debug cursor (green highlight over function name) gets lost. No error message and screen just remains at that point till I suspend debugging. 

Hit the "pause" icon to stop the code where it's at.

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