stm32f4 discovery fatfs problem
Moderator: RoccoMarco
stm32f4 discovery fatfs problem
Hello,
I am trying to use my microsd card with stm32f4-DISCOVERY board. I can mount the sd card but I get "f_getfree failed" error, when I try to read something from the sd card. I am using ChibiOS 2.4.3 . I programmed the card as fat32.
Thank you,
I am trying to use my microsd card with stm32f4-DISCOVERY board. I can mount the sd card but I get "f_getfree failed" error, when I try to read something from the sd card. I am using ChibiOS 2.4.3 . I programmed the card as fat32.
Thank you,
Re: stm32f4 discovery fatfs problem
Could you please give some more background about your hardware setup, the software you use etc? Did you custom wire a socket to the F4 or do you use a well known expansion board? Do you try to use the MMC or the SDC driver (SPI vs. SDIO)? Did you try an official demo or did you do something yourself?
What toolchain are you using?
~ Tectu
What toolchain are you using?
~ Tectu
Re: stm32f4 discovery fatfs problem
Hi,
I am using stm32f407-DISCOVERY board (VGT6) . I use arm-none-abi-gcc toolchain. I use a demo application in ChibiOS. I added the fatfs library to the demo example. I use SPI communication. (PA 5-6-7 and PC4 as chip select). I change the halconf.h file to support MMC-SPI.
Thank you!
I am using stm32f407-DISCOVERY board (VGT6) . I use arm-none-abi-gcc toolchain. I use a demo application in ChibiOS. I added the fatfs library to the demo example. I use SPI communication. (PA 5-6-7 and PC4 as chip select). I change the halconf.h file to support MMC-SPI.
Thank you!
- Giovanni
- Site Admin
- Posts: 14892
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 996 times
Re: stm32f4 discovery fatfs problem
Hi,
Without more info is hard to give advice.
Is the card connection phase successful?
Giovanni
Without more info is hard to give advice.
Is the card connection phase successful?
Giovanni
Re: stm32f4 discovery fatfs problem
Code: Select all
static void cmd_tree(BaseChannel *chp, int argc, char *argv[]) {
FRESULT err;
uint32_t clusters;
FATFS *fsp;
(void)argv;
if (argc > 0) {
chprintf(chp, "Usage: tree\r\n");
return;
}
if (!fs_ready) {
chprintf(chp, "File System not mounted\r\n");
return;
}
err = f_getfree("/", &clusters, &fsp);
if (err != FR_OK) {
chprintf(chp, "FS: f_getfree() failed\r\n");
return;
}
chprintf(chp,
"FS: %lu free clusters, %lu sectors per cluster, %lu bytes free\r\n",
clusters, (uint32_t)MMC_FS.csize,
clusters * (uint32_t)MMC_FS.csize * (uint32_t)MMC_SECTOR_SIZE);
fbuff[0] = 0;
scan_files(chp, (char *)fbuff);
}it gives me f_getfee failed error. So card is mounted.
- Giovanni
- Site Admin
- Posts: 14892
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 996 times
Re: stm32f4 discovery fatfs problem
You need to step into that function and see exactly what is failing inside there, from the info we have it could be anything, from an HW problem to a bug into the SD driver or FatFs, it could be also be a problem with that specific card.
Giovanni
Giovanni
Re: stm32f4 discovery fatfs problem
Can you try another card? There have been some issues with a few models...
~ Tectu
~ Tectu
Re: stm32f4 discovery fatfs problem
I have Sandisk microsd card. It gives me error in ff.c file. chk_mounted :which checks if the file system object is valid or not. it cannot verify...
Re: stm32f4 discovery fatfs problem
Before we digg deeper into this, can you please try another card? I have used three dozen different cards now and the SanDisk were the only ones which didn't work always properly for me.
~ Tectu
~ Tectu
Re: stm32f4 discovery fatfs problem
I tried other cards but they dont even mount the card. When I debug the function (which gives me error), I released that I get:
FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume on the physical drive */
error.
This is the function.
FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume on the physical drive */
error.
Code: Select all
/*static*/
FRESULT chk_mounted ( /* FR_OK(0): successful, !=0: any error occurred */
const TCHAR **path, /* Pointer to pointer to the path name (drive number) */
FATFS **rfs, /* Pointer to pointer to the found file system object */
BYTE chk_wp /* !=0: Check media write protection for write access */
)
{
BYTE fmt, b, *tbl;
UINT vol;
DSTATUS stat;
DWORD bsect, fasize, tsect, sysect, nclst, szbfat;
WORD nrsv;
const TCHAR *p = *path;
FATFS *fs;
/* Get logical drive number from the path name */
vol = p[0] - '0'; /* Is there a drive number? */
if (vol <= 9 && p[1] == ':') { /* Found a drive number, get and strip it */
p += 2; *path = p; /* Return pointer to the path name */
} else { /* No drive number is given */
#if _FS_RPATH
vol = CurrVol; /* Use current drive */
#else
vol = 0; /* Use drive 0 */
#endif
}
/* Check if the logical drive is valid or not */
if (vol >= _VOLUMES) /* Is the drive number valid? */
return FR_INVALID_DRIVE;
*rfs = fs = FatFs[vol]; /* Return pointer to the corresponding file system object */
if (!fs) return FR_NOT_ENABLED; /* Is the file system object available? */
ENTER_FF(fs); /* Lock file system */
if (fs->fs_type) { /* If the logical drive has been mounted */
stat = disk_status(fs->drv);
if (!(stat & STA_NOINIT)) { /* and the physical drive is kept initialized (has not been changed), */
#if !_FS_READONLY
if (chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */
return FR_WRITE_PROTECTED;
#endif
return FR_OK; /* The file system object is valid */
}
}
/* The logical drive must be mounted. */
/* Following code attempts to mount a volume. (analyze BPB and initialize the fs object) */
fs->fs_type = 0; /* Clear the file system object */
fs->drv = (BYTE)LD2PD(vol); /* Bind the logical drive and a physical drive */
stat = disk_initialize(fs->drv); /* Initialize low level disk I/O layer */
if (stat & STA_NOINIT) /* Check if the initialization succeeded */
return FR_NOT_READY; /* Failed to initialize due to no media or hard error */
#if _MAX_SS != 512 /* Get disk sector size (variable sector size cfg only) */
if (disk_ioctl(fs->drv, GET_SECTOR_SIZE, &fs->ssize) != RES_OK)
return FR_DISK_ERR;
#endif
#if !_FS_READONLY
if (chk_wp && (stat & STA_PROTECT)) /* Check disk write protection if needed */
return FR_WRITE_PROTECTED;
#endif
/* Search FAT partition on the drive. Supports only generic partitionings, FDISK and SFD. */
fmt = check_fs(fs, bsect = 0); /* Check sector 0 if it is a VBR */
if (fmt == 1) { /* Not an FAT-VBR, the disk may be partitioned */
/* Check the partition listed in top of the partition table */
tbl = &fs->win[MBR_Table + LD2PT(vol) * SZ_PTE];/* Partition table */
if (tbl[4]) { /* Is the partition existing? */
bsect = LD_DWORD(&tbl[8]); /* Partition offset in LBA */
fmt = check_fs(fs, bsect); /* Check the partition */
}
}
if (fmt == 3) return FR_DISK_ERR;
if (fmt) return FR_NO_FILESYSTEM; /* No FAT volume is found */
/* Following code initializes the file system object */
if (LD_WORD(fs->win+BPB_BytsPerSec) != SS(fs)) /* (BPB_BytsPerSec must be equal to the physical sector size) */
return FR_NO_FILESYSTEM;
fasize = LD_WORD(fs->win+BPB_FATSz16); /* Number of sectors per FAT */
if (!fasize) fasize = LD_DWORD(fs->win+BPB_FATSz32);
fs->fsize = fasize;
fs->n_fats = b = fs->win[BPB_NumFATs]; /* Number of FAT copies */
if (b != 1 && b != 2) return FR_NO_FILESYSTEM; /* (Must be 1 or 2) */
fasize *= b; /* Number of sectors for FAT area */
fs->csize = b = fs->win[BPB_SecPerClus]; /* Number of sectors per cluster */
if (!b || (b & (b - 1))) return FR_NO_FILESYSTEM; /* (Must be power of 2) */
fs->n_rootdir = LD_WORD(fs->win+BPB_RootEntCnt); /* Number of root directory entries */
if (fs->n_rootdir % (SS(fs) / SZ_DIR)) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be sector aligned) */
tsect = LD_WORD(fs->win+BPB_TotSec16); /* Number of sectors on the volume */
if (!tsect) tsect = LD_DWORD(fs->win+BPB_TotSec32);
nrsv = LD_WORD(fs->win+BPB_RsvdSecCnt); /* Number of reserved sectors */
if (!nrsv) return FR_NO_FILESYSTEM; /* (BPB_RsvdSecCnt must not be 0) */
/* Determine the FAT sub type */
sysect = nrsv + fasize + fs->n_rootdir / (SS(fs) / SZ_DIR); /* RSV+FAT+DIR */
if (tsect < sysect) return FR_NO_FILESYSTEM; /* (Invalid volume size) */
nclst = (tsect - sysect) / fs->csize; /* Number of clusters */
if (!nclst) return FR_NO_FILESYSTEM; /* (Invalid volume size) */
fmt = FS_FAT12;
if (nclst >= MIN_FAT16) fmt = FS_FAT16;
if (nclst >= MIN_FAT32) fmt = FS_FAT32;
/* Boundaries and Limits */
fs->n_fatent = nclst + 2; /* Number of FAT entries */
fs->database = bsect + sysect; /* Data start sector */
fs->fatbase = bsect + nrsv; /* FAT start sector */
if (fmt == FS_FAT32) {
if (fs->n_rootdir) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be 0) */
fs->dirbase = LD_DWORD(fs->win+BPB_RootClus); /* Root directory start cluster */
szbfat = fs->n_fatent * 4; /* (Required FAT size) */
} else {
if (!fs->n_rootdir) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must not be 0) */
fs->dirbase = fs->fatbase + fasize; /* Root directory start sector */
szbfat = (fmt == FS_FAT16) ? /* (Required FAT size) */
fs->n_fatent * 2 : fs->n_fatent * 3 / 2 + (fs->n_fatent & 1);
}
if (fs->fsize < (szbfat + (SS(fs) - 1)) / SS(fs)) /* (BPB_FATSz must not be less than required) */
return FR_NO_FILESYSTEM;
#if !_FS_READONLY
/* Initialize cluster allocation information */
fs->free_clust = 0xFFFFFFFF;
fs->last_clust = 0;
/* Get fsinfo if available */
if (fmt == FS_FAT32) {
fs->fsi_flag = 0;
fs->fsi_sector = bsect + LD_WORD(fs->win+BPB_FSInfo);
if (disk_read(fs->drv, fs->win, fs->fsi_sector, 1) == RES_OK &&
LD_WORD(fs->win+BS_55AA) == 0xAA55 &&
LD_DWORD(fs->win+FSI_LeadSig) == 0x41615252 &&
LD_DWORD(fs->win+FSI_StrucSig) == 0x61417272) {
fs->last_clust = LD_DWORD(fs->win+FSI_Nxt_Free);
fs->free_clust = LD_DWORD(fs->win+FSI_Free_Count);
}
}
#endif
fs->fs_type = fmt; /* FAT sub-type */
fs->id = ++Fsid; /* File system mount ID */
fs->winsect = 0; /* Invalidate sector cache */
fs->wflag = 0;
#if _FS_RPATH
fs->cdir = 0; /* Current directory (root dir) */
#endif
#if _FS_SHARE /* Clear file lock semaphores */
clear_lock(fs);
#endif
return FR_OK;
}
This is the function.