Thanks for the detailed explanation, Gowri M
Problem Summary
You're encountering a FXFATREAD_ERROR on the second run when skipping fx_media_format()
and directly calling fx_media_open()
. This happens even though the first run works perfectly after enabling LX_STM32_XSPI_ERASE = 1
and formatting the media.
You also noted that setting the sector size to 64KB results in a FXSECTORINVALID status, and the maximum allowable value appears to be 4KB.
Here are my findings
- Sector Size Limitation
The sector size limitation you're facing is consistent with the STM32 and FileX constraints. According to STMicroelectronics documentation, the sector size for most STM32 platforms is capped at 4KB. Attempting to use 64KB will trigger a FX_SECTOR_INVALID
error [1].
- Hidden Sectors and Metadata Conflicts
A related issue discussed in Microsoft Q&A suggests that raw writes to hidden sectors can cause fx_media_open()
to fail because the file system expects specific metadata in those sectors. If those sectors are modified or missing expected structures, the media may be unreadable [2].
- FAT Boot Sector Requirements
The FAT32 specification emphasizes that the boot sector must contain specific signatures (e.g., 0x55AA at offsets 510 and 511) and that the BPB_TotSec
field must not exceed the actual disk size. If these values are misconfigured, the volume may be considered malformed, leading to read errors [3].
- Concurrency Issues
In multi-threaded environments, simultaneous access to the file system without proper synchronization (e.g., mutexes) can cause FX_IO_ERROR
and FX_FAT_READ_ERROR
. If your application uses multiple threads, ensure that file operations are protected by a single mutex [4].
Recommendations
Stick to 4KB Sector Size Confirm that your driver and media support 4KB sectors and avoid using larger sizes unless explicitly supported.
Always Erase Before Format Continue using LX_STM32_XSPI_ERASE = 1
to ensure a clean slate before formatting. This seems to resolve the initial formatting issue.
Validate Boot Sector and FAT Metadata Double-check the parameters passed to fx_media_format()
—especially hidden_sectors
, total_sectors
, and bytes_per_sector
. Ensure they align with the actual media characteristics.
Use Mutex for File Access If your application is multi-threaded, wrap all file operations (open, read, write, close) in a mutex to prevent concurrent access issues.
Consider Reopening Media with Format Check On the second run, instead of skipping fx_media_format()
, consider checking whether the media is formatted correctly before calling fx_media_open()
. If the check fails, reformat the media.
Would you like help reviewing the exact parameters you're passing to fx_media_format()
? If you can share that snippet, I can help validate them for correctness.
References
[1] FILEX: fx_media_format
parameters. - STMicroelectronics
[2] [Why available size of flash(obtained using fx_media_space_available) remains same irrespective of hidden sectors argument in fx_media_format? - Microsoft Q&;A](https://learn.microsoft.com/en-us/answers/questions/1649667/why-available-size-of-flash(obtained-using-fx-medi)
[3] FAT32 File System Specification
[4] FX_FAT_READ_ERROR / FX_IO_ERROR error writing and reading with multiple ...
Thank you.