ARM专辑-152M-pdf.zip
源代码在线查看: tpbulk.c
//*************************************************************************
//
// P H I L I P S P R O P R I E T A R Y
//
// COPYRIGHT (c) 1999 BY PHILIPS SINGAPORE.
// -- ALL RIGHTS RESERVED --
//
// File Name: TPBulk.C
// Author: ZhenYu Zhang
// Created: Nov. 26, 1999
// Modified:
// Revision: 0.0
//
//*************************************************************************
//
// USB Mass Storage
// Class Spec 1.0 Oct. 1998
// Bulk Only Transport 1.0 Jun.21 1999
//
// Notes:
// 3. Share Mem between CBW & CSW to minimize Operations as well as RAM
// 2. CSW structure size is 13[0xd] bytes
// 1. bInterfaceProtocol for Bulk-Only Transport
// 0x50 = 'P'
//
//
//
#include "config.h" /* special function register declarations */
#undef GLOBAL_EXT
#include "SysCnfg.h"
#include "BasicTyp.h"
#include "usb100.h"
#include "common.h"
#include "Hal4Sys.h"
#include "Hal4d12.h"
#include "ATA.h"
#include "RBCCmd.h"
#include "RBC.h"
#include "HAL4ATA.h"
#include "MainLoop.h"
#include "Chap_9.h"
#include "TPBulk.h"
//*************************************************************************
// Public Data
//*************************************************************************
// bit Flags
STRUC_EXT BITFLAGS bFlags;
// MCU Timer bit flags
#define MCUBF_Timer bFlags.bits.timer
INT8_EXT Hal4Sys_ClockTicks;
// D12 bit flags
#define D12BF_SetupOverwritten bFlags.bits.setup_overwritten
#define D12BF_Configuration bFlags.bits.;configuration
//USB
// DefaultControlPipe Finite State Machine [One-Hot]
STRUC_EXT char_bit _DCPFSMstate;
#define DCPFSMstate _DCPFSMstate
#define DCPFSM_SetupProc _DCPFSMstate.bit_.b0
#define DCPFSM_DataIn _DCPFSMstate.bit_.b1
#define DCPFSM_DataOut _DCPFSMstate.bit_.b2
#define DCPFSM_COhandshake _DCPFSMstate.bit_.b3
#define DCPFSM_CIhandshake _DCPFSMstate.bit_.b4
#define DCPFSM_Stall _DCPFSMstate.bit_.b7
// DCP FSM
// SETUP Stage -> SETUP Proc -> DATA OUT Stage -> CONTROL OUT Handshake -> STATUS Stage -> IDLE
// SETUP Stage -> SETUP Proc -> CONTROL OUT Handshake -> STATUS Stage -> IDLE
// SETUP Stage -> SETUP Proc -> DATA IN Stage-> CONTROL IN Handshake ->STATUS Stage -> IDLE
//
INT16_EXT DCPXfer_wResidue;
INT8_EXT * DCPXfer_pData;
// USB Device Request
STRUC_EXT DEVICE_REQUEST DCPDeviceRequest;
INT8_EXT UsbReq_Recipient;
INT8_EXT UsbReq_Type;
INT8_EXT UsbReq_Request;
#define REQBF_DCPRequest_dir bFlags.bits.DCPRequst_Dir
#define REQBF_DCPRequest_EPdir bFlags.bits.DCPRequst_EPDir
#define REQBF_StallDCPRequest bFlags.bits.Stall_DCPRequest
// Bulk-Only TP Finite State Machine [One-Hot]
STRUC_EXT char_bit _BOTFSMstate;
#define BOTFSMstate _BOTFSMstate.char_
#define BOTFSM_IDLE _BOTFSMstate.bit_.b0
#define BOTFSM_CBWProc _BOTFSMstate.bit_.b1
#define BOTFSM_DataIn _BOTFSMstate.bit_.b2
#define BOTFSM_DataOut _BOTFSMstate.bit_.b3
#define BOTFSM_CSWProc _BOTFSMstate.bit_.b4
#define BOTFSM_CSW _BOTFSMstate.bit_.b5
#define BOTFSM_Stall _BOTFSMstate.bit_.b7
// BOT FSM
// IDLE Stage -> CBW -> CBW Proc -> DATA OUT Stage -> CSW Proc -> CSW Stage -> IDLE
// IDLE Stage -> CBW -> CBW Proc -> DATA IN Stage -> CSW Proc -> CSW Stage -> IDLE
// STALL Stage -> IDLE
//
INT16_EXT BOTXfer_wResidue;
INT8_EXT * BOTXfer_pData;
//INT8_EXT * BOTXfer_pData;
STRUC_EXT TPBLK_STRUC TPBulk_Block;
#define TPBulk_CBW TPBulk_Block.TPBulk_CommandBlock
#define RBC_CDB TPBulk_CBW.cdbRBC
#define RBC_LUN TPBulk_CBW.bCBW_LUN
#define TPBulk_CSW TPBulk_Block.TPBulk_CommandStatus
#define BOTBF_StallAtBulkOut bFlags.bits.BO_Stalled
#define BOTBF_StallAtBulkIn bFlags.bits.BI_Stalled
#define BOTXfer_Abort bFlags.bits.Abort_BOT
// XferSpace
STRUC_EXT char_bit _Xfer_Space;
#define Xfer_Space _Xfer_Space.char_
// DCPXfer space
#define DCPXfer_atMCUCODE _Xfer_Space.bit_.b0
#define DCPXfer_atMCURAM _Xfer_Space.bit_.b1
#define DCPXfer_atEEROM _Xfer_Space.bit_.b2
#define DCPXfer_atATA _Xfer_Space.bit_.b3
// Bulk Only Tranport Xfer Space
#define BOTXfer_atRAM _Xfer_Space.bit_.b4
#define BOTXfer_atATA _Xfer_Space.bit_.b5
#define BOTXfer_atROM _Xfer_Space.bit_.b6
//*************************************************************************
// Public temp var
//*************************************************************************
// Public temp var
STRUC_EXT FLEXI_INT32 tempvars4UsbReq;
STRUC_EXT char_bit _FlexByte;
#define FlexByte _FlexByte
#define FlexByte_b0 _FlexByte.bit_.b0
#define FlexByte_b1 _FlexByte.bit_.b1
#define FlexByte_b2 _FlexByte.bit_.b2
#define FlexByte_b3 _FlexByte.bit_.b3
#define FlexByte_b4 _FlexByte.bit_.b4
#define FlexByte_b5 _FlexByte.bit_.b5
#define FlexByte_b6 _FlexByte.bit_.b6
#define FlexByte_b7 _FlexByte.bit_.b7
STRUC_EXT HW_ATA_DEVICES_EXTENSION Hal4ATA_DevExt;
//*************************************************************************
// Private Data
//*************************************************************************
//*************************************************************************
// Class Request Subroutines
// Public Functions
//*************************************************************************
#define USB_CLASS_REQUEST_USBSTOR_RESET 0xFF
#define USB_CLASS_REQUEST_USBSTOR_GETMAXLUN 0xFE
//*************************************************************************
//Bulk-Only TP-Bulk Hanlder
//*************************************************************************
void TPBulk_CBWHandler( void )
{
//如果接收到的数据的长度与CBW长度一致,
//if( sizeof(CBW) == Hal4D12_ReadEndpoint(4,EP2_PACKET_SIZE,(PINT8)&(TPBulk_CBW)) )
if( 0x1f == Hal4D12_ReadEndpoint(4,EP2_PACKET_SIZE,(PINT8)&(TPBulk_CBW)) )
{
if(TPBulksup_IsCBWValid())
{
RBC_Handler(); //有效CBW,命令处理
return;
}
}
TPBulksup_ErrorHandler(CASECBW,0); //无效CBW,错误处理
TPBulk_CSWHandler();// Goto USBFSM4BOT_CSWPROC;
}
void
TPBulksup_ErrorHandler(INT8 HostDevCase,INT16 wByteCounterDevWillXfer)
{
TPBulk_CSW.dCSW_DataResidue = TPBulk_CBW.dCBW_DataXferLen - wByteCounterDevWillXfer;
switch(HostDevCase)
{
case CASEOK:
case CASE1: /* Hn=Dn*/
case CASE6: /* Hi=Di*/
TPBulk_CSW.bCSW_Status = CSW_GOOD;
break;
case CASE12: /* Ho=Do*/
TPBulk_CSW.bCSW_Status = CSW_GOOD;
break;
case CASE2: /* Hn case CASE3: /* Hn //BOTBF_StallAtBulkIn = 1; // may or may-not
TPBulk_CSW.bCSW_Status = CSW_PHASE_ERROR;
break;
case CASE4: /* Hi>Dn*/
case CASE5: /* Hi>Di*/
BOTBF_StallAtBulkIn = 1;
TPBulk_CSW.bCSW_Status = CSW_FAIL;//CSW_GOOD or CSW_FAIL
break;
case CASE7: /* Hi case CASE8: /* HiDo */
//BOTBF_StallAtBulkIn = 1; // may or may-not
TPBulk_CSW.bCSW_Status = CSW_PHASE_ERROR;
break;
case CASE9: /* Ho>Dn*/
case CASE11: /* Ho>Do*/
//BOTBF_StallAtBulkOut = 1; // may or may-not
TPBulk_CSW.bCSW_Status = CSW_FAIL;//CSW_GOOD or CSW_FAIL
break;
case CASE10: /* HoDi */
case CASE13: /* Ho //TBF_StallAtBulkIn = 1;// may or may-not
//TBF_StallAtBulkOut = 1;// may or may-not
TPBulk_CSW.bCSW_Status = CSW_PHASE_ERROR;
break;
case CASECBW: /* invalid CBW */
//BOTBF_StallAtBulkIn = 1; //
//BOTBF_StallAtBulkOut = 1;
TPBulk_CSW.bCSW_Status = CSW_PHASE_ERROR;//0x02
break;
case CASECMDFAIL:
BOTBF_StallAtBulkIn = 1;
TPBulk_CSW.bCSW_Status = CSW_FAIL;
break;
default:
break;
}
TPBulk_CSW.dCSW_Signature = CSW_SIGNATURE; //0x55534253 CSW后固有的特征码
}
//*************************************************************************
// 函数名:void TPBulk_CSWHandler( void )
// 输 入:void
// 输 出:void
// 描 述:命令状态
//*************************************************************************
void TPBulk_CSWHandler( void ) //CSW处于理
{
if(BOTBF_StallAtBulkIn)
{
Hal4D12_SetEndpointStatus(5,1); //Bulk-In设置端点5状态为停止
TPBulk_CSW.dCSW_DataResidue += BOTXfer_wResidue;
Xfer_Space &= BOTXFERSPACE_MASK;//0x0F
BOTXfer_atRAM = 1;
BOTXfer_pData = (PINT8) &TPBulk_CSW; // CSW数据指针
//BOTXfer_wResidue = sizeof(CSW); // CSW包的数据长度
BOTXfer_wResidue = 13; // CSW包的数据长度
BOTFSMstate = USBFSM4BOT_CSW; // 设置CSW包传输标志位
}
else if(BOTBF_StallAtBulkOut)
{
Hal4D12_SetEndpointStatus(4,1); //Bulk-Out设置端点4状态为停止
BOTFSMstate = USBFSM4BOT_STALL; // 设置端点4空闲标志位
}
else
{
TPBulk_CSW.dCSW_DataResidue += BOTXfer_wResidue;
Xfer_Space &= BOTXFERSPACE_MASK; // 清各传输地址标志位0x0F
BOTXfer_atRAM = 1; // 从RAM传输数据
BOTXfer_pData = (PINT8) &TPBulk_CSW; // 设置CSW包数据指针
//BOTXfer_wResidue = sizeof(CSW); // CSW包数据长度
BOTXfer_wResidue = 13; // CSW包数据长度
BOTFSMstate = USBFSM4BOT_CSW; // 设置CSW包传输标志位
}
}
//*************************************************************************
//Bulk-Only TP - Class Request Handler
//*************************************************************************
void TPBulk_ResetATA(void)
{
Hal4ATA_IdeHardReset(); //IDE硬件复位
Chap9sup_SingleTransmitEP0(0,0);
}
void TPBulk_GetMaxLUN(void) //
{
INT8 c = 0;
Chap9sup_SingleTransmitEP0(&c,1);
}
//*************************************************************************
//Bulk Only Transport support functions
//*************************************************************************
//*************************************************************************
// 函数名:
// 输入:Length发送数据的长度
// 输出:Length已发送数据的长度
// 描述:批量读取数据
//*************************************************************************
INT8 TPBulksup_ReadFrBOEP(INT8 Length)
{
if(Length>EP2_PACKET_SIZE)
Length = EP2_PACKET_SIZE;
if( BOTXfer_atROM )
Length = Hal4D12_ReadEPAtCode(4,Length); //读取数据到ROM(实际丢弃)
else if(BOTXfer_atRAM)
Length = Hal4D12_ReadEndpoint(4,Length,BOTXfer_pData);
return Length; //返回已读数据长度
}
//*************************************************************************
// 函数名:
// 输入:Length发送数据的长度
// 输出:Length已发送数据的长度
// 描述: 批量发送数据
//*************************************************************************
INT8 TPBulksup_WriteToBIEP(INT8 Length)
{
if(Length > EP2_PACKET_SIZE)
Length = EP2_PACKET_SIZE;
if( BOTXfer_atROM )
Length = Hal4D12_WriteEPAtCode(5,Length,BOTXfer_pData);
else if(BOTXfer_atRAM)
Length = Hal4D12_WriteEndpoint(5,Length,BOTXfer_pData );
return Length; //返回已写数据长度
}
//*************************************************************************
// 函数名:uint8 TPBulksup_IsCBWValid( void)
// 输入:
// 输出:
// 描述: 效检CBW是否有效
//*************************************************************************
uint8 TPBulksup_IsCBWValid( void)
{
if( TPBulk_CBW.dCBW_Signature == CBW_SIGNATURE \
&& TPBulk_CBW.bCBW_LUN && TPBulk_CBW.bCBW_CDBLen return(1); //有效返回1
else
return(0); //无效返回0
}