1+ /*
2+ This is an example of how to use the RTclock of STM32F4 device
3+
4+ This example can also be used to set the RTC to the current epoch time:
5+ - goto: http://www.unixtimestamp.com/
6+ - enter the current date and time to the right field "Timestamp converter"
7+ - press the "Convert" button
8+ -
9+ */
10+
11+ #include < RTClock.h>
12+
13+ #include < Streaming.h>
14+
15+
16+ // RTClock rt(RTCSEL_LSE); // initialise
17+ RTClock rtc;
18+
19+ time_t tt;
20+ tm_t tm;
21+
22+ const uint32_t DEFAULT_TIME = 1498944019 ; // 2017.07.01, 21:20:19 used as reference epoch time
23+
24+ #define TIME_HEADER ' T' // Header tag for serial time sync message
25+ #define TIME_REQUEST 7 // ASCII bell character requests a time sync message
26+
27+ #define LED_PIN BOARD_LED_PIN
28+
29+ // -----------------------------------------------------------------------------
30+ void blink ()
31+ {
32+ digitalWrite (LED_PIN, digitalRead (LED_PIN)?LOW:HIGH);
33+ }
34+
35+ uint8_t s[20 ]; // for serial parsing
36+
37+ // -----------------------------------------------------------------------------
38+ char * read_line ()
39+ {
40+ while ( Serial.available () ) Serial.read (); // empty Rx buffer
41+ while ( Serial.available ()<=0 ) ; // wait for new characters
42+ uint8_t c, i = 0 ;
43+ s[0 ] = 0 ;
44+ while ( Serial.available () && (i<20 ) ) {
45+ c = Serial.read ();
46+ if ( c==' \n ' || c== ' \r ' ) {
47+ s[i] = 0 ;
48+ break ;
49+ }
50+ s[i++] = c;
51+ }
52+ while ( Serial.available () ) Serial.read (); // flush Rx
53+ return (char *)&s[0 ];
54+ }
55+
56+ // -----------------------------------------------------------------------------
57+ void processSyncMessage (void )
58+ {
59+ if ( Serial.available () ) {
60+ if ( *read_line ()==(TIME_HEADER) ) {
61+ uint32_t pctime = atoi ((const char *)&s[1 ]);
62+ Serial << (" Epoch time received: " ) << pctime << endl;
63+ if ( pctime >= DEFAULT_TIME) { // check the integer is a valid epoch time
64+ rtc.setTime (pctime); // Set RTC to the time received via the serial port
65+ }
66+ }
67+ Serial << endl;
68+ }
69+ }
70+
71+ // -----------------------------------------------------------------------------
72+ void Change_DateTime (void )
73+ {
74+ // check and correct the weekday if necessary
75+ rtc.getTime (tm);
76+ Serial << " Current weekday is " << (tm.weekday );
77+ // get time elements
78+ tt = rtc.makeTime (tm);
79+ uint16_t tmp = rtc.weekday (tt);
80+ if ( tmp!=tm.weekday ) {// correct weekday
81+ rtc.setTime (tt);
82+ Serial << " instead of " << tmp << " . Now is corrected.\n " ;
83+ } else {
84+ Serial << " - seems to be fine, no need to change it.\n " ;
85+ }
86+
87+ uint8_t chg = 0 ;
88+ // get time elements
89+ rtc.getTime (tm);
90+ Serial << " \n Current RTC date: " << (1970 +tm.year ) << " ." << (tm.month ) << (" ." ) << (tm.day ) << (" , weekday: " ) << (tm.weekday ) << endl;
91+ Serial << " Do you want to change it? (y/n)\n " ;
92+ if ( *read_line ()==' y' ) {
93+ // change here the date
94+ change_year:
95+ Serial << " Current year: " << (1970 +tm.year ) << " . Enter new year in \" YYYY\" format (numbers only) or press enter to skip.\n " ;
96+ if (*read_line ()==0 ) goto change_month;
97+ tmp = atoi ((const char *)s);
98+ if ( tmp<1970 ) { Serial << " Please enter a valid number greater than 1970\n " ; goto change_year; }
99+ Serial << " You entered: " << tmp << " . Accept value? (y/n)\n " ;
100+ if ( *read_line ()==' n' ) goto change_year;
101+ tm.year = tmp-1970 ;
102+ chg = 1 ;
103+ change_month:
104+ Serial << " Current month: " << tm.month << " . Enter new month in \" MM\" format [1..12] or press enter to skip.\n " ;
105+ if (*read_line ()==0 ) goto change_day;
106+ tmp = atoi ((const char *)s);
107+ if ( tmp<1 || tmp>12 ) { Serial << " Please enter a valid number [1..12]\n " ; goto change_month; }
108+ Serial << " You entered: " << tmp << " . Accept value? (y/n)\n " ;
109+ if ( *read_line ()==' n' ) goto change_month;
110+ tm.month = tmp;
111+ chg = 1 ;
112+ change_day:
113+ Serial << " Current day: " << tm.day << " . Enter new day in \" DD\" format [1..31] or press enter to skip.\n " ;
114+ if (*read_line ()==0 ) goto change_weekday;
115+ tmp = atoi ((const char *)s);
116+ if ( tmp<1 || tmp>31 ) { Serial << " Please enter a valid number [1..31]\n " ; goto change_day; }
117+ Serial << " You entered: " << tmp << " . Accept value? (y/n)\n " ;
118+ if ( *read_line ()==' n' ) goto change_day;
119+ tm.day = tmp;
120+ chg = 1 ;
121+ change_weekday:
122+ Serial << " Current weekday: " << tm.weekday << " . Enter new weekday [1(=Monday)..7(=Sunday)] or press enter to skip.\n " ;
123+ if (*read_line ()==0 ) goto change_time;
124+ tmp = atoi ((const char *)s);
125+ if ( tmp<1 || tmp>7 ) { Serial << " Please enter a valid number [1..7]\n " ; goto change_weekday; }
126+ Serial << " You entered: " << tmp << " . Accept value? (y/n)\n " ;
127+ if ( *read_line ()==' n' ) goto change_weekday;
128+ tm.weekday = tmp;
129+ chg = 1 ;
130+ change_time:
131+ Serial << " Current RTC time: " << _TIME (tm.hour , tm.minute , tm.second ) << endl;
132+ Serial << " Do you want to change it? (y/n)\n " ;
133+ if ( *read_line ()==' n' ) goto change_end;
134+ change_hour:
135+ Serial << " Current hour: " << tm.hour << " . Enter new hour [0..23] or press enter to skip.\n " ;
136+ if (*read_line ()==0 ) goto change_minute;
137+ tmp = atoi ((const char *)s);
138+ if ( tmp>23 ) { Serial << " Please enter a valid number [0..23]\n " ; goto change_hour; }
139+ Serial << " You entered: " << tmp << " . Accept value? (y/n)\n " ;
140+ if ( *read_line ()==' n' ) goto change_hour;
141+ tm.hour = tmp;
142+ chg = 1 ;
143+ change_minute:
144+ Serial << " Current minute: " << tm.minute << " . Enter new minute [0..59] or press enter to skip.\n " ;
145+ if (*read_line ()==0 ) goto change_second;
146+ tmp = atoi ((const char *)s);
147+ if ( tmp>59 ) { Serial << " Please enter a valid number [0..59]\n " ; goto change_minute; }
148+ Serial << " You entered: " << tmp << " . Accept value? (y/n)\n " ;
149+ if ( *read_line ()==' n' ) goto change_minute;
150+ tm.minute = tmp;
151+ chg = 1 ;
152+ change_second:
153+ Serial << " Current second: " << tm.second << " . Enter new second [0..59] or press enter to skip.\n " ;
154+ if (*read_line ()==0 ) goto change_end;
155+ tmp = atoi ((const char *)s);
156+ if ( tmp>59 ) { Serial << " Please enter a valid number [0..59]\n " ; goto change_second; }
157+ Serial << " You entered: " << tmp << " . Accept value? (y/n)\n " ;
158+ if ( *read_line ()==' n' ) goto change_second;
159+ tm.second = tmp;
160+ chg = 1 ;
161+ } else {
162+ goto change_time;
163+ }
164+
165+ change_end:
166+ if ( chg ) {
167+ // set here the RTC time.
168+ Serial << " Changed date & time: " << (1970 +tm.year ) << " ." << (tm.month ) << (" ." ) << (tm.day ) << (" , weekday: " ) << (tm.weekday ) << " , " << _TIME (tm.hour , tm.minute , tm.second ) << endl;
169+ Serial << " Write now to RTC? (y/n)\n " ;
170+ read_line ();
171+ if ( s[0 ]==' y' ) {
172+ rtc.setTime (tm);
173+ Serial << " Data written to RTC.\n\n " ;
174+ }
175+ } else
176+ Serial << " RTC was not changed.\n\n " ;
177+ }
178+ // -----------------------------------------------------------------------------
179+ void setup ()
180+ {
181+ Serial.begin ();
182+ delay (3000 );
183+
184+ pinMode (LED_PIN, OUTPUT);
185+
186+ Serial << " This is an example of how to use the STM32F4 RTC library.\n\n " ;
187+
188+ Change_DateTime ();
189+ }
190+
191+ // -----------------------------------------------------------------------------
192+ void loop ()
193+ {
194+ if ( Serial.available () ) {
195+ // adjust time according to received epoch time from PC
196+ processSyncMessage ();
197+ }
198+
199+ if (tt!=rtc.now ()) {
200+ // get epoch time
201+ tt = rtc.now ();
202+ Serial << (" - RTC epoch timestamp = " ) << (tt);
203+ // get time elements
204+ rtc.getTime (tm);
205+ // rtc.breakTime(tt, tm);
206+ Serial << (" == " ) << (1970 +tm.year ) << " ." << (tm.month ) << (" ." ) << (tm.day ) << (" , weekday " ) << (tm.weekday ) << (" , " );
207+ Serial << _TIME (tm.hour , tm.minute , tm.second ) << endl;
208+ }
209+ }
0 commit comments