6c45191d958632845416a078bcc6f27c88677c3b
[fw/sdcc] / device / examples / rtc390 / 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 //#define USE_LCD
31
32 #ifdef USE_LCD
33 #include "lcd.h"
34 #endif
35
36 /* this is the address of the ds1315 phantom time chip, although
37    it doesn't really matter as long as it's in the 300000-3ffff
38    range since the chip only uses CE3*
39 */
40
41 xdata at 0x310000 static volatile unsigned char rtc;
42
43 // this is the 64bit pattern that has to be recognized by the ds1315
44 code unsigned char rtcMagic[8]={0xc5,0x3a,0xa3,0x5c,0xc5,0x3a,0xa3,0x5c};
45
46 static struct RTCDate{
47   int year;
48   unsigned char month, day, weekDay, hour, minute, second, hundredth;
49 };
50
51 #define BCDtoINT(x) (((x)&0x0f)+((x)>>4)*10)
52 #define INTtoBCD(x) (((x)%10)+(((x)/10)<<4))
53
54 void RtcSync(void) {
55   unsigned char dummy, byte,bitMask;
56
57   // reset rtc chip
58   dummy=rtc;
59
60   // say the magic word
61   for (byte=0; byte<8; byte++) {
62     for (bitMask=0x01; bitMask; bitMask<<=1) {
63       rtc = (rtcMagic[byte]&bitMask) ? 0xff : 0x00;
64     }
65   }
66 }
67
68 unsigned char RtcRead(struct RTCDate *rtcDate) {
69   unsigned char rtcBytes[8];
70   unsigned char byte,bitMask;
71
72   RtcSync();
73
74   for (byte=0; byte<8; byte++) {
75     rtcBytes[byte]=0;
76     for (bitMask=0x01; bitMask; bitMask<<=1) {
77       if (rtc&1) {
78         rtcBytes[byte]|=bitMask;
79       }
80     }
81   }
82   rtcDate->year=2000 + BCDtoINT(rtcBytes[7]);
83   rtcDate->month=BCDtoINT(rtcBytes[6]);
84   rtcDate->day=BCDtoINT(rtcBytes[5]);
85   rtcDate->weekDay=rtcBytes[4]&0x07;
86   rtcDate->hour=BCDtoINT(rtcBytes[3]);
87   rtcDate->minute=BCDtoINT(rtcBytes[2]);
88   rtcDate->second=BCDtoINT(rtcBytes[1]);
89   rtcDate->hundredth=BCDtoINT(rtcBytes[0]);
90   if ((rtcBytes[4]&0x30) || (rtcBytes[3]&0x80)) {
91     //oscillator not running, reset not active or in 12h mode
92     return 0;
93   }
94   return 1;
95 }
96
97 void RtcWrite(struct RTCDate *rtcDate) {
98   unsigned char rtcBytes[8];
99   unsigned char byte,bitMask;
100
101   rtcBytes[7]=INTtoBCD(rtcDate->year-2000);
102   rtcBytes[6]=INTtoBCD(rtcDate->month);
103   rtcBytes[5]=INTtoBCD(rtcDate->day);
104   rtcBytes[4]=INTtoBCD(rtcDate->weekDay)&0x07; //set 24h  mode
105   rtcBytes[3]=INTtoBCD(rtcDate->hour)&0x3f; // oscilator on, reset on
106   rtcBytes[2]=INTtoBCD(rtcDate->minute);
107   rtcBytes[1]=INTtoBCD(rtcDate->second);
108   rtcBytes[0]=INTtoBCD(rtcDate->hundredth);
109
110   RtcSync();
111
112   for (byte=0; byte<8; byte++) {
113     for (bitMask=0x01; bitMask; bitMask<<=1) {
114       rtc = (rtcBytes[byte]&bitMask) ? 0xff : 0x00;
115     }
116   }
117 }
118
119 int ScanInt(int current) {
120   char reply[32], *r;
121
122   gets(reply);
123   if (isdigit(*(r=reply))) {
124     current=0;
125     do {
126       current*=10;
127       current+=(*r++)-'0';
128     } while (isdigit(*r));
129   }
130   return current;
131 }
132
133 char GetTime(struct RTCDate *rtcDate) {
134   printf ("Enter year [%d]: ", rtcDate->year);
135   rtcDate->year=ScanInt(rtcDate->year);
136   printf ("Enter month [%d]: ", rtcDate->month);
137   rtcDate->month=ScanInt(rtcDate->month);
138   printf ("Enter day [%d]: ", rtcDate->day);
139   rtcDate->day=ScanInt(rtcDate->day);
140   printf ("Enter hour [%d]: ", rtcDate->hour);
141   rtcDate->hour=ScanInt(rtcDate->hour);
142   printf ("Enter minute [%d]: ", rtcDate->minute);
143   rtcDate->minute=ScanInt(rtcDate->minute);
144   printf ("Enter second [%d]: ", rtcDate->second);
145   rtcDate->second=ScanInt(rtcDate->second);
146   rtcDate->hundredth=0;
147 }
148
149 void PrintTime(struct RTCDate *rtcDate) {
150   printf ("%04d-%02bd-%02bd %02bd:%02bd:%02bd.%02bd\n", 
151           rtcDate->year, rtcDate->month, rtcDate->day,
152           rtcDate->hour, rtcDate->minute, rtcDate->second,
153           rtcDate->hundredth);
154 }
155
156 unsigned int intmul(unsigned int a, unsigned int b) {
157   return a*b;
158 }
159
160 long longmul(long a, long b) {
161   return a*b;
162 }
163
164 void main (void) {
165   struct RTCDate rtcDate;
166   unsigned char seconds=0xff;
167
168   printf ("\nStarting RTC demo.\n");
169
170 #ifdef USE_LCD
171   LcdInit();
172   LcdLPrintf (0, "Starting RTC demo.");
173 #endif
174   
175   while(1) {
176     RtcRead(&rtcDate);
177
178 #ifdef USE_LCD
179     // if lcd is enabled it only syncs the second time
180     RtcRead(&rtcDate);
181 #endif    
182
183 #ifdef USE_LCD
184     LcdLPrintf (2,"%04d-%02bd-%02bd",
185                 rtcDate.year, rtcDate.month, rtcDate.day);
186     LcdLPrintf (3, "%02bd:%02bd:%02bd.%02bd", 
187                 rtcDate.hour, rtcDate.minute, rtcDate.second,
188                 rtcDate.hundredth);
189 #endif
190     if (rtcDate.second!=seconds) {
191       PrintTime(&rtcDate);
192       seconds=rtcDate.second;
193     }
194     
195     if (Serial0CharArrived()) {
196       switch (getchar()) 
197         {
198         case 0:
199           break;
200         case 'q': 
201           printf ("Ok.\n");
202           return;
203         default:
204           if (GetTime(&rtcDate)) {
205 #ifndef USE_LCD
206             PrintTime(&rtcDate);
207             RtcWrite(&rtcDate);
208             printf ("Time written.\n");
209 #endif
210           }
211         }
212     }
213   }
214
215 }