forked fromzephyrproject-rtos/arduino-core-zephyr
Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34
New RTC library implementation#231
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
bogdanarduino wants to merge1 commit intoarduino:mainChoose a base branch frombogdanarduino:zephyr_rtc
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+860 −2
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
491 changes: 491 additions & 0 deletionslibraries/RTC/RTC.cpp
Large diffs are not rendered by default.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
87 changes: 87 additions & 0 deletionslibraries/RTC/RTC.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // RTC.h | ||
| #pragma once | ||
| #include<zephyr/kernel.h> | ||
| #include<zephyr/device.h> | ||
| #include<zephyr/drivers/counter.h> | ||
| #include<zephyr/drivers/rtc.h> | ||
| #include<time.h> | ||
| // Alarm callback types | ||
| typedefvoid (*RTCAlarmCallback)(void *user_data); | ||
| typedefvoid (*RTCUpdateCallback)(void *user_data); | ||
| classArduinoRTC { | ||
| public: | ||
| ArduinoRTC(); | ||
| boolbegin(); | ||
| ~ArduinoRTC(); | ||
| /* setters*/ | ||
| boolsetDayOfMonth(int day); | ||
| boolsetMonthOfYear(int m); | ||
| boolsetYear(int year); | ||
| boolsetHour(int hour); | ||
| boolsetMinute(int minute); | ||
| boolsetSecond(int second); | ||
| /* Getters*/ | ||
| intgetDayOfMonth(); | ||
| intgetMonth(); | ||
| intgetYear(); | ||
| intgetHour(); | ||
| intgetMinutes(); | ||
| intgetSeconds(); | ||
| intsetTime(int year,int month,int day,int hour,int minute,int second); | ||
| intgetTime(int &year,int &month,int &day,int &hour,int &minute,int &second); | ||
| #if defined(ARDUINO_GIGA) || defined(ARDUINO_PORTENTA_H7) || defined(ARDUINO_OPTA) | ||
| intsetAlarm(int year,int month,int day,int hour,int minute,int second, | ||
| RTCAlarmCallback cb =nullptr,void *user_data =nullptr); | ||
| intcancelAlarm(); | ||
| #elif defined(ARDUINO_NANO33BLE) || defined(ARDUINO_NICLA_SENSE_ME) | ||
| intsetAlarm(int year,int month,int day,int hour,int minute,int second, | ||
| void (*callback)(conststructdevice *dev,uint8_t chan_id,uint32_t ticks,void *user_data), | ||
| void *cb_user_data); | ||
| voidcancelAlarm(); | ||
| #endif | ||
| #if defined(ARDUINO_GIGA) || defined(ARDUINO_PORTENTA_H7) || defined(ARDUINO_OPTA) | ||
| // Optional APIs (only if supported) | ||
| intgetAlarm(int &year,int &month,int &day,int &hour,int &minute,int &second); | ||
| boolisAlarmPending(); | ||
| intsetUpdateCallback(RTCUpdateCallback cb,void *user_data); | ||
| intsetCalibration(int32_t calibration); | ||
| intgetCalibration(int32_t &calibration); | ||
| #endif | ||
| private: | ||
| #if defined(ARDUINO_GIGA) || defined(ARDUINO_PORTENTA_H7) || defined(ARDUINO_OPTA) | ||
| conststructdevice *rtc_dev; | ||
| staticvoidalarmCallbackWrapper(conststructdevice *dev,uint16_t id,void *user_data); | ||
| staticvoidupdateCallbackWrapper(conststructdevice *dev,void *user_data); | ||
| RTCAlarmCallback userAlarmCallback =nullptr; | ||
| void *userAlarmCallbackData =nullptr; | ||
| RTCUpdateCallback userUpdateCallback =nullptr; | ||
| void *userUpdateCallbackData =nullptr; | ||
| uint16_t alarmId =0;// default to alarm ID 0 | ||
| #elif defined(ARDUINO_NANO33BLE) || defined(ARDUINO_NICLA_SENSE_ME) | ||
| conststructdevice *counter_dev; | ||
| time_t timeOffset; | ||
| // Alarm members | ||
| structcounter_alarm_cfg alarm_cfg; | ||
| void (*user_callback)(conststructdevice *dev,uint8_t chan_id,uint32_t ticks,void *user_data); | ||
| void *user_data; | ||
| staticvoidalarmHandler(conststructdevice *dev,uint8_t chan_id,uint32_t ticks,void *user_data); | ||
| time_tdatetimeToEpoch(int year,int month,int day,int hour,int minute,int second); | ||
| voidepochToDatetime(time_t t,int &year,int &month,int &day,int &hour,int &minute,int &second); | ||
| #endif | ||
| }; |
76 changes: 76 additions & 0 deletionslibraries/RTC/examples/AlarmRTC/AlarmRTC.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * This sketch sets an alarm 10 seconds in the future and handles it via a callback. | ||
| */ | ||
| #include "RTC.h" | ||
| #include <stdio.h> | ||
| ArduinoRTC rtc; | ||
| char printBuffer[30]; // declare a buffer of large enough size for the message we want to display | ||
| int year, month, day, hour, minute, second; | ||
| #if defined(ARDUINO_NICLA_SENSE_ME) || defined(ARDUINO_NANO33BLE) | ||
| void onAlarm(const struct device *dev, uint8_t chan_id, uint32_t ticks, void *user_data) { | ||
| char printBuffer[40]; | ||
| // Assuming user_data is a string or message you want to print | ||
| sprintf(printBuffer, "Alarm went off! Message: %s\n", (char *)user_data); | ||
| Serial.println(printBuffer); | ||
| } | ||
| #elif | ||
| void onAlarm(void *user_data) { | ||
| char printBuffer[40]; | ||
| sprintf(printBuffer, "Alarm went off! Message: %s\n", (char *)user_data); | ||
| Serial.println(printBuffer); | ||
| } | ||
| #endif | ||
| void setup() { | ||
| int ret = 0xDEADBEEFu; // Starting with a custom value for the return which will definitely lead to failure if not changed to zero (i.e. success) by the functions below | ||
| char printBuffer[60]; | ||
| Serial.begin(115200); | ||
| delay(1000); | ||
| if (!rtc.begin()) { | ||
| Serial.println("RTC not ready\n"); | ||
| return; | ||
| } | ||
| int year, month, day, hour, minute, second; | ||
| ret = rtc.getTime(year, month, day, hour, minute, second); | ||
| if(ret != 0) | ||
| { | ||
| rtc.setTime(2025, 10, 21, 12, 0, 0); | ||
| } | ||
| sprintf(printBuffer, "Current Time: %04d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second); | ||
| Serial.println(printBuffer); | ||
| // Set alarm 10 seconds into the future | ||
| second += 5; | ||
| // Correct for minute rollover if necessary | ||
| if (second >= 60) { | ||
| second -= 60; | ||
| minute += 1; | ||
| } | ||
| // The method also allows for registering a function callback which will be called when the alarm sets off and a display message which will be shown in the console | ||
| ret = rtc.setAlarm(year, month, day, hour, minute, second, onAlarm, (void *)"Wake up!!!"); | ||
| if (ret == 0) { | ||
| sprintf(printBuffer, "Alarm set for: %02d:%02d:%02d\n", hour, minute, second); | ||
| Serial.println(printBuffer); | ||
| } else { | ||
| sprintf(printBuffer, "Failed to set alarm (%d)\n", ret); | ||
| Serial.println(printBuffer); | ||
| } | ||
| } | ||
| void loop() { | ||
| char printBuffer[30]; // declare a buffer of large enough size for the message we want to display | ||
| int y, m, d, h, min, s; | ||
| int status = rtc.getTime(y, m, d, h, min, s); | ||
| // Because the print() and println() functions do not support formatted output directly, we can use this trick to prepare a buffer with the string we want to show | ||
| sprintf(printBuffer, "Time is: %04d-%02d-%02d %02d:%02d:%02d", y, m, d, h, min, s); | ||
| Serial.println(printBuffer); | ||
| delay(1000); | ||
| } |
40 changes: 40 additions & 0 deletionslibraries/RTC/examples/CalibrationRTC/CalibrationRTC.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #include "RTC.h" | ||
| #include <stdio.h> | ||
| // Doesn't work on the Opta for some reason. | ||
| ArduinoRTC rtc; | ||
| void setup() { | ||
| char printBuffer[30]; | ||
| delay(1000); | ||
| Serial.begin(115200); | ||
| if (!rtc.begin()) { | ||
| printf("RTC not ready\n"); | ||
| return; | ||
| } | ||
| int calib = 0; | ||
| if (rtc.getCalibration(calib) == 0) { | ||
| sprintf(printBuffer, "Current calibration: %d\n", calib); | ||
| Serial.println(printBuffer); | ||
| } else { | ||
| Serial.println("Failed to get calibration"); | ||
| } | ||
| // Apply a small positive calibration (e.g., +1) | ||
| // This value is hardware-dependent, in a real application you should check the microcontroller's datasheet for the correct amount. | ||
| int32_t new_calib = calib + 1; | ||
| if (rtc.setCalibration(new_calib) == 0) { | ||
| sprintf(printBuffer, "Calibration updated to: %d\n", new_calib); | ||
| Serial.println(printBuffer); | ||
| } else { | ||
| Serial.println("Failed to set calibration"); | ||
| } | ||
| } | ||
| void loop() { | ||
| delay(5000); | ||
| } |
72 changes: 72 additions & 0 deletionslibraries/RTC/examples/EnhancedRTC/EnhancedRTC.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #include "RTC.h" | ||
| ArduinoRTC rtc; | ||
| // 7-segment style representation for digits 0–9 and colon ":" | ||
| const char* bigDigits[11][3] = { | ||
| {" _ ", "| |", "|_|"}, // 0 | ||
| {" ", " |", " |"}, // 1 | ||
| {" _ ", " _|", "|_ "}, // 2 | ||
| {" _ ", " _|", " _|"}, // 3 | ||
| {" ", "|_|", " |"}, // 4 | ||
| {" _ ", "|_ ", " _|"}, // 5 | ||
| {" _ ", "|_ ", "|_|"}, // 6 | ||
| {" _ ", " |", " |"}, // 7 | ||
| {" _ ", "|_|", "|_|"}, // 8 | ||
| {" _ ", "|_|", " _|"}, // 9 | ||
| {" ", " . ", " . "} // colon ":" | ||
| }; | ||
| void printBigTime(int h, int m, int s) { | ||
| // Format time as HH:MM:SS string | ||
| char timeStr[9]; | ||
| memset(timeStr, 0, sizeof(timeStr)); | ||
| char bigDigitsPrint[40]; | ||
| memset(bigDigitsPrint, 0, sizeof(bigDigitsPrint)); | ||
| snprintf(timeStr, sizeof(timeStr), "%02d:%02d:%02d", h, m, s); // This (and further such manipulations) is necessary because Serial.print() does not support formatted output like printf() | ||
| Serial.println(timeStr); | ||
| // Print each of the 3 lines row by row | ||
| for (int row = 0; row < 3; row++) { | ||
| for (int i = 0; timeStr[i] != '\0'; i++) { | ||
| char c = timeStr[i]; | ||
| if (c >= '0' && c <= '9') { | ||
| sprintf(bigDigitsPrint, "%s ", bigDigits[c - '0'][row]); | ||
| Serial.print(bigDigitsPrint); | ||
| } else if (c == ':') { | ||
| sprintf(bigDigitsPrint, "%s ", bigDigits[10][row]); | ||
| Serial.print(bigDigitsPrint); | ||
| } else { | ||
| Serial.print(" "); // Space or unknown | ||
| } | ||
| } | ||
| Serial.println(); | ||
| } | ||
| Serial.println(); | ||
| } | ||
| void setup() { | ||
| Serial.begin(115200); | ||
| rtc.begin(); | ||
| bool status = rtc.setTime(2025, 9, 25, 7, 46, 0); // Initial time | ||
| } | ||
| void loop() { | ||
| int y, m, d, h, min, s; | ||
| char printBuffer[60]; | ||
| int status = rtc.getTime(y, m, d, h, min, s); | ||
| // Clear screen (optional line, works on many terminals, tested on Tera Term. Does not take effect in Arduino IDE console unfortunately) | ||
| Serial.println("\033[2J\033[H"); | ||
| // Print date and time in plain format | ||
| sprintf(printBuffer, "Date: %04d-%02d-%02d\n", y, m, d); | ||
| Serial.println(printBuffer); | ||
| Serial.println("Time:"); | ||
| // Print time in big digits | ||
| printBigTime(h, min, s); | ||
| delay(1000); | ||
| } |
23 changes: 23 additions & 0 deletionslibraries/RTC/examples/SimpleRTC/SimpleRTC.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #include "RTC.h" | ||
| ArduinoRTC rtc; | ||
| int year, month, day, hour, minute, second; | ||
| int previousSecond = 0; | ||
| char printBuffer[30]; // Allocate large enough buffer to hold the 28 characters of the output format "Time is: 2025-09-25 11:49:26" | ||
| void setup() { | ||
| Serial.begin(115200); | ||
| if (!rtc.begin()) { | ||
| Serial.println("RTC not ready\n"); | ||
| return; | ||
| } | ||
| rtc.setTime(2025, 10, 21, 12, 0, 0); | ||
| } | ||
| void loop() { | ||
| rtc.getTime(year, month, day, hour, minute, second); // Read back time from hardware | ||
| sprintf(printBuffer, "Time is: %04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second); | ||
| Serial.println(printBuffer); | ||
| delay(1000); | ||
| } |
6 changes: 6 additions & 0 deletionsvariants/arduino_giga_r1_stm32h747xx_m7/arduino_giga_r1_stm32h747xx_m7.conf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletionsvariants/arduino_giga_r1_stm32h747xx_m7/arduino_giga_r1_stm32h747xx_m7.overlay
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionsvariants/arduino_nano_33_ble_nrf52840_sense/arduino_nano_33_ble_nrf52840_sense.conf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletionsvariants/arduino_nano_33_ble_nrf52840_sense/arduino_nano_33_ble_nrf52840_sense.overlay
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletionsvariants/arduino_nano_33_ble_nrf52840_sense/arduino_nano_33_ble_sense.overlay
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletionvariants/arduino_nicla_sense_me_nrf52832/arduino_nicla_sense_me_nrf52832.conf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
8 changes: 7 additions & 1 deletionvariants/arduino_nicla_sense_me_nrf52832/arduino_nicla_sense_me_nrf52832.overlay
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.