stm32duino / STM32LowPower

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

ExternalWakeup example and UART #25

Closed mamanbudiman closed 4 years ago

mamanbudiman commented 4 years ago

On the ExternalWakeup example, If I add Serial.println(repetitions) instruction. Then the LowPower.sleep(); will not worked.

#include "STM32LowPower.h"

#define LED_BUILTIN  PB12

// Blink sequence number
// Declare it volatile since it's incremented inside an interrupt
volatile int repetitions = 1;

// Pin used to trigger a wakeup
const int pin = PB5;

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  // Set pin as INPUT_PULLUP to avoid spurious wakeup
  pinMode(pin, INPUT_PULLUP);

  // Configure low power
  LowPower.begin();
  // Attach a wakeup interrupt on pin, calling repetitionsIncrease when the device is woken up
  LowPower.attachInterruptWakeup(pin, repetitionsIncrease, RISING);
}

void loop() {
  /*
  for (int i = 0; i < repetitions; i++) {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    digitalWrite(LED_BUILTIN, LOW);
    delay(500);
  }*/
  Serial.println(repetitions);
  // Triggers an infinite sleep (the device will be woken up only by the registered wakeup sources)
  // The power consumption of the chip will drop consistently
  LowPower.sleep();
}

void repetitionsIncrease() {
  // This function will be called once on device wakeup
  // You can do some little operations here (like changing variables which will be used in the loop)
  // Remember to avoid calling delay() and long running functions since this functions executes in interrupt context
  repetitions ++;
}
fpistm commented 4 years ago

Hi,

this is normal as the Serial use IT mode. When it starts enter in sleep mode the interrupt fire and then wakeup directly as the sleep mode is configured for WFI.

So, you have to ensure to have send all using flush():

  Serial.println(repetitions);
  Serial.flush();
  LowPower.sleep();