STM32LowPower

repository·main·Indexed 42 days ago

https://github.com/stm32duino/stm32lowpower

An Arduino library for managing low-power modes (Idle, Sleep, Deep Sleep, and Shutdown) on STM32 microcontrollers. It provides mechanisms for wakeup via GPIO, RTC, and UART, and supports specific hardware states with varying power consumption and wake-up latency. Compatible with Arduino_Core_STM32 and STM32RTC.

Tokens
1.9K
Snippets
4
Records
8
Agent score
3%

What's inside STM32LowPower

  1. Understand STM32 low power hardware states

    main

    The library manages different hardware power states with varying trade-offs between power consumption and wake-up latency:

    • Idle mode: Lowest latency (µs range). Minimal power saving (mostly core). Memories and voltage supplies are retained.
    • Sleep mode: Low latency (µs range). Higher power saving than idle. Memories and voltage supplies are retained.
    • Deep sleep mode: Medium latency (ms range). Clocks are gated to reduce power. Memories and voltage supplies are retained. Supports peripheral wake-up (UART, I2C, etc.) if supported by the board.
    • Shutdown mode: High latency (ms to seconds). Voltage supplies are cut except for the always-on domain. Memory content is lost and the system reboots upon wake-up.
  2. Configure STM32LowPower dependencies

    main

    The required dependencies for STM32LowPower depend on the library version you are using:

    For Library version 1.x.x:

    • Arduino_Core_STM32 version >= 1.3.0
    • STM32RTC version 1.x.x

    For Library version 2.x.x:

    • Arduino_Core_STM32 version 3.x.x
    • STM32RTC version 2.x.x
  3. Configure wakeup sources

    main

    To wake the device from low power modes, you must configure a wakeup source using attachInterruptWakeup() or enableWakeupFrom() before calling any sleep/idle functions.

    GPIO Interrupt Wakeup

    void attachInterruptWakeup(uint32_t pin, voidFuncPtrVoid callback, uint32_t mode, LP_Mode LowPowerMode)

    • pin: Pin number.
    • callback: Pointer to the callback function.
    • mode: Interrupt mode (HIGH, LOW, RISING, FALLING, or CHANGE).
    • LowPowerMode: The mode to use (IDLE_MODE, SLEEP_MODE, DEEP_SLEEP_MODE, or SHUTDOWN_MODE).
      • Note: If SHUTDOWN_MODE is used, the Wakeup pin capability is automatically activated.

    UART Wakeup

    void enableWakeupFrom(Uart *serial, voidFuncPtrVoid callback)

    • serial: Pointer to a UART peripheral.
    • callback: Pointer to the callback function.
    • Note: Using UART as a wakeup source configures it to use the HSI clock source, even if another peripheral clock is configured.

    RTC Wakeup

    void enableWakeupFrom(STM32RTC *rtc, voidFuncPtr callback, void * data)

    • rtc: Pointer to the RTC (can be NULL as RTC is a Singleton).
    • callback: Pointer to the callback function.
    • data: Optional pointer to callback data parameters (defaults to NULL).
    • Note: RTC wakeup requires LSE or LSI as a clock source. If neither is used, it will configure the LSI. If SHUTDOWN_MODE is requested and PWR_CR1_LPMS is defined, LSE is strictly required; if the board lacks LSE, it will fail.

    I2C Wakeup

    void enableWakeupFrom(TwoWire *wire, voidFuncPtrVoid callback)

    • wire: Pointer to an I2C peripheral.
    • callback: Pointer to the callback function.
    • Note: Currently not available.
  4. Use STM32LowPower low power modes

    main

    The library provides four primary low power modes. You must call begin() at least once before using any of these modes.

    | Method | Description | |---|---|void begin() | Configures the Low Power settings. | | void idle(uint32_t ms) | Enters idle mode. Low wake-up latency (µs range). Memories and voltage supplies are retained. | | void sleep(uint32_t ms) | Enters sleep mode. Low wake-up latency (µs range). Memories and voltage supplies are retained. | | void deepSleep(uint32_t ms) | Enters deepSleep mode. Medium latency (ms range). Clocks are gated to reduce power. Memories and voltage supplies are retained. | | void shutdown(uint32_t ms) | Enters shutdown mode. High wake-up latency (ms to seconds). Voltage supplies are cut except for the always-on domain. Memory content is lost and the system reboots upon exit. |

    Notes:

    • The ms parameter is optional. If provided, the RTC is used in alarm mode to wake the chip after the specified milliseconds.
    • With STM32RTC version lower than 1.1.0, the minimum value for ms is 1000 ms.
    • Important for STM32WB0x and STM32WL3x: The board will restart when exiting shutdown or deepSleep modes. Shutdown wakeup is only possible via the reset pin, and UART wakeup is not supported.
    // Example usage pattern
    STM32LowPower.begin();
    // ... setup wakeup sources ...
    STM32LowPower.sleep(10000); // Sleep for 10 seconds
  5. Troubleshoot STM32LowPower compatibility and configuration errors

    main

    If you encounter compilation errors, check the following requirements:

    1. Core Version: The library requires an STM32 core version greater than 0x020C0000. If your core is older, you must update it.

      • Error: #error "This library is not compatible with core version used. Please update the core."
    2. PWR Module: The STM32 Power (PWR) HAL module must be enabled in your board's configuration.

      • Error: #error "PWR configuration is missing. Check flag HAL_PWR_MODULE_ENABLED in variants/board_name/stm32yzxx_hal_conf.h"
      • Fix: Ensure HAL_PWR_MODULE_ENABLED is defined in your variant's stm32yzxx_hal_conf.h file.
  6. Configure wakeup interrupts and peripherals

    main

    You can configure specific triggers to wake the MCU from low power modes.

    Pin Wakeup

    Use attachInterruptWakeup to trigger a wakeup via a GPIO pin.

    • pin: The pin to monitor.
    • callback: The function to execute on wakeup.
    • mode: The PinStatus (e.g., RISING, FALLING).
    • LowPowerMode: The specific LP_Mode this interrupt is intended to wake the device from (defaults to SHUTDOWN_MODE).

    Serial Wakeup

    Use enableWakeupFrom(Uart *serial, voidFuncPtr callback) to wake up when data is received on a serial port.

    RTC Wakeup

    Use enableWakeupFrom(STM32RTC *rtc, voidFuncPtrParam callback, void *data = NULL) to wake up using the Real-Time Clock. This requires an STM32RTC instance.

    // Wakeup from pin
    LowPower.attachInterruptWakeup(PA0, myCallback, RISING, DEEP_SLEEP_MODE);
    
    // Wakeup from RTC
    LowPower.enableWakeupFrom(&myRtc, myRtcCallback, &myData);
  7. Enter low power states with idle(), sleep(), deepSleep(), and shutdown()

    main

    Use these methods to transition the MCU into specific power-saving states. For most modes, you can specify a duration in milliseconds.

    MethodDescription
    idle(uint32_t ms = 0)Enters Idle mode.
    sleep(uint32_t ms = 0)Enters Sleep mode.
    deepSleep(uint32_t ms = 0)Enters Deep Sleep mode.
    shutdown(uint32_t ms = 0)Enters Shutdown mode. Note: On STM32WB0x or STM32WL3x variants, shutdown() takes no arguments.
    LowPower.idle(100);
    LowPower.sleep(1000);
    LowPower.deepSleep(5000);
    LowPower.shutdown(10000);
  8. Use the STM32LowPower class to manage low power modes

    main

    The STM32LowPower class provides an interface for putting STM32 microcontrollers into various low-power states. You can use the global LowPower instance to trigger these modes. Most methods accept an optional ms parameter (milliseconds) to define a duration for the low-power state.

    Available Low Power Modes (LP_Mode)

    • IDLE_MODE
    • SLEEP_MODE
    • DEEP_SLEEP_MODE
    • SHUTDOWN_MODE
    #include <STM32LowPower.h>
    
    void setup() {
      LowPower.begin();
    }
    
    void loop() {
      // Enter deep sleep for 5 seconds
      LowPower.deepSleep(5000);
    }