2025-10-07 6:12 AM - last edited on 2025-10-07 7:14 AM by Andrew Neil
Hi,
I am working on STM32G0c1RET6 board and i want to understand the working of low power timer (LPTIM) in stop mode.
i am using the HAL_LPTIM_Counter_Start_IT(&hlptim1, 65534) to enable LPTIM1 and print a value in function
void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim) {
if (hlptim->Instance == LPTIM1) {/* If instance called for HEATER DRIVER */
printf("1\r\n");
}
}
is it the correct way to print the value. because at some places i have seen using
HAL_LPTIM_TimeOut_Start_IT() function. which one is correct ??
Edited to apply source code formatting - please see How to insert source code for future reference.
2025-10-07 6:46 AM
HAL_LPTIM_Counter_Start_IT will start the counter and printf, if configured correctly, will print text.
Note that in STOP 0 or STOP 1 mode, the CPU is not running. It will have to wake up to execute code.
2025-10-07 7:18 AM
@Rohit007 wrote:is it the correct way to print the value
In general, it's not a good idea to be calling printf from an interrupt handler - especially if you have implemented your printf over a slow interface like UART.
@Rohit007 wrote:at some places i have seen using HAL_LPTIM_TimeOut_Start_IT() function
Where have you seen that? Please give a link...
2025-10-07 11:15 PM
@TDK Thanks for the reply,
i am facing an issue in generating the interrupt in LPTIM1, my observation is that whenever i enable the LPTIM1 using the HAL_LPTIM_Counter_Start_IT(&hlptim1,5112); function, it is not getting enabled and then when i enable the LPTIM2 using the function HAL_LPTIM_Counter_Start_IT(&hlptim2, 65534); it starts interrupt for LPTIM1 as well.
these are prints i am getting in LPTIM1 interrupt after enabling LPTIM2;
[2025-10-08 11:27:20.711] 1
[2025-10-08 11:27:20.711] 1
[2025-10-08 11:27:20.727] 1
[2025-10-08 11:27:20.743] 1
i have shared the LPTIM1 and LPTIM2 init function with its callback function.
static void MX_LPTIM1_Init(void)
{
/* USER CODE BEGIN LPTIM1_Init 0 */
/* USER CODE END LPTIM1_Init 0 */
/* USER CODE BEGIN LPTIM1_Init 1 */
/* USER CODE END LPTIM1_Init 1 */
hlptim1.Instance = LPTIM1;
hlptim1.Init.Clock.Source = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;
hlptim1.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV128;
hlptim1.Init.Trigger.Source = LPTIM_TRIGSOURCE_SOFTWARE;
hlptim1.Init.OutputPolarity = LPTIM_OUTPUTPOLARITY_HIGH;
hlptim1.Init.UpdateMode = LPTIM_UPDATE_IMMEDIATE;
hlptim1.Init.CounterSource = LPTIM_COUNTERSOURCE_INTERNAL;
hlptim1.Init.Input1Source = LPTIM_INPUT1SOURCE_GPIO;
hlptim1.Init.Input2Source = LPTIM_INPUT2SOURCE_GPIO;
if (HAL_LPTIM_Init(&hlptim1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN LPTIM1_Init 2 */
/* USER CODE END LPTIM1_Init 2 */
}
/**
* @brief LPTIM2 Initialization Function
* @param None
* @retval None
*/
static void MX_LPTIM2_Init(void)
{
/* USER CODE BEGIN LPTIM2_Init 0 */
/* USER CODE END LPTIM2_Init 0 */
/* USER CODE BEGIN LPTIM2_Init 1 */
/* USER CODE END LPTIM2_Init 1 */
hlptim2.Instance = LPTIM2;
hlptim2.Init.Clock.Source = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;
hlptim2.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV128;
hlptim2.Init.Trigger.Source = LPTIM_TRIGSOURCE_SOFTWARE;
hlptim2.Init.OutputPolarity = LPTIM_OUTPUTPOLARITY_HIGH;
hlptim2.Init.UpdateMode = LPTIM_UPDATE_IMMEDIATE;
hlptim2.Init.CounterSource = LPTIM_COUNTERSOURCE_INTERNAL;
hlptim2.Init.Input1Source = LPTIM_INPUT1SOURCE_GPIO;
if (HAL_LPTIM_Init(&hlptim2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN LPTIM2_Init 2 */
/* USER CODE END LPTIM2_Init 2 */
}
void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim) {
if (hlptim->Instance == LPTIM1) {/* If instance called for HEATER DRIVER */
HAL_GPIO_TogglePin(HEAT_DRIVER_GPIO_Port, HEAT_DRIVER_Pin);
printf("1\r\n");
}
if (hlptim->Instance == LPTIM2) {/* If instance called for HEATER DRIVER */
pan_delay++;
/* The pan motor power remains enabled for a few milliseconds after stopping to prevent motor sleep. */
if ( (pan_power_stop == MOTOR_ENABLE) && (pan_s.motor_enable == MOTOR_DISABLE) && (pan_delay > 10) ) {
pan_power_off();
refer_adc = true;
refer_delay = HAL_GetTick();
pan_drift_s.motor_drift = NULL_PT;
pan_delay = 0;
pan_power_stop = 0;
HAL_LPTIM_Counter_Stop_IT(&hlptim2);
}
}
}2025-10-07 11:19 PM
@Andrew Neil Thanks for the reply,
i have asked it on chatgpt and it suggested that
HAL_LPTIM_Counter_Start_IT() → triggers compare match interrupt, not auto-reload.
HAL_LPTIM_TimeOut_Start_IT() → triggers auto-reload match callback, but requires proper ARR & clock.
If you call the wrong start function, callback will never fire.
2025-10-08 12:58 AM - edited 2025-10-08 1:01 AM
@Rohit007 wrote:i have asked it on chatgpt and it suggested that
You always need to verify what so-called "AI" says!
You can check what those functions actually do in the Cube Firmware Pack documentation.
You could also look at the examples in the Pack ...
PS:
See also:
AN4013, Introduction to timers for STM32 MCUs
AN4776, General-purpose timer cookbook for STM32 microcontrollers
https://wiki.st.com/stm32mcu/wiki/Getting_started_with_TIM
2025-10-08 6:44 AM
The timers are independent. I suggest creating a minimal working example which shows the issue. Nothing wrong with the code you presented. Likely a bug in the code elsewhere.