cancel
Showing results for 
Search instead for 
Did you mean: 

5 minute Timer (LPTIM1) STM32L073

Simon NOWAK
Associate II
Posted on March 30, 2018 at 10:33

Hi everyone, 

I would like to use LPTIM1 on my STM32L0 to make it wake up (the micro is in Stop Mode) after 5 minutes but I don't know what is the best configuration to do that. Currently I have the configuration below.

&sharpinclude 'Lib_Timer.h'

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

LPTIM_HandleTypeDef lptimHandle;

uint32_t cptCallBack = 0;

/* LPTIM1 init function */

void MX_LPTIM1_Init(void)

{

lptimHandle.Instance = LPTIM1;

lptimHandle.Init.Clock.Source = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;

lptimHandle.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV128;

lptimHandle.Init.Trigger.Source = LPTIM_TRIGSOURCE_SOFTWARE;

lptimHandle.Init.OutputPolarity = LPTIM_OUTPUTPOLARITY_HIGH;

lptimHandle.Init.UpdateMode = LPTIM_UPDATE_IMMEDIATE;

lptimHandle.Init.CounterSource = LPTIM_COUNTERSOURCE_INTERNAL;

if (HAL_LPTIM_Init(&lptimHandle) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

}

void HAL_LPTIM_MspInit(LPTIM_HandleTypeDef* lptimHandle)

{

if(lptimHandle->Instance==LPTIM1)

{

      /* USER CODE BEGIN LPTIM1_MspInit 0 */

      /* USER CODE END LPTIM1_MspInit 0 */

      /* LPTIM1 clock enable */

      __HAL_RCC_LPTIM1_CLK_ENABLE();

      /* LPTIM1 interrupt Init */

      HAL_NVIC_SetPriority(LPTIM1_IRQn, 0, 0);

      HAL_NVIC_EnableIRQ(LPTIM1_IRQn);

      /* USER CODE BEGIN LPTIM1_MspInit 1 */

      /* USER CODE END LPTIM1_MspInit 1 */

}

}

void HAL_LPTIM_MspDeInit(LPTIM_HandleTypeDef* lptimHandle)

{

if(lptimHandle->Instance==LPTIM1)

{

      /* USER CODE BEGIN LPTIM1_MspDeInit 0 */

      /* USER CODE END LPTIM1_MspDeInit 0 */

      /* Peripheral clock disable */

      __HAL_RCC_LPTIM1_CLK_DISABLE();

      /* LPTIM1 interrupt Deinit */

      HAL_NVIC_DisableIRQ(LPTIM1_IRQn);

      /* USER CODE BEGIN LPTIM1_MspDeInit 1 */

      /* USER CODE END LPTIM1_MspDeInit 1 */

}

}

void Start_Counter(uint32_t period){

      HAL_LPTIM_TimeOut_Start_IT(&lptimHandle, period, 0x0F);

}

void Stop_Counter( void ){

      HAL_LPTIM_Counter_Stop_IT(&lptimHandle);

}

uint32_t Read_Counter( void ){

      return HAL_LPTIM_ReadCounter(&lptimHandle);

}

void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim){

      cptCallBack++;

}

uint32_t get_cptCallBack( void ){

      return cptCallBack;

}

I don't know if I use the good callback and the good configuration, currently my callback increase my counter each second, it's too fast (when I call '

Start_Counter' I put period = 0xFFFFF).

Have you any ideas or advices ?

Thank's a lot

Regards

Simon

#deepsleep #stm32l073 #stm32l073rz #lptim #stopmode
4 REPLIES 4
RomainR.
ST Employee
Posted on March 30, 2018 at 12:00

Hello Simon

You can take a look into your local Cube repository for STOP with RTC autowakeup example:

\STM32Cube_FW_L0_V1.10.0\Projects\STM32L073RZ-Nucleo\Examples\PWR\PWR_STOP_RTC

Regards,

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Posted on March 30, 2018 at 13:51

lptimHandle.Init.Clock.Source = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;

lptimHandle.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV128;

So, as it appears that you did not change the default value of RCC_CCIPR.LPTIM1SEL,  what's your APB clock frequency? Divide that by 128 and the autoreload value, which is

it's too fast (when I call '

Start_Counter' I put period = 0xFFFFF)

oh, the LPTIM is 16-bit only, so you can't set it to 0xFFFFF.

JW

henry.dick
Senior II
Posted on March 30, 2018 at 17:07

take a look at its prescaler and timer width and see if you can achieve the said duration with just the prescaler + timer period.

if not, consider a different timer that can;

if this timer is your only option, set up the mcu to wake up at certain interval, like roll-over. In the isr, increment a counter and test if certain number of roll-overs has been achieved. Essentially that counter forms your most significant bits of the timer.

helmi haddad
Associate II
Posted on June 27, 2018 at 12:50

any idea how i can configure the LPTim for period hour please , and any solution for period in minutes please