andrey
andrey

Reputation: 1595

Using MCU's internal flash memory as FatFs drive

I'm trying to make internal flash on STM32F051xx to be seen as a drive.

This is the code on the top level:

char USER_Path[4]; /* USER logical drive path */
FATFS USER_FatFs;  /* File system object for User logical drive */
FIL USER_File;     /* File object */  

uint32_t bytesWritten;
uint8_t text[] = "Text to write to logical disk";
if (FATFS_LinkDriver(&USER_Driver, USER_Path) == 0) {
  if(f_mount(&USER_FatFs, (TCHAR const*)USER_Path, 0) == FR_OK) {
    if(f_open(&USER_File, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) == FR_OK) {
      if(f_write(&USER_File, text, sizeof(text), (void *)&bytesWritten) == FR_OK); {
        f_close(&USER_File);
      }
    }
  }
}

f_mount() returns FR_OK, but when it comes to creating a new file via f_open(), which calls find_volume(), which calls check_fs() which returns FR_NO_FILESYSTEM. I assume this is because a boot sector wasn't created, but I have no idea how to do that.

I've written USER_read(), USER_write() and USER_ioctl() functions, but I don't know what to write in the USER_initialize() function. Right now I've left it in it's original state, where it returns RES_OK without doing anything. I feel like that may be the source of the problem.

Any suggestions?

Upvotes: 1

Views: 4101

Answers (2)

Kamran Kia
Kamran Kia

Reputation: 347

Well that's not going to work very well after all. My suggestion would be to reconsider the generality of idea.

FatFS works with a block device in which it can write any 512-byte sector of the disk at any time. But that's not how STM32 flash memories work. In STM32 series, flash memory is organized as a set of 1-2-4-16-64-128 KB sectors! (clearly not 512-byte sectors assumed by FatFS). Each sector would be needed to be written to as part of a program/erase cycle.

Writing to a single 512-byte subsector of those STM32 flash sectors will require an expensive, slow operation where you'll have to erase the entire flash sector, then re-write it with one 512 sector modified! On a 128KB sector it could take up to 4 seconds to just erase the sector! This will wear the flash memory very quickly, resulting in progressive failure of the NOR flash.

Upvotes: 0

andrey
andrey

Reputation: 1595

I did some research and what it comes down to is that there simply isn't enough space in internal flash memory (64KB total) to have it used for FAT.

Upvotes: 1

Related Questions