Help with fx_media_format() parameters for external NOR flash (MX66L1G45G) on STM32U5G9J-DK2

Gowri M 20 Reputation points
2025-06-16T05:27:51.2633333+00:00

Help with fx_media_format() parameters for external NOR flash (MX66L1G45G) on STM32U5G9J-DK2

Hello Microsoft Community,

I’m currently working on implementing a file system using FileX and LevelX on the external NOR flash (MX66L1G45G) available on the STM32U5G9J-DK2 board. I am using FileX + LevelX stack as part of the STM32CubeU5 firmware package.

The memory characteristics of the NOR flash are:

  • Sector Size: 64 KB
  • Flash Size: 128 MB
  • Page Size: 256 bytes

I am attempting to format the NOR flash using the fx_media_format() API, but when I try to open the media in the next run using fx_media_open(), it returns FX_FAT_READ_ERROR. I suspect the formatting parameters might not be correctly aligned with the memory characteristics of the NOR flash. My Queries:

  • What are the correct parameters (especially sector size, cluster size, number of FATs, etc.) to be passed to fx_media_format() for this type of memory?
  • Is there a formula or guideline to compute these parameters for NOR flash storage?
  • Do any additional configuration steps need to be done (such as cache settings, memory alignment, FileX/LevelX defines) to make the system FAT-compliant and reliable?

I am following the Fx_NoR_Write_Read_File (https://github.com/STMicroelectronics/STM32CubeU5/tree/main/Projects/STM32U5G9J-DK2/Applications/FileX/Fx_NoR_Write_Read_File)example as a reference, but it does not cover the full details of formatting parameters and reinitialization logic.

Any guidance, example parameter sets, or documentation references would be greatly appreciated! Thanks in Advance!

Gowri

Azure RTOS
Azure RTOS
An Azure embedded development suite including a small but powerful operating system for resource-constrained devices.
{count} votes

Accepted answer
  1. Manas Mohanty 8,325 Reputation points Microsoft External Staff Moderator
    2025-06-17T20:01:04.4333333+00:00

    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

    1. 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].

    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].

    1. 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].

    1. 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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. VSawhney 880 Reputation points Microsoft External Staff Moderator
    2025-06-16T12:04:49.68+00:00

    Hello Gowri M,

    Hey Gowri! It looks like you’re working on setting up FileX with the external NOR flash on your STM32U5G9J-DK2, and you're running into some trouble with the fx_media_format() parameters. Let’s break down your questions and provide some guidance.

    1. Formatting Parameters: Given your NOR flash characteristics (Sector Size: 64 KB, Flash Size: 128 MB, Page Size: 256 bytes), you'll need to select the parameters for fx_media_format() accordingly:
      • Sector Size: This should match your NOR flash sector size, which is 64 KB (or 65536 bytes).
      • Cluster Size: Typically, it should be a multiple of the sector size. This could be set to 64 KB, but you may want to try smaller sizes (such as 16 KB) for better space efficiency, especially if you're dealing with many small files.
      • Number of FATs: This is usually set to 2 for redundancy.
      • FAT Size and Other Parameters: The specific values can be derived from the total size of the flash and the sector size. The total number of clusters can be determined from the total flash size divided by the cluster size.
    2. Calculating Parameters: A good guideline to compute the parameters for NOR flash storage is:
      • Ensure that the cluster size is a power of 2 and aligns with the sector size.
      • Verify that the total number of clusters plus reserved sectors fits within the total flash size.
    3. Additional Configuration Steps: It’s vital to ensure that you are considering cache settings and memory alignment. This might include:
      • Aligning your data structures to flash page boundaries.
      • Making sure cache settings are compatible with your flash memory operations.
      • Setting any necessary FileX/LevelX configuration defines to facilitate FAT compliance and efficient operation.

    I hope this helps.
    Thank you!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.