幫我分析一下,就是簡(jiǎn)單的以扇區(qū)為單位的讀寫(xiě)操作,調(diào)試了一天,還是只能寫(xiě)扇區(qū)(每次只讀或者寫(xiě)一個(gè)扇區(qū)),讀扇區(qū)的時(shí)間就死掉了。要么是發(fā)出讀扇區(qū)命令后反回的是1F(出錯(cuò)),要么是返回USB_INT_DISK_READ,但隨后發(fā)讀命令時(shí)就死掉了,沒(méi)有出現(xiàn)中斷,U盤(pán)的燈一直亮著,導(dǎo)致這種情況可能會(huì)是什么原因? 程序如下: /* 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); }