$npx -y skills add mohitmishra786/low-level-dev-skills --skill low-power-embeddedLow-power embedded skill for sleep modes and energy optimization. Use when configuring MCU sleep/stop/standby, peripheral clock gating, wake-up sources, or measuring firmware current draw. Activates on queries about WFI, STOP mode, STM32 PWR, nRF sleep, wake-up EXTI, or reducing
| 1 | # Low-Power Embedded |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | 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 `WFI`/`WFE` and vendor PWR examples (STM32-focused patterns apply broadly). |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Battery-powered firmware missing power budget |
| 10 | - Wake-up latency vs consumption tradeoffs |
| 11 | - Debugging "device won't wake" or "current still mA in sleep" |
| 12 | - Before shipping RTOS idle hook or bare-metal main loop sleep |
| 13 | - Cross-linking with `skills/baremetal/interrupts-and-exceptions-baremetal` |
| 14 | |
| 15 | ## Workflow |
| 16 | |
| 17 | ### 1. Power mode hierarchy (STM32-style) |
| 18 | |
| 19 | | Mode | CPU | Peripherals | RAM | Wake source | Relative current | |
| 20 | |------|-----|-------------|-----|-------------|------------------| |
| 21 | | Run | on | on | on | — | highest | |
| 22 | | Sleep | off | on | on | any IRQ | medium | |
| 23 | | Stop | off | most off | on | EXTI, RTC, UART | low | |
| 24 | | Standby | off | off | lost* | WKUP pins, RTC | lowest | |
| 25 | |
| 26 | \* Standby clears most SRAM; use backup domain or external EEPROM for state. |
| 27 | |
| 28 | ### 2. Enter Sleep (WFI) |
| 29 | |
| 30 | ```c |
| 31 | /* Cortex-M — sleep until interrupt */ |
| 32 | __disable_irq(); |
| 33 | /* configure wake source (e.g. EXTI, RTC alarm) */ |
| 34 | __enable_irq(); |
| 35 | __WFI(); /* or __WFE() for event-based wake */ |
| 36 | ``` |
| 37 | |
| 38 | Ensure pending interrupts are cleared before `WFI` or wake may be immediate. |
| 39 | |
| 40 | ### 3. STM32 Stop mode pattern |
| 41 | |
| 42 | ```c |
| 43 | #include "stm32f4xx.h" |
| 44 | |
| 45 | void enter_stop_mode(void) |
| 46 | { |
| 47 | /* Gate clocks you do not need */ |
| 48 | RCC->AHB1ENR &= ~RCC_AHB1ENR_GPIOAEN; /* example — only if safe */ |
| 49 | |
| 50 | PWR->CR |= PWR_CR_CWUF; /* clear wake flags */ |
| 51 | PWR->CR |= PWR_CR_PDDS; /* deep sleep = Stop */ |
| 52 | SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; |
| 53 | |
| 54 | __WFI(); |
| 55 | |
| 56 | /* After wake: re-enable HSE/PLL — clocks lost in Stop on many parts */ |
| 57 | SystemInit(); |
| 58 | } |
| 59 | ``` |
| 60 | |
| 61 | After Stop, re-init clocks and peripherals that lost their registers. |
| 62 | |
| 63 | ### 4. Clock gating checklist |
| 64 | |
| 65 | ``` |
| 66 | Before sleep |
| 67 | ├── Disable unused peripheral clocks (RCC xENR) |
| 68 | ├── Disable ADC/DAC continuous modes |
| 69 | ├── Stop DMA channels |
| 70 | ├── Enter peripheral low-power (UART mute, SPI off) |
| 71 | └── Configure lowest viable regulator scale (Voltage Scale 2/3) |
| 72 | ``` |
| 73 | |
| 74 | ### 5. Wake-up sources |
| 75 | |
| 76 | | Source | Config | |
| 77 | |--------|--------| |
| 78 | | EXTI GPIO | SYSCFG + EXTI IMR, edge trigger | |
| 79 | | RTC alarm | RTC WUT/WAKEUP | |
| 80 | | UART | Start bit detection (some MCUs) | |
| 81 | | IWDG | Not a wake source — resets device | |
| 82 | |
| 83 | ### 6. Measurement tips |
| 84 | |
| 85 | - Use ammeter in series with VDDA/VDD; short sampling window for uA |
| 86 | - DWT cycle counter for wake latency benchmarking |
| 87 | - Compare run vs sleep current **with same clock tree** documented |
| 88 | |
| 89 | ### 7. RTOS note |
| 90 | |
| 91 | FreeRTOS `configUSE_TICKLESS_IDLE` maps to WFI/Stop — coordinate with `skills/embedded/freertos`. |
| 92 | |
| 93 | ### 8. Agent usage |
| 94 | |
| 95 | ``` |
| 96 | /low-power-embedded Configure STM32 Stop mode with EXTI0 wake and PLL restore |
| 97 | ``` |
| 98 | |
| 99 | ## Common Problems |
| 100 | |
| 101 | | Symptom | Cause | Fix | |
| 102 | |---------|-------|-----| |
| 103 | | Still mA in "sleep" | Debug interface active (SWD) | Disconnect debugger; disable DBGMCU | |
| 104 | | Immediate wake from WFI | Pending NVIC IRQ | Clear flags before WFI | |
| 105 | | Lost state after wake | Entered Standby not Stop | Use Stop if RAM must persist | |
| 106 | | UART dead after wake | Clock tree not restored | Call `SystemInit()` path | |
| 107 | | Higher than datasheet uA | Floating GPIO | Set unused pins analog or pull | |
| 108 | |
| 109 | ## Related Skills |
| 110 | |
| 111 | - `skills/baremetal/interrupts-and-exceptions-baremetal` — EXTI wake IRQs |
| 112 | - `skills/baremetal/gpio-baremetal` — pin mode for leakage control |
| 113 | - `skills/baremetal/stm32-baremetal` — RCC and PWR registers |
| 114 | - `skills/baremetal/timers-pwm-baremetal` — stop timers before sleep |
| 115 | - `skills/embedded/freertos` — tickless idle integration |