Make the get_time on non-RTC sims behave like target, i.e. always return a constant...
[kugel-rb/myfork.git] / firmware / common / timefuncs.c
blobcc54840376cb1ac78368f34cc46984dec39d8772
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
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 ****************************************************************************/
22 #include <stdio.h> /* get NULL */
23 #include "config.h"
25 #include "kernel.h"
26 #include "rtc.h"
27 #include "timefuncs.h"
28 #include "debug.h"
30 #if !(defined SIMULATOR && CONFIG_RTC)
32 static struct tm tm;
34 static void fill_default_tm(struct tm *tm)
36 tm->tm_sec = 0;
37 tm->tm_min = 0;
38 tm->tm_hour = 0;
39 tm->tm_mday = 1;
40 tm->tm_mon = 0;
41 tm->tm_year = 70;
42 tm->tm_wday = 1;
43 tm->tm_yday = 0; /* Not implemented for now */
44 tm->tm_isdst = -1; /* Not implemented for now */
46 #endif /* !(defined SIMULATOR && CONFIG_RTC)*/
48 bool valid_time(const struct tm *tm)
50 if (tm->tm_hour < 0 || tm->tm_hour > 23 ||
51 tm->tm_sec < 0 || tm->tm_sec > 59 ||
52 tm->tm_min < 0 || tm->tm_min > 59 ||
53 tm->tm_year < 100 || tm->tm_year > 199 ||
54 tm->tm_mon < 0 || tm->tm_mon > 11 ||
55 tm->tm_wday < 0 || tm->tm_wday > 6 ||
56 tm->tm_mday < 1 || tm->tm_mday > 31)
57 return false;
58 else
59 return true;
62 struct tm *get_time(void)
64 #ifndef SIMULATOR
65 #if CONFIG_RTC
66 static long timeout = 0;
68 /* Don't read the RTC more than once per second */
69 if (current_tick > timeout)
71 /* Once per second, 1/10th of a second off */
72 timeout = HZ * (current_tick / HZ + 1) + HZ / 5;
73 #if CONFIG_RTC != RTC_JZ47XX
74 char rtcbuf[7];
75 rtc_read_datetime(rtcbuf);
77 tm.tm_sec = ((rtcbuf[0] & 0x70) >> 4) * 10 + (rtcbuf[0] & 0x0f);
78 tm.tm_min = ((rtcbuf[1] & 0x70) >> 4) * 10 + (rtcbuf[1] & 0x0f);
79 tm.tm_hour = ((rtcbuf[2] & 0x30) >> 4) * 10 + (rtcbuf[2] & 0x0f);
80 tm.tm_wday = rtcbuf[3] & 0x07;
81 tm.tm_mday = ((rtcbuf[4] & 0x30) >> 4) * 10 + (rtcbuf[4] & 0x0f);
82 tm.tm_mon = ((rtcbuf[5] & 0x10) >> 4) * 10 + (rtcbuf[5] & 0x0f) - 1;
83 #ifdef IRIVER_H300_SERIES
84 /* Special kludge to coexist with the iriver firmware. The iriver firmware
85 stores the date as 1965+nn, and allows a range of 1980..2064. We use
86 1964+nn here to make leap years work correctly, so the date will be one
87 year off in the iriver firmware but at least won't be reset anymore. */
88 tm.tm_year = ((rtcbuf[6] & 0xf0) >> 4) * 10 + (rtcbuf[6] & 0x0f) + 64;
89 #else /* Not IRIVER_H300_SERIES */
90 tm.tm_year = ((rtcbuf[6] & 0xf0) >> 4) * 10 + (rtcbuf[6] & 0x0f) + 100;
91 #endif /* IRIVER_H300_SERIES */
93 tm.tm_yday = 0; /* Not implemented for now */
94 tm.tm_isdst = -1; /* Not implemented for now */
95 #else /* CONFIG_RTC == RTC_JZ47XX */
96 rtc_read_datetime((unsigned char*)&tm);
97 #endif /* CONFIG_RTC */
99 #else /* No RTC */
100 fill_default_tm(&tm);
101 #endif /* RTC */
102 return &tm;
104 #else /* SIMULATOR */
105 #if CONFIG_RTC
106 time_t now = time(NULL);
107 return localtime(&now);
108 #else /* Simulator, no RTC */
109 fill_default_tm(&tm);
110 return &tm;
111 #endif
112 #endif /* SIMULATOR */
115 int set_time(const struct tm *tm)
117 #if CONFIG_RTC
118 int rc;
119 #if CONFIG_RTC != RTC_JZ47XX
120 char rtcbuf[7];
121 #endif
123 if (valid_time(tm))
125 #if CONFIG_RTC != RTC_JZ47XX
126 rtcbuf[0]=((tm->tm_sec/10) << 4) | (tm->tm_sec%10);
127 rtcbuf[1]=((tm->tm_min/10) << 4) | (tm->tm_min%10);
128 rtcbuf[2]=((tm->tm_hour/10) << 4) | (tm->tm_hour%10);
129 rtcbuf[3]=tm->tm_wday;
130 rtcbuf[4]=((tm->tm_mday/10) << 4) | (tm->tm_mday%10);
131 rtcbuf[5]=(((tm->tm_mon+1)/10) << 4) | ((tm->tm_mon+1)%10);
132 #ifdef IRIVER_H300_SERIES
133 /* Iriver firmware compatibility kludge, see get_time(). */
134 rtcbuf[6]=(((tm->tm_year-64)/10) << 4) | ((tm->tm_year-64)%10);
135 #else
136 rtcbuf[6]=(((tm->tm_year-100)/10) << 4) | ((tm->tm_year-100)%10);
137 #endif
139 rc = rtc_write_datetime(rtcbuf);
140 #else
141 rc = rtc_write_datetime((unsigned char*)tm);
142 #endif
144 if (rc < 0)
145 return -1;
146 else
147 return 0;
149 else
151 return -2;
153 #else
154 (void)tm;
155 return 0;
156 #endif
159 #if CONFIG_RTC
160 /* mktime() code taken from lynx-2.8.5 source, written
161 by Philippe De Muyter <phdm@macqel.be> */
162 time_t mktime(struct tm *t)
164 short month, year;
165 time_t result;
166 static int m_to_d[12] =
167 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
169 month = t->tm_mon;
170 year = t->tm_year + month / 12 + 1900;
171 month %= 12;
172 if (month < 0)
174 year -= 1;
175 month += 12;
177 result = (year - 1970) * 365 + (year - 1969) / 4 + m_to_d[month];
178 result = (year - 1970) * 365 + m_to_d[month];
179 if (month <= 1)
180 year -= 1;
181 result += (year - 1968) / 4;
182 result -= (year - 1900) / 100;
183 result += (year - 1600) / 400;
184 result += t->tm_mday;
185 result -= 1;
186 result *= 24;
187 result += t->tm_hour;
188 result *= 60;
189 result += t->tm_min;
190 result *= 60;
191 result += t->tm_sec;
192 return(result);
194 #endif