00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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, ®, 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 };