CH375能写但是不能读,咋回事啊?

帮我分析一下,就是简单的以扇区为单位的读写操作,调试了一天,还是只能写扇区(每次只读或者写一个扇区),读扇区的时间就死掉了。要么是发出读扇区命令后反回的是1F(出错),要么是返回USB_INT_DISK_READ,但随后发读命令时就死掉了,没有出现中断,U盘的灯一直亮着,导致这种情况可能会是什么原因? 程序如下: /* CH375.c */

#include "CH375.h" #include "CH375INC.h" #include "c8051F320addr.h"

// Buffer for read/write transfers: unsigned char xdata gScratch[PHYSICAL_BLOCK_SIZE];

void Delay1_2uS(void);

void DelaymS(unsigned char i);

void CH375_WR_CMD_PORT(unsigned char cmd);

void CH375_WR_DAT_PORT(unsigned char dat);

unsigned char CH375_RD_DAT_PORT(void);

unsigned char WaitInterrupt(void) { while (CH375_INT); CH375_WR_CMD_PORT(CMD_GET_STATUS); return(CH375_RD_DAT_PORT()); }

unsigned char CH375Init(void) { unsigned char i; #ifdef TEST_CH375_PORT unsigned char c; CH375_WR_CMD_PORT(CMD_CHECK_EXIST); CH375_WR_DAT_PORT(0x55); c = CH375_RD_DAT_PORT(); if (c!=0xaa) { for (i=100; i!=0; i--) { CH375_WR_CMD_PORT(CMD_RESET_ALL); c = CH375_RD_DAT_PORT(); } Delay100mS(); } #endif CH375_WR_CMD_PORT(CMD_SET_USB_MODE); CH375_WR_DAT_PORT(6); for (i=0xFF; i!=0; i--) { if (CH375_RD_DAT_PORT()==CMD_RET_SUCCESS) break; } if (i!=0) return(0); //OK else return(0xFF); //False }

unsigned char DiskInit(void) { unsigned char IntStatus; CH375_WR_CMD_PORT(CMD_GET_STATUS); IntStatus = CH375_RD_DAT_PORT(); if (IntStatus==USB_INT_DISCONNECT) return (IntStatus); CH375_WR_CMD_PORT(CMD_DISK_INIT); IntStatus = WaitInterrupt(); if (IntStatus!=USB_INT_SUCCESS) return (IntStatus);

CH375_WR_CMD_PORT(CMD_DISK_SIZE); IntStatus = WaitInterrupt(); if (IntStatus!=USB_INT_SUCCESS) { Delay100mS(); CH375_WR_CMD_PORT(CMD_DISK_SIZE); IntStatus = WaitInterrupt(); }

if (IntStatus!=USB_INT_SUCCESS) { return (IntStatus); }

return (0); }

unsigned char SectRead(unsigned long sector_LBA) { unsigned char IntStatus; unsigned char *BufferPoint; unsigned int BlockCount; unsigned char Length; CH375_WR_CMD_PORT(CMD_DISK_READ); CH375_WR_DAT_PORT((unsigned char)sector_LBA); CH375_WR_DAT_PORT((unsigned char)(sector_LBA>>8)); CH375_WR_DAT_PORT((unsigned char)(sector_LBA>>16)); CH375_WR_DAT_PORT((unsigned char)(sector_LBA>>24)); CH375_WR_DAT_PORT(1); BufferPoint = gScratch; for (BlockCount=1*CH375_BLK_PER_SEC; BlockCount!=0; BlockCount--) { IntStatus = WaitInterrupt(); if (IntStatus==USB_INT_DISK_READ) { CH375_WR_CMD_PORT(CMD_RD_USB_DATA); Length = CH375_RD_DAT_PORT(); while (Length) { *BufferPoint = CH375_RD_DAT_PORT(); BufferPoint++; Length--; } CH375_WR_CMD_PORT(CMD_DISK_RD_GO); } else break; }

if (BlockCount==0) { IntStatus = WaitInterrupt(); if (IntStatus==USB_INT_SUCCESS) return (0); }

return (IntStatus); }

unsigned char SectWrite(unsigned long sector_LBA) { unsigned char IntStatus; unsigned char *BufferPoint; unsigned int BlockCount; unsigned char Length;

CH375_WR_CMD_PORT(CMD_DISK_WRITE); CH375_WR_DAT_PORT((unsigned char)sector_LBA); CH375_WR_DAT_PORT((unsigned char)(sector_LBA>>8)); CH375_WR_DAT_PORT((unsigned char)(sector_LBA>>16)); CH375_WR_DAT_PORT((unsigned char)(sector_LBA>>24)); CH375_WR_DAT_PORT(1); BufferPoint = gScratch;

for (BlockCount=1*CH375_BLK_PER_SEC; BlockCount!=0; BlockCount--) { IntStatus = WaitInterrupt(); if (IntStatus==USB_INT_DISK_WRITE) { CH375_WR_CMD_PORT(CMD_WR_USB_DATA7); Length = CH375_BLOCK_SIZE; CH375_WR_DAT_PORT(Length); while (Length) { CH375_WR_DAT_PORT(*BufferPoint); BufferPoint++; Length--; }

CH375_WR_CMD_PORT(CMD_DISK_WR_GO); } else break; }

if (BlockCount==0) { IntStatus = WaitInterrupt(); if (IntStatus==USB_INT_SUCCESS) return (0); }

return (IntStatus); }

你的c8051单片机采用的是多大的晶振?c8051单片机比51的速度要快的多,会不会是你三个读写子函数的延时问题。能不能把你的三个读写子函数帖出来看一下。


void Delay1uS(void) { unsigned char i; for (i=2; i!=0; i--); }

void DelaymS(unsigned int ms) { unsigned int i; unsigned char j; for(j=ms; j>0; j--) for(i=1200; i>0; i--); }

void CH375_WR_CMD_PORT(unsigned char cmd) { Delay1uS(); Delay1uS();

P2 = cmd; CH375_A0 = 1; CH375_CS = 0; CH375_WR = 0; CH375_CS = 0; //Delay 167nS, at least 90nS CH375_WR = 1; CH375_CS = 1; CH375_A0 = 0; P2 = 0xFF; Delay1uS(); Delay1uS(); }

void CH375_WR_DAT_PORT(unsigned char dat) { P2 = dat; CH375_A0 = 0; CH375_CS = 0; CH375_WR = 0; CH375_CS = 0; //Delay 167nS, at least 90nS CH375_WR = 1; CH375_CS = 1; P2 = 0xFF; Delay1uS(); }

unsigned char CH375_RD_DAT_PORT(void) { unsigned char Data; Delay1uS(); P2 = 0xFF; CH375_A0 = 0; CH375_CS = 0; CH375_RD = 0; CH375_CS = 0; //Delay 167nS, at least 90nS Data = P2; CH375_RD = 1; CH375_CS = 1; return(Data); }

CH375用的是单片机输出的12MHz晶振,我计算过了,延时没有问题,Delay1uS()函数调用一次到反回总共的机器指令周期为4(LCALL)+2(MOV ri,#data)+(3/4)*2(DJNZ)+5(RET)=12.5个时钟周期,单片机时钟频率为12MHz,这样就有12.5/12>1uS了。还有,我写U盘和读取U盘容量都是正确的,这说明对命令和数据的操作应该没有问题啊,就是读U盘扇区时就死掉了,一直在那里等待中断(for循环里的第一个等待中断)。U盘的灯一直亮着。。。


在你产生USB_INT_DISK_READ中断之后,能读取到数据吗???


产生不了这个中断,一直在等那个中断,U盘灯一直亮着。写U盘扇区倒是没有问题,不同的扇区我都试过了,能写进去,但读不出来,一直在那里等中断


我换了一个U盘试了一下,这回产生USB_INT_DISK_READ中断了,但随后CH375_WR_CMD_PORT(CMD_RD_USB_DATA0)之后读出的Length = CH375_RD_DAT_PORT();为零。。。。。。。。郁闷中。。。。。。。。。


应该不会啊,你在你的三个读写子程序里面延时加大点,要是还不可以的话,话一个375试下看


有的u盘会这样 旧的就不会了


换了个CH375,现在OK了,原来是芯片的问题


只有登录才能回复,可以选择微信账号登录