CARME-M4 BSP  V1.5
app3.c
1 /*----------------------------------------------------------------------/
2 / Allocate a contiguous area to the file
3 /-----------------------------------------------------------------------/
4 / This function checks if the file is contiguous with desired size.
5 / If not, contiguous sectors are allocated to the file. If the file has
6 / been opened without FA_WRITE flag, it only checks if the file is
7 / contiguous and returns the resulut. */
8 
9 #if _FATFS != 80960 /* Check if R0.10 */
10 #error This function may not be compatible with this revision of FatFs module.
11 #endif
12 
13 /* Declarations of FatFs internal functions accessible from applications.
14 / This is intended to be used for disk checking/fixing or dirty hacks :-) */
15 DWORD clust2sect (FATFS* fs, DWORD clst);
16 DWORD get_fat (FATFS* fs, DWORD clst);
17 FRESULT put_fat (FATFS* fs, DWORD clst, DWORD val);
18 
19 
20 DWORD allocate_contiguous_clusters ( /* Returns file start sector number (0:error or not contiguous) */
21  FIL* fp, /* Pointer to the open file object */
22  DWORD len /* Number of bytes to allocate */
23 )
24 {
25  DWORD csz, tcl, ncl, ccl, cl;
26 
27 
28  if (f_lseek(fp, 0) || !len) /* Check if the given parameters are valid */
29  return 0;
30  csz = 512UL * fp->fs->csize; /* Cluster size in unit of byte (assuming 512 bytes/sector) */
31  tcl = (len + csz - 1) / csz; /* Total number of clusters required */
32  len = tcl * csz; /* Round-up file size to the cluster boundary */
33 
34  /* Check if the existing cluster chain is contiguous */
35  if (len == fp->fsize) {
36  ncl = 0; ccl = fp->sclust;
37  do {
38  cl = get_fat(fp->fs, ccl); /* Get the cluster status */
39  if (cl + 1 < 3) return 0; /* Hard error? */
40  if (cl != ccl + 1 &&; cl < fp->fs->n_fatent) break; /* Not contiguous? */
41  ccl = cl;
42  } while (++ncl < tcl);
43  if (ncl == tcl) /* Is the file contiguous? */
44  return clust2sect(fp->fs, fp->sclust); /* Return file start sector */
45  }
46 #if _FS_READONLY
47  return 0;
48 #else
49  if (f_truncate(fp)) return 0; /* Remove the existing chain */
50 
51  /* Find a free contiguous area */
52  ccl = cl = 2; ncl = 0;
53  do {
54  if (cl >= fp->fs->n_fatent) return 0; /* No contiguous area is found. */
55  if (get_fat(fp->fs, cl)) { /* Encounterd a cluster in use */
56  do { /* Skip the block of used clusters */
57  cl++;
58  if (cl >= fp->fs->n_fatent) return 0; /* No contiguous area is found. */
59  } while (get_fat(fp->fs, cl));
60  ccl = cl; ncl = 0;
61  }
62  cl++; ncl++;
63  } while (ncl < tcl);
64 
65  /* Create a contiguous cluster chain */
66  fp->fs->last_clust = ccl - 1;
67  if (f_lseek(fp, len)) return 0;
68 
69  return clust2sect(fp->fs, fp->sclust); /* Return file start sector */
70 #endif
71 }
72 
73 
74 int main (void)
75 {
76  FRESULT fr;
77  DRESULT dr;
78  FATFS fs;
79  FIL fil;
80  DWORD org;
81 
82 
83  /* Open or create a file */
84  f_mount(&fs, "", 0);
85  fr = f_open(&fil, "pagefile.dat", FA_READ | FA_WRITE | FA_OPEN_ALWAYS);
86  if (fr) return 1;
87 
88  /* Check if the file is 64MB in size and occupies a contiguous area.
89  / If not, a contiguous area will be re-allocated to the file. */
90  org = allocate_contiguous_clusters(&fil, 0x4000000);
91  if (!org) {
92  printf("Function failed due to any error or insufficient contiguous area.\n");
93  f_close(&fil);
94  return 1;
95  }
96 
97  /* Now you can read/write the file with disk functions bypassing the file system layer. */
98 
99  dr = disk_write(fil.fs->drv, Buff, org, 1024); /* Write 512Ki bytes from top of the file */
100 
101  ...
102 
103  f_close(&fil);
104  return 0;
105 }
106 
FRESULT f_lseek(FIL *fp, DWORD ofs)
Seek File R/W Pointer.
Definition: ff.c:3005
FRESULT
File function return code.
Definition: ff.h:204
File object structure.
Definition: ff.h:127
#define FA_WRITE
Definition: ff.h:385
DWORD n_fatent
Definition: ff.h:111
File system object structure.
Definition: ff.h:88
DWORD fsize
Definition: ff.h:136
BYTE csize
Definition: ff.h:91
#define FA_OPEN_ALWAYS
Definition: ff.h:402
FRESULT f_close(FIL *fp)
Close File.
Definition: ff.c:2822
DWORD sclust
Definition: ff.h:137
DRESULT
Results of Disk Functions.
Definition: diskio.h:112
DWORD clust2sect(FATFS *fs, DWORD clst)
Synchronize file system and storage device.
Definition: ff.c:952
FRESULT f_mount(FATFS *fs, const TCHAR *path, BYTE opt)
Mount/Unmount a Logical Drive.
Definition: ff.c:2392
#define FA_READ
Definition: ff.h:371
FRESULT put_fat(FATFS *fs, DWORD clst, DWORD val)
Change value of a FAT entry.
Definition: ff.c:1015
BYTE drv
Definition: ff.h:90
FRESULT f_truncate(FIL *fp)
Truncate File.
Definition: ff.c:3373
FRESULT f_open(FIL *fp, const TCHAR *path, BYTE mode)
Open or Create a File.
Definition: ff.c:2434
FATFS * fs
Definition: ff.h:128
DWORD last_clust
Definition: ff.h:105
DWORD get_fat(FATFS *fs, DWORD clst)
Read value of a FAT entry.
Definition: ff.c:976