How to put spark core to sleep and wakeup on interrupt signal on a pin?

Hi,

I am trying to safe battery power by putting the core to sleep and only wakeup when a pin is high. Is there a way to do that or an example project?

Thanks,

1 Like

There has been a discussion about this before: http://community.spark.io/t/spark-wake-during-spark-sleep/4753/11

I’m not sure if there’s an option already, but I’d be interested in one as well. Let’s ping some people from that topic: @kennethlimcp, @BDub, @peekay123, @wgbartley. And maybe @Dave. Do you guys have some updates on this?

2 Likes

As far as i know( our meeting session ended 20 seconds ago)

This function has been mentioned but when it’s coming, i have no idea.

3 Likes

@seannguyen, @Moors7, this is a capability I have been pushing for - an event-driven sleep mode. The STM32 can do it but the capability has to be added to the firmware. I just pushed for this capability in today’s Sprint hangout with Spark. :smile:

4 Likes

I want this too! – Pinging this issue again - https://github.com/spark/core-firmware/issues/95

Thanks,
David

6 Likes

This is a vital function to be used as a battery powered device. Hope to see the functionality soon.

2 Likes

Glad to hear the STM32 is capable of this as it is very important for anything battery powered. Hope to hear about a release date soon.

2 Likes

I’d like to add a ++ yes please to this one. I think it’s a pretty high priority.

2 Likes

Does anyone know what pins are able to wake the STM32 from deep sleep. I want to design this functionality into my PCBs, so when its here my boards are designed correctly from the get go.

Thanks,

Here’s an interesting read about the STM32 in particular the low power modes. Check out pages 31, 52 and 74. It seems that the STM32 can enter a STOP mode where the state of the STM32 is preserved only drawing 14-24uA. It doesn’t seem that its that difficult to incorporate this functionality but then again I know very little about programming. The below link opens as a PDF.

http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=6&ved=0CEcQFjAF&url=http%3A%2F%2Fwww.gaw.ru%2Fpdf%2FThe_Insiders_Guide_to_the_STM32_ARM-based_Microcontroller_Hitex.pdf&ei=Y0LoU-bPH9H3yQTh5IHgBQ&usg=AFQjCNFBk-GfQST8mlDfepBJpl8Ig2-I1g&bvm=bv.72676100,d.aWw

I also found the below post example that wakes up from the RTC or external button. This is for the Stop mode which preserves the state of the core.

I’m currently working on this: https://github.com/spark/core-firmware/issues/95
Will give an update on its progress soon

4 Likes

Hello All, Implemented wakeup from sleep with interrupt.
The corresponding pull request with example code is over here: https://github.com/spark/core-firmware/pull/265
Happy Testing :smile:

4 Likes

This is going to be huge! Thank you for all your efforts, you’re doing a terrific job! Seriously, thanks! :smiley:

1 Like

@satishgn, I noticed that you execute a system reset after the stop. Is there any value in having it continue from the stop instead of a full reset?

@peekay123, there are 2 instances of system reset following the Spark.sleep(pin, mode) call.

The first system reset occurs just after calling sleep after saving the system flag BKP_DR9 with the stop mode flag, pin number and trigger mode. This reset is needed since when the IWDG(Watchdog) is enabled, calling stop mode is not useful since it will cause watchdog reset and there is no way to stop IWDG once started. so we save the entries in BKP register and on main re-entry, we retrieve the stop mode information and execute the stop mode. Since IWDG is not enabled by now, the system will enter Stop mode and wait for the interrupt to trigger the wakeup.

The second reset occurs after waking up from stop mode (interrupt triggered). Now we can continue from here without resetting after re-configuring the system clocks(needed since we used PWR_Regulator_LowPower for lowest power saving). We chose to reset instead since the core is anyway at the start of main() call. If the application timing requirement is very tight, we can avoid this second reset and continue the application execution after re-configuring the clocks

Hope this answers your question.

@satishgn, that makes sense and I was wondering what to do with the clocks after exiting from stop mode and a reset is cleaner. Besides, both deep sleep and stop modes now have the same system reset behavior with the only difference being the wakeup event. Thanks!

1 Like

Just a random thought: Could it be made so that you can set a time and an interrupt? Something like:

Spark.sleep(D2, RISING, 3600);

So that would be “sleep for an hour, unless awaken beforehand by an interrupt”.

Let’s say you’d like to measure temp/humidity and movement. To conserve energy, you only publish your temp/humidity data every 30 minutes, and sleep in between. Your movement sensor should report immediately though, which would need the interrupt. Would something like that be possible, or am I being ridiculous?

Either way, this is already really cool, so again, thanks!

2 Likes

@Moors7, that is a superb idea and I think it is doable. Of course, @satishgn has the final say on that!

3 Likes

@Moors7, @peekay123, Great Idea! I will add one more overloaded method:
Spark.sleep(uint16_t wakeUpPin, uint16_t edgeTriggerMode, long seconds);
Thanks for the cool use-case

4 Likes

Ok, we have now 2 methods:

Spark.sleep(uint16_t wakeUpPin, uint16_t edgeTriggerMode);
Spark.sleep(uint16_t wakeUpPin, uint16_t edgeTriggerMode, long seconds);

Also @peekay123, I have removed the second reset after wakeup from sleep and instead just re-enabled the system clocks(HSE & PLL).

The latest commit is over here:

8 Likes