2022-08-16 11:16 PM
Hi. I am using STM32L412KBTx for my application. I am using LPTIM for 150ms and 5s timings.
The pattern is like 150ms-->5s-->150ms whenever data is received over LPUART.
So, for first 150ms timeout value, it runs fine and when I start for 5s timeout value, it returns HAL_TIMEOUT.
(NOTE: I stop tier before starting again)
Below is the code from stm32l4xx_hal_lptim.c where i returns HAL_TIMEOUT:
/* Wait for the completion of the write operation to the LPTIM_ARR register */
if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
{
return HAL_TIMEOUT;
}
Here are the screenshots of LPTIM registers:
Any Idea what is happening and how can I solve it?
2022-08-17 02:56 PM
Perhaps show more of your code? Specifically the HAL_LPTIM calls where you start/stop/restart/update the timer or timer regs.
2022-08-17 09:31 PM
Sure. Here's how I start my timer:
void start_timer(uint32_t tim_val)
{
if(HAL_LPTIM_Counter_Start_IT(&hlptim1, tim_val)!=HAL_OK)
Error_Handler();
}
Heres how I stop timer:
void stop_timer(void)
{
if(HAL_LPTIM_Counter_Stop(&hlptim1)!=HAL_OK)
Error_Handler();
}
Here's my state machine for the motor operation:
void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim)
{
stop_timer();
switch(motor_state)
{
case REVERSE:
{
stop_reverse_motor();
start_timer(DELAY_5_SEC); // DELAY_5_SEC = 0xA000
}
break;
case STANDBY:
{
start_forward_motor();
start_timer(DELAY_150_MS); // DELAY_150_MS = 0x4CC
}
break;
case FORWARD:
{
stop_forward_motor();
}
break;
default:break;
}
}
In the above case, it works fine but if I create separate function as shown below and execute it from main, it returns HAL_TIMEOUT.
void void MOTOR_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim)
{
stop_timer();
scheduler_put(motor_callback); //put the function in task scheduler for execution from main
}
void motor_callback(void)
{
stop_timer();
switch(motor_state)
{
case REVERSE:
{
stop_reverse_motor();
start_timer(DELAY_5_SEC); // DELAY_5_SEC = 0xA000
}
break;
case STANDBY:
{
start_forward_motor();
start_timer(DELAY_150_MS); // DELAY_150_MS = 0x4CC
}
break;
case FORWARD:
{
stop_forward_motor();
}
break;
default:break;
}
}
2022-08-18 12:07 PM
I'm a bit rusty on HRTIM functionality... You aren't using the ARR preload function, so I would expect it to load immediately. Two differences I see between the working and non-working code:
Is there an HRTIM interrupt still active that may be trying to handle/clear the ARROK interrupt flag while start_timer() is calling the HAL_LPTIM_Counter_Start() function?
Memory corruption/stack overflow? (grasping at straws here)