cancel
Showing results for 
Search instead for 
Did you mean: 

LPTIMER Counter for Delay in STOP2 mode not working

npatil15
Associate III

Hello,

I have application which go to STOP2 mode once no LPUART/Modbus communication (Running at 32Khz LSI clock) is in progress.

Using lptimer for silence timeout logic for UART data frame completion. The code sometimes cause delay during modbus communication.

void setLowPowerMode()
{
  clearFaultStatus();
  ResetTickMs();
  HAL_UARTEx_EnableStopMode(&hlpuart1);
  HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);
  ResetTickMs();
  Reset_SystemClock();//internally call SystemClock_Config()
  HAL_LPTIM_Init(&hlptim2); //Need to re-init otherwise doesnt work
  HAL_LPTIM_Counter_Start(&hlptim2);
  HAL_UARTEx_DisableStopMode(&hlpuart1);
}

 

uint32_t GetTickMs(void)
{
  uint32_t now = HAL_LPTIM_ReadCounter(&hlptim2);

  uint32_t delta;
  if (now >= lptim_last_cnt)
  {
    delta = now - lptim_last_cnt;
  }
  else
  {
    delta = 65536U + now - lptim_last_cnt;  // wrap-safe
  }

  lptim_last_cnt = now;

  // Add fractional milliseconds in fixed-point
  lptim_frac_acc += delta * 1000UL;

  // Extract whole milliseconds
  uint32_t ms = lptim_frac_acc / LPTIM_FREQ_HZ;
  lptim_frac_acc -= ms * LPTIM_FREQ_HZ;

  lptim_ms_acc += ms;

  return lptim_ms_acc;
}

void ResetTickMs(void)
{
  lptim_last_cnt  = HAL_LPTIM_ReadCounter(&hlptim2);
  lptim_ms_acc   = 0;
  lptim_frac_acc = 0;
}
  void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
  {
     RxTime = GetTickMs();
     RxSize += Size;
  }

void rxStart()
{
  if ((GetTickMs() - RxTime) > 3ms)
  {
    RxSize = (RxSize < UART_RX_BUF_SIZE) ? RxSize : UART_RX_BUF_SIZE;
  }
  else
  {
   //Again set the interrupt, here it works fine
  }
}

 

Here if we disable the function of "setLowPowerMode()" then it works fine, but ocne enable it works but cause some random delays. 

My only intention is to use lptimer counter as delay purpose. I have tired systick but it also has some issue as we need to suspend and resume, which casue issue when we done it multiple times. Thats why I moved to lptimer which works in low power mode.

Help me with suggestions, what wrong in GetTickMs() or does lptimer re-init need to be at correct place ?

Thanks,

Nitin

0 REPLIES 0