stm32duino/STM32LowPower

millis() does not return the correct time

nopnop2002 opened this issue · 1 comments

My Envitonment

STM32F103(8MHz Xtal) + platformIO

Platform Manager
================
Platform ststm32
--------
Checking platformio/ststm32                   15.2.0                             [Up-to-date]
Checking platformio/toolchain-gccarmnoneeabi  1.70201.0 @ >=1.60301.0,<1.80000.0 [Up-to-date]
Checking platformio/framework-arduinoststm32  4.20100.211028 @ ~4.20100.0        [Up-to-date]
Checking platformio/framework-arduinoststm32-maple 3.10000.201129 @ ~3.10000.0        [Up-to-date]
Checking platformio/tool-stm32duino           1.0.1 @ ~1.0.1                     [Up-to-date]
Checking platformio/tool-openocd              2.1100.211028 @ ~2.1100.0          [Up-to-date]
Checking platformio/tool-dfuutil              1.9.200310 @ ~1.9.190708           [Up-to-date]

My config

[platformio]
default_envs = blackpill_f103c8

[env:blackpill_f103c8]
platform = ststm32
board = blackpill_f103c8
framework = arduino
lib_deps =
  https://github.com/stm32duino/STM32LowPower
  https://github.com/stm32duino/STM32RTC

My code

#include <Arduino.h>
#include "STM32LowPower.h"

void setup() {
  LowPower.begin();
  Serial.begin(115200);
  Serial.println("serial (same as 1)");
  Serial.println("*****");
}

void loop() {
  unsigned long nowMillis = millis();
  Serial.println(nowMillis);
  //Serial.println("serial printing 2");
  //for(int i=0;i<20;i++) LowPower.deepSleep(1000);
  //for(int i=0;i<10;i++) LowPower.sleep(1000);
  for(int i=0;i<10;i++) LowPower.idle(1000);
  //delay(1000);
}

Problem

millis() does not return the correct time.
idle can stop the clock.

Screen Shot

LowPoert+Millis

In fact, there is no issue here as when you enter in one of the sleep mode the interrupt are disabled.

__disable_irq();

For IDLE mode the systick is simply disabled:

/*
* Suspend Tick increment to prevent wakeup by Systick interrupt.
* Otherwise the Systick interrupt will wake up the device within
* 1ms (HAL time base)
*/
HAL_SuspendTick();

As millis relies on the systick it is normal it is not incremented during the sleep.
You can use the RTC to compensate it if you need.