stm32duino / STM32LowPower

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

SerialDeepSleep not working with user defined Harware Serial ? #107

Closed Yannick-Marietti closed 6 months ago

Yannick-Marietti commented 6 months ago

Hello It seems example SerialDeepSleep.ino does not work with an hardware serial diffrent form the "Serial" default. For example, on my computer, the attached (picture) code does not run beyond row 33.

Capture d’écran 2024-02-17 093553

Best regards

Yannick

Desktop (please complete the following information):

Hardware (please complete the following information):

Additional context Add any other context about the problem here.

fpistm commented 6 months ago

The uart have to be low power compliant. Ex lpuart.

fpistm commented 6 months ago

Extract from L476 ref man Screenshot_20240217_132933_Drive

fpistm commented 6 months ago

Will chech deeply next week

fpistm commented 6 months ago

Hi @Yannick-Marietti I've tested and it works as expected using UART4 defined manually. image

Just for information about deepsleep and wakeup from serial feature. If the Serial is a LPUART then mcu will go in stop 2 mode, if not the stop mode 1 is used to be able to wake up. See datasheet: image With default Serial of the Nucleo L476RG, Serial is USART2 so STOP1 mode is used same if we use UART4.

Sketch used to test with Nucleo L476RG: ```C++ /* SerialDeepSleep This sketch demonstrates the usage of Serial Interrupts to wakeup a chip in deep sleep mode. This sketch is compatible only with board supporting uart peripheral in stop mode. This example code is in the public domain. */ #include "STM32LowPower.h" HardwareSerial mySerial(A1, A0); // Declare it volatile since it's incremented inside an interrupt volatile int wakeup_counter = 0; void setup() { Serial.begin(9600); mySerial.begin(9600); // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); // Configure low power LowPower.begin(); // Enable UART in Low Power mode wakeup source LowPower.enableWakeupFrom(&mySerial, SerialWakeup); Serial.println("Start deep sleep wakeup from mySerial UART4"); } void loop() { digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); // Triggers an infinite deep sleep // (the device will be woken up only by the registered wakeup sources) // The power consumption of the chip will drop consistently LowPower.deepSleep(); Serial.print(wakeup_counter); Serial.println(" wake up"); // Empty Serial Rx while (mySerial.available()) { char c = mySerial.read(); Serial.print(c); } Serial.println(); } void SerialWakeup() { // 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 wakeup_counter++; } ```

Result from Serial monitor: image

Result from mySerial monitor: image

Yannick-Marietti commented 6 months ago

The uart have to be low power compliant. Ex lpuart.

Thank you very much !

Clear, so we should avoid LPUARTs using LowPower.enableWakeupFrom(...) as wake-up won't work.

=> I just found my error coming back to my different codes ! I stupidly omitted the LoRa_Serial.begin() ... Copy-paste is a trap :)

Sorry for the time lost and thanks for the clarification about LPUART.