2025-04-20 4:44 PM
Hello.
I am facing a problem: timer tim2 is not working with initialized ble and tasks and timers HW_TS (button and LED processing). Just in case, I checked that TIM16 is working, but it is busy with another task, so I can't use it. I've tried many things, but all I get is either the interrupt triggering instantly and going to hard fault, or just going to an error handler, or nothing happening.
static void MX_TIM2_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 31999;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 2999;
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();
}
}
If I comment out all the initialization MX_APPE_Init(); and MX_APPE_Process(); then the timer will work.
Please help me solve this problem. Let me know if you need any additional information or code.
Solved! Go to Solution.
2025-04-25 2:01 AM
Hello,
I have tried your project and TIM2 works fine with BLE. One reason you're getting a hardfault is because the task
CFG_TASK_BLE_EXIT_ID wasn't registered in the sequencer and the second one is that the callback HAL_TIM_PeriodElapsedCallback is called before proper BLE initialization. Please try to call HAL_TIM_Base_Start_IT(&htim2) function at the end of APPE_SysUserEvtRx function to make sure BLE is initialized properly.
Best regards,
ST support
2025-04-20 5:33 PM
Why do you think it's not working? What in particular isn't happening that you expect to happen?
Nothing wrong with the code presented, but it doesn't even start the timer. No doubt the issue is somewhere else. Perhaps the timer is never started. Perhaps code is stuck elsewhere.
2025-04-21 12:56 AM
Hello. The timer is started with
HAL_TIM_Base_Start_IT(&htim2);
MX_APPE_Init();
InitializeAppButtonHandling(); // Creates HW timers
RegisterSequencerTasks(); // Registers all sequencer tasks (buttons, etc.)
while (1)
{
MX_APPE_Process();
}
And the callback is
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == TIM16) {
HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
}
else if (htim->Instance == TIM2)
{
if (initial_timer_state == 0) {
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
//UTIL_SEQ_SetTask(1 << CFG_TASK_BLE_EXIT_ID, CFG_SCH_PRIO_0);
initial_timer_state = 1;
uint32_t twenty_minutes_ticks = 3000 - 1;
//__HAL_TIM_SET_AUTORELOAD(&htim2, twenty_minutes_ticks);
//__HAL_TIM_SET_COUNTER(&htim2, 0);
} else if (initial_timer_state == 1) {
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
initial_timer_state = 2;
} else {
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
}
}
}
If I delete MX_APPE_Init(); and don't use the sequencer MX_APPE_Process(), the pin blinks.
2025-04-21 5:23 AM
> but all I get is either the interrupt triggering instantly and going to hard fault, or just going to an error handler
Hard faults should not be ignored. I'd solve that one first. If things go to an error handler, find out why and fix.
Sounds like it may be a stack corruption issue. Perhaps insufficient stack size.
2025-04-21 6:15 AM
> but all I get is either the interrupt triggering instantly and going to hard fault, or just going to an error handler.
This will happen if I use those commented lines with counter reset, or interrupt enable, or if I stop the timer in the handler. Without this, everything works anyway, but only if there is no ble
2025-04-23 12:50 AM
Hello,
could you please share the project?
Best regards,
Filip Kremen
2025-04-24 8:14 AM
2025-04-25 2:01 AM
Hello,
I have tried your project and TIM2 works fine with BLE. One reason you're getting a hardfault is because the task
CFG_TASK_BLE_EXIT_ID wasn't registered in the sequencer and the second one is that the callback HAL_TIM_PeriodElapsedCallback is called before proper BLE initialization. Please try to call HAL_TIM_Base_Start_IT(&htim2) function at the end of APPE_SysUserEvtRx function to make sure BLE is initialized properly.
Best regards,
ST support
2025-04-25 10:02 AM
Hello.
Calling the HAL_TIM_Base_Start_IT(&htim2) function at the end of the APPE_SysUserEvtRx function worked, even though the timer was still triggering instantly for some reason. This command before starting the timer __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_UPDATE), helped to remove the instantaneous call. Even starting the timer in the main function works properly with this command.
Thank you very much for your help!