📄 d12_int.c
字号:
/* ELEC254 Group32 USB SD Card Reader */
#include "D12_INT.h"
#include "MassStorage.h"
#include "MMC_SD.h"
CBW cbw;
CSW csw;
unsigned char DISK_INF[36]=
{
0x00,
0x00,
0x00, //02 or 00 depends on windows
0x01, //02 or 01 depends on windows
0x1F,
0x00,0x00,0x00,
'H','K','U','S','T',' ',' ',' ',
//Device Name, a ASCII string needed, length must be 8
'E','L','E','C','2','5','4',' ','G','R','O','U','P',' ','3','2',
//Driver Name a ASCII string needed,length must be 16
0x31,0x2E,0x30,0x31
};
//Disk capacity. The first four bytes is the max LBA address
//It will be reinitialized when SD card is ready
unsigned char DISK_CAPACITY[8]= //sd capacity
{
0x00,0x0F,0x1C,0xF0,
0x00,0x00,0x02,0x00
};
//SENSE
unsigned char SENSE[0x12]= //sense return
{
0x70, 0x00, 0x05, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00
};
//send CSW
void Send_CSW(U32 DataResidue,U8 status)
{
csw.dCSWSignature = 0x53425355; //csw signature
csw.dCSWTag=cbw.dCBWTag;
csw.dCSWDataResidue=DataResidue;
csw.bCSWStatus=status;
while(D12_Select_Endpoint(5));
D12_Write_Endpoint(5, (U8 *)&csw, 13);
}
//SCSI read
void read_10()
{
U32 Byte_Count; //total byte count
U32 LBA;
U8 j=0;
//Get LBA Address
LBA = 0;
LBA += cbw.CBWCB[2];
LBA <<= 8;
LBA += cbw.CBWCB[3];
LBA <<= 8;
LBA += cbw.CBWCB[4];
LBA <<= 8;
LBA += cbw.CBWCB[5];
//Byte Count
Byte_Count = 0;
Byte_Count+=cbw.CBWCB[7];
Byte_Count<<=8;
Byte_Count+=cbw.CBWCB[8];
Byte_Count<<=9;
//Receive Data
while(1)
{
if(MMC_SD_SendCommand(17, LBA<<9)!=0x00){return;}
SPI_CS_Assert();
while(SPI_WriteByte(0xff) != 0xfe);
LBA++;
for(j=0;j<8;j++)
{
D12_Write_Endpoint_SD(5);
Byte_Count-=64;
if(Byte_Count==0)
{
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_CS_Deassert();
return;
}
}
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_CS_Deassert();
}
}
//SCSI Write
void write_10()
{
U32 Byte_Count; //Write Byte Count
U32 LBA;
U8 j=0;
U8 buffer[512]; //write buffer
//LBA address
LBA = 0;
LBA += cbw.CBWCB[2];
LBA <<= 8;
LBA += cbw.CBWCB[3];
LBA <<= 8;
LBA += cbw.CBWCB[4];
LBA <<= 8;
LBA += cbw.CBWCB[5];
//total byte
Byte_Count = 0;
Byte_Count+=cbw.CBWCB[7];
Byte_Count<<=8;
Byte_Count+=cbw.CBWCB[8];
Byte_Count<<=9;
//write each while 1 sector
while(1)
{
D12_Read_Endpoint(4, &buffer[j*64], 64);
j++;
Byte_Count-=64;
if(j==8)
{
j=0;
MMC_SD_WriteSingleBlock(LBA++, buffer);
}
if(Byte_Count==0)return;
}
}
//Standard USB Request Pointer
U8 (*Stand_Device_Request[])(U8 *SetupPacket) =
{
Get_Status,
Clear_Feature,
Reserved,
Set_Feature,
Reserved,
Set_Address,
Get_Descriptor,
Reserved,
Get_Configuration,
Set_Configuration,
Get_Interface,
Set_Interface,
Reserved,
Reserved,
Reserved,
Reserved
};
void USB_Delay(U16 Time)
{
while(Time--)asm("nop");
}
//Init USB
U8 USB_Init()
{
USB_Delay(20000);
if(D12_Read_Chip_ID()!=0x1210 )
return 0;
D12_Set_DMA(MyD12DmaCfg);
if(D12_Get_DMA()!=MyD12DmaCfg)
return 0;
D12_Set_Mode(MyD12EpCfgOff, D12Pll24M);
USB_Delay(20000);
USB_Delay(20000);
D12_Set_Mode(MyD12EpCfgOn, D12Pll24M);
return 1;
}
//D12 interrupt process
void D12Ep0IntProc()
{
U8 SetupPacket[8];
U8 i = 0;
Clear_Remain_Descriptor_Flag();
if(D12_Read_Last_Transaction_Status(0)&0x20)
{
if(D12_Read_Endpoint(0, SetupPacket, 8)==8)
{
D12_Ack_Endpoint(0);
D12_Ack_Endpoint(1);
if(SetupPacket[0] == 0xa1 && SetupPacket[1] == 0xfe)
{
D12_Write_Endpoint(1,&i,1);
}
if(!Stand_Device_Request[SetupPacket[1]&0xf](SetupPacket)); //setup packet
return;
}
}
D12_Set_Endpoint_Status(0, D12EpStall);
D12_Set_Endpoint_Status(1, D12EpStall);
}
void D12Ep1IntProc()
{
U8 i;
i = D12_Read_Last_Transaction_Status(1);
if(Remain_Descriptor_Flag())
Send_Descriptor();
}
void D12Ep2IntProc()
{
D12_Set_Endpoint_Status(2, D12EpStall);
}
void D12Ep3IntProc()
{
D12_Set_Endpoint_Status(3, D12EpStall);
}
void D12Ep4IntProc()
{
D12_Read_Last_Transaction_Status(4);
D12_Read_Endpoint(4, (U8 *)&cbw, Endpoint2_Packet_Size);
if(cbw.dCBWSignature != 0x43425355)return; //cbw signature
if(cbw.bmCBWFlags&0x80) //CBW Flag
{
switch(cbw.CBWCB[0])
{
case Read_10: read_10();Send_CSW(0x00,SUCCESS);break;
case Inquiry: D12_Write_Endpoint(5,DISK_INF,36);Send_CSW(0x00,SUCCESS); break;
case Read_Capacity: D12_Write_Endpoint(5,DISK_CAPACITY,0x08);Send_CSW(0x00,SUCCESS);break;
case Read_Format_capacity: D12_Write_Endpoint(5,0x00,0x00);Send_CSW(cbw.dCBWDataTransgerLength,FAIL);break;
case Request_Sense: D12_Write_Endpoint(5,SENSE,0x12);Send_CSW(0x00,SUCCESS);break;
case 0x1a: D12_Write_Endpoint(5,0x00,0x00);Send_CSW(cbw.dCBWDataTransgerLength,FAIL);break;
default : D12_Write_Endpoint(5,0x00,0x00);Send_CSW(cbw.dCBWDataTransgerLength,FAIL);break;
}
}
else
{
switch(cbw.CBWCB[0])
{
case Write_10: write_10();Send_CSW(0x00,SUCCESS);break;
case Test_Unit_Ready: Send_CSW(0x00,SUCCESS);break;
case Verify: Send_CSW(0x00,SUCCESS);break;
default : Send_CSW(cbw.dCBWDataTransgerLength,FAIL);break;
}
}
}
void D12Ep5IntProc()
{
D12_Read_Last_Transaction_Status(5);
void D12BusRstProc()
{
D12_Clear_Buffer();
D12_Enable_Buffer();
D12_Set_Endpoint_Enable(1);
}
void D12SuspChgProc()
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -