How use LPTIM

Post here first, or if you can't find a relevant section!
hamady91
Posts: 7
Joined: Sun Oct 16, 2022 2:23 pm

Re: How use LPTIM

Post by hamady91 »

ozcar wrote: Tue Oct 18, 2022 12:53 am Perhaps you need some #include in your code, like maybe for stm32l0xx_hal_lptim.h?
Even with including this file the definitions aren't reconized....
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: How use LPTIM

Post by fpiSTM »

In fact HAL_LPTIM_MODULE_ENABLED is not enabled by default. you can add an hal_conf_extra.h file with:

Code: Select all

#define HAL_LPTIM_MODULE_ENABLED
to enable it.

In this archive, you will get a port of a STM32Cube project for L4:
https://github.com/STMicroelectronics/S ... lseCounter
LPTIM_PusleCounter.zip
(2.71 KiB) Downloaded 102 times
I've tested and it works as expected.
hamady91
Posts: 7
Joined: Sun Oct 16, 2022 2:23 pm

Re: How use LPTIM

Post by hamady91 »

fpiSTM wrote: Tue Oct 18, 2022 5:16 pm In fact HAL_LPTIM_MODULE_ENABLED is not enabled by default. you can add an hal_conf_extra.h file with:

Code: Select all

#define HAL_LPTIM_MODULE_ENABLED
to enable it.

In this archive, you will get a port of a STM32Cube project for L4:
https://github.com/STMicroelectronics/S ... lseCounter

LPTIM_PusleCounter.zip

I've tested and it works as expected.
A big thanks for the answer

I tested it (Nucleo L432KC) but there is a small issue.

To see the led toogle i need to add the MspInit

Code: Select all

 
  if (HAL_LPTIM_Init(&LptimHandle) != HAL_OK)
  {
    Error_Handler();
  }
   HAL_LPTIM_MspInit(&LptimHandle);
  
but i see no change in the frequency of the LED blinking when i whange the div of the prescaler

Do you have an idea ?
dannyf
Posts: 446
Joined: Sat Jul 04, 2020 7:46 pm

Re: How use LPTIM

Post by dannyf »

try the normal Hardware Timers
in the run mode, lots of ways to do this.

in the stop mode, you will need something that can wake up the chip. in this particular case, the timer is 16 bit wide (23 bit with the prescaler). so for multiple minutes, you will need to be prepared to wake up multiple times and keep track of it.

luckily this timer has a compare function so you can advance it like this:

Code: Select all

  calculate the desired number of cycles to achive your duration
  calculate the number of wake-ups required (essentially desired cycle mod timer width)
  compare register = current timer value + desired cycle
  put the chip to sleep
  
//in the interrupt
  if (wake-up counter--) {blink the led; reset wake-up counter / compare register}
  else go back to sleep  
dannyf
Posts: 446
Joined: Sat Jul 04, 2020 7:46 pm

Re: How use LPTIM

Post by dannyf »

if the timer didn't have the compare register, the work flow is slightly differernt: you will need to load up the timer with an offset (= -desired cycles, for an up counter).

this approach will have long-term errors however.
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: How use LPTIM

Post by fpiSTM »

hamady91 wrote: Tue Oct 18, 2022 7:23 pm
fpiSTM wrote: Tue Oct 18, 2022 5:16 pm In fact HAL_LPTIM_MODULE_ENABLED is not enabled by default. you can add an hal_conf_extra.h file with:

Code: Select all

#define HAL_LPTIM_MODULE_ENABLED
to enable it.

In this archive, you will get a port of a STM32Cube project for L4:
https://github.com/STMicroelectronics/S ... lseCounter

LPTIM_PusleCounter.zip

I've tested and it works as expected.
A big thanks for the answer

I tested it (Nucleo L432KC) but there is a small issue.

To see the led toogle i need to add the MspInit

Code: Select all

 
  if (HAL_LPTIM_Init(&LptimHandle) != HAL_OK)
  {
    Error_Handler();
  }
   HAL_LPTIM_MspInit(&LptimHandle);
  
but i see no change in the frequency of the LED blinking when i whange the div of the prescaler

Do you have an idea ?
HAL_LPTIM_MspInit is called by HAL_LPTIM_Init so I don't see why you need to call it again.
https://github.com/stm32duino/Arduino_C ... tim.c#L293

The project I've ported is for Nucleo L476RG. Maybe some init are different for L432. You can refers to the project of the L432 board:
https://github.com/STMicroelectronics/S ... lseCounter

One other thing is the L432 is a Nucleo32 and I know sometimes it is not easy to properly configure it from hardware view due to the number of SB used to be able to support different feature.
Post Reply

Return to “General discussion”