我用CH375可以往U盘里写数据,可就是读不出上面指定的文件 请高手指点,全部代码:]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 }; /* 将程序空间的字符串复制到内部RAM中,返回字符串长度 */ 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( ); /* 等待中断并获取状态 */ if( mIntStatus != USB_INT_CONNECT ); /* U盘连接 */
retStatus=EnumMassDev(); retStatus=GetCapacity();
strcpy( CHCmdBlock.CmdBlock.Cmd_OpenFile.filename, "/temp.txt" ); /* 文件名,该文件在U盘根目录下 */
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);
}
////////////////////////部分相关的函数//////////////////////////////
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盘中读取多个扇区的数据块到缓冲区中 */ unsigned char RBC_Read(unsigned long iLbaStart, unsigned char iSectorCount,unsigned char *mBufferPoint) /* iLbaStart 是读取的线起始性扇区号, iSectorCount 是读取的扇区数 */ { unsigned char mIntStatus; unsigned int mBlockCount; unsigned char mLength; CH375_CMD_PORT_d_out(CMD_DISK_READ); /* 从USB存储器读数据块 */ 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); /* 扇区数 */ // mBufferPoint = &DATA_BUFFER; /* 指向缓冲区起始地址 */ for ( mBlockCount = iSectorCount * CH375_BLK_PER_SEC; mBlockCount != 0; mBlockCount -- ) { /* 数据块计数 */ mIntStatus = mWaitInterrupt( ); /* 等待中断并获取状态 */ if ( mIntStatus == USB_INT_DISK_READ ) { /* USB存储器读数据块,请求数据读出 */ CH375_CMD_PORT_d_out(CMD_RD_USB_DATA); /* 从CH375缓冲区读取数据块 */ mLength = CH375_DAT_PORT_in(); /* 后续数据的长度 */ /* 通常数据长度是64,有些U盘要求单片机必须在2mS之内取走64字节数据,否则U盘可能数据丢失 */ /* 建议优化下面的循环程序,确保单片机在1mS之内完成64字节的数据传输 */ if ( mLength ) { /* 根据长度读取数据 */ do { // 对于C51,这个DO+WHILE结构效率高,速度快 *mBufferPoint = CH375_DAT_PORT_in(); /* 读出数据并保存 */ mBufferPoint ++; } while ( -- mLength ); } CH375_CMD_PORT_d_out( CMD_DISK_RD_GO); /* 继续执行USB存储器的读操作 */ } else break; /* 返回错误状态 */ } if ( mBlockCount == 0 ) mIntStatus = mWaitInterrupt( ); /* 等待中断并获取状态 */ 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存储器 */ pMBR=(PMBR_BLOCK)DBUF; DeviceInfo.BPB_BytesPerSec=512; //暂假设为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