M5Paper wakeup cause



  • Hello,

    I'm playing around with the M5Paper RTC and I'm stuck on differentiating between a restart caused by pressing the reset button, and a restart caused by a shutdown() with a RTC IRQ wakeup.

    I'm writing a simple clock application that updates once a minute with the following properties:

    • On boot I paint a decorative frame (I actually pull it by http), connect to ntp and setup the RTC clock. Finally I draw the time, and do a shutdown(60) to wake up in a minute.
    • After a minute I want to wake up, draw the time, and go back to sleep.

    So I did all the initialization in setup() and initialized the RTC, setup the font cache, and then went to sleep for 60s. But when I wake up after 60s I need to know whether the RTC has already been initialized, and whether I have already painted the clock frame. Right now I do all the setup once a minute.

    The ESP itself has a wake up "reason". But how would I access it on the M5Paper? And does it work in the context of the BM8563 RTC?

    Thanks!



  • Hello @dov

    the RTC clock has a timer flag which when set tells you that the RTC timer has woken M5Paper; if not set then it was a regular restart. I've implemented that for M5CoreInk (which has a similar architecture) here.

    BTW: The wake up reason of the ESP32 doesn't help you here. As far as the ESP32 is concerned it's a regular start in both cases as the shutdown kills the power to the ESP32 completely. (Only when the ESP32 stays powered and goes into deep sleep or light sleep you can use the ESP32 wake up reason.)

    Thanks
    Felix



  • You could use the EEPROM to store the state in - e.g. last time the device went to sleep, last time NTP sync happened, etc. - and use that to decide which path of logic you need to follow.

    For example, this could be the full flow:

    setup():

    1. Get EEPROM, extract last NTP sync (last_ntp_sync), last boot time (last_boot), and last requested sleep length (sleep_duration)
    2. Get RTC time (rtc_time)
    3. If last_ntp_sync is empty, go to first_boot()
    4. If last_boot is empty, go to first_boot()
    5. If rtc_time - last_boot < sleep_duration (maybe add some wiggle room here, 1-2s should be fine), go to manual_wakeup()
    6. Otherwise, go to rtc_wakeup()

    first_boot():

    1. Connect to WiFi
    2. Request date and time from NTP = new_rtc_time
    3. Update RTC with new_rtc_time
    4. Update EEPROM's last_ntp_sync and last_boot_time
    5. Proceed to draw_clock()

    rtc_wakeup():

    1. Check if last_ntp_sync is not too old (e.g. you'd probably want to sync with NTP daily)
    2. If last_ntp_sync is too old, go to a method that syncs RTC time
    3. Update EEPROM's last_boot_time
    4. Proceed to draw_clock()

    manual_wakeup():

    1. Do the whole NTP check dance again (probably worth organising into a separate method)
    2. Do whatever you want to do if the device wakeup happened due to the button press
    3. Proceed to draw_clock()

    Obviously extend it to your liking, but by storing these values, you can compare the last stored date with the RTC time, and act accordingly. It's not as elegant as e.g. having a proper boot reason, but it works.



  • @dov I'm very new to the M5Paper, but I'm wondering if you couldn't use the esp_deep_sleep_start() instead of shutdown because this will keep the RTC going and shutdown everything else.

    In theory, you might be able to call esp_err_tesp_sleep_enable_timer_wakeup() with an appropriate duration and then simply continue. According to the documentation the Wifi connection data is kept as well.

    I'm currently trying to work on something like this as well.



  • Hi @felmue,

    Interesting post you wrote - nice solution to detect reason for awakening the M5Paper. But unfortunately the code you gave for M5Corelink doesn't compile on a M5Paper.

    I did some trial and error modifications and within 5 minutes I got code that does compile.
    Here just the relevant code in setup() - sketch from an example in the M5EPD library:

    #include <M5EPD.h>
    
    // is not yet working as should be
    
    M5EPD_Canvas canvas(&M5.EPD);
    
    void setup() {
      // Check power on reason before calling M5.begin()
      //  which calls Rtc.begin() which clears the timer flag.
      Wire1.begin(21, 22);                  // Jop: compiles
    //uint8_t data = M5.rtc.ReadReg(0x01);  // Jop: compiles NOT
      uint8_t data = M5.RTC.readReg(0x01);  // Jop: compiles   
       
      M5.begin();
    
      bool flagRTC = false;
      
      // Check timer flag
      if ((data & 0b00000100) == 0b00000100) {
        flagRTC = true;
        Serial.println("Power on by: RTC timer");
      }
      else {
        Serial.println("Power on by: PWR Btn");
      }
         
      M5.EPD.SetRotation(90);
      M5.TP.SetRotation(90);
      M5.EPD.Clear(true);
      M5.RTC.begin();
      
      canvas.createCanvas(540, 960);  
      canvas.setTextSize(3);
      if (flagRTC)
        canvas.drawString("Power on by: RTC timer", 25, 250); // <= this one we see always
      else  
        canvas.drawString("Power on by: PWR Btn", 25, 250);
        
      canvas.drawString("Press PWR Btn for sleep!", 45, 350);
      canvas.drawString("after 5 sec wakeup!", 70, 450);
      canvas.pushCanvas(0, 0, UPDATE_MODE_DU4);
    }
    
    void loop() {
      if (M5.BtnP.wasPressed()) {      
        canvas.drawString("I'm going to sleep.zzzZZZ~", 45, 550);
        canvas.pushCanvas(0, 0, UPDATE_MODE_DU4);
        delay(1000);
        M5.shutdown(5);
      }
      M5.update();
      delay(100);
    }
    

    As you see I had to change just some capital-mode in identifiers to get it compiled.
    But apparently not the right register or bits are used for detecting wakeup cause.

    Maybe someone else is triggered by this to find the full solution for the M5Paper.



  • Hello @Jop

    thank you for trying it on an M5Paper.

    Try to change Wire1.begin(21, 22) to Wire.begin(21, 22). M5Paper unlike M5CoreInk uses Wire for its internal I2C. And having Wire and Wire1 setup to use the same set of GPIOs makes M5.RTC.begin() fail and thus the timer flag never gets cleared.

    Cheers
    Felix



  • @felmue
    Hi Felix,

    Good tip. It works.

    Here the update code based on an example from the M5EPD library:

    #include <M5EPD.h>
    
    // m5paper-wakeup-cause
    // see forum: https://community.m5stack.com/topic/2851/m5paper-wakeup-cause/6
    
    M5EPD_Canvas canvas(&M5.EPD);
    
    void setup() {
      //  Check power on reason before calling M5.begin()
      //  which calls RTC.begin() which clears the timer flag.
      Wire.begin(21, 22);                  
      uint8_t data = M5.RTC.readReg(0x01);     
       
      M5.begin();
    
      bool flagRTC = false;
      
      // Check timer flag
      if ((data & 0b00000100) == 0b00000100) {
        flagRTC = true;
        Serial.println("Power on by: RTC timer");
      }
      else {
        Serial.println("Power on by: PWR Btn");
      }
         
      M5.EPD.SetRotation(90);
      M5.TP.SetRotation(90);
      M5.EPD.Clear(true);
      M5.RTC.begin();
      
      canvas.createCanvas(540, 960);  
      canvas.setTextSize(3);
      if (flagRTC)
        canvas.drawString("Power on by: RTC timer", 25, 250); // <= this one we should when waking up from sleep
      else  
        canvas.drawString("Power on by: PWR Btn", 25, 250);
        
      canvas.drawString("Press PWR Btn for sleep!", 45, 350);
      canvas.drawString("after 5 sec wakeup!", 70, 450);
      canvas.pushCanvas(0, 0, UPDATE_MODE_DU4);
    }
    
    void loop() {
      if (M5.BtnP.wasPressed()) {      
        canvas.drawString("I'm going to sleep.zzzZZZ~", 45, 550);
        canvas.pushCanvas(0, 0, UPDATE_MODE_DU4);
        delay(1000);
        M5.shutdown(5); // shut donw now and wake up after 5 seconds
      }
      M5.update();
      delay(100);
    }
    

    One extra remark (maybe for a new topic): the shutdown() routine has some overloaded variations. Unfortunately not very well documented, as the version with only time (see: int shutdown( const rtc_time_t &RTC_TimeStruct); ) seems not to work as expected. If add the date too (last overloaded version) it works. Maybe my expectations are wrong. Sometimes one should like a bit more specifications from M5Stack in this documentation (is a timesaver).

    Anyway thnx for your suggestion Wire1 => Wire.



  • Hello @Jop

    thanks for reporting back. I am happy to hear you got it working to your liking.

    Re shutdown: that has been fixed in a pull-request, but unfortunately M5Stack engineers are very slow lately (or no longer allowed or interested?) to approve and use pull-requests from the community.

    Essentially the following two lines need to be removed:

        out_buf[2] = 0x00;
        out_buf[3] = 0x00;
    

    See here around line 232.

    Thanks
    Felix



  • @felmue
    Hi Felix,

    Thnx for your fast response.

    I out commented the 2 lines you mentioned, this seems to work, but I experience more problems.
    This shutdown() logic of the M5Paper is rather buggy. I think some engineers of M5Stack should inspect this modules and repair.
    For example: I want my M5Paper to be waked up by the RTC every full 5 minutes, then it has to run for about 10 to 15 seconds and to go in sleepmode for the rest of that 5-minute period. For that I wrote routine goSleep() to calculate the next moment of awakening.
    I tried several possibilities for the different overlaid methods of shutdown():

    1. calculate remaining seconds to go sleep and used overload-2
    2. calculate timestamp for the next 5-minute moment and used overload-3
    3. calculate date & timestamps for the next 5-minute moment and used overload-4

    Now comes the weird thing (so the bugs), in any of these cases the wakeup comes ALSO at every 3-minute, so both at every 3-minute and at every 5-minute moments.
    So where I want to have this wake up schedule: hh:00 hh:05 hh:10 hh:15 hh:20 ...and so on...
    I get this wake up schedule: hh:00 hh:03 hh:05 hh:08 hh:10 hh:13 hh:15 hh:18 hh:20 ...and so on...

    Before your suggested patch I tried only method 1 (as 2 did not work without your patch) and then it was every 4-minute and every 5-minute my M5Paper woke up by the RTC.
    I inspected my calculations logic and this is OK. The problem is somewhere in the M5Paper library.

    If necessary I can isolate the code I use to show you; but that's a bit more work.



  • @felmue

    Hi Felix,

    In addition to my last post, I wrote that the shutdown() looks rather buggy.

    Now I found reason for this behavior - partly reason - some overload variations are now OK, but one has still problem as I described in my last post.
    The reason for buggy behavior, I included this:
    #include <ESP32Time.h>
    ESP32Time rtc;
    to make it possible to set also the ESP32 internal RTC that's present in this processor, named by me with "rtc".
    The external BM8563 I named as "RTC".
    Maybe these library is interfering with the BM8563-code.

    I said "partly reason" as Overload-2 still has the same problem as I wrote before.



  • Hello @Jop

    have you tried to rename rtc to something else? Does that fix the issue?

    @M5Stack engineers : any idea what's going on here?

    Thanks
    Felix



  • @felmue
    Hello Felix,

    No, didn't try rename rtc. What difference should it make? (In C++ rtc and RTC are different identifiers, is n't it).
    The reason for introduction of the own internal rtc of the ESP32 was looking for a simple way to set the system clock from the M5Paper RTC after awakening and without connecting to WiFi (this is very time- and battery-consuming).

    The weird thing is: syncing with NTP is rather simple in ESP32-Arduino environment, but just setting the system time equal to the 'external' BM8563 RTC-time is more complicated. There is no simple routine. I found some not so elegant way with mktime().

    Another point: I discovered bugs with awakening the M5Paper when this awakening moment should be exactly at midnight: so at 24:00u or 00:00u.
    See a next post (if I find time to describe).



  • To make this detection really reliable I had to change the RTC register check like this:

    Wire.begin(21, 22);
    uint8_t reason = M5.RTC.readReg(0x01);
    // now it's safe
    M5.begin();
    // check reboot reason flag: TIE (timer int enable) && TF (timer flag active)
    if ((reason & 0b0000101) == 0b0000101) {
        restartByRTC = true;
        Serial.println("Reboot by RTC");
    } else {
        restartByRTC = false;
        Serial.println("Reboot by power button / USB");
    }