00001 /* 00002 * Copyright (C) 2001-2007 by egnite Software GmbH. All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions 00006 * are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the copyright holders nor the names of 00014 * contributors may be used to endorse or promote products derived 00015 * from this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS 00018 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00019 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00020 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE 00021 * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00022 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00023 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 00024 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 00025 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00026 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 00027 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00028 * SUCH DAMAGE. 00029 * 00030 * For additional information see http://www.ethernut.de/ 00031 * 00032 */ 00033 /* 00034 * $Log: time.c,v $ 00035 * Revision 1.5.4.1 2009/03/09 17:52:07 haraldkipp 00036 * Access the hardware clock on the first time query only. 00037 * 00038 * Revision 1.5 2007/06/03 08:49:56 haraldkipp 00039 * Bug #1724015 fixed. Simplified code in time(). 00040 * 00041 * Revision 1.4 2006/10/05 17:20:13 haraldkipp 00042 * Standard C functions now support hardware RTC. 00043 * Ugly type conversion replaced. 00044 * 00045 * Revision 1.3 2003/12/19 22:26:38 drsung 00046 * Dox written. 00047 * 00048 * Revision 1.2 2003/11/26 11:14:32 haraldkipp 00049 * Portability issues 00050 * 00051 * Revision 1.1 2003/11/24 18:07:37 drsung 00052 * first release 00053 * 00054 * 00055 */ 00056 00057 #include <time.h> 00058 #include <sys/timer.h> 00059 #include <sys/atom.h> 00060 #include <stdlib.h> 00061 00062 #include <dev/rtc.h> 00063 00064 static u_long epo_offs; 00065 00071 00083 time_t time(time_t * timer) 00084 { 00085 struct _tm *tm; 00086 time_t r; 00087 00088 if (epo_offs == 0) { 00089 /* Try to get initial time from hardware clock. */ 00090 if ((tm = malloc(sizeof(struct _tm))) != NULL) { 00091 tm->tm_isdst = -1; 00092 if (NutRtcGetTime(tm) == 0) { 00093 /* Success. */ 00094 epo_offs = _mkgmtime(tm); 00095 } 00096 free(tm); 00097 } 00098 } 00099 00100 /* Return result. */ 00101 r = epo_offs + NutGetSeconds(); 00102 if (timer) { 00103 *timer = r; 00104 } 00105 return r; 00106 } 00107 00115 int stime(time_t * timer) 00116 { 00117 /* Try to set hardware clock. */ 00118 NutRtcSetTime(gmtime(timer)); 00119 /* Set internal seconds counter. */ 00120 epo_offs = (u_long)(*timer) - NutGetSeconds(); 00121 00122 return 0; 00123 } 00124