SAMV71 Xplained Ultra Software Package 1.0

app3.c

00001 /*----------------------------------------------------------------------/
00002 / Allocate a contiguous area to the file
00003 /-----------------------------------------------------------------------/
00004 / This function checks if the file is contiguous with desired size.
00005 / If not, a block of contiguous sectors is allocated to the file.
00006 / If the file has been opened without FA_WRITE flag, it only checks if
00007 / the file is contiguous and returns the resulut. */
00008 
00009 #if _FATFS != 8051 /* Check if R0.10b */
00010 #error This function may not be compatible with this revision of FatFs module.
00011 #endif
00012 
00013 /* Declarations of FatFs internal functions accessible from applications.
00014 /  This is intended to be used for disk checking/fixing or dirty hacks :-) */
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 (    /* Returns the first sector in LBA (0:error or not contiguous) */
00021     FIL* fp,    /* Pointer to the open file object */
00022     DWORD len   /* Number of bytes to allocate */
00023 )
00024 {
00025     DWORD csz, tcl, ncl, ccl, cl;
00026 
00027 
00028     if (f_lseek(fp, 0) || !len)     /* Check if the given parameters are valid */
00029         return 0;
00030     csz = 512UL * fp->fs->csize;    /* Cluster size in unit of byte (assuming 512 bytes/sector) */
00031     tcl = (len + csz - 1) / csz;    /* Total number of clusters required */
00032     len = tcl * csz;                /* Round-up file size to the cluster boundary */
00033 
00034     /* Check if the existing cluster chain is contiguous */
00035     if (len == fp->fsize) {
00036         ncl = 0; ccl = fp->sclust;
00037         do {
00038             cl = get_fat(fp->fs, ccl);  /* Get the cluster status */
00039             if (cl + 1 < 3) return 0;   /* Hard error? */
00040             if (cl != ccl + 1 &&; cl < fp->fs->n_fatent) break;  /* Not contiguous? */
00041             ccl = cl;
00042         } while (++ncl < tcl);
00043         if (ncl == tcl)             /* Is the file contiguous? */
00044             return clust2sect(fp->fs, fp->sclust);  /* Return file start sector */
00045     }
00046 #if _FS_READONLY
00047     return 0;
00048 #else
00049     if (f_truncate(fp)) return 0;   /* Remove the existing chain */
00050 
00051     /* Find a free contiguous area */
00052     ccl = cl = 2; ncl = 0;
00053     do {
00054         if (cl >= fp->fs->n_fatent) return 0;   /* No contiguous area is found. */
00055         if (get_fat(fp->fs, cl)) {  /* Encounterd a cluster in use */
00056             do {    /* Skip the block of used clusters */
00057                 cl++;
00058                 if (cl >= fp->fs->n_fatent) return 0;   /* No contiguous area is found. */
00059             } while (get_fat(fp->fs, cl));
00060             ccl = cl; ncl = 0;
00061         }
00062         cl++; ncl++;
00063     } while (ncl < tcl);
00064 
00065     /* Create a contiguous cluster chain */
00066     fp->fs->last_clust = ccl - 1;
00067     if (f_lseek(fp, len)) return 0;
00068 
00069     return clust2sect(fp->fs, fp->sclust);  /* Return file start sector */
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     /* Open or create a file */
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     /* Check if the file is 64MB in size and occupies a contiguous area.
00089     /  If not, a contiguous area will be re-allocated to the file. */
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     /* Now you can read/write the file with disk functions bypassing the file system layer. */
00098 
00099     dr = disk_write(fil.fs->drv, Buff, org, 1024);   /* Write 512KiB from top of the file */
00100 
00101     ...
00102 
00103     f_close(&fil);
00104     return 0;
00105 }
00106 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines