stm32duino / STM32LowPower

Arduino Low Power library for STM32
185 stars 52 forks source link

TimedWakeup not working #79

Closed tshcherban closed 2 years ago

tshcherban commented 2 years ago

I've tried a very basic example of timed wakeup:

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  LowPower.begin();
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  LowPower.sleep(1000);
  digitalWrite(LED_BUILTIN, LOW);
  LowPower.sleep(1000);
}

And its working wrong. Works as expected on first board power-on, but on subsequent resets via rst button - LED stays on forewer. Also tried deepSleep, shutdown, idle - behavior is the same. And if i put before LowPower.begin(); call the following snippet:

STM32RTC &rtc = STM32RTC::getInstance();
rtc.begin(true); // note a parameter here - tells RTC library to do a time reset

all works as expected both on first power-on and subsequent resets. (of course shutdown reboots board so LED blinks shortly instead of cycling 1 sec, that im aware of). Not sure, maybe there is some bug in RTC library? or LowPower should care about some additional stuff? Verified on: STM32L051C8T6 custom board, basically bare chip with minimum required R/C STM32L053R8 nucleo-64 STM32F446RE nucleo-64

tshcherban commented 2 years ago

Update: TimedWakeup example (idle, sleep and deepSleep functions) works good with Stm32RTC v.1.2.0 (internally marked as 1.1.1 for mistake) from releases page. Will try finding exact commit which brakes functionality. Update2: commit cfd98df1d316cff8cb46158e9654acde87b3dc9c from 31.05.2022 PR) breaks idle, sleep and deepSleep functions. Update3: shutdown works only with workaround from first comment.

ABOSTM commented 2 years ago

Hi @tshcherban, I tested example TimedWakeup (deepSleep) on my Nucleo STM32F446RE, and it is working fine, even after reset button is pressed.

Please note that I used latest commit from both libraries:

Can you please test the same ?

tshcherban commented 2 years ago

Okay, tested (using latest changes). That example is ok:

digitalWrite(LED_BUILTIN, HIGH);
LowPower.deepSleep(1000);
digitalWrite(LED_BUILTIN, LOW);
LowPower.deepSleep(1000);

but that - not:

digitalWrite(LED_BUILTIN, HIGH);
LowPower.deepSleep(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
LowPower.shutdown(1000);

LED should be 1s ON 2s OFF, but instead it is 1s ON 1s OFF, so LowPower.shutdown doesnt respect passed time value and reboots CPU immediately.

ABOSTM commented 2 years ago

Hi @tshcherban , You are right, when LowPower.shutdown() is invoked after LowPower.deepSleep(), MCU wakes up immediately. This was due to WakeUp flag that remained set.

I pushed a fix to clear this flag, and it is now working fine on my side: https://github.com/stm32duino/STM32LowPower/pull/81 Feel free to test it on your side.

tshcherban commented 2 years ago

Yep, works great. Verified on both STM32F446RE and STM32L053R8. Maybe its time to release a new version of the lib? There's a few bugfixes after 1.2.0