📄 tff.lst
字号:
C51 COMPILER V7.02b TFF 03/24/2008 15:52:29 PAGE 1
C51 COMPILER V7.02b, COMPILATION OF MODULE TFF
OBJECT MODULE PLACED IN .\Output\tff.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE tff.c LARGE OPTIMIZE(9,SIZE) BROWSE ORDER DEBUG OBJECTEXTEND PRINT(.\Output
-\tff.lst) OBJECT(.\Output\tff.obj)
stmt level source
1 /*--------------------------------------------------------------------------/
2 / Tiny FatFs - FAT file system module R0.02a (C)ChaN, 2006
3 /---------------------------------------------------------------------------/
4 / FatFs module is an experimenal project to implement FAT file system to
5 / cheap microcontrollers. This is opened for education, reserch and
6 / development. You can use, modify and republish it for non-profit or profit
7 / use without any limitation under your responsibility.
8 /---------------------------------------------------------------------------/
9 / Feb 26, 2006 R0.00 Prototype
10 / Apr 29, 2006 R0.01 First stable version
11 / Jun 01, 2006 R0.02 Added FAT12. Removed unbuffered mode.
12 / Fixed a problem on small (<32M) patition.
13 / Jun 10, 2006 R0.02a Added a configuration option (_FS_MINIMUM).
14 /---------------------------------------------------------------------------*/
15
16
17 #include <string.h>
18 #include "tff.h" /* Tiny-FatFs declarations */
19 #include "diskio.h" /* Include file for user provided functions */
20
21
22 extern FATFS fatfsentity;
23 extern BYTE Buff[512];
24
25 FATFS *FatFs; /* Pointer to the file system object */
26
27
28 /*-------------------------------------------------------------------------
29 Module Private Functions
30 -------------------------------------------------------------------------*/
31
32 /*----------------------*/
33 /* Change Window Offset */
34
35 static BOOL move_window (
36 DWORD sector /* Sector number to make apperance in the FatFs->win */
37 ) /* Move to zero only writes back dirty window */
38 {
39 1 DWORD wsect;
40 1 FATFS *fs = FatFs;
41 1
42 1
43 1 wsect = fs->winsect;
44 1 if (wsect != sector) { /* Changed current window */
45 2 #ifndef _FS_READONLY
46 2 BYTE n;
47 2 if (fs->winflag) { /* Write back dirty window if needed */
48 3 if (disk_write(fs->win, wsect, 1) != RES_OK) return FALSE;
49 3 fs->winflag = 0;
50 3 if (wsect < (fs->fatbase + fs->sects_fat)) { /* In FAT area */
51 4 for (n = fs->n_fats; n >= 2; n--) { /* Refrect the change to all FAT copies */
52 5 wsect += fs->sects_fat;
53 5 if (disk_write(fs->win, wsect, 1) != RES_OK) break;
54 5 }
C51 COMPILER V7.02b TFF 03/24/2008 15:52:29 PAGE 2
55 4 }
56 3 }
57 2 #endif
58 2 if (sector) {
59 3 if (disk_read(fs->win, sector, 1) != RES_OK) return FALSE;
60 3 fs->winsect = sector;
61 3 }
62 2 }
63 1 return TRUE;
64 1 }
65
66
67
68 /*----------------------*/
69 /* Get a Cluster Status */
70
71 static
72 WORD get_cluster (WORD clust) /* Cluster# to get the link information */
73 {
74 1 WORD wc, bc;
75 1 DWORD fatsect;
76 1 FATFS *fs = FatFs;
77 1
78 1
79 1 if ((clust >= 2) && (clust < fs->max_clust)) { /* Valid cluster# */
80 2 fatsect = fs->fatbase;
81 2 if (fs->fs_type == FS_FAT12) {
82 3 bc = clust * 3 / 2;
83 3 if (!move_window(fatsect + bc / 512)) return 1;
84 3 wc = fs->win[bc % 512]; bc++;
85 3 if (!move_window(fatsect + bc / 512)) return 1;
86 3 wc |= (WORD)fs->win[bc % 512] << 8;
87 3 return (clust & 1) ? (wc >> 4) : (wc & 0xFFF);
88 3 } else {
89 3 if (!move_window(fatsect + clust / 256)) return 1;
90 3 return LD_WORD(&(fs->win[(clust * 2) % 512]));
91 3 }
92 2 }
93 1 return 1; /* Return with 1 means function failed */
94 1 }
95
96
97
98 /*-------------------------*/
99 /* Change a Cluster Status */
100
101 #ifndef _FS_READONLY
102 static
103 BOOL put_cluster (
104 WORD clust, /* Cluster# to change */
105 WORD val /* New value to mark the cluster */
106 )
107 {
108 1 WORD bc;
109 1 BYTE *p;
110 1 DWORD fatsect;
111 1 FATFS *fs = FatFs;
112 1
113 1
114 1 fatsect = fs->fatbase;
115 1 if (fs->fs_type == FS_FAT12) {
116 2 bc = clust * 3 / 2;
C51 COMPILER V7.02b TFF 03/24/2008 15:52:29 PAGE 3
117 2 if (!move_window(fatsect + bc / 512)) return FALSE;
118 2 p = &fs->win[bc % 512];
119 2 *p = (clust & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;
120 2 fs->winflag = 1; bc++;
121 2 if (!move_window(fatsect + bc / 512)) return FALSE;
122 2 p = &fs->win[bc % 512];
123 2 *p = (clust & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));
124 2 } else {
125 2 if (!move_window(fatsect + clust / 256)) return FALSE;
126 2 ST_WORD(&(fs->win[((WORD)clust * 2) % 512]), (WORD)val);
127 2 }
128 1
129 1 fs->winflag = 1;
130 1 return TRUE;
131 1 }
132 #endif /* _FS_READONLY */
133
134
135
136 /*------------------------*/
137 /* Remove a Cluster Chain */
138
139 #ifndef _FS_READONLY
140 static
141 BOOL remove_chain (
142 WORD clust /* Cluster# to remove chain from */
143 )
144 {
145 1 WORD nxt;
146 1
147 1
148 1 while ((nxt = get_cluster(clust)) >= 2) {
149 2 if (!put_cluster(clust, 0)) return FALSE;
150 2 clust = nxt;
151 2 }
152 1 return TRUE;
153 1 }
154 #endif
155
156
157
158 /*-----------------------------------*/
159 /* Stretch or Create a Cluster Chain */
160
161 #ifndef _FS_READONLY
162 static
163 DWORD create_chain (
164 WORD clust /* Cluster# to stretch, 0 means create new */
165 )
166 {
167 1 WORD ncl, ccl, mcl = FatFs->max_clust;
168 1
169 1
170 1 if (clust == 0) { /* Create new chain */
171 2 ncl = 1;
172 2 do {
173 3 ncl++; /* Check next cluster */
174 3 if (ncl >= mcl) return 0; /* No free custer was found */
175 3 ccl = get_cluster(ncl); /* Get the cluster status */
176 3 if (ccl == 1) return 0; /* Any error occured */
177 3 } while (ccl); /* Repeat until find a free cluster */
178 2 }
C51 COMPILER V7.02b TFF 03/24/2008 15:52:29 PAGE 4
179 1 else { /* Stretch existing chain */
180 2 ncl = get_cluster(clust); /* Check the cluster status */
181 2 if (ncl < 2) return 0; /* It is an invalid cluster */
182 2 if (ncl < mcl) return ncl; /* It is already followed by next cluster */
183 2 ncl = clust; /* Search free cluster */
184 2 do {
185 3 ncl++; /* Check next cluster */
186 3 if (ncl >= mcl) ncl = 2; /* Wrap around */
187 3 if (ncl == clust) return 0; /* No free custer was found */
188 3 ccl = get_cluster(ncl); /* Get the cluster status */
189 3 if (ccl == 1) return 0; /* Any error occured */
190 3 } while (ccl); /* Repeat until find a free cluster */
191 2 }
192 1
193 1 if (!put_cluster(ncl, 0xFFFF)) return 0; /* Mark the new cluster "in use" */
194 1 if (clust && !put_cluster(clust, ncl)) return 0; /* Link it to previous one if needed */
195 1
196 1 return ncl; /* Return new cluster number */
197 1 }
198 #endif /* _FS_READONLY */
199
200
201
202 /*----------------------------*/
203 /* Get Sector# from Cluster# */
204
205 static
206 DWORD clust2sect (
207 WORD clust /* Cluster# to be converted */
208 )
209 {
210 1 FATFS *fs = FatFs;
211 1
212 1
213 1 clust -= 2;
214 1 if (clust >= fs->max_clust) return 0; /* Invalid cluster# */
215 1 return (DWORD)clust * fs->sects_clust + fs->database;
216 1 }
217
218
219
220 /*------------------------*/
221 /* Check File System Type */
222
223 static
224 BYTE check_fs (
225 DWORD sect /* Sector# to check if it is a FAT boot record or not */
226 )
227 {
228 1 const char fatsign[] = "FAT12FAT16";
229 1 FATFS *fs = FatFs;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -