forked from mengguang/DS1338_RTC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS1338.cpp
473 lines (414 loc) · 8.71 KB
/
DS1338.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include "DS1338.h"
#include "Arduino.h"
extern "C" {
#include <inttypes.h>
}
/**
* \defgroup ds1338_rtctime_t Time Structure and Associated Methods
*/
/**
* \brief Set the values in a struct rtctime_t instance.
*
* \param time A pointer to a struct rtctime_t instance.
* \param year The year.
* \param month The month.
* \param day The day.
* \param hour The hour.
* \param minute The minute.
* \param second The second.
*/
void make_time(struct rtctime_t *time, uint8_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
{
time->year = year;
time->month = month;
time->day = day;
time->hour = hour;
time->minute = minute;
time->second = second;
}
/**
* \brief Output a string representation of a struct rtctime_t object.
*
* Requires a buffer size of at least 20 characters.
*
* \param time A pointer to a struct rtctime_t instance.
* \param buf A pointer to a character buffer in which to store the string.
*/
void format_time_str(struct rtctime_t *time, char *buf)
{
// Year
buf[0] = '2';
if (time->year < 10)
{
buf[1] = '0';
buf[2] = '0';
buf[3] = '0' + time->year;
}
else if (time->year < 100)
{
buf[1] = '0';
buf[2] = '0' + (time->year / 10);
buf[3] = '0' + (time->year % 10);
}
else
{
buf[1] = '1';
buf[2] = '0' + ((time->year - 100) / 10);
buf[3] = '0' + ((time->year - 100) % 10);
}
buf[4] = '-';
// Month
if (time->month < 10)
{
buf[5] = '0';
buf[6] = '0' + time->month;
}
else
{
buf[5] = '0' + (time->month / 10);
buf[6] = '0' + (time->month % 10);
}
buf[7] = '-';
// Day
if (time->day < 10)
{
buf[8] = '0';
buf[9] = '0' + time->day;
}
else
{
buf[8] = '0' + (time->day / 10);
buf[9] = '0' + (time->day % 10);
}
buf[10] = 'T';
// Hour
if (time->hour < 10)
{
buf[11] = '0';
buf[12] = '0' + time->hour;
}
else
{
buf[11] = '0' + (time->hour / 10);
buf[12] = '0' + (time->hour % 10);
}
buf[13] = ':';
// Minute
if (time->minute < 10)
{
buf[14] = '0';
buf[15] = '0' + time->minute;
}
else
{
buf[14] = '0' + (time->minute / 10);
buf[15] = '0' + (time->minute % 10);
}
buf[16] = ':';
// Second
if (time->second < 10)
{
buf[17] = '0';
buf[18] = '0' + time->second;
}
else
{
buf[17] = '0' + (time->second / 10);
buf[18] = '0' + (time->second % 10);
}
buf[19] = 0;
}
/**
* \defgroup ds1338_i2c I2C Functions
*/
/**
* \brief Read data from an I2C device.
*
* \param addr The address of the device from which to read.
* \param buf A pointer to a buffer in which to store the data.
* \param num The number of bytes to read.
*
* \return 0 on success; otherwise an I2C error.
*/
static uint8_t i2c_read(uint8_t addr, uint8_t *buf, uint8_t num)
{
Wire.requestFrom(addr, num);
if (Wire.available() < num)
{
return READ_ERROR;
}
for (uint8_t i = 0; i < num; i++)
{
buf[i] = Wire.read();
}
return 0;
}
/**
* \brief Write data to an I2C device.
*
* \param addr The address of the device to which to write.
* \param buf A pointer to a buffer from which to read the data.
* \param num The number of bytes to write.
*
* \return 0 on success; otherwise an I2C error.
*/
static uint8_t i2c_write(uint8_t addr, uint8_t *buf, uint8_t num)
{
Wire.beginTransmission(addr);
for (uint8_t i = 0; i < num; i++)
{
Wire.write(buf[i]);
}
return Wire.endTransmission();
}
static uint8_t i2c_write_reg(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t num)
{
Wire.beginTransmission(addr);
Wire.write(reg);
for (uint8_t i = 0; i < num; i++)
{
Wire.write(buf[i]);
}
return Wire.endTransmission();
}
/**
* \brief Write a single byte to an I2C device.
*
* \param addr The address of the device to which to write.
* \param b The byte to write.
*
* \return 0 on success; otherwise an I2C error.
*/
static uint8_t i2c_write_1(uint8_t addr, uint8_t b)
{
Wire.beginTransmission(addr);
Wire.write(b);
return Wire.endTransmission();
}
/**
* \defgroup ds1338_time Time Getter and Setter Methods
*/
/**
* \brief Read the current time.
*
* \param time A pointer to a struct rtctime_t instance in which to store the time.
*
* \return 0 on success; otherwise an I2C error.
*/
uint8_t ds1338_read_time(struct rtctime_t *time)
{
uint8_t buf[7];
uint8_t res = i2c_write_1(DS1338_ADDR, DS1338_REG_SECONDS);
if (res)
{
p("I2C write error: ");
pln(res, DEC);
return 1;
}
res = i2c_read(DS1338_ADDR, buf, 7);
if (res)
{
pln("I2C read error.");
return 2;
}
time->second = decode_bcd(buf[0]);
time->minute = decode_bcd(buf[1]);
if (buf[2] & DS1338_HOUR_12)
{
time->hour = ((buf[2] >> 4) & 0x01) * 12 + ((buf[2] >> 5) & 0x01) * 12;
}
else
{
time->hour = decode_bcd(buf[2]);
}
time->day = decode_bcd(buf[4]);
time->month = decode_bcd(buf[5] & 0x1F);
time->year = 100 * ((buf[5] >> 7) & 0x01) + decode_bcd(buf[6]);
return 0;
}
/**
* \brief Set the time.
*
* \param time A pointer to a struct rtctime_t instance containing the time to set.
*
* \return 0 on success; otherwise an I2C error.
*/
uint8_t ds1338_write_time(struct rtctime_t *time)
{
uint8_t buf[8];
buf[0] = DS1338_REG_SECONDS;
buf[1] = encode_bcd(time->second);
buf[2] = encode_bcd(time->minute);
buf[3] = encode_bcd(time->hour); // Time always stored in 24-hour format
buf[4] = 1; // Not used
buf[5] = encode_bcd(time->day);
buf[6] = ((time->year / 100) << 7) | encode_bcd(time->month);
buf[7] = encode_bcd((time->year) % 100);
uint8_t res = i2c_write(DS1338_ADDR, buf, 8);
if (res)
{
p("I2C write error: ");
pln(res, DEC);
return 1;
}
return 0;
}
/**
* \defgroup ds1338_control Control Register Methods
*/
/**
* \brief Get the value of the control register.
*
* \param ctrl A pointer to a value in which to store the value of the control register.
*
* \return 0 on success; otherwise an I2C error.
*/
static uint8_t ds1338_get_control(uint8_t *ctrl)
{
uint8_t res = i2c_write_1(DS1338_ADDR, DS1338_REG_CONTROL);
if (res)
{
p("I2C write error: ");
pln(res, DEC);
return res;
}
res = i2c_read(DS1338_ADDR, ctrl, 1);
if (res)
{
p("I2C write error: ");
pln(res, DEC);
return res;
}
return 0;
}
/**
* \brief Set the value of the control register.
*
* \param ctrl The value to set.
*
* \return 0 on success; otherwise an I2C error.
*/
static uint8_t ds1338_set_control(uint8_t ctrl)
{
uint8_t buf[2];
buf[0] = DS1338_REG_CONTROL;
buf[1] = ctrl;
return i2c_write(DS1338_ADDR, buf, 2);
}
/**
* \brief Set the specified bits in the control register.
*
* \param mask A mask specifying which bits to set. (High bits will be set.)
*
* \return 0 on success; otherwise an I2C error.
*/
static uint8_t ds1338_set_control_bits(uint8_t mask)
{ // set bits
uint8_t ctrl;
uint8_t res = ds1338_get_control(&ctrl);
if (res)
{
return res;
}
ctrl |= mask;
return ds1338_set_control(ctrl);
}
/**
* \brief Clear the specified bits in the control register.
*
* \param mask A mask specifying which bits to clear. (High bits will be cleared.)
*
* \return 0 on success; otherwise an I2C error.
*/
static uint8_t ds1338_clear_control_bits(uint8_t mask)
{
return ds1338_set_control(~mask);
}
uint8_t ds1338_get_osf(uint8_t *osf)
{
uint8_t ctrl;
uint8_t res = ds1338_get_control(&ctrl);
if (res)
{
return res;
}
if (ctrl & DS1338_OSC_STOP_FLAG == 0)
{
*osf = 0;
}
else
{
*osf = 1;
}
return 0;
}
uint8_t ds1338_clean_osf()
{
return ds1338_clear_control_bits(DS1338_OSC_STOP_FLAG);
}
/*
#define DS1338_SQWE_FLAG (0x01 << 4)
#define DS1338_SQW_MASK (0x03)
#define DS1338_SQW_32768HZ (0x03)
#define DS1338_SQW_8192HZ (0x02)
#define DS1338_SQW_4096HZ (0x01)
#define DS1338_SQW_1HZ (0x00)
*/
uint8_t ds1338_enable_sqw(uint8_t frequence)
{
uint8_t ctrl=0;
frequence = frequence & DS1338_SQW_MASK;
ctrl |= frequence;
ctrl |= DS1338_SQWE_FLAG;
ds1338_clear_control_bits(DS1338_SQW_MASK);
return ds1338_set_control_bits(ctrl);
}
uint8_t ds1338_disable_sqw()
{
return ds1338_clear_control_bits(DS1338_SQWE_FLAG|DS1338_SQW_MASK);
}
uint8_t ds1338_read_ram(uint8_t address, uint8_t *data, uint8_t length)
{
if (address > DS1338_REG_RAM_END || address < DS1338_REG_RAM_BEGIN)
{
return 1;
}
if (address + length > DS1338_REG_RAM_END)
{
return 1;
}
uint8_t res = i2c_write_1(DS1338_ADDR, address);
if (res)
{
p("I2C write error: ");
pln(res, DEC);
return res;
}
res = i2c_read(DS1338_ADDR, data, length);
if (res)
{
p("I2C write error: ");
pln(res, DEC);
return res;
}
return 0;
}
uint8_t ds1338_write_ram(uint8_t address, uint8_t *data, uint8_t length)
{
if (address > DS1338_REG_RAM_END || address < DS1338_REG_RAM_BEGIN)
{
return 1;
}
if (address + length > DS1338_REG_RAM_END)
{
return 1;
}
uint8_t res = i2c_write_reg(DS1338_ADDR, address, data, length);
if (res)
{
p("I2C write error: ");
pln(res, DEC);
return 1;
}
return 0;
}