小弟用CH374做USB HOST用,在讀KINGSTONG的4G U盤 時(shí),程序跳不出來。不知為什么? 其它的U盤以調(diào)到可以件了。
對(duì)了,不知道參考代碼中用到的 /* 執(zhí)行基于BulkOnly協(xié)議的命令,該子程序比較簡化,并未處理各種錯(cuò)誤,實(shí)際應(yīng)用必須處理 */ 中的各種錯(cuò)誤有哪些,以及如何處理? 本人的BULKONLY代碼如下 不知有何問題? UINT8 BulkOnlyCmd(UINT8 *pDatBuf) { UINT8 bStatus; UINT32 wTotleLen; UINT8 bLen; UINT32 wTemp; gBOC.sCBW.wCBWSig[3] = 0x43; gBOC.sCBW.wCBWSig[2] = 0x42; gBOC.sCBW.wCBWSig[1] = 0x53; gBOC.sCBW.wCBWSig[0] = 0x55; gBOC.sCBW.wCBWTag[3] = 0x03; gBOC.sCBW.wCBWTag[2] = 0x74; gBOC.sCBW.wCBWTag[1] = 0x03; gBOC.sCBW.wCBWTag[0] = 0x74; gBOC.sCBW.bCBWLUN = 0; /* Write CBW command to host transfer buffer */ Write374Block(RAM_HOST_TRAN, USB_BO_CBW_SIZE, (UINT8 *)&gBOC.sCBW); Write374Byte(REG_USB_LENGTH, USB_BO_CBW_SIZE); SetBulkOut(); /* transfer start */
bStatus = HostTransactInter(); /* 向USB盤的OUT端點(diǎn)輸出CBW */ if(bStatus == USB_INT_DISCONNECT) return bStatus; /* 檢測到USB設(shè)備斷開事件,磁盤已經(jīng)斷開 */ if(bStatus != USB_INT_SUCCESS) /* 發(fā)送CBW失敗 */ { ResetErrorBOC(bStatus); Write374Block(RAM_HOST_TRAN, USB_BO_CBW_SIZE, (UINT8 *)&gBOC.sCBW); /* 向CH374主機(jī)端點(diǎn)的發(fā)送緩沖區(qū)寫入CBW */ Write374Byte(REG_USB_LENGTH, USB_BO_CBW_SIZE); SetBulkOut(); /* transfer start */ bStatus = HostTransactInter(); /* 向USB盤的OUT端點(diǎn)輸出CBW */ if(bStatus != USB_INT_SUCCESS) return (ResetErrorBOC(bStatus)); } // success, then start to transfer data gSaveDevEndpTog ^= BIT_HOST_TRAN_TOG;/* OUT端點(diǎn)的數(shù)據(jù)同步標(biāo)志翻轉(zhuǎn) */ SwapArraytoWord(&wTotleLen, gBOC.sCBW.wCBWDataLen); if(wTotleLen) /* need data to transfer *//* 有數(shù)據(jù)需要傳輸,此處使用雙緩沖區(qū)提高效率,但導(dǎo)致程序復(fù)雜 */ { if(gBOC.sCBW.bCBWFlag & 0x80) // receive {
//429 // Write374Byte( REG_USB_SETUP, M_SET_RAM_MODE_2RX( BIT_SETP_HOST_MODE | BIT_SETP_AUTO_SOF ) ); /* 全速USB主機(jī)模式,USB總線空閑,發(fā)SOF,雙緩沖區(qū) */ // SetBulkIn( ); /* 輸入 */ //429
// here not use double Receive RAM while(wTotleLen) { //429 SetBulkIn(); // start Bulk IN transaction bStatus = HostTransactInter(); /* 接收數(shù)據(jù) */ if(bStatus != USB_INT_SUCCESS) /* 操作失敗 */ { if(bStatus == USB_INT_RET_STALL) ClearEndpStall(gDiskBulkInEndp | 0x80); /* 清除端點(diǎn)錯(cuò)誤 */ break; }
else { // success gSaveDevEndpTog ^= BIT_HOST_RECV_TOG; bLen = Read374Byte(REG_USB_LENGTH); /* read received data length */ Read374Block(RAM_HOST_RECV, bLen, pDatBuf); pDatBuf += bLen; if(wTotleLen > bLen) wTotleLen -= bLen; else wTotleLen = 0; if(bLen < 64) break; /* short packet */ } } } else // transmit { while(wTotleLen) { bLen = (wTotleLen>=64)? 64 : (UINT8)wTotleLen; Write374Block(RAM_HOST_TRAN, bLen, pDatBuf); Write374Byte(REG_USB_LENGTH, bLen); SetBulkOut(); /* 輸出 */ wTotleLen -= bLen; pDatBuf += bLen; bStatus = HostTransactInter();/* 發(fā)送數(shù)據(jù) */ if(bStatus != USB_INT_SUCCESS)/* 操作失敗 */ { if(bStatus == USB_INT_RET_STALL) ClearEndpStall(gDiskBulkOutEndp); /* 清除端點(diǎn)錯(cuò)誤 */ break; } else { // success gSaveDevEndpTog ^= BIT_HOST_TRAN_TOG; /* OUT端點(diǎn)的數(shù)據(jù)同步標(biāo)志翻轉(zhuǎn) */
//429 // if(bLen < 64) break; /* short packet */ //429 } } } if(bStatus != USB_INT_SUCCESS) /* 數(shù)據(jù)傳輸失敗 */ { if(bStatus == USB_INT_RET_STALL) return bStatus; } } // start to read CSW SetBulkIn(); bStatus = HostTransactInter(); /* 從USB盤的IN端點(diǎn)輸入CSW */ if(bStatus != USB_INT_SUCCESS) return bStatus;/* 接收CSW失敗 */ gSaveDevEndpTog ^= BIT_HOST_RECV_TOG;/* IN端點(diǎn)的數(shù)據(jù)同步標(biāo)志翻轉(zhuǎn) */ bLen = Read374Byte(REG_USB_LENGTH); Read374Block(RAM_HOST_RECV, bLen, (UINT8 *) &gBOC.sCSW);
SwapArraytoWord(&wTemp, gBOC.sCSW.wCSWSig);
if(bLen != USB_BO_CSW_SIZE || wTemp != USB_BO_CSW_SIG ) return USB_INT_DISK_ERR; if(gBOC.sCSW.bCSWStatus == 0) return USB_INT_SUCCESS; else if(gBOC.sCSW.bCSWStatus >= 2) return (ResetErrorBOC(USB_INT_DISK_ERR)); else return USB_INT_DISK_ERR; /* 磁盤操作錯(cuò)誤 */ }