cancel
Showing results for 
Search instead for 
Did you mean: 

LPTIM_COUNTER_START Problem

GFont.1
Associate III

Hi,

I am using stm32431. I config LPTIM and call HAL_LPTIM_Counter_Start() but return HAL_TIMEOUT and no pulses is count. When I change HAL_LPTIM_Counter_Start() function putting 'HAL_OK' to be return forced, the pulses is counted normally, but not in every time, some yes some no.

The function that i changed:

if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)

 {

 // return HAL_TIMEOUT;

return HAL_OK;

 }

Why is returned HAL_TIMEOUT ?

P.S I know that need 5 pulses to begin count.

6 REPLIES 6
GFont.1
Associate III

as a matter of fac, I comment "if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)", not put return OK as I said, so the waitforflag is ignored.

prain
Senior III

Use a debugger to inspect timer registers. Check register values with reference manual to ​verify correct bits are set

S.Ma
Principal

On STM32L4R I use LPTIM to drive buzzer audio notes or short delays in used. Code extract: (48MHz SYSCLK)

Maybe you'll find some clues from this?

extern LPTIM_HandleTypeDef hlptim1;
 
void LPTIM1_Init(void) {
  LPTIM_HandleTypeDef* hlptim = &hlptim1;
  // Set the LPTIM2 clock to PCLK because HSI16 is off
//  RCC->CCIPR &= ~(3<<20);
//  RCC->CCIPR |=  (0<<20); 48 MHz = MSI = APB
  HAL_NVIC_SetPriority(LPTIM1_IRQn, 2/*0*/, 0);
  HAL_NVIC_EnableIRQ(LPTIM1_IRQn);
  hlptim->Instance->CFGR |= LPTIM_PRESCALER_DIV16; // 3 ticks = 1 us
  /* Set WAVE bit to enable the set once mode */
  hlptim->Instance->CFGR |= LPTIM_CFGR_PRELOAD;// LPTIM_CFGR_WAVE;
 
  /* Enable Autoreload match interrupt */
  hlptim->Instance->ICR |= 0x0002; // ARRMCF clear flag  
  __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
 
  /* Enable Compare match interrupt */
//  __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
 
  /* Enable the Peripheral */
//  __HAL_LPTIM_ENABLE(hlptim);
}
 
void LPTIM1_SetTick_us(uint32_t delay_us) {
  LPTIM_HandleTypeDef* hlptim = &hlptim1;
  
  if((delay_us/3)>0xFFFF) TrapError(); // change the clock prescaler if looking at more than one milisecond
  if(delay_us==0) {
    // turn the timer event off
    __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
    __HAL_LPTIM_DISABLE(hlptim);
    return; // done
  }
 
  /* Enable Autoreload match interrupt */
  __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
 
  /* Enable Compare match interrupt */
//  __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
 
  /* Enable the Peripheral */
  __HAL_LPTIM_ENABLE(hlptim);
 
  /* Load the period value in the autoreload register */
  __HAL_LPTIM_AUTORELOAD_SET(hlptim, delay_us * 3);
 
  /* Start timer in freerun mode */
  __HAL_LPTIM_START_CONTINUOUS(hlptim);
}
 
void LPTIM1_SetPWM_us(uint32_t delay_us) { // this delay is the half period
 
  LPTIM_HandleTypeDef* hlptim = &hlptim1;
  
  if((delay_us/3)>0xFFFF) TrapError(); // change the clock prescaler if looking at more than one milisecond
  if(delay_us==0) {
    // turn the timer event off
    __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
    __HAL_LPTIM_DISABLE(hlptim);
    return; // done
  }
 
  /*HAL_StatusTypeDef*/ HAL_LPTIM_PWM_Start(hlptim, delay_us * 3 * 2, delay_us * 3);
  return;
  
  __HAL_LPTIM_COMPARE_SET(hlptim, delay_us * 3);  
  
  /* Enable Autoreload match interrupt */
//-  __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
  __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
 
  /* Enable Compare match interrupt */
//  __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
 
  /* Enable the Peripheral */
  __HAL_LPTIM_ENABLE(hlptim);
 
  /* Load the period value in the autoreload register */
  __HAL_LPTIM_AUTORELOAD_SET(hlptim, delay_us * 3 * 2); // it becomes the period
 
  /* Start timer in freerun mode */
  __HAL_LPTIM_START_CONTINUOUS(hlptim);
}
 
/*
void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim) { 
  // Timer is one shot mode, so it should trigger only once after kitstarted
  NOPs(1);
}
*/
 
 
void LPTIM1_IRQHandler(void) { 
//  HAL_LPTIM_IRQHandler(&hlptim1); // ==>   HAL_LPTIM_AutoReloadMatchCallback(&hlptim2); 
  
  /* Autoreload match interrupt */
  if(__HAL_LPTIM_GET_FLAG(&hlptim1, LPTIM_FLAG_ARRM) != RESET)
  {
    if(__HAL_LPTIM_GET_IT_SOURCE(&hlptim1, LPTIM_IT_ARRM) != RESET)
    {
      /* Clear Autoreload match flag */
      __HAL_LPTIM_CLEAR_FLAG(&hlptim1, LPTIM_FLAG_ARRM);
 
      /* Autoreload match Callback */
      ...
    }
  }
}

What is the LPTIM clock?

JW

Thank you !!!!

I tried, LSE, HSE, HSI. The same timeout. The problem is in 'LPTIM_FLAG_ARROK' I think.