Skip to content

Commit

Permalink
fixed remaining RTC issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cristidragomir97 committed Jan 18, 2024
1 parent 59d306f commit 1ad5e0b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
63 changes: 44 additions & 19 deletions examples/WakeFromRTC/WakeFromRTC.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,67 @@
#include "Arduino_Portenta_C33_LowPower.h"

LowPower lowPower;


RTCTime initialTime(1, Month::JANUARY, 2000, 12, 10, 00, DayOfWeek::TUESDAY, SaveLight::SAVING_TIME_ACTIVE);


void alarmCallback()
{
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
digitalWrite(LED_BUILTIN, HIGH);
lowPower.deepSleep();
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
lowPower.deepSleep();
}


bool sleepFor(int hours, int minutes, int seconds){
{

RTCTime currentTime;
if (!RTC.getTime(currentTime)) {
Serial.println("Failed to get current time");
return false; // Failed to get current time
}
Serial.println(currentTime.toString());

// Convert current time to UNIX timestamp and add the desired interval
time_t currentTimestamp = currentTime.getUnixTime();
currentTimestamp += hours * 3600 + minutes * 60 + seconds;
// Convert back to RTCTime
RTCTime alarmTime(currentTimestamp);
// Configure the alarm match criteria
AlarmMatch match;
match.addMatchSecond(); // Trigger the alarm when the seconds match
match.addMatchMinute(); // Trigger the alarm when the minutes match
match.addMatchHour(); // Trigger the alarm when the hours match
// Set the alarm callback (assuming you have a callback function named alarmCallback)
if (!RTC.setAlarmCallback(alarmCallback, alarmTime, match)) {
Serial.println("Failed to set the alarm");
return false; // Failed to set the alarm
}

// Enable the alarm
Serial.println("Enabling the alarm");
return true;
}
}


void setup(){
Serial.begin(9600);
//while(!Serial);

lowPower = LowPower();
// lowPower.enableWakeupFromRTC(&initialTime, &callback);
lowPower.enableWakeupFromRTC();

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(LED_BUILTIN, LOW);

RTC.begin();

if (!RTC.isRunning()) RTC.setTime(initialTime);

// Trigger the alarm every time the seconds are zero
RTCTime alarmTime;
alarmTime.setSecond(0);

// Make sure to only match on the seconds in this example - not on any other parts of the date/time
AlarmMatch matchTime;
matchTime.addMatchSecond();

//sets the alarm callback
RTC.setAlarmCallback(alarmCallback, alarmTime, matchTime);
if (!RTC.isRunning()) {
RTC.setTime(initialTime);
sleepFor(0, 0, 1);
}

}

Expand Down
11 changes: 2 additions & 9 deletions src/Arduino_Portenta_C33_LowPower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,8 @@ void LowPower::deepSleep(){
}

void LowPower::enableWakeupFromRTC(){
if(deepSleepWakeupSource == 0)
deepSleepWakeupSource = LPM_DEEP_STANDBY_CANCEL_SOURCE_RTC_ALARM;
else
deepSleepWakeupSource = deepSleepWakeupSource | LPM_DEEP_STANDBY_CANCEL_SOURCE_RTC_ALARM;

if(standbyWakeupSource == 0)
standbyWakeupSource = LPM_STANDBY_WAKE_SOURCE_RTCALM;
else
standbyWakeupSource = standbyWakeupSource | LPM_STANDBY_WAKE_SOURCE_RTCALM;
RenesasLowPowerConfig.deep_standby_cancel_source = LPM_DEEP_STANDBY_CANCEL_SOURCE_RTC_ALARM;
RenesasLowPowerConfig.standby_wake_sources = LPM_STANDBY_WAKE_SOURCE_RTCALM;
}

bool LowPower::enableWakeupFromPin(uint8_t pin, PinStatus direction){
Expand Down

0 comments on commit 1ad5e0b

Please sign in to comment.