cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_LPTIM_Counter_Start_IT returning HAL_TIMEOUT

SBhon.1
Associate III

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:

0693W00000QNrgAQAT.png0693W00000QNrgPQAT.png0693W00000QNrgjQAD.png0693W00000QNrhIQAT.pngAny Idea what is happening and how can I solve it?

3 REPLIES 3
Bob S
Principal

Perhaps show more of your code? Specifically the HAL_LPTIM calls where you start/stop/restart/update the timer or timer regs.

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;
 
	}
 
}

Bob S
Principal

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:

  • non-working calls timer_stop() 2 times. Probably not an issue but worth a test.
  • working code calls start_timer() from an interrupt context (presuming the "Callback" function is a normal HAL IRQ callback). Non-working code calls from normal non-interrupt context.

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)