Skip to content

Commit d3f6526

Browse files
authored
Merge pull request #93 from FRASTM/rtc_mix
Rtc mix mode with subseconds param. expressed in milliseconds
2 parents 5f53d03 + e466ec9 commit d3f6526

File tree

8 files changed

+427
-28
lines changed

8 files changed

+427
-28
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,30 @@ It is possible to use it thanks all alarm API with an extra argument:
153153
rtc.enableAlarm(rtc.MATCH_DHHMMSS, STM32RTC::ALARM_B);
154154
```
155155

156+
### Since STM32RTC version higher than 1.3.7
157+
_Get the RTC handle_
158+
159+
* **`RTC_HandleTypeDef *RTC_GetHandle(void)`**
160+
161+
_binary and mix modes_
162+
163+
Some STM32 RTC have a binary mode with 32-bit free-running counter
164+
in addition to their BCD mode for calendar (for example stm32wl55).
165+
Combined with BCD this is the MIX mode. Only using the binary counter is the BIN mode.
166+
Three RTC functional modes are available:
167+
- `STM32RTC::MODE_BCD`
168+
- `STM32RTC::MODE_MIX`
169+
- `STM32RTC::MODE_BIN`
170+
171+
* **`Binary_Mode getBinaryMode(void);`**
172+
* **`void setBinaryMode(Binary_Mode mode);`**
173+
174+
175+
Any API using the Subsecond parameter is expressed in milliseconds
176+
whatever the RTC input clock. This parameter is [0..999] in MIX or BCD mode
177+
and [0..0xFFFFFFFF] in BIN mode. In this configuration, time and date registers
178+
are not used by the RTC.
179+
156180
Refer to the Arduino RTC documentation for the other functions
157181
http://arduino.cc/en/Reference/RTC
158182

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
mode BINary only RTC alarm
3+
4+
This sketch shows how to configure the alarm A & B of the RTC in BIN mode
5+
6+
Creation 12 Dec 2017
7+
by Wi6Labs
8+
Modified 03 Jul 2020
9+
by Frederic Pillon for STMicroelectronics
10+
Modified 03 sept 2023
11+
by Francois Ramu for STMicroelectronics
12+
13+
This example code is in the public domain.
14+
15+
https://github.com/stm32duino/STM32RTC
16+
*/
17+
18+
#include <STM32RTC.h>
19+
20+
/* Get the rtc object */
21+
STM32RTC& rtc = STM32RTC::getInstance();
22+
23+
uint32_t timeout;
24+
25+
void setup()
26+
{
27+
Serial.begin(115200);
28+
29+
// Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK.
30+
rtc.setClockSource(STM32RTC::LSE_CLOCK);
31+
32+
/* Configure the RTC mode */
33+
rtc.setBinaryMode(STM32RTC::MODE_BIN);
34+
35+
/* in BIN mode time and Date register are not used, only the subsecond register for milisseconds */
36+
rtc.begin(true, STM32RTC::HOUR_24);
37+
38+
/* wait for a while */
39+
delay(300);
40+
41+
/* subsecond expressed in milliseconds */
42+
Serial.printf("Start at %d ms \r\n", rtc.getSubSeconds());
43+
44+
/* Attach the callback function before enabling Interrupt */
45+
rtc.attachInterrupt(alarmAMatch);
46+
47+
/* Program the AlarmA in 12 seconds */
48+
rtc.setAlarmTime(0, 0, 0, 12000);
49+
rtc.enableAlarm(rtc.MATCH_SUBSEC);
50+
Serial.printf("Set Alarm A in 12s (at %d ms)\r\n", rtc.getAlarmSubSeconds());
51+
52+
#ifdef RTC_ALARM_B
53+
/* Program ALARM B in 600ms ms from now (keep timeout < 1000ms) */
54+
timeout = rtc.getSubSeconds() + 600;
55+
56+
rtc.attachInterrupt(alarmBMatch, STM32RTC::ALARM_B);
57+
rtc.setAlarmSubSeconds(timeout, STM32RTC::ALARM_B);
58+
rtc.enableAlarm(rtc.MATCH_SUBSEC, STM32RTC::ALARM_B);
59+
Serial.printf("Set Alarm B (in %d ms) at %d ms\r\n", 600,
60+
rtc.getAlarmSubSeconds(STM32RTC::ALARM_B));
61+
#endif /* RTC_ALARM_B */
62+
63+
}
64+
65+
void loop()
66+
{
67+
68+
}
69+
70+
void alarmAMatch(void *data)
71+
{
72+
UNUSED(data);
73+
rtc.disableAlarm(STM32RTC::ALARM_A);
74+
Serial.printf("Alarm A Match at %d ms \r\n", rtc.getSubSeconds());
75+
}
76+
77+
#ifdef RTC_ALARM_B
78+
void alarmBMatch(void *data)
79+
{
80+
UNUSED(data);
81+
rtc.disableAlarm(STM32RTC::ALARM_B); /* Else it will trig again */
82+
Serial.printf("Alarm B Match at %d ms\r\n", rtc.getSubSeconds());
83+
}
84+
#endif /* RTC_ALARM_B */
85+

examples/mixRTCAlarm/mixRTCAlarm.ino

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
mode Mix RTC alarm
3+
4+
This sketch shows how to configure the alarm A & B (if exists)
5+
of the RTC in MIX or BCD (BINARY none) mode
6+
7+
Creation 12 Dec 2017
8+
by Wi6Labs
9+
Modified 03 Jul 2020
10+
by Frederic Pillon for STMicroelectronics
11+
Modified 03 Jul 2023
12+
by Francois Ramu for STMicroelectronics
13+
14+
This example code is in the public domain.
15+
16+
https://github.com/stm32duino/STM32RTC
17+
*/
18+
19+
#include <STM32RTC.h>
20+
21+
/* Get the rtc object */
22+
STM32RTC& rtc = STM32RTC::getInstance();
23+
24+
/* Change these values to set the current initial time */
25+
const byte seconds = 06;
26+
const byte minutes = 22;
27+
const byte hours = 16;
28+
29+
/* Change these values to set the current initial date */
30+
const byte day = 25;
31+
const byte month = 6;
32+
const byte year = 23;
33+
34+
uint32_t timeout;
35+
36+
void setup()
37+
{
38+
Serial.begin(115200);
39+
40+
// Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK.
41+
rtc.setClockSource(STM32RTC::LSE_CLOCK);
42+
43+
/* Configure the RTC mode : STM32RTC::MODE_MIX or STM32RTC::MODE_BCD */
44+
rtc.setBinaryMode(STM32RTC::MODE_BCD);
45+
46+
rtc.begin(true, STM32RTC::HOUR_24);
47+
48+
rtc.setTime(hours, minutes, seconds);
49+
rtc.setDate(day, month, year);
50+
51+
/* wait for a while */
52+
delay(300);
53+
54+
Serial.printf("Start at %02d:%02d:%02d.%03d\r\n",
55+
rtc.getHours(), rtc.getMinutes(), rtc.getSeconds(), rtc.getSubSeconds());
56+
57+
/* Attach the callback function before enabling Interrupt */
58+
rtc.attachInterrupt(alarmAMatch);
59+
60+
/* Program the AlarmA in 12 seconds */
61+
rtc.setAlarmDay(day);
62+
rtc.setAlarmTime(hours, minutes, seconds + 12);
63+
rtc.enableAlarm(rtc.MATCH_DHHMMSS);
64+
Serial.printf("Set Alarm A in 12s (at %02d:%02d:%02d)\r\n",
65+
rtc.getAlarmHours(), rtc.getAlarmMinutes(), rtc.getAlarmSeconds());
66+
67+
#ifdef RTC_ALARM_B
68+
/* Program ALARM B in 600ms ms from now (keep timeout < 1000ms) */
69+
timeout = rtc.getSubSeconds() + 600;
70+
71+
rtc.attachInterrupt(alarmBMatch, STM32RTC::ALARM_B);
72+
rtc.setAlarmSubSeconds(timeout, STM32RTC::ALARM_B);
73+
rtc.enableAlarm(rtc.MATCH_SUBSEC, STM32RTC::ALARM_B);
74+
Serial.printf("Set Alarm B (in %d ms) at %d ms\r\n", 600,
75+
rtc.getAlarmSubSeconds(STM32RTC::ALARM_B));
76+
#endif /* RTC_ALARM_B */
77+
}
78+
79+
void loop()
80+
{
81+
/* Just wait for Alarm */
82+
}
83+
84+
void alarmAMatch(void *data)
85+
{
86+
UNUSED(data);
87+
rtc.disableAlarm(STM32RTC::ALARM_A);
88+
Serial.printf("Alarm A Match at %02d:%02d:%02d\r\n",
89+
rtc.getHours(), rtc.getMinutes(), rtc.getSeconds());
90+
}
91+
92+
#ifdef RTC_ALARM_B
93+
void alarmBMatch(void *data)
94+
{
95+
UNUSED(data);
96+
rtc.disableAlarm(STM32RTC::ALARM_B);
97+
Serial.printf("Alarm B Match at %d ms\r\n", rtc.getSubSeconds());
98+
}
99+
#endif /* RTC_ALARM_B */

keywords.txt

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ STM32RTC KEYWORD1
1313
#######################################
1414

1515
getInstance KEYWORD2
16+
getHandle KEYWORD2
17+
18+
getBinaryMode KEYWORD2
19+
setBinaryMode KEYWORD2
1620

1721
getWeekDay KEYWORD2
1822
getDay KEYWORD2
@@ -85,6 +89,7 @@ IS_HOUR_FORMAT KEYWORD2
8589
# Constants (LITERAL1)
8690
#######################################
8791
MATCH_OFF LITERAL1
92+
MATCH_SUBSEC LITERAL1
8893
MATCH_SS LITERAL1
8994
MATCH_MMSS LITERAL1
9095
MATCH_HHMMSS LITERAL1
@@ -100,3 +105,6 @@ LSI_CLOCK LITERAL1
100105
HSE_CLOCK LITERAL1
101106
ALARM_A LITERAL1
102107
ALARM_B LITERAL1
108+
MODE_BCD LITERAL1
109+
MODE_BIN LITERAL1
110+
MODE_MIX LITERAL1

0 commit comments

Comments
 (0)