Skip to content

Conversation

@dirkju
Copy link
Contributor

@dirkju dirkju commented Nov 15, 2025

Implement Mysensor hwSleep in STM32 HAL using stop mode support.

Decision Matrix STM32 low power modes

Criterion SLEEP Mode STOP Mode STANDBY Mode Winner
Sleep Current 2-5 mA ❌ 10-50 µA ✅ 2-4 µA ✅ STOP/STANDBY
Wake-up Time <1 µs ✅ 1-3 ms ✅ 5-10 ms ⚠️ STOP
GPIO EXTI Wake ✅ Yes ✅ Yes ❌ No STOP
State Retention ✅ Full ✅ Full ❌ Lost STOP
Radio Compatible ✅ Yes ✅ Yes ❌ No STOP
Battery Life (5-min) Weeks ❌ 5+ years ✅ 10+ years ✅ STOP/STANDBY
Complexity Low ✅ Low ✅ High ❌ STOP
MySensors API Fit ✅ Good ✅ Perfect ⚠️ Poor STOP

Winner: STOP Mode (7 out of 8 criteria favorable)

Why STOP Mode is Optimal

  1. GPIO EXTI Wake-up Support (Critical Differentiator)

    • MySensors nodes need to wake on radio IRQ (nRF24 IRQ pin, RFM69/RFM95 DIO pins)
    • STOP mode supports EXTI on any GPIO pin
    • STANDBY only supports dedicated WKUP pins (typically PA0-PA5)
    • Radio IRQ pins are NOT WKUP pins, so STANDBY cannot be used
  2. Fast Wake-up Time

    • STOP mode: 1-3 ms (within 10 ms radio timing requirement)
    • STANDBY mode: 5-10 ms + reinitialization (exceeds 10 ms requirement)
    • Radio packet reception requires fast MCU response
  3. State Retention

    • STOP mode: SRAM and registers preserved (no reinitialization)
    • STANDBY mode: Everything lost (must reinitialize MySensors stack, radio, peripherals)
    • Simpler code, faster wake-up, less error-prone
  4. Battery Life is Sufficient

    • STOP mode: 10-50 µA sleep current = 5-10 year battery life (2x AA)
    • STANDBY mode: 2-4 µA = 10-20 year battery life (limited by battery self-discharge ~2%/year)
    • Diminishing returns: STOP already exceeds typical sensor node lifetime
  5. Complexity and Reliability

    • STOP mode: Simple, straightforward implementation
    • STANDBY mode: Complex (wake-up detection, state restoration, MySensors re-initialization)
    • STOP mode: Lower risk, easier to debug
  6. MySensors API Compatibility

    • STOP mode: Perfect fit (interrupt tracking, timer wake-up, state preservation)
    • STANDBY mode: Requires workarounds (no interrupt number return, state loss)

Component Overview

┌─────────────────────────────────────────────────────────────┐
│                  MySensors Application                      │
│                                                               │
│  MyMessage msg;                                              │
│  wait(sleep(5 * 60 * 1000));  // Sleep 5 minutes            │
└───────────────────┬───────────────────────────────────────────┘
                    │
                    ▼
┌─────────────────────────────────────────────────────────────┐
│            MySensors Core (platform-independent)            │
│                                                               │
│  int8_t sleep(uint32_t ms) { return hwSleep(ms); }          │
└───────────────────┬───────────────────────────────────────────┘
                    │
                    ▼
┌─────────────────────────────────────────────────────────────┐
│         STM32 HAL Implementation (MyHwSTM32.cpp)            │
│                                                               │
│  ┌──────────────────────────────────────────────────────┐  │
│  │ int8_t hwSleep(uint32_t ms)                          │  │
│  │ int8_t hwSleep(uint8_t int1, uint8_t mode1, ...)    │  │
│  │ int8_t hwSleep(uint8_t int1, ..., uint8_t int2, ...)│  │
│  └──────────────────┬───────────────────────────────────┘  │
│                     │                                        │
│  ┌──────────────────▼───────────────────────────────────┐  │
│  │        STM32 Sleep Management Module                 │  │
│  │                                                        │  │
│  │  • RTC initialization and configuration              │  │
│  │  • RTC wake-up timer setup                           │  │
│  │  • EXTI interrupt configuration                      │  │
│  │  • STOP mode entry                                   │  │
│  │  • Wake-up source detection                          │  │
│  │  • System clock reconfiguration                      │  │
│  └──────────────────┬───────────────────────────────────┘  │
└────────────────────┬┼──────────────────────────────────────┘
                     ││
        ┌────────────┘└────────────┐
        ▼                           ▼
┌──────────────────┐       ┌──────────────────┐
│   STM32 HAL API  │       │  STM32 Hardware  │
│                  │       │                  │
│  • HAL_RTC_*     │       │  • RTC registers │
│  • HAL_PWR_*     │       │  • PWR registers │
│  • HAL_GPIO_*    │       │  • EXTI lines    │
│  • HAL_NVIC_*    │       │  • NVIC          │
└──────────────────┘       └──────────────────┘

Data Flow

Sleep Entry Flow

Application calls sleep(5*60*1000)
  │
  ▼
MySensors core calls hwSleep(300000)
  │
  ▼
STM32 HAL hwSleep():
  1. Configure RTC wake-up timer (5 minutes)
  2. Optionally configure EXTI interrupt(s)
  3. Suspend SysTick timer
  4. Enter STOP mode (HAL_PWR_EnterSTOPMode)
  ├─► MCU enters low-power state (10-50 µA)
  │   └─► Wait for wake-up event...
  │
  └─► [Wake-up event occurs]
  │
  5. System clock defaults to HSI (16 MHz)
  6. Call SystemClock_Config() to restore HSE/PLL
  7. Resume SysTick timer
  8. Determine wake-up source (RTC vs EXTI)
  9. Detach interrupt handlers (if configured)
  10. Return wake-up source to MySensors
  │
  ▼
MySensors core resumes execution
  │
  ▼
Application continues (read sensor, transmit, etc.)

@dirkju
Copy link
Contributor Author

dirkju commented Nov 16, 2025

@tekka007 or @mfalkvidd - here's the 2nd PR to complete STM32 support. Can you PTAL?

@tekka007 tekka007 added the STM32 label Nov 16, 2025
@tekka007 tekka007 changed the title adding STM32 HAL sleep implementation Adding STM32 HAL sleep implementation Nov 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants