-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDS1302.h
314 lines (289 loc) · 7.96 KB
/
DS1302.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
/**
* @file DS1302.h
* @version 1.0
*
* @section License
* Copyright (C) 2017, Mikael Patel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#ifndef DS1302_H
#define DS1302_H
#include "GPIO.h"
#include "RTC.h"
#ifndef CHARBITS
#define CHARBITS 8
#endif
/**
* GPIO based device driver for DS1302, Trickle-Charge Timekeeping Chip.
* @param[in] CS_PIN chip select board pin.
* @param[in] SDA_PIN serial data board pin.
* @param[in] CLK_PIN clock board pin.
*
* @section Circuit
* @code
* DS1302/RTC
* +------------+
* (VCC)---------------1-|VCC |
* (GND)---------------2-|GND |
* (CLK)---------------3-|CLK |
* (SDA)---------------4-|DAT |
* (CS)----------------5-|RST |
* +------------+
* @endcode
*
* @section References
* 1. On-line product description,
* http://www.maximintegrated.com/datasheet/index.mvp/id/2685
* 2. Datasheet, http://datasheets.maximintegrated.com/en/ds/DS1302.pdf
*/
template<BOARD::pin_t CS_PIN, BOARD::pin_t SDA_PIN, BOARD::pin_t CLK_PIN>
class DS1302 {
public:
/** Static memory size. */
static const size_t RAM_MAX = 31;
/**
* Construct device driver for DS1302 Real-Time Clock with the given
* pins. Initiate pins (output mode).
*/
DS1302()
{
m_cs.output();
m_cs.low();
m_sda.output();
m_clk.output();
m_clk.low();
}
/**
* Read clock and calender from the device. Return in standard
* time structure.
* @param[in,out] now time structure for return value.
*/
void get_time(struct tm& now)
{
// Burst read clock and calender from device
rtc_t rtc;
m_cs.high();
write(RTC_BURST | READ);
m_sda.input();
uint8_t* rp = (uint8_t*) &rtc;
for (size_t i = 0; i < sizeof(rtc); i++, rp++)
*rp = read();
m_sda.output();
m_cs.low();
// Convert to standard time structure
now.tm_sec = rtc.seconds;
now.tm_min = rtc.minutes;
now.tm_hour = rtc.hours;
now.tm_mday = rtc.date;
now.tm_wday = rtc.day - 1;
now.tm_mon = rtc.month - 1;
now.tm_year = rtc.year + 100;
}
/**
* Write clock and calender in given standard time structure to the
* device.
* @param[in] now time to set.
*/
void set_time(struct tm& now)
{
// Convert from standard time structure, and add write disable
rtc_t rtc;
rtc.seconds = now.tm_sec;
rtc.minutes = now.tm_min;
rtc.hours = now.tm_hour;
rtc.date = now.tm_mday;
rtc.day = now.tm_wday + 1;
rtc.month = now.tm_mon + 1;
rtc.year = now.tm_year - 100;
rtc.wp = 0x80;
// Burst write clock and calender to device
write_enable();
m_cs.high();
write(RTC_BURST | WRITE);
uint8_t* rp = (uint8_t*) &rtc;
for (size_t i = 0; i < sizeof(rtc); i++, rp++)
write(*rp);
m_cs.low();
}
/**
* Enable write of clock/calender or memory.
*/
void write_enable()
__attribute__((always_inline))
{
write(WP, 0x00);
}
/**
* Disable write of clock/calender or memory.
*/
void write_disable()
__attribute__((always_inline))
{
write(WP, 0x80);
}
/**
* Read given static memory address on the device and return byte.
* @param[in] addr memory address on the device (0..RAM_MAX-1).
*/
uint8_t read_ram(uint8_t addr)
__attribute__((always_inline))
{
return (read(RAM_START | (addr & ADDR_MASK)));
}
/**
* Write given data to the static memory (31 bytes). Requires
* handling of write enable/disable.
* @param[in] addr memory address (0..RAM_MAX-1).
* @param[in] data to write to the memory address.
*/
void write_ram(uint8_t addr, uint8_t data)
__attribute__((always_inline))
{
write(RAM_START | (addr & ADDR_MASK), data);
}
/**
* Burst read memory block from the device starting at address
* zero(0). Data block is returned in the given buffer.
* @param[in] buf pointer to buffer to store data read.
* @param[in] size number of bytes to read (max RAM_MAX(31)).
*/
void read_ram(void* buf, size_t size)
{
if (size == 0) return;
uint8_t* bp = (uint8_t*) buf;
if (size > RAM_MAX) size = RAM_MAX;
m_cs.high();
write(RAM_BURST | READ);
m_sda.input();
do *bp++ = read(); while (--size);
m_sda.output();
m_cs.low();
}
/**
* Burst write data in buffer with given size to the static memory
* in the device (max 31 bytes). Burst write is always from address
* zero(0) and includes handling of write protect.
* @param[in] buf pointer to memory block to write.
* @param[in] size number of bytes to write (max RAM_MAX(31)).
*/
void write_ram(void* buf, size_t size)
{
if (size == 0) return;
uint8_t* bp = (uint8_t*) buf;
if (size > RAM_MAX) size = RAM_MAX;
write_enable();
m_cs.high();
write(RAM_BURST | WRITE);
do write(*bp++); while (--size);
m_cs.low();
write_disable();
}
protected:
/** Start address of static memory. */
static const uint8_t RAM_START = 32;
/** Write protect register. */
static const uint8_t WP = 0x07;
/** Command byte. */
enum {
WRITE = 0x80, //!< Read/write bit in write mode.
READ = 0x81, //!< Read/write bit in read mode.
RTC_BURST = 0xbe, //!< RTC register burst transfer.
RAM_BURST = 0xfe, //!< RAM burst transfer.
ADDR_MASK = 0x3f //!< Mask address bits.
} __attribute__((packed));
/**
* Table 3. Register Address/Definition.
*/
struct rtc_t {
bcd_t seconds; //!< 00-59 Seconds.
bcd_t minutes; //!< 00-59 Minutes.
bcd_t hours; //!< 00-23 Hours.
bcd_t date; //!< 01-31 Date.
bcd_t month; //!< 01-12 Month.
bcd_t day; //!< 01-07 Day.
bcd_t year; //!< 00-99 Year.
uint8_t wp; //!< Write protect register.
} __attribute__((packed));
GPIO<CS_PIN> m_cs; //!< Chip select, asserted high.
GPIO<SDA_PIN> m_sda; //!< Serial data, bidirectional.
GPIO<CLK_PIN> m_clk; //!< Clock for synchronized data.
/**
* Low level RTC access function. Read data from the clock/calender
* register or static memory on device.
* @param[in] addr device address.
* @return data.
*/
uint8_t read(uint8_t addr)
{
addr = ((addr << 1) | READ);
uint8_t res = 0;
m_cs.high();
write(addr);
m_sda.input();
res = read();
m_sda.output();
m_cs.low();
return (res);
}
/**
* Low level RTC access function. Write given data to the clock
* register or static memory on device.
* @param[in] addr device address.
* @param[in] data to write.
*/
void write(uint8_t addr, uint8_t data)
{
addr = ((addr << 1) | WRITE);
m_cs.high();
write(addr);
write(data);
m_cs.low();
}
/**
* Low level read data from the device. Internal transfer
* function. Used within a chip select block. Data direction must be
* set before calling this function.
* @return data read from the device.
*/
uint8_t read()
{
uint8_t mask = 0x01;
uint8_t res = 0;
do {
res |= (m_sda ? mask : 0x00);
m_clk.toggle();
__asm__ __volatile__("nop");
mask <<= 1;
m_clk.toggle();
__asm__ __volatile__("nop");
} while (mask);
return (res);
}
/**
* Write low level data to the device. Internal transfer
* function. Used within a chip select block.
* @param[in] data to write to the device.
*/
void write(uint8_t data)
{
uint8_t mask = 0x01;
do {
m_sda = data & mask;
__asm__ __volatile__("nop");
m_clk.toggle();
__asm__ __volatile__("nop");
mask <<= 1;
m_clk.toggle();
} while (mask);
}
};
#endif