1 /*-------------------------------------------------------------------------
2 rtc390.c - rtc routines for the DS1315 (tested on TINI)
4 Copyright (C) 2001, Johan Knol <johan.knol AT iduna.nl>
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this library; see the file COPYING. If not, write to the
18 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
21 As a special exception, if you link this library with other files,
22 some of which are compiled with SDCC, to produce an executable,
23 this library does not by itself cause the resulting executable to
24 be covered by the GNU General Public License. This exception does
25 not however invalidate any other reasons why the executable file
26 might be covered by the GNU General Public License.
27 -------------------------------------------------------------------------*/
34 /* this is the address of the ds1315 phantom time chip, although
35 it doesn't really matter as long as it's in the 300000-3fffff
36 range since the chip only uses CE3*
39 __xdata
static volatile unsigned char __at (0x310000) rtc
;
41 // this is the 64bit pattern that has to be recognized by the ds1315
42 __code
unsigned char rtcMagic
[8]={0xc5,0x3a,0xa3,0x5c,0xc5,0x3a,0xa3,0x5c};
44 #define BCDtoINT(x) (((x)&0x0f)+((x)>>4)*10)
45 #define INTtoBCD(x) (((x)%10)+(((x)/10)<<4))
47 static void RtcSync(void) {
48 unsigned char dummy
, byte
,bitMask
;
54 for (byte
=0; byte
<8; byte
++) {
55 for (bitMask
=0x01; bitMask
; bitMask
<<=1) {
56 rtc
= (rtcMagic
[byte
]&bitMask
) ? 0xff : 0x00;
61 unsigned char RtcRead(struct tm
*rtcDate
) {
62 unsigned char rtcBytes
[8];
63 unsigned char byte
,bitMask
;
67 for (byte
=0; byte
<8; byte
++) {
69 for (bitMask
=0x01; bitMask
; bitMask
<<=1) {
71 rtcBytes
[byte
]|=bitMask
;
75 rtcDate
->tm_year
=BCDtoINT(rtcBytes
[7])+100; // year since 1900
76 rtcDate
->tm_mon
=BCDtoINT(rtcBytes
[6])-1; // jan=0
77 rtcDate
->tm_mday
=BCDtoINT(rtcBytes
[5]);
78 rtcDate
->tm_wday
=(rtcBytes
[4]&0x07)-1; // monday=0?
79 rtcDate
->tm_hour
=BCDtoINT(rtcBytes
[3]);
80 rtcDate
->tm_min
=BCDtoINT(rtcBytes
[2]);
81 rtcDate
->tm_sec
=BCDtoINT(rtcBytes
[1]);
82 rtcDate
->tm_hundredth
=BCDtoINT(rtcBytes
[0]);
83 if ((rtcBytes
[4]&0x30) || (rtcBytes
[3]&0x80)) {
84 //oscillator not running, reset not active or in 12h mode
90 void RtcWrite(struct tm
*rtcDate
) {
91 unsigned char rtcBytes
[8];
92 unsigned char byte
,bitMask
;
94 rtcBytes
[7]=INTtoBCD(rtcDate
->tm_year
%100);
95 rtcBytes
[6]=INTtoBCD(rtcDate
->tm_mon
)+1;
96 rtcBytes
[5]=INTtoBCD(rtcDate
->tm_mday
);
97 rtcBytes
[4]=(INTtoBCD(rtcDate
->tm_wday
)+1)&0x07; //set 24h mode
98 rtcBytes
[3]=INTtoBCD(rtcDate
->tm_hour
)&0x3f; // oscillator on, reset on
99 rtcBytes
[2]=INTtoBCD(rtcDate
->tm_min
);
100 rtcBytes
[1]=INTtoBCD(rtcDate
->tm_sec
);
101 //rtcBytes[0]=INTtoBCD(rtcDate->hundredth);
106 for (byte
=0; byte
<8; byte
++) {
107 for (bitMask
=0x01; bitMask
; bitMask
<<=1) {
108 rtc
= (rtcBytes
[byte
]&bitMask
) ? 0xff : 0x00;