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

📄 time920.c

📁 三星2410的BSP开发包
💻 C
字号:
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Copyright (c) 2001. Samsung Electronics, co. ltd  All rights reserved.

Module Name:  

Abstract:

    Platform dependent PCMCIA initialization functions

rev:
	2002.4.11	: RTC function work (Hyojoon KIM, zzartto@samsung.com)
	2002.4.3	: first S3C2410 version (SOC)
	
	2002.2.5	: timer related bug fixups (kwangyoon LEE, kwangyoon@samsung.com)
				  prevent timer round-up
					- CPUSetSysTimerCount()
					- CPUClearSysTimerIRQ()
					- CPUGetSysTimerCountElapsed()
	2002.1.29	: bug fixups (kwangyoon LEE, kwangyoon@samsung.com)
					- PerfCountFreq()
					- PerfCountSinceTick()
					- CPUSetSysTimerCount()
					- CPUClearSysTimerIRQ()
					- CPUGetSysTimerCountElapsed()
					- CPUGetSysTimerCountMax()
	2002.1.28	: CE.NET initial port (kwangyoon LEE, kwangyoon@samsung.com)

Notes: 
--*/

#include <windows.h>
#include <nkintr.h>

#include <S2410.h>

extern DWORD CurMSec;
extern DWORD DiffMSec;
DWORD dwReschedIncrement;
DWORD OEMCount1ms;
static volatile DWORD dwCurReschedIncr;

unsigned __int64 RealTimeBias = 0;  // Number of 100-nanosecond intervals since
                                    // January 1, 1601. 
DWORD AlarmTime = 0;    			// Alarm Off at startup

volatile BOOL fInterruptFlag;

#define OEMDEBUG   0

volatile int RtcInitFlag = 0;
void SetRtcForce(void);

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void 
HalTimerInit(void) 
{
}

//------------------------------------------------------------------------------
//
// When this is called we will set up GPIO<1> to be a falling edge interrupt  
// InitClock sets up the OS timer to int via match reg 0 on the IRQ level
// an int is requested in 1ms from now.
//
// Interrupts are disable when this is called. check with Thomas.
//
//------------------------------------------------------------------------------
void 
InitClock(void) 
{
    volatile PWMreg *s2410PWM =(PWMreg *)PWM_BASE;
    volatile INTreg *s2410INT = (INTreg *)INT_BASE;  
    DWORD ttmp;  


	// Timer4 as OS tick and disable it first.
    s2410INT->rINTMSK |= BIT_TIMER4;		// Mask timer4 interrupt.
    s2410INT->rSRCPND = BIT_TIMER4;			// Clear pending bit
    s2410INT->rINTPND = BIT_TIMER4;

    // Operating clock : PCLK=101500000 (101.5 Mhz)    
	// IMPORTANT : MUST CHECK S2410.H DEFINITIONS !!!!
	s2410PWM->rTCFG0 |= (PRESCALER << 8);	// prescaler value=15
	s2410PWM->rTCFG1 |= (D1_16 << 16); 		// MUX4 
	s2410PWM->rTCNTB4 = RESCHED_INCREMENT;	//((RESCHED_PERIOD * OEM_CLOCK_FREQ) / 1000)  	
	ttmp = s2410PWM->rTCON & (~(0xf << 20));

	s2410PWM->rTCON = ttmp | (2 << 20);		/* update TCVNTB4, stop					*/
	s2410PWM->rTCON = ttmp | (1 << 20);		/* one-shot mode,  start				*/

//	s2410PWM->rTCON = 0x200000;				// update TCVNTB4 	
//	s2410PWM->rTCON = 0x100000;				// start 	

    // Number of timer counts for reschedule interval
    dwReschedIncrement = RESCHED_INCREMENT;

	// Set OEM timer count for 1 ms
    OEMCount1ms = OEM_COUNT_1MS;
    
    // Set OEM clock frequency
    OEMClockFreq = OEM_CLOCK_FREQ;

    s2410INT->rINTMSK &= ~BIT_TIMER4;		

    return;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
DWORD 
GetTimerPeriod(void) 
{

#if (OEMDEBUG)
    RETAILMSG(1, (TEXT("GetTimerPeriod.\r\n")));
#endif
    return RESCHED_PERIOD;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#define FROM_BCD(n)		((((n) >> 4) * 10) + ((n) & 0xf))

BOOL 
OEMGetRealTime(LPSYSTEMTIME lpst) 
{
    volatile RTCreg *s2410RTC = (RTCreg *)RTC_BASE;
    
	lpst->wSecond    = FROM_BCD(s2410RTC->rBCDSEC );
	lpst->wMinute	 = FROM_BCD(s2410RTC->rBCDMIN );
	lpst->wHour		 = FROM_BCD(s2410RTC->rBCDHOUR);

	lpst->wDayOfWeek = s2410RTC->rBCDDATE - 1;

	lpst->wDay		 = FROM_BCD(s2410RTC->rBCDDAY );
	lpst->wMonth	 = FROM_BCD(s2410RTC->rBCDMON );
	lpst->wYear		 = FROM_BCD(s2410RTC->rBCDYEAR) + 2000;

    return TRUE;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#define TO_BCD(n)		((((n) / 10) << 4) | ((n) % 10))

BOOL 
OEMSetRealTime(LPSYSTEMTIME lpst) 
{
    volatile INTreg *s2410INT = (INTreg *)INT_BASE;
    volatile RTCreg *s2410RTC = (RTCreg *)RTC_BASE;
    
    s2410RTC->rRTCCON = (1 << 3) | (1 << 0);		/* RTC Control Enable & Reset	*/
    
    s2410RTC->rBCDSEC  = (unsigned char)TO_BCD(lpst->wSecond );
    s2410RTC->rBCDMIN  = (unsigned char)TO_BCD(lpst->wMinute );
    s2410RTC->rBCDHOUR = (unsigned char)TO_BCD(lpst->wHour   );

	s2410RTC->rBCDDATE = (unsigned char)(lpst->wDayOfWeek + 1);
    s2410RTC->rBCDDAY  = (unsigned char)TO_BCD(lpst->wDay    );
    s2410RTC->rBCDMON  = (unsigned char)TO_BCD(lpst->wMonth  );
    s2410RTC->rBCDYEAR = (unsigned char)TO_BCD((lpst->wYear % 100));

	s2410RTC->rRTCCON = (0 << 0);					/* RTC Control Disable			*/

	s2410INT->rSRCPND  =  BIT_RTC;					/* RTC Alarm Interrupt Clear	*/
	s2410INT->rINTPND  =  BIT_RTC;
	s2410INT->rINTMSK &= ~BIT_RTC;					/* RTC Alarm Enable				*/

    return TRUE;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BOOL 
OEMSetAlarmTime(LPSYSTEMTIME lpst) 
{
    volatile RTCreg *s2410RTC = (RTCreg *)RTC_BASE;
    
    s2410RTC->rRTCCON = (1 << 0);					/* RTC Control Enable 			*/
    
    s2410RTC->rALMSEC  = (unsigned char)TO_BCD(lpst->wSecond );
    s2410RTC->rALMMIN  = (unsigned char)TO_BCD(lpst->wMinute );
    s2410RTC->rALMHOUR = (unsigned char)TO_BCD(lpst->wHour   );

    s2410RTC->rALMDAY  = (unsigned char)TO_BCD(lpst->wDay    );
    s2410RTC->rALMMON  = (unsigned char)TO_BCD(lpst->wMonth  );
    s2410RTC->rALMYEAR = (unsigned char)TO_BCD((lpst->wYear % 100));

	s2410RTC->rRTCCON = (0 << 0);					/* RTC Control Disable			*/

	
    return TRUE;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
DWORD
PerfCountFreq()
{
#if (OEMDEBUG)
    RETAILMSG(1, (TEXT("PerfCountFreq\r\n")));
#endif
    return (OEMClockFreq);
}



//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
DWORD
PerfCountSinceTick()
{
    volatile PWMreg *s2410PWM;
    DWORD dwCount;

    s2410PWM = (PWMreg *)PWM_BASE;

#if (OEMDEBUG)
    RETAILMSG(1, (TEXT("PerfCountSinceTick.\r\n")));
#endif
    
    dwCount= ((DWORD)s2410PWM->rTCNTO4);
    
    // Note: if dwCount is negative, the counter went past the match point.  The math
	// still works since it accounts for the dwReschedIncr time plus the time past
	// the match.
    return dwCurReschedIncr - dwCount;
}


//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void
CPUSetSysTimerCount(
    DWORD dwCountdownMSec
    )
{
//    DWORD dwCount, dwMatch, ttmp;
	DWORD dwMatch, ttmp;
	volatile PWMreg *s2410PWM;

    s2410PWM = (PWMreg *)PWM_BASE;
	
    dwCurReschedIncr = dwCountdownMSec * OEMCount1ms;
    dwMatch = dwCurReschedIncr;
//    dwCount = (DWORD)s2410PWM->rTCNTO4;
//    dwMatch = dwCount + dwCurReschedIncr;

#if (OEMDEBUG)
//	RETAILMSG(1, (TEXT("Moving match to 0x%08X, count is 0x%08X (%d ms)\r\n"), dwMatch, dwCount, dwCountdownMSec));
	RETAILMSG(1, (TEXT("Moving match to 0x%08X (%d ms)\r\n"), dwMatch, dwCountdownMSec));
#endif

//	WHY ??
//	s2410PWM->rTCNTB4 = dwMatch;
//	s2410PWM->rTCNTB4 = dwCurReschedIncr;

	s2410PWM->rTCNTB4 = dwMatch;

	ttmp = s2410PWM->rTCON & (~(0xf << 20));

	s2410PWM->rTCON = ttmp | (2 << 20);		/* update TCVNTB4, stop					*/
	s2410PWM->rTCON = ttmp | (1 << 20);		/* one-shot mode,  start				*/

//	s2410PWM->rTCON = 0x200000;     // update TCVNTB4 	
//	s2410PWM->rTCON = 0x100000;     // start 	
}


//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BOOL
CPUClearSysTimerIRQ(
    void
    )
{
	volatile INTreg *s2410INT; 
	BOOL fPending;
	DWORD intstatus;
	    
	s2410INT = (INTreg *)INT_BASE;
		
#if (OEMDEBUG)
	RETAILMSG(1, (TEXT("CPUClearSysTimerIRQ\r\n")));
#endif
	intstatus = s2410INT->rSRCPND;

	if ((intstatus & (BIT_TIMER4)) != 0) {
#if (OEMDEBUG)
		RETAILMSG(1, (TEXT("[PENDING]\r\n")));
#endif
		s2410INT->rSRCPND = BIT_TIMER4;        
		s2410INT->rINTPND = BIT_TIMER4;
		
		fPending = TRUE;
	} else {
		fPending = FALSE;
	}
	
    return fPending;	
}


//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
DWORD
CPUGetSysTimerCountElapsed(
    DWORD dwTimerCountdownMSec,
    volatile DWORD *pCurMSec,
    DWORD *pPartialCurMSec,
    volatile ULARGE_INTEGER *pCurTicks
    )
{
    volatile PWMreg *s2410PWM;
	DWORD dwTick, dwCount;

#if (OEMDEBUG)
	RETAILMSG(1, (TEXT("CPUGetSysTimerCountElapsed\r\n")));
#endif
    
    s2410PWM = (PWMreg *)PWM_BASE;
    dwTick = dwTimerCountdownMSec * OEMCount1ms;

    // If timer IRQ pending, a full resched period elapsed
    if (CPUClearSysTimerIRQ( )) {
        *pCurMSec += dwTimerCountdownMSec;
        pCurTicks->QuadPart += dwTick;
        return dwTimerCountdownMSec;
    }

    // No timer IRQ pending, calculate how much time has elapsed
	dwCount= ((DWORD)s2410PWM->rTCNTO4);	
	
#if (OEMDEBUG)
	RETAILMSG(1, (TEXT("dwTick=%d, dwCount=%d\r\n"),dwTick, dwCount));
#endif

    if (dwCount > dwTick) {
        // This is an error case.  Recover gracefully.
#if (OEMDEBUG)
        RETAILMSG(1, (L"CPUGetSysTimerCountElapsed( ): Correcting periodic timer read error\r\n"));
#endif
        dwCount = dwTick;
    } else {
        dwCount = dwTick - dwCount;
    }

    pCurTicks->QuadPart += dwCount;

    dwCount += *pPartialCurMSec;
    *pPartialCurMSec = dwCount % OEMCount1ms;
    *pCurMSec += (dwCount /= OEMCount1ms);

    return dwCount;
}





//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
extern void CPUEnterIdleMode(void);

extern void OEMWriteDebugLED(WORD wIndex, DWORD dwPattern);


void
CPUEnterIdle()
{

	static volatile CLKPWRreg * s2410CLKPW = (CLKPWRreg *)CLKPWR_BASE;
	static volatile IOPreg    * s2410IOP   = (IOPreg    *)IOP_BASE;

	static volatile UART0reg    * s2410UART0   = (UART0reg    *)UART0_BASE; // for serial
	static volatile UART2reg    * s2410UART2   = (UART0reg    *)UART2_BASE; // for serial
	
    int i;

    INTERRUPTS_ON();
  
	s2410IOP->rGPFDAT &= ~(1 << 5);
	if ( !(s2410UART0->rUFSTAT & 0x3ff) || !(s2410UART2->rUFSTAT & 0x3ff) )	// For Serial
		s2410CLKPW->rCLKCON |=  (1 << 2);           /* Enter IDLE Mode                              */
	
	for (i = 0; i < 10; i++);                   	/* Wait until S3C2400X enters IDLE mode         */
		s2410CLKPW->rCLKCON &= ~(1 << 2);           /* turn-off IDLE bit.                           */
                                                			/* Any interrupt will wake up from IDLE mode    */
		s2410IOP->rGPFDAT |= (1 << 5);

    
}


// Maximum idle is fixed a 1 ms because the PIT has to count down to 0 before reloading
// the new countdown value.
#define IDLE_MAX_MS  100
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
DWORD
CPUGetSysTimerCountMax(
    DWORD dwIdleMSecRequested
    )
{
#if(OEMDEBUG)
    RETAILMSG(1, (TEXT("CPUGetSysTimerCountMax %x requested\r\n"), dwIdleMSecRequested));
#endif
    if (dwIdleMSecRequested > IDLE_MAX_MS) {
        //
        // Our timer isn't capable of idling more than IDLE_MAX_MS milliseconds. 
        // We'll have to break the sleep time into reasonable chunks.
        //
        return IDLE_MAX_MS;
    }

    return dwIdleMSecRequested;
}


 

⌨️ 快捷键说明

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