帮我分析一下,就是简单的以扇区为单位的读写操作,调试了一天,还是只能写扇区(每次只读或者写一个扇区),读扇区的时间就死掉了。要么是发出读扇区命令后反回的是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); }