目录
1.main 初始化函数
PIO_InitializeInterrupts(0);
/* Initialize PIO pins */
_ConfigurePIOs();
/* Initialize drivers */
_ConfigureDrivers();
_MemoriesInitialize(pSd);
/* BOT driver initialization */
MSDDriver_Initialize(&msdDriverDescriptors, luns, MAX_LUNS);
/* Start USB stack to authorize VBus monitoring */
VBus_Configure();
while (1)
{
/* Mass storage state machine */
if (USBD_GetState() < USBD_STATE_CONFIGURED) {}
else
{
MSDDriver_StateMachine();
if (msdRefresh)
{
msdRefresh = 0;
if (msdWriteTotal < 50 * 1000)
{
/* Flush Disk Media */
}
msdWriteTotal = 0;
}
}
}
1.2 MSDFunction.c
samv7\libraries\libusb\device\massstorage\MSDFunction.c
* State machine for the MSD driver
*/
void MSDFunction_StateMachine(void)
{
if (USBD_GetState() < USBD_STATE_CONFIGURED) {}
else
{
MSDD_StateMachine(&msdFunction);
}
}
1.3 MSDDStateMachine.c
atmel-samv7-master\samv7\libraries\libusb\device\massstorage\MSDDStateMachine.c
/**
* State machine for the MSD %device driver
* \param pMsdDriver Pointer to a MSDDriver instance
*/
void MSDD_StateMachine(MSDDriver *pMsdDriver)
{
MSDCommandState *commandState = &(pMsdDriver->commandState);
MSCbw *cbw = &(commandState->cbw);
MSCsw *csw = &(commandState->csw);
MSDTransfer *transfer = &(commandState->transfer);
unsigned char status;
Usbhs *pUdp = USBHS;
/* Identify current driver state */
switch (pMsdDriver->state)
{
/*---------------------- */
case MSDD_STATE_READ_CBW:
/*---------------------- */
case MSDD_STATE_WAIT_CBW:
/*------------------------- */
case MSDD_STATE_PROCESS_CBW:
* Pre-process command */
MSDD_PreProcessCommand(pMsdDriver);
/*---------------------- */
case MSDD_STATE_SEND_CSW:
/*---------------------- */
case MSDD_STATE_WAIT_CSW:
/*---------------------- */
case MSDD_STATE_WAIT_HALT:
/**
* Processes the latest command received by the %device.
* \param pMsdDriver Pointer to a MSDDriver instance
* \return 1 if the command has been completed, false otherwise.
*/
static unsigned char MSDD_ProcessCommand(MSDDriver *pMsdDriver)
{
unsigned char status;
MSDCommandState *commandState = &(pMsdDriver->commandState);
MSCbw *cbw = &(commandState->cbw);
MSCsw *csw = &(commandState->csw);
MSDLun *lun = &(pMsdDriver->luns[(unsigned char) cbw->bCBWLUN]);
unsigned char isCommandComplete = 1;
/* Check if LUN is valid */
if (cbw->bCBWLUN > pMsdDriver->maxLun)
{
TRACE_WARNING(
"MSDD_ProcessCommand: LUN %d not exist\n\r", cbw->bCBWLUN);
status = MSDD_STATUS_ERROR;
}
else
{
/* Process command */
if (pMsdDriver->maxLun > 0)
{
TRACE_INFO_WP("LUN%d ", cbw->bCBWLUN);
}
status = SBC_ProcessCommand(lun, commandState); //SCSI命令
}
1.5 文件:SBCMethods.c
atmel-samv7-master\samv7\libraries\libusb\device\massstorage\SBCMethods.c
//samv7\libraries\libusb\device\massstorage\SBCMethods.c
/**
* \brief Processes a SBC command by dispatching it to a subfunction.
* \param lun Pointer to the affected LUN
* \param commandState Pointer to the current command state
* \return Operation result code
*/
unsigned char SBC_ProcessCommand(MSDLun *lun,
MSDCommandState *commandState)
{
unsigned char result = MSDD_STATUS_INCOMPLETE;
SBCCommand *command = (SBCCommand *) commandState->cbw.pCommand;
/* Identify command */
switch (command->bOperationCode)
{
/*--------------- */
case SBC_READ_10:
/*---------------- */
case SBC_WRITE_10:
/*--------------------- */
case SBC_READ_CAPACITY_10:
case SBC_VERIFY_10:
/*--------------------- */
case SBC_INQUIRY:
/*--------------- */
case SBC_MODE_SENSE_6:
/*-------------------- */
case SBC_TEST_UNIT_READY:
/*----------------------- */
case SBC_REQUEST_SENSE:
/*--------------------- */
case SBC_PREVENT_ALLOW_MEDIUM_REMOVAL:
/*------------------------------------ */
/*------------------------------------ */
case SBC_READ_FORMAT_CAPACITIES:
/*------------------------------------ */
result = MSDD_STATUS_PARAMETER;
break;
/*------ */
default:
/*------ */
result = MSDD_STATUS_PARAMETER;
}
return result;
}