The USB interface reads and writes files, that is, the USB large-capacity storage device, that is, the USB flash disk. The project mainly reads data from the SD card of the Development Board as a USB flash drive.
The knowledge involved includes the USB protocol, SCSI interface, and BOT protocol.
This article briefly records the debugging process as follows:
I. Data Analysis on serial port printing: (LBA is the fan area code, Len is the slice length, and one slice is 512 bytes)
0x1e8 is the start sector of the Dir
0x208 indicates the start sector of the data.
LBA: 0, Len: 1 LBA: 0, Len: 1 LBA: 0, Len: 1 LBA: 0, Len: 1 LBA: 0, Len: 8 LBA: 0, len: 1 LBA: 40, Len: 4 LBA: 0, Len: 8 LBA: 8, Len: 8 LBA: 10, Len: 8 LBA: 18, Len: 8 LBA: 20, Len: 8 LBA: 28, Len: 8 LBA: 30, Len: 8 LBA: 38, Len: 8 LBA: 40, Len: 8 LBA: 48, Len: 8 LBA: 50, Len: 8 LBA: 58, Len: 8 LBA: 60, Len: 8 LBA: 68, Len: 8 LBA: 70, Len: 8 LBA: 78, len: 8 LBA: 80, Len: 8 LBA: 88, Len: 8 LBA: 90, Len: 8 LBA: 98, Len: 8 LBA: A0, Len: 8 LBA: a8, Len: 8 LBA: B0, Len: 8 LBA: B8, Len: 8 LBA: C0, Len: 8 LBA: C8, Len: 8 LBA: D0, Len: 8 LBA: D8, Len: 8 LBA: E0, Len: 8 LBA: E8, Len: 8 LBA: F0, Len: 8 LBA: 1e8, Len: 8
// The entire disk has stopped.
LBA: 208, Len: 1 // click a file to print this line.
// Copy a file and print it as follows: 4 is the starting sector where fat1 is located, F6 is the starting sector where fat2 is located
LBA: 0, Len: 1
LBA: 1e8, Len: 8 LBA: 1e8, Len: 8
LBA: 4, Len: 1 LBA: F6, Len: 1
LBA: 1e8, Len: 8 LBA: 1e8, Len: 8 LBA: 1e8, Len: 8
LBA: 20C, Len: 1 // The file database occupied by replication is 0x208 (a file is a cluster at least, and a cluster in this article is 4 sectors)
LBA: 0, Len: 1
LBA: 4, Len: 1 LBA: F6, Len: 1
After creating a TXT file, the following error occurs: 1e8 is the start sector where DIR is located, 4 is the start sector where fat1 is located, and F6 is the start sector where fat2 is located.
LBA: 0, Len: 1 LBA: 1e8, Len: 8 LBA: 1e8, Len: 8 LBA: 4, Len: 1 LBA: F6, Len: 1
LBA: 0, Len: 1 LBA: 4, Len: 1 LBA: F6, Len: 1
After creating a TXT file, the following occurs: (same as above)
LBA: 0, Len: 1 LBA: 1e8, Len: 8 LBA: 1e8, Len: 8 LBA: 4, Len: 1 LBA: F6, Len: 1
LBA: 0, Len: 1 LBA: 4, Len: 1 LBA: F6, Len: 1
2. File error records:
After transplantation, a new file or copy will become stuck. Once I let my computer go on strike, restart, and detect it, and I am going crazy.
Fortunately, the results are always good. In this case, you can always find the root cause of the problem.
Therefore, I first check the data written to the Dir through the serial port, as shown below:
Efs1 txt m T-shirt? 0 k? ? A branch, G Branch Ech. t x T
1. From this sentence, we should write all the items in the USB flash drive displayed in windows to the USB flash drive.
Later, I found that the original write address was added incorrectly.
Writememoryoffset + = bulklen;
W_offset + = bulklen; // must add before write
If (writememoryoffset = 512) // write a page
{
Msd_writeblock (u8 *) writememory, w_offset, 512 );
Writememoryoffset = 0; // WriteMemoryOffset-512; // keep the last data
}
The address written to the SD card must be 512-byte aligned. Otherwise, an error will occur. Adding w_offset to the end of the write operation results in an offset of 448, which is naturally wrong.
2. Plug in the Development Board and write it into it. But after the power is cut off, or you can directly use the card reader to check whether the files are written into it.
Check winhex. dri started at 0x3d000 and there is only one file item in it.
Now, at dri0x3d200, there is a file written to me (it should have appeared at 0x3d000, and there is a 512-byte difference in the block offset ).
But Windows cannot recognize it at the moment.
What should we do?
The cause in 512 is clarified, mainly because msd_writeblock the address passed by this function should be the first address of the block, so it should be increased by after writing.
The code snippet is as follows:
If (writememoryoffset = 512) // write a page
{
Msd_writeblock (u8 *) writememory, w_offset, 512 );
Writememoryoffset = 0; // WriteMemoryOffset-512; // keep the last data
W_offset + = 512; // must add after write
}
Haha, since then, I have been able to read and write files, delete files, and use them like a USB flash drive ~~
PS: The fat16 file system is not used here. It is estimated that SCSI commands are used instead. It seems that this excuse is really good!