From 494d3b0337838453c5922a51cf0e8205ece784c0 Mon Sep 17 00:00:00 2001 From: Francois Ramu Date: Mon, 3 Jul 2023 09:35:00 +0200 Subject: [PATCH] add new sketch for RTC running in MIX mode with Alarm This examples is configuring the RTC in MIX mode (binary + calendar BCD) and set Alarm B (if exists) few ms after the current time, whenAlarm A is set in calendar mode. Mainly used on stm32wl55 devices. --- examples/mixRTCAlarm/mixRTCAlarm.ino | 97 ++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 examples/mixRTCAlarm/mixRTCAlarm.ino diff --git a/examples/mixRTCAlarm/mixRTCAlarm.ino b/examples/mixRTCAlarm/mixRTCAlarm.ino new file mode 100644 index 0000000..02ea167 --- /dev/null +++ b/examples/mixRTCAlarm/mixRTCAlarm.ino @@ -0,0 +1,97 @@ +/* + mode Mix RTC alarm + + This sketch shows how to configure the alarm A & B of the RTC in MIX mode + + Creation 12 Dec 2017 + by Wi6Labs + Modified 03 Jul 2020 + by Frederic Pillon for STMicroelectronics + Modified 03 Jul 2023 + by Francois Ramu for STMicroelectronics + + This example code is in the public domain. + + https://github.com/stm32duino/STM32RTC +*/ + +#include + +/* Get the rtc object */ +STM32RTC& rtc = STM32RTC::getInstance(); + +/* Change these values to set the current initial time */ +const byte seconds = 06; +const byte minutes = 22; +const byte hours = 16; + +/* Change these values to set the current initial date */ +const byte day = 25; +const byte month = 6; +const byte year = 23; + +uint32_t timeout; + +void setup() +{ + Serial.begin(115200); + + // Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK. + rtc.setClockSource(STM32RTC::LSE_CLOCK); + + /* Configure the MIX mode */ + rtc.setBinaryMode(STM32RTC::MODE_MIX); + + rtc.begin(true, STM32RTC::HOUR_24); + + rtc.setTime(hours, minutes, seconds); + rtc.setDate(day, month, year); + + /* wait for a while */ + delay(200); + + Serial.printf("Start at %02d:%02d:%02d.%03d\r\n", + rtc.getHours(), rtc.getMinutes(), rtc.getSeconds(), rtc.getSubSeconds()); + + /* Attach the callback function before enabling Interrupt */ + rtc.attachInterrupt(alarmAMatch); + + /* Program the AlarmA in a 12 seconds */ + rtc.setAlarmDay(day); + rtc.setAlarmTime(hours, minutes, seconds + 12); + rtc.enableAlarm(rtc.MATCH_DHHMMSS); + Serial.printf("Set Alarm A in 12s (at %02d:%02d:%02d)\r\n", + rtc.getAlarmHours(), rtc.getAlarmMinutes(), rtc.getAlarmSeconds()); + +#ifdef RTC_ALARM_B + /* Program ALARM B in 400ms ms from now (keep timeout < 1000ms) */ + timeout = rtc.getSubSeconds() + 400; + + rtc.attachInterrupt(alarmBMatch, STM32RTC::ALARM_B); + rtc.setAlarmSubSeconds(timeout, STM32RTC::ALARM_B); + rtc.enableAlarm(rtc.MATCH_SUBSEC, STM32RTC::ALARM_B); + Serial.printf("Set Alarm B (in %d ms) at %d ms\r\n", 400, + rtc.getAlarmSubSeconds(STM32RTC::ALARM_B)); +#endif + +} + +void loop() +{ + +} + +void alarmAMatch(void *data) +{ + UNUSED(data); + Serial.printf("Alarm A Match at %02d:%02d:%02d\r\n", + rtc.getHours(), rtc.getMinutes(), rtc.getSeconds()); +} + +void alarmBMatch(void *data) +{ + UNUSED(data); + Serial.printf("Alarm B Match at %d ms\r\n", rtc.getSubSeconds()); +} + +