📄 fat.lst
字号:
C51 COMPILER V7.02a FAT 09/13/2007 11:53:07 PAGE 1
C51 COMPILER V7.02a, COMPILATION OF MODULE FAT
OBJECT MODULE PLACED IN fat.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE modules\file\fat.c OPTIMIZE(7,SPEED) BROWSE INCDIR(.\modules\system;.\;.\;.
-\lib_refd) DEFINE(KEIL) DEBUG OBJECTEXTEND PRINT(.\fat.lst) OBJECT(fat.obj)
stmt level source
1 /*C**************************************************************************
2 * NAME: fat.c
3 *----------------------------------------------------------------------------
4 * Copyright (c) 2003 Atmel.
5 *----------------------------------------------------------------------------
6 * RELEASE: snd1c-refd-nf-4_0_3
7 * REVISION: 1.41
8 *----------------------------------------------------------------------------
9 * PURPOSE:
10 * FAT16/FAT12 file-system basics functions
11 *
12 * NOTES:
13 * Supports only the first partition
14 * Supports only 512 bytes sector size
15 * Supports only file fragmentation < MAX_FILE_FRAGMENT_NUMBER
16 * Supports only one file openned at a time
17 *
18 * Global Variables:
19 * - gl_buffer: array of bytes in pdata space
20 *****************************************************************************/
21
22 /*_____ I N C L U D E S ____________________________________________________*/
23
24 #include "config.h" /* system configuration */
25 #include "..\mem\hard.h" /* low level function definition */
26 #include "file.h" /* file function definition */
27 #include "fat.h" /* fat file-system definition */
28 #include "math.h"
29 //void intr_data_task( );
30 /*_____ M A C R O S ________________________________________________________*/
31
32 void intr_data_task_man( );
33
34 //void select_hundred( );
35 //void select_ten( );
36 //void select_bass( );
37 void hundred_selec( );
38 void ten_selec( );
39 void bass_selec( );
40 //unsigned int xdata num_thou , num_hundr,num_ten,num_bass ;
41 unsigned int xdata all_num;//num_hundr_sign,num_ten_sign ;
42 extern unsigned char change_data ,song_num9,song_num8;
43
44 unsigned int xdata thousand,hundred,ten,bass;
45 /*_____ D E F I N I T I O N ________________________________________________*/
46
47 extern bit reserved_disk_space;
48 extern pdata Byte gl_buffer[];
49 extern xdata Byte fat_buf_sector[]; /* 512 bytes buffer */
50
51 extern char pdata *lfn_name; /* long filename limited to MAX_FILENAME_LEN chars */
52
53 /* disk management */
54 extern data Uint32 fat_ptr_fats; /* address of the first byte of FAT */
C51 COMPILER V7.02a FAT 09/13/2007 11:53:07 PAGE 2
55 extern data Uint32 fat_ptr_rdir; /* address of the first byte of root dir */
56 extern data Uint32 fat_ptr_data; /* address of the first byte of data */
57 extern data Byte fat_cluster_size; /* cluster size (sector count) */
58 extern idata Byte fat_cluster_mask; /* mask for end of cluster test */
59
60 extern bdata bit dir_is_root; /* TRUE: point the root directory */
61 extern bdata bit fat_is_fat16; /* TRUE: FAT16 - FALSE: FAT12 */
62 extern bdata bit fat_open_mode; /* READ or WRITE */
63 extern bdata bit fat_2_is_present; /* TRUE: 2 FATs - FALSE: 1 FAT */
64 extern bdata bit flag_end_disk_file;
65
66
67 extern xdata Uint32 fat_count_of_clusters;/* number of cluster - 2 */
68 extern xdata Uint16 fat_root_entry; /* position in root dir */
69 extern xdata Union32 fat_file_size;
70 extern xdata Uint16 fat_fat_size; /* FAT size in sector count */
71
72
73 /* directory management */
74 extern idata Uint32 fat_dir_current_sect; /* sector of selected entry in dir list */
75 extern xdata Uint16 fat_dir_list_index; /* index of current entry in dir list */
76 extern xdata Uint16 fat_dir_list_last; /* index of last entry in dir list */
77 extern idata Uint16 fat_dclust_byte_count;/* byte counter in directory sector */
78 extern idata Uint16 fat_dchain_index; /* the number of the fragment of the dir, in fact
79 the index of the table in the cluster chain */
80 extern idata Byte fat_dchain_nb_clust; /* the offset of the cluster from the first cluster
81 of the dir fragment */
82 extern xdata Uint32 fat_dir_start_sect; /* start sector of dir list */
83 extern xdata Uint16 fat_dir_current_offs; /* entry offset from fat_dir_current_sect */
84 extern xdata Byte fat_last_dclust_index;/* index of the last cluster in directory chain */
85 extern xdata fat_st_cache fat_cache; /* The cache structure, see the .h for more info */
86 extern xdata fat_st_clust_chain dclusters[MAX_DIR_FRAGMENT_NUMBER];
87 /* cluster chain for the current directory */
88 extern xdata char ext[7]; /* file extension (limited to 3 characters) */
89 #define fat_dir_entry_list fat_buf_sector /* manual variable overlay */
90
91
92 /* file management */
93 extern data Uint16 fat_fclust_byte_count;/* byte counter in file cluster */
94 extern idata Byte fat_last_clust_index; /* index of the last cluster in file chain */
95 extern idata Byte fat_fchain_index; /* the number of the fragment of the file, in fact
96 the index of the table in the cluster chain */
97 extern idata Uint16 fat_fchain_nb_clust; /* the offset of the cluster from the first cluster
98 of the file fragment */
99
100 extern xdata fat_st_clust_chain fclusters[MAX_FILE_FRAGMENT_NUMBER];
101 /* cluster chain for the current file */
102
103
104 /* Mode repeat A/B variables */
105 extern xdata Byte fat_fchain_index_save;
106 extern xdata Byte fat_fchain_nb_clust_save;
107 extern xdata Uint16 fat_fclust_byte_count_save;
108
109 static xdata fat_st_free_space free_space;
110
111 /*_____ D E C L A R A T I O N ______________________________________________*/
112
113 static void fat_get_dir_entry (fat_st_dir_entry xdata *);
114 static void fat_get_dir_file_list (Byte);
115 static bit fat_get_clusters (fat_st_clust_chain xdata *, Byte);
116 static bit fat_set_clusters (void);
C51 COMPILER V7.02a FAT 09/13/2007 11:53:07 PAGE 3
117 static bit fat_dopen (void);
118 static bit fat_dseek (Int16);
119 static Byte fat_dgetc (void);
120
121 code Byte PBR_record_part1[] =
122 {
123 0xEB, 0x3C, 0x90, /* JMP instruction to boot code */
124 'O', 'E', 'M', ' ', 'N', 'A', 'M', 'E', /* OEM name */
125 SECTOR_SIZE, SECTOR_SIZE >> 8, /* number of bytes per sector */
126 0x00, /* number of sector per cluster */
127 NB_RESERVED, NB_RESERVED >> 8, /* number of reserved sector */
128 NB_FATS, /* number of FAT */
129 NB_ROOT_ENTRY, NB_ROOT_ENTRY >> 8, /* number of root directory entries */
130 0x00, 0x00, /* total sectors if less than 65535 */
131 HARD_DISK, /* media byte */
132 };
133
134 code Byte PBR_record_part2[] =
135 {
136 FAT_DRIVE_NUMBER, /* Drive number */
137 0x00, /* not used */
138 FAT_EXT_SIGN, /* extended boot signature */
139 0x00, 0x00, 0x00, 0x00, /* volume ID */
140 'N', 'O', ' ', 'N', 'A', 'M', 'E', ' ', ' ', ' ', ' ', /* volume label */
141 'F', 'A', 'T', '1', 0x00, ' ', ' ', ' ', /* File system type in ASCII */
142
143 };
144
145 /*F**************************************************************************
146 * NAME: fat_load_sector
147 *----------------------------------------------------------------------------
148 * PARAMS:
149 *
150 * return:
151 *----------------------------------------------------------------------------
152 * PURPOSE:
153 * This function load a sector in fat_buf_sector
154 *----------------------------------------------------------------------------
155 * EXAMPLE:
156 *----------------------------------------------------------------------------
157 * NOTE:
158 *----------------------------------------------------------------------------
159 * REQUIREMENTS:
160 *
161 *****************************************************************************/
162 bit fat_load_sector(Uint32 sector)
163 {
164 1 Uint16 i;
165 1 if (Hard_read_open(sector) == OK)
166 1 {
167 2 for (i = 0; i < (SECTOR_SIZE); i++)
168 2 {
169 3 fat_buf_sector[i++] = Hard_read_byte();
170 3 fat_buf_sector[i++] = Hard_read_byte();
171 3 fat_buf_sector[i++] = Hard_read_byte();
172 3 fat_buf_sector[i] = Hard_read_byte();
173 3 }
174 2 Hard_read_close();
175 2 return OK;
176 2 }
177 1 else
178 1 {
C51 COMPILER V7.02a FAT 09/13/2007 11:53:07 PAGE 4
179 2 return KO;
180 2 }
181 1 }
182
183 /*F**************************************************************************
184 * NAME: fat_install
185 *----------------------------------------------------------------------------
186 * PARAMS:
187 *
188 * return:
189 * - OK: intallation succeeded
190 * - KO: - partition 1 signature not recognized
191 * - FAT type is not FAT12/FAT16
192 * - sector size is not 512 bytes
193 * - MBR or PBR signatures are not correct
194 * - low level read open failure
195 *----------------------------------------------------------------------------
196 * PURPOSE:
197 * Install the fat system, read mbr, bootrecords...
198 *----------------------------------------------------------------------------
199 * EXAMPLE:
200 *----------------------------------------------------------------------------
201 * NOTE:
202 * if MBR not found, try to mount unpartitionned FAT
203 * sector size is fixed to 512 bytes to simplify low level drivers
204 * fat_ptr_fats = partition offset + nb_reserved_sector
205 * fat_ptr_rdir = fat_ptr_fat + fat_size * nb_fat
206 * fat_ptr_data = fat_ptr_rdir + nb_root_entries * 32 / 512
207 *----------------------------------------------------------------------------
208 * REQUIREMENTS:
209 *****************************************************************************/
210 bit fat_install (void)
211 {
212 1 Uint32 fat_nb_sector;
213 1
214 1 /* MBR/PBR determination */
215 1 fat_ptr_fats = 1;
216 1 if (fat_load_sector(MBR_ADDRESS) == OK)
217 1 {
218 2
219 2 if ((fat_buf_sector[0] == 0xEB) && /* PBR Byte 0 */
220 2 (fat_buf_sector[2] == 0x90) && /* PBR Byte 2 */
221 2 ((fat_buf_sector[21] & 0xF0) == 0xF0)) /* PBR Byte 21 : Media byte */
222 2 {
223 3 if ((fat_buf_sector[510] != LOW(BR_SIGNATURE)) && /* check PBR signature */
224 3 (fat_buf_sector[511] != HIGH(BR_SIGNATURE)))
225 3 {
226 4 return KO;
227 4 }
228 3 else
229 3 {
230 4 fat_ptr_fats = 0x00000000; /* first sector is PBR */
231 4 }
232 3 }
233 2 else
234 2 { /* first sector is MBR */
235 3 if ((fat_buf_sector[446] != PARTITION_ACTIVE) &&
236 3 (fat_buf_sector[446] != 0x00))
237 3 {
238 4 return KO; /* not a MBR */
239 4 }
240 3 else
C51 COMPILER V7.02a FAT 09/13/2007 11:53:07 PAGE 5
241 3 {
242 4 /* read partition offset (in sectors) at offset 8 */
243 4 ((Byte*)&fat_ptr_fats)[3] = fat_buf_sector[454];
244 4 ((Byte*)&fat_ptr_fats)[2] = fat_buf_sector[455];
245 4 ((Byte*)&fat_ptr_fats)[1] = fat_buf_sector[456];
246 4 ((Byte*)&fat_ptr_fats)[0] = fat_buf_sector[457];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -