Skip to content

Commit 9179225

Browse files
committed
Add a RTC_Tests example
This test several STM32RTC library features: - get/set date and time - get/set alarm date and time - epoch API - hours format - alarm settings - clock source selection - custom prescalers Signed-off-by: Frederic.Pillon <frederic.pillon@st.com>
1 parent 6634068 commit 9179225

File tree

3 files changed

+369
-0
lines changed

3 files changed

+369
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
void rtc_config(STM32RTC::RTC_Source_Clock source, STM32RTC::RTC_Hour_Format format, const char* date, const char* time) {
3+
if (!IS_CLOCK_SOURCE(source)) {
4+
Serial.println("Wrong clock source");
5+
return;
6+
}
7+
if (!IS_HOUR_FORMAT(format)) {
8+
Serial.println("Wrong hour format");
9+
return;
10+
}
11+
if (rtc.isConfigured())
12+
rtc.end();
13+
14+
rtc.setClockSource(source);
15+
hourFormat = format;
16+
rtc_setTime(date, time);
17+
}
18+
19+
void rtc_setTime(const char* date, const char* time) {
20+
initDateTime(date, time);
21+
rtc.begin(hourFormat);
22+
23+
// Set the time
24+
rtc.setHours(hours, period);
25+
rtc.setMinutes(minutes);
26+
rtc.setSeconds(seconds);
27+
28+
// Set the date
29+
rtc.setWeekDay(weekDay);
30+
rtc.setDay(day);
31+
rtc.setMonth(month);
32+
rtc.setYear(year);
33+
34+
// you can use also
35+
//rtc.setTime(hours, minutes, seconds);
36+
//rtc.setDate(weekDay, day, month, year);
37+
}
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
/*
2+
RTC_Tests
3+
4+
This sketch allows to test STM32RTC library API.
5+
6+
Creation 25 Apr 2018
7+
by Frederic Pillon for STMicroelectronics
8+
9+
This example code is in the public domain.
10+
11+
https://github.com/stm32duino/STM32RTC
12+
13+
*/
14+
15+
#include <STM32RTC.h>
16+
17+
/* Create a rtc object */
18+
STM32RTC rtc;
19+
20+
/* Change these values to set the current initial time
21+
*
22+
* format: date: "Dec 31 2017" and time: "23:59:56"
23+
* by default use built date and time
24+
*/
25+
static const char* mydate = __DATE__;
26+
static const char* mytime = __TIME__;
27+
//static const char* mydate = "Dec 31 2017";
28+
//static const char* mytime = "23:59:56";
29+
30+
/* Change these values to set user (a)synchronous prescalers
31+
* value to test.
32+
*
33+
* Default 99/9999 are for RTCCLK = 1MHz
34+
* Example for DISCO_F746NG with a HSE of 25MHz
35+
* HSE divider will be 25 and RTCCLK will be 1MHz. (HSE/HSEdiv)
36+
*
37+
* To have a calendar clock of 1 Hz:
38+
* clk = RTCCLK / ((predA +1) * (predS +1))
39+
* clk = 1000000 / ((99 +1) * (9999+1)) = 1 Hz
40+
*/
41+
static int8_t userPredA = 99;
42+
static int16_t userPredS = 9999;
43+
44+
/* */
45+
static byte seconds = 0;
46+
static byte minutes = 0;
47+
static byte hours = 0;
48+
49+
static byte weekDay = 1;
50+
static byte day = 0;
51+
static byte month = 0;
52+
static byte year = 0;
53+
54+
static STM32RTC::RTC_Hour_Format hourFormat = STM32RTC::RTC_HOUR_24;
55+
static STM32RTC::RTC_AM_PM period = STM32RTC::RTC_AM;
56+
57+
#ifndef STM32F1xx
58+
static STM32RTC::Alarm_Match SS_MATCH = STM32RTC::MATCH_SS;
59+
static STM32RTC::Alarm_Match MMSS_MATCH = STM32RTC::MATCH_MMSS;
60+
static STM32RTC::Alarm_Match HHMMSS_MATCH = STM32RTC::MATCH_HHMMSS;
61+
#endif
62+
static STM32RTC::Alarm_Match DHHMMSS_MATCH = STM32RTC::MATCH_DHHMMSS;
63+
64+
void setup()
65+
{
66+
Serial.begin(115200);
67+
while(!Serial) {}
68+
}
69+
70+
void loop()
71+
{
72+
int c; // Serial input
73+
STM32RTC::RTC_Source_Clock clkSource = rtc.RTC_LSI_CLOCK;
74+
75+
// Select RTC clock source: RTC_LSI_CLOCK, RTC_LSE_CLOCK or RTC_HSE_CLOCK.
76+
Serial.println("Select clock Source:");
77+
Serial.println("1- RTC_LSI_CLOCK");
78+
Serial.println("2- RTC_LSE_CLOCK");
79+
Serial.println("3- RTC_HSE_CLOCK");
80+
Serial.println();
81+
// get input
82+
while (1) {
83+
while (Serial.read() >= 0) {}
84+
Serial.print("Enter number [1-3] ");
85+
while ((c = Serial.read()) < 0) {}
86+
Serial.println((char)c);
87+
if (c < '1' || c > '3') {
88+
Serial.println("Invalid input");
89+
continue;
90+
}
91+
switch(c) {
92+
case '1':
93+
default:
94+
Serial.println("Test will use RTC_LSI_CLOCK");
95+
clkSource = rtc.RTC_LSI_CLOCK;
96+
break;
97+
case '2':
98+
Serial.println("Test will use RTC_LSE_CLOCK");
99+
clkSource = rtc.RTC_LSE_CLOCK;
100+
break;
101+
case '3':
102+
Serial.println("Test will use RTC_HSE_CLOCK");
103+
clkSource = rtc.RTC_HSE_CLOCK;
104+
break;
105+
}
106+
break;
107+
}
108+
109+
Serial.println("Testing asynchronous and synchronous prescaler setting");
110+
int8_t a;
111+
int16_t s;
112+
rtc.getPrediv(&a, &s);
113+
Serial.print("Async/Sync for default LSI clock: ");
114+
Serial.print(a); Serial.print('/'); Serial.println(s);
115+
rtc_config(clkSource, rtc.RTC_HOUR_24, mydate, mytime);
116+
Serial.print("Async/Sync for selected clock: ");
117+
rtc.getPrediv(&a, &s);
118+
Serial.print(a); Serial.print('/'); Serial.println(s);
119+
rtc.end();
120+
121+
if(clkSource == rtc.RTC_HSE_CLOCK) {
122+
Serial.print("User Async/Sync set to ");
123+
Serial.print(userPredA);
124+
Serial.print("/");
125+
Serial.print(userPredS);
126+
Serial.print(": ");
127+
rtc.setPrediv(userPredA, userPredS);
128+
rtc_config(clkSource, rtc.RTC_HOUR_24, mydate, mytime);
129+
rtc.getPrediv(&a, &s);
130+
Serial.print(a); Serial.print('/'); Serial.println(s);
131+
printDateTime(10, 1000, false);
132+
}
133+
134+
Serial.print("User Async/Sync reset use the computed one: ");
135+
rtc.setPrediv(-1, -1);
136+
rtc_config(clkSource, rtc.RTC_HOUR_24, mydate, mytime);
137+
rtc.getPrediv(&a, &s);
138+
Serial.print(a); Serial.print('/'); Serial.println(s);
139+
140+
// Check date change
141+
Serial.println("Testing date and time");
142+
Serial.println("24H format, new year");
143+
rtc_config(clkSource, rtc.RTC_HOUR_24, "Dec 31 2017", "23:59:56");
144+
printDateTime(8, 1000, false);
145+
Serial.println();
146+
147+
#ifndef STM32F1xx
148+
Serial.println("12H format, new year");
149+
rtc_config(clkSource, rtc.RTC_HOUR_12, "Dec 31 2017", "23:59:56");
150+
printDateTime(8, 1000, false);
151+
Serial.println();
152+
153+
Serial.println("12H format, from AM to PM");
154+
rtc_config(clkSource, rtc.RTC_HOUR_12, "Dec 31 2017", "11:59:56");
155+
printDateTime(8, 1000, false);
156+
Serial.println();
157+
#endif //STM32F1xx
158+
159+
Serial.println("Using Epoch API, set to Jan 1, 2016");
160+
rtc.setEpoch(1451606400); // Jan 1, 2016
161+
for (uint32_t i=0; i<8; i++) {
162+
Serial.print("Unix time = ");
163+
Serial.println(rtc.getEpoch());
164+
Serial.print("Seconds since Jan 1 2000 = ");
165+
Serial.println(rtc.getY2kEpoch());
166+
printDateTime(1, 1000, false);
167+
}
168+
169+
Serial.println("\nTesting alarm");
170+
rtc_config(clkSource, rtc.RTC_HOUR_24, mydate, mytime);
171+
byte alarmSeconds = ((seconds+5)<60) ? seconds+5 : 5;
172+
byte alarmMinutes = ((seconds+5)<60) ? minutes : ((minutes+1)<60) ? minutes+1 : 0;
173+
byte alarmHours = ((seconds+5)<60) ? hours : ((minutes+1)<60) ? hours : ((hours+1)<24) ? hours+1 : 0;
174+
byte alarmDay = (hours==alarmHours)? day: ((day+1)<=31) ? day+1 : 1;
175+
176+
#ifndef STM32F1xx
177+
rtc.attachInterrupt(alarmMatch, (void*)&SS_MATCH);
178+
rtc.setAlarmSeconds(alarmSeconds);
179+
rtc.enableAlarm(rtc.MATCH_SS);
180+
printDateTime(20, 1000, true);
181+
rtc.disableAlarm();
182+
rtc.detachInterrupt();
183+
184+
Serial.println("\nEvery hours");
185+
rtc_setTime(mydate, mytime);
186+
rtc.attachInterrupt(alarmMatch, (void*)&MMSS_MATCH);
187+
rtc.setAlarmMinutes(alarmMinutes);
188+
rtc.enableAlarm(rtc.MATCH_MMSS);
189+
printDateTime(20, 1000, true);
190+
rtc.disableAlarm();
191+
rtc.detachInterrupt();
192+
193+
Serial.println("\nEvery day");
194+
rtc_setTime(mydate, mytime);
195+
rtc.attachInterrupt(alarmMatch, (void*)&HHMMSS_MATCH);
196+
rtc.setAlarmHours(alarmHours);
197+
rtc.enableAlarm(rtc.MATCH_HHMMSS);
198+
printDateTime(20, 1000, true);
199+
rtc.disableAlarm();
200+
rtc.detachInterrupt();
201+
#endif // STM32F1xx
202+
203+
Serial.println("\nEvery month");
204+
rtc_setTime(mydate, mytime);
205+
rtc.attachInterrupt(alarmMatch, (void*)&DHHMMSS_MATCH);
206+
rtc.setAlarmTime(alarmHours, alarmMinutes, alarmSeconds);
207+
rtc.setAlarmDay(alarmDay);
208+
rtc.enableAlarm(rtc.MATCH_DHHMMSS);
209+
printDateTime(20, 1000, true);
210+
rtc.disableAlarm();
211+
rtc.detachInterrupt();
212+
213+
rtc_config(clkSource, rtc.RTC_HOUR_24, mydate, mytime);
214+
Serial.println("\nAlarm disabled. Printing each 10s.");
215+
printDateTime(-1, 10000, false);
216+
}
217+
218+
void alarmMatch(void *data)
219+
{
220+
STM32RTC::Alarm_Match m = *(STM32RTC::Alarm_Match*)data;
221+
222+
Serial.print("Alarm Match ");
223+
switch(m) {
224+
case STM32RTC::MATCH_OFF:
225+
Serial.println("MATCH_OFF could not happen");
226+
break;
227+
case STM32RTC::MATCH_YYMMDDHHMMSS://kept for compatibility
228+
case STM32RTC::MATCH_MMDDHHMMSS: //kept for compatibility
229+
case STM32RTC::MATCH_DHHMMSS:
230+
Serial.println("MATCH_DHHMMSS");
231+
rtc.setMonth(((rtc.getMonth()+1)<13)? rtc.getMonth()+1: 1);
232+
rtc.setEpoch(rtc.getEpoch()- 10);
233+
break;
234+
case STM32RTC::MATCH_HHMMSS:
235+
Serial.println("MATCH_HHMMSS");
236+
rtc.setEpoch(rtc.getEpoch()+86395);
237+
break;
238+
case STM32RTC::MATCH_MMSS:
239+
Serial.println("MATCH_MMSS");
240+
rtc.setEpoch(rtc.getEpoch()+3595);
241+
break;
242+
case STM32RTC::MATCH_SS:
243+
Serial.println("MATCH_SS");
244+
rtc.setEpoch(rtc.getEpoch()+55);
245+
break;
246+
default:
247+
Serial.println("Unknown STM32RTC::Alarm_Match type");
248+
break;
249+
}
250+
}
251+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
static uint8_t conv2d(const char* p) {
3+
uint8_t v = 0;
4+
if ('0' <= *p && *p <= '9')
5+
v = *p - '0';
6+
return 10 * v + *++p - '0';
7+
}
8+
9+
void print2digits(int number) {
10+
if (number < 10) {
11+
Serial.print("0"); // print a 0 before if the number is < than 10
12+
}
13+
Serial.print(number);
14+
}
15+
16+
// sample input: date = "Dec 26 2009", time = "12:34:56"
17+
void initDateTime (const char* date, const char* time) {
18+
year = conv2d(date + 9);
19+
// Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
20+
switch (date[0]) {
21+
case 'J': month = (date[1] == 'a') ? 1 : ((date[2] == 'n') ? 6 : 7); break;
22+
case 'F': month = 2; break;
23+
case 'A': month = date[2] == 'r' ? 4 : 8; break;
24+
case 'M': month = date[2] == 'r' ? 3 : 5; break;
25+
case 'S': month = 9; break;
26+
case 'O': month = 10; break;
27+
case 'N': month = 11; break;
28+
case 'D': month = 12; break;
29+
}
30+
day = conv2d(date + 4);
31+
hours = conv2d(time);
32+
if (hourFormat == rtc.RTC_HOUR_12) {
33+
period = hours >= 12 ? rtc.RTC_PM : rtc.RTC_AM;
34+
hours = hours >= 13 ? hours - 12 : (hours < 1 ? hours + 12 : hours);
35+
}
36+
minutes = conv2d(time + 3);
37+
seconds = conv2d(time + 6);
38+
}
39+
40+
// t: number of print
41+
// d: delay between each print
42+
// a: display alarm
43+
void printDateTime(uint32_t t, uint32_t d, bool a) {
44+
for (uint32_t i=0; i<t; i++) {
45+
// Print date...
46+
print2digits(rtc.getMonth());
47+
Serial.print("/");
48+
print2digits(rtc.getDay());
49+
Serial.print("/");
50+
print2digits(rtc.getYear());
51+
Serial.print("\t");
52+
53+
// ...and time
54+
print2digits(rtc.getHours(&period));
55+
Serial.print(":");
56+
print2digits(rtc.getMinutes());
57+
Serial.print(":");
58+
print2digits(rtc.getSeconds());
59+
if (hourFormat == rtc.RTC_HOUR_12) {
60+
Serial.print(period == rtc.RTC_AM ? " AM":" PM");
61+
}
62+
if(a) {
63+
// Print day...
64+
Serial.print("\t");
65+
print2digits(rtc.getAlarmDay());
66+
Serial.print("\t");
67+
68+
// ...and time
69+
print2digits(rtc.getAlarmHours(&period));
70+
Serial.print(":");
71+
print2digits(rtc.getAlarmMinutes());
72+
Serial.print(":");
73+
print2digits(rtc.getAlarmSeconds());
74+
if (hourFormat == rtc.RTC_HOUR_12) {
75+
Serial.print(period == rtc.RTC_AM ? " AM":" PM");
76+
}
77+
}
78+
Serial.println();
79+
delay(d);
80+
}
81+
}

0 commit comments

Comments
 (0)