⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hostrbc.c

📁 SmartARM2400系列开发板全套资料
💻 C
📖 第 1 页 / 共 3 页
字号:
** Descriptions:        启动或停止设备(LUN)
** input parameters:    ucInterfaceIndex : 接口号
**                      ucLunIndex       : LUN号
**                      ucStatus         : 状态,启动(RBC_MAKEREADY)或停止(RBC_STOPMEDIUM) LUN
** output parameters:   None
** Returned value:      错误代码,若返回MS_ERR_SUCESS, 说明执行成功, 否则执行失败
*********************************************************************************************************/
USB_INT8U rbcStartStopUnit (USB_INT16U ucInterfaceIndex, 
                            USB_INT8U  ucLunIndex, 
                            USB_INT8U  ucStatus)
{
    __BULK_ONLY_CBW cbwInq;
    USB_INT8U       ucErrCode;
    
    OS_ENTER_CRITICAL();
    if (__GbHostMsExitReq) {
        OS_EXIT_CRITICAL();
        return MS_ERR_EXIT_REQ;
    }
    __GucMsCmdCnt++;
    OS_EXIT_CRITICAL();

    usbMemSet(&cbwInq, 0 , sizeof(__BULK_ONLY_CBW));
 
    /*
     *  单批量传输的特征码:0x43425355(小端模式), 即:"USBC"(大端模式)
     */
    cbwInq.dCBWSignature0 = 0x55;                                       /*  'U'                         */
    cbwInq.dCBWSignature1 = 0x53;                                       /*  'S'                         */
    cbwInq.dCBWSignature2 = 0x42;                                       /*  'B'                         */
    cbwInq.dCBWSignature3 = 0x43;                                       /*  'C'                         */
    
    /*
     *  传输标志位,应为随机数,
     *  但为加快程序执行速度,此处固定为0x01,0x02,0x03,0x04,而不是产生随机数
     */
    cbwInq.dCBWTag0 = 0x01;
    cbwInq.dCBWTag1 = 0x02;
    cbwInq.dCBWTag2 = 0x03;
    cbwInq.dCBWTag3 = 0x04;
    
    /*  
     *  CBW 和 CSW 间要传输的数据量
     */
    cbwInq.dCBWDataTransferLength0 = 0;
    cbwInq.dCBWDataTransferLength1 = 0;
    cbwInq.dCBWDataTransferLength2 = 0;
    cbwInq.dCBWDataTransferLength3 = 0;
    
    cbwInq.bmCBWFlags   = 0x80;                                         /*  方向: BULK IN               */
    cbwInq.bCBWLUN      = (USB_INT8U)(ucLunIndex & 0x0F);
    cbwInq.bCBWCBLength = 6;
    
    cbwInq.CBWCB[0] = SCSI_START_STOP;                                  /*  操作码                      */
    cbwInq.CBWCB[4] = (USB_INT8U)(ucStatus & 0x03);
    
    ucErrCode = __boSendCommand (&cbwInq, NULL);
    if (ucErrCode == MS_ERR_CSW) {
        __boMassStorReset(ucInterfaceIndex);
        OS_ENTER_CRITICAL();
        __GucMsCmdCnt--;
        OS_EXIT_CRITICAL();
        return SCSI_ERR_CMDEXE;
    }
    
    OS_ENTER_CRITICAL();
    __GucMsCmdCnt--;
    OS_EXIT_CRITICAL();
    
    return ucErrCode;
}    
    
/*********************************************************************************************************
** Function name:       rbcRead10
** Descriptions:        读取数据
** input parameters:    usInterface : 接口号
**                      ucLunIndex  : LUN号
**                      uiLba       : 块地址
**                      uiTranLen   : 要读取的块数
** output parameters:   pucData     : 接收数据缓冲区
** Returned value:      错误代码,若返回MS_ERR_SUCESS, 说明执行成功, 否则执行失败
*********************************************************************************************************/
USB_INT8U rbcRead10 (USB_INT16U usInterface,
                     USB_INT8U  ucLunIndex,
                     USB_INT32U uiLba,                                  /*  LBA,即逻辑块地址            */
                     USB_INT32U uiTranLen,                              /*  要读取的数据长度(块数)      */
                     USB_INT8U *pucData)                                /*  接收数据缓冲区              */
{
    __BULK_ONLY_CBW  cbwInq;
    USB_INT8U        ucErrCode;
    
    USB_INT32U       uiCurLen;
    USB_INT32U       uiTotalLen;
    
    
    if ((pucData == NULL) || (uiTranLen == 0) || (ucLunIndex >= __msInfo.ucMaxLun) ||
        (uiLba + uiTranLen > __msInfo.msLunInfo[ucLunIndex].uiMaxLba)) {
        return MS_ERR_INVALID_PARAM;
    }
    
    OS_ENTER_CRITICAL();
    if (__GbHostMsExitReq) {
        OS_EXIT_CRITICAL();
        return MS_ERR_EXIT_REQ;
    }
    __GucMsCmdCnt++;
    OS_EXIT_CRITICAL();

    usbMemSet(&cbwInq, 0 , sizeof(__BULK_ONLY_CBW));
 
    /*
     *  单批量传输的特征码:0x43425355(小端模式), 即:"USBC"(大端模式)
     */
    cbwInq.dCBWSignature0 = 0x55;                                       /*  'U'                         */
    cbwInq.dCBWSignature1 = 0x53;                                       /*  'S'                         */
    cbwInq.dCBWSignature2 = 0x42;                                       /*  'B'                         */
    cbwInq.dCBWSignature3 = 0x43;                                       /*  'C'                         */
    
    /*
     *  传输标志位,应为随机数,
     *  但为加快程序执行速度,此处固定为0x01,0x02,0x03,0x04,而不是产生随机数
     */
    cbwInq.dCBWTag0 = 0x01;
    cbwInq.dCBWTag1 = 0x02;
    cbwInq.dCBWTag2 = 0x03;
    cbwInq.dCBWTag3 = 0x04;
    
    cbwInq.bmCBWFlags   = 0x80;                                         /*  方向: BULK IN               */
    cbwInq.bCBWLUN      = (USB_INT8U)(__msInfo.ucUsedLun[ucLunIndex] & 0x0F);
    cbwInq.bCBWCBLength = 10;
    
    cbwInq.CBWCB[0] = 0x28;                                             /*  操作码                      */
    cbwInq.CBWCB[1] = (USB_INT8U)((__msInfo.ucUsedLun[ucLunIndex] & 0x07) << 5);
    
    uiTotalLen = 0;
    do {
        if (__msInfo.msLunInfo[ucLunIndex].uiBytesPerBlock == 512) {
            if (((uiTranLen - uiTotalLen) * 512) > 512 * 1024) {
                uiCurLen = (512 * 1024) / 512;
            } else {
                uiCurLen = uiTranLen - uiTotalLen;
            }
        } else {
            if ((uiTranLen - uiTotalLen) * __msInfo.msLunInfo[ucLunIndex].uiBytesPerBlock 
                 > 512 * 1024) {
                uiCurLen = (512 * 1024) / __msInfo.msLunInfo[ucLunIndex].uiBytesPerBlock;
            } else {
                uiCurLen = uiTranLen - uiTotalLen;
            }
        }            
        
        /*  
         *  CBW 和 CSW 间要传输的数据量
         */
        if (__msInfo.msLunInfo[ucLunIndex].uiBytesPerBlock == 512) {
            cbwInq.dCBWDataTransferLength0 = __MSLSB(uiCurLen * 512, 0);
            cbwInq.dCBWDataTransferLength1 = __MSLSB(uiCurLen * 512, 1);
            cbwInq.dCBWDataTransferLength2 = __MSLSB(uiCurLen * 512, 2);
            cbwInq.dCBWDataTransferLength3 = __MSLSB(uiCurLen * 512, 3);
        } else {
            cbwInq.dCBWDataTransferLength0 = __MSLSB(uiCurLen * \
                                                     __msInfo.msLunInfo[ucLunIndex].uiBytesPerBlock, 0);
            cbwInq.dCBWDataTransferLength1 = __MSLSB(uiCurLen * \
                                                     __msInfo.msLunInfo[ucLunIndex].uiBytesPerBlock, 1);
            cbwInq.dCBWDataTransferLength2 = __MSLSB(uiCurLen * \
                                                     __msInfo.msLunInfo[ucLunIndex].uiBytesPerBlock, 2);
            cbwInq.dCBWDataTransferLength3 = __MSLSB(uiCurLen * \
                                                     __msInfo.msLunInfo[ucLunIndex].uiBytesPerBlock, 3);
        }   
        
        cbwInq.CBWCB[2] = __MSLSB(uiLba, 3);
        cbwInq.CBWCB[3] = __MSLSB(uiLba, 2);
        cbwInq.CBWCB[4] = __MSLSB(uiLba, 1);
        cbwInq.CBWCB[5] = __MSLSB(uiLba, 0);
        
        cbwInq.CBWCB[7] = __MSLSB(uiCurLen, 1);
        cbwInq.CBWCB[8] = __MSLSB(uiCurLen, 0);
        
        ucErrCode = __boSendCommand(&cbwInq, pucData);
        if (ucErrCode != MS_ERR_SUCESS) {
            if (ucErrCode == MS_ERR_CSW) {
                ucErrCode = rbcRequestSense (ucLunIndex);
                if ((ucErrCode != MS_ERR_SUCESS) && (ucErrCode != 0x06)) {
                    __boMassStorReset(usInterface);
                    break;
                }
            } else {
                __boMassStorReset(usInterface);
                break;
            }
        }
        uiTotalLen = (USB_INT32U)(uiTotalLen + uiCurLen);
        uiLba      = (USB_INT32U)(uiLba + uiCurLen);
        pucData    = pucData + uiCurLen * __msInfo.msLunInfo[ucLunIndex].uiBytesPerBlock;
    } while (uiTotalLen < uiTranLen);
    
    OS_ENTER_CRITICAL();
    __GucMsCmdCnt--;
    OS_EXIT_CRITICAL();
    
    return ucErrCode;
}

/*********************************************************************************************************
** Function name:       rbcWrite10
** Descriptions:        读取数据
** input parameters:    usInterface : 接口号
**                      ucLunIndex  : LUN号
**                      uiLba       : 块地址
**                      uiTranLen   : 要写的块数
** output parameters:   pucData     : 写数据缓冲区
** Returned value:      错误代码,若返回MS_ERR_SUCESS, 说明执行成功, 否则执行失败
*********************************************************************************************************/
USB_INT8U rbcWrite10 (USB_INT16U usInterface,
                      USB_INT8U  ucLunIndex,
                      USB_INT32U uiLba,                                 /*  LBA,即逻辑块地址            */
                      USB_INT32U uiTranLen,                             /*  要读取的数据长度            */
                      USB_INT8U *pucData)                               /*  接收数据缓冲区              */
{
    USB_INT8U       ucErrCode;
    __BULK_ONLY_CBW cbwInq;
    
    USB_INT32U      uiCurLen;
    USB_INT32U      uiTotalLen;

    
    if ((pucData == NULL) || (uiTranLen == 0) || (ucLunIndex >= __msInfo.ucMaxLun) ||
        (uiLba + uiTranLen > __msInfo.msLunInfo[ucLunIndex].uiMaxLba)) {
        return MS_ERR_INVALID_PARAM;
    }
    
    OS_ENTER_CRITICAL();
    if (__GbHostMsExitReq) {
        OS_EXIT_CRITICAL();
        return MS_ERR_EXIT_REQ;
    }
    __GucMsCmdCnt++;
    OS_EXIT_CRITICAL();

    usbMemSet(&cbwInq, 0 , sizeof(__BULK_ONLY_CBW));
 
    /*
     *  单批量传输的特征码:0x43425355(小端模式), 即:"USBC"(大端模式)
     */
    cbwInq.dCBWSignature0 = 0x55;                                       /*  'U'                         */
    cbwInq.dCBWSignature1 = 0x53;                                       /*  'S'                         */
    cbwInq.dCBWSignature2 = 0x42;                                       /*  'B'                         */
    cbwInq.dCBWSignature3 = 0x43;                                       /*  'C'                         */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -