📄 tff.lst
字号:
C51 COMPILER V8.08 TFF 10/31/2008 14:44:16 PAGE 15
ST_WORD(&dir[DIR_FstClusHI], 0);
#else
rs = LD_WORD(&dir[DIR_FstClusLO]);
#endif
ST_WORD(&dir[DIR_FstClusLO], 0); /* cluster = 0 */
ST_DWORD(&dir[DIR_FileSize], 0); /* size = 0 */
dj.fs->winflag = 1;
dw = dj.fs->winsect; /* Remove the cluster chain */
if (!remove_chain(rs) || !move_window(dw))
return FR_RW_ERROR;
dj.fs->last_clust = rs - 1; /* Reuse the cluster hole */
}
}
if (mode & FA_CREATE_ALWAYS) {
dir[DIR_Attr] = 0; /* Reset attribute */
dw = get_fattime();
ST_DWORD(&dir[DIR_CrtTime], dw); /* Created time */
dj.fs->winflag = 1;
mode |= FA__WRITTEN; /* Set file changed flag */
}
}
/* Open an existing file */
else {
#endif /* !_FS_READONLY */
883 1 if (res != FR_OK) return res; /* Trace failed */
884 1 if (!dir || (dir[DIR_Attr] & AM_DIR)) /* It is a directory */
885 1 return FR_NO_FILE;
886 1 #if !_FS_READONLY
if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */
return FR_DENIED;
}
fp->dir_sect = dj.fs->winsect; /* Pointer to the directory entry */
fp->dir_ptr = dir;
#endif
893 1 fp->flag = mode; /* File access mode */
894 1 fp->org_clust = /* File start cluster */
895 1 #if _FAT32
896 1 ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) |
897 1 #endif
898 1 LD_WORD(&dir[DIR_FstClusLO]);
899 1 fp->fsize = LD_DWORD(&dir[DIR_FileSize]); /* File size */
900 1 fp->fptr = 0; fp->csect = 255; /* File pointer */
901 1 fp->fs = dj.fs; fp->id = dj.fs->id; /* Owner file system object of the file */
902 1
903 1 return FR_OK;
904 1 }
905
906
907
908
909 /*-----------------------------------------------------------------------*/
910 /* Read File */
911 /*-----------------------------------------------------------------------*/
912
913 FRESULT f_read (
914 FIL *fp, /* Pointer to the file object */
915 void *buff, /* Pointer to data buffer */
916 UINT btr, /* Number of bytes to read */
917 UINT *br /* Pointer to number of bytes read */
918 )
919 {
920 1 FRESULT res;
C51 COMPILER V8.08 TFF 10/31/2008 14:44:16 PAGE 16
921 1 DWORD sect, remain;
922 1 UINT rcnt, cc;
923 1 CLUST clust;
924 1 BYTE *rbuff = buff;
925 1
926 1
927 1 *br = 0;
928 1 res = validate(fp->fs, fp->id); /* Check validity of the object */
929 1 if (res != FR_OK) return res;
930 1 if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
931 1 if (!(fp->flag & FA_READ)) return FR_DENIED; /* Check access mode */
932 1 remain = fp->fsize - fp->fptr;
933 1 if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
934 1
935 1 for ( ; btr; /* Repeat until all data transferred */
936 1 rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
937 2 if ((fp->fptr % 512U) == 0) { /* On the sector boundary? */
938 3 if (fp->csect >= fp->fs->csize) { /* On the cluster boundary? */
939 4 clust = (fp->fptr == 0) ? /* On the top of the file? */
940 4 fp->org_clust : get_cluster(fp->curr_clust);
941 4 if (clust < 2 || clust >= fp->fs->max_clust) goto fr_error;
942 4 fp->curr_clust = clust; /* Update current cluster */
943 4 fp->csect = 0; /* Reset sector address in the cluster */
944 4 }
945 3 sect = clust2sect(fp->curr_clust) + fp->csect; /* Get current sector */
946 3 cc = btr / 512U; /* When remaining bytes >= sector size, */
947 3 if (cc) { /* Read maximum contiguous sectors directly */
948 4 if (fp->csect + cc > fp->fs->csize) /* Clip at cluster boundary */
949 4 cc = fp->fs->csize - fp->csect;
950 4 if (disk_read(0, rbuff, sect, (BYTE)cc) != RES_OK)
951 4 goto fr_error;
952 4 fp->csect += (BYTE)cc; /* Next sector address in the cluster */
953 4 rcnt = 512U * cc; /* Number of bytes transferred */
954 4 continue;
955 4 }
956 3 fp->csect++; /* Next sector address in the cluster */
957 3 }
958 2 sect = clust2sect(fp->curr_clust) + fp->csect - 1; /* Get current sector */
959 2 if (!move_window(sect)) goto fr_error; /* Move sector window */
960 2 rcnt = 512U - (fp->fptr % 512U); /* Get partial sector from sector window */
961 2 if (rcnt > btr) rcnt = btr;
962 2 memcpy(rbuff, &fp->fs->win[fp->fptr % 512U], rcnt);
963 2 }
964 1
965 1 return FR_OK;
966 1
967 1 fr_error: /* Abort this file due to an unrecoverable error */
968 1 fp->flag |= FA__ERROR;
969 1 return FR_RW_ERROR;
970 1 }
971
972
973
974
975 #if !_FS_READONLY
/*-----------------------------------------------------------------------*/
/* Write File */
/*-----------------------------------------------------------------------*/
FRESULT f_write (
FIL *fp, /* Pointer to the file object */
const void *buff, /* Pointer to the data to be written */
C51 COMPILER V8.08 TFF 10/31/2008 14:44:16 PAGE 17
UINT btw, /* Number of bytes to write */
UINT *bw /* Pointer to number of bytes written */
)
{
FRESULT res;
DWORD sect;
UINT wcnt, cc;
CLUST clust;
const BYTE *wbuff = buff;
*bw = 0;
res = validate(fp->fs, fp->id); /* Check validity of the object */
if (res != FR_OK) return res;
if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
if (!(fp->flag & FA_WRITE)) return FR_DENIED; /* Check access mode */
if (fp->fsize + btw < fp->fsize) return FR_OK; /* File size cannot reach 4GB */
for ( ; btw; /* Repeat until all data transferred */
wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
if ((fp->fptr % 512U) == 0) { /* On the sector boundary? */
if (fp->csect >= fp->fs->csize) { /* On the cluster boundary? */
if (fp->fptr == 0) { /* On the top of the file? */
clust = fp->org_clust; /* Follow from the origin */
if (clust == 0) /* When there is no cluster chain, */
fp->org_clust = clust = create_chain(0); /* Create a new cluster chain */
} else { /* Middle or end of the file */
clust = create_chain(fp->curr_clust); /* Trace or streach cluster chain */
}
if (clust == 0) break; /* Could not allocate a new cluster (disk full) */
if (clust == 1 || clust >= fp->fs->max_clust) goto fw_error;
fp->curr_clust = clust; /* Update current cluster */
fp->csect = 0; /* Reset sector address in the cluster */
}
sect = clust2sect(fp->curr_clust) + fp->csect; /* Get current sector */
cc = btw / 512U; /* When remaining bytes >= sector size, */
if (cc) { /* Write maximum contiguous sectors directly */
if (fp->csect + cc > fp->fs->csize) /* Clip at cluster boundary */
cc = fp->fs->csize - fp->csect;
if (disk_write(0, wbuff, sect, (BYTE)cc) != RES_OK)
goto fw_error;
fp->csect += (BYTE)cc; /* Next sector address in the cluster */
wcnt = 512U * cc; /* Number of bytes transferred */
continue;
}
if (fp->fptr >= fp->fsize) { /* Flush R/W window without reading if needed */
if (!move_window(0)) goto fw_error;
fp->fs->winsect = sect;
}
fp->csect++; /* Next sector address in the cluster */
}
sect = clust2sect(fp->curr_clust) + fp->csect - 1; /* Get current sector */
if (!move_window(sect)) goto fw_error; /* Move sector window */
wcnt = 512U - (fp->fptr % 512U); /* Put partial sector into sector window */
if (wcnt > btw) wcnt = btw;
memcpy(&fp->fs->win[fp->fptr % 512U], wbuff, wcnt);
fp->fs->winflag = 1;
}
if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */
fp->flag |= FA__WRITTEN; /* Set file changed flag */
return res;
C51 COMPILER V8.08 TFF 10/31/2008 14:44:16 PAGE 18
fw_error: /* Abort this file due to an unrecoverable error */
fp->flag |= FA__ERROR;
return FR_RW_ERROR;
}
/*-----------------------------------------------------------------------*/
/* Synchronize the file object */
/*-----------------------------------------------------------------------*/
FRESULT f_sync (
FIL *fp /* Pointer to the file object */
)
{
FRESULT res;
DWORD tim;
BYTE *dir;
res = validate(fp->fs, fp->id); /* Check validity of the object */
if (res == FR_OK) {
if (fp->flag & FA__WRITTEN) { /* Has the file been written? */
/* Update the directory entry */
if (!move_window(fp->dir_sect))
return FR_RW_ERROR;
dir = fp->dir_ptr;
dir[DIR_Attr] |= AM_ARC;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -