Appropriate low-power mode for timing applications

Post here all questions related to STM32 core if you can't find a relevant section!
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Appropriate low-power mode for timing applications

Post by mebab »

Hi everybody,
I tried to use STM32LowPower library to minimize the power consumption of my STM32L476-based application. Despite sleep mode, in the deep sleep mode, the timing-related commands (for example millis()) didn't work properly.
As I understand from the library, the following options exist:

Code: Select all

IDLE_MODE
SLEEP_MODE
DEEP_SLEEP_MODE
SHUTDOWN_MODE
With a clock frequency of 2 MHz, is there any other lower power mode than sleep mode where we are able to keep variables and timing normal and safe?
Is there any way to keep timing other than using millis() while using deep sleep mode?
by fpiSTM » Thu Sep 23, 2021 12:53 pm
millis() relies on the systick. in low power mode systick interrupt is disabled so millis is no more increased.
About the RTC time, ensure to use the LSE as input for theRTC to have a better precision.
Then you ask for ms, this implies the subsecond usage which is fairly dependant of the RTC synchronous prescaler value. depending of the this value the subsecond match is more or less precise.

For example, with 255 as prescaler value the best possible is Alarm activated every 1/128 s -- > 7,8 ms (for RTC2 type, the one of STM32L476)
Go to full post
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Appropriate low-power mode for timing applications

Post by fpiSTM »

When you use deep sleep interrupt are disabled. You have to use the RTC to be able to wait the desired time. Check the API you will see that you can pass delay as argument.
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Re: Appropriate low-power mode for timing applications

Post by mebab »

fpiSTM wrote: Tue Sep 21, 2021 7:32 am When you use deep sleep interrupt are disabled. You have to use the RTC to be able to wait the desired time. Check the API you will see that you can pass delay as argument.
Thanks fpiSTM. You are correct. Using the RTC should solve the problem of keeping the time record. I need to see if I can use the deep sleep mode while my application works based on the interrupt or not.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: Appropriate low-power mode for timing applications

Post by mrburnette »

mebab wrote: Tue Sep 21, 2021 8:10 am ...
Thanks fpiSTM. You are correct. Using the RTC should solve the problem of keeping the time record. I need to see if I can use the deep sleep mode while my application works based on the interrupt or not.
These things are discussed in both summary charts and details in the Reference Manual. Yes, the PDF is voluminous but easily searchable, but it should be opened in a window anytime you are doing coding of complex chip functionality.
STM32L476xx.jpg
STM32L476xx.jpg (37.83 KiB) Viewed 3504 times
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Re: Appropriate low-power mode for timing applications

Post by mebab »

Thanks mrburnette for your helpful comment!
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Appropriate low-power mode for timing applications

Post by ag123 »

If you simply want the CPU part of stm32 to 'sleep', try doing

wait for interrupt:

Code: Select all

asm("wfi');
This depends on systick to wake the statement up. I tend to use a sleep() function like such

Code: Select all

void setup() {
	pinMode(LED_BUILTIN, OUTPUT);
}

void sleep(uint16_t ms) {
	for(uint16_t i = 0; i< ms; i++)
		asm("wfi");
}

void loop() {
	sleep(1000);
	togglePin(LED_BUILTIN);
}
This works quite similar to delay(), but that it isn't always milliseconds. If other interrupts such as USB SOF interrupt or timer interrupts fires, it would also wake that asm("wfi") statement up.

Accordingly, one way to save power is not to clock the peripherals that you don't use, but in the context of 'sketches', it normally means hacking codes in the core. You can do so in your local copy of it.

Deep sleep is 'hard to use', as functionally it is good as turning the stm32 off.
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Re: Appropriate low-power mode for timing applications

Post by mebab »

Thanks ag123. Yes, I have already implemented the sleep mode and the way we deactivate unnecessary clocks and they helped a lot to lower power consumption. Agree with you about the deep sleep mode. I think the deep sleep mode may work fine in many applications which mainly are not exactly fixed-time dependent despite mine.

Thanks again to all who gave valuable comments.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: Appropriate low-power mode for timing applications

Post by mrburnette »

mebab wrote: Mon May 31, 2021 8:04 pm ...
Is there any way to keep timing other than using millis() while using deep sleep mode?
Could you explain just what "keep timing" actually means? That is, do you require only "seconds accuracy" or do you require system clock phase accuracy. My thinking is it is very easy to configure a 32KHz "watch crystal" to generate an interrupt to wake up the application uC.

Ex:
https://www.insidegadgets.com/2011/10/0 ... -attiny85/

Or, a clock chip: https://www.adafruit.com/product/3295

Or, https://www.mouser.com/new/adafruit/ada ... mer-board/
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Re: Appropriate low-power mode for timing applications

Post by mebab »

More details in the following:
1. I do everything to lower power consumption in my working application.
2. I use the internal RTC of STM32 to activate a software interrupt, to read few sensors every 10 ms within a main loop (fixed in 10 sec).
3. The total time of the whole operation in the loop varies. Therefore, at the end of the loop, I might have few seconds left which is good for further power saving while the interrupt service routine has to keep running.

4. I use sleep mode whenever I need either a time delay or during that remaining time of the loop.
5. The total average current consumption of the whole system is about 5 mA thanks to working around clocks, sleep mode, etc.

6. I replaced sleep mode with deep sleep mode. However, I was faced with the timing inaccuracy while using millis() command. Please refer to fpiSTM's comment.

My question:
Based on mrburnette , I should also be able to use an external clock (LSE or HSE) source while I have deep sleep mode. Then, I would have the same millis()command and interrupt working accurately. Am I right? If yes, I need some time to prepare the system to do the test and reply.

Thanks a lot.
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Appropriate low-power mode for timing applications

Post by fpiSTM »

Honestly I don't understand your point with millis() vs deep sleep mode.
Could you share your sketch?
Post Reply

Return to “General discussion”