123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- #if _FATFS != 8051
- #error This function may not be compatible with this revision of FatFs module.
- #endif
- DWORD clust2sect (FATFS* fs, DWORD clst);
- DWORD get_fat (FATFS* fs, DWORD clst);
- FRESULT put_fat (FATFS* fs, DWORD clst, DWORD val);
- DWORD allocate_contiguous_clusters (
- FIL* fp,
- DWORD len
- )
- {
- DWORD csz, tcl, ncl, ccl, cl;
- if (f_lseek(fp, 0) || !len)
- return 0;
- csz = 512UL * fp->fs->csize;
- tcl = (len + csz - 1) / csz;
- len = tcl * csz;
-
- if (len == fp->fsize) {
- ncl = 0; ccl = fp->sclust;
- do {
- cl = get_fat(fp->fs, ccl);
- if (cl + 1 < 3) return 0;
- if (cl != ccl + 1 &&; cl < fp->fs->n_fatent) break;
- ccl = cl;
- } while (++ncl < tcl);
- if (ncl == tcl)
- return clust2sect(fp->fs, fp->sclust);
- }
- #if _FS_READONLY
- return 0;
- #else
- if (f_truncate(fp)) return 0;
-
- ccl = cl = 2; ncl = 0;
- do {
- if (cl >= fp->fs->n_fatent) return 0;
- if (get_fat(fp->fs, cl)) {
- do {
- cl++;
- if (cl >= fp->fs->n_fatent) return 0;
- } while (get_fat(fp->fs, cl));
- ccl = cl; ncl = 0;
- }
- cl++; ncl++;
- } while (ncl < tcl);
-
- fp->fs->last_clust = ccl - 1;
- if (f_lseek(fp, len)) return 0;
- return clust2sect(fp->fs, fp->sclust);
- #endif
- }
- int main (void)
- {
- FRESULT fr;
- DRESULT dr;
- FATFS fs;
- FIL fil;
- DWORD org;
-
- f_mount(&fs, "", 0);
- fr = f_open(&fil, "swapfile.sys", FA_READ | FA_WRITE | FA_OPEN_ALWAYS);
- if (fr) return 1;
-
- org = allocate_contiguous_clusters(&fil, 0x4000000);
- if (!org) {
- printf("Function failed due to any error or insufficient contiguous area.\n");
- f_close(&fil);
- return 1;
- }
-
- dr = disk_write(fil.fs->drv, Buff, org, 1024);
- ...
- f_close(&fil);
- return 0;
- }
|