📄 fat.lst
字号:
1011 3 /* seek to the beginning of the file */
1012 3 fat_open_mode = READ;
1013 3
1014 3 return Hard_read_open(fat_ptr_data + ((Uint32)(fclusters[0].cluster)*fat_cluster_size));
1015 3 }
1016 2 }
1017 1 }
1018
1019
1020 /*F**************************************************************************
1021 * NAME: fat_fclose
1022 *----------------------------------------------------------------------------
1023 * PARAMS:
1024 *
1025 * return:
1026 *----------------------------------------------------------------------------
1027 * PURPOSE:
1028 * Close opened file
1029 *----------------------------------------------------------------------------
1030 * EXAMPLE:
1031 *----------------------------------------------------------------------------
1032 * NOTE:
1033 *----------------------------------------------------------------------------
1034 * REQUIREMENTS:
1035 *****************************************************************************/
1036 void fat_fclose (void)
1037 {
1038 1 if (fat_open_mode == READ)
1039 1 {
1040 2 Hard_read_close(); /* close reading */
1041 2 }
1042 1 }
1043
1044
1045 /*F**************************************************************************
1046 * NAME: fat_refresh_dir_file_info
C51 COMPILER V7.50 FAT 02/16/2009 09:59:55 PAGE 18
1047 *----------------------------------------------------------------------------
1048 * PARAMS:
1049 *
1050 * return:
1051 *
1052 *----------------------------------------------------------------------------
1053 * PURPOSE:
1054 * Reconstruct the file directory list and seek to the file pointed by
1055 * fat_dir_list_index
1056 *----------------------------------------------------------------------------
1057 * EXAMPLE:
1058 *----------------------------------------------------------------------------
1059 * NOTE:
1060 *
1061 *----------------------------------------------------------------------------
1062 * REQUIREMENTS:
1063 *
1064 *****************************************************************************/
1065 void fat_refresh_dir_file_info (Byte id)
1066 {
1067 1 /* update fat_dir_current_sect with directory starting value */
1068 1 if (dir_is_root)
1069 1 {
1070 2 fat_dir_current_sect = fat_ptr_rdir;
1071 2 }
1072 1 else
1073 1 {
1074 2 fat_dir_current_sect = (((Uint32)(dclusters[0].cluster)) * fat_cluster_size)
1075 2 + fat_ptr_data;
1076 2 }
1077 1
1078 1 fat_get_dir_file_list(id); /* Refresh file list */
1079 1 fat_seek_entry_record(); /* Re-fetch the entry <-> fat_dir_list_index */
1080 1 fat_get_dir_entry(&fat_cache.current); /* update current file info */
1081 1 }
1082
1083
1084 /*F**************************************************************************
1085 * NAME: fat_read_cluster12
1086 *----------------------------------------------------------------------------
1087 * PARAMS:
1088 * init : initialize the parity bit or not
1089 * return:
1090 * FAT12 cluster value
1091 *----------------------------------------------------------------------------
1092 * PURPOSE:
1093 * Read in fat12 file system a cluster value
1094 *----------------------------------------------------------------------------
1095 * EXAMPLE:
1096 *----------------------------------------------------------------------------
1097 * NOTE:
1098 *
1099 *----------------------------------------------------------------------------
1100 * REQUIREMENTS:
1101 *****************************************************************************/
1102 Uint16 fat_read_cluster (bit init)
1103 {
1104 1 static bit fat12_parity;
1105 1 static idata Uint16 cluster;
1106 1
1107 1 if (fat_is_fat16)
1108 1 {
C51 COMPILER V7.50 FAT 02/16/2009 09:59:55 PAGE 19
1109 2 ((Byte*)&cluster)[1] = Hard_read_byte();
1110 2 ((Byte*)&cluster)[0] = Hard_read_byte();
1111 2 return cluster;
1112 2 }
1113 1
1114 1 if (init)
1115 1 {
1116 2 fat12_parity = 0;
1117 2 cluster = 0;
1118 2 }
1119 1
1120 1 if (fat12_parity == 0)
1121 1 {
1122 2 ((Byte*)&cluster)[1] = Hard_read_byte();
1123 2 ((Byte*)&cluster)[0] = Hard_read_byte();
1124 2 fat12_parity = 1;
1125 2 return (cluster & 0x0FFF);
1126 2 }
1127 1 else
1128 1 {
1129 2 cluster = (cluster & 0xF000) >> 12;
1130 2 cluster += (Hard_read_byte() << 4);
1131 2 fat12_parity = 0;
1132 2 return (cluster);
1133 2 }
1134 1 }
1135
1136
1137
1138 /*F**************************************************************************
1139 * NAME: fat_set_clusters
1140 *----------------------------------------------------------------------------
1141 * PARAMS:
1142 *
1143 * return:
1144 * - OK: allocation done
1145 * - KO: allocation cannot be done : no free cluster
1146 *----------------------------------------------------------------------------
1147 * PURPOSE:
1148 * Prepare a list of the free clusters:
1149 * chain[n].cluster contains the starting cluster number of a fragment
1150 * chain[n].number contains the number of contiguous clusters in fragment
1151 *----------------------------------------------------------------------------
1152 * EXAMPLE:
1153 *----------------------------------------------------------------------------
1154 * NOTE:
1155 * Free cluster list is limited by the nb_frag parameter.
1156 * If memory is too much fragmented, created file may be limited in size.
1157 * Last list item always has single cluster
1158 *----------------------------------------------------------------------------
1159 * REQUIREMENTS:
1160 *****************************************************************************/
1161 bit fat_set_clusters (void)
1162 {
1163 1 bit cluster_free;
1164 1 Uint16 cluster;
1165 1
1166 1 cluster = 0;
1167 1 cluster_free = FALSE;
1168 1 fat_last_clust_index = 0;
1169 1 Hard_read_open(fat_ptr_fats);
1170 1
C51 COMPILER V7.50 FAT 02/16/2009 09:59:55 PAGE 20
1171 1 /* search the first free cluster in fat */
1172 1 fat_read_cluster(1);
1173 1 fat_read_cluster(0);
1174 1 do /* search for first free cluster */
1175 1 {
1176 2 if (fat_read_cluster(0) == 0x0000)
1177 2 cluster_free = TRUE;
1178 2 else
1179 2 cluster++;
1180 2 }
1181 1 while ((cluster != fat_count_of_clusters) && (!cluster_free));
1182 1
1183 1 if (!cluster_free)
1184 1 {
1185 2 Hard_read_close();
1186 2 return KO; /* no free cluster found */
1187 2 }
1188 1
1189 1 fclusters[fat_last_clust_index].number = 1;
1190 1 fclusters[fat_last_clust_index].cluster = cluster; /* store first cluster */
1191 1 cluster++;
1192 1
1193 1 if (cluster != fat_count_of_clusters)
1194 1 {
1195 2 do /* construct the list */
1196 2 {
1197 3 cluster_free = FALSE;
1198 3 if (fat_read_cluster(0) == 0x0000)
1199 3 cluster_free = TRUE;
1200 3 else
1201 3 cluster++;
1202 3
1203 3 if (cluster_free) /* It's a contiguous cluster */
1204 3 { /* add it to the list */
1205 4 if (fclusters[fat_last_clust_index].number == MAX_CL_PER_FRAG)
1206 4 {
1207 5 fat_last_clust_index++;
1208 5 if (fat_last_clust_index == MAX_FILE_FRAGMENT_NUMBER)
1209 5 {
1210 6 Hard_read_close();
1211 6 fat_last_clust_index--;
1212 6 return OK;
1213 6 }
1214 5 fclusters[fat_last_clust_index].number = 1;
1215 5 fclusters[fat_last_clust_index].cluster = cluster;
1216 5 }
1217 4 else
1218 4 {
1219 5 fclusters[fat_last_clust_index].number++;
1220 5 }
1221 4 cluster++; /* process next cluster */
1222 4 }
1223 3 else if (cluster != fat_count_of_clusters)
1224 3 { /* cluster is already used */
1225 4 do /* search for next free fragment */
1226 4 {
1227 5 if (fat_read_cluster(0) == 0x0000)
1228 5 cluster_free = TRUE;
1229 5 else
1230 5 cluster++;
1231 5 }
1232 4 while ((cluster != fat_count_of_clusters) && (!cluster_free));
C51 COMPILER V7.50 FAT 02/16/2009 09:59:55 PAGE 21
1233 4
1234 4 if (!cluster_free) /* no more free cluster */
1235 4 {
1236 5 Hard_read_close();
1237 5 return OK; /* end of partition reached */
1238 5 }
1239 4
1240 4 fat_last_clust_index++; /* new free fragment cluster
- */
1241 4 if (fat_last_clust_index == MAX_FILE_FRAGMENT_NUMBER)
1242 4 {
1243 5 Hard_read_close();
1244 5 fat_last_clust_index--;
1245 5 return OK;
1246 5 }
1247 4 fclusters[fat_last_clust_index].number = 1;
1248 4 fclusters[fat_last_clust_index].cluster = cluster;
1249 4 cluster++; /* process next cluster */
1250 4 }
1251 3 }
1252 2 while ((fat_last_clust_index < MAX_FILE_FRAGMENT_NUMBER) && (cluster < fat_count_of_clusters));
1253 2 }
1254 1
1255 1 Hard_read_close();
1256 1 return OK;
1257 1 }
1258
1259
1260
1261
1262
1263 /*F****************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -