-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDS1306.h
190 lines (162 loc) · 6.28 KB
/
DS1306.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
/*
* File DS1306.h
*
* Synopsis Support for Dallas Semiconductor DS1306 Real Time Clock, using SPI
*
* Author Chris Bearman
*
* Version 1.0
*
* License This software is released under the terms of the Mozilla Public License (MPL) version 2.0
* Full details of licensing terms can be found in the "LICENSE" file, distributed with this code
*
* Instructions
* Create an instance of the DS1306 class using either constructor.
* By default all writes will use 24 hour form, if you wish to write clock using 12 hour form
* then use parameterized constructor and select writeHours24 = false
*
* Current date/time is passed as ds1306time type
* Alarm specifications are passed as ds1306alarm type
* For both types, hours is hours in 24 hour form, hours12 is 12 hour form and ampm is AM/PM
* indicator where AM is represented by (char) 'A' and PM is represented by (char) 'P'
* If ampm indicator is neither 'A' nor 'B' then 12 hour time is considered undefined
* Both 12 hour and 24 hour times are always provided by calls that read the time
* You MUST provide 24 hour time to write time/alarm unless you select writeHours24 = false in which
* case you MUST provide 12 hour time to write time/alarm.
*
* Supports DS1306 RTC chip connected to SPI bus (MOSI/MISO/SCLK) with chip select on any line
* as designated by the init call.
*
* On init call, the SPI bus is initialized, the clock oscillator is started and the clock
* write protect is deactivated.
*
* For optimization, values are NOT checked for correctness when being written to the DS1306.
* Per specification, writing illogical values will result in undefined behavior.
*
* Full details on the operation and use of each method can be found in DS1306.cpp
*/
#ifndef __DS1306_RTC_
#define __DS1306_RTC_
/* Memory Locations */
#define DS1306_DATETIME 0x00
#define DS1306_ALARM0 0x07
#define DS1306_ALARM1 0x0B
#define DS1306_CR 0x0F
#define DS1306_SR 0x10
#define DS1306_TCR 0x11
#define DS1306_USER_START 0x20
#define DS1306_USER_END 0x7F
/* Buffer sizes required for reading / writing time/date and alarms to the RTC */
#define DS1306_SIZE_DATETIME 7
#define DS1306_SIZE_ALARM 4
/* Bit Position of key register parameters (CR) */
#define DS1306_CR_WP 6
#define DS1306_CR_1HZ 2
#define DS1306_CR_AIE1 1
#define DS1306_CR_AIE0 0
/* Bit Position of key register parameters (SR) */
#define DS1306_SR_IRQF1 1
#define DS1306_SR_IRQF0 0
/* Bit Positions of key register parameters (TCR) */
#define DS1306_TCR_TCS 7
#define DS1306_TCR_DS 3
#define DS1306_TCR_RS 1
/* Days of week (suggested, see spec) */
#define DS1306_SUNDAY 1
#define DS1306_MONDAY 2
#define DS1306_TUESDAY 3
#define DS1306_WEDNESDAY 4
#define DS1306_THURSDAY 5
#define DS1306_FRIDAY 6
#define DS1306_SATURDAY 7
/* "Any" designator, for alarms */
#define DS1306_ANY 0x80
/* Write offset used when writing DS1306 registers */
#define DS1306_WRITE_OFFSET 0x80
/* Representation of the current time/date */
typedef struct {
unsigned char seconds;
unsigned char minutes;
unsigned char hours;
unsigned char hours12;
char ampm; // 'A' = AM, 'P' = PM, anything else is hours12 undefined
unsigned char dow;
unsigned char day;
unsigned char month;
unsigned char year;
} ds1306time;
/* Representation of an alarm */
typedef struct {
unsigned char seconds;
unsigned char minutes;
unsigned char hours;
unsigned char hours12;
char ampm; // 'A' = AM, 'P' = PM, anything else is hours12 undefined
unsigned char dow;
} ds1306alarm;
class DS1306
{
public:
// Constructors
DS1306();
DS1306(bool writeHours24);
// Initialize DS1306 using ce as chip enable line, turn on osc
void init(unsigned char ce);
// Primary clock (time/date) operations
void setTime(const ds1306time *time);
void getTime(ds1306time *time);
// Alarm management operations
void setAlarm(int alarm, const ds1306alarm *time);
void getAlarm(int alarm, ds1306alarm *time);
bool getAlarmState(unsigned int alarm);
void getAlarmBothState(bool *state1, bool *state2);
void clearAlarmState(unsigned int alarm);
void clearAlarmBothState();
bool getAlarmEnabled(unsigned int alarm);
void getAlarmBothEnabled(bool *enabled1, bool *enabled2);
void enableAlarm(unsigned int alarm);
void disableAlarm(unsigned int alarm);
void enableBothAlarms();
void disableBothAlarms();
// 1Hz state
bool get1HzState();
void set1HzState(bool enabled);
// Trickle charge management
bool enableTrickleCharge(unsigned char numDiodes, unsigned char kRes);
void disableTrickleCharge();
bool getTrickleChargeState(unsigned char *numDiodes, unsigned char *kRes);
// User memory management
bool writeUser(unsigned char addr, const char *buf, int num);
bool readUser(unsigned char addr, char *buf, int num);
// Write Protection management
bool isWriteProtected();
void setWriteProtection(bool on);
// Direct Register access (use for direct access to registers, if needed)
void read(unsigned char address, unsigned char *data, int len);
unsigned char read(unsigned char address);
void write(unsigned char address, const unsigned char *data, int len);
void write(unsigned char address, const unsigned char value);
private:
// Class Properties
unsigned char ce; // Chip enable line
bool writeHours24; // True (default) means time/alarm writes use 24 hour form
// Encode a time / alarm packet
void encodeTimePacket(unsigned char *buf, const ds1306time *time);
void encodeAlarmPacket(unsigned char *buf, const ds1306alarm *alarm);
// Decode a time / alarm packet
void decodeTimePacket(const unsigned char *buf, ds1306time *time);
void decodeAlarmPacket(const unsigned char *buf, ds1306alarm *alarm);
// Hour parameter management
void decodeHourByte(unsigned char hourByte, unsigned char *hour24, unsigned char *hour12, char *ampm);
unsigned char encodeHourByte(unsigned char hour24, unsigned char hour12, char ampm);
// Parameter encode / decode
unsigned char encodeBCD7(unsigned char value);
unsigned char encodeBCD7(unsigned char value, unsigned char mask);
unsigned char encodeBCD8(unsigned char value);
unsigned char decodeBCD7(unsigned char value);
unsigned char decodeBCD7(unsigned char value, unsigned char mask);
unsigned char decodeBCD8(unsigned char value);
// Wait for SPI operation to finish
void waitSPI();
};
#endif /* __DS1306_RTC_ */