?? ufi.c
字號:
/**************************************************************************
* H E A D E R F I L E S
*************************************************************************/
#include <reg51.h>
#include <intrins.h>
#include <stdio.h>
#include "include\AT89C51SND.h"
#include "include\typedef.h"
#include "include\config.h"
#include "include\constant.h"
#include "include\global.h"
#include "include\gpio.h"
#include "include\macro.h"
#include "include\external.h"
#include "include\usb.h"
/**************************************************************************
* C O N S T A N T S
*************************************************************************/
/**************************************************************************
* M A C R O S
*************************************************************************/
/**************************************************************************
* D A T A T Y P E S
*************************************************************************/
/**************************************************************************
* G L O B A L D A T A
*************************************************************************/
//answer for UFI inquery command
UINT8 code ATAInquiryData[] =
{
0x00,0x00,0x00,0x01,0x1F,0,0,0, //peripheral device type=0x00,RMB=0,ANSI=0,RESPONSE DATAFORMAT=1,Direct-access device
'w','e','i','c','h','i','n','g', //8 bytes for Vendor Information
'U','S','B',' ','D','E','V','I','C','E',' ',' ',' ',' ',' ',' ', //16 bytes for Product Information
' ',' ',' ',' ', //4 bytes for Product Revision
};
/**************************************************************************
* Function Name:
*
* Descriptions:
*
* Arguments:
*
* Returns: None
**************************************************************************/
void dmaCopyIn(UINT16 u16Offset, UINT8 *pu8Buf, UINT8 u8Size)
{
UINT8 i ;
u16Offset=0;
for(i=0; i<u8Size; i++) UEPDATX= *(pu8Buf++) ;
}
/**************************************************************************
* Function Name:
*
* Descriptions:
*
* Arguments:
*
* Returns: None
**************************************************************************/
UINT8 ufiInquiry(void)
{
UINT8 TransLength;
Usb_sel_ep(1);
if(gCBWDataLen>36)
TransLength=36;
else
TransLength=gCBWDataLen;
dmaCopyIn(0, ATAInquiryData,TransLength);
Usb_set_TXRDY_bit();
while (!(Usb_tx_complete_bit()));
Usb_clear_TXCMPL_bit();
gCBWDataLen-=TransLength;
return (0);
}
/**************************************************************************
* Function Name:
*
* Descriptions:
*
* Arguments:
*
* Returns: None
**************************************************************************/
UINT8 ufiRead10(void)
{
UINT8 Status ;
gbUsbReadError=0;//for Request Sense
Status=stReadSectors(gCmdBlock.CB1.LUN,gCmdBlock.CB1.LBA.ULONG, gCmdBlock.CB1.Length2.INTEGER) ;
if(Status)
{
if(gCBWDataLen!=0)//SEND NULL DATA IN BULK-IN ENDPOINT.
{
Usb_set_TXRDY_bit();
while (!(Usb_tx_complete_bit()));
Usb_clear_TXCMPL_bit();
}
gCBWDataLen=gOrgCBWDataLen;
gbUsbReadError=1;
}
return(Status);
}
/**************************************************************************
* Function Name:
*
* Descriptions:
*
* Arguments:
*
* Returns: None
**************************************************************************/
UINT8 ufiWrite10(void)
{
UINT8 Status;
gbUsbWriteError=0;
Status=stWriteSectors(gCmdBlock.CB1.LUN, gCmdBlock.CB1.LBA.ULONG, gCmdBlock.CB1.Length2.INTEGER) ;
if(Status)
{
if(gCBWDataLen!=0)//STALL ENDPOINT2
{
Usb_sel_ep(2);
Usb_set_STALLRQ();
}
gCBWDataLen=gOrgCBWDataLen;
gbUsbWriteError=1;
}
return(Status);
}
/**************************************************************************
* Function Name:
*
* Descriptions:
*
* Arguments:
*
* Returns: None
**************************************************************************/
UINT8 ufiRequestSense(void)
{
Usb_sel_ep(1);
UEPDATX=0x70;
UEPDATX=0x00;
UEPDATX=0x05;
UEPDATX=0x00;
UEPDATX=0x00;
UEPDATX=0x00;
UEPDATX=0x00;
UEPDATX=0x0a;
UEPDATX=0x00;
UEPDATX=0x00;
UEPDATX=0x00;
UEPDATX=0x00;
UEPDATX=0x24;
UEPDATX=0x00;
UEPDATX=0x00;
UEPDATX=0x00;
UEPDATX=0x00;
UEPDATX=0x00;
Usb_set_TXRDY_bit();
while (!(Usb_tx_complete_bit()));
Usb_clear_TXCMPL_bit();
gCBWDataLen=0;
return (0);
}
/**************************************************************************
* Function Name:
*
* Descriptions:
*
* Arguments:
*
* Returns: None
**************************************************************************/
UINT8 ufiReadCapacity(void)
{
LongByte BAddr,TotalSectors;
Usb_sel_ep(1);
TotalSectors.ULONG =stGetCapacity(gCmdBlock.CB1.LUN) ;
if(TotalSectors.ULONG==0)
BAddr.ULONG = 0;
else
BAddr.ULONG = TotalSectors.ULONG-1; //Last Logical Block Address
UEPDATX= BAddr.BYTE.B3 ; //(J)LBA range
UEPDATX= BAddr.BYTE.B2 ;
UEPDATX= BAddr.BYTE.B1 ;
UEPDATX= BAddr.BYTE.B0 ;
//Block Length In Bytes
UEPDATX= 0x00 ; //(J)Block length=512 bytes
UEPDATX= 0x00 ;
UEPDATX= 0x02 ;
UEPDATX= 0x00 ;
Usb_set_TXRDY_bit();
while (!(Usb_tx_complete_bit()));
Usb_clear_TXCMPL_bit();
gCBWDataLen=0;
if(TotalSectors.ULONG==0) return(1);
else return(0);
}
/**************************************************************************
* Function Name:
*
* Descriptions:
*
* Arguments:
*
* Returns: None
**************************************************************************/
UINT8 ufiReadFormatCapacities(void)
{
LongByte TotalSectors;
Usb_sel_ep(1);
TotalSectors.ULONG =stGetCapacity(gCmdBlock.CB1.LUN) ;
//*Capacity List
//Reserved
UEPDATX=0x00;
UEPDATX=0x00;
UEPDATX=0x00;
//Capacity List Length
UEPDATX= 16 ;
//*Current/Maximum Capacity descriptor
//Number of Blocks
UEPDATX= TotalSectors.BYTE.B3 ;
UEPDATX= TotalSectors.BYTE.B2 ;
UEPDATX= TotalSectors.BYTE.B1 ;
UEPDATX= TotalSectors.BYTE.B0 ;
//Descriptor Code 10b Formatted Media
UEPDATX= 0x02 ;
//Block Length
UEPDATX= 0x00 ;
UEPDATX= 0x02 ;
UEPDATX= 0x00 ;
//*Formattable Capacity Descriptor
//Number of Block
UEPDATX= TotalSectors.BYTE.B3 ;
UEPDATX= TotalSectors.BYTE.B2 ;
UEPDATX= TotalSectors.BYTE.B1 ;
UEPDATX= TotalSectors.BYTE.B0 ;
//Reserved
UEPDATX= 0x00 ;
//Block Length
UEPDATX= 0x00 ;
UEPDATX= 0x02 ;
UEPDATX= 0x00 ;
Usb_set_TXRDY_bit();
while (!(Usb_tx_complete_bit()));
Usb_clear_TXCMPL_bit();
gCBWDataLen-=20;
if(TotalSectors.ULONG==0)return(1);
else return(0);
}
/**************************************************************************
* Function Name:
*
* Descriptions:
*
* Arguments:
*
* Returns: None
**************************************************************************/
UINT8 ufiTestUnitReady(void)
{
gCBWDataLen=0;
return 0;
}
/**************************************************************************
* Function Name:
*
* Descriptions:
*
* Arguments:
*
* Returns: None
**************************************************************************/
UINT8 ufiModeSense6(void)
{
Usb_sel_ep(1);
Usb_set_TXRDY_bit();
while (!(Usb_tx_complete_bit()));
Usb_clear_TXCMPL_bit();
return 1;
}
/**************************************************************************
* Function Name:
*
* Descriptions:
*
* Arguments:
*
* Returns: None
**************************************************************************/
UINT8 ufiModeSelect(void)
{
Usb_sel_ep(1);
Usb_set_TXRDY_bit();
while (!(Usb_tx_complete_bit()));
Usb_clear_TXCMPL_bit();
return 1;
}
/**************************************************************************
* Function Name:
*
* Descriptions:
*
* Arguments:
*
* Returns: None
**************************************************************************/
UINT8 ufiModeSense10(void)
{
Usb_sel_ep(1);
Usb_set_TXRDY_bit();
while (!(Usb_tx_complete_bit()));
Usb_clear_TXCMPL_bit();
return 1;
}
/**************************************************************************
* Function Name:
*
* Descriptions:
*
* Arguments:
*
* Returns: None
**************************************************************************/
UINT8 UFI_NOTSUPPORT_CMD(void)
{
if(gCBWDataLen!=0)
{
if(gCBWFlags)//send NULL data on bulk in endpoint.
{
Usb_sel_ep(1);
Usb_set_TXRDY_bit();
while (!(Usb_tx_complete_bit()));
Usb_clear_TXCMPL_bit();
return 1;
}
else //STALL bulk-out endpoint.
{ Usb_sel_ep(2);
Usb_set_STALLRQ();
return 1;
}
}
else return 1;
}
/**************************************************************************
* Function Name:
*
* Descriptions:
*
* Arguments:
*
* Returns: None
**************************************************************************/
UINT8 ufiCommandDecoder(void)
{
//printf("UFICMD:%#X\n",gCmdBlock.CB1.OPcode);
switch (gCmdBlock.CB1.OPcode) {
case UFI_RequestSense : //0x03
return ufiRequestSense();
case UFI_Inquiry : //0x12
return ufiInquiry();
case UFI_ModeSense : //0x5a
return ufiModeSense10();
case UFI_ModeSense_ : //0x1a
return ufiModeSense6();
case UFI_ModeSelect : //0x55
case UFI_ModeSelect_ : //0x15
return ufiModeSelect();
case UFI_Read10 : //0X28
return ufiRead10();
case UFI_ReadCapacity : //0x25
return ufiReadCapacity();
case UFI_Write10 :
return ufiWrite10(); //0X2A
case UFI_TestUnitReady: //0x00
return ufiTestUnitReady();
case UFI_ReadFormatCapacities : //0x23
return ufiReadFormatCapacities();
case UFI_AllowMediaRemove : //0x1e
case UFI_Verify : //0x2f
return (0);
default:
return UFI_NOTSUPPORT_CMD();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -