TinyCircuits

Sleep for 300ms

Sleep for 300ms
« on: July 22, 2019, 11:04:41 AM »
Hey Tiny... Duiners!?

I'm doing a fairly simple virtual pet "game" using TinyScreen+ (Video Player kit). It's mostly short processed videos of our pet in an interactive shell so that it's kind of "alive" and has "needs" (tamagotchi like). Still WIP, I have a strict deadline for a gift to my wife :))

Would be neat to make it less power hungry. How do you make TinyScreen+ sleep for, say, 300ms (consuming very minimal power)? I think I figured out how to do deep sleep interrupted by press of a button:
```

    attachInterrupt(TSP_PIN_BT1, blink, LOW);
    attachInterrupt(TSP_PIN_BT2, blink, LOW);
    attachInterrupt(TSP_PIN_BT3, blink, LOW);
    attachInterrupt(TSP_PIN_BT4, blink, LOW);
    display.off();
    SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
    __WFI();
    detachInterrupt(TSP_PIN_BT1);
    detachInterrupt(TSP_PIN_BT2);
    detachInterrupt(TSP_PIN_BT3);
    detachInterrupt(TSP_PIN_BT4);
    delay(500); // Just to ignore the already pressed button for 0.5s
    display.on();
```
I'm pretty new at C++ and embedded. Looks as if this works (does it look so to you?), but still measuring the battery life with and without this mode on. What I cannot figure out though, is how to make it wake up by itself, say in 300ms? I know I need to crank up some magic clocks like TC5 and fiddle with some register, but I decided to not read the entire datasheet for SAMD21 just yet and ask if maybe some one has a routine handy for sleeping? And all of that is assuming that the built-in "delay" function is not good. I looked at its source code and it looks like it's just a `while` loop, so must be burning through these mAh pretty quick.

Willing to keep doing my research, but hit a roadblock for now, tough to get oriented in the world of C++ in a couple of weeks (I'm a .NET dev)

Re: Sleep for 300ms
« Reply #1 on: July 22, 2019, 01:58:14 PM »
Hmm... I think I found a good sample on how to set up an RTC clock alarm here:
https://learn.tinycircuits.com/Sensors/Real-Time-Clock_TinyShield_Tutorial/

Not sure if it's useful for my SAMD21 as it mentions AVR... But can be useful in more ways than one as I can also benefit from being able to tell time to regulate the life of the creature. Will try to incorporate the RTC alarm interrupt into my code and post my own answer with the snippet of what I end up with, if it's working.

Re: Sleep for 300ms
« Reply #2 on: July 27, 2019, 03:51:47 PM »
Found it. The sleep() function can be found in this Arduino library:
https://www.arduinolibraries.info/libraries/arduino-low-power

Works perfectly on my TinyScreen+ (Atmel SAM D21). Also depends on a very handy RTCZero library that has all the methods needed to manipulate the clock and set alarms. That's how it wakes the device up, using the RTC clock, which limits the minimum deep sleep duration to 1 second. Can't guarantee waking up faster than 1 second using this method.

What I also found a bit annoying is that there's an audible pop in my speaker (I'm using the SD card & Audio output shield) when going to sleep and then when waking up. This can be slightly reduced by writing the value of (experimentally found) 323 to audio output before and after to minimize the drop of the membrane. Beats me why so.
« Last Edit: August 30, 2019, 03:44:07 AM by evilkos »