I am using stm32L4 microcontroller and I have connected my board with an external flash (AT45DB041E), which I have successfully managed to communicate with the it using the SPI peripheral. All I want to do is to find a binary file that exists in the external flash in a specific path "fs/update.bin". For that reason I use FATFS. However, I am not able to mount the external flash. Maybe I am not using it properly. This is my code for mounting the external flash and trying to see if the file exists in the flash memory. Every time I am getting FR_NOT_READY status. I use 3.6V in a custom board and I can read the manufacturer ID of the flash, so the connections are ok.
Is this the proper way to do what I have described above?
FATFS *fs;
FIL fil;
FRESULT err = FR_NOT_READY;
fs = malloc(sizeof(FATFS));
/*Initialize the external flash memory*/
at45db_init();
HAL_Delay(2);
/*Enable the device*/
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);
err = f_mount(fs, "fs", 1);
if (err == FR_OK) {
printf("Mounted successfully\n\r");
} else {
printf("Error in mounting external flash memory\n\r");
}
err = f_open(&fil, "/fs/update.bin", FA_OPEN_EXISTING);
if (err == FR_OK) {
printf("The file exists in flash memory\n\r");
} else {
printf("The file does not exists in flash memory\n\r");
}
You use LittleFS to write data to Flash, but you try to read that data using FAT. This won't work, because different file systems use different data structures to store file metadata and data locations on a disk (pointers to file contents).
Every file system uses different data structures to keep track of memory blocks and files, they can have different flags and properties assigned to files, and they are stored in a way unique to that file system. FAT file system driver will simply fail to interpret data structures of LittleFS as a valid FAT file system.
This is why you can't open a storage managed by LittleFS as FAT, this is why you can't read a Mac APFS disk as a Windows NTFS disk - the bytes in the same physical locations have different meanings for them.
A storage area should be managed only by a single file system. You either write and read it all with FAT, or you write and read it all with LittleFS.