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

📄 bulk.c

📁 ISP1362 Fat32 coldfire523x
💻 C
字号:
#include "Bulk.h"

uint32 DirStartCluster32, NowCluster32, NowSector;
uint8  DBUF[512];
extern uint16 PTD [20];
SFATBPBINFO DeviceInfo;
SFILEINFO   ThisFile;
extern SUDEV  uDev;
extern SFLAGS bFlags;

uint8 EnumMassStorage(uint8* WriteFileData)
{
    if(! WritePairedSector((uint16 *)WriteFileData, 0x50000, 256))
	{
	    return FALSE;
	}
    if(! GetDecBPBInfo())
	{
	    return FALSE;
	}
    	
	return TRUE;
}
									
uint8 ReadSector(uint8 * pSec, uint32 SecNum, uint16 SecCnt)
{
    uint32 Index = 0 ,LgcSecAddr;
    uint16 Translen, ByteLen;
	uint16 SCSI_READ[16] = {0}; 

    LgcSecAddr = SecNum;
    Translen = SecCnt;
    ByteLen  = (SecCnt * DeviceInfo.BPB_BytesPerSec);

    /******************OUT/CBW**************************************/
    PTD[0] = (0xF800 | (uDev.OutTogl   << 10));
    PTD[1] = (0x0040 | (uDev.epbulkout << 12));
    PTD[2] = 0x041F;  PTD[3] = 0x0001;
    SCSI_READ[0] = 0x5355;  SCSI_READ[1] = 0x4342; SCSI_READ[2] = 0xA660;  SCSI_READ[3] = 0xDE24;
    SCSI_READ[4] = ByteLen;  
    SCSI_READ[5] = 0x0000;  SCSI_READ[6] = 0x0080; SCSI_READ[7] = 0x280A;
    SCSI_READ[8] = (0x0000 | (uint16)((LgcSecAddr & 0xFF000000) >> 16));
    SCSI_READ[9] = (0x0000 | (uint16)((LgcSecAddr & 0x00FF0000) >> 16) | (uint16)(LgcSecAddr & 0x0000FF00));
    SCSI_READ[10] = (0x0000 | (uint16)(LgcSecAddr & 0x000000FF));
    SCSI_READ[11] = (0x0000 | ((Translen & 0xFF00) >> 8) | ((Translen & 0x00FF) << 8));

    WriteATLDirect(SCSI_READ, (ATLSTARTADDR + 0x08), 0x20);
    if(! Ep0Xfer(PTD, ATLSTARTADDR, Index++))
    {
        return FALSE;
    }

	/*****************IN/DATA ( 8 frames、or 8 IN Packets )*********/
	PTD[0] = (0xF800 | (uDev.InTogl   << 10));
	PTD[1] = (0x0040 | (uDev.epbulkin << 12));
	PTD[2] = 0x0A00;
	PTD[3] = 0x0001;
    if(! Ep0Xfer(PTD, (ATLSTARTADDR + Hc_PTDLen), Index++))
    {
        return FALSE;
    }
    uDev.InTogl ^= 1;

	/*****************IN/CSW****************************************/
	PTD[0] = 0xF800 | (uDev.InTogl   << 10);
	PTD[1] = 0x0040 | (uDev.epbulkin << 12);
	PTD[2] = 0x080D;
	PTD[3] = 0x0001;
    if(! Ep0Xfer(PTD, (ATLSTARTADDR + (Hc_PTDLen << 1)), Index))
    {
        return FALSE;
    }	
	
	ReadATLDirect((uint16 *)pSec, (ATLSTARTADDR + Hc_PTDLen + 0x08), 0x200);

	return TRUE;
}

uint8 WriteSector(uint8 * pSec, uint32 SecNum, uint16 SecCnt)
{
    uint32 Index = 0 ,LgcSecAddr;
    uint16 Translen, ByteLen;
	uint16 SCSI_WRITE[16] = {0};

    LgcSecAddr = SecNum;
    Translen = SecCnt;
    ByteLen = (SecCnt * DeviceInfo.BPB_BytesPerSec);

    SwapWriteATLDirect((uint16 *)pSec, (ATLSTARTADDR + Hc_PTDLen + 0x08), 0x200);

    /******************OUT/CBW**************************************/
    PTD[0] = (0xF800 | (uDev.OutTogl   << 10));
    PTD[1] = (0x0040 | (uDev.epbulkout << 12));
    PTD[2] = 0x041F;  PTD[3] = 0x0001;
    SCSI_WRITE[0] = 0x5355;  SCSI_WRITE[1] = 0x4342; SCSI_WRITE[2] = 0xD9B4;  SCSI_WRITE[3] = 0xC177;
    SCSI_WRITE[4] = ByteLen;  
    SCSI_WRITE[5] = 0x0000;  SCSI_WRITE[6] = 0x0000; SCSI_WRITE[7] = 0x2A0A;
    SCSI_WRITE[8] = (0x0000 | (uint16)((LgcSecAddr & 0xFF000000) >> 16));
    SCSI_WRITE[9] = (0x0000 | (uint16)((LgcSecAddr & 0x00FF0000) >> 16) | (uint16)(LgcSecAddr & 0x0000FF00));
    SCSI_WRITE[10] = (0x0000 | (uint16)(LgcSecAddr & 0x000000FF));
    SCSI_WRITE[11] = (0x0000 | ((Translen & 0xFF00) >> 8) | ((Translen & 0x00FF) << 8));

    WriteATLDirect(SCSI_WRITE, (ATLSTARTADDR + 0x08), 0x20);
    if(! Ep0Xfer(PTD, ATLSTARTADDR, Index++))
    {
        return FALSE;
    }

	/*****************OUT/DATA ( 8 frames、or 8 IN Packets )*********/
   	PTD[0] = (0xF800 | (uDev.OutTogl   << 10));
   	PTD[1] = (0x0040 | (uDev.epbulkout << 12));
   	PTD[2] = 0x0600;
   	PTD[3] = 0x0001;
    if(! Ep0Xfer(PTD, (ATLSTARTADDR + Hc_PTDLen), Index++))
    {
        return FALSE;
    }
  	uDev.OutTogl ^= 1;
  
	/******************IN/CSW****************************************/
	PTD[0] = 0xF800 | (uDev.InTogl   << 10);
	PTD[1] = 0x0010 | (uDev.epbulkin << 12);
	PTD[2] = 0x080D;
	PTD[3] = 0x0001;
    if(! Ep0Xfer(PTD, (ATLSTARTADDR + (Hc_PTDLen << 1)), Index))
    {
        return FALSE;
    }	
	
	return TRUE;
}

uint8 ReadPairedSector(uint8 * pSec, uint32 SecNum, uint16 SecCnt)
{
    uint32 LgcSecAddr, count = 0;
    uint16 Translen, ByteLen, PingPong = 0;
	uint16 SCSI_READ[16] = {0};

    LgcSecAddr = SecNum;
    Translen = SecCnt;
    ByteLen  = (SecCnt * DeviceInfo.BPB_BytesPerSec);

    SCSI_READ[0] = 0x5355;  SCSI_READ[1] = 0x4342; SCSI_READ[2] = 0xA660;  SCSI_READ[3] = 0xDE24;
    SCSI_READ[4] = ByteLen;  
    SCSI_READ[5] = 0x0000;  SCSI_READ[6] = 0x0080; SCSI_READ[7] = 0x280A;
    SCSI_READ[8] = (0x0000 | (uint16)((LgcSecAddr & 0xFF000000) >> 16));
    SCSI_READ[9] = (0x0000 | (uint16)((LgcSecAddr & 0x00FF0000) >> 16) | (uint16)(LgcSecAddr & 0x0000FF00));
    SCSI_READ[10] = (0x0000 | (uint16)(LgcSecAddr & 0x000000FF));
    SCSI_READ[11] = (0x0000 | ((Translen & 0xFF00) >> 8) | ((Translen & 0x00FF) << 8));

    ISP1362Reg32Write(Hc_ATLCurrentActivePTD, 0xFFFF);		  /* ACTIVE BLOCK */
    ISP1362Reg32Write(Hc_ATLPTDSkipMap, 0xFFFFFFFC);		  /* ACTIVE BLOCK */
    ISP1362Reg32Write(Hc_ATLLastPTD, 0x00000002);

	//-------------------FILLED PING AND PONG ( 8 frames、or 8 IN Packets )-----------
    ResetPairedPTD();
    PTD[0] = (0xF800 | (uDev.OutTogl   << 10));
    PTD[1] = (0x0020 | (uDev.epbulkout << 12));
    PTD[2] = 0x841F;  
    PTD[3] = 0x0001;
    WriteATLDirect(SCSI_READ, 0x08, 0x20);
    WriteATLDirect(PTD, 0, 8);              /* 写 PING PTD头 */
	uDev.OutTogl ^= 1;
	PingPong ^= 1;

	do
	{
       	PTD[0] = (0xF800 | (uDev.OutTogl   << 10));
       	PTD[1] = (0x0040 | (uDev.epbulkout << 12));
       	PTD[2] = (0x8A00 | (PingPong << 14));
       	PTD[3] = 0x0001;
		if(! PingPong)
		{
            WriteATLDirect(PTD, ATLSTARTADDR, 8);
		}
		else
		{			 
            WriteATLDirect(PTD, ATLSTARTADDR + Hc_PTDLen, 8);
		}

    	if(! CheckPingPong(PingPong))
    	{
    	    return FALSE;
    	}
        ReadATLDirect((uint16 *)(pSec + count * 0x100), ATLSTARTADDR + 0x8, 0x200);
        ReadATLDirect((uint16 *)(pSec + count * 0x100), ATLSTARTADDR + Hc_PTDLen + 0x8, 0x200);

        PingPong ^= 1;
		ByteLen -= 0x200;
		count ++;

	}while(ByteLen != 0);

	PTD[0] = 0xF800 | (uDev.InTogl   << 10);										  
	PTD[1] = 0x0010 | (uDev.epbulkin << 12);
	PTD[2] = 0x880D | (PingPong << 14);
	PTD[3] = 0x0001;
    if(! PingPong)                                         // 非PONG
	{
        WriteATLDirect(PTD, ATLSTARTADDR, 8);
   	}
	else
	{
        WriteATLDirect(PTD, ATLSTARTADDR + Hc_PTDLen, 8);
	}

    if(! CheckPingPong(PingPong))
	{
	    return FALSE;
	}
    PingPong ^= 1;
    if(! CheckPingPong(PingPong))
	{
	    return FALSE;
	}
    uDev.InTogl ^= 1;
	ResetPairedPTD();

	return TRUE;
}

uint8 WritePairedSector(uint16 * pSec, uint32 SecNum, uint16 SecCnt)
{
    uint32 LgcSecAddr, count = 0, ByteLen;
    uint16 Translen, PingPong = 0;
	uint16 SCSI_WRITE[16] = {0};

    LgcSecAddr = SecNum;
    Translen = SecCnt;
    ByteLen = (uint32)(SecCnt * DeviceInfo.BPB_BytesPerSec);

    SCSI_WRITE[0]  = 0x5355;  SCSI_WRITE[1] = 0x4342; SCSI_WRITE[2] = 0xD9B4;  SCSI_WRITE[3] = 0xC177;
    SCSI_WRITE[4]  = (ByteLen & 0xFFFF); SCSI_WRITE[5] = ((ByteLen & 0xFFFF0000) >> 16); SCSI_WRITE[7] = 0x2A0A;
    SCSI_WRITE[8]  = (0x0000 | (uint16)((LgcSecAddr & 0xFF000000) >> 16));
    SCSI_WRITE[9]  = (0x0000 | (uint16)((LgcSecAddr & 0x00FF0000) >> 16) | (uint16)(LgcSecAddr & 0x0000FF00));
    SCSI_WRITE[10] = (0x0000 | (uint16)(LgcSecAddr & 0x000000FF));
    SCSI_WRITE[11] = (0x0000 | ((Translen & 0xFF00) >> 8) | ((Translen & 0x00FF) << 8));

    PTD[0] = (0xF800 | (uDev.OutTogl   << 10));
    PTD[1] = (0x0020 | (uDev.epbulkout << 12));
    PTD[2] = (0x841F | (PingPong << 14));
    PTD[3] = 0x0001;
	ResetPairedPTD();
    WriteATLDirect(PTD, ATLSTARTADDR, 8);
    WriteATLDirect(SCSI_WRITE, ATLSTARTADDR + 0x08, 0x20);
    uDev.OutTogl ^= 1;
	PingPong ^= 1;

	do
	{
       	PTD[0] = (0xF800 | (uDev.OutTogl   << 10));
       	PTD[1] = (0x0040 | (uDev.epbulkout << 12));
       	PTD[2] = (0x8600 | (PingPong << 14));
       	PTD[3] = 0x0001;
		if(! PingPong)
		{
	        //ResetPairedPTD();
            WriteATLDirect(PTD, ATLSTARTADDR, 8);
            SwapWriteATLDirect((pSec + count * 0x100), ATLSTARTADDR + 0x8, 0x200);
		}
		else
		{
	        //ResetPairedPTD();
            WriteATLDirect(PTD, ATLSTARTADDR + Hc_PTDLen, 8);
            SwapWriteATLDirect((pSec + count * 0x100), ATLSTARTADDR + Hc_PTDLen + 0x8, 0x200);
		}

		if(! CheckPingPong(PingPong))
		{
		    return FALSE;
		}

        PingPong ^= 1;
		ByteLen -= 0x200;
		count ++;
		//if(count == 4)    count = 0;

	}while(ByteLen != 0);

	PTD[0] = 0xF800 | (uDev.InTogl   << 10);										  
	PTD[1] = 0x0010 | (uDev.epbulkin << 12);
	PTD[2] = 0x880D | (PingPong << 14);
	PTD[3] = 0x0001;
    if(! PingPong)                                         // 非PONG
	{
        WriteATLDirect(PTD, ATLSTARTADDR, 8);
   	}
	else
	{
        WriteATLDirect(PTD, ATLSTARTADDR + Hc_PTDLen, 8);
	}

    if(! CheckPingPong(PingPong))
	{
	    return FALSE;
	}
    PingPong ^= 1;
	//ResetPairedPTD();
    if(! CheckPingPong(PingPong))
	{
	    return FALSE;
	}
    uDev.InTogl ^= 1;
	ResetPairedPTD();

	return TRUE;
}

uint8 GetDecBPBInfo(void)
{
	uint16 ReservedSectorsNum;

	DeviceInfo.BPB_BytesPerSec = 0x200;	

	if(! ReadSector(DBUF, 0, 1))
	{
		return FALSE;
	}
								  
	if((DBUF[510] == 0x55) && (DBUF[511] == 0xAA))
	{
		if(((DBUF[0]  == 0xEB) && (DBUF[2] == 0x90)) || (DBUF[0] == 0xE9))
		{
			DeviceInfo.StartSector = LSwapuint32(DBUF[28], DBUF[29], DBUF[30], DBUF[31]);
		}
		else
		{
			DeviceInfo.StartSector = LSwapuint32(DBUF[454], DBUF[455], DBUF[456], DBUF[457]);
		}
	}
	else
	{
		return FALSE;
	}	

	if(! ReadSector(DBUF, DeviceInfo.StartSector, 1))
	{
		return FALSE;	
	}

	DeviceInfo.BPB_BytesPerSec = LSwapuint16((uint16)DBUF[11], (uint16)DBUF[12]);
	DeviceInfo.BPB_SecPerClus = DBUF[13];
	ReservedSectorsNum = LSwapuint16((uint16)DBUF[14], (uint16)DBUF[15]);
	DeviceInfo.BPB_NumFATs = DBUF[16];

	if((DBUF[82] == 'F') && (DBUF[83] == 'A') && (DBUF[84] == 'T') && (DBUF[85] == '3') && (DBUF[86] == '2'))
	{
		DeviceInfo.BPB_TotSec32 = LSwapuint32(DBUF[32], DBUF[33], DBUF[34], DBUF[35]);
		DeviceInfo.BPB_FATSz32 = LSwapuint32(DBUF[36], DBUF[37], DBUF[38], DBUF[39]);
		DeviceInfo.RootStartCluster = LSwapuint32(DBUF[44], DBUF[45], DBUF[46], DBUF[47]);
		DeviceInfo.FatStartSector = DeviceInfo.StartSector + ReservedSectorsNum; 
		DeviceInfo.FirstDataSector = DeviceInfo.FatStartSector + (DeviceInfo.BPB_NumFATs * DeviceInfo.BPB_FATSz32);
		DeviceInfo.TotCluster = (DeviceInfo.BPB_TotSec32 - DeviceInfo.FirstDataSector + 1) / DeviceInfo.BPB_SecPerClus + 1;
		DirStartCluster32 = DeviceInfo.RootStartCluster;

		DeviceInfo.FAT = 1;	
	}
	else
	{		
		DeviceInfo.FAT=0;	/*FAT16=0,FAT32=1;*/
	}		

	ThisFile.bFileOpen=0;
	
	return TRUE;	
}

⌨️ 快捷键说明

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