FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / firmware / drivers / rtc / rtc_as3514.c
blobd0c4cd7c17cf9deffe75cbcbe82f49ac92431a50
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include <stdbool.h>
22 #include "config.h"
23 #include "rtc.h"
24 #include "as3514.h"
25 #include "ascodec.h"
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 */
30 #else
31 #define SECS_ADJUST 0
32 #endif
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)) )
50 return true;
51 else
52 return false;
55 void rtc_init(void)
59 int rtc_read_datetime(unsigned char* buf)
61 char tmp[4];
62 int year;
63 int i;
64 unsigned int seconds;
66 /* RTC_AS3514's slave address is 0x46*/
67 for (i=0;i<4;i++){
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 */
76 /* weekday */
77 buf[3] = ((seconds % WEEK_SECONDS) / DAY_SECONDS + 2) % 7;
79 /* Year */
80 year = 1980;
81 while(seconds>=LEAP_YEAR_SECONDS)
83 if(is_leapyear(year)){
84 seconds -= LEAP_YEAR_SECONDS;
85 } else {
86 seconds -= YEAR_SECONDS;
89 year++;
92 if(is_leapyear(year)) {
93 days_in_month[1] = 29;
94 } else {
95 days_in_month[1] = 28;
96 if(seconds>YEAR_SECONDS){
97 year++;
98 seconds -= YEAR_SECONDS;
101 buf[6] = year%100;
103 /* Month */
104 for(i=0; i<12; i++)
106 if(seconds < days_in_month[i]*DAY_SECONDS){
107 buf[5] = i+1;
108 break;
111 seconds -= days_in_month[i]*DAY_SECONDS;
114 /* Month Day */
115 buf[4] = seconds/DAY_SECONDS;
116 seconds -= buf[4]*DAY_SECONDS;
117 buf[4]++; /* 1 ... 31 */
119 /* Hour */
120 buf[2] = seconds/HOUR_SECONDS;
121 seconds -= buf[2]*HOUR_SECONDS;
123 /* Minute */
124 buf[1] = seconds/MINUTE_SECONDS;
125 seconds -= buf[1]*MINUTE_SECONDS;
127 /* Second */
128 buf[0] = seconds;
130 /* Convert to Binary Coded Decimal format */
131 for(i=0; i<7; i++)
132 buf[i] = DEC2BCD(buf[i]);
134 return 7;
137 int rtc_write_datetime(unsigned char* buf)
139 int i, year;
140 unsigned int year_days = 0;
141 unsigned int month_days = 0;
142 unsigned int seconds = 0;
144 /* Convert from Binary Coded Decimal format */
145 for(i=0; i<7; i++)
146 buf[i] = BCD2DEC(buf[i]);
148 year = 2000 + buf[6];
150 if(is_leapyear(year)) {
151 days_in_month[1] = 29;
152 } else {
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 */
165 seconds = buf[0]
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 */
174 for (i=0;i<4;i++){
175 ascodec_write(AS3514_RTC_0 + i, ((seconds >> (8 * i)) & 0xff));
177 return 1;