00001
00002
00003
00004
00005
00006
00007
00008
00009 #if _FATFS != 8051
00010 #error This function may not be compatible with this revision of FatFs module.
00011 #endif
00012
00013
00014
00015 DWORD clust2sect (FATFS* fs, DWORD clst);
00016 DWORD get_fat (FATFS* fs, DWORD clst);
00017 FRESULT put_fat (FATFS* fs, DWORD clst, DWORD val);
00018
00019
00020 DWORD allocate_contiguous_clusters (
00021 FIL* fp,
00022 DWORD len
00023 )
00024 {
00025 DWORD csz, tcl, ncl, ccl, cl;
00026
00027
00028 if (f_lseek(fp, 0) || !len)
00029 return 0;
00030 csz = 512UL * fp->fs->csize;
00031 tcl = (len + csz - 1) / csz;
00032 len = tcl * csz;
00033
00034
00035 if (len == fp->fsize) {
00036 ncl = 0; ccl = fp->sclust;
00037 do {
00038 cl = get_fat(fp->fs, ccl);
00039 if (cl + 1 < 3) return 0;
00040 if (cl != ccl + 1 &&; cl < fp->fs->n_fatent) break;
00041 ccl = cl;
00042 } while (++ncl < tcl);
00043 if (ncl == tcl)
00044 return clust2sect(fp->fs, fp->sclust);
00045 }
00046 #if _FS_READONLY
00047 return 0;
00048 #else
00049 if (f_truncate(fp)) return 0;
00050
00051
00052 ccl = cl = 2; ncl = 0;
00053 do {
00054 if (cl >= fp->fs->n_fatent) return 0;
00055 if (get_fat(fp->fs, cl)) {
00056 do {
00057 cl++;
00058 if (cl >= fp->fs->n_fatent) return 0;
00059 } while (get_fat(fp->fs, cl));
00060 ccl = cl; ncl = 0;
00061 }
00062 cl++; ncl++;
00063 } while (ncl < tcl);
00064
00065
00066 fp->fs->last_clust = ccl - 1;
00067 if (f_lseek(fp, len)) return 0;
00068
00069 return clust2sect(fp->fs, fp->sclust);
00070 #endif
00071 }
00072
00073
00074 int main (void)
00075 {
00076 FRESULT fr;
00077 DRESULT dr;
00078 FATFS fs;
00079 FIL fil;
00080 DWORD org;
00081
00082
00083
00084 f_mount(&fs, "", 0);
00085 fr = f_open(&fil, "swapfile.sys", FA_READ | FA_WRITE | FA_OPEN_ALWAYS);
00086 if (fr) return 1;
00087
00088
00089
00090 org = allocate_contiguous_clusters(&fil, 0x4000000);
00091 if (!org) {
00092 printf("Function failed due to any error or insufficient contiguous area.\n");
00093 f_close(&fil);
00094 return 1;
00095 }
00096
00097
00098
00099 dr = disk_write(fil.fs->drv, Buff, org, 1024);
00100
00101 ...
00102
00103 f_close(&fil);
00104 return 0;
00105 }
00106