Low Power/Sleep Library?

Post here first, or if you can't find a relevant section!
bdbell
Posts: 20
Joined: Tue Apr 28, 2015 7:35 am

Low Power/Sleep Library?

Post by bdbell » Mon Jun 08, 2015 12:50 pm

I have been lurking in this forum and the original thread for a while - first off want to thank Roger and everyone else here for this great work on getting the STM32 working in the latest arduino environment!

Has anyone here done anything with Low-Power or sleep modes with the Maple Mini or similar clones? I am designing a device using STMF103C8T6 and a 2.4" ILI9341 TFT. I want to turn off the display and put the device to sleep after a certain time of non-use. I have it working to turn off the display, and turning on using an interrupt on a button - but not having much luck finding any information on using Low Power modes. There are some examples on Leaflabs site using the same Sleep library as Arduino - but I have had no luck getting anything to work. Not so good with the low-level programming or library coding - more of a hardware guy...

The only thing I have found that does seem to work, at least reduces the CPU speed and power draw;

rcc_set_prescaler(RCC_PRESCALER_AHB, RCC_AHB_SYSCLK_DIV_256); //slows down cpu and reduces power

rcc_set_prescaler(RCC_PRESCALER_AHB, RCC_AHB_SYSCLK_DIV_1); /normal cpu speed

but when I use this occasionally the button interrupt to wake the device does not work consistently. This may be the way I have set up the interrupts...

Any help would certainly be appreciated!

Brent

User avatar
mrburnette
Posts: 1877
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: Low Power/Sleep Library?

Post by mrburnette » Mon Jun 08, 2015 12:58 pm

I have personally not looked into low-power, but the original Leaflabs forum has some relevant posts. As we inherited and evolved from the Leaflabs' effort, most of the structure should still be in place:

http://forums.leaflabs.com/topic.php?id=1437

http://forums.leaflabs.com/topic.php?id=922


As you explore the possibilities, post back your experience.

ray

victor_pv
Posts: 1737
Joined: Mon Apr 27, 2015 12:12 pm

Re: Low Power/Sleep Library?

Post by victor_pv » Tue Jun 09, 2015 2:18 pm

Thanks for sharing how to change the clock divider to save some power. Have you done any measurement on power usage by any chance?

I am making something that will run on batteries and definitely reducing speed will help me, as I probably don't need half the CPU power in this MCU.

EDIT: Those posts by Ray look promising.
I think you could use the systick to trip an interrupt every ms, check if there is something to do (like any input to process) and if not, go back to sleep.
The systick handler in libmaple has a function to set a callback function in your code, so once the interrupt trips besides incrementing the systick counter it will call your function to do whatever needed. You can also implement your own systick handler altogether.

If you have a look at the CoOS or FreeRTOS files, and search for systick, you will see how those setup the systick and systick callback.

I will try later to change CoOS and set the idle task to put the CPU to sleep when there is nothing to do and see if it works.

Anyone can suggest a measurable way to see if the CPU is sleeping much?

User avatar
ahull
Posts: 1650
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: Low Power/Sleep Library?

Post by ahull » Tue Jun 09, 2015 2:37 pm

victor_pv wrote: Anyone can suggest a measurable way to see if the CPU is sleeping much?
Current measurement would seem the obvious one. If the average current goes down.. we are sleeping..

One of the things that irritates me about most of these board is the "power led" that they invariably include with no way to jumper it off. Even a single LED consumes significant amounts of juice when you are running on batteries, especially if your project is otherwise pretty frugal. I might do a little surgery on my "ugly board" and disconnect the power LED, then rig up my multimeter (or one of the other boards and a suitable current shunt resistor to measure things with the ADC). The spec of the processor suggests we should be able to drop down to a few uA in lowest powwr mode if we get things right. The LED on the other hand may consume 20mA
- Andy Hull -

User avatar
mrburnette
Posts: 1877
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: Low Power/Sleep Library?

Post by mrburnette » Tue Jun 09, 2015 3:35 pm

Anyone can suggest a measurable way to see if the CPU is sleeping much?
A small 1 Ohm resistor in series with the STM32 and use a scope to visualize the On/Sleep cycles by measurement of the Vd across the resistor.

On my Maple Mini, at full speed, the current is around 50mA, which would be a Vd of 165mV so a 1x probe should provide a decent indication while leaving 3.3 - 0.165 or 3.135V for the uC. If you are going to simulate a battery project, then I would recommend using the actual (fresh) batteries you intend on using, LiFePO4, Alkaline, etc. Chemistry will have an impact although I have not documented (anywhere I can find easily) what that will be. From a current measurement, you are not really overly concerned since you are looking for the deltas in current across the 1 Ohm resistor.

I wonder if the trick of reading the Arduino AVR battery voltage using the uC would work with the STM32... if so, once a critical voltage was reached you could do something: blink Power LED, display icon on 5110, turn off backlight, drop the uC speed further!


Ray

victor_pv
Posts: 1737
Joined: Mon Apr 27, 2015 12:12 pm

Re: Low Power/Sleep Library?

Post by victor_pv » Tue Jun 09, 2015 4:22 pm

I found code by someone to measure the CPU usage in the CoOS idle task, but happens that just measures time wasted there, so even if the MCU is sleeping the time is passing, so that is not valid.

Seems like the current measurement may be the best. This can be a project to put the pig-o-scope to some practical usage too :)
I have a bunch of really low value 1/2W resistor that could eat 50mA for breakfast.
with 0.5ohm I calculate 25mV accross the resistor.
Andy, just have tested the pigoscope more, you think we have good sensitivity for 25mV?
I think I have some multimeter than is sensitive enough too.

I am planning on running something CPU intensive in the awake periods, then let it idle for a while and then repeat again.
Probably periods of several seconds will work the best.
Something like:
-CoOS
-Set idle taks to put CPU to sleep, which should be waken up in the next systick (1ms)
-1 Task only doing CPU math for 5 seconds.
-Then a delay of 5 seconds before running again. during that time the CPU should sleep for almost 1ms, wake up for very short, check that there is no other task to run, and go back to sleep.

Does something like that make sense as a measurable test?

P.S. I was thinking on doing IO to a display, but I think it would be better to measure just the CPU by itself, as an LCD can be put to sleep in other ways.

EDIT:
Ray, I am not sure how you are calculating 165mV with a 1Ohm resistor with 50mA across. I am missing something? that would give me 50mV accross the resistor. :?

User avatar
mrburnette
Posts: 1877
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: Low Power/Sleep Library?

Post by mrburnette » Tue Jun 09, 2015 4:44 pm

Does something like that make sense as a measurable test?
For uC power down testing, the proof is in the actual code. Simulation will get you into the ballpark.
EDIT:
Ray, I am not sure how you are calculating 165mV with a 1Ohm resistor with 50mA across. I am missing something? that would give me 50mV accross the resistor. :?
I'm an idiot :o
My staff would never give me a calculator... especially RPN! 1-2-3 and Excel were all I had.

Ray


True story: A friend working for Tek long ago had a boss that 'stole' the desktop HP programmable calc, the one with the template overlays... He wrote templates and then marked-up with pencil the overlay for easy entry. The calculator sensed the particular template and knew which program to run. So, friend and colleges made an exact duplicate template carefully copying the boss's handwriting - exact except that Pi was slightly skewed from the exact value of 3.141592654. When the boss would check a junior engineer's work, the answer would be different. The boss would go out in the cube area to get the engineer and inevitably they would go back to the boss's office. During this absence, one of the senior engineers would sneak in and swap the template on the calculator with the original template which of course gave the correct answer! They never got caught but had to stop the prank because the boss was getting very agitated that all the new engineers thought he was bonkers!

strawberrymaker
Posts: 266
Joined: Wed Apr 29, 2015 4:04 pm
Location: Hamm, Germany
Contact:

Re: Low Power/Sleep Library?

Post by strawberrymaker » Tue Jun 09, 2015 8:19 pm

Isnt it already pretty much low power when you use less peripherals?
Image
Are all peripherals initialized even if there arent used?
I'd guess using npn transistors for led & vcc for the screen and a ldo with low quiescent current would be good enough. you would only need to send the starting commands of the screen again.

If im wrong ,please tell me :)

You may want to read the datasheet. it got many infos about sleep and wakeup(isnt a0 even described as wakeup?)

~Straw
Twitter : @maker_berry

User avatar
ahull
Posts: 1650
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: Low Power/Sleep Library?

Post by ahull » Tue Jun 09, 2015 9:32 pm

@Straw, you are correct to the extent that when active, the peripherals consume significant amounts of current (well significant in the case of a battery powered project), however to get really low currents, we need to sleep too as even when idling, the processor and memory are likely to be major factors in power consumptions. To use the least amount of power, we need to put as much of the silicon to sleep as we can. The other thing that affects power is clock speed, but often this is not as simple a factor as reduce speed, save power, since getting tasks done quickly may result in longer sleep periods.

EDIT: This library (AVR specific) and the associated documentation should provide some useful insights. I have used it in the past for battery powered projects.
- Andy Hull -

User avatar
ahull
Posts: 1650
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: Low Power/Sleep Library?

Post by ahull » Tue Jun 09, 2015 10:36 pm

Another current hog is likely to be the voltage regulator. Not a worry if you are powering *directly* from batteries, since there wont be a regulator, but if you are serious about low power and your power supply is anything other than a 3.6V LiPo and a diode, or a couple of AA cells then the regulator is a serious design consideration. Here is an example of what I am talking about.

It may sound strange to think of 5mA in any context as hogging power, but where battery powered designs are concerned, it can mean the difference between days of standby battery power and weeks, months or even years. If you can reduce current from 5mA to 500uA then obviously your battery lasts 10 times as long on standby, so your little 120mAh lipo that lasts 1 day between charges becomes a more useful device that goes 10 days before you need to recover it and recharge. A 2000mAh lipo goes from 16 1/2 days to 167 days of standby, and my "Sticks O Dynamite" battery pack lasts forever or thereabouts ;) (theoretically, assuming the average lifespan of a modern commercial product) . Hence my comment about removing the power LED.
- Andy Hull -

Post Reply