Sleep modes for STM32F1

Post here first, or if you can't find a relevant section!
Post Reply
dolfandringa
Posts: 20
Joined: Wed Jun 03, 2020 3:47 am

Sleep modes for STM32F1

Post by dolfandringa »

I am running a SM32F103C8 as a sensor node. It is sending data through LoraWAN, and runs off batteries. I want to put it to sleep between send intervals. I've seen the different sleep modes been called differently in different libraries (idle, runsleep, sleep, deepsleep, stop, standby) and confusingly, some even change the name, and call stop deepsleep and such. So my question is:

What mode allows RAM to still be kept, so the code continues where it left-off without going back to `setup`, while turning as much off aside from that. I'd love just the RTC to wakeup the device, and then continue where it left off. What would be the "official" Microchip STM32 name be of that mode? If I understand it correctly, there is the sleep mode, and then with SLEEPDEEP set, there are a two modes: stop and standby.
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Sleep modes for STM32F1

Post by ag123 »

my favorite 'sleep' is

Code: Select all

void sleep(int n) {
  while(n-- > 0)
    asm("wfi");
}
...
e.g.
void loop() {
	digitalWrite(LED_BUILTIN,1);
	sleep(100);
	digitalWrite(LED_BUILTIN,0);
	sleep(100);
}
unlike delay() it simply counts the number of systick (or some other) interrupts so it is imprecise. but this saves power, i tend to see core temperatures running lower.
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: Sleep modes for STM32F1

Post by GonzoG »

Official ST low power library:
https://github.com/stm32duino/STM32LowPower
All low power modes are explained there also.
The most power efficient mode without loosing RAM is deepsleep.
dolfandringa
Posts: 20
Joined: Wed Jun 03, 2020 3:47 am

Re: Sleep modes for STM32F1

Post by dolfandringa »

The confusing bit is that ST uses different names in their docs. The RM0008 Reference Manual https://www.st.com/resource/en/referenc ... ronics.pdf talks about sleep, stop and standby modes, with the option to keep the regulator off and on, and wake from event or wake from interrupt. There is no deepsleep mode, but a deepsleep bit that is used both for stop and standby? So how do they compare? That is where the confusion comes from, and then there are other libraries, howto's and other docs out there that use different defintions still. So the deepsleep from the LowPower library, what mode is that?
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Sleep modes for STM32F1

Post by ag123 »

my thoughts are that using any of the hardware 'sleep' and 'deepsleep' modes one would need to work the design in terms of keeping state between the start of sleep and resume from sleep. that for sure would be more complicated than the simple minded

Code: Select all

asm("wfi")
i tend to find using asm("wfi") runs a few deg C lower than if not, hence some energy is saved, but certainly not the same as sleep and deep sleep modes. sleep modes that keep memory intact is probably easier to manage. but really to save power, un-clock the peripherals that you don't use.
that in itself may save a lot of on chip power. i often find that pheriperials e.g. an lcd hooked up can consume more power than do the stm32 chip alone. so those things may be worth noting in terms of saving power, e.g. some 'external' peripherals can be 'turned off' if not needed to save power.
the trouble with all these mainly is the need to keep state. so one of the more common approach is when resuming from deep sleep, do the same as a reset. and maybe keep some minor bits of state information in the rtc backup registors which can run off a separate coin cell (vbat)
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: Sleep modes for STM32F1

Post by GonzoG »

The name of low power modes are different because it's a library for Arduino IDE. There is already Arduino low power library for Arduino 32b boards so this one uses same names as in Arduino library for compatibility.
You either you this low power library or you do it by yourself using asm or registers.

How does library low power modes translate to MCU modes - well, you would have to go through library code to know it exactly.
For me the basic description from library github is enough:
Idle mode: low wake-up latency (µs range) (e.g. ARM WFI). Memories and voltage supplies are retained. Minimal power saving mainly on the core itself.

sleep mode: low wake-up latency (µs range) (e.g. ARM WFI), Memories and voltage supplies are retained. Minimal power saving mainly on the core itself but higher than idle mode.

deep sleep mode: medium latency (ms range), clocks are gated to reduced. Memories and voltage supplies are retained. If supported, Peripherals wake-up is possible (UART, I2C ...).

shutdown mode: high wake-up latency (posible hundereds of ms or second timeframe), voltage supplies are cut except always-on domain, memory content are lost and system basically reboots.
I use deep sleep when I need to keep RAM and shutdown if I need only RTC and/or few data saved in registers.
Post Reply

Return to “General discussion”