The f_mkfs fucntion creates an FAT file system on the logical drive.
FRESULT f_mkfs ( const TCHAR* path, /* [IN] Logical drive number */ BYTE sfd, /* [IN] Partitioning rule */ UINT au /* [IN] Size of the allocation unit */ );
FR_OK, FR_DISK_ERR, FR_NOT_READY, FR_WRITE_PROTECTED, FR_INVALID_DRIVE, FR_NOT_ENABLED, FR_MKFS_ABORTED, FR_INVALID_PARAMETER
The f_mkfs() function creates an FAT volume on the specified logical drive. When FDISK format is specified, a primary partition occupies entire space of the physical drive is created and then an FAT volume is created on the partition. When SFD format is specified, the FAT volume starts from the first sector of the physical drive.
If the logical drive is bound to the specific partition (1-4) by multiple partition feature (_MULTI_PARTITION), the FAT volume is created into the partition. In this case, the second argument sfd is ignored. The physical drive must have been partitioned with f_fdisk() function or any other partitioning tools prior to create the FAT volume with this function.
Note that there are two partitioning rules, FDISK and SFD. The FDISK partitioning is usually used for harddisk, MMC, SDC, CFC and U Disk. It can divide a physical drive into one or more partitions with a partition table on the MBR. However Windows does not support multiple partition on the removable disk. The SFD is non-partitioned method. The FAT volume starts from the first sector on the physical drive without partition table. It is usually used for floppy disk, Microdrive, optical disk and any type of super-floppy media.
The FAT sub-type, FAT12/FAT16/FAT32, is determined by number of clusters on the volume and nothing else, according to the FAT specification issued by Microsoft. Thus which FAT sub-type is selected, is depends on the volume size and the specified cluster size. The cluster size affects read/write throughput and space usage efficiency. Larger cluster size increases the read/write throughput and decreases the space usage efficiency of the volume.
In case of the number of clusters gets near the FAT sub-type boundaries, the function can fail with FR_MKFS_ABORTED.
Available when _FS_READOLNY == 0 and _USE_MKFS == 1.
/* Format the default drive */ int main (void) { FATFS fs; /* File system object (volume work area) */ FIL fil; /* File object */ FRESULT res; /* API result code */ UINT bw; /* Bytes written */ /* Register work area */ f_mount(&fs, "", 0); /* Create FAT volume with default cluster size */ res = f_mkfs("", 0, 0); if (res) ... /* Create a file as new */ res = f_open(&fil, "hello.txt", FA_CREATE_NEW | FA_WRITE); if (res) ... /* Write a message */ f_write(&fil, "Hello, World!\r\n", 15, &bw); if (bw != 15) ... /* Close the file */ f_close(&fil); /* Unregister work area */ f_mount(0, "", 0);