我用CH375可以往U盤(pán)里寫(xiě)數(shù)據(jù),可就是讀不出上面指定的文件 請(qǐng)高手指點(diǎn),全部代碼:]CH375FAT.rar 程序的部分代碼如下: ////////////////////////// main.c ////////////////////////////////// #include "common.h" #include
unsigned char xdata DBUF[BUFFER_LENGTH]; XXGFLAGS bdata bXXGFlags;
extern unsigned char xdata CHBUF[CHBUF_LENGTH]; extern SYS_INFO_BLOCK xdata DeviceInfo; extern CH_CMD_BLOCK xdata CHCmdBlock; extern CH_RSP_BLOCK xdata CHRspBlock; extern FILE_INFO xdata ThisFile; extern FREE_FAT_INFO xdata FreeFat; code unsigned char cmd_test_createfile[]= { 0x54,0x45,0x53,0x54,0x20,0x20,0x20,0x20, 0x54,0x58,0x54,0x20,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x21,0x30,0x00,0x00,0x20,0x20,0x20,0x20, 0x00,0x00,0x20,0x20,0x20,0x20,0x20,0x20, 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00 }; code unsigned char cmd_test_writefile[]= { 0x09, 0x00, 0x00,0x70, 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00 }; /* 將程序空間的字符串復(fù)制到內(nèi)部RAM中,返回字符串長(zhǎng)度 */ void mCopyCodeStringToIRAM( unsigned char *iDestination, unsigned char code *iSource, unsigned char len) { //unsigned char i = 0; while ( len ) { *iDestination = *iSource; iDestination ++; iSource ++; len --; } // return( i ); }
void main(void) { unsigned char temp; unsigned char mIntStatus,retStatus; unsigned int len,tempDataLength;
// printf( "star \n" ); mCH375Init( ); //mInitSTDIO( ); // printf( " wait usb connect \n" ); DelayMs(50); for(temp=0;temp<64;temp++)DBUF[temp]=0; mIntStatus = mWaitInterrupt( ); /* 等待中斷并獲取狀態(tài) */ if( mIntStatus != USB_INT_CONNECT ); /* U盤(pán)連接 */
retStatus=EnumMassDev(); retStatus=GetCapacity();
strcpy( CHCmdBlock.CmdBlock.Cmd_OpenFile.filename, "/temp.txt" ); /* 文件名,該文件在U盤(pán)根目錄下 */
retStatus=OpenFile(CHCmdBlock.CmdBlock.Cmd_OpenFile.filename); len=ThisFile.LengthInByte; tempDataLength=ThisFile.LengthInByte; retStatus=ReadFile(len,CHBUF); // strcpy( CHCmdBlock.CmdBlock.Cmd_CreateFile.item, cmd_test_createfile );
mCopyCodeStringToIRAM( CHCmdBlock.CmdBlock.Cmd_CreateFile.item, cmd_test_createfile ,32);
retStatus=CreateFile(CHCmdBlock.CmdBlock.Cmd_CreateFile.item);
// len=tempDataLength; retStatus=WriteFile(len,CHBUF);
while(1);
}
////////////////////////部分相關(guān)的函數(shù)//////////////////////////////
unsigned long SwapINT32(unsigned long dData) { dData = (dData&0xff)<<24|(dData&0xff00)<<8|(dData&0xff000000)>>24|(dData&0xff0000)>>8; return dData; }
unsigned int SwapINT16(unsigned int dData) { dData = (dData&0xff00)>>8|(dData&0x00ff)<<8; return dData; }
/* 從U盤(pán)中讀取多個(gè)扇區(qū)的數(shù)據(jù)塊到緩沖區(qū)中 */ unsigned char RBC_Read(unsigned long iLbaStart, unsigned char iSectorCount,unsigned char *mBufferPoint) /* iLbaStart 是讀取的線起始性扇區(qū)號(hào), iSectorCount 是讀取的扇區(qū)數(shù) */ { unsigned char mIntStatus; unsigned int mBlockCount; unsigned char mLength; CH375_CMD_PORT_d_out(CMD_DISK_READ); /* 從USB存儲(chǔ)器讀數(shù)據(jù)塊 */ CH375_DAT_PORT_d_out((unsigned char)iLbaStart); /* LBA的最低8位 */ CH375_DAT_PORT_d_out ((unsigned char)( iLbaStart >> 8 )); CH375_DAT_PORT_d_out((unsigned char)( iLbaStart >> 16 )); CH375_DAT_PORT_d_out((unsigned char)( iLbaStart >> 24 )); /* LBA的最高8位 */ CH375_DAT_PORT_d_out( iSectorCount); /* 扇區(qū)數(shù) */ // mBufferPoint = &DATA_BUFFER; /* 指向緩沖區(qū)起始地址 */ for ( mBlockCount = iSectorCount * CH375_BLK_PER_SEC; mBlockCount != 0; mBlockCount -- ) { /* 數(shù)據(jù)塊計(jì)數(shù) */ mIntStatus = mWaitInterrupt( ); /* 等待中斷并獲取狀態(tài) */ if ( mIntStatus == USB_INT_DISK_READ ) { /* USB存儲(chǔ)器讀數(shù)據(jù)塊,請(qǐng)求數(shù)據(jù)讀出 */ CH375_CMD_PORT_d_out(CMD_RD_USB_DATA); /* 從CH375緩沖區(qū)讀取數(shù)據(jù)塊 */ mLength = CH375_DAT_PORT_in(); /* 后續(xù)數(shù)據(jù)的長(zhǎng)度 */ /* 通常數(shù)據(jù)長(zhǎng)度是64,有些U盤(pán)要求單片機(jī)必須在2mS之內(nèi)取走64字節(jié)數(shù)據(jù),否則U盤(pán)可能數(shù)據(jù)丟失 */ /* 建議優(yōu)化下面的循環(huán)程序,確保單片機(jī)在1mS之內(nèi)完成64字節(jié)的數(shù)據(jù)傳輸 */ if ( mLength ) { /* 根據(jù)長(zhǎng)度讀取數(shù)據(jù) */ do { // 對(duì)于C51,這個(gè)DO+WHILE結(jié)構(gòu)效率高,速度快 *mBufferPoint = CH375_DAT_PORT_in(); /* 讀出數(shù)據(jù)并保存 */ mBufferPoint ++; } while ( -- mLength ); } CH375_CMD_PORT_d_out( CMD_DISK_RD_GO); /* 繼續(xù)執(zhí)行USB存儲(chǔ)器的讀操作 */ } else break; /* 返回錯(cuò)誤狀態(tài) */ } if ( mBlockCount == 0 ) mIntStatus = mWaitInterrupt( ); /* 等待中斷并獲取狀態(tài) */ if ( mIntStatus == USB_INT_SUCCESS ) return 1; /* 操作成功 */ else return 0; /* 操作失敗 */ }
unsigned char EnumMassDev(void) { PMBR_BLOCK pMBR; PBPB_BLOCK pBPB; if(mInitDisk( )!=USB_INT_SUCCESS)return 0 ; /* 初始化USB存儲(chǔ)器 */ pMBR=(PMBR_BLOCK)DBUF; DeviceInfo.BPB_BytesPerSec=512; //暫假設(shè)為512 //if(!SPC_RequestSense()) // return FALSE; if(!SPC_TestUnit()) return FALSE; if(!SPC_LockMedia()) return FALSE; if(!ReadCapacity()) return FALSE; if(!RBC_Read(0x0,1,DBUF)) return FALSE; ////////////////////////////////// if(DBUF[0]==0xeb||DBUF[0]==0xe9){ //判斷是否是BS_jmpBoot DeviceInfo.StartSector=0; //DeviceInfo.TotalSector=SwapINT32(pMBR->TotalSector); } else { ////////////////////////////////// DeviceInfo.StartSector=SwapINT32(pMBR->StartSector); //DeviceInfo.TotalSector=SwapINT32(pMBR->TotalSector); } /////////////////////////////////////////////////////// pBPB=(PBPB_BLOCK)DBUF; if(!RBC_Read(DeviceInfo.StartSector,1,DBUF)) return FALSE; DeviceInfo.BPB_BytesPerSec=SwapINT16(pBPB->BPB_BytesPerSec); DeviceInfo.BPB_SecPerClus=pBPB->BPB_SecPerClus; DeviceInfo.BPB_NumFATs=pBPB->BPB_NumFATs; DeviceInfo.BPB_RootEntCnt=SwapINT16(pBPB->BPB_RootEntCnt); DeviceInfo.BPB_TotSec16=SwapINT16(pBPB->BPB_TotSec16); DeviceInfo.BPB_FATSz16=SwapINT16(pBPB->BPB_FATSz16); DeviceInfo.BPB_TotSec32=SwapINT32(pBPB->BPB_TotSec32); DeviceInfo.FatStartSector=DeviceInfo.StartSector+pBPB->BPB_RsvdSecCn[0]; DeviceInfo.RootStartSector=DeviceInfo.StartSector+2*DeviceInfo.BPB_FATSz16+pBPB->BPB_RsvdSecCn[0];
DeviceInfo.FirstDataSector=DeviceInfo.FatStartSector+2*DeviceInfo.BPB_FATSz16+32; /////////////////////////////////////////////////////// ThisFile.bFileOpen=0; bXXGFlags.bits.SLAVE_IS_ATTACHED=1; /////////////////////////////////////////////////////// return TRUE; }
unsigned char OpenFile(unsigned char *pBuffer) { #define RspBlockOpenFile CHRspBlock.RspBlock.Rsp_OpenFile unsigned int i; unsigned char j,bstop,sector; PDIR_INFO pDirInfo;
if(!bXXGFlags.bits.SLAVE_IS_ATTACHED) { CHRspBlock.errcode=ERC_NODEVICE; return FALSE; } ///////////////////////////////////////////////////////////
ThisFile.bFileOpen=0; //RspBlockOpenFile.result=0x0; //RspBlockOpenFile.errcode=ERC_OK; for(sector=0;sector { ////////////////////////////////////////////////// if(!RBC_Read(DeviceInfo.RootStartSector+sector,1,DBUF)) { CHRspBlock.errcode=ERC_DEVICEERR; return FALSE;
} /////////////////////////////////////////////////// for(i=0;i { if(DBUF[i]==0x00) { CHRspBlock.errcode=ERC_FILENOTFOUND; //CHRspBlock.errcode=ERC_DEVICEERR; return FALSE; } /////////////////////////////////////////// j=0; while(DBUF[i+j]==*(pBuffer+j)) { j=j+1; if(j>10) break; }
if(j>10) { for(j=0;j<32;j++) RspBlockOpenFile.item[j]=DBUF[i+j]; //RspBlockOpenFile.result=0x1; ThisFile.bFileOpen=1; bsto