void RTC::release() { // set power off time regs[POFMI] = TO_BCD(cur_time.minute); regs[POFH] = TO_BCD(cur_time.hour); regs[POFD] = TO_BCD(cur_time.day); // save rtc regs image FILEIO* fio = new FILEIO(); if(fio->Fopen(create_local_path(_T("RTC.BIN")), FILEIO_WRITE_BINARY)) { fio->Fwrite(regs + 8, 32, 1); fio->Fclose(); } delete fio; }
/************************************************ 函数名: RTC_Init 函数描述: Rtc初始化函数 修改历史: 2011-1-2 徐军 修改 ************************************************/ VOID RTC_Init(VOID) { INT32 wYear=0, wMonth=0,wDay=0,wDayOfWeek=0; INT32 wHour=0,wMinute=0,wSecond=0; wYear = 2015; wMonth = 7; wDay = 12; wDayOfWeek = 7; wHour= 17; wMinute = 05; wSecond = 0; rRTCCON = 1 ; /*使能RTC读写*/ rBCDYEAR = (UINT8)TO_BCD(wYear%100); /*年*/ rBCDMON = (UINT8)TO_BCD(wMonth); /*月*/ rBCDDAY = (UINT8)TO_BCD(wDay); /*日*/ rBCDDATE = wDayOfWeek+1; /*星期*/ rBCDHOUR = (UINT8)TO_BCD(wHour); /*小时*/ rBCDMIN = (UINT8)TO_BCD(wMinute); /*分*/ rBCDSEC = (UINT8)TO_BCD(wSecond); /*秒*/ rRTCCON &= ~1 ; /*关闭使能RTC读写*/ }
//------------------------------------------------------------------------------ // // Function: OEMSetRealTime // // Updates the RTC with the specified system time. // BOOL OEMSetRealTime(LPSYSTEMTIME pTime) { BOOL rc = FALSE; volatile S3C6400_RTC_REG *pRTCReg; if (pTime == NULL) goto cleanUp; OALMSG(OAL_RTC&&OAL_FUNC, ( L"+OEMSetRealTime(%d/%d/%d %d:%d:%d.%03d)\r\n", pTime->wYear, pTime->wMonth, pTime->wDay, pTime->wHour, pTime->wMinute, pTime->wSecond, pTime->wMilliseconds)); OALMSG(TRUE, ( L"OEMSetRealTime(%d/%d/%d %d:%d:%d.%03d)\r\n", pTime->wYear, pTime->wMonth, pTime->wDay, pTime->wHour, pTime->wMinute, pTime->wSecond, pTime->wMilliseconds)); // The RTC will only support a BCD year value of 0 - 99. The year datum is // 1980, so any dates greater than 2079 will fail unless the datum is // adjusted. if ((pTime->wYear < RTC_YEAR_DATUM) || ((pTime->wYear - RTC_YEAR_DATUM) > 99)) { OALMSG(OAL_ERROR, (L"ERROR: OEMSetRealTime: " L"RTC cannot support a year greater than %d or less than %d " L"(value %d)\r\n", (RTC_YEAR_DATUM + 99), RTC_YEAR_DATUM, pTime->wYear)); goto cleanUp; } // Get uncached virtual address pRTCReg = (S3C6400_RTC_REG *)OALPAtoVA(S3C6400_BASE_REG_PA_RTC, FALSE); // Enable RTC control. pRTCReg->RTCCON |= (1<<0); pRTCReg->BCDSEC = TO_BCD(pTime->wSecond); pRTCReg->BCDMIN = TO_BCD(pTime->wMinute); pRTCReg->BCDHOUR = TO_BCD(pTime->wHour); pRTCReg->BCDDATE = TO_BCD(pTime->wDay); pRTCReg->BCDDAY = pTime->wDayOfWeek + 1; pRTCReg->BCDMON = TO_BCD(pTime->wMonth); pRTCReg->BCDYEAR = TO_BCD(pTime->wYear - RTC_YEAR_DATUM); // Disable RTC control. pRTCReg->RTCCON &= ~(1<<0); // Done rc = TRUE; cleanUp: OALMSG(OAL_RTC&&OAL_FUNC, (L"-OEMSetRealTime(rc = %d)\r\n", rc)); return rc; }
//------------------------------------------------------------------------------ // // Function: OEMSetAlarmTime // // Set the RTC alarm time. // BOOL OEMSetAlarmTime(SYSTEMTIME *pTime) { BOOL rc = FALSE; volatile S3C6400_RTC_REG *pRTCReg; UINT32 irq; if (pTime == NULL) goto cleanUp; OALMSG(OAL_RTC&&OAL_FUNC, ( L"+OEMSetAlarmTime(%d/%d/%d %d:%d:%d.%03d)\r\n", pTime->wMonth, pTime->wDay, pTime->wYear, pTime->wHour, pTime->wMinute, pTime->wSecond, pTime->wMilliseconds)); OALMSG(TRUE, ( L"+OEMSetAlarmTime(%d/%d/%d %d:%d:%d.%03d)\r\n", pTime->wMonth, pTime->wDay, pTime->wYear, pTime->wHour, pTime->wMinute, pTime->wSecond, pTime->wMilliseconds)); // Get uncached virtual address pRTCReg = (S3C6400_RTC_REG *)OALPAtoVA(S3C6400_BASE_REG_PA_RTC, FALSE); // Enable RTC control pRTCReg->RTCCON |= (1<<0); pRTCReg->ALMSEC = TO_BCD(pTime->wSecond); pRTCReg->ALMMIN = TO_BCD(pTime->wMinute); pRTCReg->ALMHOUR = TO_BCD(pTime->wHour); pRTCReg->ALMDATE = TO_BCD(pTime->wDay); pRTCReg->ALMMON = TO_BCD(pTime->wMonth); pRTCReg->ALMYEAR = TO_BCD(pTime->wYear - RTC_YEAR_DATUM); // Enable the RTC Alarm pRTCReg->RTCALM = 0x7f; // Disable RTC control. pRTCReg->RTCCON &= ~(1<<0); // Clear RTC Alarm Interrupt Pending pRTCReg->INTP |= (1<<1); // Enable and Clear RTC Alarm Interrupt irq = IRQ_RTC_ALARM; OALIntrDoneIrqs(1, &irq); // Done rc = TRUE; cleanUp: OALMSG(OAL_RTC&&OAL_FUNC, (L"-OEMSetAlarmTime(rc = %d)\r\n", rc)); return rc; }
//--------------------------------------------------------------------------------- void Ds3231::setTime(RtcDatetime& time) { unsigned char buf[7]; memset(buf, 0, sizeof(buf)); buf[0] = TO_BCD( time.second ); buf[1] = TO_BCD( time.minute ); buf[2] = TO_BCD( time.hour ); buf[3] = 1; // #TODO: day of week buf[4] = TO_BCD( time.day ); buf[5] = TO_BCD( time.month ); buf[6] = TO_BCD( time.year ); i2c_bust_write(0, buf, 7); }
/* * Note: the TOD should store the current GMT */ STATUS m41t81_tod_set(int year, /* 1980-2079 */ int month, /* 01-12 */ int day, /* 01-31 */ int hour, /* 00-23 */ int minute, /* 00-59 */ int second) /* 00-59 */ { UINT8 y2k, temp; /* write time */ temp = time_readrtc(M41T81_CCR_ADDRESS, M41T81REG_HR); temp &= (M41T81REG_HR_CB | M41T81REG_HR_CEB); hour = TO_BCD(hour); hour |= temp; time_writertc(M41T81_CCR_ADDRESS, M41T81REG_HR, hour); time_writertc(M41T81_CCR_ADDRESS, M41T81REG_MN, TO_BCD(minute)); second &= ~M41T81REG_SC_ST; time_writertc(M41T81_CCR_ADDRESS, M41T81REG_SC, TO_BCD(second)); /* write date */ time_writertc(M41T81_CCR_ADDRESS, M41T81REG_MO, TO_BCD(month)); time_writertc(M41T81_CCR_ADDRESS, M41T81REG_DT, TO_BCD(day)); y2k = (year >= 2000) ? 0x20 : 0x19; year %= 100; time_writertc(M41T81_CCR_ADDRESS, M41T81REG_YR, TO_BCD(year)); /* * M41T81 does not have a century byte, so we don't need to write y2k * But we should flip the century bit (CB) to "1" for year 20xx and to "0" * for year 19xx. */ temp = (UINT8) time_readrtc(M41T81_CCR_ADDRESS, M41T81REG_HR); if (y2k == 0x19) { temp &= ~M41T81REG_HR_CB; } else if (y2k == 0x20) { temp |= M41T81REG_HR_CB; } time_writertc(M41T81_CCR_ADDRESS, M41T81REG_HR, temp); return 0; }
//------------------------------------------------------------------------------ // // Function: OEMSetAlarmTime // // Set the RTC alarm time. // BOOL OEMSetAlarmTime(SYSTEMTIME *pTime) { BOOL rc = FALSE; S3C2410X_RTC_REG *pRTCReg; UINT32 irq; OALMSG(OAL_RTC&&OAL_FUNC, ( L"+OEMSetAlarmTime(%d/%d/%d %d:%d:%d.%03d)\r\n", pTime->wMonth, pTime->wDay, pTime->wYear, pTime->wHour, pTime->wMinute, pTime->wSecond, pTime->wMilliseconds )); if (pTime == NULL) goto cleanUp; // Get uncached virtual address pRTCReg = OALPAtoVA(S3C2410X_BASE_REG_PA_RTC, FALSE); // Enable RTC control SETREG32(&pRTCReg->RTCCON, 1); OUTPORT32(&pRTCReg->ALMSEC, TO_BCD(pTime->wSecond)); OUTPORT32(&pRTCReg->ALMMIN, TO_BCD(pTime->wMinute)); OUTPORT32(&pRTCReg->ALMHOUR, TO_BCD(pTime->wHour)); OUTPORT32(&pRTCReg->ALMDATE, TO_BCD(pTime->wDay)); OUTPORT32(&pRTCReg->ALMMON, TO_BCD(pTime->wMonth)); OUTPORT32(&pRTCReg->ALMYEAR, TO_BCD(pTime->wYear - RTC_YEAR_DATUM)); // Enable the RTC alarm interrupt OUTPORT32(&pRTCReg->RTCALM, 0x7F); // Disable RTC control. CLRREG32(&pRTCReg->RTCCON, 1); // Enable/clear RTC interrupt irq = IRQ_RTC; OALIntrDoneIrqs(1, &irq); // Done rc = TRUE; cleanUp: OALMSG(OAL_RTC&&OAL_FUNC, (L"-OEMSetAlarmTime(rc = %d)\r\n", rc)); return rc; }
/************************【实时时钟】*********************************/ void Rtc_Init(void) { int wYear, wMonth,wDay,wDayOfWeek,wHour,wMinute,wSecond; wYear = 2009; wMonth = 3; wDay = 5; wDayOfWeek = 4; wHour = 12; wMinute = 00; wSecond = 00; rRTCCON = 1 ; //RTC read and write enable rBCDYEAR = (unsigned char)TO_BCD(wYear%100); //年 rBCDMON = (unsigned char)TO_BCD(wMonth); //月 rBCDDAY = (unsigned char)TO_BCD(wDay); //日 rBCDDATE = wDayOfWeek+1; //星期 rBCDHOUR = (unsigned char)TO_BCD(wHour); //小时 rBCDMIN = (unsigned char)TO_BCD(wMinute); //分 rBCDSEC = (unsigned char)TO_BCD(wSecond); //秒 rRTCCON &= ~1 ; //RTC read and write disable }
//------------------------------------------------------------------------------ // // Function: OEMSetAlarmTime // // Set the RTC alarm time. // BOOL OEMSetAlarmTime(SYSTEMTIME *pTime) { BOOL rc = FALSE; volatile S3C6410_RTC_REG *pRTCReg; UINT32 irq; BOOL csEnter; /* can get called before IoctlHalInitRTC, if so then don't try to enter (uninitialized) cs (kernel should be single-proc, single-threaded here) */ if (g_oalRTCcsInit) { EnterCriticalSection(&g_oalRTCcs); csEnter = TRUE; } else csEnter = FALSE; if (pTime == NULL) goto cleanUp; OALMSG(OAL_RTC&&OAL_FUNC, ( L"+OEMSetAlarmTime(%d/%d/%d %d:%d:%d.%03d)\r\n", pTime->wMonth, pTime->wDay, pTime->wYear, pTime->wHour, pTime->wMinute, pTime->wSecond, pTime->wMilliseconds)); OALMSG(TRUE, ( L"+OEMSetAlarmTime(%d/%d/%d %d:%d:%d.%03d)\r\n", pTime->wMonth, pTime->wDay, pTime->wYear, pTime->wHour, pTime->wMinute, pTime->wSecond, pTime->wMilliseconds)); // The RTC will only support a BCD year value of 0 - 99. The year datum is // 1980, so any dates greater than 2079 will fail unless the datum is // adjusted. if ((pTime->wYear <RTC_YEAR_DATUM) || ((pTime->wYear - RTC_YEAR_DATUM) > 99)) { OALMSG(OAL_ERROR, (L"ERROR: OEMSetRealTime: " L"RTC cannot support a year greater than %d or less than %d " L"(value %d)\r\n", (RTC_YEAR_DATUM + 99), RTC_YEAR_DATUM, pTime->wYear)); goto cleanUp; } // Get uncached virtual address pRTCReg = (S3C6410_RTC_REG *)OALPAtoVA(S3C6410_BASE_REG_PA_RTC, FALSE); // Enable RTC control pRTCReg->RTCCON |= (1<<0); pRTCReg->ALMSEC = TO_BCD(pTime->wSecond); pRTCReg->ALMMIN = TO_BCD(pTime->wMinute); pRTCReg->ALMHOUR = TO_BCD(pTime->wHour); pRTCReg->ALMDATE = TO_BCD(pTime->wDay); pRTCReg->ALMMON = TO_BCD(pTime->wMonth); pRTCReg->ALMYEAR = TO_BCD(pTime->wYear - RTC_YEAR_DATUM); // Enable the RTC Alarm pRTCReg->RTCALM = 0x7f; // Disable RTC control. pRTCReg->RTCCON &= ~(1<<0); // Clear RTC Alarm Interrupt Pending pRTCReg->INTP |= (1<<1); // Enable and Clear RTC Alarm Interrupt irq = IRQ_RTC_ALARM; OALIntrDoneIrqs(1, &irq); // Done rc = TRUE; cleanUp: OALMSG(OAL_RTC&&OAL_FUNC, (L"-OEMSetAlarmTime(rc = %d)\r\n", rc)); if (csEnter) LeaveCriticalSection(&g_oalRTCcs); return rc; }
/* * Note: the TOD should store the current GMT */ STATUS m41t81_tod_set(int year, /* 1980-2079 */ int month, /* 01-12 */ int day, /* 01-31 */ int hour, /* 00-23 */ int minute, /* 00-59 */ int second) /* 00-59 */ { UINT8 y2k; int status; char buffer; /* write time */ /* * M41T81 does not have a century byte, so we don't need to write y2k * But we should flip the century bit (CB) to "1" for year 20xx and to "0" * for year 19xx. */ y2k = (year >= 2000) ? 0x20 : 0x19; buffer = 0; if (y2k == 0x20) { buffer = M41T81REG_HR_CB; } buffer |= (char) (TO_BCD(hour) & 0xff); status = i2cWrite(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_HR, 1, &buffer); buffer = (char) (TO_BCD(minute) & 0xff); status = i2cWrite(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_MN, 1, &buffer); buffer = (char) (TO_BCD(second) & 0xff); buffer &= ~M41T81REG_SC_ST; status = i2cWrite(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_SC, 1, &buffer); /* write date */ buffer = (char) (TO_BCD(month) & 0xff); status = i2cWrite(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_MO, 1, &buffer); buffer = (char) (TO_BCD(day) & 0xff); status = i2cWrite(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_DT, 1, &buffer); year %= 100; buffer = (char) (TO_BCD(year) & 0xff); status = i2cWrite(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_YR, 1, &buffer); return 0; }