cancel
Showing results for 
Search instead for 
Did you mean: 

FX_FILE_CORRUPT when accessing eMMC file on STM32U5G9J-DK1 after reprogramming

Gowri_M
Associate II

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!

6 REPLIES 6
Saket_Om
ST Employee

Hello @Gowri_M 

Did you close the media in the first run?

Could you please share your code?

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om

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*/ }
View more

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

 

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.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om

Hi @Saket_Om 
I’m not using USB in this setup, and since the storage is onboard eMMC (STM32U5G9J-DK1), it's not possible to remove the device and format it externally.

Gowri_M
Associate II

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!



Hello @Saket_Om 

I was referring to the post How to use FileX with eMMC for file system management  , and I found that the issue was due to incorrect parameters passed to the fx_media_format function. By reducing the number of sectors, I was able to solve the initial formatting issue.

That said, I would still like to format the media using the fx_media_format function provided by FileX, since my goal is to fully integrate the formatting step into the embedded system itself. The capacity of my eMMC is 4GB.

Here's the code I'm using for formatting:

mmc_status = fx_media_format(&mmc_disk, // eMMC_disk pointer fx_stm32_mmc_driver, // Driver entry (VOID*) FX_NULL, // Device info pointer (VOID*) 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 MMC_NUM_OF_SECTORS, // Total sectors MMC_SECTORSIZE, // Sector size 1, // Sectors per cluster 1, // Heads 1); // Sectors per track

And here are the macro definitions and their values:

#define FX_MMC_VOLUME_NAME "STM32_MMC_DISK" #define FX_MMC_NUMBER_OF_FATS 1 #define FX_MMC_HIDDEN_SECTORS 0 #define FX_STM32_MMC_DEFAULT_SECTOR_SIZE 512 #define MMC_SECTORSIZE (4*1024) #define MMC_NUM_OF_SECTORS (1024*1024) ALIGN_32BYTES (uint32_t fx_mmc_media_memory[FX_STM32_MMC_DEFAULT_SECTOR_SIZE / sizeof(uint32_t)]);

 
With these values, I was able to successfully create 31 files, and both write and read operations worked during the first run.

However, in the second run (after skipping the format step), I attempted to read the files created earlier. I was only able to read up to 8 files successfully. Trying to open the 9th file either returns FX_NOT_FOUND or, in some cases, opens the file but results in FX_FILE_CORRUPT during the read operation.

I'm not sure what's causing this inconsistency. 

Also, I would really appreciate it if someone could help me understand how to correctly calculate or choose the values for the fx_media_format()parameters—such as number_of_sectors, bytes_per_sector, sectors_per_cluster, etc. Any reference or examples would be helpful.

Thanks!