pcf8563.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 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 
00054 #include <cfg/os.h>
00055 #include <dev/twif.h>
00056 #include <sys/event.h>
00057 #include <sys/timer.h>
00058 
00059 #include <time.h>
00060 #include <stdlib.h>
00061 #include <string.h>
00062 
00063 #include <dev/pcf8563.h>
00064 
00065 #ifndef I2C_SLA_RTC
00066 #define I2C_SLA_RTC     0x51
00067 #endif
00068 
00069 static u_long rtc_status;
00070 
00080 int PcfRtcReadRegs(u_char reg, u_char *buff, size_t cnt)
00081 {
00082     int rc = -1;
00083 
00084     if (TwMasterTransact(I2C_SLA_RTC, &reg, 1, buff, cnt, NUT_WAIT_INFINITE) == cnt) {
00085         rc = 0;
00086     }
00087     return rc;
00088 }
00089 
00103 int PcfRtcWrite(int nv, CONST u_char *buff, size_t cnt)
00104 {
00105     return TwMasterTransact(I2C_SLA_RTC, buff, cnt, 0, 0, NUT_WAIT_INFINITE);
00106 }
00107 
00116 int PcfRtcGetClock(struct _tm *tm)
00117 {
00118     int rc;
00119     u_char data[7];
00120 
00121     if ((rc = PcfRtcReadRegs(0x02, data, 7)) == 0) {
00122         tm->tm_sec = BCD2BIN(data[0] & 0x7F);
00123         tm->tm_min = BCD2BIN(data[1] & 0x7F);
00124         tm->tm_hour = BCD2BIN(data[2] & 0x3F);
00125         tm->tm_mday = BCD2BIN(data[3] & 0x3F);
00126         tm->tm_mon = BCD2BIN(data[5] & 0x1F) - 1;
00127         tm->tm_year = BCD2BIN(data[6]);
00128         if (data[5] & 0x80) {
00129             tm->tm_year += 100;
00130         }
00131         tm->tm_wday = data[4] & 0x07;
00132     }
00133     return rc;
00134 }
00135 
00146 int PcfRtcSetClock(CONST struct _tm *tm)
00147 {
00148     u_char data[8];
00149 
00150     memset(data, 0, sizeof(data));
00151     if (tm) {
00152         data[0] = 0x02;
00153         data[1] = BIN2BCD(tm->tm_sec);
00154         data[2] = BIN2BCD(tm->tm_min);
00155         data[3] = BIN2BCD(tm->tm_hour);
00156         data[4] = BIN2BCD(tm->tm_mday);
00157         data[5] = tm->tm_wday;
00158         data[6] = BIN2BCD(tm->tm_mon + 1);
00159         if (tm->tm_year > 99) {
00160             data[7] = BIN2BCD(tm->tm_year - 100);
00161             data[6] |= 0x80;
00162         }
00163         else {
00164             data[7] = BIN2BCD(tm->tm_year);
00165         }
00166     }
00167     return PcfRtcWrite(0, data, 8);
00168 }
00169 
00183 int PcfRtcGetAlarm(int idx, struct _tm *tm, int *aflgs)
00184 {
00185     return -1;
00186 }
00187 
00206 int PcfRtcSetAlarm(int idx, CONST struct _tm *tm, int aflgs)
00207 {
00208     return -1;
00209 }
00210 
00221 int PcfRtcGetStatus(u_long *sflgs)
00222 {
00223     int rc;
00224     u_char data;
00225 
00226     if ((rc = PcfRtcReadRegs(0x02, &data, 1)) == 0) {
00227         if (data & 0x80) {
00228             rtc_status = RTC_STATUS_PF;
00229         }
00230         *sflgs = rtc_status;
00231     }
00232     return rc;
00233 }
00234 
00242 int PcfRtcClearStatus(u_long sflgs)
00243 {
00244     rtc_status &= ~sflgs;
00245 
00246     return 0;
00247 }
00248 
00255 int PcfRtcInit(void)
00256 {
00257     int rc;
00258     u_long tmp;
00259 
00260     if ((rc = TwInit(0)) == 0) {
00261         rc = PcfRtcGetStatus(&tmp);
00262     }
00263     return rc;
00264 }
00265 
00266 NUTRTC rtcPcf8563 = {
00267     PcfRtcInit,         
00268     PcfRtcGetClock,     
00269     PcfRtcSetClock,     
00270     PcfRtcGetAlarm,     
00271     PcfRtcSetAlarm,     
00272     PcfRtcGetStatus,    
00273     PcfRtcClearStatus   
00274 };

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/