Good Morning,
currently I am working on a mass-storage-solution based on LPC2468 with external flash. I modified the USBMEM-Examples function MSC_MemoryRead, MSC_MemoryWrite, MSC_MemoryVerify and MSC_MemorySize, MSC_Blockcount. The MSD is supposed to be used with a FAT16 filesystem (no MBR, just PBR).
After connecting the device to a PC, the PBR in sector 0 is read, but the following sectors aren't. Using WinHEX i could verify, that even modifying sector 1 (which is hidden, FAT1 begins at sector 32) isn't recognized by the MSD.
Here are the modifications to MSC_ReadMemory function.
[...] NANDFLASHPageRead((Offset &(~(0x1FF)))+FAT_offset_pages<<9 , &Buffer[0], 512); // read corresponding page if ((Offset + n) > MSC_MemorySize) { n = MSC_MemorySize - Offset; BulkStage = MSC_BS_DATA_IN_LAST_STALL; } USB_WriteEP(MSC_EP_IN, &Buffer[Offset%512], n); [...]
I can't figure out, whats wrong. The USBMEM-Example works just fine and I haven't changed anything regarding the MSC except for the functions I've mentioned.
Thank you for reading and hopefully trying to help me.
Regards Frank
On the stack, MSC_MemoryRead() is called repeatedly, even for single sector read. The sector size is 512 bytes (MSC_BlockSize), but the max packet size of the bulk endpoint (EP) is 64 bytes. 512 bytes sector is split into 8 transactions (packets) over the bulk EP. ie. MSC_MemoryRead() is called 8 times for single sector read.
When MSC_MemoryRead() is called once, 64 bytes packet is passed to the endpoint by USB_WriteEP(). MSC_MemoryRead() moves "Offset" variables for the next 64 bytes packet.
In your code, NANDFLASHPageRead() reads out 512 bytes at a time. Is there any problem when NANDFLASHPageRead() is repeatedly called?
Tsuneo
Hello Tsuneo,
Reading the sector multiple times is no problem. I tried various modifications today. For now I am able to read the first 128 sectors (MSC-blocks) without any problem. What I did was to number the sectors in the flash. The first two bytes contain the block- and page-address. The output of disk probe and WinHex shows correct the sector-numbers. Therefor I had to modify the LBA, which seems pretty weird. In the example the LBA is extracted off the CBW and then multiplied by the sectorsize to get the final byte-address. In my case the LBA had to be divided by 512 to get the byte-adress. I can't really explain the reason, I've just found out (by numbering the sectors), that the MSC read every 512th sector.
By now the problem has moved. I can read the first 128 sectors, but the next 64 sectors being read start at sector 0 again with an offset of 128 Byte. Same thing happens every 128 sectors. Maybe I'm just blind for the problem, so here's the code:
void MSC_MemoryRead (void) { u32 n; for(n=0;n<16384;n++) Buffer[n]=0x00; if (Length > MSC_MAX_PACKET) n = MSC_MAX_PACKET; else n = Length; if ((Offset + n) > MSC_MemorySize) { n = MSC_MemorySize - Offset; BulkStage = MSC_BS_DATA_IN_LAST_STALL; } NANDFLASHPageRead(Offset/512+FAT_offset_pages<<9 , &Buffer[0], 512); // entsprechende Page einlesen USB_WriteEP(MSC_EP_IN, &Buffer[Offset%512], n); Offset += n; Length -= n; CSW.dDataResidue-=n; if(Length==0) BulkStage = MSC_BS_DATA_IN_LAST; if (BulkStage != MSC_BS_DATA_IN) { CSW.bStatus = CSW_CMD_PASSED; } }
As there seems to be no way to debug the MSC-functions inside the IDE, i'm a bit stuck right here. Maybe I'm blind and just don't see the/my mistake.
Greetings Frank
Hello Tsuneo
finally I made it working. I forgot brackets in the makro FAT_offset_pages. This totally screwed up the addressing.
For now I can read the device using Disk Probe and WinHex, what doesn't work is opening a testfile via windows explorer. I guess, that windows needs correct working write-functions for opening a file.
Thanks a lot! Frank