/
rtcc.c
447 lines (386 loc) · 9.32 KB
/
rtcc.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
#include "rtcc.h"
#include "i2c.h"
#include "os.h"
#include <xc.h>
static uint8_t _rtcc_bcd_to_decimal(uint8_t input)
{
uint8_t tens = (input & 0xF0) >> 4;
uint8_t ones = input & 0x0F;
return ((10*tens) + ones);
}
static void _rtcc_write_enable(void)
{
while(!RTCCFGbits.RTCWREN)
{
EECON2 = 0x55; //Make RTCWREN bit writable
EECON2 = 0xAA;
RTCCFGbits.RTCWREN = 1; //Enable writes
}
}
static void _rtcc_write_disable(void)
{
while(RTCCFGbits.RTCWREN)
{
EECON2 = 0x55; //Make RTCWREN bit writable
EECON2 = 0xAA;
RTCCFGbits.RTCWREN = 0; //Enable writes
}
}
static uint8_t _rtcc_increment_bcd(uint8_t value, uint8_t min, uint8_t max)
{
if(value==max)
return min;
++value;
if((value&0x0F)>0x09)
{
value += 0x10;
value &= 0xF0;
}
return value;
}
static uint8_t _rtcc_decrement_bcd(uint8_t value, uint8_t min, uint8_t max)
{
if(value==min)
return max;
--value;
if((value&0x0F)>0x09)
{
value &= 0xF0;
value |= 0x09;
}
return value;
}
static uint8_t _number_of_days(void)
{
uint8_t month = rtcc_get_month();
uint8_t bcd_year = rtcc_get_year();
uint8_t year;
if(month==0x04 || month==0x06 || month==0x09 || month==0x11)
return 0x30;
if(month==0x02)
{
year = bcd_year>>4;
year *= 10;
year += bcd_year & 0x0F;
if(year&0b11) // no leap year
return 0x28;
else //leap year
return 0x29;
}
return 0x31;
}
static uint8_t _rtcc_verify_bcd(uint8_t value, uint8_t min, uint8_t max)
{
//Ensure that both decimals are in the range 0-9
if((value&0x0F)>0x09)
{
return min;
}
if((value&0xF0)>0x90)
{
return min;
}
//Ensure values are within their limits
if(value<min)
{
return min;
}
if(value>max)
{
return max;
}
//All is well, return unchanged value
return value;
}
void rtcc_init(void)
{
EECON2 = 0x55; //Make RTCWREN bit writable
EECON2 = 0xAA;
RTCCFGbits.RTCWREN = 1; //Enable writes
RTCCFGbits.RTCEN = 1;
RTCCFGbits.RTCOE = 0;
RTCCAL = 0x00; //No calibration
//Restore date and time from EEPROM
rtcc_read_eeprom();
}
void rtcc_correct_day(void)
{
uint8_t day = rtcc_get_day();
uint8_t max = _number_of_days();
if(day>max)
rtcc_set_day(max);
}
uint8_t rtcc_get_year(void)
{
RTCCFGbits.RTCPTR1 = 1;
RTCCFGbits.RTCPTR0 = 1;
while(RTCCFGbits.RTCSYNC);
return RTCVALL;
}
uint8_t rtcc_get_year_decimal(void)
{
uint8_t bcd_year = rtcc_get_year();
return _rtcc_bcd_to_decimal(bcd_year);
}
void rtcc_set_year(uint8_t year)
{
_rtcc_write_enable();
RTCCFGbits.RTCPTR1 = 1;
RTCCFGbits.RTCPTR0 = 1;
RTCVALL = year;
_rtcc_write_disable();
}
void rtcc_increment_year(void)
{
uint8_t year = rtcc_get_year();
year = _rtcc_increment_bcd(year, 0x00, 0x99);
rtcc_set_year(year);
}
void rtcc_decrement_year(void)
{
uint8_t year = rtcc_get_year();
year = _rtcc_decrement_bcd(year, 0x00, 0x99);
rtcc_set_year(year);
}
uint8_t rtcc_get_month(void)
{
RTCCFGbits.RTCPTR1 = 1;
RTCCFGbits.RTCPTR0 = 0;
while(RTCCFGbits.RTCSYNC);
return RTCVALH;
}
uint8_t rtcc_get_month_decimal(void)
{
uint8_t bcd_month = rtcc_get_month();
return _rtcc_bcd_to_decimal(bcd_month);
}
void rtcc_set_month(uint8_t month)
{
_rtcc_write_enable();
RTCCFGbits.RTCPTR1 = 1;
RTCCFGbits.RTCPTR0 = 0;
RTCVALH = month;
_rtcc_write_disable();
}
void rtcc_increment_month(void)
{
uint8_t month = rtcc_get_month();
month = _rtcc_increment_bcd(month, 0x01, 0x12);
rtcc_set_month(month);
}
void rtcc_decrement_month(void)
{
uint8_t month = rtcc_get_month();
month = _rtcc_decrement_bcd(month, 0x01, 0x12);
rtcc_set_month(month);
}
uint8_t rtcc_get_day(void)
{
RTCCFGbits.RTCPTR1 = 1;
RTCCFGbits.RTCPTR0 = 0;
while(RTCCFGbits.RTCSYNC);
return RTCVALL;
}
uint8_t rtcc_get_day_decimal(void)
{
uint8_t bcd_day = rtcc_get_day();
return _rtcc_bcd_to_decimal(bcd_day);
}
void rtcc_set_day(uint8_t day)
{
_rtcc_write_enable();
RTCCFGbits.RTCPTR1 = 1;
RTCCFGbits.RTCPTR0 = 0;
RTCVALL = day;
_rtcc_write_disable();
}
void rtcc_increment_day(void)
{
uint8_t day = rtcc_get_day();
day = _rtcc_increment_bcd(day, 0x01, _number_of_days());
rtcc_set_day(day);
}
void rtcc_decrement_day(void)
{
uint8_t day = rtcc_get_day();
day = _rtcc_decrement_bcd(day, 0x01, _number_of_days());
rtcc_set_day(day);
}
uint8_t rtcc_get_hours(void)
{
RTCCFGbits.RTCPTR1 = 0;
RTCCFGbits.RTCPTR0 = 1;
while(RTCCFGbits.RTCSYNC);
return RTCVALL;
}
uint8_t rtcc_get_hours_decimal(void)
{
uint8_t bcd_hours = rtcc_get_hours();
return _rtcc_bcd_to_decimal(bcd_hours);
}
void rtcc_set_hours(uint8_t hours)
{
_rtcc_write_enable();
RTCCFGbits.RTCPTR1 = 0;
RTCCFGbits.RTCPTR0 = 1;
RTCVALL = hours;
_rtcc_write_disable();
}
void rtcc_increment_hours(void)
{
uint8_t hours = rtcc_get_hours();
hours = _rtcc_increment_bcd(hours, 0x00, 0x23);
rtcc_set_hours(hours);
}
void rtcc_decrement_hours(void)
{
uint8_t hours = rtcc_get_hours();
hours = _rtcc_decrement_bcd(hours, 0x00, 0x23);
rtcc_set_hours(hours);
}
uint8_t rtcc_get_minutes(void)
{
RTCCFGbits.RTCPTR1 = 0;
RTCCFGbits.RTCPTR0 = 0;
while(RTCCFGbits.RTCSYNC);
return RTCVALH;
}
uint8_t rtcc_get_minutes_decimal(void)
{
uint8_t bcd_minutes = rtcc_get_minutes();
return _rtcc_bcd_to_decimal(bcd_minutes);
}
void rtcc_set_minutes(uint8_t minutes)
{
_rtcc_write_enable();
RTCCFGbits.RTCPTR1 = 0;
RTCCFGbits.RTCPTR0 = 0;
RTCVALH = minutes;
_rtcc_write_disable();
}
void rtcc_increment_minutes(void)
{
uint8_t minutes = rtcc_get_minutes();
minutes = _rtcc_increment_bcd(minutes, 0x00, 0x59);
rtcc_set_minutes(minutes);
}
void rtcc_decrement_minutes(void)
{
uint8_t minutes = rtcc_get_minutes();
minutes = _rtcc_decrement_bcd(minutes, 0x00, 0x59);
rtcc_set_minutes(minutes);
}
uint8_t rtcc_get_seconds(void)
{
RTCCFGbits.RTCPTR1 = 0;
RTCCFGbits.RTCPTR0 = 0;
while(RTCCFGbits.RTCSYNC);
return RTCVALL;
}
uint8_t rtcc_get_seconds_decimal(void)
{
uint8_t bcd_seconds = rtcc_get_seconds();
return _rtcc_bcd_to_decimal(bcd_seconds);
}
void rtcc_set_seconds(uint8_t seconds)
{
_rtcc_write_enable();
RTCCFGbits.RTCPTR1 = 0;
RTCCFGbits.RTCPTR0 = 0;
RTCVALL = seconds;
_rtcc_write_disable();
}
void rtcc_increment_seconds(void)
{
uint8_t seconds = rtcc_get_seconds();
seconds = _rtcc_increment_bcd(seconds, 0x00, 0x59);
rtcc_set_seconds(seconds);
}
void rtcc_decrement_seconds(void)
{
uint8_t seconds = rtcc_get_seconds();
seconds = _rtcc_decrement_bcd(seconds, 0x00, 0x59);
rtcc_set_seconds(seconds);
}
void rtcc_read_eeprom(void)
{
uint8_t date[6];
uint8_t buffer;
uint8_t error_found = 0;
//Read values from eeprom
i2c_eeprom_read(EEPROM_RTCC_ADDRESS, &date[0], 6);
//Correct values from eeprom
//If this is the first time this code runs, EEPROM may contain invalid data
//year
buffer = _rtcc_verify_bcd(date[0], 0x00, 0x99);
if(buffer != date[0])
{
date[0] = buffer;
error_found = 1;
}
rtcc_set_year(date[0]);
//month
buffer = _rtcc_verify_bcd(date[1], 0x01, 0x12);
if(buffer != date[1])
{
date[1] = buffer;
error_found = 1;
}
rtcc_set_month(date[1]);
//day
buffer = _rtcc_verify_bcd(date[2], 0x01, 0x31);
if(buffer != date[2])
{
date[2] = buffer;
error_found = 1;
}
rtcc_set_day(date[2]);
//correct day if necessary
rtcc_correct_day();
buffer = rtcc_get_day();
if(buffer != date[2])
{
date[2] = buffer;
error_found = 1;
}
//hours
buffer = _rtcc_verify_bcd(date[3], 0x00, 0x23);
if(buffer != date[3])
{
date[3] = buffer;
error_found = 1;
}
rtcc_set_hours(date[3]);
//minutes
buffer = _rtcc_verify_bcd(date[4], 0x00, 0x59);
if(buffer != date[4])
{
date[4] = buffer;
error_found = 1;
}
rtcc_set_minutes(date[4]);
//seconds
buffer = _rtcc_verify_bcd(date[5], 0x00, 0x59);
if(buffer != date[5])
{
date[5] = buffer;
error_found = 1;
}
rtcc_set_seconds(date[5]);
//Write date/time to EEPROM if any correction was necessary
if(error_found)
{
rtcc_write_eeprom();
}
}
void rtcc_write_eeprom(void)
{
uint8_t date[6];
date[0] = rtcc_get_year();
date[1] = rtcc_get_month();
date[2] = rtcc_get_day();
date[3] = rtcc_get_hours();
date[4] = rtcc_get_minutes();
date[5] = rtcc_get_seconds();
i2c_eeprom_write(EEPROM_RTCC_ADDRESS, &date[0], 6);
}