//--------------------------------------------------------------------------------- Ds3231::ResultCode Ds3231::getTime(RtcDatetime& outTime) { unsigned char buf[7]; memset(buf, 0, sizeof(buf)); i2c_bust_read(0, buf, 7); bool allZero = true; for(size_t i=0; i<7; i++ ) { if( buf[i] != 0 ) allZero = false; } if( allZero ) return rcFail; // day and month should be not zero if( FROM_BCD(buf[4], 3) == 0 || FROM_BCD(buf[5], 1) == 0 ) return rcFail; outTime.second = FROM_BCD(buf[0], 7); outTime.minute = FROM_BCD(buf[1], 7); outTime.hour = FROM_BCD(buf[2], 3); // #TODO: day of week outTime.day = FROM_BCD(buf[4], 3); outTime.month = FROM_BCD(buf[5], 1); outTime.year = FROM_BCD(buf[6], 15); return rcSuccess; }
void Task2(void *pdata) { unsigned int i, x, m, n, k, y; int tmp,key; int width = 10; int height = 100; if(user_task2_firstboot == 1) { //LCD 初始化 Lcd_Display(); user_task2_firstboot = 0; } while(1) { i++; if(i>99)i=0; if(rBCDYEAR == 0x99) rYear = 1999; else rYear = (2000 + rBCDYEAR); rMonth = FROM_BCD(rBCDMON & 0x1f); rDay = FROM_BCD(rBCDDAY & 0x03f); rDayOfWeek = rBCDDATE - 1; rHour = FROM_BCD(rBCDHOUR & 0x3f); rMinute = FROM_BCD(rBCDMIN & 0x7f); rSecond = FROM_BCD(rBCDSEC & 0x7f); OSTimeDly( 5 ); OSPrintf("\nEnter Task2\n"); OSPrintf("LCD display Test!\n\n"); //在LCD上打印日期,星期,时间 Lcd_printf(0,65,RGB( 0xFF,0xFF,0xFF),RGB( 0x00,0x00,0x00),0,"时间:%4d-%02d-%02d 星期%d %02d:%02d:%02d\n", rYear, rMonth, rDay,rDayOfWeek, rHour, rMinute, rSecond); Lcd_printf(84,92,RGB( 0xFF,0xFF,0xFF),RGB( 0x00,0x00,0x00),1," uC/OS2任务演示"); Lcd_printf(89,122,RGB( 0xFF,0xFF,0xFF),RGB( 0x00,0x00,0x00),0,"任务 计数中:%02d" , i); OSTimeDly(OS_TICKS_PER_SEC/5); } }
//------------------------------------------------------------------------------ // // Function: OEMGetRealTime // // Reads the current RTC value and returns a system time. // BOOL OEMGetRealTime(SYSTEMTIME *pTime) { volatile S3C6410_RTC_REG *pRTCReg; UINT16 seconds; BOOL rc = FALSE; 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; OALMSG(OAL_RTC&&OAL_FUNC, (L"+OEMGetRealTime(pTime = 0x%x)\r\n", pTime)); if (pTime == NULL) goto cleanUp; // Get uncached virtual address pRTCReg = (S3C6410_RTC_REG *)OALPAtoVA(S3C6410_BASE_REG_PA_RTC, FALSE); // Enable RTC control. pRTCReg->RTCCON |= (1<<0); do { seconds = FROM_BCD(pRTCReg->BCDSEC & 0x7f); pTime->wYear = FROM_BCD((pRTCReg->BCDYEAR & 0xff)) + RTC_YEAR_DATUM; pTime->wMonth = FROM_BCD(pRTCReg->BCDMON & 0x1f); pTime->wDay = FROM_BCD(pRTCReg->BCDDATE & 0x3f); pTime->wDayOfWeek = pRTCReg->BCDDAY - 1; pTime->wHour = FROM_BCD(pRTCReg->BCDHOUR & 0x3f); pTime->wMinute = FROM_BCD(pRTCReg->BCDMIN & 0x7f); pTime->wSecond = FROM_BCD(pRTCReg->BCDSEC & 0x7f); pTime->wMilliseconds= 0; } while (pTime->wSecond != seconds); // Disable RTC control. pRTCReg->RTCCON &= ~(1<<0); // Done rc = TRUE; cleanUp: OALMSG(OAL_RTC&&OAL_FUNC, (L"-OEMGetRealTime(rc = %d)\r\n", rc)); if (csEnter) LeaveCriticalSection(&g_oalRTCcs); return rc; }
STATUS m41t81_tod_get(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 byte; byte = (UINT8) time_readrtc(M41T81_CCR_ADDRESS, M41T81REG_HR); byte &= ~(M41T81REG_HR_CB | M41T81REG_HR_CEB); *hour = (UINT8) FROM_BCD(byte); byte = (UINT8) time_readrtc(M41T81_CCR_ADDRESS, M41T81REG_MN); *minute = (UINT8) FROM_BCD(byte); byte = (UINT8) time_readrtc(M41T81_CCR_ADDRESS, M41T81REG_SC); byte &= ~(M41T81REG_SC_ST); *second = (UINT8) FROM_BCD(byte); byte = (UINT8) time_readrtc(M41T81_CCR_ADDRESS, M41T81REG_MO); *month = (UINT8) byte; byte = (UINT8) time_readrtc(M41T81_CCR_ADDRESS, M41T81REG_DT); *day = (UINT8) FROM_BCD(byte); byte = (UINT8) time_readrtc(M41T81_CCR_ADDRESS, M41T81REG_YR); *year = (UINT8) byte; byte = (UINT8) time_readrtc(M41T81_CCR_ADDRESS, M41T81REG_HR); byte &= M41T81REG_HR_CB; if (byte) { *year += 2000; /*Year 20xx*/ } else { *year += 1900; /*Year 19xx*/ } return 0; }
int m41t81_tod_get_second(void) { int second; char byte; second = 0; if (i2cRead(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_SC, 1, &byte) == 0) { byte &= ~(M41T81REG_SC_ST); second = (UINT8) FROM_BCD(byte); } return second; }
//------------------------------------------------------------------------------ // // Function: OEMGetRealTime // // Reads the current RTC value and returns a system time. // BOOL OEMGetRealTime(SYSTEMTIME *pTime) { volatile S3C6400_RTC_REG *pRTCReg; UINT16 seconds; BOOL rc = FALSE; OALMSG(OAL_RTC&&OAL_FUNC, (L"+OEMGetRealTime(pTime = 0x%x)\r\n", pTime)); if (pTime == NULL) goto cleanUp; // Get uncached virtual address pRTCReg = (S3C6400_RTC_REG *)OALPAtoVA(S3C6400_BASE_REG_PA_RTC, FALSE); // Enable RTC control. pRTCReg->RTCCON |= (1<<0); do { seconds = FROM_BCD(pRTCReg->BCDSEC & 0x7f); pTime->wYear = FROM_BCD(pRTCReg->BCDYEAR & 0xff) + RTC_YEAR_DATUM; pTime->wMonth = FROM_BCD(pRTCReg->BCDMON & 0x1f); pTime->wDay = FROM_BCD(pRTCReg->BCDDATE & 0x3f); pTime->wDayOfWeek = pRTCReg->BCDDAY - 1; pTime->wHour = FROM_BCD(pRTCReg->BCDHOUR & 0x3f); pTime->wMinute = FROM_BCD(pRTCReg->BCDMIN & 0x7f); pTime->wSecond = FROM_BCD(pRTCReg->BCDSEC & 0x7f); pTime->wMilliseconds = 0; } while (pTime->wSecond != seconds); // Disable RTC control. pRTCReg->RTCCON &= ~(1<<0); // Done rc = TRUE; cleanUp: OALMSG(OAL_RTC&&OAL_FUNC, (L"-OEMGetRealTime(rc = %d)\r\n", rc)); return rc; }
/* @func BOOL | OEMGetRealTime | Returns the current wall-clock time from the RTC. @rdesc TRUE = Success, FALSE = Failure. @comm @xref */ static BOOL OEMGetRealTime(LPSYSTEMTIME lpst) { volatile S3C6410_RTC_REG *s6410RTC = (S3C6410_RTC_REG *)OALPAtoVA(S3C6410_BASE_REG_PA_RTC, FALSE); do { lpst->wYear = FROM_BCD(s6410RTC->BCDYEAR) + 2000 ; lpst->wMonth = FROM_BCD(s6410RTC->BCDMON & 0x1f); lpst->wDay = FROM_BCD(s6410RTC->BCDDATE & 0x3f); lpst->wDayOfWeek = (s6410RTC->BCDDAY - 1); lpst->wHour = FROM_BCD(s6410RTC->BCDHOUR & 0x3f); lpst->wMinute = FROM_BCD(s6410RTC->BCDMIN & 0x7f); lpst->wSecond = FROM_BCD(s6410RTC->BCDSEC & 0x7f); lpst->wMilliseconds = 0; } while (!(lpst->wSecond)); return(TRUE); }
void OSystem_PS2::readRtcTime(void) { struct CdClock cdClock; readRTC(&cdClock); g_lastTimeCheck = getMillis(); if (cdClock.stat) { msgPrintf(5000, "Unable to read RTC time, EC: %d\n", cdClock.stat); g_day = g_month = 1; g_year = 0; g_timeSecs = 0; } else { int gmtOfs = configGetTimezone(); if (configIsDaylightSavingEnabled()) gmtOfs += 60; int timeSecs = (FROM_BCD(cdClock.hour) * 60 + FROM_BCD(cdClock.minute)) * 60 + FROM_BCD(cdClock.second); timeSecs -= 9 * 60 * 60; // minus 9 hours, JST -> GMT conversion timeSecs += gmtOfs * 60; // GMT -> timezone the user selected g_day = FROM_BCD(cdClock.day); g_month = FROM_BCD(cdClock.month); g_year = FROM_BCD(cdClock.year); if (timeSecs < 0) { buildNewDate(-1); timeSecs += SECONDS_PER_DAY; } else if (timeSecs >= SECONDS_PER_DAY) { buildNewDate(+1); timeSecs -= SECONDS_PER_DAY; } g_timeSecs = (uint32)timeSecs; } sioprintf("Time: %d:%02d:%02d - %d.%d.%4d\n", g_timeSecs / (60 * 60), (g_timeSecs / 60) % 60, g_timeSecs % 60, g_day, g_month, g_year + 2000); }
STATUS m41t81_tod_get(int *year, /* 1980-2079 */ int *month, /* 01-12 */ int *date, /* 01-31 */ int *hour, /* 00-23 */ int *minute, /* 00-59 */ int *second) /* 00-59 */ { int status; char byte; int y2k; status = i2cRead(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_HR, 1, &byte); y2k = ((byte & M41T81REG_HR_CB) == M41T81REG_HR_CB); byte &= ~(M41T81REG_HR_CB | M41T81REG_HR_CEB); *hour = (UINT8) FROM_BCD(byte); status = i2cRead(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_MN, 1, &byte); *minute = (UINT8) FROM_BCD(byte); status = i2cRead(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_SC, 1, &byte); byte &= ~(M41T81REG_SC_ST); *second = (UINT8) FROM_BCD(byte); status = i2cRead(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_MO, 1, &byte); *month = (UINT8) byte; status = i2cRead(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_DT, 1, &byte); *date = (UINT8) FROM_BCD(byte); #if 0 status = i2cRead(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_DY, 1, &byte); *day = (UINT8) byte; #endif status = i2cRead(M41T81_SMBUS_CHAN, M41T81_CCR_ADDRESS, I2C_DEVICE_TYPE_RTC_M41T48, M41T81REG_YR, 1, &byte); *year = (UINT8) byte; if (y2k) { *year += 2000; /*Year 20xx*/ } else { *year += 1900; /*Year 19xx*/ } return 0; }