same7 device_examples\usb_massstorage 示例代码

目录

初始化入口函数

MSDFunction.c

1.main 初始化函数

1.2 MSDFunction.c

1.3 MSDDStateMachine.c

1.5 文件:SBCMethods.c


 


1.main 初始化函数

MSDAppArch

MSDDriverArch

MSDDriverClasses

     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

MSDDriverStates
标题
/**
 * 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;
}
MSDDriverStates
标题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值