Low Power on STM32F0

_images/lone-warrior.jpg

One Lone Electronics Technician battles with STM32F0 Low Power Modes

Documentation

If you’re a Forth addict like me, you know that mastering unfamiliar hardware involves reading what documentation you can find, understanding it, then trying out your assumptions based on the new knowledge.

In this case, the ST documentation was incomplete, ambiguous, maybe outdated, and I certainly misunderstood some of it.

Note

Caveat, I prefer ST Documentation, it’s not perfect, but I think it’s nicely laid out and easy to read. ST had a massive job documenting Cortex M plus their peripherals, registers, bitfields etc.

Documentation Fragmentation

This was my main issue, because the key (SCR_SLEEPDEEP) to initiating ‘Standby Mode’ wasn’t obvious to me when I read the STM32F0 Technical and Programming pdfs.

For one thing, I rely heavily on CMSIS-SVD to create all the STM32F051 Memory Mapped Words and Bitfields for my code and while CMSIS-SVD has all the MCU Peripheral data I need, there is no mention of the System Control Register.

The System Control Register information was hiding in STM document PM0215 on page 81. https://www.st.com/resource/en/programming_manual/dm00051352.pdf

Researching OnLine Code Examples

Forth

To put it bluntly, there aren’t any STM32F0 low power code examples for Forth online that I can find.

Google: "forth stm32f0 low power"
No results found for "forth stm32f0 low power".

C Code Examples

These are everywhere, but usually not much use to me.

<rant>: Why I Don’t Use C Programming Language Examples

STM32F0 Low Power

One can’t just consider the Low Power Modes by themselves, because they fit in the middle of the whole process.

Something activates the Low Power process, and something wakes it up. Both the activate and wakeup process can be somewhat complex in themselves, involving the wide range of peripheral devices and interrupts.

Activating Low Power

I’ll try to keep it simple, and start the Low Power Modes using the WFI instruction on a STM32F0 Discovery Board.

WFI ( Wait For Interrupt)

Is a hint instruction that suspends execution until one of the following events occurs:

An exception
An interrupt becomes pending which would preempt if PRIMASK was clear
A Debug Entry request, regardless of whether Debug is enabled.

Note

WFI is intended for power saving only. When writing software assume that WFI might behave as a NOP operation.

WFI Forth Word

Presented below as WFI is not yet in the Mecrisp-Stellaris Dictionary.

: wfi ( -- mcu asleep ) [ $BF30 h, ] inline ;  \ WFI Opcode, Wait For Interrupt, enters

Low Power Register/Bitfields

There are three Bitfields (SLEEPDEEP, PDDS and LPDS) that control the low power modes.

Bitfield Name

Description

Register

Memory

Bit

SLEEPDEEP

System Control Register

SCR

0xE000ED10

2

PDDS

Power down deep sleep

PWR_CR

0x40007000

1

LPDS

Low power deep sleep

PWR_CR

0x40007000

2

Low Power Modes

X = Don't Care
All Low Power Modes are initiated by the WFI or WFE instruction

Current

Voltage

Clock MHz

Mode

SLEEPDEEP

PDDS

PDS

REGULATOR

CALL WFI?

16 mA

3.0

48

RUN

X

X

X

ON

NO

3.1mA

3.0

8

RUN

X

X

X

ON

NO

1.4 mA

3.0

8

SLEEP

0

X

X

ON

YES

0.9 mA

3.0

8

STOP

1

0

0

ON

YES

0.9 mA

3.0

8

STOP LOW POWER

1

1

0

LOW POWER

YES

4.8 uA

3.0

8

STANDBY

1

X

1

OFF

YES

Internal 1.8 Volt Regulator

The voltage regulator is always enabled after Reset. It works in five different modes.

Mode

Description

Run

The regulator supplies power to the core, memories and digital peripherals (everywhere).

Sleep

The CPU clock is turned off, all other clocks remain running.

Stop

All clocks are stopped, the regulator supplies power only to the registers and SRAM

Stop Low Power

All clocks are stopped except when required by any peripheral that can work in STOP mode. Registers and SRAM have power.

Standby

The regulator is powered off except for the Standby circuitry and the RTC. The contents of the registers and SRAM are lost.

Note

One ongoing mystery is which peripherals “can work in STOP mode”, and which ones cannot ? more testing is needed.

Wakeup the Device

Mode

Wakeup Methods

Sleep

Any interrupt can wake-up the device.

Stop

The User Pushbutton via A WKUP pin

Stop Low Power

The User Pushbutton via A WKUP pin, RTC

Standby

The User Pushbutton via A WKUP pin (rising edge), IWDG reset, external reset on NRST pin, RTC alarm.

My Example Forth Code

Download: f0-lowpower-1.fs

/home/tp/f0-lowpower-1/f0-lowpower-1.fs