-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathrtc_ds1307.cc
426 lines (382 loc) · 13.8 KB
/
rtc_ds1307.cc
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* ########################################################################
PICSimLab - Programmable IC Simulator Laboratory
########################################################################
Copyright (c) : 2010-2024 Luis Claudio Gambôa Lopes <lcgamboa@yahoo.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
For e-mail suggestions : lcgamboa@yahoo.com
######################################################################## */
#include "rtc_ds1307.h"
#include "../lib/board.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #define _DEBUG
#define dprintf \
if (1) { \
} else \
printf
enum { DS_SECONDS = 0, DS_MINUTES, DS_HOURS, DS_DAY, DS_DATE, DS_MONTH, DS_YEAR, DS_CTRL };
#ifdef _DEBUG
static char buff[30];
const char* get_addr_str(const unsigned char addr, const unsigned char data) {
switch (addr) {
case DS_SECONDS:
sprintf(buff, "SECONDS [%02X] = %02X", addr, data);
break;
case DS_MINUTES:
sprintf(buff, "MINUTES [%02X] = %02X", addr, data);
break;
case DS_HOURS:
if ((data & 0x40)) { // 12h mode
sprintf(buff, "HOURS [%02X] = %02X (%02X %s)", addr, data, data & 0x1F, (data & 0x20) ? "PM" : "AM");
} else { // 24h mode
sprintf(buff, "HOURS [%02X] = %02X (%02X)", addr, data, data & 0x3F);
}
break;
case DS_DAY: {
const char wdays[8][4] = {"***", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
sprintf(buff, "DAY [%02X] = %02X (%s)", addr, data, wdays[data]);
} break;
case DS_DATE:
sprintf(buff, "DATE [%02X] = %02X", addr, data);
break;
case DS_MONTH:
sprintf(buff, "MONTH [%02X] = %02X", addr, data);
break;
case DS_YEAR:
sprintf(buff, "YEAR [%02X] = %02X", addr, data);
break;
case DS_CTRL:
sprintf(buff, "CONTROL [%02X] = %02X", addr, data);
break;
default:
sprintf(buff, "RAM [%02X] = %02X", addr, data);
break;
}
return buff;
}
#endif
void rtc_ds1307_rst(rtc_ds1307_t* rtc) {
bitbang_i2c_rst(&rtc->bb_i2c);
rtc->addr = 0;
rtc->ucont = 0;
rtc->alarm = 0;
rtc->backup_mday = 0;
dprintf("rtc rst\n");
}
static void rtc_ds1307_callback(void* arg) {
rtc_ds1307_t* rtc = (rtc_ds1307_t*)arg;
if ((rtc->data[0] & 0x80) == 0) {
/*
rtc->dtime.tm_sec++;
if(rtc->dtime.tm_sec == 60)
{
rtc->dtime.tm_sec =0;
rtc->dtime.tm_min++;
}
if(rtc->dtime.tm_min == 60)
{
rtc->dtime.tm_min=0;
rtc->dtime.tm_hour++;
}
if(rtc->dtime.tm_hour == 24)
{
rtc->dtime.tm_hour=0;
rtc->dtime.tm_mday++;
rtc->dtime.tm_wday++;
}
if(rtc->dtime.tm_wday == 7)rtc->dtime.tm_wday=0;
if(rtc->dtime.tm_mday == 31)
{
rtc->dtime.tm_mday=0;
rtc->dtime.tm_mon++;
}
if(rtc->dtime.tm_mon == 13)
{
rtc->dtime.tm_mon=1;
rtc->dtime.tm_year++;
}
*/
time_t utime = mktime(&rtc->dtime) + 1;
#ifdef _WIN_
localtime_s(&rtc->dtime, &utime);
#else
localtime_r(&utime, &rtc->dtime);
#endif
rtc->data[DS_SECONDS] = ((rtc->dtime.tm_sec / 10) << 4) | (rtc->dtime.tm_sec % 10);
rtc->data[DS_MINUTES] = ((rtc->dtime.tm_min / 10) << 4) | (rtc->dtime.tm_min % 10);
if (rtc->data[DS_HOURS] & 0x40) { // 12h mode
if (rtc->dtime.tm_hour < 12) {
int hour = rtc->dtime.tm_hour;
if (hour == 0) {
hour = 12;
}
rtc->data[DS_HOURS] = (rtc->data[DS_HOURS] & 0xC0) | ((((hour / 10) << 4) | (hour % 10)) & 0x1F);
} else {
int hour = rtc->dtime.tm_hour;
if (hour > 12) {
hour -= 12;
}
rtc->data[DS_HOURS] = (rtc->data[DS_HOURS] & 0xC0) | 0x20 | ((((hour / 10) << 4) | (hour % 10)) & 0x1F);
}
} else { // 24h mode
rtc->data[DS_HOURS] =
(rtc->data[DS_HOURS] & 0xC0) | ((((rtc->dtime.tm_hour / 10) << 4) | (rtc->dtime.tm_hour % 10)) & 0x3F);
}
rtc->data[DS_DAY] = rtc->dtime.tm_wday;
if (rtc->data[DS_DAY] == 0)
rtc->data[DS_DAY] = 7;
rtc->data[DS_DATE] = ((rtc->dtime.tm_mday / 10) << 4) | (rtc->dtime.tm_mday % 10);
rtc->data[DS_MONTH] = (((rtc->dtime.tm_mon + 1) / 10) << 4) | ((rtc->dtime.tm_mon + 1) % 10);
rtc->data[DS_YEAR] = (((rtc->dtime.tm_year % 100) / 10) << 4) | (rtc->dtime.tm_year % 10);
}
// alarm
rtc->alarm = 0;
/*
//minute
if((rtc->data[0x9]&0x80) == 0)
{
if((rtc->data[0x9]&0x7F)==(rtc->data[0x3]&0x7F))
{
alarm2=1;
}
else
{
alarm2=0;
}
}
//hour
if((rtc->data[0xA]&0x80) == 0)
{
if((rtc->data[0xA]&0x3F)==(rtc->data[0x4]&0x3F))
{
alarm2=1;
}
else
{
alarm2=0;
}
}
//day
if((rtc->data[0xB]&0x80) == 0)
{
if((rtc->data[0xB]&0x3F)==(rtc->data[0x5]&0x3F))
{
alarm2=1;
}
else
{
alarm2=0;
}
}
//week day
if((rtc->data[0xC]&0x80) == 0)
{
if((rtc->data[0xC]&0x07)==(rtc->data[0x6]&0x07))
{
alarm2=1;
}
else
{
alarm2=0;
}
}
if(alarm2)
{
rtc->data[0x2]|=0x08; //AF
//printf("RTC Alarm !\n");
//interrupt
if(rtc->data[0x2] & 0x02)
{
//printf("RTC Alarm Interrupt!\n");
}
}
*/
rtc->ucont++;
if (rtc->ucont == 10) {
#ifdef _DEBUG
printf("test sync... ");
#endif
time_t now = time(NULL);
long int dsys = now - rtc->systime;
long int drtc = rtc_ds1307_getUtime(rtc) - rtc->rtctime;
int drift = dsys - drtc;
#ifdef _DEBUG
printf("sys=%li rtc=%li drift=%li\n", dsys, drtc, dsys - drtc);
#endif
if (drift > 0) {
#ifdef _DEBUG
printf("resync ...\n");
#endif
rtc_ds1307_setUtime(rtc, rtc_ds1307_getUtime(rtc) + drift);
rtc->systime = now;
rtc->rtctime = rtc_ds1307_getUtime(rtc);
}
rtc->ucont = 0;
}
}
void rtc_ds1307_init(rtc_ds1307_t* rtc, board* pboard_) {
time_t utime;
dprintf("rtc init\n");
rtc->pboard = pboard_;
bitbang_i2c_init(&rtc->bb_i2c, 0x68);
memset(rtc->data, 0xFF, 8);
rtc->data[DS_HOURS] &= 0x3F; // 24h mode
rtc->data[DS_CTRL] = 0;
rtc_ds1307_rst(rtc);
utime = time(NULL);
rtc_ds1307_setUtime(rtc, utime);
rtc->TimerID = rtc->pboard->TimerRegister_ms(1000, rtc_ds1307_callback, rtc);
}
void rtc_ds1307_setUtime(rtc_ds1307_t* rtc, time_t utime) {
rtc->systime = time(NULL);
rtc->rtctime = utime;
rtc->ucont = 0;
#ifdef _WIN_
localtime_s(&rtc->dtime, &utime);
#else
localtime_r(&utime, &rtc->dtime);
#endif
rtc->data[DS_SECONDS] = ((rtc->dtime.tm_sec / 10) << 4) | (rtc->dtime.tm_sec % 10);
rtc->data[DS_MINUTES] = ((rtc->dtime.tm_min / 10) << 4) | (rtc->dtime.tm_min % 10);
if (rtc->data[DS_HOURS] & 0x40) { // 12h mode
if (rtc->dtime.tm_hour < 12) {
int hour = rtc->dtime.tm_hour;
if (hour == 0) {
hour = 12;
}
rtc->data[DS_HOURS] = (rtc->data[DS_HOURS] & 0xC0) | ((((hour / 10) << 4) | (hour % 10)) & 0x1F);
} else {
int hour = rtc->dtime.tm_hour;
if (hour > 12) {
hour -= 12;
}
rtc->data[DS_HOURS] = (rtc->data[DS_HOURS] & 0xC0) | 0x20 | ((((hour / 10) << 4) | (hour % 10)) & 0x1F);
}
} else { // 24h mode
rtc->data[DS_HOURS] =
(rtc->data[DS_HOURS] & 0xC0) | ((((rtc->dtime.tm_hour / 10) << 4) | (rtc->dtime.tm_hour % 10)) & 0x3F);
}
rtc->data[DS_DAY] = rtc->dtime.tm_wday;
if (rtc->data[DS_DAY] == 0)
rtc->data[DS_DAY] = 7;
rtc->data[DS_DATE] = ((rtc->dtime.tm_mday / 10) << 4) | (rtc->dtime.tm_mday % 10);
rtc->data[DS_MONTH] = (((rtc->dtime.tm_mon + 1) / 10) << 4) | ((rtc->dtime.tm_mon + 1) % 10);
rtc->data[DS_YEAR] = (((rtc->dtime.tm_year % 100) / 10) << 4) | (rtc->dtime.tm_year % 10);
}
time_t rtc_ds1307_getUtime(rtc_ds1307_t* rtc) {
return mktime(&rtc->dtime);
}
void rtc_ds1307_end(rtc_ds1307_t* rtc) {
dprintf("rtc end\n");
rtc->pboard->TimerUnregister(rtc->TimerID);
}
unsigned char rtc_ds1307_I2C_io(rtc_ds1307_t* rtc, unsigned char scl, unsigned char sda) {
unsigned char ret = bitbang_i2c_io(&rtc->bb_i2c, scl, sda);
switch (bitbang_i2c_get_status(&rtc->bb_i2c)) {
case I2C_DATAW:
if (rtc->bb_i2c.byte == 2) {
rtc->addr = rtc->bb_i2c.datar;
} else {
#ifdef _DEBUG
dprintf("> rtc write %s\n", get_addr_str(rtc->addr, rtc->bb_i2c.datar));
dprintf("====>old %s", asctime(&rtc->dtime));
#endif
rtc->data[rtc->addr] = rtc->bb_i2c.datar;
switch (rtc->addr) {
case DS_SECONDS:
rtc->dtime.tm_sec = (((rtc->bb_i2c.datar & 0xF0) >> 4) * 10) + (rtc->bb_i2c.datar & 0x0F);
break;
case DS_MINUTES:
rtc->dtime.tm_min = (((rtc->bb_i2c.datar & 0xF0) >> 4) * 10) + (rtc->bb_i2c.datar & 0x0F);
break;
case DS_HOURS:
if (rtc->bb_i2c.datar & 0x40) { // 12h mode
if (rtc->bb_i2c.datar & 0x20) { // PM
rtc->dtime.tm_hour =
(((rtc->bb_i2c.datar & 0x10) >> 4) * 10) + (rtc->bb_i2c.datar & 0x0F);
if (rtc->dtime.tm_hour < 12) {
rtc->dtime.tm_hour += 12;
}
} else { // AM
rtc->dtime.tm_hour =
(((rtc->bb_i2c.datar & 0x10) >> 4) * 10) + (rtc->bb_i2c.datar & 0x0F);
if (rtc->dtime.tm_hour == 12) {
rtc->dtime.tm_hour = 0;
}
}
} else { // 24h mode
rtc->dtime.tm_hour = (((rtc->bb_i2c.datar & 0x30) >> 4) * 10) + (rtc->bb_i2c.datar & 0x0F);
}
break;
case DS_DATE:
rtc->dtime.tm_mday = (((rtc->bb_i2c.datar & 0xF0) >> 4) * 10) + (rtc->bb_i2c.datar & 0x0F);
rtc->backup_mday = rtc->dtime.tm_mday;
break;
case DS_DAY: {
int wday = rtc->bb_i2c.datar;
if (wday == 7)
wday = 0;
rtc->dtime.tm_wday = (((wday & 0xF0) >> 4) * 10) + (wday & 0x0F);
} break;
case DS_MONTH:
rtc->dtime.tm_mon =
((((rtc->bb_i2c.datar - 1) & 0xF0) >> 4) * 10) + ((rtc->bb_i2c.datar - 1) & 0x0F);
if (rtc->backup_mday) {
rtc->dtime.tm_mday = rtc->backup_mday;
rtc->backup_mday = 0;
}
break;
case DS_YEAR:
rtc->dtime.tm_year = (rtc->dtime.tm_year - (rtc->dtime.tm_year % 100)) +
((((rtc->bb_i2c.datar & 0xF0) >> 4) * 10) + (rtc->bb_i2c.datar & 0x0F));
break;
default:
break;
}
dprintf("====>new %s", asctime(&rtc->dtime));
rtc->addr++;
if (rtc->addr > 63) {
rtc->addr -= 64;
}
rtc->systime = time(NULL);
rtc->rtctime = rtc_ds1307_getUtime(rtc);
#ifdef _DEBUG
struct tm ddtime;
#ifdef _WIN_
localtime_s(&ddtime, &rtc->rtctime);
#else
localtime_r(&rtc->rtctime, &ddtime);
#endif
dprintf("====>rtc %s\n", asctime(&ddtime));
#endif
rtc->ucont = 0;
}
break;
case I2C_DATAR:
bitbang_i2c_send(&rtc->bb_i2c, rtc->data[rtc->addr]);
#ifdef _DEBUG
dprintf("< rtc read %s\n", get_addr_str(rtc->addr, rtc->data[rtc->addr]));
if (rtc->addr == DS_YEAR) {
dprintf("====>date %s", asctime(&rtc->dtime));
}
#endif
rtc->addr++;
if (rtc->addr > 63) {
rtc->addr -= 64;
}
break;
}
return ret;
}
// TODO int output