|
| 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 | + |
0 commit comments