UbixOS V2 2.0

time.c
Go to the documentation of this file.
1 /*-
2 * Copyright (c) 2002-2018 The UbixOS Project.
3 * All rights reserved.
4 *
5 * This was developed by Christopher W. Olsen for the UbixOS Project.
6 *
7 * Redistribution and use in source and binary forms, with or without modification, are permitted
8 * provided that the following conditions are met:
9 *
10 * 1) Redistributions of source code must retain the above copyright notice, this list of
11 * conditions, the following disclaimer and the list of authors.
12 * 2) Redistributions in binary form must reproduce the above copyright notice, this list of
13 * conditions, the following disclaimer and the list of authors in the documentation and/or
14 * other materials provided with the distribution.
15 * 3) Neither the name of the UbixOS Project nor the names of its contributors may be used to
16 * endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <ubixos/time.h>
30 #include <ubixos/vitals.h>
31 #include <lib/kprintf.h>
32 #include <assert.h>
33
34 static int month[12] = { 0,
35 DAY * (31),
36 DAY * (31 + 29),
37 DAY * (31 + 29 + 31),
38 DAY * (31 + 29 + 31 + 30),
39 DAY * (31 + 29 + 31 + 30 + 31),
40 DAY * (31 + 29 + 31 + 30 + 31 + 30),
41 DAY * (31 + 29 + 31 + 30 + 31 + 30 + 31),
42 DAY * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31),
43 DAY * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30),
44 DAY * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31),
45 DAY * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30) };
46
47 static int timeCmosRead(int addr) {
48 outportByteP(0x70, addr);
49 return ((int) inportByte(0x71));
50 }
51
52 int time_init() {
53 int i;
54 struct timeStruct time;
55
56 for (i = 0; i < 1000000; i++) {
57 if (!(timeCmosRead(10) & 0x80)) {
58 break;
59 }
60 }
61
62 do {
63 time.sec = timeCmosRead(0);
64 time.min = timeCmosRead(2);
65 time.hour = timeCmosRead(4);
66 time.day = timeCmosRead(7);
67 time.mon = timeCmosRead(8);
68 time.year = timeCmosRead(9);
69 } while (time.sec != timeCmosRead(0));
70
71 BCD_TO_BIN(time.sec);
72 BCD_TO_BIN(time.min);
73 BCD_TO_BIN(time.hour);
74 BCD_TO_BIN(time.day);
75 BCD_TO_BIN(time.mon);
76 BCD_TO_BIN(time.year);
77
78 /* Set up our start time in seconds */
79 systemVitals->timeStart = timeMake(&time);
80
81 kprintf("%i/%i/%i %i:%i.%i\n", time.mon, time.day, time.year, time.hour, time.min, time.sec);
82
83 /* Return so we know all went well */
84 return (0x0);
85 }
86
87 uint32_t timeMake(struct timeStruct *time) {
88 uint32_t res;
89 int year;
90
91 year = (time->year + 100) - 70;
92
93 /* magic offsets (y+1) needed to get leapyears right.*/
94 res = YEAR * year + DAY * ((year + 1) / 4);
95
96 res += month[time->mon];
97
98 /* and (y+2) here. If it wasn't a leap-year, we have to adjust */
99 if (time->mon > 1 && ((year + 2) % 4))
100 res -= DAY;
101
102 res += DAY * (time->day - 1);
103 res += HOUR * time->hour;
104 res += MINUTE * time->min;
105 res += time->sec;
106
107 return (res);
108 }
109
110 int gettimeofday(struct timeval *tp, struct timezone *tzp) {
111 tp->tv_sec = systemVitals->timeStart + systemVitals->sysUptime;
112 tp->tv_usec = 0x0;
113
114 tzp->tz_minuteswest = (-5 * 60);
115 tzp->tz_dsttime = 0x0;
116
117 return (0x0);
118 }
timeMake
uint32_t timeMake(struct timeStruct *time)
Definition: time.c:87
gettimeofday
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: time.c:110
timezone
Definition: time.h:55
outportByteP
void outportByteP(unsigned int port, unsigned char value)
outputut one byte to specified port with a delay
Definition: io.c:87
MINUTE
#define MINUTE
Definition: time.h:40
timeval::tv_usec
suseconds_t tv_usec
Definition: _timeval.h:19
timeStruct::year
int year
Definition: time.h:52
timeStruct::min
int min
Definition: time.h:48
assert.h
DAY
#define DAY
Definition: time.h:42
inportByte
unsigned char inportByte(unsigned int)
input one byte from specified port
Definition: io.c:38
vitalsStruct::timeStart
uint32_t timeStart
Definition: vitals.h:46
systemVitals
vitalsNode * systemVitals
Definition: vitals.c:35
kprintf.h
HOUR
#define HOUR
Definition: time.h:41
timeStruct::sec
int sec
Definition: time.h:47
time.h
vitals.h
timeStruct::hour
int hour
Definition: time.h:49
uint32_t
__uint32_t uint32_t
Definition: types.h:46
timeval::tv_sec
time_t tv_sec
Definition: _timeval.h:18
YEAR
#define YEAR
Definition: time.h:43
timezone::tz_dsttime
int tz_dsttime
Definition: time.h:57
time_init
int time_init()
Definition: time.c:52
timeStruct
Definition: time.h:46
timeStruct::day
int day
Definition: time.h:50
timeval
Definition: _timeval.h:17
BCD_TO_BIN
#define BCD_TO_BIN(val)
Definition: time.h:38
timeStruct::mon
int mon
Definition: time.h:51
vitalsStruct::sysUptime
uint32_t sysUptime
Definition: vitals.h:38
kprintf
int kprintf(const char *,...)
Definition: kprintf.c:259
timezone::tz_minuteswest
int tz_minuteswest
Definition: time.h:56