Needed LPTIMER valid reinitialize sequence
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-03 1:32 AM - edited ‎2025-06-03 1:33 AM
Hello,
I have problem statement, in which I have to re-Initialize lptimer for different timer value, as per current scenario, I have
Clock: 32Khz (LSI)
Divisor = 128
Period: Any time as per dynamic input.
So I want information about how I can safely reset the lptimer and then again re-init for different timer. Here is the reference lptimer code. where
/* uint32_t effectiveFreq = 32000/ 128; // 250 Hz
uint32_t tickTimeInMS = 1000 / effectiveFreq; // 4 ms*/
#define TICK_TIME_IN_MS 4
void ReInit_LPTIM1(uint32_t timerInMS)
{
/*lptimer reset block start*/
HAL_LPTIM_IC_Stop_IT(&hlptim1, LPTIM_CHANNEL_1);
__HAL_LPTIM_DISABLE(&hlptim1);
if (HAL_LPTIM_DeInit(&hlptim1) != HAL_OK)
{
Error_Handler();
}
__HAL_RCC_LPTIM1_FORCE_RESET();
__HAL_RCC_LPTIM1_RELEASE_RESET();
/*lptimer reset block end*/
LPTIM_OC_ConfigTypeDef sConfig1 = {0};
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.UpdateMode = LPTIM_UPDATE_IMMEDIATE;
hlptim1.Init.CounterSource = LPTIM_COUNTERSOURCE_INTERNAL;
hlptim1.Init.Input1Source = LPTIM_INPUT1SOURCE_GPIO;
hlptim1.Init.Input2Source = LPTIM_INPUT2SOURCE_GPIO;
hlptim1.Init.RepetitionCounter = 0;
hlptim1.Init.Period = timerInMS/TICK_TIME_IN_MS - 1 ;
if (HAL_LPTIM_Init(&hlptim1) != HAL_OK)
{
Error_Handler();
}
sConfig1.Pulse = hlptim1.Init.Period;
sConfig1.OCPolarity = LPTIM_OCPOLARITY_HIGH;
if (HAL_LPTIM_OC_ConfigChannel(&hlptim1, &sConfig1, LPTIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
__HAL_LPTIM_ENABLE_IT(&hlptim1, LPTIM_IT_CC1);
HAL_LPTIM_IC_Start_IT(&hlptim1, LPTIM_CHANNEL_1);
}
Here, adding reset block is not working correctly, and if we remove this reset block then it works fine. But I want it should be clear/reset before re-initialize for safe working for longer run.
Thanks
Nitin
Solved! Go to Solution.
- Labels:
-
STM32U0 Series
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-03 1:45 AM
Here’s a modified ReInit_LPTIM1() approach that should work more reliably and be safe for long-term use without hard-resetting the peripheral at the RCC level:
void ReInit_LPTIM1(uint32_t timerInMS)
{
// Step 1: Disable LPTIM and stop channel
__HAL_LPTIM_DISABLE(&hlptim1);
HAL_LPTIM_IC_Stop_IT(&hlptim1, LPTIM_CHANNEL_1);
// Step 2: Disable interrupt
__HAL_LPTIM_DISABLE_IT(&hlptim1, LPTIM_IT_CC1);
// Step 3: DeInit peripheral
if (HAL_LPTIM_DeInit(&hlptim1) != HAL_OK)
{
Error_Handler();
}
// Step 4: Reconfigure peripheral without RCC force-reset
LPTIM_OC_ConfigTypeDef sConfig1 = {0};
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.UpdateMode = LPTIM_UPDATE_IMMEDIATE;
hlptim1.Init.CounterSource = LPTIM_COUNTERSOURCE_INTERNAL;
hlptim1.Init.Input1Source = LPTIM_INPUT1SOURCE_GPIO;
hlptim1.Init.Input2Source = LPTIM_INPUT2SOURCE_GPIO;
hlptim1.Init.RepetitionCounter = 0;
// Step 5: Calculate and set new period
hlptim1.Init.Period = timerInMS / TICK_TIME_IN_MS - 1;
if (HAL_LPTIM_Init(&hlptim1) != HAL_OK)
{
Error_Handler();
}
sConfig1.Pulse = hlptim1.Init.Period;
sConfig1.OCPolarity = LPTIM_OCPOLARITY_HIGH;
if (HAL_LPTIM_OC_ConfigChannel(&hlptim1, &sConfig1, LPTIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
// Step 6: Re-enable interrupt and start again
__HAL_LPTIM_ENABLE_IT(&hlptim1, LPTIM_IT_CC1);
HAL_LPTIM_IC_Start_IT(&hlptim1, LPTIM_CHANNEL_1);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-03 1:45 AM
Here’s a modified ReInit_LPTIM1() approach that should work more reliably and be safe for long-term use without hard-resetting the peripheral at the RCC level:
void ReInit_LPTIM1(uint32_t timerInMS)
{
// Step 1: Disable LPTIM and stop channel
__HAL_LPTIM_DISABLE(&hlptim1);
HAL_LPTIM_IC_Stop_IT(&hlptim1, LPTIM_CHANNEL_1);
// Step 2: Disable interrupt
__HAL_LPTIM_DISABLE_IT(&hlptim1, LPTIM_IT_CC1);
// Step 3: DeInit peripheral
if (HAL_LPTIM_DeInit(&hlptim1) != HAL_OK)
{
Error_Handler();
}
// Step 4: Reconfigure peripheral without RCC force-reset
LPTIM_OC_ConfigTypeDef sConfig1 = {0};
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.UpdateMode = LPTIM_UPDATE_IMMEDIATE;
hlptim1.Init.CounterSource = LPTIM_COUNTERSOURCE_INTERNAL;
hlptim1.Init.Input1Source = LPTIM_INPUT1SOURCE_GPIO;
hlptim1.Init.Input2Source = LPTIM_INPUT2SOURCE_GPIO;
hlptim1.Init.RepetitionCounter = 0;
// Step 5: Calculate and set new period
hlptim1.Init.Period = timerInMS / TICK_TIME_IN_MS - 1;
if (HAL_LPTIM_Init(&hlptim1) != HAL_OK)
{
Error_Handler();
}
sConfig1.Pulse = hlptim1.Init.Period;
sConfig1.OCPolarity = LPTIM_OCPOLARITY_HIGH;
if (HAL_LPTIM_OC_ConfigChannel(&hlptim1, &sConfig1, LPTIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
// Step 6: Re-enable interrupt and start again
__HAL_LPTIM_ENABLE_IT(&hlptim1, LPTIM_IT_CC1);
HAL_LPTIM_IC_Start_IT(&hlptim1, LPTIM_CHANNEL_1);
}
