Page 1 of 2

Appropriate low-power mode for timing applications

Posted: Mon May 31, 2021 8:04 pm
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?

Re: Appropriate low-power mode for timing applications

Posted: Tue Sep 21, 2021 7:32 am
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.

Re: Appropriate low-power mode for timing applications

Posted: Tue Sep 21, 2021 8:10 am
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.

Re: Appropriate low-power mode for timing applications

Posted: Wed Sep 22, 2021 12:51 pm
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 3677 times

Re: Appropriate low-power mode for timing applications

Posted: Wed Sep 22, 2021 1:08 pm
by mebab
Thanks mrburnette for your helpful comment!

Re: Appropriate low-power mode for timing applications

Posted: Wed Sep 22, 2021 2:10 pm
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.

Re: Appropriate low-power mode for timing applications

Posted: Wed Sep 22, 2021 4:09 pm
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.

Re: Appropriate low-power mode for timing applications

Posted: Wed Sep 22, 2021 11:19 pm
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/

Re: Appropriate low-power mode for timing applications

Posted: Thu Sep 23, 2021 7:33 am
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.

Re: Appropriate low-power mode for timing applications

Posted: Thu Sep 23, 2021 9:27 am
by fpiSTM
Honestly I don't understand your point with millis() vs deep sleep mode.
Could you share your sketch?