这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » DIY与开源设计 » 电子DIY » STM32 上FATFS文件系统移植問題(一)----- f_mkfs 求解(跪

共1条 1/1 1 跳转至

STM32 上FATFS文件系统移植問題(一)----- f_mkfs 求解(跪謝)

工程师
2013-09-08 15:03:08     打赏

 在按照论坛上给的野火 零死角玩转STM32-高级篇2 文件系统进行相关操作时,搞了3个星期,都没弄通。现在主要问题是:主函数中調用 res=f_open(&fsrc,"0:/Demo.TXT",FA_CREATE_ALWAYS |FA_WRITE);

就報open file error :13 沒有文件系統。

接著就一步一步分析程序,其中到如下這個創建文件系統的文件,遇到幾個問題,還請論壇里,搞過這個的指導一下。主要問題就是 n_fat 偏移地址的計算;先謝了。

用的是KINGSTON  2G SD 卡,

FRESULT f_mkfs (
BYTE drv, /* Logical drive number */
BYTE partition, /* Partitioning rule 0:FDISK, 1:SFD */  選的是1 SFD,我想0扇區并沒有MBR
WORD allocsize /* Allocation unit size [bytes] */  一個簇為8個扇區,從WINHEX得到
)
{
static const DWORD sstbl[] = { 2048000, 1024000, 512000, 256000, 128000, 64000, 32000, 16000, 8000, 4000,   0 };
static const WORD cstbl[] =  {   32768,   16384,   8192,   4096,   2048, 16384,  8192,  4096, 2048, 1024, 512 };
BYTE fmt, m, *tbl;
DWORD b_part, b_fat, b_dir, b_data; /* Area offset (LBA) */
DWORD n_part, n_rsv, n_fat, n_dir; /* Area size */
DWORD n_clst, d, n;
WORD as;
FATFS *fs;
DSTATUS stat;
/* Check validity of the parameters */
if (drv >= _DRIVES) return FR_INVALID_DRIVE;

if (partition >= 2) return FR_MKFS_ABORTED;


/* Check mounted drive and clear work area */
fs = FatFs[drv];
if (!fs) return FR_NOT_ENABLED;
fs->fs_type = 0;
drv = LD2PD(drv);


/* Get disk statics */
stat = disk_initialize(drv);
if (stat & STA_NOINIT) return FR_NOT_READY;
if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
#if _MAX_SS != 512 /* Get disk sector size */
if (disk_ioctl(drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK
|| SS(fs) > _MAX_SS)
return FR_MKFS_ABORTED;
#endif
if (disk_ioctl(drv, GET_SECTOR_COUNT, &n_part) != RES_OK || n_part < MIN_SECTOR)
return FR_MKFS_ABORTED;
printf(" sector_number=%d\r\n",n_part);
if (n_part > MAX_SECTOR) n_part = MAX_SECTOR;
/*n_part 總的扇區數*/
b_part = (!partition) ? 63 : 0; /* Boot sector *///認為此部份是MBR區的大小碼?
/*b_part 要是為64個扇區,32K,一個簇的最大容量*/
n_part -= b_part;
/*allocsize 一個簇有多少個扇區,應為8*/
for (d = 512; d <= 32768U && d != allocsize; d <<= 1) ; /* Check validity of the allocation unit size */
if (d != allocsize) allocsize = 0;
if (!allocsize) { /* Auto selection of cluster size */
d = n_part;
for (as = SS(fs); as > 512U; as >>= 1) d >>= 1;
/*一個簇的大小,好像有毛病,沒法定位到allocsize=8,這些是自動進行一個簇大小的計算,先不用管*/
for (n = 0; d < sstbl[n]; n++) ;
allocsize = cstbl[n];
}
if (allocsize < SS(fs)) allocsize = SS(fs);
allocsize /= SS(fs); /* Number of sectors per cluster */


/* Pre-compute number of clusters and FAT type */

 n_part,總的扇區數量3854336,這個與Winhex中一致,但這個是多少容量呢?每個簇8個扇區,4K,n_clst計算的值481792,與Winhex得到的480848,相差944944*4K=3.6875M,與兩個FAT表的大小1.8M相符,也就是Winhex中得到的總簇數實際為:這裡算的總簇數—FAT表 所占扇區數折合簇數-保留區扇區數折合的簇數,所以開頭是預計算,不知道是不是這個道理*/

n_clst = n_part / allocsize;
printf("n_clst=%d\r\n",n_clst);
fmt = FS_FAT12;
if (n_clst >= 0xFF5) fmt = FS_FAT16;
if (n_clst >= 0xFFF5) fmt = FS_FAT32;

/* Determine offset and size of FAT structure */
switch (fmt) {
case FS_FAT12:
n_fat = ((n_clst * 3 + 1) / 2 + 3 + SS(fs) - 1) / SS(fs);
n_rsv = 1 + partition;
n_dir = N_ROOTDIR * 32 / SS(fs);
break;
case FS_FAT16:
n_fat = ((n_clst * 2) + 4 + SS(fs) - 1) / SS(fs);
n_rsv = 1 + partition;
n_dir = N_ROOTDIR * 32 / SS(fs);
break;

default:

//計算FAT 含有的扇區數,why is it? 3757是從winhex得來,看是否能對應上

 N_clst 總簇數從Winhex 中得到為480848個。N_clst得到得是481792,按這樣計算的話一個FAT表項占的扇區數為3765,而從Winhex得到的一個FAT表項所占的扇區數為3757,相差8個扇區;

n_fat = ((n_clst * 4) + 8 + SS(fs) - 1) / SS(fs);
  printf("n_fat1=%d\r\n",n_fat);
//n_rsv = 33 - partition;
n_rsv = 39 - partition;//從Winhex得到保留扇區為38個,
n_dir = 0;
 break;//原來沒有此句
}
b_fat = b_part + n_rsv; /* FATs start sector */
//b_fat = n_rsv;
b_dir = b_fat + n_fat * N_FATS; /* Directory start sector */
b_data = b_dir + n_dir; /* Data start sector=38+3757*2=7552=0X1D80 */
printf("b_data=%d\r\n",b_data);
/* Align data start sector to erase block boundary (for flash memory media) */
if (disk_ioctl(drv, GET_BLOCK_SIZE, &n) != RES_OK) return FR_MKFS_ABORTED;
n = (b_data + n - 1) & ~(n - 1);
n_fat += (n - b_data) / N_FATS;//為什麽呢?
printf("n_fat2=%d\r\n",n_fat);

/* b_dir and b_data are no longer used below */

/* Determine number of cluster and final check of validity of the FAT type */
n_clst = (n_part - n_rsv - n_fat * N_FATS - n_dir) / allocsize;
if (   (fmt == FS_FAT16 && n_clst < 0xFF5)
|| (fmt == FS_FAT32 && n_clst < 0xFFF5))
return FR_MKFS_ABORTED;

 附上源文件,請大俠們幫忙看看,到底什麽原因?跪謝了

Test_12_SDIO(with FILE).rar






关键词: STM32     FATFS     f_mkfs    

共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]