📄 fat.lst
字号:
1015 *----------------------------------------------------------------------------
1016 * NOTE:
1017 *----------------------------------------------------------------------------
1018 * REQUIREMENTS:
1019 * fat_root_entry must be updated.
1020 *****************************************************************************/
1021 void fat_update_buf_fat(Uint16 cluster_old, Uint16 cluster, bit end)
1022 {
1023 1 bit fat_12_parity;
1024 1 Byte sector_number;
1025 1 #define i cluster_old
1026 1
1027 1 fat_12_parity = ((Byte*)&cluster_old)[1] & 0x01;
1028 1 sector_number = cluster_old * 3 / 1024;
1029 1 i = (cluster_old * 3 / 2) & 0x1FF;
1030 1
1031 1 if (end == TRUE)
1032 1 cluster = 0xFFF;
1033 1 if (fat_12_parity == 0)
1034 1 {
1035 2 fat_buf_sector[i++] = ((Byte*)&cluster)[1];
1036 2 if (((Byte*)&i)[0] == 0x02)
1037 2 {
1038 3 fat_update_fat_sector(sector_number);
1039 3 sector_number++;
1040 3 fat_load_sector(fat_ptr_fats + sector_number);
1041 3 ((Byte*)&i)[0] = 0x00;
1042 3 }
1043 2 fat_buf_sector[i] &= 0xF0;
1044 2 fat_buf_sector[i] |= ((Byte*)&cluster)[0] & 0x0F;
1045 2 }
1046 1 else
C51 COMPILER V7.50 FAT 09/20/2005 21:47:47 PAGE 18
1047 1 {
1048 2 fat_buf_sector[i] &= 0x0F;
1049 2 fat_buf_sector[i] |= (((Byte*)&cluster)[1] & 0x0F) << 4;
1050 2 i++;
1051 2 if (((Byte*)&i)[0] == 0x02)
1052 2 {
1053 3 fat_update_fat_sector(sector_number);
1054 3 sector_number++;
1055 3 fat_load_sector(fat_ptr_fats + sector_number);
1056 3 ((Byte*)&i)[0] = 0x00;
1057 3 }
1058 2 fat_buf_sector[i++] = (cluster & 0x0FF0) >> 4;
1059 2 if (((Byte*)&i)[0] == 0x02)
1060 2 {
1061 3 fat_update_fat_sector(sector_number);
1062 3 sector_number++;
1063 3 fat_load_sector(fat_ptr_fats + sector_number);
1064 3 ((Byte*)&i)[0] = 0x00;
1065 3 }
1066 2 }
1067 1 if (end == TRUE)
1068 1 fat_update_fat_sector(sector_number);
1069 1 #undef i
1070 1
1071 1 }
1072 /*F**************************************************************************
1073 * NAME: fat_update_entry_fat
1074 *----------------------------------------------------------------------------
1075 * PARAMS:
1076 *
1077 * return:
1078 *----------------------------------------------------------------------------
1079 * PURPOSE:
1080 * Update root entry and FAT after a writing file session (create or re-write)
1081 *----------------------------------------------------------------------------
1082 * EXAMPLE:
1083 *----------------------------------------------------------------------------
1084 * NOTE:
1085 *----------------------------------------------------------------------------
1086 * REQUIREMENTS:
1087 * fat_root_entry must be updated.
1088 *****************************************************************************/
1089 void fat_update_entry_fat (void)
1090 {
1091 1 Byte index;
1092 1 Byte chain_index;
1093 1 Byte sector_number;
1094 1 Uint16 i;
1095 1 Uint16 cluster;
1096 1 Uint16 nb_cluster;
1097 1
1098 1 /*********************/
1099 1 /* Update root entry */
1100 1 /*********************/
1101 1 fat_load_sector(fat_ptr_rdir + (fat_root_entry >> 4));
1102 1 i = (fat_root_entry % 16) * 32 ; /* Position of entry in the sector */
1103 1 /* Update file size */
1104 1 if (fat_cache.current.size.l <= fat_file_size.l)
1105 1 fat_cache.current.size.l = fat_file_size.l;
1106 1 fat_buf_sector[i + 28] = fat_cache.current.size.b[3];
1107 1 fat_buf_sector[i + 29] = fat_cache.current.size.b[2];
1108 1 fat_buf_sector[i + 30] = fat_cache.current.size.b[1];
C51 COMPILER V7.50 FAT 09/20/2005 21:47:47 PAGE 19
1109 1 fat_buf_sector[i + 31] = fat_cache.current.size.b[0];
1110 1
1111 1 ext[0] = fat_buf_sector[i + 8];
1112 1 ext[1] = fat_buf_sector[i + 9];
1113 1 ext[2] = fat_buf_sector[i + 10];
1114 1
1115 1
1116 1 Hard_write_open(fat_ptr_rdir + (fat_root_entry >> 4));
1117 1 for (i= 0; i< SECTOR_SIZE; i++)
1118 1 Hard_write_byte(fat_buf_sector[i]);
1119 1 Hard_write_close();
1120 1
1121 1
1122 1 /********************/
1123 1 /* Update fat 1 & 2 */
1124 1 /********************/
1125 1 /* Calculate file size cluster */
1126 1 nb_cluster = (fat_cache.current.size.l / SECTOR_SIZE) / fat_cluster_size;
1127 1 if ((fat_cache.current.size.l % (fat_cluster_size * SECTOR_SIZE)))
1128 1 {
1129 2 nb_cluster++;
1130 2 }
1131 1 nb_cluster--;
1132 1 index = 0;
1133 1
1134 1 /********************/
1135 1 /* FAT16 management */
1136 1 /********************/
1137 1 if (fat_is_fat16)
1138 1 {
1139 2 /* init the starting cluster value */
1140 2 cluster = fclusters[0].cluster + 2;
1141 2 /* Start at first chain cluster */
1142 2 sector_number = cluster / 256;
1143 2 /* Bufferize fat sector */
1144 2 fat_load_sector(fat_ptr_fats + sector_number);
1145 2 /* i -> word fat sector position */
1146 2 i = (cluster * 2) & 0x1FF;
1147 2 chain_index = 1;
1148 2
1149 2 while (nb_cluster != 0)
1150 2 {
1151 3 /* Determinate the value of the next cluster */
1152 3 if (fclusters[index].number == chain_index)
1153 3 {
1154 4 /* increase index */
1155 4 index++;
1156 4 cluster = fclusters[index].cluster + 2;
1157 4 fat_buf_sector[i++] = ((Byte*)&cluster)[1];
1158 4 fat_buf_sector[i] = ((Byte*)&cluster)[0];
1159 4 chain_index = 1;
1160 4 if ( (cluster / 256) != sector_number)
1161 4 { /* Fat change sector */
1162 5 fat_update_fat_sector(sector_number);
1163 5 sector_number = (Uint16)(cluster / 256);
1164 5 fat_load_sector(fat_ptr_fats + sector_number);
1165 5 }
1166 4 i = (cluster * 2) & 0x1FF;
1167 4
1168 4 }
1169 3 else
1170 3 {
C51 COMPILER V7.50 FAT 09/20/2005 21:47:47 PAGE 20
1171 4 cluster++;
1172 4 fat_buf_sector[i++] = ((Byte*)&cluster)[1];
1173 4 fat_buf_sector[i++] = ((Byte*)&cluster)[0];
1174 4 chain_index++;
1175 4 if (((Byte*)&i)[0] == 0x02)
1176 4 {
1177 5 fat_update_fat_sector(sector_number);
1178 5 sector_number++;
1179 5 fat_load_sector(fat_ptr_fats + sector_number);
1180 5 ((Byte*)&i)[0] = 0x00;
1181 5 }
1182 4 }
1183 3 nb_cluster--;
1184 3 }
1185 2 /* End of file indicate by 0xFFFF */
1186 2 fat_buf_sector[i++] = 0xFF;
1187 2 fat_buf_sector[i] = 0xFF;
1188 2 fat_update_fat_sector(sector_number);
1189 2 }
1190 1 /********************/
1191 1 /* FAT12 management */
1192 1 /********************/
1193 1 else
1194 1 {
1195 2 cluster = fclusters[index].cluster + 2;
1196 2 sector_number = cluster * 3 / 1024;
1197 2 /* Bufferize fat sector */
1198 2 fat_load_sector(fat_ptr_fats + sector_number);
1199 2 i = cluster;
1200 2 chain_index = 1;
1201 2 while (nb_cluster != 0)
1202 2 {
1203 3 /* Determinate the value of the next cluster */
1204 3 if (fclusters[index].number == chain_index)
1205 3 {
1206 4 /* increase index */
1207 4 index++;
1208 4 fat_update_buf_fat(cluster, fclusters[index].cluster + 2, 0);
1209 4 cluster = fclusters[index].cluster + 2;
1210 4 chain_index = 1;
1211 4 i = cluster * 3 / 1024;
1212 4 if ( i != sector_number)
1213 4 { /* Fat change sector */
1214 5 fat_update_fat_sector(sector_number);
1215 5 sector_number = i;
1216 5 fat_load_sector(fat_ptr_fats + sector_number);
1217 5 }
1218 4 }
1219 3 else
1220 3 {
1221 4 cluster++;
1222 4 fat_update_buf_fat(cluster - 1, cluster, 0);
1223 4 chain_index++;
1224 4 }
1225 3 nb_cluster--;
1226 3 }
1227 2 fat_update_buf_fat(cluster, cluster, 1);
1228 2 }
1229 1
1230 1 /* Reconstruct list file */
1231 1 i = fat_dir_list_index;
1232 1
C51 COMPILER V7.50 FAT 09/20/2005 21:47:47 PAGE 21
1233 1 fat_dir_current_sect = fat_ptr_rdir;
1234 1 fat_dclust_byte_count = 0;
1235 1 dir_is_root = TRUE;
1236 1
1237 1 fat_get_dir_file_list(fat_check_ext()); /* create list of entries */
1238 1 fat_dir_list_index = i;
1239 1 for (i = 0; i <= fat_dir_list_index; i++)
1240 1 fat_dseek(fat_dir_entry_list[i] * DIR_SIZE);
1241 1
1242 1 fat_get_dir_entry(&fat_cache.current); /* update current file info */
1243 1 }
1244
1245
1246 /*F**************************************************************************
1247 * NAME: fat_fopen
1248 *----------------------------------------------------------------------------
1249 * PARAMS:
1250 * mode: READ: open file for read
1251 * WRITE: open file for write
1252 *
1253 * return:
1254 * - OK: file opened
1255 * - KO: file not opened: - file is empty
1256 * - low level read error
1257 *----------------------------------------------------------------------------
1258 * PURPOSE:
1259 * Open the file in read or write mode
1260 *----------------------------------------------------------------------------
1261 * EXAMPLE:
1262 * if (fat_get_root_directory(FILE_WAV) == OK) // Select first WAV file in root
1263 * {
1264 * fat_fopen(WRITE); // Open this file in WRITE mode
1265 * for (j = 0; j < 10; j++)
1266 * fat_fputc(buff[j]);
1267
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -