-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathds3234.cpp
395 lines (333 loc) · 9.55 KB
/
ds3234.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
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
#include <SPI.h>
#include <stdio.h>
#include "ds3234.h"
#ifdef __AVR__
#include <avr/pgmspace.h>
// Workaround for http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34734
#ifdef PROGMEM
#undef PROGMEM
#define PROGMEM __attribute__((section(".progmem.data")))
#endif
#else
#define PROGMEM
//#define pgm_read_byte(addr) (*(const uint8_t *)(addr))
#endif
/* control register 0Eh/8Eh
bit7 EOSC Enable Oscillator (1 if oscillator must be stopped when on battery)
bit6 BBSQW Battery Backed Square Wave
bit5 CONV Convert temperature (1 forces a conversion NOW)
bit4 RS2 Rate select - frequency of square wave output
bit3 RS1 Rate select
bit2 INTCN Interrupt control (1 for use of the alarms and to disable square wave)
bit1 A2IE Alarm2 interrupt enable (1 to enable)
bit0 A1IE Alarm1 interrupt enable (1 to enable)
*/
void DS3234_init(const uint8_t pin, const uint8_t ctrl_reg)
{
pinMode(pin, OUTPUT); // chip select pin
SPI.begin();
//SPI.setBitOrder(MSBFIRST);
//SPI.setDataMode(SPI_MODE1);
DS3234_set_creg(pin, ctrl_reg);
delay(10);
}
void DS3234_set(const uint8_t pin, struct ts t)
{
uint8_t i, century;
if (t.year > 2000) {
century = 0x80;
t.year_s = t.year - 2000;
} else {
century = 0;
t.year_s = t.year - 1900;
}
SPI.beginTransaction(SPISettings(pin, MSBFIRST, SPI_MODE1));
uint8_t TimeDate[7] = { t.sec, t.min, t.hour, t.wday, t.mday, t.mon, t.year_s };
for (i = 0; i <= 6; i++) {
digitalWrite(pin, LOW);
SPI.transfer(i + 0x80);
if (i == 5)
SPI.transfer(dectobcd(TimeDate[5]) + century);
else
SPI.transfer(dectobcd(TimeDate[i]));
digitalWrite(pin, HIGH);
}
SPI.endTransaction();
}
void DS3234_get(const uint8_t pin, struct ts *t)
{
uint8_t TimeDate[7]; //second,minute,hour,dow,day,month,year
uint8_t century = 0;
uint8_t i, n;
uint16_t year_full;
SPI.beginTransaction(SPISettings(pin, MSBFIRST, SPI_MODE1));
for (i = 0; i <= 6; i++) {
digitalWrite(pin, LOW);
SPI.transfer(i + 0x00);
n = SPI.transfer(0x00);
digitalWrite(pin, HIGH);
if (i == 5) { // month address also contains the century on bit7
TimeDate[5] = bcdtodec(n & 0x1F);
century = (n & 0x80) >> 7;
} else {
TimeDate[i] = bcdtodec(n);
}
}
SPI.endTransaction();
if (century == 1)
year_full = 2000 + TimeDate[6];
else
year_full = 1900 + TimeDate[6];
t->sec = TimeDate[0];
t->min = TimeDate[1];
t->hour = TimeDate[2];
t->mday = TimeDate[4];
t->mon = TimeDate[5];
t->year = year_full;
t->wday = TimeDate[3];
t->year_s = TimeDate[6];
#ifdef CONFIG_UNIXTIME
t->unixtime = get_unixtime(*t);
#endif
}
void DS3234_set_addr(const uint8_t pin, const uint8_t addr, const uint8_t val)
{
SPI.beginTransaction(SPISettings(pin, MSBFIRST, SPI_MODE1));
digitalWrite(pin, LOW);
SPI.transfer(addr);
SPI.transfer(val);
digitalWrite(pin, HIGH);
SPI.endTransaction();
}
uint8_t DS3234_get_addr(const uint8_t pin, const uint8_t addr)
{
uint8_t rv;
SPI.beginTransaction(SPISettings(pin, MSBFIRST, SPI_MODE1));
digitalWrite(pin, LOW);
SPI.transfer(addr);
rv = SPI.transfer(0x00);
digitalWrite(pin, HIGH);
SPI.endTransaction();
return rv;
}
// control register
void DS3234_set_creg(const uint8_t pin, const uint8_t val)
{
DS3234_set_addr(pin, 0x8E, val);
}
// status register 0Fh/8Fh
/*
bit7 OSF Oscillator Stop Flag (if 1 then oscillator has stopped and date might be innacurate)
bit6 BB32kHz Battery Backed 32kHz output (1 if square wave is needed when powered by battery)
bit5 CRATE1 Conversion rate 1 temperature compensation rate
bit4 CRATE0 Conversion rate 0 temperature compensation rate
bit3 EN32kHz Enable 32kHz output (1 if needed)
bit2 BSY Busy with TCXO functions
bit1 A2F Alarm 2 Flag - (1 if alarm2 was triggered)
bit0 A1F Alarm 1 Flag - (1 if alarm1 was triggered)
*/
void DS3234_set_sreg(const uint8_t pin, const uint8_t sreg)
{
DS3234_set_addr(pin, 0x8F, sreg);
}
uint8_t DS3234_get_sreg(const uint8_t pin)
{
uint8_t rv;
rv = DS3234_get_addr(pin, 0x0f);
return rv;
}
// aging register
void DS3234_set_aging(const uint8_t pin, const int8_t value)
{
uint8_t reg;
if (value >= 0)
reg = value;
else
reg = ~(-value) + 1; // 2C
DS3234_set_addr(pin, 0x90, reg);
}
int8_t DS3234_get_aging(const uint8_t pin)
{
uint8_t reg;
int8_t rv;
reg = DS3234_get_addr(pin, 0x10);
if ((reg & 0x80) != 0)
rv = reg | ~((1 << 8) - 1); // if negative get two's complement
else
rv = reg;
return rv;
}
// temperature register
float DS3234_get_treg(const uint8_t pin)
{
float rv;
uint8_t temp_msb, temp_lsb;
int8_t nint;
temp_msb = DS3234_get_addr(pin, 0x11);
temp_lsb = DS3234_get_addr(pin, 0x12) >> 6;
if ((temp_msb & 0x80) != 0)
nint = temp_msb | ~((1 << 8) - 1); // if negative get two's complement
else
nint = temp_msb;
rv = 0.25 * temp_lsb + nint;
return rv;
}
// alarms
// flags are: A1M1 (seconds), A1M2 (minutes), A1M3 (hour),
// A1M4 (day) 0 to enable, 1 to disable, DY/DT (dayofweek == 1/dayofmonth == 0)
void DS3234_set_a1(const uint8_t pin, const uint8_t s, const uint8_t mi, const uint8_t h,
const uint8_t d, const uint8_t * flags)
{
uint8_t t[4] = { s, mi, h, d };
uint8_t i;
SPI.beginTransaction(SPISettings(pin, MSBFIRST, SPI_MODE1));
for (i = 0; i <= 3; i++) {
digitalWrite(pin, LOW);
SPI.transfer(i + 0x87);
if (i == 3) {
SPI.transfer(dectobcd(t[3]) | (flags[3] << 7) | (flags[4] << 6));
} else
SPI.transfer(dectobcd(t[i]) | (flags[i] << 7));
digitalWrite(pin, HIGH);
}
SPI.endTransaction();
}
void DS3234_get_a1(const uint8_t pin, char *buf, const uint8_t len)
{
uint8_t n[4];
uint8_t t[4]; //second,minute,hour,day
uint8_t f[5]; // flags
uint8_t i;
SPI.beginTransaction(SPISettings(pin, MSBFIRST, SPI_MODE1));
for (i = 0; i <= 3; i++) {
digitalWrite(pin, LOW);
SPI.transfer(i + 0x07);
n[i] = SPI.transfer(0x00);
digitalWrite(pin, HIGH);
f[i] = (n[i] & 0x80) >> 7;
t[i] = bcdtodec(n[i] & 0x7F);
}
SPI.endTransaction();
f[4] = (n[3] & 0x40) >> 6;
t[3] = bcdtodec(n[3] & 0x3F);
snprintf(buf, len,
"s%02d m%02d h%02d d%02d fs%d m%d h%d d%d wm%d %d %d %d %d",
t[0], t[1], t[2], t[3], f[0], f[1], f[2], f[3], f[4], n[0],
n[1], n[2], n[3]);
}
// when the alarm flag is cleared the pulldown on INT is also released
void DS3234_clear_a1f(const uint8_t pin)
{
uint8_t reg_val;
reg_val = DS3234_get_sreg(pin) & ~DS3234_A1F;
DS3234_set_sreg(pin, reg_val);
}
uint8_t DS3234_triggered_a1(const uint8_t pin)
{
return DS3234_get_sreg(pin) & DS3234_A1F;
}
// flags are: A2M2 (minutes), A2M3 (hour), A2M4 (day) 0 to enable, 1 to disable, DY/DT (dayofweek == 1/dayofmonth == 0) -
void DS3234_set_a2(const uint8_t pin, const uint8_t mi, const uint8_t h, const uint8_t d,
const uint8_t * flags)
{
uint8_t t[3] = { mi, h, d };
uint8_t i;
SPI.beginTransaction(SPISettings(pin, MSBFIRST, SPI_MODE1));
for (i = 0; i <= 2; i++) {
digitalWrite(pin, LOW);
SPI.transfer(i + 0x8B);
if (i == 2) {
SPI.transfer(dectobcd(t[2]) | (flags[2] << 7) | (flags[3] << 6));
} else
SPI.transfer(dectobcd(t[i]) | (flags[i] << 7));
digitalWrite(pin, HIGH);
}
SPI.endTransaction();
}
void DS3234_get_a2(const uint8_t pin, char *buf, const uint8_t len)
{
uint8_t n[3];
uint8_t t[3]; //second,minute,hour,day
uint8_t f[4]; // flags
uint8_t i;
SPI.beginTransaction(SPISettings(pin, MSBFIRST, SPI_MODE1));
for (i = 0; i <= 2; i++) {
digitalWrite(pin, LOW);
SPI.transfer(i + 0x0B);
n[i] = SPI.transfer(0x00);
digitalWrite(pin, HIGH);
f[i] = (n[i] & 0x80) >> 7;
t[i] = bcdtodec(n[i] & 0x7F);
}
SPI.endTransaction();
f[3] = (n[2] & 0x40) >> 6;
t[2] = bcdtodec(n[2] & 0x3F);
snprintf(buf, len, "m%02d h%02d d%02d fm%d h%d d%d wm%d %d %d %d", t[0],
t[1], t[2], f[0], f[1], f[2], f[3], n[0], n[1], n[2]);
}
// when the alarm flag is cleared the pulldown on INT is also released
void DS3234_clear_a2f(const uint8_t pin)
{
uint8_t reg_val;
reg_val = DS3234_get_sreg(pin) & ~DS3234_A2F;
DS3234_set_sreg(pin, reg_val);
}
uint8_t DS3234_triggered_a2(const uint8_t pin)
{
return DS3234_get_sreg(pin) & DS3234_A2F;
}
// sram
void DS3234_set_sram_8b(const uint8_t pin, const uint8_t address, const uint8_t value)
{
DS3234_set_addr(pin, 0x98, address);
DS3234_set_addr(pin, 0x99, value);
}
uint8_t DS3234_get_sram_8b(const uint8_t pin, const uint8_t address)
{
uint8_t rv;
DS3234_set_addr(pin, 0x98, address);
rv = DS3234_get_addr(pin, 0x19);
return rv;
}
// helpers
#ifdef CONFIG_UNIXTIME
const uint8_t days_in_month [12] PROGMEM = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// returns the number of seconds since 01.01.1970 00:00:00 UTC, valid for 2000..FIXME
uint32_t get_unixtime(struct ts t)
{
uint8_t i;
uint16_t d;
int16_t y;
uint32_t rv;
if (t.year >= 2000) {
y = t.year - 2000;
} else {
return 0;
}
d = t.mday - 1;
for (i = 1; i < t.mon; i++) {
d += pgm_read_byte(days_in_month + i - 1);
}
if (t.mon > 2 && y % 4 == 0) {
d++;
}
// count leap days
d += (365 * y + (y + 3) / 4);
rv = ((d * 24UL + t.hour) * 60 + t.min) * 60 + t.sec + SECONDS_FROM_1970_TO_2000;
return rv;
}
#endif
uint8_t dectobcd(const uint8_t val)
{
return ((val / 10 * 16) + (val % 10));
}
uint8_t bcdtodec(const uint8_t val)
{
return ((val / 16 * 10) + (val % 16));
}
uint8_t inp2toi(const char *cmd, const uint16_t seek)
{
uint8_t rv;
rv = (cmd[seek] - 48) * 10 + cmd[seek + 1] - 48;
return rv;
}