Milos RTOS v0.3.4a
Real Time Operating System
FatFS functions
Collaboration diagram for FatFS functions:

Functions

__FATFS_STATUS disk_initialize (u8 drv)
 Initializes the disk drive.
__FATFS_STATUS disk_status (BYTE drv)
 Returns the current disk status.
__FATFS_RES disk_ioctl (u8 drv, u8 ctrl, __PVOID buff)
 Controls device specified features and miscellaneous functions other than disk read/write.
__FATFS_RES disk_read (u8 drv, u8 *buff, u32 sector, u8 count)
 Reads sector(s) from the disk drive.
__FATFS_RES disk_write (BYTE drv, __CONST u8 *buff, u32 sector, u8 count)
 Writes sector(s) to the disk.
u32 get_fattime (__VOID)
 Gets current time.
__BOOL ff_cre_syncobj (u8 vol, _SYNC_t *sobj)
 FatFS synchronization object creation.
BOOL ff_del_syncobj (_SYNC_t sobj)
 FatFS deletes a synchronization object.
BOOL ff_req_grant (_SYNC_t sobj)
 FatFS requests grant to access the volume.
__VOID ff_rel_grant (_SYNC_t sobj)
 FatFS releases grant to access the volume.

Detailed Description

Fat functions.


Function Documentation

Initializes the disk drive.

Description from FatFs documentation:

The disk_initialize() function initializes a physical drive and put it ready to read/write. When the function succeeded, STA_NOINIT flag in the return value is cleared. Application program should not call this function, or FAT structure on the volume can be collapsed. To re-initialize the file system, use f_mount() function.This function is called on volume mount process in the FatFs module to manage the media change.

Parameters:
drvSpecifies the physical drive number to initialize.
Returns:
This function returns a disk status as the result. For details of the disk status, refer to the disk_status function.

Definition at line 809 of file fat.c.

{
    /* Get the device */
    __PDEVICE dv = __fatGetDevice(drv);
    if (!dv) return RES_NOTRDY;

    /* Call __deviceOpen. The device should return 0 on success.
     * The device should be already registered with __deviceRegister().
     */
    if (__deviceOpen(dv, 0) == 0) return RES_OK;
    return RES_ERROR;
}

Here is the call graph for this function:

__FATFS_STATUS disk_status ( BYTE  drv)

Returns the current disk status.

Description from FatFs documentation:

The disk status is returned in combination of following flags. FatFs refers only STA_NOINIT and STA_PROTECTED.

Parameters:
drvSpecifies the physical drive number to be confirmed.
Returns:
The disk status is returned in combination of following flags. FatFs refers only STA_NOINIT and STA_PROTECTED.
  • STA_NOINIT Indicates that the disk drive has not been initialized. This flag is set on: system reset, disk removal and disk_initialize function failed, and cleared on: disk_initialize function succeeded.
  • STA_NODISK Indicates that no medium in the drive. This is always cleared on fixed disk drive.
  • STA_PROTECTED Indicates that the medium is write protected. This is always cleared on the drive that does not support write protect notch. Not valid when STA_NODISK is set.

Definition at line 842 of file fat.c.

{
    __FATFS_STATUS ret = 0;
    __PDEVICE dv;

    /* Get the device */
    dv = __fatGetDevice(drv);
    if (!dv) return ret;

    switch (dv->dv_type)
    {
        /* SD/CF? */
        case __DEV_CFSD:
            if (__deviceIOCtl(dv, __IOCTL_WRITE_PROTECTED, 0, __NULL, 0))
            {
                ret |= STA_PROTECT;
            }

            if (!__deviceIOCtl(dv, __IOCTL_MEDIA_AVAILABLE, 0, __NULL, 0))
            {
                ret |= STA_NODISK;
            }
            break;
        default:
            /* TODO */
            ret = STA_NOINIT;
            break;
    }

    if (!dv->dv_initd || !dv->dv_opcnt)
    {
        ret |= STA_NOINIT;
    }
    return ret;
}

Here is the call graph for this function:

__FATFS_RES disk_ioctl ( u8  drv,
u8  ctrl,
__PVOID  buff 
)

Controls device specified features and miscellaneous functions other than disk read/write.

From FatFS documentation: The FatFs module uses only device independent commands described below. Any device dependent function is not used.

Parameters:
drvSpecifies the drive number (0-9).
ctrlSpecifies the command code.
  • CTRL_SYNC Make sure that the disk drive has finished pending write process. When the disk I/O module has a write back cache, flush the dirty sector immediately. This command is not used in read-only configuration.
  • GET_SECTOR_SIZE Returns sector size of the drive into the WORD variable pointed by Buffer. This command is not used in fixed sector size configuration, _MAX_SS is 512.
  • GET_SECTOR_COUNT Returns number of available sectors on the drive into the DWORD variable pointed by Buffer. This command is used by only f_mkfs function to determine the volume size to be created.
  • GET_BLOCK_SIZE Returns erase block size of the flash memory in unit of sector into the DWORD variable pointed by Buffer. The allowable value is 1 to 32768 in power of 2. Return 1 if the erase block size is unknown or disk devices. This command is used by only f_mkfs function and it attempts to align data area to the erase block boundary.
  • CTRL_ERASE_SECTOR Erases a part of the flash memory specified by a DWORD array {start sector, end sector} pointed by Buffer. When this feature is not supported or not a flash memory media, this command has no effect. The FatFs does not check the result code and the file function is not affected even if the sectors are not erased well. This command is called on removing a cluster chain when _USE_ERASE is 1.
buffPointer to the parameter buffer depends on the command code. When it is not used, specify a NULL pointer.

Definition at line 907 of file fat.c.

{
    __FATFS_RES res = RES_ERROR;
    u8* ptr = (u8*) buff;
    __PDEVICE dv;

    /* Get the device */
    dv = __fatGetDevice(drv);
    if (!dv) return res;

    /* SC/CF? */
    if (dv->dv_type == __DEV_CFSD)
    {
        /* Power control code */
        if (ctrl == CTRL_POWER)
        {
            switch (*ptr) {
                /* Power off */
                case 0:
                    if (__deviceIOCtl(dv, __IOCTL_GET_POWER_STATUS, 0, __NULL, 0)) {
                        __deviceIOCtl(dv, __IOCTL_SET_POWER_STATUS, 0, __NULL, 0);
                    }
                    res = RES_OK;
                    break;

                /* Power on */
                case 1:
                    __deviceIOCtl(dv, __IOCTL_SET_POWER_STATUS, 1, __NULL, 0);
                    res = RES_OK;
                    break;

                /* Get power status */
                case 2:
                    *(ptr+1) = (u8) __deviceIOCtl(dv, __IOCTL_GET_POWER_STATUS, 0, __NULL, 0);
                    res = RES_OK;
                    break;

                default:
                    res = RES_PARERR;
                    break;
            }
        } else
        {
            /* The next control codes need the device in ready state */
            if (disk_status(drv) & STA_NOINIT) return RES_NOTRDY;

            switch (ctrl)
            {
                /* Make sure that the disk drive has finished pending write process.
                 * When the disk I/O module has a write back cache, flush the dirty
                 * sector immediately. This command is not used in read-only configuration.
                 */
                case CTRL_SYNC:
                    /* TODO flush cache */
                    res = RES_OK;
                    break;

                /* Returns number of available sectors on the drive into the DWORD variable
                 * pointed by Buffer. This command is used by only f_mkfs function to determine
                 * the volume size to be created.
                 */
                case GET_SECTOR_COUNT:
                    *(u32*) buff = __deviceIOCtl(dv, __IOCTL_GET_SECTOR_COUNT, 0, __NULL, 0);
                    res = RES_OK;
                    break;

                /* Returns sector size of the drive into the WORD variable pointed by Buffer.
                 * This command is not used in fixed sector size configuration, _MAX_SS is 512.
                 */
                case GET_SECTOR_SIZE:
                    *(u16*) buff = 512;
                    res = RES_OK;
                    break;

                /* Returns erase block size of the flash memory in unit of sector into the
                 * DWORD variable pointed by Buffer. The allowable value is 1 to 32768 in power
                 * of 2. Return 1 if the erase block size is unknown or disk devices. This command
                 * is used by only f_mkfs function and it attempts to align data area to the erase
                 * block boundary.
                 */
                case GET_BLOCK_SIZE:
                    *(u32*) buff = __deviceIOCtl(dv, __IOCTL_GET_BLOCK_SIZE, 0, __NULL, 0);
                    res = RES_OK;
                    break;

                /* Get card type */
                case MMC_GET_TYPE:
                    *ptr = (u8) __deviceIOCtl(dv, __IOCTL_GET_CARD_TYPE, 0, __NULL, 0);
                    res = RES_OK;
                    break;

                case MMC_GET_CSD:
                     __deviceIOCtl(dv, __IOCTL_GET_CSD, 0, __NULL, 0);
                    res = RES_OK;
                    break;

                case MMC_GET_CID:
                    __deviceIOCtl(dv, __IOCTL_GET_CID, 0, __NULL, 0);
                    res = RES_OK;
                    break;

                case MMC_GET_OCR:
                    __deviceIOCtl(dv, __IOCTL_GET_OCR, 0, __NULL, 0);
                    res = RES_OK;
                    break;

                case MMC_GET_SDSTAT:
                    __deviceIOCtl(dv, __IOCTL_GET_SDSTAT, 0, __NULL, 0);
                    res = RES_OK;
                    break;

                default:
                    res = RES_PARERR;
                    break;
            }
        }
    }

    return res;
}

Here is the call graph for this function:

__FATFS_RES disk_read ( u8  drv,
u8 *  buff,
u32  sector,
u8  count 
)

Reads sector(s) from the disk drive.

Parameters:
drvSpecifies the physical drive number.
buffPointer to the byte array to store the read data. The buffer size of number of bytes to be read, sector size * sector count, is required. Note that the specified memory address is not that always aligned to word boundary. If the hardware does not support misaligned data transfer, it must be solved in this function.
sectorSpecifies the start sector number in logical block address (LBA).
countSpecifies number of sectors to read. The value can be 1 to 128. Generally, a multiple sector transfer request must not be split into single sector transactions to the device, or you may not get good read performance (TODO).
Returns:
  • RES_OK (0) The function succeeded.
  • RES_ERROR Any hard error occured during the read operation and could not recover it.
  • RES_PARERR Invalid parameter.
  • RES_NOTRDY The disk drive has not been initialized.
__TRUE on success, otherwise __FALSE.

Definition at line 1052 of file fat.c.

{
    __PDEVICE dv;

    /* Get the device */
    dv = __fatGetDevice(drv);
    if (!dv) return RES_PARERR;

    /* Set the sector to read */
    if (dv->dv_type == __DEV_CFSD) __deviceIOCtl(dv, __IOCTL_SET_SECTOR, sector, __NULL, 0);
    if (__deviceRead(dv, buff, count) == 0) return RES_OK;

    return RES_ERROR;
}

Here is the call graph for this function:

__FATFS_RES disk_write ( BYTE  drv,
__CONST u8 *  buff,
u32  sector,
u8  count 
)

Writes sector(s) to the disk.

Parameters:
drvSpecifies the physical drive number.
buffPointer to the byte array to be written. Note that the specified memory address is not that always aligned to word boundary. If the hardware does not support misaligned data transfer, it must be solved in this function.
sectorSpecifies the start sector number in logical block address (LBA).
countSpecifies the number of sectors to write. The value can be 1 to 128. Generally, a multiple sector transfer request must not be split into single sector transactions to the device, or you will never get good write performance (TODO).
Returns:
  • RES_OK (0) The function succeeded.
  • RES_ERROR Any hard error occured during the read operation and could not recover it.
  • RES_PARERR Invalid parameter.
  • RES_NOTRDY The disk drive has not been initialized.
  • RES_OK (0) The function succeeded.
  • RES_ERROR Any hard error occured during the write operation and could not recover it.
  • RES_WRPRT The medium is write protected.
  • RES_PARERR Invalid parameter.
  • RES_NOTRDY The disk drive has not been initialized.

Definition at line 1093 of file fat.c.

{
    __PDEVICE dv;

    /* Get the device */
    dv = __fatGetDevice(drv);
    if (!dv) return RES_PARERR;

    if (dv->dv_type == __DEV_CFSD)
    {
        if (__deviceIOCtl(dv, __IOCTL_WRITE_PROTECTED, 0, __NULL, 0))
        {
            return RES_WRPRT;
        }
    }

    /* Set the sector to write */
    if (dv->dv_type == __DEV_CFSD)
    {
        __deviceIOCtl(dv, __IOCTL_SET_SECTOR, sector, __NULL, 0);
    }

    /* Write */
    if (__deviceWrite(dv, buff, count) == 0) return RES_OK;

    return RES_ERROR;
}

Here is the call graph for this function:

u32 get_fattime ( __VOID  )

Gets current time.

The get_fattime function must return any valid time even if the system does not support a real time clock. If a zero is returned, the file will not have a valid time. This function is not required in read only configuration.

Returns:
Current time is returned with packed into a DWORD value. The bit field is as follows: bit31:25 Year from 1980 (0..127) bit24:21 Month (1..12) bit20:16 Day in month(1..31) bit15:11 Hour (0..23) bit10:5 Minute (0..59) bit4:0 Second / 2 (0..29)

Definition at line 1143 of file fat.c.

{

#if __CONFIG_COMPILE_RTC
    __DATETIME dt;

    __rtcGetDateTime(&dt);

    return ((dt.year - 1980) << 25  |
             dt.month << 21         |
             dt.day << 16           |
             dt.hour << 11          |
             dt.min << 5            |
             dt.sec);

#else

    /* fixed date-time */
    return (30 << 25 | 12 << 21 | 30 << 16 | 19 << 11 | 2 << 5 | 50);

#endif /* __CONFIG_COMPILE_RTC */

}

Here is the call graph for this function:

__BOOL ff_cre_syncobj ( u8  vol,
_SYNC_t *  sobj 
)

FatFS synchronization object creation.

From FatFS description: this function is called in f_mount function to create a new synchronization object, such as semaphore and mutex. When a FALSE is returned, the f_mount function fails with FR_INT_ERR.

Parameters:
volCorresponding logical drive being processed.
sobjPointer to return the created sync object.
Returns:
__TRUE on success, otherwise __FALSE.

Definition at line 1181 of file fat.c.

{
    __BOOL ret = __FALSE;

    *sobj = __lockCreate();
    ret = (*sobj != __NULL) ? __TRUE : __FALSE;

    return ret;
}

Here is the call graph for this function:

BOOL ff_del_syncobj ( _SYNC_t  sobj)

FatFS deletes a synchronization object.

From FatFS description: This function is called in f_mount function to delete a synchronization object that created with ff_cre_syncobj function. When a FALSE is returned, the f_mount function fails with FR_INT_ERR.

Parameters:
sobjSync object tied to the logical drive to be deleted.
Returns:
__TRUE on success, otherwise __FALSE.

Definition at line 1204 of file fat.c.

                                   {
    __lockDestroy(sobj);
    return __TRUE;
}

Here is the call graph for this function:

BOOL ff_req_grant ( _SYNC_t  sobj)

FatFS requests grant to access the volume.

From FatFS description: This function is called on entering file functions to lock the volume. When a FALSE is returned, the file function fails with FR_TIMEOUT.

Parameters:
sobjSync object to wait.
Returns:
__TRUE on success, otherwise __FALSE.

Definition at line 1221 of file fat.c.

                                {
__BOOL ret;

    ret = __lockOwn(sobj, _FS_TIMEOUT);
    return ret;
}

Here is the call graph for this function:

__VOID ff_rel_grant ( _SYNC_t  sobj)

FatFS releases grant to access the volume.

From FatFS description: This function is called on leaving file functions to unlock the volume.

Parameters:
sobjSync object to be signaled.
Returns:
Nothing.

Definition at line 1239 of file fat.c.

{
    __lockRelease(sobj);
}

Here is the call graph for this function:

 All Data Structures Files Functions Variables Typedefs Defines