1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007 by Barry Wardell
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
27 /* AMS Sansas start counting from Jan 1st 1970 instead of 1980 */
28 #if (CONFIG_CPU==AS3525)
29 #define SECS_ADJUST 315532800 /* seconds between 1970-1-1 and 1980-1-1 */
34 #define MINUTE_SECONDS 60
35 #define HOUR_SECONDS 3600
36 #define DAY_SECONDS 86400
37 #define WEEK_SECONDS 604800
38 #define YEAR_SECONDS 31536000
39 #define LEAP_YEAR_SECONDS 31622400
41 #define BCD2DEC(X) (((((X)>>4) & 0x0f) * 10) + ((X) & 0xf))
42 #define DEC2BCD(X) ((((X)/10)<<4) | ((X)%10))
44 /* Days in each month */
45 static unsigned int days_in_month
[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
47 static inline bool is_leapyear(int year
)
49 if( ((year
%4)==0) && (((year
%100)!=0) || ((year
%400)==0)) )
59 int rtc_read_datetime(unsigned char* buf
)
66 /* RTC_AS3514's slave address is 0x46*/
68 tmp
[i
] = ascodec_read(AS3514_RTC_0
+ i
);
70 seconds
= tmp
[0] + (tmp
[1]<<8) + (tmp
[2]<<16) + (tmp
[3]<<24);
71 seconds
-= SECS_ADJUST
;
73 /* Convert seconds since Jan-1-1980 to format compatible with
74 * get_time() from firmware/common/timefuncs.c */
77 buf
[3] = ((seconds
% WEEK_SECONDS
) / DAY_SECONDS
+ 2) % 7;
81 while(seconds
>=LEAP_YEAR_SECONDS
)
83 if(is_leapyear(year
)){
84 seconds
-= LEAP_YEAR_SECONDS
;
86 seconds
-= YEAR_SECONDS
;
92 if(is_leapyear(year
)) {
93 days_in_month
[1] = 29;
95 days_in_month
[1] = 28;
96 if(seconds
>YEAR_SECONDS
){
98 seconds
-= YEAR_SECONDS
;
106 if(seconds
< days_in_month
[i
]*DAY_SECONDS
){
111 seconds
-= days_in_month
[i
]*DAY_SECONDS
;
115 buf
[4] = seconds
/DAY_SECONDS
;
116 seconds
-= buf
[4]*DAY_SECONDS
;
117 buf
[4]++; /* 1 ... 31 */
120 buf
[2] = seconds
/HOUR_SECONDS
;
121 seconds
-= buf
[2]*HOUR_SECONDS
;
124 buf
[1] = seconds
/MINUTE_SECONDS
;
125 seconds
-= buf
[1]*MINUTE_SECONDS
;
130 /* Convert to Binary Coded Decimal format */
132 buf
[i
] = DEC2BCD(buf
[i
]);
137 int rtc_write_datetime(unsigned char* buf
)
140 unsigned int year_days
= 0;
141 unsigned int month_days
= 0;
142 unsigned int seconds
= 0;
144 /* Convert from Binary Coded Decimal format */
146 buf
[i
] = BCD2DEC(buf
[i
]);
148 year
= 2000 + buf
[6];
150 if(is_leapyear(year
)) {
151 days_in_month
[1] = 29;
153 days_in_month
[1] = 28;
156 /* Number of days in months gone by this year*/
157 for(i
=0; i
<(buf
[5]-1); i
++){
158 month_days
+= days_in_month
[i
];
161 /* Number of days in years gone by since 1-Jan-1980 */
162 year_days
= 365*(buf
[6]+20) + (buf
[6]-1)/4 + 6;
164 /* Convert to seconds since 1-Jan-1980 */
166 + buf
[1]*MINUTE_SECONDS
167 + buf
[2]*HOUR_SECONDS
168 + (buf
[4]-1)*DAY_SECONDS
169 + month_days
*DAY_SECONDS
170 + year_days
*DAY_SECONDS
;
171 seconds
+= SECS_ADJUST
;
173 /* Send data to RTC */
175 ascodec_write(AS3514_RTC_0
+ i
, ((seconds
>> (8 * i
)) & 0xff));