I used to have a project on arduino using the atmega328p chip with this code:
// used for deep sleep
#include <avr/wdt.h>
static void go_to_sleep_and_power_down_and_wake_up_again_in_2_seconds()
{
// wake up again in 4 seconds
wdt_enable(WDTO_4S);
// go to deep sleep
{
// BIG difference when in sleep
// Diable ADC (analog to digital converter)
ADCSRA &= ~(1 << 7);
SMCR |= (1 << 2); // power down mode
SMCR |= 1; // enable sleep;
// BOD DISABLE (big difference when in sleep only)
MCUCR |= (3 << 5); // set both BODS and BODSE at the same time
MCUCR = (MCUCR & ~(1 << 5)) | (1 << 6); // then set the BODS bit and clear the BOSE bit at the same time
__asm__ __volatile__("sleep");
}
// this line should not execute
}
void setup()
{
// init code...
// read sensor value
// pseudo code:
if(valueReadFromSensor == 0)
{
turnOnAlarm();
return;
}
go_to_sleep_and_power_down_and_wake_up_again_in_2_seconds()
}
void loop(){
// not used
}
The code is very simple, once it wakes up it reads data from a sensor and goes back to sleep. After 2 seconds it wakes up again and repeats the same process.
How can I do the same thing on stm32 on the bluepill for example? I have managed to go to standby mode using this function HAL_PWR_EnterSTANDBYMode();
. But how can I enable waking up again in 2 seconds for example?
Edit
I have tried this sources:
uses an old version of stm32 cube ide and different board. I tried the video doing the same steps and some defines make the code unable to compile. https://www.youtube.com/watch?v=zQ1sT9fjd3E
does not have code https://www.stm32duino.com/viewtopic.php?t=922
does not have code STM32 wake up from standby by RTC
I am not using freeRtos https://electronics.stackexchange.com/a/562120/56969