-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtc2.c
581 lines (446 loc) · 13.2 KB
/
rtc2.c
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// vim: foldmethod=marker
#include <avr/io.h>
#include <util/delay.h>
#include "rtc2.h"
#if RTC2_RAM_STRINGS
#include <string.h>
#endif
// Registers addresses from datasheet {{{
#define RTC2_SECONDS_READ 0x81
#define RTC2_SECONDS_WRITE 0x80
#define RTC2_MINUTES_READ 0x83
#define RTC2_MINUTES_WRITE 0x82
#define RTC2_HOURS_READ 0x85
#define RTC2_HOURS_WRITE 0x84
#define RTC2_DATE_READ 0x87
#define RTC2_DATE_WRITE 0x86
#define RTC2_MONTH_READ 0x89
#define RTC2_MONTH_WRITE 0x88
#define RTC2_WDAY_READ 0x8B
#define RTC2_WDAY_WRITE 0x8A
#define RTC2_YEAR_READ 0x8D
#define RTC2_YEAR_WRITE 0x8C
#define RTC2_WP_READ 0x8F
#define RTC2_WP_WRITE 0x8E
#define RTC2_CHARGER_READ 0x91
#define RTC2_CHARGER_WRITE 0x90
#define RTC2_BURST_READ 0xBF
#define RTC2_BURST_WRITE 0xBE
#define RTC2_BURST_MEM_READ 0xFF
#define RTC2_BURST_MEM_WRITE 0xFE
// }}}
// RTC2 utility macro handling I/O {{{
#define RTC2_IO_OUTPUT (RTC2_DDR |= _BV(RTC2_IO))
#define RTC2_IO_INPUT (RTC2_DDR &= ~_BV(RTC2_IO))
#define RTC2_IO_HIGH (RTC2_PORT |= _BV(RTC2_IO))
#define RTC2_IO_LOW (RTC2_PORT &= ~_BV(RTC2_IO))
#define RTC2_CLK_HIGH (RTC2_PORT |= _BV(RTC2_CLK))
#define RTC2_CLK_LOW (RTC2_PORT &= ~_BV(RTC2_CLK))
#define RTC2_CE_HIGH (RTC2_PORT |= _BV(RTC2_CE))
#define RTC2_CE_LOW (RTC2_PORT &= ~_BV(RTC2_CE))
#define RTC2_MEM_END_WRITE (RTC2_MEM_END - 1)
#define RTC2_MEM_START_WRITE (RTC2_MEM_START - 1)
#define RTC2_MEM_READ_INVALID(x) ((x) > RTC2_MEM_END || (x) < RTC2_MEM_START)
#define RTC2_MEM_WRITE_INVALID(x) ((x) > RTC2_MEM_END_WRITE || (x) < RTC2_MEM_START_WRITE)
#define RTC2_START_TRANSMISSION(kind) { rtc2_reset(); rtc2_write_byte(kind); } while(0)
#define RTC2_STOP_TRANSMISSION { RTC2_CE_LOW; RTC2_CLK_LOW; } while(0)
// }}}
// Default global pointer memory {{{
#if RTC2_DEFAULT && (RTC2_READ || RTC2__WRITE)
static rtc2_datetime_t rtc2_default = {0};
#endif
// }}}
// Initializer. Configures I/O ports and maybe sets up global variable {{{
void rtc2_init(void){
// set all pins to output
RTC2_DDR |= _BV(RTC2_CE) | _BV(RTC2_CLK) | _BV(RTC2_IO);
// and turn them off
RTC2_PORT &= ~(_BV(RTC2_CE) | _BV(RTC2_CLK) | _BV(RTC2_IO));
// initialize default global pointer if needed
#if RTC2_DEFAULT && (RTC2_READ || RTC2__WRITE)
RTC2_VALUE = &rtc2_default;
#endif
}
// }}}
// Utility stuff used to reset current transfer state {{{
static inline void rtc2_reset(void){
RTC2_STOP_TRANSMISSION;
RTC2_CE_HIGH;
}
// }}}
// Low level write functions {{{
// Routine used for pretty much any operation {{{
#if RTC2_READ || RTC2_WRITE ||RTC2_RAM || RTC2_UTILITY
static void rtc2_write_byte(uint8_t byte){
uint8_t i;
RTC2_IO_OUTPUT;
for(i = 0; i < 8; ++i){
if(byte & 1)
RTC2_IO_HIGH;
else
RTC2_IO_LOW;
RTC2_CLK_LOW;
_delay_us(2);
RTC2_CLK_HIGH;
_delay_us(2);
byte >>= 1;
}
}
#endif
// }}}
// Write to register {{{
#if RTC2_WRITE || RTC2_RAM || RTC2_UTILITY
static void rtc2_write(uint8_t reg, uint8_t val){
RTC2_START_TRANSMISSION(reg);
rtc2_write_byte(val);
RTC2_STOP_TRANSMISSION;
}
#endif
// }}}
// }}}
// Low level read functions {{{
#if RTC2_READ || RTC2_UTILITY || RTC2_RAM
static uint8_t rtc2_read_byte(void){
uint8_t i, ret = 0;
RTC2_IO_INPUT;
for(i = 0; i < 8; ++i){
RTC2_CLK_HIGH;
_delay_us(2);
RTC2_CLK_LOW;
_delay_us(2);
ret >>= 1;
if(bit_is_set(RTC2_PIN, RTC2_IO))
ret |= _BV(7);
}
return ret;
}
static uint8_t rtc2_read(uint8_t reg){
uint8_t ret;
RTC2_START_TRANSMISSION(reg);
ret = rtc2_read_byte();
RTC2_STOP_TRANSMISSION;
return ret;
}
#endif
// }}}
// Writing (presetting clock stuff) {{{
#if RTC2_WRITE
void rtc2_preset(rtc2_datetime ptr){
rtc2_set(ptr, RTC2_ALL_FIELDS);
}
// encodes given values in BCD for sending to DS1302.
// we have to cut off higher parts to avoid accidently
// setting control bits.
static uint8_t rtc2_store_field(uint8_t field, uint8_t val){
switch(field){
case RTC2_SECONDS_WRITE:
case RTC2_MINUTES_WRITE:
val = (((val / 10) << 4) & 0x70) | (val % 10);
break;
case RTC2_HOURS_WRITE:
val = (val & RTC2_FORMAT_PM) | (((val / 10) << 4) & 0x10) | (val % 10);
break;
case RTC2_DATE_WRITE:
val = (((val / 10) << 4) & 0x30) | (val % 10);
break;
case RTC2_MONTH_WRITE:
val = (((val / 10) << 4) & 0x10) | (val % 10);
break;
case RTC2_WDAY_WRITE:
val &= 0x03;
break;
case RTC2_YEAR_WRITE:
val = (((val / 10) << 4) & 0xF0) | (val % 10);
break;
}
return val;
}
static void rtc2_set_val(uint8_t field, uint8_t val){
rtc2_write(field, rtc2_store_field(field, val));
}
void rtc2_set(rtc2_datetime ptr, uint8_t fields){
#if RTC2_BURST
if(fields & RTC2_ALL_FIELDS){
RTC2_START_TRANSMISSION(RTC2_BURST_WRITE);
rtc2_write_byte(rtc2_store_field(RTC2_SECONDS_WRITE, ptr->seconds));
rtc2_write_byte(rtc2_store_field(RTC2_MINUTES_WRITE, ptr->minutes));
rtc2_write_byte(rtc2_store_field(RTC2_HOURS_WRITE, ptr->format | ptr->hours));
rtc2_write_byte(rtc2_store_field(RTC2_DATE_WRITE, ptr->date));
rtc2_write_byte(rtc2_store_field(RTC2_MONTH_WRITE, ptr->month));
rtc2_write_byte(rtc2_store_field(RTC2_WDAY_WRITE, ptr->wday));
rtc2_write_byte(rtc2_store_field(RTC2_YEAR_WRITE, ptr->year));
rtc2_write_byte(0);
RTC2_STOP_TRANSMISSION;
}else{
#endif
if(fields & RTC2_SECONDS_FIELD)
rtc2_set_val(RTC2_SECONDS_WRITE, ptr->seconds);
if(fields & RTC2_MINUTES_FIELD)
rtc2_set_val(RTC2_MINUTES_WRITE, ptr->minutes);
if(fields & RTC2_HOURS_FIELD)
rtc2_set_val(RTC2_HOURS_WRITE, (ptr->format | ptr->hours));
if(fields & RTC2_DATE_FIELD)
rtc2_set_val(RTC2_DATE_WRITE, ptr->date);
if(fields & RTC2_MONTH_FIELD)
rtc2_set_val(RTC2_MONTH_WRITE, ptr->month);
if(fields & RTC2_WDAY_FIELD)
rtc2_set_val(RTC2_WDAY_WRITE, ptr->wday);
if(fields & RTC2_YEAR_FIELD)
rtc2_set_val(RTC2_YEAR_WRITE, ptr->year);
#if RTC2_BURST
}
#endif
}
#endif
// }}}
// High level decoding and reading (updating) functions {{{
#if RTC2_READ
void rtc2_update(rtc2_datetime ptr){
rtc2_get(ptr, RTC2_ALL_FIELDS);
}
// decode values received from DS1302 from BCD to bin encoding.
// see datasheet for details. because some registers
// contain also control bits we cut off higher parts
// that are not data.
static uint8_t rtc2_parse_val(uint8_t field, uint8_t val){
switch(field){
case RTC2_SECONDS_READ:
case RTC2_MINUTES_READ:
val = (val & 0x0F) + ((val & 0x70) >> 4) * 10;
break;
case RTC2_HOURS_READ:
// hours format is passed along with the hour itself
val = (val & RTC2_FORMAT_PM) | ((val & 0x0F) + ((val & 0x10) >> 4) * 10);
break;
case RTC2_DATE_READ:
val = (val & 0x0F) + ((val & 0x30) >> 4) * 10;
break;
case RTC2_MONTH_READ:
val = (val & 0x0F) + ((val & 0x10) >> 4) * 10;
break;
case RTC2_WDAY_READ:
val &= 0x07;
break;
case RTC2_YEAR_READ:
val = (val & 0x0F) + ((val & 0xF0) >> 4) * 10;
break;
}
return val;
}
static uint8_t rtc2_get_val(uint8_t field){
return rtc2_parse_val(field, rtc2_read(field));
}
void rtc2_get(rtc2_datetime ptr, uint8_t fields){
#if RTC2_BURST
if(fields & RTC2_ALL_FIELDS){
uint8_t tmp;
RTC2_START_TRANSMISSION(RTC2_BURST_READ);
ptr->seconds = rtc2_parse_val(RTC2_SECONDS_READ, rtc2_read_byte());
ptr->minutes = rtc2_parse_val(RTC2_MINUTES_READ, rtc2_read_byte());
// hours format is passed along with the hour itself
tmp = rtc2_parse_val(RTC2_HOURS_READ, rtc2_read_byte());
ptr->format = tmp & RTC2_FORMAT_PM;
ptr->hours = tmp & ~RTC2_FORMAT_PM;
ptr->date = rtc2_parse_val(RTC2_DATE_READ, rtc2_read_byte());
ptr->month = rtc2_parse_val(RTC2_MONTH_READ, rtc2_read_byte());
ptr->wday = rtc2_parse_val(RTC2_WDAY_READ, rtc2_read_byte());
ptr->year = rtc2_parse_val(RTC2_YEAR_READ, rtc2_read_byte());
RTC2_STOP_TRANSMISSION;
}else{
#endif
if(fields & RTC2_SECONDS_FIELD)
ptr->seconds = rtc2_get_val(RTC2_SECONDS_READ);
if(fields & RTC2_MINUTES_FIELD)
ptr->minutes = rtc2_get_val(RTC2_MINUTES_READ);
if(fields & RTC2_HOURS_FIELD){
uint8_t tmp = rtc2_get_val(RTC2_HOURS_READ);
ptr->format = tmp & RTC2_FORMAT_PM;
ptr->hours = tmp & ~RTC2_FORMAT_PM;
}
if(fields & RTC2_DATE_FIELD)
ptr->date = rtc2_get_val(RTC2_DATE_READ);
if(fields & RTC2_MONTH_FIELD)
ptr->month = rtc2_get_val(RTC2_MONTH_READ);
if(fields & RTC2_WDAY_FIELD)
ptr->wday = rtc2_get_val(RTC2_WDAY_READ);
if(fields & RTC2_YEAR_FIELD)
ptr->year = rtc2_get_val(RTC2_YEAR_READ);
#if RTC2_BURST
}
#endif
}
// UNIX timestamp utilities {{{
#if RTC2_TIMESTAMP
// not exactly tzdata but will do
static uint8_t rtc2_monthes[] = {
31, 28, 31,
30, 31, 30,
31, 31, 30,
31, 30, 31
};
uint32_t rtc2_mktime(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t date, uint8_t month, uint8_t year){
uint32_t ret = RTC2_BASE_TIMESTAMP; // 1st January 2000, midnight
ret += seconds;
ret += minutes * 60L;
ret += hours * 60L * 60;
--month;
if(year) // a special case for base timestamp
date += (year - 1) / 4;
else
--date;
for(seconds = 0; seconds < month; ++seconds)
ret += rtc2_monthes[seconds] * 24L * 60 * 60;
if(month > 1 && year % 4 == 0)
++date;
ret += date * 24L * 60 * 60;
ret += year * 365L * 24 * 60 * 60;
return ret;
}
uint32_t rtc2_timestamp(rtc2_datetime src){
uint32_t hours = src->hours;
if(src->format == RTC2_FORMAT_PM)
hours += 12;
return rtc2_mktime(
src->seconds, src->minutes, hours,
src->date, src->month, src->year
);
}
// will return 0 if timestamp is older tha RTC2_BASE_TIMESTAMP
// which is 1st January 2000
uint8_t rtc2_localtime(rtc2_datetime dst, uint32_t timestamp){
uint16_t i, y;
if(timestamp < RTC2_BASE_TIMESTAMP)
return 0;
timestamp -= RTC2_BASE_TIMESTAMP;
dst->seconds = timestamp % 60;
timestamp /= 60;
dst->minutes = timestamp % 60;
timestamp /= 60;
dst->hours = timestamp % 24;
timestamp /= 24;
dst->wday = (timestamp + 6) % 7; // because 1st January 2000 is Saturday add 6
for(y = 0; timestamp; ++y){
i = 365 + (y % 4 == 0);
if(timestamp < i)
break;
timestamp -= i;
}
dst->year = y;
++timestamp;
for(i = 0; timestamp > rtc2_monthes[i]; ++i){
if(i == 1 && y % 4 == 0){
if(timestamp > 29)
timestamp -= 1;
else
break;
}
timestamp -= rtc2_monthes[i];
}
dst->month = i + 1;
dst->date = timestamp;
dst->format = 0;
return 1;
}
#endif
// }}}
#endif
// }}}
// RAM access {{{
#if RTC2_RAM
// RAM has same access method, just different address
void rtc2_mem_write_byte(uint8_t offset, uint8_t val){
offset += RTC2_MEM_START_WRITE;
// check for addres validity
if(RTC2_MEM_WRITE_INVALID(offset))
return; // kind of panic here or something
rtc2_write(offset, val);
}
uint8_t rtc2_mem_read_byte(uint8_t offset){
offset += RTC2_MEM_START;
if(RTC2_MEM_READ_INVALID(offset))
return 0;
return rtc2_read(offset);
}
void rtc2_mem_write(uint8_t offset, size_t size, const void *buffer){
offset += RTC2_MEM_START_WRITE;
// check that first byte is valid *and* last byte is valid too
// (hence whole chunk fits)
if(RTC2_MEM_WRITE_INVALID(offset) || RTC2_MEM_WRITE_INVALID(offset + size * 2))
return;
#if RTC2_BURST
if(size > 2 && offset == RTC2_MEM_START_WRITE){
RTC2_START_TRANSMISSION(RTC2_BURST_MEM_WRITE);
for(;size > 0; offset += 2, --size, ++buffer)
rtc2_write_byte(*(uint8_t*)buffer);
RTC2_STOP_TRANSMISSION;
}else{
#endif
for(; size > 0; offset += 2, --size, ++buffer){
rtc2_write(offset, *(uint8_t*)buffer);
}
#if RTC2_BURST
}
#endif
}
void rtc2_mem_read(uint8_t offset, size_t size, void *buffer){
offset += RTC2_MEM_START;
if(RTC2_MEM_READ_INVALID(offset) || RTC2_MEM_READ_INVALID(offset + size * 2))
return;
#if RTC2_BURST
if(size > 1 && offset == RTC2_MEM_START){
RTC2_START_TRANSMISSION(RTC2_BURST_MEM_READ);
for(; size > 0; offset += 2, --size, ++buffer)
*(uint8_t*)buffer = rtc2_read_byte();
RTC2_STOP_TRANSMISSION;
}else{
#endif
for(; size > 0; offset += 2, --size, ++buffer)
*(uint8_t*)buffer = rtc2_read(offset);
#if RTC2_BURST
}
#endif
}
// RAM string functions {{{
#if RTC2_RAM_STRINGS
void rtc2_mem_puts(uint8_t offset, const char *str){
rtc2_mem_write(offset, strlen(str) + 1, str);
}
void rtc2_mem_gets(uint8_t offset, size_t maxlen, char *str){
offset += RTC2_MEM_START;
if(RTC2_MEM_READ_INVALID(offset))
return;
// here we read until 0 byte or up to maxlen - 1
// to guarantee we always have a valid C string.
for(; maxlen > 1; --maxlen, ++str, offset += 2){
*str = rtc2_read(offset);
if(*str == 0)
return;
}
*str = 0;
}
#endif
// }}}
#endif
// }}}
// Utility functions {{{
#if RTC2_UTILITY
void rtc2_set_charger(uint8_t flags){
rtc2_write(RTC2_CHARGER_WRITE, flags);
}
uint8_t rtc2_get_charger(void){
return rtc2_read(RTC2_CHARGER_READ);
}
uint8_t rtc2_halt(void){
return rtc2_read(RTC2_SECONDS_READ) >> 7;
}
void rtc2_set_halt(uint8_t v){
rtc2_write(RTC2_SECONDS_WRITE, v << 7);
}
uint8_t rtc2_protection(void){
return rtc2_read(RTC2_WP_READ) >> 7;
}
void rtc2_set_protection(uint8_t v){
rtc2_write(RTC2_WP_WRITE, v << 7);
}
#endif
// }}}