-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRTCAdjust.ino
245 lines (216 loc) · 5.93 KB
/
RTCAdjust.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* RTCAdjust.cpp
*
* Created on: Dec 10, 2018
*
* @license MIT use at your own risk
*
* Copyright 2018 andrew goh
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <Arduino.h>
#include "rtadjust.h"
/* note that the adjustment functions needs this RTClock
* if you rename variable rt, update rtadjust.h so that the
* extern refers to this RTClock*/
RTClock rt(RTCSEL_LSE); // initialise
#define BUFLEN 100
uint8_t ind = 0;
char cmd = 0;
char buf[BUFLEN];
tm_t tm;
enum LineState {
KEY,
LINE
} state;
void processkey();
void showtime();
void synctime(char *buf, int len);
void settime(char *buf, int len);
void calibrate(char *buf, int len);
void setdriftdur(char *buf, int len);
void help();
void clearbuf();
void setup() {
state = LineState::KEY;
Serial.begin(115200);
/* initialise access to backup registers,
* this is necessary due to the use of backup registers */
bkp_init();
/* adjust rtc */
adjtime();
//turn on the led basic check
//pinMode(LED_BUILTIN, OUTPUT);
//digitalWrite(LED_BUILTIN, HIGH);
}
void loop() {
if(Serial.available())
processkey();
else
delay(1);
}
void processkey() {
char c = Serial.read();
//echo the char;
Serial.print(c);
if( state == LineState::LINE ) {
if (ind < BUFLEN && c != '\r') {
if(c == 8 || c == 127 ) // backspace
ind--;
else
buf[ind++] = c; // copy the chars into buf
} else {
switch(cmd) {
case 's':
synctime(buf,ind);
break;
case 'T':
settime(buf,ind);
break;
case 'c':
calibrate(buf,ind);
break;
case 'x':
setdriftdur(buf,ind);
break;
default:
break;
}
state = LineState::KEY;
cmd = 0;
clearbuf();
}
} else {
switch(c) {
case 't':
showtime();
break;
case 's':
state = LineState::LINE;
clearbuf();
cmd = 's';
break;
case 'T':
state = LineState::LINE;
clearbuf();
cmd = 'T';
break;
case 'c':
state = LineState::LINE;
clearbuf();
cmd = 'c';
break;
case 'x':
state = LineState::LINE;
clearbuf();
cmd = 'x';
break;
case 'h':
case 'H':
case '?':
help();
break;
default:
break;
}
}
}
void showtime() {
// get and print actual RTC timestamp
rt.breakTime(rt.now(), tm);
memset(buf,0,BUFLEN);
sprintf(buf, "RTC timestamp: %u-%u-%u, %02u:%02u:%02u",
tm.year+1970, tm.month, tm.day, tm.hour, tm.minute, tm.second);
Serial.println(buf);
clearbuf();
Serial.print("last adj:");
rt.breakTime(getbkptime(), tm);
memset(buf,0,BUFLEN);
sprintf(buf, "RTC timestamp: %u-%u-%u, %02u:%02u:%02u",
tm.year+1970, tm.month, tm.day, tm.hour, tm.minute, tm.second);
Serial.println(buf);
clearbuf();
Serial.print(F("drift duration, number of seconds for the stm32 rtc to drift 1 secs (faster):"));
Serial.println(getdrift());
Serial.print(F("BKP_RTCCR:"));
Serial.println(getrtccr());
}
void synctime(char *buf, int len) {
if (len == BUFLEN) buf[BUFLEN-1] = 0; //terminate the string for safety
if(parsetimestamp(buf, tm) <0) {
Serial.println(F("invalid date/time"));
return;
}
time_t time = rt.makeTime(tm);
/* this call the actual function to set the RTC and update
* the last adjusted time simultaneously
*/
synctime(time);
}
void calibrate(char *buf, int len) {
if (len == BUFLEN) buf[BUFLEN-1] = 0; //terminate the string for safety
if(parsetimestamp(buf, tm) <0) {
Serial.println(F("invalid date/time"));
return;
}
time_t time = rt.makeTime(tm);
/* this call the calibratertc() function to calculate
* the drift duration
*/
calibratertc(time);
}
/*
* this function sets the rtc directly by-passing all the adjustments
*
* note that this function is used during tests to simulate drifts etc
* hence it is not features in help();
*
* in a normal context use synctime() to set the RTC time so that
* the last adjustment date/time is updated as well
*/
void settime(char *buf, int len) {
if (len == BUFLEN) buf[BUFLEN-1] = 0; //terminate the string for safety
if(parsetimestamp(buf, tm) <0) {
Serial.println(F("invalid date/time"));
return;
}
rt.setTime(tm);
}
void setdriftdur(char *buf, int len) {
if (len == BUFLEN) buf[BUFLEN-1] = 0; //terminate the string for safety
int16_t drift_dur = atoi(buf);
/* this funciton updates the drift duration directly */
setbkpdrift(drift_dur);
}
void help() {
Serial.println(F("h, H, ? - help this message"));
Serial.println(F("t - current time"));
Serial.println(F("sYYYY-MM-DD HH:MM:SS - sync/set time"));
Serial.println(F("cYYYY-MM-DD HH:MM:SS - calibrate rtc"));
Serial.println(F("where YYYY-MM-DD HH:MM_SS - is the current accurate time"));
Serial.println(F("xnnnn - set the drift duration where nnnn is the drift duration"));
Serial.println(F(" number of seconds for the stm32 rtc to drift 1 secs (faster)"));
Serial.println(F("the s, c, x commands needs to end with a carriage return at the end of line"));
Serial.println();
}
void clearbuf() {
ind = 0;
memset(buf,0,BUFLEN);
}