cancel
Showing results for 
Search instead for 
Did you mean: 

Using internal flash memory as FatFs drive

dscdsc
Senior
Posted on February 13, 2015 at 13:56

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, 
''STMTXT''
, 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? #fatfs #stm32 #flash #middleware
5 REPLIES 5
qwer.asdf
Senior
Posted on February 13, 2015 at 14:12

Try to create a filesystem using f_mkfs, don't forget to #define _USE_MKFS  1 in ffconf.h

dscdsc
Senior
Posted on February 13, 2015 at 14:34

f_mkfs() fails with FR_DISK_ERR because sector count is lower than 128 in this line:

if
(disk_ioctl(pdrv, GET_SECTOR_COUNT, &n_vol) != RES_OK || n_vol < 128)

In my case sector count equals the number of available pages - 32 (half of what's available on chip to allow space for code). Does this mean that I can't use FatFs with only 32KB available for it?
qwer.asdf
Senior
Posted on February 13, 2015 at 15:06

One sector is typically 512 bytes, so 128 sectors will be 64KB minium for FatFS to allow you to create a filesystem. It seems 32KB is too small for FAT, but I can't be sure. Are you sure you need such a small filesystem? Maybe just create some raw C structures and write the data without a filesystem?

Posted on February 13, 2015 at 15:08

At some point the sectors committed to file system structures, and wasted space, dwarf the usable space. Are you sure that FAT is suitable for your application, or would something else be more usable/efficient?

You could try building your own initialized file system structure in memory, this might better help you understand how things are working, or force you to make better decisions.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
dscdsc
Senior
Posted on February 13, 2015 at 16:38

Right, I guess I'll stick with my own system then.