Go to the documentation of this file.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
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, ®, 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 };