Skip to content

Commit 3e2aed5

Browse files
authored
Merge pull request #125 from facchinm/rtc_c33_fix
C33: fix RTC functionality
2 parents 4c30dc1 + aec195f commit 3e2aed5

File tree

3 files changed

+46
-36
lines changed

3 files changed

+46
-36
lines changed

Diff for: libraries/RTC/examples/Test_RTC/Test_RTC.ino

+33-27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
Test RTC
3-
4-
A test sketch showcasing all RTC showcasing various functionalities related to the RTC module,
3+
4+
A test sketch showcasing all RTC showcasing various 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:
@@ -19,15 +19,15 @@ void periodic_cbk() {
1919
static bool clb_st = false;
2020

2121
// Toggle the LED based on callback state
22-
if(clb_st) {
23-
digitalWrite(LED_ON_INTERRUPT,HIGH);
22+
if (clb_st) {
23+
digitalWrite(LED_ON_INTERRUPT, HIGH);
2424
}
2525
else {
26-
digitalWrite(LED_ON_INTERRUPT,LOW);
26+
digitalWrite(LED_ON_INTERRUPT, LOW);
2727
}
2828

2929
clb_st = !clb_st; // Toggle callback state
30-
30+
3131
// Print message indicating periodic interrupt
3232
Serial.println("PERIODIC INTERRUPT");
3333
}
@@ -41,23 +41,30 @@ void setup() {
4141
// Initialize serial communication
4242
Serial.begin(9600);
4343
// Wait for serial connection
44-
while(!Serial) {
45-
44+
while (!Serial) {
45+
4646
}
47-
47+
4848
// Set LED pins as outputs
4949
pinMode(LED_BUILTIN, OUTPUT);
5050
pinMode(LED_ON_INTERRUPT, OUTPUT);
51-
51+
5252
// Initialize the RTC
5353
RTC.begin();
5454

5555
// Set a specific initial time (August 25, 2022, 14:37:00 Thursday)
5656
RTCTime mytime(25, Month::AUGUST, 2022, 14, 37, 00, DayOfWeek::THURSDAY, SaveLight::SAVING_TIME_ACTIVE);
5757

58+
RTCTime savedTime;
59+
RTC.getTime(savedTime);
60+
5861
// Set the initial time if RTC is not running
59-
if(!RTC.isRunning()) {
60-
RTC.setTime(mytime);
62+
if (!RTC.isRunning()) {
63+
if (savedTime.getYear() != 2000) {
64+
RTC.setTime(mytime);
65+
} else {
66+
RTC.setTime(savedTime);
67+
}
6168
}
6269

6370
// Create an alarm time set to 35 seconds
@@ -69,36 +76,35 @@ void setup() {
6976
am.addMatchSecond();
7077

7178
// Set the periodic callback function to run once every 2 seconds
72-
if(!RTC.setPeriodicCallback(periodic_cbk, Period::ONCE_EVERY_2_SEC)) {
79+
if (!RTC.setPeriodicCallback(periodic_cbk, Period::ONCE_EVERY_2_SEC)) {
7380
Serial.println("ERROR: periodic callback not set");
7481
}
75-
82+
7683
// Set the alarm callback function with the alarm time and matching condition
77-
if(!RTC.setAlarmCallback(alarm_cbk, alarmtime, am)) {
84+
if (!RTC.setAlarmCallback(alarm_cbk, alarmtime, am)) {
7885
Serial.println("ERROR: alarm callback not set");
7986
}
80-
8187
}
8288

8389
void loop() {
8490
static bool status = false;
85-
91+
8692
RTCTime currenttime;
87-
93+
8894
// Check if RTC is running and print status
89-
if(status) {
95+
if (status) {
9096

9197
// Toggle LED and display RTC status if 'status' is true
92-
if(RTC.isRunning()) {
98+
if (RTC.isRunning()) {
9399
Serial.println("RTC is running");
94100
}
95101
else {
96102
Serial.println("RTC is not running");
97103
}
98-
104+
99105
/* GET CURRENT TIME FROM RTC */
100106
RTC.getTime(currenttime);
101-
107+
102108
/* PRINT CURRENT TIME on Serial */
103109
Serial.print("Current time: ");
104110
/* DATE */
@@ -108,20 +114,20 @@ void loop() {
108114
Serial.print("/");
109115
Serial.print(currenttime.getYear());
110116
Serial.print(" - ");
111-
117+
112118
/* ORE:MINUTI:SECONDI */
113119
Serial.print(currenttime.getHour());
114120
Serial.print(":");
115121
Serial.print(currenttime.getMinutes());
116122
Serial.print(":");
117123
Serial.println(currenttime.getSeconds());
118-
119-
digitalWrite(LED_BUILTIN, HIGH);
124+
125+
digitalWrite(LED_BUILTIN, HIGH);
120126
}
121127
else {
122-
digitalWrite(LED_BUILTIN, LOW);
128+
digitalWrite(LED_BUILTIN, LOW);
123129
}
124130

125131
status = !status;
126132
delay(1000);
127-
}
133+
}

Diff for: libraries/RTC/src/RTC.cpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -437,18 +437,21 @@ void __attribute__((weak)) rtc_callback(rtc_callback_args_t *p_args) {
437437
}
438438
}
439439

440-
rtc_instance_ctrl_t rtc_ctrl;
440+
rtc_instance_ctrl_t rtc_ctrl = {
441+
.open = 0,
442+
};
443+
444+
#ifndef RTC_CLOCK_SOURCE
445+
#define RTC_CLOCK_SOURCE RTC_CLOCK_SOURCE_LOCO
446+
#endif
447+
441448
const rtc_error_adjustment_cfg_t rtc_err_cfg = {
442449
.adjustment_mode = RTC_ERROR_ADJUSTMENT_MODE_AUTOMATIC,
443450
.adjustment_period = RTC_ERROR_ADJUSTMENT_PERIOD_10_SECOND,
444451
.adjustment_type = RTC_ERROR_ADJUSTMENT_NONE,
445452
.adjustment_value = 0, };
446453
rtc_cfg_t rtc_cfg = {
447-
// TODO: change me to RTC_CLOCK_SOURCE_SUBCLK when capacitors are mounted
448-
// https://arduino.atlassian.net/browse/HWH33-204
449-
// Fixes counting time in VBAT mode on H33
450-
// Leave as is for Santiago
451-
.clock_source = RTC_CLOCK_SOURCE_LOCO,
454+
.clock_source = RTC_CLOCK_SOURCE,
452455
.freq_compare_value_loco = 255,
453456
.p_err_cfg = &rtc_err_cfg,
454457
.alarm_ipl = (12),
@@ -458,8 +461,7 @@ rtc_cfg_t rtc_cfg = {
458461
.carry_ipl = (12),
459462
.carry_irq = FSP_INVALID_VECTOR,
460463
.p_callback = rtc_callback,
461-
.p_context = NULL,
462-
464+
.p_context = NULL,
463465
};
464466

465467
#ifdef __cplusplus

Diff for: variants/PORTENTA_C33/pins_arduino.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,6 @@ static const uint8_t SS = PIN_SPI_CS;
204204
#define LORA_RESET 2 //PWM2
205205
#define LORA_BOOT0 3 //PWM3
206206
#define LORA_IRQ_DUMB 4 //PWM4
207-
#define SerialLoRa Serial3
207+
#define SerialLoRa Serial3
208+
209+
#define RTC_CLOCK_SOURCE RTC_CLOCK_SOURCE_SUBCLK

0 commit comments

Comments
 (0)