This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BudikRTC.cpp
337 lines (275 loc) · 9.02 KB
/
BudikRTC.cpp
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
#include <WProgram.h>
#include <Wire.h>
#include "config.h"
#include "BudikRTC.h"
#include "utils.h"
#include "Streaming.h"
#include "BudikInterfaces.h"
void setAddr(uint8_t a2, uint8_t a1, uint8_t a0)
{
static uint8_t b2 = 0x1;
static uint8_t b1 = 0xFF;
static uint8_t b0 = 0x0;
uint8_t lcdp;
if(b0!=a0){
writeI2CData(I2CADDR, 0x0, a0);
b0 = a0;
}
if(b1!=a1){
writeI2CData(I2CADDR, 0x1, a1);
b1 = a1;
}
a2 &= 0x1;
if(b2!=a2){
// highest bit is on the lcd port
Wire.beginTransmission(I2CDATA);
Wire.send(I2CLCD);
Wire.endTransmission();
// read old value and changle only one bit
Wire.requestFrom(I2CDATA, 1);
delayMicroseconds(I2CWRITEDELAYUS);
lcdp = Wire.receive() & 0x7F;
writeI2CData(I2CDATA, I2CLCD, lcdp | ((a2 & 0x1) << 7));
//
b2 = a2;
}
}
int readI2CMux()
{
delayMicroseconds(I2CREADDELAYUS); // standard delay of the I2C chip
// query
Wire.beginTransmission(I2CDATA);
Wire.send(I2CRTC);
Wire.endTransmission();
// reply
Wire.requestFrom(I2CDATA, 1);
delayMicroseconds(I2CWRITEDELAYUS);
return Wire.receive();
}
// Switch to mode true -> in / false -> out
void dirI2CMux0(boolean input)
{
digitalWrite(WE, 1);
digitalWrite(OE, (input) ? 0 : 1);
Wire.beginTransmission(I2CDATA);
Wire.send(I2CRTC+0x06);
Wire.send((input)?0xFF:0x00);
Wire.endTransmission();
}
void writeI2CMux0(byte data)
{
// Write
Wire.beginTransmission(I2CDATA);
Wire.send(I2CRTC);
Wire.send(data);
Wire.endTransmission();
}
void writeI2CData(int address, byte block, byte data)
{
// Write
Wire.beginTransmission(address);
Wire.send(block);
Wire.send(data);
Wire.endTransmission();
}
#define RTC_ADDR2 0x1
#define RTC_ADDR1 0xFF
#define RTC_SECOND 0xF9
#define RTC_MINUTE 0xFA
#define RTC_HOUR 0xFB
#define RTC_DOW 0xFC
#define RTC_DAY 0xFD
#define RTC_MONTH 0xFE
#define RTC_YEAR 0xFF
#define RTC_CENTURY 0xF8
// mode 0 - normal
// mode 1 - force refresh
// mode 2 - 1 + onelined
// mode 3 - 1 + month, year and century
TimeValue readTime(bool fullmode)
{
static TimeValue ts;
byte* vars[] = {&(ts.second), &(ts.minute), &(ts.hour), &(ts.dow), &(ts.day), &(ts.month), &(ts.year), &(ts.century)};
int addrs[] = {RTC_SECOND, RTC_MINUTE, RTC_HOUR, RTC_DOW,
RTC_DAY, RTC_MONTH, RTC_YEAR, RTC_CENTURY};
byte i;
int val;
dirI2CMux0(true);
for(i=0; i<8; i++){
setAddr(RTC_ADDR2, RTC_ADDR1, addrs[i]);
delayMicroseconds(I2CREADDELAYUS);
val = readI2CMux();
if(!fullmode && *(vars[i]) == val) break;
else *(vars[i]) = val;
}
ts.second = ts.second & 0x7F;
ts.century = ts.century & 0x3F;
ts.dow = ts.dow & 0x7;
return ts;
}
void writeTime(TimeValue &ts)
{
byte* vars[] = {&(ts.second), &(ts.minute), &(ts.hour), &(ts.dow), &(ts.day), &(ts.month), &(ts.year), &(ts.century)};
int addrs[] = {RTC_SECOND, RTC_MINUTE, RTC_HOUR, RTC_DOW,
RTC_DAY, RTC_MONTH, RTC_YEAR, RTC_CENTURY};
byte i;
dirI2CMux0(true);
setAddr(RTC_ADDR2, RTC_ADDR1, RTC_CENTURY);
i = readI2CMux();
dirI2CMux0(false);
writeI2CMux0(i | 0x80); // set write flag
digitalWrite(WE, 0);
delayMicroseconds(1);
digitalWrite(WE, 1);
for(i=0; i<8; i++){
setAddr(RTC_ADDR2, RTC_ADDR1, addrs[i]);
delayMicroseconds(I2CREADDELAYUS);
writeI2CMux0(*(vars[i]));
digitalWrite(WE, 0);
delayMicroseconds(1);
digitalWrite(WE, 1);
}
// write flag is reset by the century write which is the last one
dirI2CMux0(true);
}
#define ALARM_ADDR2 0x1
#define ALARM_ADDR1 0xFE
#define ALARM_ADDR0 0x00
#define ALARM_NEXT_SHIFT 2
#define ALARM_MASK (0xFF >> ALARM_NEXT_SHIFT)
#define ALARM_DOW 0
#define ALARM_HOUR 1
#define ALARM_MINUTE 2
#define ALARM_FLAGS 3
AlarmValue readAlarm(uint8_t alidx)
{
AlarmValue v;
uint8_t dow_en;
dirI2CMux0(true);
alidx &= ALARM_MASK;
v.id = alidx;
// DOW + EN bits
setAddr(ALARM_ADDR2, ALARM_ADDR1, ALARM_ADDR0 + ALARM_DOW + (alidx << ALARM_NEXT_SHIFT));
dow_en = readI2CMux();
v.en = dow_en & 0x1;
v.dow = dow_en >> 1;
// Hour
setAddr(ALARM_ADDR2, ALARM_ADDR1, ALARM_ADDR0 + ALARM_HOUR + (alidx << ALARM_NEXT_SHIFT));
v.hour = readI2CMux();
// Minute
setAddr(ALARM_ADDR2, ALARM_ADDR1, ALARM_ADDR0 + ALARM_MINUTE + (alidx << ALARM_NEXT_SHIFT));
v.minute = readI2CMux();
// Flags
setAddr(ALARM_ADDR2, ALARM_ADDR1, ALARM_ADDR0 + ALARM_FLAGS + (alidx << ALARM_NEXT_SHIFT));
v.flags = readI2CMux() & 0b111;
return v;
}
uint8_t findNextAlarm(TimeValue &tv, AlarmValue *upcoming)
{
uint8_t idx, skip = 0, isupcoming = 0;
uint8_t dowidx, dowcount;
AlarmValue alarm;
for(idx=0; idx<ALARM_MAX; idx++){
alarm = readAlarm(idx);
// alarm is not enabled or does not happen on any day
if(!alarm.en || !alarm.dow) continue;
skip = 0;
isupcoming++;
//first alarm is automatically upcoming
if(isupcoming==1){
*upcoming = alarm;
continue;
}
// test if alarm can happen (have happened) today
dowidx = tv.dow - 1;
if((alarm.dow & _BV(dowidx))){
// alarm happens later today
if((alarm.hour>tv.hour) ||
(alarm.hour == tv.hour && alarm.minute>tv.minute)){
// test if upcoming does not happen today or today, but later than alarm
if(!(upcoming->dow & _BV(dowidx)) ||
(upcoming->hour<tv.hour || alarm.hour<upcoming->hour) ||
(alarm.hour == upcoming->hour && alarm.minute<upcoming->minute)){
*upcoming = alarm;
continue;
}
}
// alarm already happened today
else{
// test if upcoming happens later today
if((upcoming->dow & _BV(dowidx)) && (tv.hour<upcoming->hour ||
(tv.hour == upcoming->hour && tv.minute < upcoming->minute))){
continue;
}
}
}
else{
// test if upcoming happens later today
if((upcoming->dow & _BV(dowidx)) && (tv.hour<upcoming->hour ||
(tv.hour == upcoming->hour && tv.minute < upcoming->minute))){
continue;
}
}
//both tested alarm and upcoming does not happen till later day
for(dowcount = 1; dowcount < 7; dowcount++){
dowidx = (tv.dow - 1 + dowcount) % 7;
// alarm does not happen on tested day, but upcoming is
if((upcoming->dow & _BV(dowidx)) && (!(alarm.dow & _BV(dowidx)))){
skip++;
break;
}
// alarm does happen on tested day, upcoming isn't
if(!(upcoming->dow & _BV(dowidx)) && (alarm.dow & _BV(dowidx))){
*upcoming = alarm;
skip++;
break;
}
// both happen on tested day
if(alarm.dow & _BV(dowidx)){
break;
}
// neither happened on tested day, test another day
}
if(skip) continue;
//at this point, both upcoming and alarm happen on the same day
//tested alarm happens on later hour
if(upcoming->hour < alarm.hour){
continue;
}
//tested alarm happens on the same hour but the same or later minute
if(upcoming->hour==alarm.hour && upcoming->minute < alarm.minute){
continue;
}
//tested alarm happens earlier after all..
*upcoming = alarm;
}
return isupcoming;
}
void writeAlarm(uint8_t alidx, AlarmValue &v)
{
dirI2CMux0(false);
// DOW + EN bits
setAddr(ALARM_ADDR2, ALARM_ADDR1, ALARM_ADDR0 + ALARM_DOW + (alidx << ALARM_NEXT_SHIFT));
writeI2CData(I2CDATA, I2CRTC, (v.dow << 1) + v.en);
digitalWrite(WE, 0);
delayMicroseconds(I2CWRITEDELAYUS);
digitalWrite(WE, 1);
// Hour
setAddr(ALARM_ADDR2, ALARM_ADDR1, ALARM_ADDR0 + ALARM_HOUR + (alidx << ALARM_NEXT_SHIFT));
writeI2CData(I2CDATA, I2CRTC, v.hour);
digitalWrite(WE, 0);
delayMicroseconds(I2CWRITEDELAYUS);
digitalWrite(WE, 1);
// Minute
setAddr(ALARM_ADDR2, ALARM_ADDR1, ALARM_ADDR0 + ALARM_MINUTE + (alidx << ALARM_NEXT_SHIFT));
writeI2CData(I2CDATA, I2CRTC, v.minute);
digitalWrite(WE, 0);
delayMicroseconds(I2CWRITEDELAYUS);
digitalWrite(WE, 1);
// Flags
setAddr(ALARM_ADDR2, ALARM_ADDR1, ALARM_ADDR0 + ALARM_FLAGS + (alidx << ALARM_NEXT_SHIFT));
writeI2CData(I2CDATA, I2CRTC, v.flags);
digitalWrite(WE, 0);
delayMicroseconds(I2CWRITEDELAYUS);
digitalWrite(WE, 1);
}