-
Notifications
You must be signed in to change notification settings - Fork 33
/
RtcDS3231.cpp
349 lines (271 loc) · 8.54 KB
/
RtcDS3231.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
#if defined(ESP8266)
#include <pgmspace.h>
#else
#include <avr/pgmspace.h>
#endif
#include <Wire.h>
#include "RtcUtility.h"
#include "RtcDS3231.h"
#define Write(x) write(static_cast<uint8_t>(x))
#define Read(x) read(x)
//I2C Slave Address
#define DS3231_ADDRESS 0x68
//DS3231 Register Addresses
#define DS3231_REG_TIMEDATE 0x00
#define DS3231_REG_ALARMONE 0x07
#define DS3231_REG_ALARMTWO 0x0B
#define DS3231_REG_CONTROL 0x0E
#define DS3231_REG_STATUS 0x0F
#define DS3231_REG_AGING 0x10
#define DS3231_REG_TEMP 0x11
// DS3231 Control Register Bits
#define DS3231_A1IE 0
#define DS3231_A2IE 1
#define DS3231_INTCN 2
#define DS3231_RS1 3
#define DS3231_RS2 4
#define DS3231_CONV 5
#define DS3231_BBSQW 6
#define DS3231_EOSC 7
#define DS3231_AIEMASK (_BV(DS3231_A1IE) | _BV(DS3231_A2IE))
#define DS3231_RSMASK (_BV(DS3231_RS1) | _BV(DS3231_RS2))
// DS3231 Status Register Bits
#define DS3231_A1F 0
#define DS3231_A2F 1
#define DS3231_BSY 2
#define DS3231_EN32KHZ 3
#define DS3231_OSF 7
#define DS3231_AIFMASK (_BV(DS3231_A1F) | _BV(DS3231_A2F))
void RtcDS3231::Begin()
{
Wire.begin(5,4);
}
bool RtcDS3231::IsDateTimeValid()
{
uint8_t status = getReg(DS3231_REG_STATUS);
return !(status & _BV(DS3231_OSF));
}
bool RtcDS3231::GetIsRunning()
{
uint8_t creg = getReg(DS3231_REG_CONTROL);
return !(creg & _BV(DS3231_EOSC));
}
void RtcDS3231::SetIsRunning(bool isRunning)
{
uint8_t creg = getReg(DS3231_REG_CONTROL);
if (isRunning)
{
creg &= ~_BV(DS3231_EOSC);
}
else
{
creg |= _BV(DS3231_EOSC);
}
setReg(DS3231_REG_CONTROL, creg);
}
void RtcDS3231::SetDateTime(const RtcDateTime& dt)
{
// clear the invalid flag
uint8_t status = getReg(DS3231_REG_STATUS);
status &= ~_BV(DS3231_OSF); // clear the flag
setReg(DS3231_REG_STATUS, status);
// set the date time
Wire.beginTransmission(DS3231_ADDRESS);
Wire.Write(DS3231_REG_TIMEDATE);
Wire.Write(Uint8ToBcd(dt.Second()));
Wire.Write(Uint8ToBcd(dt.Minute()));
Wire.Write(Uint8ToBcd(dt.Hour())); // 24 hour mode only
uint8_t year = dt.Year() - 2000;
uint8_t centuryFlag = 0;
if (year >= 100)
{
year -= 100;
centuryFlag = _BV(7);
}
Wire.Write(Uint8ToBcd(dt.DayOfWeek()));
Wire.Write(Uint8ToBcd(dt.Day()));
Wire.Write(Uint8ToBcd(dt.Month()) | centuryFlag);
Wire.Write(Uint8ToBcd(year));
Wire.endTransmission();
}
RtcDateTime RtcDS3231::GetDateTime()
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.Write(DS3231_REG_TIMEDATE);
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 7);
uint8_t second = BcdToUint8(Wire.Read() & 0x7F);
uint8_t minute = BcdToUint8(Wire.Read());
uint8_t hour = BcdToBin24Hour(Wire.Read());
Wire.Read(); // throwing away day of week as we calculate it
uint8_t dayOfMonth = BcdToUint8(Wire.Read());
uint8_t monthRaw = Wire.Read();
uint16_t year = BcdToUint8(Wire.Read()) + 2000;
if (monthRaw & _BV(7)) // century wrap flag
{
year += 100;
}
uint8_t month = BcdToUint8(monthRaw & 0x7f);
return RtcDateTime(year, month, dayOfMonth, hour, minute, second);
}
RtcTemperature RtcDS3231::GetTemperature()
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.Write(DS3231_REG_TEMP);
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 2);
int8_t degrees = Wire.Read();
// fraction is just the upper bits
// representing 1/4 of a degree
uint8_t fract = (Wire.Read() >> 6) * 25;
return RtcTemperature(degrees, fract);
}
void RtcDS3231::Enable32kHzPin(bool enable)
{
uint8_t sreg = getReg(DS3231_REG_STATUS);
if (enable == true)
{
sreg |= _BV(DS3231_EN32KHZ);
}
else
{
sreg &= ~_BV(DS3231_EN32KHZ);
}
setReg(DS3231_REG_STATUS, sreg);
}
void RtcDS3231::SetSquareWavePin(DS3231SquareWavePinMode pinMode)
{
uint8_t creg = getReg(DS3231_REG_CONTROL);
// clear all relevant bits to a known "off" state
creg &= ~(DS3231_AIEMASK | _BV(DS3231_BBSQW));
creg |= _BV(DS3231_INTCN); // set INTCN to disables SQW
switch (pinMode)
{
case DS3231SquareWavePin_ModeNone:
break;
case DS3231SquareWavePin_ModeBatteryBackup:
creg |= _BV(DS3231_BBSQW); // set battery backup flag
creg &= ~_BV(DS3231_INTCN); // clear INTCN to enable SQW
break;
case DS3231SquareWavePin_ModeClock:
creg &= ~_BV(DS3231_INTCN); // clear INTCN to enable SQW
break;
case DS3231SquareWavePin_ModeAlarmOne:
creg |= _BV(DS3231_A1IE);
break;
case DS3231SquareWavePin_ModeAlarmTwo:
creg |= _BV(DS3231_A2IE);
break;
case DS3231SquareWavePin_ModeAlarmBoth:
creg |= _BV(DS3231_A1IE) | _BV(DS3231_A2IE);
break;
}
setReg(DS3231_REG_CONTROL, creg);
}
void RtcDS3231::SetSquareWavePinClockFrequency(DS3231SquareWaveClock freq)
{
uint8_t creg = getReg(DS3231_REG_CONTROL);
creg &= ~DS3231_RSMASK; // Set to 0
creg |= (freq & DS3231_RSMASK); // Set freq bits
setReg(DS3231_REG_CONTROL, creg);
}
void RtcDS3231::SetAlarmOne(const DS3231AlarmOne& alarm)
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.Write(DS3231_REG_ALARMONE);
Wire.Write(Uint8ToBcd(alarm.Second()) | ((alarm.ControlFlags() & 0x01) << 7));
Wire.Write(Uint8ToBcd(alarm.Minute()) | ((alarm.ControlFlags() & 0x02) << 6));
Wire.Write(Uint8ToBcd(alarm.Hour()) | ((alarm.ControlFlags() & 0x04) << 5)); // 24 hour mode only
Wire.Write(Uint8ToBcd(alarm.DayOf()) | ((alarm.ControlFlags() & 0x18) << 3));
Wire.endTransmission();
}
void RtcDS3231::SetAlarmTwo(const DS3231AlarmTwo& alarm)
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.Write(DS3231_REG_ALARMTWO);
Wire.Write(Uint8ToBcd(alarm.Minute()) | ((alarm.ControlFlags() & 0x01) << 7));
Wire.Write(Uint8ToBcd(alarm.Hour()) | ((alarm.ControlFlags() & 0x02) << 6)); // 24 hour mode only
Wire.Write(Uint8ToBcd(alarm.DayOf()) | ((alarm.ControlFlags() & 0x0c) << 4));
Wire.endTransmission();
}
DS3231AlarmOne RtcDS3231::GetAlarmOne()
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.Write(DS3231_REG_ALARMONE);
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 4);
uint8_t raw = Wire.Read();
uint8_t flags = (raw & 0x80) >> 7;
uint8_t second = BcdToUint8(raw & 0x7F);
raw = Wire.Read();
flags |= (raw & 0x80) >> 6;
uint8_t minute = BcdToUint8(raw & 0x7F);
raw = Wire.Read();
flags |= (raw & 0x80) >> 5;
uint8_t hour = BcdToBin24Hour(raw & 0x7f);
raw = Wire.Read();
flags |= (raw & 0xc0) >> 3;
uint8_t dayOf = BcdToUint8(raw & 0x3f);
return DS3231AlarmOne(dayOf, hour, minute, second, (DS3231AlarmOneControl)flags);
}
DS3231AlarmTwo RtcDS3231::GetAlarmTwo()
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.Write(DS3231_REG_ALARMTWO);
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 3);
uint8_t raw = Wire.Read();
uint8_t flags = (raw & 0x80) >> 7;
uint8_t minute = BcdToUint8(raw & 0x7F);
raw = Wire.Read();
flags |= (raw & 0x80) >> 6;
uint8_t hour = BcdToBin24Hour(raw & 0x7f);
raw = Wire.Read();
flags |= (raw & 0xc0) >> 4;
uint8_t dayOf = BcdToUint8(raw & 0x3f);
return DS3231AlarmTwo(dayOf, hour, minute, (DS3231AlarmTwoControl)flags);
}
DS3231AlarmFlag RtcDS3231::LatchAlarmsTriggeredFlags()
{
uint8_t sreg = getReg(DS3231_REG_STATUS);
uint8_t alarmFlags = (sreg & DS3231_AIFMASK);
sreg &= ~DS3231_AIFMASK; // clear the flags
setReg(DS3231_REG_STATUS, sreg);
return (DS3231AlarmFlag)alarmFlags;
}
void RtcDS3231::ForceTemperatureCompensationUpdate(bool block)
{
uint8_t creg = getReg(DS3231_REG_CONTROL);
creg |= _BV(DS3231_CONV); // Write CONV bit
setReg(DS3231_REG_CONTROL, creg);
while (block && (creg & _BV(DS3231_CONV)) != 0)
{
// Block until CONV is 0
creg = getReg(DS3231_REG_CONTROL);
}
}
int8_t RtcDS3231::GetAgingOffset()
{
return getReg(DS3231_REG_AGING);
}
void RtcDS3231::SetAgingOffset(int8_t value)
{
setReg(DS3231_REG_AGING, value);
}
uint8_t RtcDS3231::getReg(uint8_t regAddress)
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.Write(regAddress);
Wire.endTransmission();
// control register
Wire.requestFrom(DS3231_ADDRESS, 1);
uint8_t regValue = Wire.Read();
return regValue;
}
void RtcDS3231::setReg(uint8_t regAddress, uint8_t regValue)
{
Wire.beginTransmission(DS3231_ADDRESS);
Wire.Write(regAddress);
Wire.Write(regValue);
Wire.endTransmission();
}