⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timex20t.c

📁 此压缩包为杰得开发得z228的BSP的源代码,可以实现很多功能,尤其是视频解码有很好的效果.
💻 C
📖 第 1 页 / 共 2 页
字号:
		/*
		if(IO_R)
			*data |= (0x01<<i);*/
	}	
	RST_0;
	return;	
}
//yz_end

u16 tempsec = 60;
#define FROM_BCD(n)		((((n) >> 4) * 10) + ((n) & 0xf))
BOOL OEMGetRealTime(LPSYSTEMTIME lpst)
{
    unsigned __int64 realTime;
    FILETIME ft;
    BOOL bRet;
    //u16 ucCurtime[7];
    static unsigned char RtcFirstRead = 1;
    //int i, j;
    static DWORD dwCurRTCDR = 0;
    pvstRTCRegs pRTC;
    DWORD dwRTCDR;
    
    pRTC = (pvstRTCRegs) VA_RTC_BASE;
    //dwRTCDR = pRTC->DR / TIMERSCALE;
    dwRTCDR = (DWORD)(pRTC->DR * 1.09225);
//yz_add for first beginning
#if 1
  if(RtcFirstRead == 1)
  {
    u8 i, j, data, ucCurtime[7];
    u8 ucAddr = 0x81;
	
    for(j = 0; j < 3; j++)
    {
    	ucAddr = 0x81;
    	apSleep(1);
	for (i=0;i<7;i++)
    	{
            R1381(ucAddr, &data);/*sec min hour date month day year */
            ucCurtime[i] = data;
            ucAddr += 2;	
        }
        if((ucCurtime[4] == 0)||(ucCurtime[3] == 0)||(ucCurtime[5] == 0 ))
	    continue;
	else
	    break;            
    }
    //if(j == 3)	
    	RETAILMSG(1,(TEXT("RTC:: Read %d times Error!\r\n"), j));

    if((ucCurtime[4] == 0)&&(ucCurtime[3] == 0)&&(ucCurtime[5] == 0 ))
    {
    	ucCurtime[6] = 0x06; ucCurtime[4] = 0x11; ucCurtime[3] = 0x01; ucCurtime[5] = 0x04; ucCurtime[0] = 0; ucCurtime[1] = 0; ucCurtime[2] = 0;
    	W1381(0x80, 0x0);  //sec
    	W1381(0x82, 0x0);  //min
    	W1381(0x84, 0x0);  //hour
    	W1381(0x86, 0x01);  //date
    	W1381(0x88, 0x11);  //month
    	W1381(0x8a, 0x04);  //day
    	W1381(0x8c, 0x06);  //year
    	RETAILMSG(1,(TEXT("OEMGetRealTime()::first get, default date: 2006-11-1\r\n")));
    }	
			
    lpst->wMilliseconds = 0;
    lpst->wSecond = FROM_BCD(ucCurtime[0]&0x7f);
    lpst->wMinute = FROM_BCD(ucCurtime[1]&0x7f);
    lpst->wHour = FROM_BCD(ucCurtime[2]&0x3f);
    lpst->wDay = FROM_BCD(ucCurtime[3]&0x3f);
    lpst->wMonth = FROM_BCD(ucCurtime[4]&0x1f);
    lpst->wDayOfWeek = FROM_BCD(ucCurtime[5]&0x0f) - 1;    
    lpst->wYear = FROM_BCD(ucCurtime[6]) + 2000;	
	
    RETAILMSG(1,(TEXT("GetRealTime:: Year: %x, Month: %x, Day: %x, DW: %x, Hour: %x, Minute: %x, second: %x \r\n"), lpst->wYear, lpst->wMonth,lpst->wDay, lpst->wDayOfWeek, lpst->wHour, lpst->wMinute,lpst->wSecond));
    RETAILMSG(1,(TEXT("Get(register):: Year: %x, Month: %x, Day: %x, DW: %x, Hour: %x, Minute: %x, second: %x \r\n"), ucCurtime[6],ucCurtime[4],ucCurtime[3],ucCurtime[5],ucCurtime[2],ucCurtime[1],ucCurtime[0]));

    if (bRet = KSystemTimeToFileTime(lpst, &ft))
    {
        /* Read the clock registers */
        realTime = (unsigned __int64) dwRTCDR;      /* data in seconds  */
        realTime *= 1000;                           /* convert to ms    */
        realTime *= 10000;                          /* convert to 100ns */
        
        /* Get the desired "real" time */
        RealTimeBias = (unsigned __int64) ft.dwHighDateTime << 32;
        RealTimeBias += ft.dwLowDateTime;
        
        /* Compensate for the clock time */
        RealTimeBias -= realTime;		
	RETAILMSG(1, (TEXT("GetRealTime()::RTC: %d, RealTimeBias: %d-%d, High: %d - Low: %d\r\n"),dwRTCDR, (unsigned long)(RealTimeBias>>32), (unsigned long)RealTimeBias, ft.dwHighDateTime, ft.dwLowDateTime));
    }      	
    RtcFirstRead = 0;	
  }	
//end    

    //yz_add
    if((dwRTCDR - dwCurRTCDR) > 60)
    {
    	RTCBias = (unsigned __int64)(dwRTCDR - dwCurRTCDR);
    	dwCurRTCDR = dwRTCDR;
    	RETAILMSG(1, (TEXT("GetRealTime():: dwRTCDR: %d, dwCurRTCDR: %d\r\n"),dwRTCDR, dwCurRTCDR));
    }
    else
    	dwCurRTCDR = dwRTCDR;	
    //end	
#endif	    
    /* Read the clock registers */
    realTime = (unsigned __int64) dwRTCDR;      /* data in seconds          */
    //yz_add
    realTime -= RTCBias;    
    realTime *= 1000;                           /* convert to ms            */
    realTime *= 10000;                          /* convert to 100ns         */
    realTime += RealTimeBias;                   /* convert to "real" time   */
    //yz_add
    //realTime -= RTCBias;

    /* Load time/data structure */
    ft.dwLowDateTime = (DWORD)realTime;
    ft.dwHighDateTime = (DWORD)(realTime >> 32);	
	
	  //RETAILMSG(1, (TEXT("RTC2: %u, RTCBias: %u, RealTimeBias: %u-%u, High: %u, Low: %u\r\n"),dwRTCDR, (DWORD)RTCBias, (unsigned long)(RealTimeBias>>32), (unsigned long)RealTimeBias, ft.dwHighDateTime, ft.dwLowDateTime));
    bRet = KFileTimeToSystemTime( &ft, lpst );
    //add for test
    //RETAILMSG(1,(TEXT("Year2: %u, Month: %u, Day: %u, DW: %u, Hour: %u, Minute: %u, second: %u \r\n"), lpst->wYear, lpst->wMonth,lpst->wDay, lpst->wDayOfWeek, lpst->wHour, lpst->wMinute,lpst->wSecond));
    /*
    RETAILMSG(1, (TEXT("Ms: %d, Second: %d, Minute: %d, Hour: %d, Day: %d, Month: %d, DW: %d, Year: %d\r\n"),
    lpst->wMilliseconds,
    lpst->wSecond,
    lpst->wMinute,
    lpst->wHour,
    lpst->wDay,
    lpst->wMonth,
    lpst->wDayOfWeek,
    lpst->wYear));*/
    //end
    
    return bRet;
    
    //return KFileTimeToSystemTime( &ft, lpst );
}

#define TO_BCD(n)		((((n) / 10) << 4) | ((n) % 10))
BOOL OEMSetRealTime(LPSYSTEMTIME lpst)
{
    unsigned __int64 realTime;
    FILETIME ft;
    BOOL bRet;
    u16 ucCurtime[7];
    u8 ucCurtime1[7];
    u8 ucAddr = 0x80;
    int i;
    pvstRTCRegs pRTC = (pvstRTCRegs) VA_RTC_BASE;
    //DWORD dwRTCDR = pRTC->DR / TIMERSCALE;
    DWORD dwRTCDR;
    dwRTCDR = (DWORD)(pRTC->DR / 12.0 * 13.107);
    
    if (bRet = KSystemTimeToFileTime(lpst, &ft))
    {
        /* Read the clock registers */
        realTime = (unsigned __int64) dwRTCDR;      /* data in seconds  */
        //yz_add
        realTime -= RTCBias;    
        realTime *= 1000;                           /* convert to ms    */
        realTime *= 10000;                          /* convert to 100ns */
        
        /* Get the desired "real" time */
        RealTimeBias = (unsigned __int64) ft.dwHighDateTime << 32;
        RealTimeBias += ft.dwLowDateTime;
        
        /* Compensate for the clock time */
        RealTimeBias -= realTime;
		
	RETAILMSG(1, (TEXT("SetTime()::RTC: %u, RealTimeBias: %u - %u, High: %u - Low: %u\r\n"),dwRTCDR, (unsigned long)RealTimeBias, (unsigned long)(RealTimeBias>>32),ft.dwHighDateTime, ft.dwLowDateTime));     			
	 RETAILMSG(1,(TEXT("GetRealTime:: Year: %u, Month: %u, Day: %u, DW: %u, Hour: %u, Minute: %u, second: %u \r\n"), lpst->wYear, lpst->wMonth,lpst->wDay, lpst->wDayOfWeek, lpst->wHour, lpst->wMinute,lpst->wSecond));
    }
#if 1
    //yz_add for set outside RTC chip
    ucCurtime[0] = lpst->wSecond;
    ucCurtime[1] = lpst->wMinute;
    ucCurtime[2] = lpst->wHour;
    ucCurtime[3] = lpst->wDay;
    ucCurtime[4] = lpst->wMonth;
    ucCurtime[5] = lpst->wDayOfWeek + 1;	
    ucCurtime[6] = lpst->wYear;  
    
    ucCurtime[6] = ucCurtime[6] % 100;
    
    for(i = 0; i < 7; i++)		
    	ucCurtime1[i] = (u8)TO_BCD(ucCurtime[i]);
	
    for (i=0;i<7;i++)
    {
       W1381(ucAddr, ucCurtime1[i]);/*sec min hour date month day year */       
       ucAddr += 2;	
    }		  
    //end
#endif    
    return bRet;
}

BOOL OEMSetAlarmTime(LPSYSTEMTIME lpst)
{
    FILETIME ft;
    BOOL bRet = FALSE;
    ULARGE_INTEGER alarmTime, currentTime, deltaTime;
    pvstRTCRegs pRTC = (pvstRTCRegs) VA_RTC_BASE;
    DWORD dwRTCDR = pRTC->DR / TIMERSCALE;
    
    /* Get the desired alarm time */
    if (bRet = KSystemTimeToFileTime(lpst, &ft)) 
    {
        alarmTime.LowPart = ft.dwLowDateTime;
        alarmTime.HighPart = ft.dwHighDateTime;
        alarmTime.QuadPart -= RealTimeBias;
        
        /* Get the current time */
        currentTime.QuadPart = (unsigned __int64) dwRTCDR;  /* data in seconds  */
        currentTime.QuadPart *= 1000;                       /* convert to ms    */
        currentTime.QuadPart *= 10000;                      /* convert to 100ns */
        
        /* Make sure the alarm occurs in the future */
        if(alarmTime.QuadPart < currentTime.QuadPart) 
        {
            DEBUGMSG(FALSE, (_T("OEMSetAlarmTime: alarm 0x%08x:%08x occurs before 0x%08x:%08x\r\n"),
                alarmTime.HighPart, alarmTime.LowPart, currentTime.HighPart, 
                currentTime.LowPart));
        }
        else 
        {
            /* round up to the nearest number of seconds */
            deltaTime.QuadPart = alarmTime.QuadPart - currentTime.QuadPart;
            deltaTime.QuadPart += 9999999;              /* msecs, usecs, 100ns  */
            deltaTime.QuadPart /= 10000000;             /* convert to seconds   */
            
            /* do we have enough resolution in our timer to handle the request? */
            if(deltaTime.HighPart != 0) 
            {
                DEBUGMSG(FALSE, (_T("OEMSetAlarmTime: alarm 0x%08x:%08x delta with 0x%08x:%08x (0x%08x:%08x) is too large\r\n"),
                    alarmTime.HighPart, alarmTime.LowPart, 
                    currentTime.HighPart, currentTime.LowPart, 
                    deltaTime.HighPart, deltaTime.LowPart));
            } 
            else 
            {
                /* clear interrupts, write the comparator, and enable interrupts */
                pRTC->ICR = 0;                          /* Clears pending interrupts */
                pRTC->MR = dwRTCDR + deltaTime.LowPart; /* Set match reg value */
                pRTC->IMSC = 0x1;                       /* Enable RTC interrupt */
                pHALir_EnableIrq(LOGINTR_RTC);          /* Enable VIC Interrupt */
                bRet = TRUE;
            }
        }
    }

    /* return TRUE if alarm set, FALSE otherwise */
    return bRet;
}


/* EOF timex20t.c */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -