Imported Upstream version 2.9.0
[debian/cc1111] / 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 #include <stdio.h>
27 #include <time.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-3fffff
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 #define BCDtoINT(x) (((x)&0x0f)+((x)>>4)*10)
41 #define INTtoBCD(x) (((x)%10)+(((x)/10)<<4))
42
43 static void RtcSync(void) {
44   unsigned char dummy, byte,bitMask;
45
46   // reset rtc chip
47   dummy=rtc;
48
49   // say the magic word
50   for (byte=0; byte<8; byte++) {
51     for (bitMask=0x01; bitMask; bitMask<<=1) {
52       rtc = (rtcMagic[byte]&bitMask) ? 0xff : 0x00;
53     }
54   }
55 }
56
57 unsigned char RtcRead(struct tm *rtcDate) {
58   unsigned char rtcBytes[8];
59   unsigned char byte,bitMask;
60
61   RtcSync();
62
63   for (byte=0; byte<8; byte++) {
64     rtcBytes[byte]=0;
65     for (bitMask=0x01; bitMask; bitMask<<=1) {
66       if (rtc&1) {
67         rtcBytes[byte]|=bitMask;
68       }
69     }
70   }
71   rtcDate->tm_year=BCDtoINT(rtcBytes[7])+100; // year since 1900
72   rtcDate->tm_mon=BCDtoINT(rtcBytes[6])-1; // jan=0
73   rtcDate->tm_mday=BCDtoINT(rtcBytes[5]);
74   rtcDate->tm_wday=(rtcBytes[4]&0x07)-1; // monday=0?
75   rtcDate->tm_hour=BCDtoINT(rtcBytes[3]);
76   rtcDate->tm_min=BCDtoINT(rtcBytes[2]);
77   rtcDate->tm_sec=BCDtoINT(rtcBytes[1]);
78   rtcDate->tm_hundredth=BCDtoINT(rtcBytes[0]);
79   if ((rtcBytes[4]&0x30) || (rtcBytes[3]&0x80)) {
80     //oscillator not running, reset not active or in 12h mode
81     return 0;
82   }
83   return 1;
84 }
85
86 void RtcWrite(struct tm *rtcDate) {
87   unsigned char rtcBytes[8];
88   unsigned char byte,bitMask;
89
90   rtcBytes[7]=INTtoBCD(rtcDate->tm_year%100);
91   rtcBytes[6]=INTtoBCD(rtcDate->tm_mon)+1;
92   rtcBytes[5]=INTtoBCD(rtcDate->tm_mday);
93   rtcBytes[4]=(INTtoBCD(rtcDate->tm_wday)+1)&0x07; //set 24h  mode
94   rtcBytes[3]=INTtoBCD(rtcDate->tm_hour)&0x3f; // oscilator on, reset on
95   rtcBytes[2]=INTtoBCD(rtcDate->tm_min);
96   rtcBytes[1]=INTtoBCD(rtcDate->tm_sec);
97   //rtcBytes[0]=INTtoBCD(rtcDate->hundredth);
98   rtcBytes[0]=0;
99
100   RtcSync();
101
102   for (byte=0; byte<8; byte++) {
103     for (bitMask=0x01; bitMask; bitMask<<=1) {
104       rtc = (rtcBytes[byte]&bitMask) ? 0xff : 0x00;
105     }
106   }
107 }