Nut/OS  4.10.3
API Reference
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 
00057 #include <cfg/os.h>
00058 #include <dev/twif.h>
00059 #include <sys/event.h>
00060 #include <sys/timer.h>
00061 
00062 #include <time.h>
00063 #include <stdlib.h>
00064 #include <string.h>
00065 
00066 #include <dev/pcf8563.h>
00067 
00068 #ifndef I2C_SLA_RTC
00069 #define I2C_SLA_RTC     0x51
00070 #endif
00071 
00072 static uint32_t rtc_status;
00073 
00083 int PcfRtcReadRegs(uint8_t reg, uint8_t *buff, size_t cnt)
00084 {
00085     int rc = -1;
00086 
00087     if (TwMasterTransact(I2C_SLA_RTC, &reg, 1, buff, cnt, NUT_WAIT_INFINITE) == cnt) {
00088         rc = 0;
00089     }
00090     return rc;
00091 }
00092 
00106 int PcfRtcWrite(int nv, CONST uint8_t *buff, size_t cnt)
00107 {
00108     return TwMasterTransact(I2C_SLA_RTC, buff, cnt, 0, 0, NUT_WAIT_INFINITE);
00109 }
00110 
00119 int PcfRtcGetClock(struct _tm *tm)
00120 {
00121     int rc;
00122     uint8_t data[7];
00123 
00124     if ((rc = PcfRtcReadRegs(0x02, data, 7)) == 0) {
00125         tm->tm_sec = BCD2BIN(data[0] & 0x7F);
00126         tm->tm_min = BCD2BIN(data[1] & 0x7F);
00127         tm->tm_hour = BCD2BIN(data[2] & 0x3F);
00128         tm->tm_mday = BCD2BIN(data[3] & 0x3F);
00129         tm->tm_mon = BCD2BIN(data[5] & 0x1F) - 1;
00130         tm->tm_year = BCD2BIN(data[6]);
00131         if (data[5] & 0x80) {
00132             tm->tm_year += 100;
00133         }
00134         tm->tm_wday = data[4] & 0x07;
00135     }
00136     return rc;
00137 }
00138 
00149 int PcfRtcSetClock(CONST struct _tm *tm)
00150 {
00151     uint8_t data[8];
00152 
00153     memset(data, 0, sizeof(data));
00154     if (tm) {
00155         data[0] = 0x02;
00156         data[1] = BIN2BCD(tm->tm_sec);
00157         data[2] = BIN2BCD(tm->tm_min);
00158         data[3] = BIN2BCD(tm->tm_hour);
00159         data[4] = BIN2BCD(tm->tm_mday);
00160         data[5] = tm->tm_wday;
00161         data[6] = BIN2BCD(tm->tm_mon + 1);
00162         if (tm->tm_year > 99) {
00163             data[7] = BIN2BCD(tm->tm_year - 100);
00164             data[6] |= 0x80;
00165         }
00166         else {
00167             data[7] = BIN2BCD(tm->tm_year);
00168         }
00169     }
00170     return PcfRtcWrite(0, data, 8);
00171 }
00172 
00186 int PcfRtcGetAlarm(int idx, struct _tm *tm, int *aflgs)
00187 {
00188     return -1;
00189 }
00190 
00209 int PcfRtcSetAlarm(int idx, CONST struct _tm *tm, int aflgs)
00210 {
00211     return -1;
00212 }
00213 
00224 int PcfRtcGetStatus(uint32_t *sflgs)
00225 {
00226     int rc;
00227     uint8_t data;
00228 
00229     if ((rc = PcfRtcReadRegs(0x02, &data, 1)) == 0) {
00230         if (data & 0x80) {
00231             rtc_status = RTC_STATUS_PF;
00232         }
00233         *sflgs = rtc_status;
00234     }
00235     return rc;
00236 }
00237 
00245 int PcfRtcClearStatus(uint32_t sflgs)
00246 {
00247     rtc_status &= ~sflgs;
00248 
00249     return 0;
00250 }
00251 
00258 int PcfRtcInit(void)
00259 {
00260     int rc;
00261     uint32_t tmp;
00262 
00263     if ((rc = TwInit(0)) == 0) {
00264         rc = PcfRtcGetStatus(&tmp);
00265     }
00266     return rc;
00267 }
00268 
00269 NUTRTC rtcPcf8563 = {
00270     PcfRtcInit,         
00271     PcfRtcGetClock,     
00272     PcfRtcSetClock,     
00273     PcfRtcGetAlarm,     
00274     PcfRtcSetAlarm,     
00275     PcfRtcGetStatus,    
00276     PcfRtcClearStatus   
00277 };