ch375读U盘

ch375读U盘
ch375读U盘

特别说明

此资料来自百度文库(https://www.360docs.net/doc/0116401968.html,/)您目前所

看到的文档是使用的抱米花百度文库下载器所生成此文档

原地址来自

https://www.360docs.net/doc/0116401968.html,/view/92509af90242a8956bece47b.html 感谢您的支持

抱米花

https://www.360docs.net/doc/0116401968.html,/lotusbaob

51+ch375UflqMfl()

E?2005-10-16 19:50:00

7*S±*M$ 180ffC flSWflFAT 16tffSUfl cW

flif i ,1ritunwffiTmn JS+*MW1W1NDOWStFAT 164t4-I flJU, YSJ*M*IM31 LAFflikilit5O% ±ftiU CH375AfrtjiiUfl?MEW185%,O*1tTh WCH J tIEtft&*fl

flTUflIt:flfl64M/flfl128MfU16O-64MJt& 128M,fl/i

fl16M/fl,

I! f/64M,1&N/64M, MI32M/64MI128M, Z/C200-64M$?*4/256M,:1±7128M..., 503W

fl4ttM*

fl128M,?]Rt3!USB2.0-1 28M, ,4N1W CH flW)CH375A *1

4 i nclude

4 i nclude “CH375INC.H”

#include /TtfflTMcs51Thfl*

#define UNT8 unsigned that

#define Ull?TT 16 unsigned short

#define UUTT32 unsigned long

#define UII?TT8X unsigned char xdata

#define Ull?JT8VX unsigned char volatile xdata

UINT8VX CH375 CMD PORT atOxBDFl;1* CH375*tFE1flIJO*thtlE *7

U1NT8VX CH375 DAT PORT at OxBCFO; I CR375 EL4fl II flJIJOtthhE *7

#define CR375 INT WIRE INTO 1* P3.2, ICH375flNT#IPj1W*i?ftJ+ *1

UINT8X DISK BUFFER[5 12*32] at Ox0000; 1*9RAM11hiIE*1

UINT32 DiskStart; 7*! tMLBA *1

U1NT8 SecPerClus; 7*

UII?1T8RsvdSecCnt; 1*Ic

U1NT 16FATSz16; /* FAT 16i JFAT Jffi I%*7

7*

!#USBfl*7 **********

void niDelaymS( UINT8 delay) {

U]ENT8i,j,c;

for ( i = delay, i != 0; i --)

{ for(j=200;j != 0;j--)c+= 3;

for(j=200;j!=0;j--)c+=3;

void CH375 WRCMDPORT(UINT8 cmd) {1* CH375IJ44flA*4

CH37SCMDPORT=cmd;

for(cmd =2; cmd =0; cmd )7*W*4i-flt2uS */

void CH375 WRDATPORT( UINT8 dat) {1* IiJ CR375 flJ3ttM* FT A

CH375 DAT PORT=dat; 7* MCS51 tJLQWffi thflKt

UINT8 CR375 RD DAT PORT( void) {/*bkCR375flthflfl

return(CH375DATPORT);7* MCS51 IL JZflHt*/ UINT8 mWaitlnterrupt( void) { 7*CR375#k

while( CR375 TNT WIRE); 7**/

CR375 WR CMD PORT(CMD GET STATUS); 7*11JW*1 return( CH375 RD DAT PORT(j);

BullcOn1y4flitJ43L,41CR375T,t*1L*M*7

7*

**********

RBC/SCSIfl,.flCR375 J I

7*

**********

/

UINT8 nilnitDisk( void) {7* 4D4r1tfl*7

UINT8 Status;

CR375_WR_CMD_PORT( CMD_GET_STATUS); 7* JZtI$,*7 Status = CR3 75 RD DAT PORTO;

if ( Status = USB INT DISCONNECT ) retum( Status); /* USB1J19T*7

CR375 WRCMDPORT( CMD DISK INIT); 1* ft{IUSBM*7

Status = mWaitlnterruptQ; 7*rj *7

if ( Status != USB TNT SUCCESS) retum( Status); 7**1

CR375 WRCMDPORT( CMD DISK SIZE); 7*USB‘fljfl*7

Status = mWaitlnterruptQ; 7*fliJ*7

if( Status != USB TNT SUCCESS) {1* tI?*7

7* tTCR375fl,*J?Mt—CMDDISKRSENSE*4*7

rnDelaymS( 250);

CR375 WRCMDPORT( CMD DISK SIZE); 7*NZUSB#MJWt*7

Status = mWaitlnterruptQ; 7*rfi *7

if ( Status != USB TNT SUCCESS ) retum( Status); 7*t±iEQ4’i*7

return( 0); 1* UDM1*1

UINT8 mReadSector( UINT32 iLbaStart, UINT8 iSectorCount, UINT8X *oDataBuffer)

U1NT 16mBlockCount;

U1NT8 c;

CH375WRCMDPORT( CMD DISK READ); 7*AUSBiflfl

CH375WRDATPORT( (UINT8)iLbaStart); 1* LBA{8ttcL*7

CH375WRDATPORT( (UINT8)( iLbaStart >> 8));

CH375WRDATPORT( (UINT8)( iLbaStart>> 16));

CH375WRDATPORT((UINT8)( iLbaStart >> 24));/* LBAflJ18tL *7

CH375WRDATPORT( iSectorCount); i

8; mBlockCount != 0; mBlockCount --) {

for (mBlockCount = iSectorCount

*

c = mWaitlnterruptQ; 1*

if(c =USB TNT DISK READ) {7* flflj*7

CH375WRCMDPORT( CMDRDUSBDATA); 1*),kCH375flktV**1

c = CH375RDDATPORTQ;7*tES*1

while (c --) *oDataBufferf_f = CH375RDDATPORT();

CH375WRCMDPORT( CMD DISK RD 00);1*ffUSB4Jfl4?1E*7 else break; /* J14W1k

if ( mBlockCount = 0) {

c=mWaitlnterrupt();1*

if( c== USB TNT SUCCESS ) retum( 0); 1*1’MZJ*1

return( c); 7* 4’J*7

7*

**********

A*1? *7

UINT16 mGetPointWord(UTNT8X *iAddr) { 1*MCS51ticM445Et*7 retum( iAddr[0] I(UINT16)iAddr[1] <<8);

UINT8 mldenflisk( void) {7* *1

UINT8 Status;

DiskStart =0; /

Status = niReadSector( 0, 1, DISK_BUFFER); /* I1ft/

if( Status != 0) retum( Status);

if( DISK BUFFER[O] != OxEB && DISK BUFFER[O] != OxE9) {1*

DiskStart = DISK BUFFER[OxlC6] (UINT16)DISKBUFFER[OxlC7] <<8 (UINT32)DISKBUFFER[OxlC8] <<16 I(UTNT32)DISK BUFFER[OxlC9] <<24;

Status = mReadSector( DiskStart, 1, DISK_BUFFER);

if ( Status != 0 ) return( Status);

SecPerClus = DISK BUFFER[OxOD]; 1**1

RsvdSecCnt = DISK BUFFER[OxOE]; 1**1

FATSz16 = mGetPointWord( &DISKBUFFER[0x161);1* FATIJU IZ*1 return( 0); /*Mtb

UINT16 mLinkCluster( U1NT16 iCluster) {1* *1

!iA: iCluster J:Jtfl n*o *1

UINT8 Status;

Status = mReadSector( DiskStart + RsvdSecCnt + iCluster I 256, 1,

DISK_BUFFER);

if( Status != 0) return( 0); /**/

return( mGetPointWord( &DISK BUFFER[ ( iCluster + iCluster) & OxO1FF]));

UINT32 mClusterToLba( U1NT16 iCluster) {1*I#$t4LBAIJthE

retum( DiskStart + RsvdSecCnt + FATSz16

* 2 + 32 + (iCluster - 2)

*

SecPerClus);

void mInitSTDIO( void) {1* flPCt,-bflt5E

SCON = 0x50; PCON = 0x80; TMOD = 0x20; TH1 = Oxf3; TR1=1; TI=1; !24MHz, 9600bps */

void mStoplfError( UINT8 iErrCode) {/*U* IU#I49tk*1

if ( iErrCode = 0) return;

printf( “Error status, %02X\n”,(UINT16)iErrCode);

mainQ{

UINT8 Status;

UINT8X

UINT16Cluster;

mDelaymS( 200); /* flt200tLt*7

mInitSTDIOQ;

CH375_WR_CMD_PORT( CMD_SET_USB_MODE); /*CH375,tWUSBI1?Pttt*7 CH375WRDATPORT( 6); 7*WJt4t4j, USBfl3

while (1) {

printf( “Insert USB disk\n”);

while (mWaitlnterrupt() != USB_INT_CONNECT); 7*I

mDelaymS( 250); /*UAIIfj

Status = mlnitDisk(); /*ftUiJUffifhH

mStoplfError( Status);

Status = midenDiskO; 7*R’J3*7

mStoplfError( Status);

2, 32,

Status = mReadSector( DiskStart + RsvdSecCnt + FATSz16

*

DISK_BUFFER);

mStoplfError( Status); /* i*FAT 16fl flJ*II,3fl?*r!ij1J 32t, I

/

for ( CurrentDir = DISK_BUFFER; CurrentDir[0] != 0; CurrentDir += 32) { if

( (CurrentDir[OxOBl & 0x08 ) == 0 && CurrentDir[0] !OxE5)

{ CurrentDir[OxOBj =0; 7*T±,t1tfl<43 AM*t

printf( “Name:%s\n”,CurrentDir); 7*1L1f

* fl1LBW

}

7*±St flfl,ttff,flflCtfffl*7 if( (DISK BUFFER[OxOB]&0x08)==0 && DISK BUFFER[0]!=OxE5 && DISK BUFFER[8] ==…C?){

Cluster = mGetPointWord( &DISK BUFFER[Ox1A]); /*ff *7

while (Cluster

if( Cluster == 0) mStoplfError( Ox8F); /*

/

Status = mReadSector( mClusterToLba( Cluster ), SecPerClus,

DISK_BUFFER);

mStoplfError( Status); 7*J1??jI:*7

DISK_BUFFER[30] =0; printf( …Data:%s\n”,DISK BUFFER); /*

Cluster = mLinkCluster( Cluster); 7**7

}

while (mWaitlnterrupt() != USB_INT_DISCONNECT); 7*1U1LB*7

mDelaymS( 250);

相关主题