system.c

来自「WinCE 3.0 BSP, 包含Inter SA1110, Intel_815」· C语言 代码 · 共 1,664 行 · 第 1/4 页

C
1,664
字号
	    return FALSE;
	}
	dwTimer = 0;

	while( *ATAPI_BCTL != 0x14 ){						// Byte Count Low  = 14h after Device Reset
		Sleep(ATAPI_SLEEP);								// 100ms SLEEP
		dwTimer++;
		if(dwTimer>ATAPI_SLEEP)
	   		break;
	}
	if(dwTimer>ATAPI_SLEEP){
	    SetLastError(ERROR_NOT_READY);
 		RETAILMSG(1, (TEXT("ATPCOM_SOFTRESET Command : GetLastError() = %d\r\n"), GetLastError()));
	    return FALSE;
	}
	dwTimer = 0;
	while( *ATAPI_BCTH != 0xeb ){						// Byte Count High = EBh after Device Reset
		Sleep(ATAPI_SLEEP);								// 100ms SLEEP
		dwTimer++;
		if(dwTimer>ATAPI_SLEEP)
	   		break;
	}
	if(dwTimer>ATAPI_SLEEP){
	    SetLastError(ERROR_NOT_READY);
 		RETAILMSG(1, (TEXT("ATPCOM_SOFTRESET Command : GetLastError() = %d\r\n"), GetLastError()));
	    return FALSE;
	}

	
	dwCount = 0;
	while( TurnATAPI() == FALSE ){						// Byte Count High = EBh after Device Reset
		dwCount++;
		if(dwCount>ATAPI_SLEEP)
	   		break;
	}
	if(dwCount>ATAPI_SLEEP){
		if(ATAPI_errcode != ERROR_NO_MEDIA_IN_DRIVE){
	   		 SetLastError(ATAPI_errcode);
		 	 RETAILMSG(1, (TEXT("ATAPI_TESTUNITREADY Command : GetLastError() = %d\r\n"), GetLastError()));
	  		  return FALSE;
	    }
	}

// Device selection protocol
	dwTimer = 0;
	while( ( *ATAPI_STAT & ATPMASK_BUSYDREQ ) != 0){	// Status Reg BSY , DRQ = 0
		Sleep(ATAPI_SLEEP);								// 100ms SLEEP
		dwTimer++;
		if(dwTimer>ATAPI_SLEEP)
	   		break;
	}
	if(dwTimer>ATAPI_SLEEP){
	    SetLastError(ERROR_NOT_READY);
	    return FALSE;
	}
	dwTimer = 0;

	*ATAPI_DSEL = ATAPI_DEVICESELECT;					// Device Select ( DRIVE 0 master )
	while( ( *ATAPI_STAT & ATPMASK_BUSYDREQ ) != 0){	// Status Reg BSY , DRQ = 0
		Sleep(ATAPI_SLEEP);								// 100ms SLEEP
		dwTimer++;
		if(dwTimer>ATAPI_SLEEP)
	   		break;
	}
	if(dwTimer>ATAPI_SLEEP){
	    SetLastError(ERROR_NOT_READY);
	    return FALSE;
	}
	dwTimer = 0;

// Non-data command Protocol
	*ATAPI_FETR = 0x00;									//  ATAPI Features 
	*ATAPI_IRRN = 0x00;									//  Sector Count  
	*ATAPI_RSVE = 0x00;									//  Sector Number
	*ATAPI_BCTL = 0x00;									//  Cylinder Low
	*ATAPI_BCTH = 0x02;									//  Cylinder High
	*ATAPI_DCTR = 0x02;									//  Device Control ( INTRQ-OFF )
	*ATAPI_COMD = ATPCOM_IDENTFYDEVICE;					//  ATA Command ( ATAPI Identify Device Command )

// Status Reg BSY=0 , DRQ=1 ? 
	while( ( *ATAPI_STAT & ATPMASK_BUSYDREQ ) != 0x08 ){// Status BSY=0,DRQ=1 then next 
		Sleep(ATAPI_SLEEP);								// 100ms SLEEP
		dwTimer++;
		if(dwTimer>ATAPI_SLEEP)
	   		break;
	}
	if(dwTimer>ATAPI_SLEEP){
	    SetLastError(ERROR_NOT_READY);
	    return FALSE;
	}
	dwTimer = 0;

	if( ( *ATAPI_STAT & ATPMASK_CHEK) != 0 ){			// Error ?
		GetATAPError();
		SetLastError(ATAPI_errcode);
       return FALSE;
	}

	rdata = (WORD*)Kdata;								// Saved data pointer 

// Execute Data Read
	dwCount = 0;
	while(dwCount < 256){
		*rdata++ = *ATAPI_DATW;							// Saved sense data 
		dwCount++;
	}

	dwPIOmode = Kdata[51];								//Device PIO data transfer mode number
	switch(dwPIOmode){
		case 0x0100:	dwPIOdata = 0x09;				//Device PIO data transfer mode 1
						break;
		case 0x0200:	dwPIOdata = 0x0a;				//Device PIO data transfer mode 2
						break;
		case 0x0300:	dwPIOdata = 0x0b;				//Device PIO data transfer mode 3
						break;
		case 0x0400:	dwPIOdata = 0x0c;				//Device PIO data transfer mode 4
						break;
		case 0x0500:	dwPIOdata = 0x0d;				//Device PIO data transfer mode 5
						break;
		default:		dwPIOdata = 0x00;				//Device PIO data transfer mode default
						break;
	}
// Device selection protocol
	dwTimer = 0;
	while( ( *ATAPI_STAT & ATPMASK_BUSYDREQ ) != 0){	// Status Reg BSY , DRQ = 0
		Sleep(ATAPI_SLEEP);								// 100ms SLEEP
		dwTimer++;
		if(dwTimer>ATAPI_SLEEP)
	   		break;
	}
	if(dwTimer>ATAPI_SLEEP){
	    SetLastError(ERROR_NOT_READY);
	    return FALSE;
	}
	dwTimer = 0;
	*ATAPI_DSEL = ATAPI_DEVICESELECT;					// Device Select ( DRIVE 0 master )
	while( ( *ATAPI_STAT & ATPMASK_BUSYDREQ ) != 0){	// Status Reg BSY , DRQ = 0
		Sleep(ATAPI_SLEEP);								// 100ms SLEEP
		dwTimer++;
		if(dwTimer>ATAPI_SLEEP)
	   		break;
	}
	if(dwTimer>ATAPI_SLEEP){
	    SetLastError(ERROR_NOT_READY);
 		RETAILMSG(1, (TEXT("ATAPI Identify Device Command : GetLastError() = %d\r\n"), GetLastError()));
	    return FALSE;
	}
	dwTimer = 0;
// Non-data command Protocol
	*ATAPI_FETR = 0x03;									//  ATAPI Features ( Set Transfer Mode 03h )
	*ATAPI_IRRN = 00; //dwPIOdata;							//  Sector Count  (0x00:PIO mode, default)

														//  (0x0b:PIO flow control transfer mode, mode3)
														//  (0x0c:PIO flow control transfer mode, mode4)
	*ATAPI_RSVE = 0x00;									//  Sector Number
	*ATAPI_BCTL = 0x00;									//  Cylinder Low
	*ATAPI_BCTH = 0x00;									//  Cylinder High
	*ATAPI_DCTR = 0x02;									//  Device Control ( INTRQ-OFF )
	*ATAPI_COMD = ATPCOM_SETFEATURE;					//  ATA Command ( Set Feature Command )

// Set Feature Command operation complete ?
	dwTimer = 0;
	while( ( *ATAPI_STAT & ATPMASK_BUSY ) != 0){		// Status Reg BSY=0 then next
		Sleep(ATAPI_SLEEP);								// 100ms SLEEP
		dwTimer++;
		if(dwTimer>ATAPI_SLEEP)
	   		break;
	}
	if(dwTimer>ATAPI_SLEEP){
	    SetLastError(ERROR_NOT_READY);
 		RETAILMSG(1, (TEXT("ATAPI Set Feature Command : GetLastError() = %d\r\n"), GetLastError()));

	    return FALSE;
	}

	
	if( ( *ATAPI_STAT & ATPMASK_CHEK) != 0 ){			// Error ?
		GetATAPError();
		SetLastError(ATAPI_errcode);
 		RETAILMSG(1, (TEXT("ATAPI Set Feature Command : GetLastError() = %d\r\n"), GetLastError()));
        return FALSE;
	}

//    if( !StartStopCD( 0x0100 ) )						// Start rotation CD-ROM
//    	return FALSE;

	return TRUE;

}   // ATAPIDetect

WORD
TurnATAPI()
{
// Device selection protocol 
	dwTimer = 0;
	while( ( *ATAPI_STAT & ATPMASK_BUSYDREQ ) != 0){	// Status Reg BSY , DRQ = 0  
		Sleep(ATAPI_SLEEP);								// 100ms SLEEP
		dwTimer++;
		if(dwTimer>ATAPI_SLEEP)
	   		break;
	}
	if(dwTimer>ATAPI_SLEEP){
	    SetLastError(ERROR_NOT_READY);
	 	RETAILMSG(1, (TEXT("ATAPI_TESTUNITREADY Command : GetLastError() = %d\r\n"), GetLastError()));
	    return FALSE;
	}
	dwTimer = 0;

	*ATAPI_DSEL = ATAPI_DEVICESELECT;					// Device Select ( DRIVE 0 master )
	while( ( *ATAPI_STAT & ATPMASK_BUSYDREQ ) != 0){  	// Status Reg BSY , DRQ = 0  
		Sleep(ATAPI_SLEEP);								// 100ms SLEEP
		dwTimer++;
		if(dwTimer>ATAPI_SLEEP)
	   		break;
	}
	if(dwTimer>ATAPI_SLEEP){
	    SetLastError(ERROR_NOT_READY);
	 	RETAILMSG(1, (TEXT("ATAPI_TESTUNITREADY Command : GetLastError() = %d\r\n"), GetLastError()));
	    return FALSE;
	}


// PACKET command protocol 
	*ATAPI_FETR = 0;									// ATAPI Features ( Not use OverLap and DMA )
	*ATAPI_IRRN = 0;									// Sector Count   
	*ATAPI_RSVE = 0;									// Sector Number   
	*ATAPI_BCTL = 0;									// Byte Count Low ( 512 byte )
	*ATAPI_BCTH = 0;									// Byte Count High	
	*ATAPI_DCTR = 0x02;									// Device Control ( INTRQ-OFF )
	*ATAPI_COMD = ATPCOM_ATAPIPAKET;					// ATA Command ( Packet Command )

// Status Reg BSY=0 , DRQ=1 ? 
	dwTimer = 0;
	while( ( *ATAPI_STAT & ATPMASK_BUSYDREQ ) != 0x08 ){	// Status BSY=0,DRQ=1 then next 
		Sleep(ATAPI_SLEEP);									// 100ms SLEEP
		dwTimer++;
		if(dwTimer>ATAPI_SLEEP)
	   		break;
	}
	if(dwTimer>ATAPI_SLEEP){
	    SetLastError(ERROR_NOT_READY);
	 	RETAILMSG(1, (TEXT("ATAPI_TESTUNITREADY Command : GetLastError() = %d\r\n"), GetLastError()));
	    return FALSE;
	}
// Write Data Register (Command Packet) 
	*ATAPI_DATW = ATAPI_TESTUNITREADY;	//0,1 Command code 
	*ATAPI_DATW = 0;					// 2,3  Reserved 
	*ATAPI_DATW = 0;					// 4,5  Reserved 
	*ATAPI_DATW = 0;					// 6,7  Reserved 
	*ATAPI_DATW = 0;					// 8,9  Reserved 
	*ATAPI_DATW = 0;					//10,11 Reserved 

// Status Reg BSY=0 , DRDY=1 ? 
	dwTimer = 0;
	while( ( *ATAPI_STAT & ATPMASK_BUSYDRDY ) != 0x40 ){	// Status BSY=0,DRDY=1 then next 
		Sleep(ATAPI_SLEEP);									// 100ms SLEEP
		dwTimer++;
		if(dwTimer>ATAPI_SLEEP)
	   		break;
	}
	if(dwTimer>ATAPI_SLEEP){
	    SetLastError(ERROR_NOT_READY);
	 	RETAILMSG(1, (TEXT("ATAPI_TESTUNITREADY Command : GetLastError() = %d\r\n"), GetLastError()));
	    return FALSE;
	}

	if( ( *ATAPI_STAT & ATPMASK_CHEK) != 0 ){				// Error ? 
		GetATAPError();
        return FALSE;
	}
	return TRUE;
}

//
// Start/Stop and Eject operations
//
WORD
StartStopCD(
	WORD StartBit
	)
{
// LoEj Start
//  0    0     Stop Disk
//  0    1     Start the Disc and read the TOC
//  1    0     Eject the Disc if possible
//  1    1     Load the Disc

// Device selection protocol 
	dwTimer = 0;
	while( (  *ATAPI_STAT & ATPMASK_BUSYDREQ ) != 0){	// Status Reg BSY , DRQ = 0  
		Sleep(ATAPI_SLEEP);								// 100ms SLEEP
		dwTimer++;
		if(dwTimer>ATAPI_SLEEP)
	   		break;
	}
	if(dwTimer>ATAPI_SLEEP){
	    SetLastError(ERROR_NOT_READY);
	 	RETAILMSG(1, (TEXT("StartStopCD Command : GetLastError() = %d\r\n"), GetLastError()));
	    return FALSE;
	}

	*ATAPI_DSEL = ATAPI_DEVICESELECT;					// Device Select ( DRIVE 0 master )
	dwTimer = 0;
	while( ( *ATAPI_STAT & ATPMASK_BUSYDREQ ) != 0){	// Status Reg BSY , DRQ = 0 
		Sleep(ATAPI_SLEEP);								// 100ms SLEEP
		dwTimer++;
		if(dwTimer>ATAPI_SLEEP)
	   		break;
	}
	if(dwTimer>ATAPI_SLEEP){
	    SetLastError(ERROR_NOT_READY);
	 	RETAILMSG(1, (TEXT("StartStopCD Command : GetLastError() = %d\r\n"), GetLastError()));
	    return FALSE;
	}

// PACKET command protocol 
	*ATAPI_FETR = 0;									// ATAPI Features ( Not use OverLap and DMA )
	*ATAPI_IRRN = 0;									// Sector Count 
	*ATAPI_RSVE = 0;									// Sector Number
	*ATAPI_BCTL = 0;									// Byte Count Low ( 512 byte )
	*ATAPI_BCTH = 0;									// Byte Count High	
	*ATAPI_DCTR = 0x02;									// Device Control ( INTRQ-OFF )
	*ATAPI_COMD = ATPCOM_ATAPIPAKET;					// ATA Command ( Packet Command ) 
	
// Status Reg BSY=0 , DRQ=1 ? 
	dwTimer = 0;
	while( ( *ATAPI_STAT & ATPMASK_BUSYDREQ ) != 0x08 ){	// Status BSY=0,DRQ=1 then next 
		Sleep(ATAPI_SLEEP);									// 100ms SLEEP
		dwTimer++;
		if(dwTimer>ATAPI_SLEEP)
	   		break;
	}
	if(dwTimer>ATAPI_SLEEP){
	    SetLastError(ERROR_NOT_READY);
	 	RETAILMSG(1, (TEXT("StartStopCD Command : GetLastError() = %d\r\n"), GetLastError()));
	    return FALSE;
	}

//  Command Packet send (DS4-compati) 
	*ATAPI_DATW = ATAPI_STARTSTOPUNIT;					//0,1 Command code
	*ATAPI_DATW = 0;									// 2,3  Reserved
	*ATAPI_DATW = StartBit;								// 4,5  
	*ATAPI_DATW = 0;									// 6,7  
	*ATAPI_DATW = 0;									// 8,9  
	*ATAPI_DATW = 0;									//10,11

// Complete Start Stop operation 丠 
	dwTimer = 0;
	while( ( *ATAPI_STAT & ATPMASK_BUSY ) != 0){		// Status Reg BSY=0 then next 
		Sleep(ATAPI_SLEEP);								// 100ms SLEEP
		dwTimer++;
		if(dwTimer>ATAPI_SLEEP)
	   		break;
	}
	if(dwTimer>ATAPI_SLEEP){
	    SetLastError(ERROR_NOT_READY);
	 	RETAILMSG(1, (TEXT("StartStopCD Command : GetLastError() = %d\r\n"), GetLastError()));
	    return FALSE;
	}

	if( (*ATAPI_STAT & ATPMASK_CHEK) != 0 ){			// Error ? 
		GetATAPError();
		SetLastError(ATAPI_errcode);
 		RETAILMSG(1, (TEXT("ATAPI_STARTSTOPUNIT Command : GetLastError() = %d\r\n"), GetLastError()));
        return FALSE;
	}

   return TRUE;
}

WORD
GetATAPError()
{
uchar		err ;
	CD_ROM_STA.err = 0;
	err = (UCHAR)*ATAPI_AERR;			// Read Error Reg
	if( (err & 0x01) != 0 )
		CD_ROM_STA.err = 0x01;			// Illigal Length Indication
	if( (err & 0x02) != 0 )
		CD_ROM_STA.err += 0x02;			// End of Media
	if( (err & 0x04) != 0 )
		CD_ROM_STA.err += 0x04;			// Aborted Command 
	if( (err & 0x08) != 0 )
		CD_ROM_STA.err += 0x08;			// Media Change Requested
	if( (err & 0xf0) != 0 )
		CD_ROM_STA.err += (err & 0xf0) ;// Sence Key 
	SenseData();

	return TRUE;
}

WORD
SenseData()
{
	WORD	*rdata;
//	WORD	SenseKey;
//	WORD	SenseCode;

// Device selection protocol
	dwTimer = 0;
	while( (  *ATAPI_STAT & 0x88 ) != 0){		// Status Reg BSY , DRQ = 0 
		Sleep(ATAPI_SLEEP);						// 100ms SLEEP
		dwTimer++;
		if(dwTimer>ATAPI_SLEEP)
	   		break;
	}
	if(dwTimer>ATAPI_SLEEP){
	    SetLastError(ERROR_NOT_READY);
	 	RETAILMSG(1, (TEXT("ATAPI_REQUESTSENSE Command : GetLastError() = %d\r\n"), GetLastError()));
	    return FALSE;
	}
	 *ATAPI_DSEL = 0xa0;						// Device Select ( DRIVE 0 master )
	dwTimer = 0;
	while( ( *ATAPI_STAT & 0x88 ) != 0){		// Status Reg BSY , DRQ = 0
		Sleep(ATAPI_SLEEP);						// 100ms SLEEP
		dwTimer++;

⌨️ 快捷键说明

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