HAL_TIM_Base_Start_IT hanging

Post here first, or if you can't find a relevant section!
Post Reply
jay_stm
Posts: 11
Joined: Mon Oct 18, 2021 2:33 pm

HAL_TIM_Base_Start_IT hanging

Post by jay_stm »

I'm having issues with timer interrupts.
None of the handlers seem to get called and the program "hangs" after HAL_TIM_Base_Start_IT is called.
Arduino IDE 1.8.19, stm32h743zi2 nucleo board, stm32 libraries 2.4.0.
To eliminate other issues i made a very small program that has a timer with around 1 second time. If i use HAL_TIM_Base_Start the UIF is set after the expired time, if i reset it, back on again after the expired time, so the timer is actually working.
But when i either enable interrupts or start with HAL_TIM_Base_Start_IT the next instructions are not reached, the interrupt handlers do not seem to get called.
What did i try? different timers, setup the same configuration in the stm32cubeide and it works fine there, tried to disable the hardwaretimer function, clear the UIF flag before calling other functions...
I already have 7000 lines of code in the project im working on, all running but i need to add a function to measure a frequency of an external signal using two timers, either i get error messages on irq handler that the function is already defined in hardwaretimer or the program hangs after trying to initialise interrupts.
No luck...

Here is the program:


// orange LED
#define UserLED_ORANGE PE1
// red LED
#define UserLED_RED PB14

boolean toggle_led = false;

TIM_HandleTypeDef htim4;

void setup() {

Serial.begin(115200);
while (!Serial) {
}

// blink orange & red leds
pinMode(UserLED_ORANGE, OUTPUT);
digitalWrite(UserLED_ORANGE, HIGH);
pinMode(UserLED_RED, OUTPUT);
digitalWrite(UserLED_RED, HIGH);
delay(100);
digitalWrite(UserLED_ORANGE, LOW);
digitalWrite(UserLED_RED, LOW);

HAL_Init();

SystemClock_Config();

MX_TIM4_Init();

if (HAL_TIM_Base_Start_IT(&htim4) != HAL_OK)
{
error1();
}

}

void loop() {

if (TIM4->SR & 0x01 == 0x01) { //test UIF
Serial.println(TIM4->SR, BIN);
Serial.println(TIM4->DIER, BIN);
Serial.println(" ");
TIM4->SR &= ~0x01; //clear UIF
}


}



void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}

__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);

while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 120;
RCC_OscInitStruct.PLL.PLLP = 2;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 2;
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
RCC_OscInitStruct.PLL.PLLFRACN = 0;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
error1();
}

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2
| RCC_CLOCKTYPE_D3PCLK1 | RCC_CLOCKTYPE_D1PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
error1();
}
}


void error1(void) {
while (1) {
digitalWrite(UserLED_RED, HIGH);
delay(50);
digitalWrite(UserLED_RED, LOW);
delay(50);
}
}



void TIM4_IRQHandler(void)
{
if (toggle_led) {
digitalWrite(UserLED_ORANGE, HIGH);
toggle_led = false;
}
else {
digitalWrite(UserLED_ORANGE, LOW);
toggle_led = true;
}
HAL_TIM_IRQHandler(&htim4);
}

void MX_TIM4_Init(void)
{

__HAL_RCC_TIM4_CLK_ENABLE();

/* TIM4 interrupt Init */
HAL_NVIC_SetPriority(TIM4_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM4_IRQn);

TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};

htim4.Instance = TIM4;
htim4.Init.Prescaler = 10000;
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
htim4.Init.Period = 25000;
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
{
error1();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
{
error1();
}

}
jay_stm
Posts: 11
Joined: Mon Oct 18, 2021 2:33 pm

Re: HAL_TIM_Base_Start_IT hanging

Post by jay_stm »

After moving these to the bottom of the init routine and disabling the hardwaretimer function, i got it working.

/* TIM4 interrupt Init */
HAL_NVIC_SetPriority(TIM4_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM4_IRQn);
jay_stm
Posts: 11
Joined: Mon Oct 18, 2021 2:33 pm

Re: HAL_TIM_Base_Start_IT hanging

Post by jay_stm »

Not there yet... the small demo program works but my large program not, same issue.
I learned that the UIF is set after initialisation so if the interrupt is enabled the interrupt routine is called immediately but my interrupt routine is never called, instead the program hangs.
If i clear the UIF after initialisation and before starting the timer in interrupt mode, the program continues until the first timer timeout occurs triggering the interrupt and then it hangs.
So it looks like the linkage to the interrupt routine is broken somehow.
Post Reply

Return to “General discussion”