Skip to content

Commit

Permalink
Merge pull request #347 from andreagilardoni/rtc-fixes
Browse files Browse the repository at this point in the history
Fix RTC resetting after reboot
  • Loading branch information
andreagilardoni authored Aug 7, 2024
2 parents a430d9f + 06f704c commit 731b943
Show file tree
Hide file tree
Showing 20 changed files with 976 additions and 226 deletions.
2 changes: 1 addition & 1 deletion extras/e2studioProjects/Santiago/configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,7 @@
</config>
<config id="config.driver.rtc">
<property id="config.driver.rtc.param_checking_enable" value="config.driver.rtc.param_checking_enable.bsp"/>
<property id="config.driver.rtc.open_set_source_clock" value="config.driver.rtc.open_set_source_clock.enabled"/>
<property id="config.driver.rtc.open_set_source_clock" value="config.driver.rtc.open_set_source_clock.disabled"/>
</config>
<config id="config.driver.spi">
<property id="config.driver.spi.param_checking_enable" value="config.driver.spi.param_checking_enable.bsp"/>
Expand Down
2 changes: 1 addition & 1 deletion extras/e2studioProjects/Santiago/ra_cfg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ FSP Configuration

Module "Realtime Clock (r_rtc)"
Parameter Checking: Default (BSP)
Set Source Clock in Open: Enabled
Set Source Clock in Open: Disabled

Module "Timer, General PWM (r_gpt)"
Parameter Checking: Default (BSP)
Expand Down
2 changes: 1 addition & 1 deletion extras/e2studioProjects/portenta_h33_lib/configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@
</config>
<config id="config.driver.rtc">
<property id="config.driver.rtc.param_checking_enable" value="config.driver.rtc.param_checking_enable.bsp"/>
<property id="config.driver.rtc.open_set_source_clock" value="config.driver.rtc.open_set_source_clock.enabled"/>
<property id="config.driver.rtc.open_set_source_clock" value="config.driver.rtc.open_set_source_clock.disabled"/>
</config>
<config id="config.driver.spi">
<property id="config.driver.spi.param_checking_enable" value="config.driver.spi.param_checking_enable.bsp"/>
Expand Down
8 changes: 4 additions & 4 deletions extras/e2studioProjects/portenta_h33_lib/ra_cfg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ FSP Configuration
Reception: FIFOs: FIFO 7: Interrupt Threshold: 1/2 full
Reception: FIFOs: FIFO 7: Payload Size: 8 bytes
Reception: FIFOs: FIFO 7: Depth: 16 stages
Reception: Acceptance Filtering: Channel 0 Rule Count: 1
Reception: Acceptance Filtering: Channel 1 Rule Count: 1
Reception: Acceptance Filtering: Channel 0 Rule Count: 2
Reception: Acceptance Filtering: Channel 1 Rule Count: 2

Module "I2C Master (r_iic_master)"
Parameter Checking: Default (BSP)
Expand Down Expand Up @@ -408,7 +408,7 @@ FSP Configuration

Module "Flash (r_flash_hp)"
Parameter Checking: Default (BSP)
Code Flash Programming Enable: Disabled
Code Flash Programming Enable: Enabled
Data Flash Programming Enable: Enabled

Module "USB PCDC (r_usb_pcdc)"
Expand Down Expand Up @@ -442,7 +442,7 @@ FSP Configuration

Module "Realtime Clock (r_rtc)"
Parameter Checking: Default (BSP)
Set Source Clock in Open: Enabled
Set Source Clock in Open: Disabled

Module "Timer, General PWM (r_gpt)"
Parameter Checking: Default (BSP)
Expand Down
171 changes: 171 additions & 0 deletions libraries/RTC/examples/RTC_Reset/RTC_Reset.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* RTC_Reset
*
* This example sets the RTC with __TIMESTAMP__ macro and can be used to test whether the settings of
* RTC persists after a reset or when VRTC is powered. If VRTC is powered I can disconnect the board
* from the usb cable and the RTC value will persist
*
* Find the full UNO R4 WiFi RTC documentation here:
* https://docs.arduino.cc/tutorials/uno-r4-wifi/rtc
*/

// Include the RTC library
#include "RTC.h"

bool alarmFlag = false;

DayOfWeek convertDayOfWeek(String s)
{
if (s == String("Mon"))
{
return DayOfWeek::MONDAY;
}
if (s == String("Tue"))
{
return DayOfWeek::TUESDAY;
}
if (s == String("Wed"))
{
return DayOfWeek::WEDNESDAY;
}
if (s == String("Thu"))
{
return DayOfWeek::THURSDAY;
}
if (s == String("Fri"))
{
return DayOfWeek::FRIDAY;
}
if (s == String("Sat"))
{
return DayOfWeek::SATURDAY;
}
if (s == String("Sun"))
{
return DayOfWeek::SUNDAY;
}
}

Month convertMonth(String s)
{
if (s == String("Jan"))
{
return Month::JANUARY;
}
if (s == String("Feb"))
{
return Month::FEBRUARY;
}
if (s == String("Mar"))
{
return Month::MARCH;
}
if (s == String("Apr"))
{
return Month::APRIL;
}
if (s == String("May"))
{
return Month::MAY;
}
if (s == String("Jun"))
{
return Month::JUNE;
}
if (s == String("Jul"))
{
return Month::JULY;
}
if (s == String("Aug"))
{
return Month::AUGUST;
}
if (s == String("Sep"))
{
return Month::SEPTEMBER;
}
if (s == String("Oct"))
{
return Month::OCTOBER;
}
if (s == String("Nov"))
{
return Month::NOVEMBER;
}
if (s == String("Dec"))
{
return Month::DECEMBER;
}
}

RTCTime currentRTCTime()
{
// Get a compilation timestamp of the format: Wed May 10 08:54:31 2023
// __TIMESTAMP__ is a GNU C extension macro
// We can't use the standard macros __DATE__ and __TIME__ because they don't provide the day of the week
String timeStamp = __TIMESTAMP__;
// Extract the day of the week
int pos1 = timeStamp.indexOf(" ");
DayOfWeek dayOfWeek = convertDayOfWeek(timeStamp.substring(0, pos1));
// Extract the month
++pos1;
int pos2 = timeStamp.indexOf(" ", pos1);
Month month = convertMonth(timeStamp.substring(pos1, pos2));
// Extract the day
pos1 = ++pos2;
pos2 = timeStamp.indexOf(" ", pos1);
int day = timeStamp.substring(pos1, pos2).toInt();
// Extract the hour
pos1 = ++pos2;
pos2 = timeStamp.indexOf(":", pos1);
int hour = timeStamp.substring(pos1, pos2).toInt();
// Extract the minute
pos1 = ++pos2;
pos2 = timeStamp.indexOf(":", pos1);
int minute = timeStamp.substring(pos1, pos2).toInt();
// Extract the second
pos1 = ++pos2;
pos2 = timeStamp.indexOf(" ", pos1);
int second = timeStamp.substring(pos1, pos2).toInt();
// Extract the year
pos1 = ++pos2;
pos2 = timeStamp.indexOf(" ", pos1);
int year = timeStamp.substring(pos1, pos2).toInt();

return RTCTime(day, month, year, hour, minute, second, dayOfWeek, SaveLight::SAVING_TIME_INACTIVE);
}

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

// Initialize the RTC
RTC.begin();

// if the RTC is not running set its value to the compile time __TIMESTAMP__ macro
if(!RTC.isRunning()) {
RTCTime timeToSet = currentRTCTime();
RTC.setTime(timeToSet);
}
}

void loop()
{
Serial.println("The RTC time is: ");
RTCTime currentTime;
RTC.getTime(currentTime);
Serial.print(currentTime.getYear());
Serial.print("-");
Serial.print(Month2int(currentTime.getMonth()));
Serial.print("-");
Serial.print(currentTime.getDayOfMonth());
Serial.print(" ");
Serial.print(currentTime.getHour());
Serial.print(":");
Serial.print(currentTime.getMinutes());
Serial.print(":");
Serial.println(currentTime.getSeconds());

delay(1000);
}
16 changes: 5 additions & 11 deletions libraries/RTC/examples/Test_RTC/Test_RTC.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Test RTC
A test sketch showcasing all RTC showcasing various functionalities related to the RTC module,
A test sketch showcasing all RTC functionalities related to the RTC module,
including setting the time, handling interrupts, and reading time values.
Find the full UNO R4 WiFi RTC documentation here:
Expand Down Expand Up @@ -45,16 +45,10 @@ void setup() {
// Set a specific initial time (August 25, 2022, 14:37:00 Thursday)
RTCTime mytime(25, Month::AUGUST, 2022, 14, 37, 00, DayOfWeek::THURSDAY, SaveLight::SAVING_TIME_ACTIVE);

RTCTime savedTime;
RTC.getTime(savedTime);

// Set the initial time if RTC is not running
// Set the initial time if RTC is not running.
// The RTC may be still running if the board was reset, or if VRTC pin was powered
if (!RTC.isRunning()) {
if (savedTime.getYear() != 2000) {
RTC.setTime(mytime);
} else {
RTC.setTime(savedTime);
}
RTC.setTime(mytime);
}

// Create an alarm time set to 35 seconds
Expand Down Expand Up @@ -147,4 +141,4 @@ void loop() {

status = !status;
delay(1000);
}
}
Loading

0 comments on commit 731b943

Please sign in to comment.