📄 tff.lst
字号:
704 FIL *fp, /* Pointer to the file object */
705 void *buff, /* Pointer to data buffer */
706 WORD btr, /* Number of bytes to read */
707 WORD *br /* Pointer to number of bytes read */
708 )
709 {
710 1 DWORD sect, ln;
711 1 WORD clust, rcnt;
712 1 BYTE cc, *rbuff = buff;
713 1 FATFS *fs = FatFs;
714 1
715 1
716 1 *br = 0;
717 1 if (!fs) return FR_NOT_ENABLED;
718 1 //if ((disk_status() & STA_NOINIT) || !fs->fs_type) return FR_NOT_READY; /* Check disk ready */
719 1 if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
720 1 if (!(fp->flag & FA_READ)) return FR_DENIED; /* Check access mode */
721 1 ln = fp->fsize - fp->fptr;
722 1 if (btr > ln) btr = (WORD)ln; /* Truncate read count by number of bytes left */
723 1 // btr = ln;
724 1 for ( ; btr; /* Repeat until all data transferred */
725 1 rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
726 2 if ((fp->fptr % 512) == 0) { /* On the sector boundary */
727 3 if (--(fp->sect_clust)) { /* Decrement sector counter */
728 4 sect = fp->curr_sect + 1; /* Next sector */
729 4 } else { /* Next cluster */
730 4 clust = (fp->fptr == 0) ? fp->org_clust : get_cluster(fp->curr_clust);
731 4 if ((clust < 2) || (clust >= fs->max_clust)) goto fr_error;
732 4 fp->curr_clust = clust; /* Current cluster */
733 4 sect = clust2sect(clust); /* Current sector */
734 4 fp->sect_clust = fs->sects_clust; /* Re-initialize the sector counter */
735 4 }
736 3 fp->curr_sect = sect; /* Update current sector */
C51 COMPILER V7.02b TFF 03/24/2008 15:52:29 PAGE 13
737 3 cc = btr / 512; /* When left bytes >= 512 */
738 3 if (cc) { /* Read maximum contiguous sectors */
739 4 if (cc > fp->sect_clust) cc = fp->sect_clust;
740 4 if (disk_read(rbuff, sect, cc) != RES_OK) goto fr_error;
741 4 fp->sect_clust -= cc - 1;
742 4 fp->curr_sect += cc - 1;
743 4 rcnt = cc * 512; continue;
744 4 }
745 3 }
746 2 if (!move_window(fp->curr_sect)) goto fr_error; /* Move sector window */
747 2 rcnt = 512 - (fp->fptr % 512); /* Copy fractional bytes from sector window */
748 2 if (rcnt > btr) rcnt = btr;
749 2 memcpy(rbuff, &fs->win[fp->fptr % 512], rcnt);
750 2 }
751 1
752 1 return FR_OK;
753 1
754 1 fr_error: /* Abort this function due to an unrecoverable error */
755 1 fp->flag |= FA__ERROR;
756 1 return FR_RW_ERROR;
757 1 }
758
759
760
761 /*------------*/
762 /* Write File */
763
764 #ifndef _FS_READONLY
765 FRESULT f_write (
766 FIL *fp, /* Pointer to the file object */
767 const void *buff, /* Pointer to the data to be written */
768 WORD btw, /* Number of bytes to write */
769 WORD *bw /* Pointer to number of bytes written */
770 )
771 {
772 1 DWORD sect;
773 1 WORD clust, wcnt;
774 1 BYTE cc;
775 1 const BYTE *wbuff = buff;
776 1 FATFS *fs = FatFs;
777 1
778 1
779 1 *bw = 0;
780 1 if (!fs) return FR_NOT_ENABLED;
781 1 // if ((disk_status() & STA_NOINIT) || !fs->fs_type) return FR_NOT_READY;
782 1 if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
783 1 if (!(fp->flag & FA_WRITE)) return FR_DENIED; /* Check access mode */
784 1 if (fp->fsize + btw < fp->fsize) btw = 0; /* File size cannot reach 4GB */
785 1
786 1 for ( ; btw; /* Repeat until all data transferred */
787 1 wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
788 2 if ((fp->fptr % 512) == 0) { /* On the sector boundary */
789 3 if (--(fp->sect_clust)) { /* Decrement sector counter */
790 4 sect = fp->curr_sect + 1; /* Next sector */
791 4 } else { /* Next cluster */
792 4 if (fp->fptr == 0) { /* Top of the file */
793 5 clust = fp->org_clust;
794 5 if (clust == 0) /* No cluster is created */
795 5 fp->org_clust = clust = create_chain(0); /* Create a new cluster chain */
796 5 } else { /* Middle or end of file */
797 5 clust = create_chain(fp->curr_clust); /* Trace or streach cluster chain */
798 5 }
C51 COMPILER V7.02b TFF 03/24/2008 15:52:29 PAGE 14
799 4 if ((clust < 2) || (clust >= fs->max_clust)) break;
800 4 fp->curr_clust = clust; /* Current cluster */
801 4 sect = clust2sect(clust); /* Current sector */
802 4 fp->sect_clust = fs->sects_clust; /* Re-initialize the sector counter */
803 4 }
804 3 fp->curr_sect = sect; /* Update current sector */
805 3 cc = btw / 512; /* When left bytes >= 512 */
806 3 if (cc) { /* Write maximum contiguous sectors */
807 4 if (cc > fp->sect_clust) cc = fp->sect_clust;
808 4 if (disk_write(wbuff, sect, cc) != RES_OK) goto fw_error;
809 4 fp->sect_clust -= cc - 1;
810 4 fp->curr_sect += cc - 1;
811 4 wcnt = cc * 512; continue;
812 4 }
813 3 if (fp->fptr >= fp->fsize) { /* Flush R/W window if needed */
814 4 if (!move_window(0)) goto fw_error;
815 4 fs->winsect = fp->curr_sect;
816 4 }
817 3 }
818 2 if (!move_window(fp->curr_sect)) goto fw_error; /* Move sector window */
819 2 wcnt = 512 - (fp->fptr % 512); /* Copy fractional bytes bytes to sector window */
820 2 if (wcnt > btw) wcnt = btw;
821 2 memcpy(&fs->win[fp->fptr % 512], wbuff, wcnt);
822 2 fs->winflag = 1;
823 2 }
824 1
825 1 if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */
826 1 fp->flag |= FA__WRITTEN; /* Set file changed flag */
827 1 return FR_OK;
828 1
829 1 fw_error: /* Abort this function due to an unrecoverable error */
830 1 fp->flag |= FA__ERROR;
831 1 return FR_RW_ERROR;
832 1 }
833 #endif /* _FS_READONLY */
834
835
836
837 /*-------------------*/
838 /* Seek File Pointer */
839 #if 0
FRESULT f_lseek (
FIL *fp, /* Pointer to the file object */
DWORD ofs /* File pointer from top of file */
)
{
WORD clust;
BYTE sc;
FATFS *fs = FatFs;
if (!fs) return FR_NOT_ENABLED;
if ((disk_status() & STA_NOINIT) || !fs->fs_type) return FR_NOT_READY;
if (fp->flag & FA__ERROR) return FR_RW_ERROR;
if (ofs > fp->fsize) ofs = fp->fsize; /* Clip offset by file size */
fp->fptr = ofs; fp->sect_clust = 1; /* Re-initialize file pointer */
/* Seek file pinter if needed */
if (ofs) {
ofs = (ofs - 1) / 512; /* Calcurate current sector */
sc = fs->sects_clust; /* Number of sectors in a cluster */
fp->sect_clust = sc - (ofs % sc); /* Calcurate sector counter */
C51 COMPILER V7.02b TFF 03/24/2008 15:52:29 PAGE 15
ofs /= sc; /* Number of clusters to skip */
clust = fp->org_clust; /* Seek to current cluster */
while (ofs--)
clust = get_cluster(clust);
if ((clust < 2) || (clust >= fs->max_clust)) goto fk_error;
fp->curr_clust = clust;
fp->curr_sect = clust2sect(clust) + sc - fp->sect_clust; /* Current sector */
}
return FR_OK;
fk_error: /* Abort this function due to an unrecoverable error */
fp->flag |= FA__ERROR;
return FR_RW_ERROR;
}
#endif
878
879 /*-------------------------------------------------*/
880 /* Synchronize between File and Disk without Close */
881
882 #ifndef _FS_READONLY
883 FRESULT f_sync (
884 FIL *fp /* Pointer to the file object */
885 )
886 {
887 1 BYTE *ptr;
888 1 FATFS *fs = FatFs;
889 1
890 1
891 1 if (!fs) return FR_NOT_ENABLED;
892 1 if ((disk_status() & STA_NOINIT) || !fs->fs_type)
893 1 return FR_INCORRECT_DISK_CHANGE;
894 1
895 1 /* Has the file been written? */
896 1 if (fp->flag & FA__WRITTEN) {
897 2 if (!move_window(fp->dir_sect)) return FR_RW_ERROR;
898 2 ptr = fp->dir_ptr;
899 2 *(ptr+11) |= AM_ARC; /* Set archive bit */
900 2 ST_DWORD(ptr+28, fp->fsize); /* Update file size */
901 2 ST_WORD(ptr+26, fp->org_clust); /* Update start cluster */
902 2 // ST_DWORD(ptr+22, get_fattime()); /* Updated time */
903 2 fs->winflag = 1;
904 2 fp->flag &= ~FA__WRITTEN;
905 2 }
906 1 if (!move_window(0)) return FR_RW_ERROR;
907 1
908 1 return FR_OK;
909 1 }
910 #endif /* _FS_READONLY */
911
912
913
914 /*------------*/
915 /* Close File */
916
917 FRESULT f_close (
918 FIL *fp /* Pointer to the file object to be closed */
919 )
920 {
921 1 FRESULT res;
922 1
C51 COMPILER V7.02b TFF 03/24/2008 15:52:29 PAGE 16
923 1
924 1 #ifndef _FS_READONLY
925 1 res = f_sync(fp);
926 1 #else
res = FR_OK;
#endif
929 1 if (res == FR_OK) {
930 2 fp->flag = 0;
931 2 FatFs->files--;
932 2 }
933 1 return res;
934 1 }
935
936
937
938 #ifndef _FS_MINIMUM
939 /*----------------------------*/
940 /* Delete a File or Directory */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -