Skip to main content
Visitor II
May 30, 2025
Question

FX_FILE_CORRUPT when accessing eMMC file on STM32U5G9J-DK1 after reprogramming

  • May 30, 2025
  • 2 replies
  • 760 views

Hi,
I’m using the STM32U5G9J-DK1 board and working on implementing a FileX-based filesystem on the onboard eMMC. 

On the first run, I format the eMMC using fx_media_format(), open the media, create a file (e.g., file1.txt), write data to it, and read it back — all of which works correctly. However, in the second run, I flash new firmware that skips the formatting step and attempts to open and read from file1.txt. In this case, although the media opens successfully and checks for the file’s existence, it returns FX_ALREADY_CREATED, but trying to open the file results in an FX_FILE_CORRUPT error.

If I do only the formatting in the first run and perform the file creation and access in the second run, everything works fine.

I’m unsure why the file becomes unreadable across firmware flashes unless the file is created again after formatting. 

Thanks!

    This topic has been closed for replies.

    2 replies

    Technical Moderator
    May 30, 2025

    Hello @Gowri_M 

    Did you close the media in the first run?

    Could you please share your code?

    Gowri_MAuthor
    Visitor II
    June 2, 2025

    Hi @Saket_Om 
    Yes I have closed the media in the first run. Here is the code snippet for the first run:

    void fx_app_thread_entry(ULONG thread_input)
    {
    
     UINT mmc_status = FX_SUCCESS;
    
    /* USER CODE BEGIN fx_app_thread_entry 0*/
     ULONG bytes_read;
     CHAR read_buffer[READ_BUFFER_SIZE];
     CHAR data[40] = "This file is stored in the eMMC card";
     pCardInfoMMC = malloc(sizeof(HAL_MMC_CardInfoTypeDef));
     mmc_status = HAL_MMC_GetCardInfo(&hmmc1, pCardInfoMMC);
     if (mmc_status != FX_SUCCESS)
     {
     while(1);
     }
    
     /* Format the MMC memory as FAT */
     mmc_status = fx_media_format(&mmc_disk, // MMC_Disk pointer
     fx_stm32_mmc_driver, // Driver entry
     (VOID *)FX_NULL, // Device info pointer
     (UCHAR *) fx_mmc_media_memory, // Media buffer pointer
     sizeof(fx_mmc_media_memory), // Media buffer size
     FX_MMC_VOLUME_NAME, // Volume Name
     FX_MMC_NUMBER_OF_FATS, // Number of FATs
     32, // Directory Entries
     FX_MMC_HIDDEN_SECTORS, // Hidden sectors
     pCardInfoMMC->BlockNbr, // Total sectors
     FX_STM32_MMC_DEFAULT_SECTOR_SIZE, // Sector size
     8, // Sectors per cluster
     1, // Heads
     1); // Sectors per track
    
     /* Check the format mmc_status */
     if (mmc_status != FX_SUCCESS)
     {
     /* USER CODE BEGIN MMC format error */
     while(1);
     /* USER CODE END MMC format error */
     }
    
    /* USER CODE END fx_app_thread_entry 0*/
    
     /* Open the disk driver */
     mmc_status = fx_media_open(&mmc_disk, FX_MMC_VOLUME_NAME, fx_stm32_mmc_driver, (VOID *)FX_NULL, (VOID *) fx_mmc_media_memory, sizeof(fx_mmc_media_memory));
    
     /* Check the media open mmc_status */
     if (mmc_status != FX_SUCCESS)
     {
     /* USER CODE BEGIN MMC open error */
     while(1);
     /* USER CODE END MMC open error */
     }
    
     /* USER CODE BEGIN fx_app_thread_entry 1*/
     mmc_status = fx_file_create(&mmc_disk, "STM32_FILE1.TXT");
    
     /* Check the create status. */
     if (mmc_status != FX_SUCCESS)
     {
     /* Check for an already created status. This is expected on the
     second pass of this loop! */
     if (mmc_status != FX_ALREADY_CREATED)
     {
     while(1);
     }
     }
    
     /* Open the test file. */
     mmc_status = fx_file_open(&mmc_disk, &fx_file_mmc, "STM32_FILE1.TXT", FX_OPEN_FOR_WRITE);
    
     /* Check the file open status. */
     if (mmc_status != FX_SUCCESS)
     {
     while(1);
     }
    
     /* Seek to the beginning of the test file. */
     mmc_status = fx_file_seek(&fx_file_mmc, 0);
    
     /* Check the file seek status. */
     if (mmc_status != FX_SUCCESS)
     {
     while(1);
     }
    
     /* Write a string to the test file. */
     mmc_status = fx_file_write(&fx_file_mmc, data, sizeof(data));
    
     /* Check the file write status. */
     if (mmc_status != FX_SUCCESS)
     {
     while(1);
     }
    
     /* Close the test file. */
     mmc_status = fx_file_close(&fx_file_mmc);
    
     /* Check the file close status. */
     if (mmc_status != FX_SUCCESS)
     {
     while(1);
     }
    
     mmc_status = fx_media_flush(&mmc_disk);
    
     /* Check the media flush status. */
     if (mmc_status != FX_SUCCESS)
     {
     while(1);
     }
    
     /* Open the test file. */
     mmc_status = fx_file_open(&mmc_disk, &fx_file_mmc, "STM32_FILE1.TXT", FX_OPEN_FOR_READ);
    
     /* Check the file open status. */
     if (mmc_status != FX_SUCCESS)
     {
     while(1);
     }
    
     /* Seek to the beginning of the test file. */
     mmc_status = fx_file_seek(&fx_file_mmc, 0);
    
     /* Check the file seek status. */
     if (mmc_status != FX_SUCCESS)
     {
     while(1);
     }
    
     /* Read the first 28 bytes of the test file. */
     mmc_status = fx_file_read(&fx_file_mmc, read_buffer, sizeof(data), &bytes_read);
    
     /* Check the file read status. */
     if ((mmc_status != FX_SUCCESS) || (bytes_read != sizeof(data)))
     {
     while(1);
     }
    
     /* Close the test file. */
     mmc_status = fx_file_close(&fx_file_mmc);
    
     /* Check the file close status. */
     if (mmc_status != FX_SUCCESS)
     {
     while(1);
     }
    
     /* Close the media. */
     mmc_status = fx_media_close(&mmc_disk);
    
     /* Check the media close status. */
     if (mmc_status != FX_SUCCESS)
     {
     while(1);
     }
    
    /* USER CODE END fx_app_thread_entry 1*/
     }

    For the second run, I just commented the format function call.

     

    Technical Moderator
    June 2, 2025

    Hello @Gowri_M 

    Could try to format and create the file with your computer and check if you can open and read it with your code.

    Gowri_MAuthor
    Visitor II
    June 3, 2025

    Hi all,

    Could anyone share a working example that demonstrates:

    1.Formatting and writing a file in one firmware,

    2.Then reading that file successfully in another firmware run (without reformatting)?


    Thanks in advance!