M0 Power draw in sleep

Started by yenda, April 04, 2020, 02:00:38 PM

yenda

Hi, I used felix's example code (plus added some more details to help me trace the M0+flash+RF95HW sleep consumption on the CurrentRanger
My results are
18mA -- without sleepin anything
16mA -- after sleeping flash
16mA -- after sleeping the lora module
~2mA -- after sleeping the SAMD (using the ZeroRTC)

This is the code:
#include <Arduino.h>

#include <RTCZero.h>

#include <RadioHead.h>
#include <RH_RF95.h>
#include <Wire.h>

#define MOTEINO_M0
#define LORA_FREQUENCY 915

#include <SPIFlash.h> //get it here: https://www.github.com/lowpowerlab/spiflash
SPIFlash flash(SS_FLASHMEM, 0xEF30); //EF30 for 4mbit  Windbond chip (W25X40CL)

constexpr static const int bauds = 115200;

void blink(int ms) {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(ms);
  digitalWrite(LED_BUILTIN, LOW);
  delay(ms);
}

#define S Serial0

void alarmMatch(void);
void resetAlarm(void);

RTCZero zerortc;

// Set how often alarm goes off here
const byte alarmSeconds = 10;
const byte alarmMinutes = 0;
const byte alarmHours = 0;

volatile bool alarmFlag = false; // Start awake

#if defined (MOTEINO_M0)
RH_RF95 rf95(A2, 9);
#else
RH_RF95 rf95;
#endif

void setup()
{
  S.begin(bauds);
  delay(1000); // Wait for console
  
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  S.println("Before flash initialized");
  S.flush();

  delay(5000);
  
  for (int8_t i = 0; i < 5; ++i) {
    if (!flash.initialize()) {
      S.println("Couldn't initialize flash");
      S.flush();
    } else {
      S.println("Flash initialized, putting to sleep");
      S.flush();
      flash.sleep();
      break;
    }
  }
  S.println("After flash initialized"); 
  S.flush();
  
  delay(5000);

  rf95.init();
  rf95.setFrequency(LORA_FREQUENCY);
  rf95.sleep();
  
  S.println("After sleeping down LORA");
  S.flush();
  
  delay(5000);


  zerortc.begin(); // Set up clocks and such
  resetAlarm();  // Set alarm
  zerortc.attachInterrupt(alarmMatch); // Set up a handler for the alarm

}

void loop()
{
  if (alarmFlag == true) {
    alarmFlag = false;  // Clear flag
    digitalWrite(LED_BUILTIN, HIGH);
    S.println("Alarm went off - I'm awake!");
    delay(5000);
  }

  
  resetAlarm();  // Reset alarm before returning to sleep
  
  S.println("Alarm set, going to sleep now.");
  digitalWrite(LED_BUILTIN, LOW);
  S.flush();
  zerortc.standbyMode();    // Sleep until next alarm match
}

void alarmMatch(void)
{
  alarmFlag = true; // Set flag
}

void resetAlarm(void)
{
  byte seconds = 0;
  byte minutes = 0;
  byte hours = 0;
  byte day = 1;
  byte month = 1;
  byte year = 1;
  
  zerortc.setTime(hours, minutes, seconds);
  zerortc.setDate(day, month, year);

  zerortc.setAlarmTime(alarmHours, alarmMinutes, alarmSeconds);
  zerortc.enableAlarm(zerortc.MATCH_HHMMSS);
}



Any idea how to get closer to those ~10uA?

I have found this:
https://lowpowerlab.com/forum/moteino-m0/moteino-m0-versus-standard-moteino-r6-current-draw/
but I'm not sure where the writeRegister function resides and when I call rf95.spiWrite(0x01, 0x00), there is no change (and the rf95.sleep() calls exactly the same function).

I have found also this:
https://github.com/things-nyc/LowPowerLoRaBoards
but I'm didn't get to experiment with that code yet. Plus the claim about the SS being incorrectly set seems not to be right, as in my case, the flash seems to go sleep even without that.

Will be grateful for any suggestion. I'm newbie, sorry if I didn't do some completely obvious step.


Felix