adding lcd i2c and rtc support for tinibios
[fw/sdcc] / device / lib / ds390 / rtc390.c
1 /*-------------------------------------------------------------------------
2   rtc390.c - rtc routines for the DS1315 (tested on TINI)
3   
4    Written By - Johan Knol, johan.knol@iduna.nl
5     
6    This program 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
9    later version.
10    
11    This program 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.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19    
20    In other words, you are welcome to use, share and improve this program.
21    You are forbidden to forbid anyone else to use, share and improve
22    what you give them.   Help stamp out software-hoarding!  
23 -------------------------------------------------------------------------*/
24
25 #include <tinibios.h>
26
27 #include <stdio.h>
28 #include <ctype.h>
29
30 /* this is the address of the ds1315 phantom time chip, although
31    it doesn't really matter as long as it's in the 300000-3ffff
32    range since the chip only uses CE3*
33 */
34
35 xdata at 0x310000 static volatile unsigned char rtc;
36
37 // this is the 64bit pattern that has to be recognized by the ds1315
38 code unsigned char rtcMagic[8]={0xc5,0x3a,0xa3,0x5c,0xc5,0x3a,0xa3,0x5c};
39
40 static struct RTCDate{
41   int year;
42   unsigned char month, day, weekDay, hour, minute, second, hundredth;
43 };
44
45 #define BCDtoINT(x) (((x)&0x0f)+((x)>>4)*10)
46 #define INTtoBCD(x) (((x)%10)+(((x)/10)<<4))
47
48 static void RtcSync(void) {
49   unsigned char dummy, byte,bitMask;
50
51   // reset rtc chip
52   dummy=rtc;
53
54   // say the magic word
55   for (byte=0; byte<8; byte++) {
56     for (bitMask=0x01; bitMask; bitMask<<=1) {
57       rtc = (rtcMagic[byte]&bitMask) ? 0xff : 0x00;
58     }
59   }
60 }
61
62 unsigned char RtcRead(struct RTCDate *rtcDate) {
63   unsigned char rtcBytes[8];
64   unsigned char byte,bitMask;
65
66   RtcSync();
67
68   for (byte=0; byte<8; byte++) {
69     rtcBytes[byte]=0;
70     for (bitMask=0x01; bitMask; bitMask<<=1) {
71       if (rtc&1) {
72         rtcBytes[byte]|=bitMask;
73       }
74     }
75   }
76   rtcDate->year=2000 + BCDtoINT(rtcBytes[7]);
77   rtcDate->month=BCDtoINT(rtcBytes[6]);
78   rtcDate->day=BCDtoINT(rtcBytes[5]);
79   rtcDate->weekDay=rtcBytes[4]&0x07;
80   rtcDate->hour=BCDtoINT(rtcBytes[3]);
81   rtcDate->minute=BCDtoINT(rtcBytes[2]);
82   rtcDate->second=BCDtoINT(rtcBytes[1]);
83   rtcDate->hundredth=BCDtoINT(rtcBytes[0]);
84   if ((rtcBytes[4]&0x30) || (rtcBytes[3]&0x80)) {
85     //oscillator not running, reset not active or in 12h mode
86     return 0;
87   }
88   return 1;
89 }
90
91 void RtcWrite(struct RTCDate *rtcDate) {
92   unsigned char rtcBytes[8];
93   unsigned char byte,bitMask;
94
95   rtcBytes[7]=INTtoBCD(rtcDate->year-2000);
96   rtcBytes[6]=INTtoBCD(rtcDate->month);
97   rtcBytes[5]=INTtoBCD(rtcDate->day);
98   rtcBytes[4]=INTtoBCD(rtcDate->weekDay)&0x07; //set 24h  mode
99   rtcBytes[3]=INTtoBCD(rtcDate->hour)&0x3f; // oscilator on, reset on
100   rtcBytes[2]=INTtoBCD(rtcDate->minute);
101   rtcBytes[1]=INTtoBCD(rtcDate->second);
102   rtcBytes[0]=INTtoBCD(rtcDate->hundredth);
103
104   RtcSync();
105
106   for (byte=0; byte<8; byte++) {
107     for (bitMask=0x01; bitMask; bitMask<<=1) {
108       rtc = (rtcBytes[byte]&bitMask) ? 0xff : 0x00;
109     }
110   }
111 }