Add support to buildzip.pl for Lua scripts
[kugel-rb.git] / firmware / drivers / rtc / rtc_as3514.c
blob43368d2dd1e7fbaea4d0c309de43f5c09b15f7b9
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 /* Days in each month */
42 static unsigned int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
44 static inline bool is_leapyear(int year)
46 if( ((year%4)==0) && (((year%100)!=0) || ((year%400)==0)) )
47 return true;
48 else
49 return false;
52 void rtc_init(void)
56 int rtc_read_datetime(struct tm *tm)
58 char tmp[4];
59 int i, year, mday, hour, min;
60 unsigned int seconds;
62 /* RTC_AS3514's slave address is 0x46*/
63 for (i = 0; i < 4; i++){
64 tmp[i] = ascodec_read(AS3514_RTC_0 + i);
66 seconds = tmp[0] + (tmp[1]<<8) + (tmp[2]<<16) + (tmp[3]<<24);
67 seconds -= SECS_ADJUST;
69 /* Convert seconds since Jan-1-1980 to format compatible with
70 * get_time() from firmware/common/timefuncs.c */
72 /* weekday */
73 tm->tm_wday = ((seconds % WEEK_SECONDS) / DAY_SECONDS + 2) % 7;
75 /* Year */
76 year = 1980;
77 while (seconds >= LEAP_YEAR_SECONDS)
79 if (is_leapyear(year)){
80 seconds -= LEAP_YEAR_SECONDS;
81 } else {
82 seconds -= YEAR_SECONDS;
85 year++;
88 if (is_leapyear(year)) {
89 days_in_month[1] = 29;
90 } else {
91 days_in_month[1] = 28;
92 if(seconds>YEAR_SECONDS){
93 year++;
94 seconds -= YEAR_SECONDS;
97 tm->tm_year = year%100 + 100;
99 /* Month */
100 for (i = 0; i < 12; i++)
102 if (seconds < days_in_month[i]*DAY_SECONDS){
103 tm->tm_mon = i;
104 break;
107 seconds -= days_in_month[i]*DAY_SECONDS;
110 /* Month Day */
111 mday = seconds/DAY_SECONDS;
112 seconds -= mday*DAY_SECONDS;
113 tm->tm_mday = mday + 1; /* 1 ... 31 */
115 /* Hour */
116 hour = seconds/HOUR_SECONDS;
117 seconds -= hour*HOUR_SECONDS;
118 tm->tm_hour = hour;
120 /* Minute */
121 min = seconds/MINUTE_SECONDS;
122 seconds -= min*MINUTE_SECONDS;
123 tm->tm_min = min;
125 /* Second */
126 tm->tm_sec = seconds;
128 return 7;
131 int rtc_write_datetime(const struct tm *tm)
133 int i, year;
134 unsigned int year_days = 0;
135 unsigned int month_days = 0;
136 unsigned int seconds = 0;
138 year = 2000 + tm->tm_year - 100;
140 if(is_leapyear(year)) {
141 days_in_month[1] = 29;
142 } else {
143 days_in_month[1] = 28;
146 /* Number of days in months gone by this year*/
147 for(i = 0; i < tm->tm_mon; i++){
148 month_days += days_in_month[i];
151 /* Number of days in years gone by since 1-Jan-1980 */
152 year_days = 365*(tm->tm_year-100+20) + (tm->tm_year-100-1)/4 + 6;
154 /* Convert to seconds since 1-Jan-1980 */
155 seconds = tm->tm_sec
156 + tm->tm_min*MINUTE_SECONDS
157 + tm->tm_hour*HOUR_SECONDS
158 + (tm->tm_mday-1)*DAY_SECONDS
159 + month_days*DAY_SECONDS
160 + year_days*DAY_SECONDS;
161 seconds += SECS_ADJUST;
163 /* Send data to RTC */
164 for (i=0; i<4; i++){
165 ascodec_write(AS3514_RTC_0 + i, ((seconds >> (8 * i)) & 0xff));
167 return 1;