Using mbed tickers with dot sleep
- This topic has 5 replies, 2 voices, and was last updated 6 years, 6 months ago by Ryan Klaassen.
-
AuthorPosts
-
May 16, 2018 at 3:00 pm #23455Costa WalcottParticipant
Hi,
Is it possible to use an Mbed Ticker together with sleep mode on an xDot?
For example:
#include "mbed.h" #include "mDot.h" #include "ChannelPlans.h" DigitalOut led1(LED1); Serial pc(USBTX, USBRX); static void ticker_callback(void) { pc.printf("callback called\r\n"); led1 = !led1; wait(0.05); led1 = !led1; } int main() { pc.baud(115200); lora::ChannelPlan* plan = new lora::ChannelPlan_US915(); mDot *dot = mDot::getInstance(plan); Ticker t; t.attach(callback(ticker_callback), 2.0); pc.printf("starting loop\r\n"); while (true) { dot->sleep(10, mDot::RTC_ALARM, false); } }
The callback is never called in this example. If I replace the
dot->sleep
call withwait(10)
then it works, but I want the xDot sleeping in between callbacks, not wasting CPU cycles.Thanks!
May 16, 2018 at 3:16 pm #23458Ryan KlaassenBlockedNo the ticker timer is not running across sleep. Also printing in an ISR can, and probably will, break things. The dot is waking up and immediately going back to sleep this will never allow the timer object to start counting. Only the RTC is running across sleep for the dot.
Other notes of interest from mbed:
Warnings and notesNo blocking code in ISR: avoid any call to wait, infinite while loop or blocking calls in general.
No printf, malloc or new in ISR: avoid any call to bulky library functions. In particular, certain library functions (such as printf, malloc and new) are not re-entrant, and their behavior could be corrupted when called from an ISR.
While an event is attached to a Ticker, deep sleep is blocked to maintain accurate timing. If you don’t need microsecond precision, consider using the LowPowerTicker class instead because this does not block deep sleep mode.
May 16, 2018 at 3:28 pm #23460Costa WalcottParticipantThanks for the quick response.
So it sounds like if I want to regularly run blocking code (for example, sending data using LoRa) then I should manually calculate appropriate amounts of time to sleep in between calls?
May 16, 2018 at 3:48 pm #23465Ryan KlaassenBlockedI am not quite sure what you are asking. When the xdot is sleeping it can only be woken by a pin set as an interrupt in or the alarm. An ISR can happen when the dot is already awake or sleeping. No blocking code should be run in an ISR. An ISR should do as little as possible, such as set a send_flag. The main loop can then check this flag before it sleeps and send if needed.
May 16, 2018 at 3:54 pm #23466Costa WalcottParticipantIf the ticker timer isn’t running across sleep, then it sounds like I can’t even set a flag in the ISR since it will never be called? It seems like instead I’ll have to manually keep track of the last time each periodic event occurred, then sleep until the next one is scheduled to occur.
May 16, 2018 at 4:14 pm #23468Ryan KlaassenBlockedThat is correct, the ticker ISR in your code will only fire after the dot has been awake for 2 seconds.
-
AuthorPosts
- You must be logged in to reply to this topic.