fatfs 学习笔记--f_mkfs用法

f_mkfs

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 */
);

Parameters

path
Pinter to the null-terminated string that specifies the logical drive to be formatted. If there is no drive number, it means the default drive.
sfd
Specifies partitioning rule (FDISK(0) or SFD(1)). This argument will be ignored on some case.
au
Specifies size of the allocation unit (cluter) in number of bytes or sectors. When the value is from 1 to 128, it specifies number of sectors. When the value is >= _MIN_SS, it specifies number of bytes. If any invalid value, zero or not power of 2, is given, the cluster size is automatically determined depends on the volume size.

Return Values

FR_OK,FR_DISK_ERR, FR_NOT_READY, FR_WRITE_PROTECTED, FR_INVALID_DRIVE, FR_NOT_ENABLED, FR_MKFS_ABORTED, FR_INVALID_PARAMETER

Description

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 argumentsfd is ignored. The physical drive must have been partitioned withf_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 withFR_MKFS_ABORTED.

QuickInfo

Available when _FS_READOLNY == 0 and _USE_MKFS == 1.

Example

/* 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);

你可能感兴趣的:(fatfs文件系统)