time.c

Go to the documentation of this file.
00001 /*****************************************************************************************
00002  Copyright (c) 2002-2004 The UbixOS Project
00003  All rights reserved.
00004 
00005  Redistribution and use in source and binary forms, with or without modification, are
00006  permitted provided that the following conditions are met:
00007 
00008  Redistributions of source code must retain the above copyright notice, this list of
00009  conditions, the following disclaimer and the list of authors.  Redistributions in binary
00010  form must reproduce the above copyright notice, this list of conditions, the following
00011  disclaimer and the list of authors in the documentation and/or other materials provided
00012  with the distribution. Neither the name of the UbixOS Project nor the names of its
00013  contributors may be used to endorse or promote products derived from this software
00014  without specific prior written permission.
00015 
00016  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
00017  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00018  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
00019  THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00020  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00021  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00022  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
00023  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00024  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025 
00026  $Id: time_8c-source.html 88 2016-01-12 00:11:29Z reddawg $
00027 
00028 *****************************************************************************************/
00029 
00030 #include <ubixos/types.h>
00031 #include <ubixos/time.h>
00032 #include <ubixos/vitals.h>
00033 #include <lib/kprintf.h>
00034 #include <assert.h>
00035 
00036 static int month[12] = {
00037         0,
00038         DAY*(31),
00039         DAY*(31+29),
00040         DAY*(31+29+31),
00041         DAY*(31+29+31+30),
00042         DAY*(31+29+31+30+31),
00043         DAY*(31+29+31+30+31+30),
00044         DAY*(31+29+31+30+31+30+31),
00045         DAY*(31+29+31+30+31+30+31+31),
00046         DAY*(31+29+31+30+31+30+31+31+30),
00047         DAY*(31+29+31+30+31+30+31+31+30+31),
00048         DAY*(31+29+31+30+31+30+31+31+30+31+30)
00049 };
00050 
00051 static int timeCmosRead(int addr) {
00052   outportByteP(0x70,addr);
00053   return((int)inportByte(0x71));
00054   }
00055 
00056 int time_init() {
00057   int i;
00058   struct timeStruct time;
00059  
00060   for (i = 0 ; i < 1000000 ; i++) {
00061     if (!(timeCmosRead(10) & 0x80)) {
00062       break;
00063       }
00064     }
00065 
00066   do {
00067     time.sec = timeCmosRead(0);
00068     time.min = timeCmosRead(2);
00069     time.hour = timeCmosRead(4);
00070     time.day = timeCmosRead(7);
00071     time.mon = timeCmosRead(8);
00072     time.year = timeCmosRead(9);
00073     } while (time.sec != timeCmosRead(0));
00074 
00075   BCD_TO_BIN(time.sec);
00076   BCD_TO_BIN(time.min);
00077   BCD_TO_BIN(time.hour);
00078   BCD_TO_BIN(time.day);
00079   BCD_TO_BIN(time.mon);
00080   BCD_TO_BIN(time.year);
00081 
00082   /* Set up our start time in seconds */
00083   systemVitals->timeStart = timeMake(&time);
00084 
00085   kprintf("%i/%i/%i %i:%i.%i\n",time.mon,time.day,time.year,time.hour,time.min,time.sec);
00086 
00087   
00088   /* Return so we know all went well */
00089   return(0x0);
00090   }
00091 
00092 uInt32 timeMake(struct timeStruct *time) {
00093   uInt32 res;
00094   int year;
00095 
00096   year = (time->year+100) - 70;
00097   /* magic offsets (y+1) needed to get leapyears right.*/
00098   res = YEAR*year + DAY*((year+1)/4);
00099   res += month[time->mon];
00100   /* and (y+2) here. If it wasn't a leap-year, we have to adjust */
00101   if (time->mon>1 && ((year+2)%4))
00102     res -= DAY;
00103   res += DAY*(time->day-1);
00104   res += HOUR*time->hour;
00105   res += MINUTE*time->min;
00106   res += time->sec;
00107   return(res);
00108   }
00109 
00110 int gettimeofday(struct timeval *tp,struct timezone *tzp) {
00111   //tp->tv_sec  = systemVitals->timeStart + systemVitals->sysUptime;
00112   tp->tv_sec    = 0x0;//systemVitals->sysUptime;
00113   tp->tv_usec = 0x0;
00114   return(0x0);
00115   }
00116 
00117 /***
00118  END
00119  ***/
00120 

Generated on Fri Dec 15 11:18:55 2006 for UbixOS V2 by  doxygen 1.4.7