我使用STM32F105RB程序作了一段写U盘扇区的代码
U8 MassStorageDevSectWrite(U32 LBAddr,U16 Len,U8 *PtrBuf)
{
U32 *tmp;
U32 Tag=0x0000000c,TotalByteNum_32,MassStorageDevSectReadTryCnt;
U16 TotalByteNum_16;
CBW.dCBWSignature=dCBWSignatureValue;
CBW.dCBWTag=Tag; //tag value, to be returned unchanged in CSW
TotalByteNum_32=Len*UsbDevHandler.BPB_BytesPerSect;
TotalByteNum_16=Len*UsbDevHandler.BPB_BytesPerSect;
CBW.dCBWDataTransferLength=TotalByteNum_32;
//number of bytes that the host expects to receive from the device
CBW.bmCBWFlags=0x00; //bit 7: 1: DataIn from device
//bit 7: 0: DataOut to device
CBW.bCBWLUN=0x00;//UsbDevHandler.LunToBeUsed;
//the device Logic Unit Number, to which the CBW is sent to
CBW.bCBWCBLength=12; //valid data in the CBW union CmdBlockByteArray, only could be
//from 0x01 to 0x10
//clear cmd array
tmp=(U32 *)CBW.CmdBlockByteArray;
*tmp++=0;*tmp++=0;*tmp++=0;*tmp++=0;
//fill the cmd array
CBW.CmdBlockByteArray[0]=0x2A;
CBW.CmdBlockByteArray[1]=0x00;
CBW.CmdBlockByteArray[2]=(U8)((LBAddr>>24)&0xff);
CBW.CmdBlockByteArray[3]=(U8)((LBAddr>>16)&0xff);
CBW.CmdBlockByteArray[4]=(U8)((LBAddr>>8)&0xff);
CBW.CmdBlockByteArray[5]=(U8)(LBAddr&0xff);
CBW.CmdBlockByteArray[6]=0x00;
CBW.CmdBlockByteArray[7]=(U8)((Len>>8)&0xff);
CBW.CmdBlockByteArray[8]=(U8)(Len&0xff);
CBW.CmdBlockByteArray[9]=0x00;
PrepareCBWTxBuf();
if(epBulkOutTransfer(CBWTxBuf,31)) return(1);
MsDelay(150);
MassStorageDevSectReadTryCnt=0;
while(1)
{
if(!epBulkOutTransfer(PtrBuf,TotalByteNum_16))
{
MsDelay(300);
break;
} else
{
MassStorageDevSectReadTryCnt++;
if (MassStorageDevSectReadTryCnt>5) return(1);
};
};
MsDelay(100);
if(epBulkInTransfer(CSWRxBuf,13)) return(3);
InterpretCSWRxBuf(); //get back CSW
if(AuditCSWReturned(&CSW,Tag)) return(4);
return(0);
}
程序单步调试的时候运行到这个红色的地方,老是取不回CSW的数据,不知道是为什么忘高手指点。
我的程序可以读取U盘文件中的数据,但是写U盘在这个地方总有问题。
U16 GetFreeCusterNum_32(void)
{
U32 clusterNum;
U32 s = FatStarts; //s 指向FAT16表头
U16 i;
clusterNum = 0;
while (s < FatStarts + BPB_FATSz32)
{
if (MassStorageDevSectRead(0,1,UDiskBuf)) //逐个扇区地读取FAT信息
return 0x0;
for (i=0; i<512; i=i+4)
{
if (DW_BUF(UDiskBuf,i) == 0)
{
UDiskBuf = 0xff;
UDiskBuf[i+1] = 0xff;
UDiskBuf[i+2] = 0xff;
UDiskBuf[i+3] = 0xff;
if(MassStorageDevSectWrite(s,1,UDiskBuf))
{
return 0x0;
}
return clusterNum;
}
clusterNum++;
}
s = s + 1;
}
return 0x0;
}
程序在while 循环中调用。
while(1)
{
GetFreeCusterNum_32();
}
望高手指点。