Skip to content
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# STM32RTC
# STM32RTCMbed
A RTC library for STM32.

> [!IMPORTANT]
> This library is a fork of [STM32RTC](https://github.com/stm32duino/STM32RTC) based on [ArduinoCore-mbed](https://github.com/arduino/ArduinoCore-mbed) instead of [Arduino_Core_STM32](https://github.com/stm32duino/Arduino_Core_STM32). Therefore, currently only `STM32H747xx` is supported by this library.

## Requirement
* [Arduino_Core_STM32](https://github.com/stm32duino/Arduino_Core_STM32) version >= 1.3.0
* [ArduinoCore-mbed](https://github.com/arduino/ArduinoCore-mbed) version >= 4.4.1

# API

Expand Down Expand Up @@ -109,7 +112,7 @@ _One-Second interrupt_
- It is not available on some stm32F0 devices.

* **new API:**
* **`void attachSecondsInterrupt(voidFuncPtr callback)`**
* **`void attachSecondsInterrupt(isrFuncPtr callback)`**
* **`void detachSecondsInterrupt(void)`**


Expand Down Expand Up @@ -184,7 +187,7 @@ _SubSeconds underflow_
Only dor STM32WLxx. Manage interrupt (SSRU) when SubSeconds register
underflow. Used by STM32LoRaWAN.

* **`void attachSubSecondsUnderflowInterrupt(voidFuncPtr callback);`**
* **`void attachSubSecondsUnderflowInterrupt(isrFuncPtr callback);`**
* **`void detachSubSecondsUnderflowInterrupt(void);`**

Refer to the Arduino RTC documentation for the other functions
Expand Down
7 changes: 5 additions & 2 deletions examples/Epoch/Epoch.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
https://github.com/stm32duino/STM32RTC
*/

#include <STM32RTC.h>
#include <STM32RTCMbed.h>

/* Get the rtc object */
STM32RTC& rtc = STM32RTC::getInstance();

void setup() {
Serial.begin(9600);
Serial.begin(115200);
while (!Serial) ;

delay(2000);

// Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK.
// By default the LSI is selected as source.
Expand Down
32 changes: 27 additions & 5 deletions examples/F1RTCDateRetention/F1RTCDateRetention.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
https://github.com/stm32duino/STM32RTC
*/

#include <STM32RTC.h>
#include <STM32RTCMbed.h>

/* Get the rtc object */
STM32RTC& rtc = STM32RTC::getInstance();
Expand Down Expand Up @@ -46,6 +46,9 @@ byte year;
void setup()
{
Serial.begin(115200);
while (!Serial) ;

delay(2000);

// Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK.
// By default the LSI is selected as source.
Expand All @@ -58,9 +61,19 @@ void setup()
// get the date if stored in BackUp reg
rtc.getDate(&weekDay, &day, &month, &year);
if (reset_bkup) {
Serial.printf("reset the date to %02d/%02d/%02d\n", day, month, year);
Serial.print("reset the date to ");
Serial.print(day);
Serial.print("/");
Serial.print(month);
Serial.print("/");
Serial.println(year);
} else {
Serial.printf("date from the BackUp %02d/%02d/%02d\n", day, month, year);
Serial.print("date from the BackUp ");
Serial.print(day);
Serial.print("/");
Serial.print(month);
Serial.print("/");
Serial.println(year);
}

// Check if date is valid = read in the backUp reg.
Expand Down Expand Up @@ -89,7 +102,12 @@ if ((day == 1) && (month == RTC_MONTH_JANUARY) && (year == 1)) {
void loop()
{
// Print date...
Serial.printf("%02d/%02d/%02d \t", rtc.getDay(), rtc.getMonth(), rtc.getYear());
Serial.print(rtc.getDay());
Serial.print("/");
Serial.print(rtc.getMonth());
Serial.print("/");
Serial.print(rtc.getYear());
Serial.print(" ");

uint8_t sec = 0;
uint8_t mn = 0;
Expand All @@ -98,7 +116,11 @@ void loop()
rtc.getTime(&hrs, &mn, &sec, &subs);

// ...and time
Serial.printf("%02d:%02d:%02d\n", hrs, mn, sec);
Serial.print(hrs);
Serial.print(":");
Serial.print(mn);
Serial.print(":");
Serial.println(sec);

delay(1000);
}
24 changes: 19 additions & 5 deletions examples/RTCClockSelection/RTCClockSelection.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
https://github.com/stm32duino/STM32RTC
*/

#include <STM32RTC.h>
#include <STM32RTCMbed.h>

/* Get the rtc object */
STM32RTC& rtc = STM32RTC::getInstance();
Expand All @@ -37,7 +37,10 @@ const byte year = 15;

void setup()
{
Serial.begin(9600);
Serial.begin(115200);
while (!Serial) ;

delay(2000);

// Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK.
// By default the LSI is selected as source.
Expand All @@ -64,10 +67,21 @@ void setup()
void loop()
{
// Print date...
Serial.printf("%02d/%02d/%02d ", rtc.getDay(), rtc.getMonth(), rtc.getYear());
Serial.print(rtc.getDay());
Serial.print("/");
Serial.print(rtc.getMonth());
Serial.print("/");
Serial.print(rtc.getYear());
Serial.print(" ");

// ...and time
Serial.printf("%02d:%02d:%02d.%03d\n", rtc.getHours(), rtc.getMinutes(), rtc.getSeconds(), rtc.getSubSeconds());
Serial.print(rtc.getHours());
Serial.print(":");
Serial.print(rtc.getMinutes());
Serial.print(":");
Serial.print(rtc.getSeconds());
Serial.print(".");
Serial.println(rtc.getSubSeconds());

delay(1000);
}
}
87 changes: 59 additions & 28 deletions examples/RTCReset/RTCReset.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
https://github.com/stm32duino/STM32RTC

*/
#include <STM32RTC.h>
#include <STM32RTCMbed.h>

/* Get the rtc object */
STM32RTC& rtc = STM32RTC::getInstance();
Expand Down Expand Up @@ -52,10 +52,6 @@ static byte year = 0;
static STM32RTC::Hour_Format hourFormat = STM32RTC::HOUR_24;
static STM32RTC::AM_PM period = STM32RTC::AM;

#ifndef USER_BTN
#define USER_BTN PA0
#endif

static uint8_t conv2d(const char* p) {
uint8_t v = 0;
if ('0' <= *p && *p <= '9')
Expand All @@ -65,7 +61,10 @@ static uint8_t conv2d(const char* p) {

// sample input: date = "Dec 26 2009", time = "12:34:56"
void initDateTime(void) {
Serial.printf("Build date & time %s, %s\n", mydate, mytime);
Serial.print("Build date & time ");
Serial.print(mydate);
Serial.print(", ");
Serial.println(mytime);

year = conv2d(mydate + 9);
// Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Expand All @@ -90,14 +89,11 @@ void initDateTime(void) {
}

void setup() {
pinMode(USER_BTN, INPUT_PULLUP);
int32_t default_state = digitalRead(USER_BTN);
Serial.begin(115200);
while (!Serial)
;
// Wait user input to start
while (digitalRead(USER_BTN) == default_state)
;
while (!Serial) ;

delay(2000);

// Convenient function to init date and time variables
initDateTime();

Expand All @@ -112,21 +108,22 @@ void setup() {
#endif
rtc.begin(); // Initialize RTC 24H format
if (!rtc.isTimeSet()) {
Serial.printf("RTC time not set\n Set it.\n");
Serial.print("RTC time not set\n Set it.\n");
// Set the time
rtc.setTime(hours, minutes, seconds);
rtc.setDate(weekDay, day, month, year);
// ALARM_A (default argument)
rtc.setAlarmDay(day);
rtc.setAlarmTime(hours, minutes, seconds + 5, 567);
rtc.setAlarmTime(hours, minutes, seconds + 10, 567);
rtc.enableAlarm(rtc.MATCH_DHHMMSS);
#ifdef RTC_ALARM_B
// ALARM_B
rtc.setAlarmDay(day, STM32RTC::ALARM_B);
rtc.setAlarmTime(hours, minutes, seconds + 5, 567, STM32RTC::ALARM_B);
rtc.setAlarmTime(hours, minutes, seconds + 10, 567, STM32RTC::ALARM_B);
rtc.enableAlarm(rtc.MATCH_DHHMMSS, STM32RTC::ALARM_B);
#endif
} else {
Serial.println("RTC time already set");
// RTC already initialized
time_t epoc, alarm_epoc;
rtc.getTime(&hours, &minutes, &seconds, &subSeconds, &period);
Expand All @@ -138,10 +135,10 @@ void setup() {
alarm_epoc = rtc.getAlarmEpoch();
epoc = rtc.getEpoch();
if (difftime(alarm_epoc, epoc) <= 0) {
Serial.printf("Alarm A was enabled and expired, force callback call\n");
Serial.print("Alarm A was enabled and expired, force callback call\n");
alarmMatch(&atime);
} else {
Serial.printf("Alarm A was enabled and restored\n");
Serial.print("Alarm A was enabled and restored\n");
}
}
#ifdef RTC_ALARM_B
Expand All @@ -151,26 +148,28 @@ void setup() {
alarm_epoc = rtc.getAlarmEpoch(STM32RTC::ALARM_B);
epoc = rtc.getEpoch();
if (difftime(alarm_epoc, epoc) <= 0) {
Serial.printf("Alarm B was enabled and expired, force callback call\n");
Serial.print("Alarm B was enabled and expired, force callback call\n");
alarmMatch(&btime);
} else {
Serial.printf("Alarm B was enabled and restored\n");
Serial.print("Alarm B was enabled and restored\n");
}
}
#endif
Serial.printf("RTC time already set\n");
Serial.print("RTC time already set\n");
}
// For STM32F1xx series, alarm is always disabled after a reset.
bool alarmA = rtc.isAlarmEnabled(STM32RTC::ALARM_A);
Serial.printf("Alarm A enable status: %s\n", (alarmA) ? "True" : "False");
Serial.print("Alarm A enable status: ");
Serial.println((alarmA) ? "True" : "False");
if (!alarmA) {
rtc.setAlarmDay(day);
rtc.setAlarmTime(hours, minutes, seconds + 5, 567);
rtc.enableAlarm(rtc.MATCH_DHHMMSS);
}
#ifdef RTC_ALARM_B
bool alarmB = rtc.isAlarmEnabled(STM32RTC::ALARM_B);
Serial.printf("Alarm B enable status: %s\n", (alarmB) ? "True" : "False");
Serial.print("Alarm B enable status: ");
Serial.println((alarmB) ? "True" : "False");
if (!alarmB) {
rtc.setAlarmDay(day, STM32RTC::ALARM_B);
rtc.setAlarmTime(hours, minutes, seconds + 5, 567, STM32RTC::ALARM_B);
Expand All @@ -184,11 +183,41 @@ void setup() {
void loop() {
rtc.getTime(&hours, &minutes, &seconds, &subSeconds, &period);
// Print current date & time
Serial.printf("\n%02d/%02d/%02d %02d:%02d:%02d.%03d\n", rtc.getDay(), rtc.getMonth(), rtc.getYear(), hours, minutes, seconds, subSeconds);
Serial.print(rtc.getDay());
Serial.print("/");
Serial.print(rtc.getMonth());
Serial.print("/");
Serial.print(rtc.getYear());
Serial.print(" ");
Serial.print(hours);
Serial.print(":");
Serial.print(minutes);
Serial.print(":");
Serial.print(seconds);
Serial.print(".");
Serial.println(subSeconds);
// Print current alarm configuration
Serial.printf("Alarm A: %02d %02d:%02d:%02d.%03d\n", rtc.getAlarmDay(), rtc.getAlarmHours(), rtc.getAlarmMinutes(), rtc.getAlarmSeconds(), rtc.getAlarmSubSeconds());
Serial.print("Alarm A: ");
Serial.print(rtc.getAlarmDay());
Serial.print(" ");
Serial.print(rtc.getAlarmHours());
Serial.print(":");
Serial.print(rtc.getAlarmMinutes());
Serial.print(":");
Serial.print(rtc.getAlarmSeconds());
Serial.print(".");
Serial.println(rtc.getAlarmSubSeconds());
#ifdef RTC_ALARM_B
Serial.printf("Alarm B: %02d %02d:%02d:%02d.%03d\n", rtc.getAlarmDay(STM32RTC::ALARM_B), rtc.getAlarmHours(STM32RTC::ALARM_B), rtc.getAlarmMinutes(STM32RTC::ALARM_B), rtc.getAlarmSeconds(STM32RTC::ALARM_B), rtc.getAlarmSubSeconds(STM32RTC::ALARM_B));
Serial.print("Alarm B: ");
Serial.print(rtc.getAlarmDay(STM32RTC::ALARM_B));
Serial.print(" ");
Serial.print(rtc.getAlarmHours(STM32RTC::ALARM_B));
Serial.print(":");
Serial.print(rtc.getAlarmMinutes(STM32RTC::ALARM_B));
Serial.print(":");
Serial.print(rtc.getAlarmSeconds(STM32RTC::ALARM_B));
Serial.print(".");
Serial.println(rtc.getAlarmSubSeconds(STM32RTC::ALARM_B));
#endif
delay(1000);
}
Expand Down Expand Up @@ -224,12 +253,14 @@ void alarmMatch(void* data) {
}
#endif
if (cbdata.alarm_a) {
Serial.printf("\t\t\tAlarm A Match %i\n", ++alarmMatch_counter);
Serial.print("\t\t\tAlarm A Match ");
Serial.println(++alarmMatch_counter);
rtc.setAlarmEpoch(epoc + sec, STM32RTC::MATCH_SS, epoc_ms);
}
#ifdef RTC_ALARM_B
else {
Serial.printf("\t\t\tAlarm B Match %i\n", ++alarmMatchB_counter);
Serial.print("\t\t\tAlarm B Match ");
Serial.println(++alarmMatchB_counter);
rtc.setAlarmEpoch(epoc + sec, STM32RTC::MATCH_SS, epoc_ms, STM32RTC::ALARM_B);
}
#endif
Expand Down
18 changes: 14 additions & 4 deletions examples/RTC_Seconds/RTC_Seconds.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

*/

#include <STM32RTC.h>
#include <STM32RTCMbed.h>

#ifndef ONESECOND_IRQn
#error "RTC has no feature for One-Second interrupt"
Expand Down Expand Up @@ -54,7 +54,9 @@ static STM32RTC::AM_PM period = STM32RTC::AM;
void setup()
{
Serial.begin(115200);
while (!Serial) {}
while (!Serial) ;

delay(2000);

#if defined(LED_BUILTIN)
// configure pin in output mode
Expand Down Expand Up @@ -95,9 +97,17 @@ void loop()
rtc.getTime(&hrs, &mn, &sec, &subs);

if (toggling) {
Serial.printf("%02d:%02d:%02d\n", hrs, mn, sec);
Serial.print(hrs);
Serial.print(":");
Serial.print(mn);
Serial.print(":");
Serial.println(sec);
} else {
Serial.printf("%02d %02d %02d\n", hrs, mn, sec);
Serial.print(hrs);
Serial.print(" ");
Serial.print(mn);
Serial.print(" ");
Serial.println(sec);
}
#if defined(LED_BUILTIN)
digitalWrite(pin, toggling);
Expand Down
Loading