-
Notifications
You must be signed in to change notification settings - Fork 5
/
RTCC_MCP7940N.h
359 lines (307 loc) · 7.59 KB
/
RTCC_MCP7940N.h
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
/* Part of RTCC (MCP7940N) Arduino library
* Copyright (C) 2017 Kane Wallmann
* See LICENCE.txt for license (MIT)*/
#ifndef __RTCC_MCP7940N_H
#define __RTCC_MCP7940N_H
#include <Arduino.h>
#include <Wire.h>
#include <time.h>
typedef enum
{
RTCC_SQWFS_1HZ,
RTCC_SQWFS_4KHZ,
RTCC_SQWFS_8KHZ,
RTCC_SQWFS_32KHZ
}
RtccSquareWaveFrequency;
typedef enum
{
RTCC_ALARM_MASK_SECONDS,
RTCC_ALARM_MASK_MINUTES,
RTCC_ALARM_MASK_HOURS,
RTCC_ALARM_MASK_WDAY,
RTCC_ALARM_MASK_DATE,
RTCC_ALARM_MASK_UNUSED1,
RTCC_ALARM_MASK_UNUSED2,
RTCC_ALARM_MASK_ALL
}
RtccAlarmMask;
typedef struct
{
byte secten;
byte secone;
byte minten;
byte minone;
byte hrten;
byte hrone;
byte wkday;
byte dateten;
byte dateone;
byte mthten;
byte mthone;
byte yrten;
byte yrone;
bool mode_24;
bool pm;
}
rtcc_time;
class RTCC_MCP7940N
{
public:
RTCC_MCP7940N();
/**
* Reads the time currently stored on the RTCC chip
*
* @param time Memory location to store the time
*/
void ReadTime(rtcc_time *time);
/**
* Writes the supplied time to the RTCC chip
*
* The clock is briefly suspended during a write to ensure an atomic write operation
*
* @param time Pointer to time to write to chip
*/
void SetTime(const rtcc_time *time);
/**
* Global enable of the clock oscillator circuit
*
* @param enabled True to enable, false to disable
*/
void SetOscillatorEnabled(bool enabled);
/**
* Set alarm 1
*
* @param time The time to set alarm 1 to
*/
void SetAlarm1(const rtcc_time *time);
/**
* Set alarm 2
*
* @param time The time to set alarm 2 to
*/
void SetAlarm2(const rtcc_time *time);
/**
* Set the RTCC chip to use 24 time or 12 hour time, in 12 hour time rtcc_time.pm is set to true for pm
*
* @param enabled True for 24 hour time, false for 12 hour time
*/
void Set24HourTimeEnabled(bool enabled);
/**
* Check what time mode the RTCC chip is in
*
* @return True for 24 hour time, false for 12 hour time
*/
bool Get24HourTimeEnabled();
/**
* Enable the square wave generator, square wave is output on MFP pin
*
* This overrides alarm settings, must be turned off to use alarms
*
* @param enabled True to enable generator
* @param frequency Square wave frequency
*/
void SetSquareWaveOutputState(bool enabled, RtccSquareWaveFrequency frequency = RTCC_SQWFS_1HZ );
/**
* Check state of square wave generator
*
* @return True of generator is enabled
*/
bool GetSquareWaveOutputEnabled();
/**
* Get the state of the oscillator
*
* If this returns false, the clock is not running and there could be an issue with crystal or clock input
*
* @return Returns true of the oscillator is running correctly
*/
bool IsOscillatorRunning();
/**
* Check the power failure flag
*
* Only has meaning if you are using external battery, you must clear this flag with software if you want to detect
* a second power failure event
*
* @return True if a power fail event has been detected
*/
bool HasPowerFailed(rtcc_time *dwTime, rtcc_time *upTime);
/**
* Clear the power failure flag so it can be raised again if another failure is detected
*/
void ClearPowerFailedFlag();
/**
* Enable or disable the low power battery-backed mode
*
* @param enabled True to enable
*/
void SetBatteryEnabled(bool enabled);
/**
* Check if the battery-backed mode is enabled
*
* @return True if enabled
*/
bool GetBatteryEnabled();
/**
* Returns whether the current time stored on the RTCC chip is a leap year
*
* @return True for leap year
*/
bool IsLeapYear();
/**
* Enable or disable alarm 1
*
* @param enabled True to enable
*/
void SetAlarm1Enabled(bool enabled);
/**
* Returns enabled state of alarm 1
*
* @return True for enabled
*/
bool GetAlarm1Enabled();
/**
* Set the polarity on the alarm output on MFP
*
* @param high True results in a HIGH when the alarm is active, false for LOW
*/
void SetAlarmPolarity(bool high);
/**
* Get the alarm polarity
*
* @return True for HIGH, false for LOW
*/
bool GetAlarmPolarity();
/**
* Get state of alarm 1 interrupt flag
*
* Must be cleared to enable future interrupts
*
* @return True for asserted
*/
bool GetAlarm1Flag();
/**
* Clear the alarm 1 interrupt flag
*/
void ClearAlarm1Flag();
/**
* Set the alarm 1 condition flag
*
* @param mask The mask to use for alarm 1
*/
void SetAlarm1Mask(RtccAlarmMask mask);
/**
* Get the alarm 1 condition flag
*
* @return The currently set alarm 1 mask
*/
RtccAlarmMask GetAlarm1Mask();
/**
* Enable or disable alarm 2
*
* @param enabled True to enable
*/
void SetAlarm2Enabled(bool enabled);
/**
* Get the enabled state of alarm 2
*
* @return True for enabled
*/
bool GetAlarm2Enabled();
/**
* Get state of alarm 2 interrupt flag
*
* Must be cleared to enable future interrupts
*
* @return True for asserted
*/
bool GetAlarm2Flag();
/**
* Clear the alarm 2 interrupt flag
*/
void ClearAlarm2Flag();
/**
* Get the alarm 1 condition flag
*
* @return The mask to use for alarm 1
*/
void SetAlarm2Mask(RtccAlarmMask mask);
/**
* Get the alarm 2 condition flag
*
* @return The currently set alarm 2 mask
*/
RtccAlarmMask GetAlarm2Mask();
/**
* Set the state of the external oscillator
*
* @param enabled True to use external clock on X1, false for crystal across X1-X2
*/
void SetExternalOscillatorEnabled(bool enabled);
/**
* Get the state of the external oscillator
*
* @return True means using external clock, false for crystal
*/
bool GetExternalOscillatorEnabled();
/**
* Enable or disable course trim mode
*
* @param enabled True for course trim enabled
*/
void SetCourseTrimEnabled(bool enabled);
/**
* Get course trim enabled state
*
* @return True for course trim enabled
*/
bool GetCourseTrimEnabled();
/**
* Get current general purpose output state
*
* @return True for HIGH, false for LOW
*/
bool GetGPOState();
/**
* Set general purpose output state
*
* Alarms and square wave generator must be disabled to use MFP as a GPO
*
* @param state True for HIGH, false for LOW
*/
void SetGPOState(bool state);
/**
* Get the current oscillator trim value
*
* @return The value of the oscillator trim
*/
signed char GetTrimValue();
/**
* Set the oscillator trim value
*
* See the datasheet for information on this value, allows you to fine tune oscillator for more precise timing
*
* @param value The oscillator trim value
*/
void SetTrimValue(signed char value);
/**
* Convert time between different formats
*
* @param from A tm struct from time.h to convert from
* @param to An rtcc_time struct to convert to
*/
static void ConvertTime(const tm *from, rtcc_time *to);
/**
* Convert time between different formats
*
* @param from An rtcc_time struct to convert from
* @param to A tm struct from time.h to convert to
*/
static void ConvertTime(const rtcc_time *from, tm *to);
private:
void SetBits(int address, int mask, int values);
byte GetByte(int address);
bool GetFlag(int address, int mask);
void SetAlarm(int address, const rtcc_time *timestamp);
int i2c_addr;
};
#endif //__RTCC_MCP7940N_H