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$ 00035 * Revision 1.7 2009/02/13 14:52:05 haraldkipp 00036 * Include memdebug.h for heap management debugging support. 00037 * 00038 * Revision 1.6 2008/08/11 06:59:41 haraldkipp 00039 * BSD types replaced by stdint types (feature request #1282721). 00040 * 00041 * Revision 1.5 2007/06/03 08:49:56 haraldkipp 00042 * Bug #1724015 fixed. Simplified code in time(). 00043 * 00044 * Revision 1.4 2006/10/05 17:20:13 haraldkipp 00045 * Standard C functions now support hardware RTC. 00046 * Ugly type conversion replaced. 00047 * 00048 * Revision 1.3 2003/12/19 22:26:38 drsung 00049 * Dox written. 00050 * 00051 * Revision 1.2 2003/11/26 11:14:32 haraldkipp 00052 * Portability issues 00053 * 00054 * Revision 1.1 2003/11/24 18:07:37 drsung 00055 * first release 00056 * 00057 * 00058 */ 00059 00060 #include <stdint.h> 00061 00062 #include <time.h> 00063 #include <sys/timer.h> 00064 #include <sys/atom.h> 00065 #include <stdlib.h> 00066 #include <memdebug.h> 00067 00068 #include <dev/rtc.h> 00069 00070 static uint32_t epo_offs; 00071 00077 00089 time_t time(time_t * timer) 00090 { 00091 struct _tm *tm; 00092 /* Initially use internal seconds counter. */ 00093 time_t r = epo_offs + NutGetSeconds(); 00094 00095 /* Try to get time from hardware clock. */ 00096 if ((tm = malloc(sizeof(struct _tm))) != NULL) { 00097 tm->tm_isdst = -1; 00098 if (NutRtcGetTime(tm) == 0) { 00099 /* Success. */ 00100 r = _mkgmtime(tm); 00101 } 00102 free(tm); 00103 } 00104 00105 /* Return result. */ 00106 if (timer) { 00107 *timer = r; 00108 } 00109 return r; 00110 } 00111 00119 int stime(time_t * timer) 00120 { 00121 /* Try to set hardware clock. */ 00122 NutRtcSetTime(gmtime(timer)); 00123 /* Set internal seconds counter. */ 00124 epo_offs = (uint32_t)(*timer) - NutGetSeconds(); 00125 00126 return 0; 00127 } 00128