Skip to content

Commit 731b943

Browse files
Merge pull request #347 from andreagilardoni/rtc-fixes
Fix RTC resetting after reboot
2 parents a430d9f + 06f704c commit 731b943

File tree

20 files changed

+976
-226
lines changed

20 files changed

+976
-226
lines changed

extras/e2studioProjects/Santiago/configuration.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@
15511551
</config>
15521552
<config id="config.driver.rtc">
15531553
<property id="config.driver.rtc.param_checking_enable" value="config.driver.rtc.param_checking_enable.bsp"/>
1554-
<property id="config.driver.rtc.open_set_source_clock" value="config.driver.rtc.open_set_source_clock.enabled"/>
1554+
<property id="config.driver.rtc.open_set_source_clock" value="config.driver.rtc.open_set_source_clock.disabled"/>
15551555
</config>
15561556
<config id="config.driver.spi">
15571557
<property id="config.driver.spi.param_checking_enable" value="config.driver.spi.param_checking_enable.bsp"/>

extras/e2studioProjects/Santiago/ra_cfg.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ FSP Configuration
198198

199199
Module "Realtime Clock (r_rtc)"
200200
Parameter Checking: Default (BSP)
201-
Set Source Clock in Open: Enabled
201+
Set Source Clock in Open: Disabled
202202

203203
Module "Timer, General PWM (r_gpt)"
204204
Parameter Checking: Default (BSP)

extras/e2studioProjects/portenta_h33_lib/configuration.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@
10741074
</config>
10751075
<config id="config.driver.rtc">
10761076
<property id="config.driver.rtc.param_checking_enable" value="config.driver.rtc.param_checking_enable.bsp"/>
1077-
<property id="config.driver.rtc.open_set_source_clock" value="config.driver.rtc.open_set_source_clock.enabled"/>
1077+
<property id="config.driver.rtc.open_set_source_clock" value="config.driver.rtc.open_set_source_clock.disabled"/>
10781078
</config>
10791079
<config id="config.driver.spi">
10801080
<property id="config.driver.spi.param_checking_enable" value="config.driver.spi.param_checking_enable.bsp"/>

extras/e2studioProjects/portenta_h33_lib/ra_cfg.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ FSP Configuration
341341
Reception: FIFOs: FIFO 7: Interrupt Threshold: 1/2 full
342342
Reception: FIFOs: FIFO 7: Payload Size: 8 bytes
343343
Reception: FIFOs: FIFO 7: Depth: 16 stages
344-
Reception: Acceptance Filtering: Channel 0 Rule Count: 1
345-
Reception: Acceptance Filtering: Channel 1 Rule Count: 1
344+
Reception: Acceptance Filtering: Channel 0 Rule Count: 2
345+
Reception: Acceptance Filtering: Channel 1 Rule Count: 2
346346

347347
Module "I2C Master (r_iic_master)"
348348
Parameter Checking: Default (BSP)
@@ -408,7 +408,7 @@ FSP Configuration
408408

409409
Module "Flash (r_flash_hp)"
410410
Parameter Checking: Default (BSP)
411-
Code Flash Programming Enable: Disabled
411+
Code Flash Programming Enable: Enabled
412412
Data Flash Programming Enable: Enabled
413413

414414
Module "USB PCDC (r_usb_pcdc)"
@@ -442,7 +442,7 @@ FSP Configuration
442442

443443
Module "Realtime Clock (r_rtc)"
444444
Parameter Checking: Default (BSP)
445-
Set Source Clock in Open: Enabled
445+
Set Source Clock in Open: Disabled
446446

447447
Module "Timer, General PWM (r_gpt)"
448448
Parameter Checking: Default (BSP)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* RTC_Reset
3+
*
4+
* This example sets the RTC with __TIMESTAMP__ macro and can be used to test whether the settings of
5+
* RTC persists after a reset or when VRTC is powered. If VRTC is powered I can disconnect the board
6+
* from the usb cable and the RTC value will persist
7+
*
8+
* Find the full UNO R4 WiFi RTC documentation here:
9+
* https://docs.arduino.cc/tutorials/uno-r4-wifi/rtc
10+
*/
11+
12+
// Include the RTC library
13+
#include "RTC.h"
14+
15+
bool alarmFlag = false;
16+
17+
DayOfWeek convertDayOfWeek(String s)
18+
{
19+
if (s == String("Mon"))
20+
{
21+
return DayOfWeek::MONDAY;
22+
}
23+
if (s == String("Tue"))
24+
{
25+
return DayOfWeek::TUESDAY;
26+
}
27+
if (s == String("Wed"))
28+
{
29+
return DayOfWeek::WEDNESDAY;
30+
}
31+
if (s == String("Thu"))
32+
{
33+
return DayOfWeek::THURSDAY;
34+
}
35+
if (s == String("Fri"))
36+
{
37+
return DayOfWeek::FRIDAY;
38+
}
39+
if (s == String("Sat"))
40+
{
41+
return DayOfWeek::SATURDAY;
42+
}
43+
if (s == String("Sun"))
44+
{
45+
return DayOfWeek::SUNDAY;
46+
}
47+
}
48+
49+
Month convertMonth(String s)
50+
{
51+
if (s == String("Jan"))
52+
{
53+
return Month::JANUARY;
54+
}
55+
if (s == String("Feb"))
56+
{
57+
return Month::FEBRUARY;
58+
}
59+
if (s == String("Mar"))
60+
{
61+
return Month::MARCH;
62+
}
63+
if (s == String("Apr"))
64+
{
65+
return Month::APRIL;
66+
}
67+
if (s == String("May"))
68+
{
69+
return Month::MAY;
70+
}
71+
if (s == String("Jun"))
72+
{
73+
return Month::JUNE;
74+
}
75+
if (s == String("Jul"))
76+
{
77+
return Month::JULY;
78+
}
79+
if (s == String("Aug"))
80+
{
81+
return Month::AUGUST;
82+
}
83+
if (s == String("Sep"))
84+
{
85+
return Month::SEPTEMBER;
86+
}
87+
if (s == String("Oct"))
88+
{
89+
return Month::OCTOBER;
90+
}
91+
if (s == String("Nov"))
92+
{
93+
return Month::NOVEMBER;
94+
}
95+
if (s == String("Dec"))
96+
{
97+
return Month::DECEMBER;
98+
}
99+
}
100+
101+
RTCTime currentRTCTime()
102+
{
103+
// Get a compilation timestamp of the format: Wed May 10 08:54:31 2023
104+
// __TIMESTAMP__ is a GNU C extension macro
105+
// We can't use the standard macros __DATE__ and __TIME__ because they don't provide the day of the week
106+
String timeStamp = __TIMESTAMP__;
107+
// Extract the day of the week
108+
int pos1 = timeStamp.indexOf(" ");
109+
DayOfWeek dayOfWeek = convertDayOfWeek(timeStamp.substring(0, pos1));
110+
// Extract the month
111+
++pos1;
112+
int pos2 = timeStamp.indexOf(" ", pos1);
113+
Month month = convertMonth(timeStamp.substring(pos1, pos2));
114+
// Extract the day
115+
pos1 = ++pos2;
116+
pos2 = timeStamp.indexOf(" ", pos1);
117+
int day = timeStamp.substring(pos1, pos2).toInt();
118+
// Extract the hour
119+
pos1 = ++pos2;
120+
pos2 = timeStamp.indexOf(":", pos1);
121+
int hour = timeStamp.substring(pos1, pos2).toInt();
122+
// Extract the minute
123+
pos1 = ++pos2;
124+
pos2 = timeStamp.indexOf(":", pos1);
125+
int minute = timeStamp.substring(pos1, pos2).toInt();
126+
// Extract the second
127+
pos1 = ++pos2;
128+
pos2 = timeStamp.indexOf(" ", pos1);
129+
int second = timeStamp.substring(pos1, pos2).toInt();
130+
// Extract the year
131+
pos1 = ++pos2;
132+
pos2 = timeStamp.indexOf(" ", pos1);
133+
int year = timeStamp.substring(pos1, pos2).toInt();
134+
135+
return RTCTime(day, month, year, hour, minute, second, dayOfWeek, SaveLight::SAVING_TIME_INACTIVE);
136+
}
137+
138+
void setup()
139+
{
140+
Serial.begin(9600);
141+
while (!Serial) ;
142+
143+
// Initialize the RTC
144+
RTC.begin();
145+
146+
// if the RTC is not running set its value to the compile time __TIMESTAMP__ macro
147+
if(!RTC.isRunning()) {
148+
RTCTime timeToSet = currentRTCTime();
149+
RTC.setTime(timeToSet);
150+
}
151+
}
152+
153+
void loop()
154+
{
155+
Serial.println("The RTC time is: ");
156+
RTCTime currentTime;
157+
RTC.getTime(currentTime);
158+
Serial.print(currentTime.getYear());
159+
Serial.print("-");
160+
Serial.print(Month2int(currentTime.getMonth()));
161+
Serial.print("-");
162+
Serial.print(currentTime.getDayOfMonth());
163+
Serial.print(" ");
164+
Serial.print(currentTime.getHour());
165+
Serial.print(":");
166+
Serial.print(currentTime.getMinutes());
167+
Serial.print(":");
168+
Serial.println(currentTime.getSeconds());
169+
170+
delay(1000);
171+
}

libraries/RTC/examples/Test_RTC/Test_RTC.ino

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
Test RTC
33
4-
A test sketch showcasing all RTC showcasing various functionalities related to the RTC module,
4+
A test sketch showcasing all RTC functionalities related to the RTC module,
55
including setting the time, handling interrupts, and reading time values.
66
77
Find the full UNO R4 WiFi RTC documentation here:
@@ -45,16 +45,10 @@ void setup() {
4545
// Set a specific initial time (August 25, 2022, 14:37:00 Thursday)
4646
RTCTime mytime(25, Month::AUGUST, 2022, 14, 37, 00, DayOfWeek::THURSDAY, SaveLight::SAVING_TIME_ACTIVE);
4747

48-
RTCTime savedTime;
49-
RTC.getTime(savedTime);
50-
51-
// Set the initial time if RTC is not running
48+
// Set the initial time if RTC is not running.
49+
// The RTC may be still running if the board was reset, or if VRTC pin was powered
5250
if (!RTC.isRunning()) {
53-
if (savedTime.getYear() != 2000) {
54-
RTC.setTime(mytime);
55-
} else {
56-
RTC.setTime(savedTime);
57-
}
51+
RTC.setTime(mytime);
5852
}
5953

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

148142
status = !status;
149143
delay(1000);
150-
}
144+
}

0 commit comments

Comments
 (0)