2015-02-13 04:56 AM
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
2015-02-13 05:12 AM
Try to create a filesystem using f_mkfs, don't forget to #define _USE_MKFS 1 in ffconf.h
2015-02-13 05:34 AM
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?
2015-02-13 06:06 AM
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?
2015-02-13 06:08 AM
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.2015-02-13 07:38 AM
Right, I guess I'll stick with my own system then.