2024-09-03 12:32 AM
Hi everyone!
I'm working on USB MSC implementation on STM32U0 series MCU with W25Q32JV (NOR Flash) by using USBX + ThreadX.
W25Q32 Memory organization:
- It has 64 blocks and each block is of size 64KB
- Each block has 16 sectors and each sector is of size 4KB (Total 1024 sectors)
- Each sector has 16 pages and each page is of size 256B (Total 16384 pages)
- Total size of flash memory = 4MB
I've already created and mounted a file system in W25Q32 using FatFS library. Also created one folder with one text file in it. Then, using USBX I tried to implement the MSC and till now I'm able to achieve these things:
1) My laptop (USB Host) is able to detect the device i.e MCU as "USB Mass Storage Device" (in the Device Manager -> USB controllers) and "AzureRTO USBX storage dev USB Device" (in the Device Manager -> Disk drives)
2) Coming to the capacity information of this disk, it is showing correctly as 4MB but the volume of the Drive (D:) as 2MB. And when I open the Properties of this Drive (D:), File system is "Unknown"
3) And in My PC -> USB Drive (D:) is appearing but empty and when I try to open it, this pop-up is coming "Format it before accessing" also, when the device is connected to PC via USB, after some time this pop-up is appearing "D:/ The directory name is invalid"
My analysis:
- There are few parameters named MediaBlockLength and LastLba. According to my understanding, I need to put
MediaBlockLength = 4096 and LastLba = (1024-1) = 1023.
- But I'm getting restricted (in terms of size of block) by other 2 parameters named UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH and UX_SLAVE_REQUEST_DATA_MAX_LENGTH.
- Maximum value of MediaBlockLength is getting restricted by the value of UX_SLAVE_REQUEST_DATA_MAX_LENGTH. But the thing is, I'm not able to put the value of UX_SLAVE_REQUEST_DATA_MAX_LENGTH > 2048 i.e the device is not getting detected at all.
- And because the MediaBlockLength = 2KB, In the device manager, I'm getting the capacity of (D:) as 2MB. Like, if I put it as 1KB, it is showing 1MB.
- Also, maximum value of UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH = 1024.
My doubts:
a) Is there anything related to UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH that is causing the issue?
b) What should be the correct value of MediaBlockLength and UX_SLAVE_REQUEST_DATA_MAX_LENGTH?
c) Finally, how to fix this issue of not recognizing the file system? By the way, I've done all the file system related things in different project. So, do I need to mount the file system again in this USB project as well? (Anyways, I've mounted in this project also and it is successful)
I've attached the code files: ux_user.h, ux_port.h and ux_device_msc.h. Also the configuration part.
2024-09-03 10:09 AM
Hi @RB4020,
it is not possible to use FatFS or USBX directly on the NOR flash you'll need a FTL (flash translation layer) that maps the logical block addresses (LBAs) to physical addresses on the flash.
USBX LBAs typical sizes is 512 bytes while the NOR flash physical sectior is 4KB, i.e a physical section can contain up to 8 LBAs how USBX could retrieve the correct data once written.
That's why you'll need to use USBX on top of LevelX API to access the NOR flash.
regards
Haithem.
2024-09-04 04:23 AM
Hi @Haithem Rahmani
But when I created a file system using FatFS library (not FileX + LevelX), it is created and mounted successfully. But because of the size of this NOR Flash i.e 4MB, the FatFS library function "ff.c" is automatically choosing Fat12 and formatting it.
So, I'm thinking that because Windows 10 doesn't support FAT12, it is not able to recognize the file system in it. So, is there any way to force format it as FAT32 only?
2024-09-09 04:13 AM
Hi @RB4020
this works because FatFS creates the MBR is sector 0 and USBX will read that sector to check for a valid filesystem.
but the issue is that as soon as your application starts accessing the memory in Wirte/Read modes then the memory will be reusing the same sectors as FatFS doesn't provide any 'wearleveling' features,ending with corrupted blocks without any way to recover it.
FatFs is designed for media with builtin hw controllers like SD card or USB thumb keys, however NOR and NAND flash requires a "Flash translation layer" library like levelx.
regards
Haithem.