Page 1 of 2
How use LPTIM
Posted: Sun Oct 16, 2022 2:30 pm
by hamady91
Hello,
When i try to use LPTIM1 of a Nucleo 32 l031k6 i got errors about the fact that TIM_TypeDef can't take the word "LPTIM1"
Is there a way to use LPTIM with stm32duino or i need to tuse HAL ?
Thanks
Re: How use LPTIM
Posted: Mon Oct 17, 2022 8:01 am
by fpiSTM
Hi @hamady91
Currently, no dedicated API for LPTIM. You can use HAL/LL instrad.
Re: How use LPTIM
Posted: Mon Oct 17, 2022 4:18 pm
by hamady91
Thanks you for the anwser
Do you have a small example of a HAL implementation for LPTIM that could work with stm32duino
Beaucause even with copying the code of the HAL generated in STM32 IDE i got these errors:
Code: Select all
LPTIM_HandleTypeDef hlptim1;
static void MX_LPTIM1_Init(void)
{
/* USER CODE BEGIN LPTIM1_Init 0 */
/* USER CODE END LPTIM1_Init 0 */
/* USER CODE BEGIN LPTIM1_Init 1 */
/* USER CODE END LPTIM1_Init 1 */
hlptim1.Instance = LPTIM1;
hlptim1.Init.Clock.Source = 0x00;
hlptim1.Init.Clock.Prescaler = 0x00;
hlptim1.Init.Trigger.Source = 0x00;
hlptim1.Init.OutputPolarity = 0x00;
hlptim1.Init.UpdateMode = 0x00;
hlptim1.Init.CounterSource = 0x00;
if (HAL_LPTIM_Init(&hlptim1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN LPTIM1_Init 2 */
/* USER CODE END LPTIM1_Init 2 */
}
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
Code: Select all
C:\Users\Hamady\AppData\Local\Temp\.arduinoIDE-unsaved2022917-9196-1u6cgbu.al1v\sketch_oct17b\sketch_oct17b.ino:2:1: error: 'LPTIM_HandleTypeDef' does not name a type; did you mean 'TIM_HandleTypeDef'?
2 | LPTIM_HandleTypeDef hlptim1;
| ^~~~~~~~~~~~~~~~~~~
| TIM_HandleTypeDef
C:\Users\Hamady\AppData\Local\Temp\.arduinoIDE-unsaved2022917-9196-1u6cgbu.al1v\sketch_oct17b\sketch_oct17b.ino: In function 'void MX_LPTIM1_Init()':
C:\Users\Hamady\AppData\Local\Temp\.arduinoIDE-unsaved2022917-9196-1u6cgbu.al1v\sketch_oct17b\sketch_oct17b.ino:14:3: error: 'hlptim1' was not declared in this scope
14 | hlptim1.Instance = LPTIM1;
| ^~~~~~~
C:\Users\Hamady\AppData\Local\Temp\.arduinoIDE-unsaved2022917-9196-1u6cgbu.al1v\sketch_oct17b\sketch_oct17b.ino:21:7: error: 'HAL_LPTIM_Init' was not declared in this scope; did you mean 'HAL_LIN_Init'?
21 | if (HAL_LPTIM_Init(&hlptim1) != HAL_OK)
| ^~~~~~~~~~~~~~
| HAL_LIN_Init
exit status 1
Compilation error: 'LPTIM_HandleTypeDef' does not name a type; did you mean 'TIM_HandleTypeDef'?
And il LL all the functions inside the init function aren't know by the software
Thanks
Re: How use LPTIM
Posted: Mon Oct 17, 2022 7:53 pm
by dannyf
It might be easier for you to say what application you have in mind for the lptim and go through the datasheet to set it up for that.
It doesn't look at complicated.
Re: How use LPTIM
Posted: Mon Oct 17, 2022 8:18 pm
by hamady91
I need in stop mode to :
Wait x second
Toggle a pin
Wait x second
Toggle a pin
But i can't use RTC beacause it is already used for the LoRaWAN part (STM32WLE55).
It is not hard . If it was just in STM Cube IDE it would be easy but here the LPTIM don't seems to be simple to implement
Re: How use LPTIM
Posted: Mon Oct 17, 2022 8:36 pm
by dannyf
In situations like this, you should think about a few questions.
1. Are there other solutions?
2. Is the timer able to wake up the chip?
3. What's the clocking arrangement?
4. How big or small is the desired interval?
5. Given 3 and 4 above, can the timer do it in one shot or you need to wake up the chip multiple times?
...
Typically it is fairly simple. If the timer is able to generate an interrupt on a compare match, a free running timer would do.
If not, you generally load up the timer with an offset...
Re: How use LPTIM
Posted: Mon Oct 17, 2022 9:53 pm
by hamady91
dannyf wrote: Mon Oct 17, 2022 8:36 pm
In situations like this, you should think about a few questions.
1. Are there other solutions?
2. Is the timer able to wake up the chip?
3. What's the clocking arrangement?
4. How big or small is the desired interval?
5. Given 3 and 4 above, can the timer do it in one shot or you need to wake up the chip multiple times?
...
Typically it is fairly simple. If the timer is able to generate an interrupt on a compare match, a free running timer would do.
If not, you generally load up the timer with an offset...
To be honest i dont understand this anwser
The LPTIM is the only timer that can wakeup a chip from stop mode (also RTC)
The interval is below 1 min
And i didn't tested if the LPTIM can go up to 1 min
But the problems are not in the knolwedge of the timer but in the software implementation with STM32duino
Re: How use LPTIM
Posted: Tue Oct 18, 2022 12:53 am
by ozcar
Perhaps you need some #include in your code, like maybe for stm32l0xx_hal_lptim.h?
Re: How use LPTIM
Posted: Tue Oct 18, 2022 2:33 am
by ag123
try the normal Hardware Timers
https://github.com/stm32duino/wiki/wiki ... er-library
those works well and doesn't use much power
https://github.com/stm32duino/STM32Exam ... llback.ino
Code: Select all
void Update_IT_callback(void)
{ // Toggle pin. 10hz toogle --> 5Hz PWM
digitalWrite(pin, !digitalRead(pin));
}
void setup()
{
// configure pin in output mode for led
pinMode(pin, OUTPUT);
// init timer
#if defined(TIM1)
TIM_TypeDef *Instance = TIM1;
#else
TIM_TypeDef *Instance = TIM2;
#endif
// Instantiate HardwareTimer object. Thanks to 'new' instanciation, HardwareTimer is not destructed when setup() function is finished.
HardwareTimer *MyTim = new HardwareTimer(Instance);
MyTim->pause();
MyTim->setOverflow(10, HERTZ_FORMAT); // 10 Hz
MyTim->attachInterrupt(Update_IT_callback);
MyTim->refresh();
MyTim->resume();
}
Re: How use LPTIM
Posted: Tue Oct 18, 2022 9:37 am
by hamady91
ag123 wrote: Tue Oct 18, 2022 2:33 am
try the normal Hardware Timers
https://github.com/stm32duino/wiki/wiki ... er-library
those works well and doesn't use much power
https://github.com/stm32duino/STM32Exam ... llback.ino
Code: Select all
void Update_IT_callback(void)
{ // Toggle pin. 10hz toogle --> 5Hz PWM
digitalWrite(pin, !digitalRead(pin));
}
void setup()
{
// configure pin in output mode for led
pinMode(pin, OUTPUT);
// init timer
#if defined(TIM1)
TIM_TypeDef *Instance = TIM1;
#else
TIM_TypeDef *Instance = TIM2;
#endif
// Instantiate HardwareTimer object. Thanks to 'new' instanciation, HardwareTimer is not destructed when setup() function is finished.
HardwareTimer *MyTim = new HardwareTimer(Instance);
MyTim->pause();
MyTim->setOverflow(10, HERTZ_FORMAT); // 10 Hz
MyTim->attachInterrupt(Update_IT_callback);
MyTim->refresh();
MyTim->resume();
}
Thanks you but i need a timer that need to work in stop mode and it is not possibe with normal timer