Skip to content

C33: fix RTC functionality #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 33 additions & 27 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 showcasing various 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 All @@ -19,15 +19,15 @@ void periodic_cbk() {
static bool clb_st = false;

// Toggle the LED based on callback state
if(clb_st) {
digitalWrite(LED_ON_INTERRUPT,HIGH);
if (clb_st) {
digitalWrite(LED_ON_INTERRUPT, HIGH);
}
else {
digitalWrite(LED_ON_INTERRUPT,LOW);
digitalWrite(LED_ON_INTERRUPT, LOW);
}

clb_st = !clb_st; // Toggle callback state

// Print message indicating periodic interrupt
Serial.println("PERIODIC INTERRUPT");
}
Expand All @@ -41,23 +41,30 @@ void setup() {
// Initialize serial communication
Serial.begin(9600);
// Wait for serial connection
while(!Serial) {
while (!Serial) {

}

// Set LED pins as outputs
pinMode(LED_BUILTIN, OUTPUT);
pinMode(LED_ON_INTERRUPT, OUTPUT);

// Initialize the RTC
RTC.begin();

// 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
if(!RTC.isRunning()) {
RTC.setTime(mytime);
if (!RTC.isRunning()) {
if (savedTime.getYear() != 2000) {
RTC.setTime(mytime);
} else {
RTC.setTime(savedTime);
}
}

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

// Set the periodic callback function to run once every 2 seconds
if(!RTC.setPeriodicCallback(periodic_cbk, Period::ONCE_EVERY_2_SEC)) {
if (!RTC.setPeriodicCallback(periodic_cbk, Period::ONCE_EVERY_2_SEC)) {
Serial.println("ERROR: periodic callback not set");
}

// Set the alarm callback function with the alarm time and matching condition
if(!RTC.setAlarmCallback(alarm_cbk, alarmtime, am)) {
if (!RTC.setAlarmCallback(alarm_cbk, alarmtime, am)) {
Serial.println("ERROR: alarm callback not set");
}

}

void loop() {
static bool status = false;

RTCTime currenttime;

// Check if RTC is running and print status
if(status) {
if (status) {

// Toggle LED and display RTC status if 'status' is true
if(RTC.isRunning()) {
if (RTC.isRunning()) {
Serial.println("RTC is running");
}
else {
Serial.println("RTC is not running");
}

/* GET CURRENT TIME FROM RTC */
RTC.getTime(currenttime);

/* PRINT CURRENT TIME on Serial */
Serial.print("Current time: ");
/* DATE */
Expand All @@ -108,20 +114,20 @@ void loop() {
Serial.print("/");
Serial.print(currenttime.getYear());
Serial.print(" - ");

/* ORE:MINUTI:SECONDI */
Serial.print(currenttime.getHour());
Serial.print(":");
Serial.print(currenttime.getMinutes());
Serial.print(":");
Serial.println(currenttime.getSeconds());
digitalWrite(LED_BUILTIN, HIGH);

digitalWrite(LED_BUILTIN, HIGH);
}
else {
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(LED_BUILTIN, LOW);
}

status = !status;
delay(1000);
}
}
18 changes: 10 additions & 8 deletions libraries/RTC/src/RTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,18 +437,21 @@ void __attribute__((weak)) rtc_callback(rtc_callback_args_t *p_args) {
}
}

rtc_instance_ctrl_t rtc_ctrl;
rtc_instance_ctrl_t rtc_ctrl = {
.open = 0,
};

#ifndef RTC_CLOCK_SOURCE
#define RTC_CLOCK_SOURCE RTC_CLOCK_SOURCE_LOCO
#endif

const rtc_error_adjustment_cfg_t rtc_err_cfg = {
.adjustment_mode = RTC_ERROR_ADJUSTMENT_MODE_AUTOMATIC,
.adjustment_period = RTC_ERROR_ADJUSTMENT_PERIOD_10_SECOND,
.adjustment_type = RTC_ERROR_ADJUSTMENT_NONE,
.adjustment_value = 0, };
rtc_cfg_t rtc_cfg = {
// TODO: change me to RTC_CLOCK_SOURCE_SUBCLK when capacitors are mounted
// https://arduino.atlassian.net/browse/HWH33-204
// Fixes counting time in VBAT mode on H33
// Leave as is for Santiago
.clock_source = RTC_CLOCK_SOURCE_LOCO,
.clock_source = RTC_CLOCK_SOURCE,
.freq_compare_value_loco = 255,
.p_err_cfg = &rtc_err_cfg,
.alarm_ipl = (12),
Expand All @@ -458,8 +461,7 @@ rtc_cfg_t rtc_cfg = {
.carry_ipl = (12),
.carry_irq = FSP_INVALID_VECTOR,
.p_callback = rtc_callback,
.p_context = NULL,

.p_context = NULL,
};

#ifdef __cplusplus
Expand Down
4 changes: 3 additions & 1 deletion variants/PORTENTA_C33/pins_arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,6 @@ static const uint8_t SS = PIN_SPI_CS;
#define LORA_RESET 2 //PWM2
#define LORA_BOOT0 3 //PWM3
#define LORA_IRQ_DUMB 4 //PWM4
#define SerialLoRa Serial3
#define SerialLoRa Serial3

#define RTC_CLOCK_SOURCE RTC_CLOCK_SOURCE_SUBCLK