#ifndef __PFC8583_C #define __PFC8583_C #include "pfc8583.h" typedef struct RTC_REAL_TIME { BCD8 Sec100; /* hundreds of second */ BCD8 Second; BCD8 Minute; BCD8 Hour; /* hour & control */ BCD8 YearDate; /* year(only LSB-bits 0 and 1) & date*/ /* & control */ BCD8 WeekdayMonth; /* month & day of week (sunday=0) */ } RTC_REAL_TIME; typedef struct RTC_ALARM_TIME { BCD8 Sec100; /* hundreds of second */ BCD8 Second; BCD8 Minute; BCD8 Hour; /* hour */ BCD8 YearDate; /* only daymonth-field is used !!! */ BCD8 WeekdayMonth; /* only month-field is used !!! */ } RTC_ALARM_TIME; /****************************************************************************/ BYTE RTCSetTime(REAL_TIME *RealTime, BYTE DeviceID) { RTC_REAL_TIME Time; BYTE TransferByte; BYTE Result, Tmp; Tmp = BCD2INT((*RealTime).Year) % 4;/* calc type of year (0..3) */ Time.Sec100 = (*RealTime).Sec100; Time.Second = (*RealTime).Second; Time.Minute = (*RealTime).Minute; Time.Hour = (*RealTime).Hour; Time.YearDate = (*RealTime).Date | (Tmp << 6); Time.WeekdayMonth = (*RealTime).Month | ((*RealTime).Weekday << 5); TransferByte = 0x84; /* control-register setting all of, stop rtc*/ /* write control register to RTC */ if ((Result = RTCPutRAM(&TransferByte, DeviceID, 0x00, 1)) != RTC_SUCCESSFUL){ return(Result); } /* write time to RTC */ if ((Result = RTCPutRAM((BYTE*) &Time, DeviceID, 0x01, sizeof(Time))) != RTC_SUCCESSFUL){ return(Result); } TransferByte = 0x00; /* value for alarm control register */ if ((Result = RTCPutRAM(&TransferByte, DeviceID, 0x08, 1)) != RTC_SUCCESSFUL){ return(Result); } TransferByte = 0x04; /* new value for control register to */ /* start RTC with enabled alarm */ if ((Result = RTCPutRAM(&TransferByte, DeviceID, 0x00, 1)) != RTC_SUCCESSFUL){ return(Result); } /* write additional Year information */ if ((Result = RTCPutRAM((BYTE *) &((*RealTime).Year), DeviceID, 0x10, 2)) != RTC_SUCCESSFUL){ return(Result); } /* write additional Year information */ if ((Result = RTCPutRAM(&Tmp, DeviceID, 0x12, 1)) != RTC_SUCCESSFUL){ return(Result); } return(RTC_SUCCESSFUL); /* successful operation */ } /****************************************************************************/ BYTE RTCGetTime(REAL_TIME *RealTime, BYTE DeviceID) { RTC_REAL_TIME Time; BYTE TransferByte; BYTE Result; BYTE Tmp; int i; /* get control register */ if ((Result = RTCGetRAM(&TransferByte, DeviceID, 0x00, 1)) != RTC_SUCCESSFUL){ return(Result); } /* rtc was not initialized ! */ if(!(TransferByte & 0x04)){ return(RTC_NOT_INITIALIZED); } if ((Result = RTCGetRAM((BYTE*) &Time, DeviceID, 0x01, sizeof(Time))) !=RTC_SUCCESSFUL){ return(Result); } /* read year from RTC */ if ((Result = RTCGetRAM((BYTE*) &((*RealTime).Year), DeviceID, 0x10, 2)) != RTC_SUCCESSFUL){ return(Result); } /* read additional year information */ if ((Result = RTCGetRAM((BYTE*) &Tmp, DeviceID, 0x12, 1)) != RTC_SUCCESSFUL){ return(Result); } if ((i = (((int) (Time.YearDate >> 6)) - ((int) Tmp)))) { /* other year? */ Tmp = Time.YearDate >> 6; if (i < 0){ i += 4; } (*RealTime).Year = INT2BCD(BCD2INT((*RealTime).Year) + i); /* write new year to RTC */ if ((Result = RTCPutRAM((BYTE*) &((*RealTime).Year), DeviceID, 0x10, 2)) != RTC_SUCCESSFUL){ return(Result); } /* write additional year information */ if ((Result = RTCPutRAM((BYTE*) &Tmp, DeviceID, 0x12, 1)) != RTC_SUCCESSFUL){ return(Result); } ////////////////////////////////////////// } (*RealTime).Weekday = ((Time.WeekdayMonth & 0xE0) >> 5) & 0x07; (*RealTime).Month = Time.WeekdayMonth & 0x1F; (*RealTime).Date = Time.YearDate & 0x3F; (*RealTime).Hour = Time.Hour; (*RealTime).Minute = Time.Minute; (*RealTime).Second = Time.Second; (*RealTime).Sec100 = Time.Sec100; return(RTC_SUCCESSFUL); } /****************************************************************************/ BYTE RTCSetAlarm(REAL_TIME *AlarmTime, BYTE DeviceID) { RTC_ALARM_TIME Time; BYTE TransferByte; BYTE Result; if ((Result = RTCGetRAM(&TransferByte, DeviceID, 0x00, 1)) != RTC_SUCCESSFUL) return(Result); if(!(TransferByte & 0x04)) return(RTC_NOT_INITIALIZED); /* rtc was not initialized ! */ Time.Sec100 = 0x00; /* setting sec100 is not useful */ Time.Second = (*AlarmTime).Second; Time.Minute = (*AlarmTime).Minute; Time.Hour = (*AlarmTime).Hour; Time.YearDate = (*AlarmTime).Date; /* Year is ignored by RTC */ Time.WeekdayMonth = (*AlarmTime).Month; /* Weekday is ignored by RTC */ if ((Result = RTCPutRAM((BYTE*)&Time, DeviceID, 0x09, sizeof(Time))) != RTC_SUCCESSFUL) /* write alarm time into RTC */ return(Result); TransferByte = 0xB0; /* set alarm control */ if ((Result = RTCPutRAM(&TransferByte, DeviceID, 0x08, 1)) != RTC_SUCCESSFUL) /* write alarm control to RTC */ return(Result); TransferByte = 0x04; /* set control register */ if ((Result = RTCPutRAM(&TransferByte, DeviceID, 0x00, 1)) != RTC_SUCCESSFUL) /* RTC starts detecting alarm intrs */ return(Result); return(RTC_SUCCESSFUL); } /****************************************************************************/ BYTE RTCGetAlarm(REAL_TIME *AlarmTime, BYTE DeviceID) { RTC_ALARM_TIME Time; BYTE TransferByte; BYTE Result; BYTE Tmp; int i; BCD8 YearDate; if ((Result = RTCGetRAM(&TransferByte, DeviceID, 0x00, 1)) != RTC_SUCCESSFUL) /* get control register */ return(Result); if(!(TransferByte & 0x04)) return(RTC_NOT_INITIALIZED); /* rtc was not initialized ! */ if ((RTCGetRAM((BYTE*) &Time, DeviceID, 0x09, sizeof(Time))) != RTC_SUCCESSFUL) /* read alarm time from RTC */ return(Result); if ((RTCGetRAM((BYTE*) &((*AlarmTime).Year), DeviceID, 0x10, 2)) != RTC_SUCCESSFUL) /* read year from RTC */ return(Result); if ((RTCGetRAM((BYTE*) &Tmp, DeviceID, 0x12, 1)) != RTC_SUCCESSFUL) return(Result); /* read additional year information */ if ((RTCGetRAM((BYTE*) &YearDate, DeviceID, 0x05, 1)) != RTC_SUCCESSFUL) return(Result); /* read YearDate field of real time */ if ((i = (((int) (YearDate >> 6)) - ((int) Tmp)))) { /* other year? */ Tmp = YearDate >> 6; if (i < 0) i += 4; (*AlarmTime).Year = INT2BCD(BCD2INT((*AlarmTime).Year) + i); if ((RTCPutRAM((BYTE*) &((*AlarmTime).Year), DeviceID, 0x10, 2)) != RTC_SUCCESSFUL) /* write new year to RTC */ return(Result); if ((RTCPutRAM((BYTE*) &Tmp, DeviceID, 0x12, 1)) != RTC_SUCCESSFUL) return(Result); /* write additional year information */ } (*AlarmTime).Year = 0x0000; /* Year is ignored by RTC */ (*AlarmTime).Weekday = 0x00; /* Weekday is ignored by RTC */ (*AlarmTime).Month = Time.WeekdayMonth & 0x1F; (*AlarmTime).Date = Time.YearDate & 0x3F; (*AlarmTime).Hour = Time.Hour; (*AlarmTime).Minute = Time.Minute; (*AlarmTime).Second = Time.Second; return(RTC_SUCCESSFUL); } /****************************************************************************/ BYTE RTCGetAlarmStatus(BYTE *AlarmStatus, BYTE DeviceID) { BYTE TransferByte; /* state of control-register in RTC */ BYTE Result; if ((Result = RTCGetRAM(&TransferByte, DeviceID, 0x00, 1)) != RTC_SUCCESSFUL) /* read control register from RTC */ return(Result); if(!(TransferByte & 0x04)) return(RTC_NOT_INITIALIZED); /* rtc was not initialized ! */ if(!(TransferByte & 0x02)) /* return state of alarm flag */ *AlarmStatus = 0x00; else { *AlarmStatus = 0x01; TransferByte = 0x00; if ((Result = RTCPutRAM(&TransferByte, DeviceID, 0x08, 1)) != RTC_SUCCESSFUL) /* stop further alarms */ return(Result); TransferByte = 0x04; if ((Result = RTCPutRAM(&TransferByte, DeviceID, 0x00, 1)) != RTC_SUCCESSFUL) /* reset alarm-flag */ return(Result); } return(RTC_SUCCESSFUL); } /****************************************************************************/ BYTE RTCPutRAM(BYTE *SourcePtr, BYTE DeviceID, BYTE DestAddr, WORD Size) { i2csig_init(); if ( i2csig_send(DeviceID, DestAddr, Size, SourcePtr) != I2C_SUCCESSFUL){ return(RTC_WRITE_ERROR); } return(RTC_SUCCESSFUL); } /****************************************************************************/ BYTE RTCGetRAM(BYTE *DestPtr, BYTE DeviceID, BYTE SourceAddr, WORD Size) { i2csig_init(); if ( i2csig_receive(DeviceID, SourceAddr, Size, DestPtr) != I2C_SUCCESSFUL){ return(RTC_READ_ERROR); } return(RTC_SUCCESSFUL); } /****************************************************************************/ /****************************************************************************/ BYTE RTCPutCfgRAM(BYTE *SourcePtr, BYTE DeviceID, BYTE DestAddr, WORD Size) { i2csig_init(); if ( i2csig_send(DeviceID, DestAddr + 0x14, Size, SourcePtr) != I2C_SUCCESSFUL){ return(RTC_WRITE_ERROR); } return(RTC_SUCCESSFUL); } /****************************************************************************/ BYTE RTCGetCfgRAM(BYTE *DestPtr, BYTE DeviceID, BYTE SourceAddr, WORD Size) { i2csig_init(); if ( i2csig_receive(DeviceID, SourceAddr + 0x14, Size, DestPtr) != I2C_SUCCESSFUL){ return(RTC_READ_ERROR); } return(RTC_SUCCESSFUL); } /****************************************************************************/ #endif