From bae403f37c15be3aeac0b9d4b1b88f5e7fd62f5b Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Thu, 7 Sep 2023 14:48:17 +0200 Subject: [PATCH 1/2] C33: use 32Khz crystal as RTC source --- libraries/RTC/src/RTC.cpp | 18 ++++++++++-------- variants/PORTENTA_C33/pins_arduino.h | 4 +++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/libraries/RTC/src/RTC.cpp b/libraries/RTC/src/RTC.cpp index 94a80bb52..26d50ba94 100644 --- a/libraries/RTC/src/RTC.cpp +++ b/libraries/RTC/src/RTC.cpp @@ -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), @@ -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 diff --git a/variants/PORTENTA_C33/pins_arduino.h b/variants/PORTENTA_C33/pins_arduino.h index 43112b7a4..e7eecf25f 100644 --- a/variants/PORTENTA_C33/pins_arduino.h +++ b/variants/PORTENTA_C33/pins_arduino.h @@ -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 \ No newline at end of file +#define SerialLoRa Serial3 + +#define RTC_CLOCK_SOURCE RTC_CLOCK_SOURCE_SUBCLK From aec195fbc99cda141fbdfcb1bc36864cc53b3db6 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Thu, 7 Sep 2023 14:49:20 +0200 Subject: [PATCH 2/2] rtc: examples: keep the date if board is restarted when powering via VRTC only --- libraries/RTC/examples/Test_RTC/Test_RTC.ino | 60 +++++++++++--------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/libraries/RTC/examples/Test_RTC/Test_RTC.ino b/libraries/RTC/examples/Test_RTC/Test_RTC.ino index 67dd08cf2..4fccdc68c 100644 --- a/libraries/RTC/examples/Test_RTC/Test_RTC.ino +++ b/libraries/RTC/examples/Test_RTC/Test_RTC.ino @@ -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: @@ -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"); } @@ -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 @@ -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 */ @@ -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); -} +} \ No newline at end of file