adafruit / Adafruit_nRF52_Arduino

Adafruit code for the Nordic nRF52 BLE SoC on Arduino
Other
606 stars 492 forks source link

nRF52840 systemOff - wake up issue with RTC #719

Closed Ryann98 closed 2 years ago

Ryann98 commented 2 years ago

Hello. I have an RTC DS3231 feather wing connected on top of my nRF52840. I am putting my nRF52 feather in deep sleep mode-SystemOff and passing as argument the interrupt pin of the RTC which is connected to GPIO#6 in my case to wake up the board. However, the board never seems to wake up.. am I missing something ?

#include <RTClib.h>
#include <Wire.h>
#include <wiring.h>

RTC_DS3231 rtc;
// the pin that is connected to SQW
#define CLOCK_INTERRUPT_PIN 6
//uint32_t pin=6;
uint8_t wake_logic = 0;
volatile byte state = LOW;

void setup()
{

  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  // while(!Serial) delay(10);
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC!");Serial.flush(); while (1) delay(10);
  }

  if (rtc.lostPower()) {
    // this will adjust to the date and time at compilation
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  //we don't need the 32K Pin, so disable it
  rtc.disable32K();

  // Making it so, that the alarm will trigger an interrupt
  pinMode(CLOCK_INTERRUPT_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(CLOCK_INTERRUPT_PIN), onAlarm, FALLING);

  // set alarm 1, 2 flag to false (so alarm 1, 2 didn't happen so far)
  // if not done, this easily leads to problems, as both register aren't reset on reboot/recompile
  rtc.clearAlarm(1);
  rtc.clearAlarm(2);

  // stop oscillating signals at SQW Pin
  // otherwise setAlarm1 will fail
  rtc.writeSqwPinMode(DS3231_OFF);

  // turn off alarm 2 (in case it isn't off already)
  // again, this isn't done at reboot, so a previously set alarm could easily go overlooked
  rtc.disableAlarm(2);

  // schedule an alarm 10 seconds in the future
  if (!rtc.setAlarm1(
        rtc.now() + TimeSpan(0, 0, 0, 10),
        DS3231_A1_Second// this mode triggers the alarm when the seconds match. See Doxygen for other options
      )) {
    Serial.println("Error, alarm wasn't set!");
  } else {
    Serial.println("Alarm will happen in 10 seconds!");
  }
   //wake logic is set to 0 in my case
   systemOff(CLOCK_INTERRUPT_PIN, wake_logic);
}
void loop()
{
  // Serial.println("Going To sleep");
  //wake on logic 0;
  digitalWrite(LED_BUILTIN, state);

  Serial.println("Test");
  // print current time
  char date[10] = "hh:mm:ss";
  rtc.now().toString(date);
  Serial.print(date);
  // the value at SQW-Pin (because of pullup 1 means no alarm)
  Serial.print(" SQW: ");
  Serial.print(digitalRead(CLOCK_INTERRUPT_PIN));
  // whether a alarm happened happened
  Serial.print(" Alarm1: ");
  Serial.print(rtc.alarmFired(1));
  // resetting SQW and alarm 1 flag
  // using setAlarm1, the next alarm could now be configurated
  if (rtc.alarmFired(1)) {
    rtc.clearAlarm(1);
    Serial.println("Alarm cleared");
    rtc.disableAlarm(1);
    Serial.println("RECONFIGURING ALARM");
    rtc.setAlarm1(rtc.now() + TimeSpan(0, 0, 0, 10),DS3231_A1_Second);
  }
  delay(2000);
}

void onAlarm() 
{
  digitalWrite(LED_BUILTIN, HIGH);
  state = !state;
  Serial.println("Alarm occured ! AWAKE NOW ");
}
hathach commented 2 years ago

for question, please use the support forum. If you think this is a bug, please file issue using bug template.