Using mbed tickers with dot sleep

Home Forums mDot/xDot Using mbed tickers with dot sleep

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #23455
    Costa Walcott
    Participant

    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 with wait(10) then it works, but I want the xDot sleeping in between callbacks, not wasting CPU cycles.

    Thanks!

    #23458
    Ryan Klaassen
    Blocked

    No 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 notes

    No 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.

    #23460
    Costa Walcott
    Participant

    Thanks 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?

    #23465
    Ryan Klaassen
    Blocked

    I 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.

    #23466
    Costa Walcott
    Participant

    If 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.

    #23468
    Ryan Klaassen
    Blocked

    That is correct, the ticker ISR in your code will only fire after the dot has been awake for 2 seconds.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.