STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
app3.c
Go to the documentation of this file.
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, a block of contiguous sectors is allocated to the file.
6 / If the file has been opened without FA_WRITE flag, it only checks if
7 / the file is contiguous and returns the resulut. */
8 
9 #if _FATFS != 32020 /* Check if R0.11 */
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 the first sector in LBA (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, "swapfile.sys", 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 512KiB from top of the file */
100 
101  ...
102 
103  f_close(&fil);
104  return 0;
105 }
106 
DWORD clust2sect(FATFS *fs, DWORD clst)
Definition: ff.c:873
FRESULT put_fat(FATFS *fs, DWORD clst, DWORD val)
Definition: ff.c:946
FRESULT f_close(FIL *fp)
Definition: ff.c:2930
Definition: ff.h:118
DWORD n_fatent
Definition: ff.h:104
Definition: ff.h:78
#define FA_OPEN_ALWAYS
Definition: ff.h:315
DWORD fsize
Definition: ff.h:130
#define FA_WRITE
Definition: ff.h:312
#define FA_READ
Definition: ff.h:308
BYTE csize
Definition: ff.h:85
FRESULT f_truncate(FIL *fp)
Definition: ff.c:3544
unsigned long DWORD
Definition: integer.h:29
DRESULT disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
Writes Sector(s)
Definition: diskio.c:114
DRESULT
Definition: diskio.h:22
FRESULT f_lseek(FIL *fp, DWORD ofs)
Definition: ff.c:3102
DWORD sclust
Definition: ff.h:131
FRESULT
Definition: ff.h:198
DWORD allocate_contiguous_clusters(FIL *fp, DWORD len)
Definition: app3.c:20
DWORD get_fat(FATFS *fs, DWORD clst)
Definition: ff.c:891
FRESULT f_mount(FATFS *fs, const TCHAR *path, BYTE opt)
Definition: ff.c:2475
BYTE drv
Definition: ff.h:84
FATFS * fs
Definition: ff.h:125
DWORD last_clust
Definition: ff.h:98
int main(void)
Definition: app3.c:74
FRESULT f_open(FIL *fp, const TCHAR *path, BYTE mode)
Definition: ff.c:2522