Philips PIDUSBP11 驱动代码
源代码在线查看: usbmass.c
#include
#include "usbmass.h"
unsigned char buffer[512];
unsigned char gbCBWCB[16];
unsigned char gbCBWFlags;
unsigned char gbCBWLUN;
unsigned char gbCBWCBLength;
unsigned int gdwCBWTag;
unsigned int gdwCBWDataTransferLength;
unsigned char *PageDataBuffer;
unsigned char wf = 0;
extern ARMUSBFLAGS bUSBFlags;
extern unsigned char SBufFlag;
void CheckReceiveCBW()
{
int i;
unsigned char * RxBuffer;
RxBuffer = Ep2BufRx;
for(i=0; i {
gbCBWCB[i] = *RxBuffer ++;
}
if((gbCBWCB[0]==0x55)&&(gbCBWCB[1]==0x53)&&(gbCBWCB[2]==0x42)&&(gbCBWCB[3]==0x43))
{
*((unsigned char *)&gdwCBWTag+3) = *RxBuffer ++; // LSB read first
*((unsigned char *)&gdwCBWTag+2) = *RxBuffer ++;
*((unsigned char *)&gdwCBWTag+1) = *RxBuffer ++;
*((unsigned char *)&gdwCBWTag+0) = *RxBuffer ++;
*((unsigned char *)&gdwCBWDataTransferLength+3) = *RxBuffer ++; // LSB read first
*((unsigned char *)&gdwCBWDataTransferLength+2) = *RxBuffer ++;
*((unsigned char *)&gdwCBWDataTransferLength+1) = *RxBuffer ++;
*((unsigned char *)&gdwCBWDataTransferLength+0) = *RxBuffer ++;
gbCBWFlags = *RxBuffer ++;
gbCBWLUN = *RxBuffer ++;
gbCBWCBLength = *RxBuffer ++;
for(i = 0; i {
gbCBWCB[i] = *RxBuffer ++;
}
OnCommandBLock();
}
else
{
UsbStallEp2();
bUSBFlags.bits.bEP2_RxDone = 0;
printf("ep2s");
}
}
void OnCommandBLock()
{
switch( gbCBWCB[0] )
{
case OP_TEST_UNIT_READY:
bUSBFlags.bits.bEP2_RxDone = 0;
gbCSWStatus = PASS;
gbBulkPipeStage = S_CSW_PHASE;
USB_ENABLE_INTERRUPT;
break;
case OP_REQUEST_SENSE:
bUSBFlags.bits.bEP2_RxDone = 0;
OnRequestSense();
gbBulkPipeStage = S_DATA_PHASE;
USB_ENABLE_INTERRUPT;
break;
case OP_INQUIRY:
bUSBFlags.bits.bEP2_RxDone = 0;
OnInquiry();
gbBulkPipeStage = S_DATA_PHASE;
USB_ENABLE_INTERRUPT;
break;
case OP_MODE_SENSE:
bUSBFlags.bits.bEP2_RxDone = 0;
OnModeSense();
gbBulkPipeStage = S_DATA_PHASE;
USB_DISABLE_INTERRUPT;
break;
case OP_MODE_SENSE6:
bUSBFlags.bits.bEP2_RxDone = 0;
OnModeSense6();
gbBulkPipeStage = S_DATA_PHASE;
USB_DISABLE_INTERRUPT;
break;
case OP_READ_CAPACITY:
bUSBFlags.bits.bEP2_RxDone = 0;
DoReadCapacity();
gbBulkPipeStage = S_DATA_PHASE;
USB_DISABLE_INTERRUPT;
break;
case OP_READ_FORMAT_CAPACITIES:
bUSBFlags.bits.bEP2_RxDone = 0;
DoReadFormatCapacity();
gbBulkPipeStage = S_DATA_PHASE;
USB_DISABLE_INTERRUPT;
break;
case OP_READ10:
bUSBFlags.bits.bEP2_RxDone = 0;
DoRead();
gbCSWStatus = PASS;
gbBulkPipeStage = S_CSW_PHASE;
USB_DISABLE_INTERRUPT;
break;
case OP_WRITE10:
DoWrite();
gbCSWStatus = PASS;
gbBulkPipeStage = S_CSW_PHASE;
USB_ENABLE_INTERRUPT;
break;
case OP_MEDIA_REMOVEAL:
bUSBFlags.bits.bEP2_RxDone = 0;
gbCSWStatus = PASS;
gbBulkPipeStage = S_CSW_PHASE;
USB_DISABLE_INTERRUPT;
break;
case OP_VERIFY:
bUSBFlags.bits.bEP2_RxDone = 0;
gbCSWStatus = PASS;
gbBulkPipeStage = S_CSW_PHASE;
USB_DISABLE_INTERRUPT;
break;
default:
UsbStallEp2();
printf("2");
break;
}
}
void OnRequestSense()
{
unsigned char * TxBuffer;
TxBuffer = Ep2BufTx;
*TxBuffer ++ = 0x70;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x03;//sense key
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x0A;//additional Length
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x30;//ASC
*TxBuffer ++ = 0x01;//ASCQ
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
Ep2LenTx = 0x12;
}
void OnInquiry()
{
unsigned char * TxBuffer;
TxBuffer = Ep2BufTx;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x80;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x01;
*TxBuffer ++ = 0x1F;//additional Length
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 'S';//Vendor Information
*TxBuffer ++ = 'T';
*TxBuffer ++ = 'M';
*TxBuffer ++ = 'i';
*TxBuffer ++ = 'c';
*TxBuffer ++ = 'r';
*TxBuffer ++ = 'o';
*TxBuffer ++ = ' ';
*TxBuffer ++ = 'A';//Product Identification
*TxBuffer ++ = 'R';
*TxBuffer ++ = 'M';
*TxBuffer ++ = '7';
*TxBuffer ++ = ' ';
*TxBuffer ++ = 'D';
*TxBuffer ++ = 'K';
*TxBuffer ++ = 'B';
*TxBuffer ++ = 'o';
*TxBuffer ++ = 'a';
*TxBuffer ++ = 'r';
*TxBuffer ++ = 'd';
*TxBuffer ++ = ' ';
*TxBuffer ++ = ' ';
*TxBuffer ++ = 0x20;
*TxBuffer ++ = 0x20;
*TxBuffer ++ = 0x30;//Product Revision Level
*TxBuffer ++ = 0x20;
*TxBuffer ++ = 0x20;
*TxBuffer ++ = 0x20;
Ep2LenTx = 0x24;
}
void OnModeSense()
{
}
void OnModeSense6()
{
unsigned char * TxBuffer;
TxBuffer = Ep2BufTx;
*TxBuffer ++ = 0x03;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
Ep2LenTx = 0x04;
}
void DoReadCapacity()
{
unsigned char * TxBuffer;
unsigned char banknum;
unsigned long logicsize;
banknum = 1;
logicsize = ((unsigned long)banknum * 1024 * 32) - 1;
TxBuffer = Ep2BufTx;
*TxBuffer ++ = logicsize >> 24;//Last logical block
*TxBuffer ++ = logicsize >> 16;
*TxBuffer ++ = logicsize >> 8;
*TxBuffer ++ = logicsize & 0xff;
*TxBuffer ++ = 0x00;//block length
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x02;
*TxBuffer ++ = 0x00;
Ep2LenTx = 0x08;
}
void DoReadFormatCapacity()
{
unsigned char * TxBuffer;
TxBuffer = Ep2BufTx;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x08;//capasity list length
*TxBuffer ++ = 0x01;//number of blocks
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x03;//block length
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x02;
*TxBuffer ++ = 0x00;
Ep2LenTx = 0x0C;
}
void DoRead()
{
unsigned int j;
unsigned long LogicalBlockAddr = 0;
unsigned int SectorNumber = 0;
PageDataBuffer = buffer;
for(j=2; j {
LogicalBlockAddr += gbCBWCB[j];
LogicalBlockAddr }
LogicalBlockAddr += gbCBWCB[5];
SectorNumber = ((unsigned int)gbCBWCB[7]
if(gbCBWFlags & 0x80)//read command
{
if(!ReadSector(LogicalBlockAddr, SectorNumber, PageDataBuffer))
{}
else
{}
}
else
{
UsbStallEp2();
printf("3");
}
}
void DoWrite()
{
unsigned int j;
unsigned long LogicalBlockAddr = 0;
unsigned int SectorNumber = 0;
PageDataBuffer = Ep2BufRx;
for(j=2; j {
LogicalBlockAddr += gbCBWCB[j];
LogicalBlockAddr }
LogicalBlockAddr += gbCBWCB[5];
SectorNumber = ((unsigned int)gbCBWCB[7]
if(!(gbCBWFlags & 0x80))//read command
{
if(!WriteSector(LogicalBlockAddr, SectorNumber, PageDataBuffer))
{
}
else
{
}
}
}
unsigned char ReadSector(unsigned long LBA,unsigned int SNum,unsigned char *pBuffer)
{
unsigned int i,j;
for(j=0;j {
memcpy(pBuffer,((unsigned char *)(0x0c400000+LBA*512+(unsigned long)j*512)),512);
for(i=0;i {
gbBulkPipeStage = S_DATA_TX_PHASE;
UsbWriteEndpoint(5,pBuffer+i*64,64);
while(gbBulkPipeStage==S_DATA_TX_PHASE);
}
}
return 0;
}
unsigned char WriteSector(unsigned long LBA,unsigned int SNum,unsigned char *pBuffer)
{
unsigned int j,k;
for(k=0;k {
for(j=0;j {
USB_ENABLE_INTERRUPT;
while(bUSBFlags.bits.bEP2_RxDone==0);
USB_DISABLE_INTERRUPT;
printf("j=%x", j);
memcpy(((unsigned char *)(0x0c400000+LBA*512+ k*512 + j*64)),pBuffer,64*wf);
wf = 0;
bUSBFlags.bits.bEP2_RxDone = 0;
}
}
printf("...");
return 0;
}
void ReturnCSW()
{
unsigned char * TxBuffer;
TxBuffer = Ep2BufTx;
*TxBuffer ++ = 0x55; // dCSWSignature
*TxBuffer ++ = 0x53;
*TxBuffer ++ = 0x42;
*TxBuffer ++ = 0x53;
*TxBuffer ++ = *((unsigned char *)&gdwCBWTag+3); // dCSWTag, LSB read first
*TxBuffer ++ = *((unsigned char *)&gdwCBWTag+2);
*TxBuffer ++ = *((unsigned char *)&gdwCBWTag+1);
*TxBuffer ++ = *((unsigned char *)&gdwCBWTag+0);
*TxBuffer ++ = 0x00; // dCSWDataResidue
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = 0x00;
*TxBuffer ++ = gbCSWStatus; // bCSWStatus
Ep2LenTx = 0x0D;
}