low-power-embedded
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseLow-Power Embedded
低功耗嵌入式开发
Purpose
用途
Guide agents through MCU low-power modes: sleep vs deep sleep (stop/standby), peripheral and bus clock gating, wake-up source configuration, and practical current measurement — with Cortex-M / and vendor PWR examples (STM32-focused patterns apply broadly).
WFIWFE指导Agent掌握MCU低功耗模式:睡眠模式 vs 深度睡眠(停止/待机)、外设与总线时钟门控、唤醒源配置,以及实用的电流测量方法——包含Cortex-M /示例和厂商PWR模块案例(以STM32为核心的模式具有广泛适用性)。
WFIWFEWhen to Use
使用场景
- Battery-powered firmware missing power budget
- Wake-up latency vs consumption tradeoffs
- Debugging "device won't wake" or "current still mA in sleep"
- Before shipping RTOS idle hook or bare-metal main loop sleep
- Cross-linking with
skills/baremetal/interrupts-and-exceptions-baremetal
- 电池供电固件未达功耗预算
- 唤醒延迟与功耗的权衡取舍
- 调试“设备无法唤醒”或“睡眠状态下电流仍为毫安级”问题
- 部署RTOS空闲钩子或裸机主循环睡眠前
- 与关联使用
skills/baremetal/interrupts-and-exceptions-baremetal
Workflow
工作流程
1. Power mode hierarchy (STM32-style)
1. 功耗模式层级(STM32风格)
| Mode | CPU | Peripherals | RAM | Wake source | Relative current |
|---|---|---|---|---|---|
| Run | on | on | on | — | highest |
| Sleep | off | on | on | any IRQ | medium |
| Stop | off | most off | on | EXTI, RTC, UART | low |
| Standby | off | off | lost* | WKUP pins, RTC | lowest |
* Standby clears most SRAM; use backup domain or external EEPROM for state.
| 模式 | CPU状态 | 外设状态 | RAM状态 | 唤醒源 | 相对功耗 |
|---|---|---|---|---|---|
| 运行模式 | 开启 | 开启 | 开启 | — | 最高 |
| 睡眠模式 | 关闭 | 开启 | 开启 | 任意IRQ | 中等 |
| 停止模式 | 关闭 | 大多关闭 | 开启 | EXTI、RTC、UART | 低 |
| 待机模式 | 关闭 | 关闭 | 丢失* | WKUP引脚、RTC | 最低 |
*待机模式会清除大部分SRAM;如需保留状态,请使用备份域或外部EEPROM。
2. Enter Sleep (WFI)
2. 进入睡眠模式(WFI)
c
/* Cortex-M — sleep until interrupt */
__disable_irq();
/* configure wake source (e.g. EXTI, RTC alarm) */
__enable_irq();
__WFI(); /* or __WFE() for event-based wake */Ensure pending interrupts are cleared before or wake may be immediate.
WFIc
/* Cortex-M — 等待中断触发唤醒 */
__disable_irq();
/* 配置唤醒源(如EXTI、RTC闹钟) */
__enable_irq();
__WFI(); /* 或使用__WFE()基于事件唤醒 */确保在调用前清除挂起的中断,否则可能立即唤醒。
WFI3. STM32 Stop mode pattern
3. STM32停止模式示例
c
#include "stm32f4xx.h"
void enter_stop_mode(void)
{
/* Gate clocks you do not need */
RCC->AHB1ENR &= ~RCC_AHB1ENR_GPIOAEN; /* example — only if safe */
PWR->CR |= PWR_CR_CWUF; /* clear wake flags */
PWR->CR |= PWR_CR_PDDS; /* deep sleep = Stop */
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
__WFI();
/* After wake: re-enable HSE/PLL — clocks lost in Stop on many parts */
SystemInit();
}After Stop, re-init clocks and peripherals that lost their registers.
c
#include "stm32f4xx.h"
void enter_stop_mode(void)
{
/* 关闭不需要的时钟 */
RCC->AHB1ENR &= ~RCC_AHB1ENR_GPIOAEN; /* 示例——仅在安全情况下执行 */
PWR->CR |= PWR_CR_CWUF; /* 清除唤醒标志 */
PWR->CR |= PWR_CR_PDDS; /* 深度睡眠 = 停止模式 */
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
__WFI();
/* 唤醒后:重新启用HSE/PLL——多数芯片在停止模式下会丢失时钟 */
SystemInit();
}停止模式唤醒后,需重新初始化丢失寄存器配置的时钟和外设。
4. Clock gating checklist
4. 时钟门控检查清单
Before sleep
├── Disable unused peripheral clocks (RCC xENR)
├── Disable ADC/DAC continuous modes
├── Stop DMA channels
├── Enter peripheral low-power (UART mute, SPI off)
└── Configure lowest viable regulator scale (Voltage Scale 2/3)睡眠前准备
├── 关闭未使用的外设时钟(RCC xENR寄存器)
├── 禁用ADC/DAC连续模式
├── 停止DMA通道
├── 进入外设低功耗状态(UART静音、SPI关闭)
└── 配置最低可行的调节器档位(电压档位2/3)5. Wake-up sources
5. 唤醒源
| Source | Config |
|---|---|
| EXTI GPIO | SYSCFG + EXTI IMR, edge trigger |
| RTC alarm | RTC WUT/WAKEUP |
| UART | Start bit detection (some MCUs) |
| IWDG | Not a wake source — resets device |
| 唤醒源 | 配置方式 |
|---|---|
| EXTI GPIO | SYSCFG + EXTI IMR、边沿触发 |
| RTC闹钟 | RTC WUT/WAKEUP |
| UART | 起始位检测(部分MCU支持) |
| IWDG | 非唤醒源——会重置设备 |
6. Measurement tips
6. 测量技巧
- Use ammeter in series with VDDA/VDD; short sampling window for uA
- DWT cycle counter for wake latency benchmarking
- Compare run vs sleep current with same clock tree documented
- 将电流表串联在VDDA/VDD之间;针对微安级电流使用短采样窗口
- 使用DWT周期计数器进行唤醒延迟基准测试
- 在相同时钟树配置下记录并对比运行模式与睡眠模式的电流
7. RTOS note
7. RTOS注意事项
FreeRTOS maps to WFI/Stop — coordinate with .
configUSE_TICKLESS_IDLEskills/embedded/freertosFreeRTOS的会映射到WFI/停止模式——需与配合使用。
configUSE_TICKLESS_IDLEskills/embedded/freertos8. Agent usage
8. Agent使用示例
/low-power-embedded Configure STM32 Stop mode with EXTI0 wake and PLL restore/low-power-embedded 配置带EXTI0唤醒和PLL恢复的STM32停止模式Common Problems
常见问题
| Symptom | Cause | Fix |
|---|---|---|
| Still mA in "sleep" | Debug interface active (SWD) | Disconnect debugger; disable DBGMCU |
| Immediate wake from WFI | Pending NVIC IRQ | Clear flags before WFI |
| Lost state after wake | Entered Standby not Stop | Use Stop if RAM must persist |
| UART dead after wake | Clock tree not restored | Call |
| Higher than datasheet uA | Floating GPIO | Set unused pins analog or pull |
| 症状 | 原因 | 解决方案 |
|---|---|---|
| “睡眠”状态下电流仍为毫安级 | 调试接口处于激活状态(SWD) | 断开调试器;禁用DBGMCU |
| 调用WFI后立即唤醒 | NVIC存在挂起的IRQ | 调用WFI前清除标志位 |
| 唤醒后丢失状态 | 进入了待机模式而非停止模式 | 若需保留RAM数据,请使用停止模式 |
| 唤醒后UART无法工作 | 时钟树未恢复 | 调用 |
| 电流高于 datasheet 标注的微安级 | GPIO引脚悬空 | 将未使用引脚设置为模拟输入或上下拉模式 |
Related Skills
相关技能
- — EXTI wake IRQs
skills/baremetal/interrupts-and-exceptions-baremetal - — pin mode for leakage control
skills/baremetal/gpio-baremetal - — RCC and PWR registers
skills/baremetal/stm32-baremetal - — stop timers before sleep
skills/baremetal/timers-pwm-baremetal - — tickless idle integration
skills/embedded/freertos
- — EXTI唤醒中断
skills/baremetal/interrupts-and-exceptions-baremetal - — 引脚模式控制以降低泄漏
skills/baremetal/gpio-baremetal - — RCC与PWR寄存器操作
skills/baremetal/stm32-baremetal - — 睡眠前停止定时器
skills/baremetal/timers-pwm-baremetal - — 无滴答空闲模式集成
skills/embedded/freertos