-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathDS1302.cpp
252 lines (210 loc) · 6.06 KB
/
DS1302.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
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "DS1302.h"
#include <stdint.h>
namespace {
enum Register {
kSecondReg = 0,
kMinuteReg = 1,
kHourReg = 2,
kDateReg = 3,
kMonthReg = 4,
kDayReg = 5,
kYearReg = 6,
kWriteProtectReg = 7,
// The RAM register space follows the clock register space.
kRamAddress0 = 32
};
enum Command {
kClockBurstRead = 0xBF,
kClockBurstWrite = 0xBE,
kRamBurstRead = 0xFF,
kRamBurstWrite = 0xFE
};
// Establishes and terminates a three-wire SPI session.
class SPISession {
public:
SPISession(const int ce_pin, const int io_pin, const int sclk_pin)
: ce_pin_(ce_pin), io_pin_(io_pin), sclk_pin_(sclk_pin) {
digitalWrite(sclk_pin_, LOW);
digitalWrite(ce_pin_, HIGH);
delayMicroseconds(4); // tCC
}
~SPISession() {
digitalWrite(ce_pin_, LOW);
delayMicroseconds(4); // tCWH
}
private:
const int ce_pin_;
const int io_pin_;
const int sclk_pin_;
};
// Returns the decoded decimal value from a binary-coded decimal (BCD) byte.
// Assumes 'bcd' is coded with 4-bits per digit, with the tens place digit in
// the upper 4 MSBs.
uint8_t bcdToDec(const uint8_t bcd) {
return (10 * ((bcd & 0xF0) >> 4) + (bcd & 0x0F));
}
// Returns the binary-coded decimal of 'dec'. Inverse of bcdToDec.
uint8_t decToBcd(const uint8_t dec) {
const uint8_t tens = dec / 10;
const uint8_t ones = dec % 10;
return (tens << 4) | ones;
}
// Returns the hour in 24-hour format from the hour register value.
uint8_t hourFromRegisterValue(const uint8_t value) {
uint8_t adj;
if (value & 128) // 12-hour mode
adj = 12 * ((value & 32) >> 5);
else // 24-hour mode
adj = 10 * ((value & (32 + 16)) >> 4);
return (value & 15) + adj;
}
} // namespace
Time::Time(const uint16_t yr, const uint8_t mon, const uint8_t date,
const uint8_t hr, const uint8_t min, const uint8_t sec,
const Day day) {
this->yr = yr;
this->mon = mon;
this->date = date;
this->hr = hr;
this->min = min;
this->sec = sec;
this->day = day;
}
DS1302::DS1302(const uint8_t ce_pin, const uint8_t io_pin,
const uint8_t sclk_pin) {
ce_pin_ = ce_pin;
io_pin_ = io_pin;
sclk_pin_ = sclk_pin;
digitalWrite(ce_pin, LOW);
pinMode(ce_pin, OUTPUT);
pinMode(io_pin, INPUT);
digitalWrite(sclk_pin, LOW);
pinMode(sclk_pin, OUTPUT);
}
void DS1302::writeOut(const uint8_t value, bool readAfter) {
pinMode(io_pin_, OUTPUT);
for (int i = 0; i < 8; ++i) {
digitalWrite(io_pin_, (value >> i) & 1);
delayMicroseconds(1);
digitalWrite(sclk_pin_, HIGH);
delayMicroseconds(1);
if (readAfter && i == 7) {
// We're about to read data -- ensure the pin is back in input mode
// before the clock is lowered.
pinMode(io_pin_, INPUT);
} else {
digitalWrite(sclk_pin_, LOW);
delayMicroseconds(1);
}
}
}
uint8_t DS1302::readIn() {
uint8_t input_value = 0;
uint8_t bit = 0;
pinMode(io_pin_, INPUT);
// Bits from the DS1302 are output on the falling edge of the clock
// cycle. This is called after readIn (which will leave the clock low) or
// writeOut(..., true) (which will leave it high).
for (int i = 0; i < 8; ++i) {
digitalWrite(sclk_pin_, HIGH);
delayMicroseconds(1);
digitalWrite(sclk_pin_, LOW);
delayMicroseconds(1);
bit = digitalRead(io_pin_);
input_value |= (bit << i); // Bits are read LSB first.
}
return input_value;
}
uint8_t DS1302::readRegister(const uint8_t reg) {
const SPISession s(ce_pin_, io_pin_, sclk_pin_);
const uint8_t cmd_byte = (0x81 | (reg << 1));
writeOut(cmd_byte, true);
return readIn();
}
void DS1302::writeRegister(const uint8_t reg, const uint8_t value) {
const SPISession s(ce_pin_, io_pin_, sclk_pin_);
const uint8_t cmd_byte = (0x80 | (reg << 1));
writeOut(cmd_byte);
writeOut(value);
}
void DS1302::writeProtect(const bool enable) {
writeRegister(kWriteProtectReg, (enable << 7));
}
void DS1302::halt(const bool enable) {
uint8_t sec = readRegister(kSecondReg);
sec &= ~(1 << 7);
sec |= (enable << 7);
writeRegister(kSecondReg, sec);
}
Time DS1302::time() {
const SPISession s(ce_pin_, io_pin_, sclk_pin_);
Time t(2099, 1, 1, 0, 0, 0, Time::kSunday);
writeOut(kClockBurstRead, true);
t.sec = bcdToDec(readIn() & 0x7F);
t.min = bcdToDec(readIn());
t.hr = hourFromRegisterValue(readIn());
t.date = bcdToDec(readIn());
t.mon = bcdToDec(readIn());
t.day = static_cast<Time::Day>(bcdToDec(readIn()));
t.yr = 2000 + bcdToDec(readIn());
return t;
}
void DS1302::time(const Time t) {
// We want to maintain the Clock Halt flag if it is set.
const uint8_t ch_value = readRegister(kSecondReg) & 0x80;
const SPISession s(ce_pin_, io_pin_, sclk_pin_);
writeOut(kClockBurstWrite);
writeOut(ch_value | decToBcd(t.sec));
writeOut(decToBcd(t.min));
writeOut(decToBcd(t.hr));
writeOut(decToBcd(t.date));
writeOut(decToBcd(t.mon));
writeOut(decToBcd(static_cast<uint8_t>(t.day)));
writeOut(decToBcd(t.yr - 2000));
// All clock registers *and* the WP register have to be written for the time
// to be set.
writeOut(0); // Write protection register.
}
void DS1302::writeRam(const uint8_t address, const uint8_t value) {
if (address >= kRamSize) {
return;
}
writeRegister(kRamAddress0 + address, value);
}
uint8_t DS1302::readRam(const uint8_t address) {
if (address >= kRamSize) {
return 0;
}
return readRegister(kRamAddress0 + address);
}
void DS1302::writeRamBulk(const uint8_t* const data, int len) {
if (len <= 0) {
return;
}
if (len > kRamSize) {
len = kRamSize;
}
const SPISession s(ce_pin_, io_pin_, sclk_pin_);
writeOut(kRamBurstWrite);
for (int i = 0; i < len; ++i) {
writeOut(data[i]);
}
}
void DS1302::readRamBulk(uint8_t* const data, int len) {
if (len <= 0) {
return;
}
if (len > kRamSize) {
len = kRamSize;
}
const SPISession s(ce_pin_, io_pin_, sclk_pin_);
writeOut(kRamBurstRead, true);
for (int i = 0; i < len; ++i) {
data[i] = readIn();
}
}