⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.lst

📁 详细介绍了关于CF卡的存储结构
💻 LST
📖 第 1 页 / 共 5 页
字号:
 945   2                      ((unsigned char*)(&dir))[i]=' ';
 946   2              }
 947   1              dir.file_name[0]='.';                           //current dir
 948   1              dir.file_attribute=0x10;
 949   1              dir.first_clust[0]=(sector)%256;
 950   1              dir.first_clust[1]=(sector)/256;
 951   1              for(i=0;i<512;i++)
 952   1              {
 953   2                      data_buf[i]=0;
 954   2              }
 955   1              for(i=0;i<32;i++)
 956   1              {
 957   2                      data_buf[i]=((unsigned char*)(&dir))[i];
 958   2              }
 959   1              temp_sec=(parent_dir_sector
 960   1                              -(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))
 961   1                              /bootinfo.sec_per_clust+2; 
 962   1              dir.file_name[1]='.';                           //parent dir
 963   1              dir.first_clust[0]=temp_sec%256;
 964   1              dir.first_clust[1]=temp_sec/256;
 965   1              for(i=0;i<32;i++)
 966   1              {
 967   2                      data_buf[32+i]=((unsigned char*)(&dir))[i];
 968   2              }
 969   1              if(WriteOneSec(data_buf,(sector-2)*bootinfo.sec_per_clust
 970   1                                      +(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))==0)
 971   1              {
 972   2                      return -1;
 973   2              }
 974   1              return 1;
 975   1      }
 976          
C51 COMPILER V7.07   MAIN                                                                  12/01/2005 22:22:50 PAGE 17  

 977          //char PutData(unsigned char* file_data,unsigned int data_len)
 978          char CFWriteFile(unsigned char* file_data,unsigned int data_len)
 979          {
 980   1              xdata unsigned int i;
 981   1              xdata int un_write_len=0;
 982   1      //      xdata unsigned int current_file_pt;
 983   1              static xdata unsigned int next_clustor;
 984   1      //    xdata unsigned int sector;
 985   1      #ifdef DEBUG
                      printf("\nstart clustor=%x\n",s_fileinfo.start_clustor);
                      printf("current sec=%d\n",(int)s_fileinfo.current_sector);
                      printf("in sec pos=%x\n",s_fileinfo.current_in_sector_pos);
                      printf("size=%x,%u,%l\n",s_fileinfo.size,s_fileinfo.size,s_fileinfo.size);
                      printf("buf pos=%d,%u,%d\n",(int)buf_pos,(long)buf_pos,buf_pos);
              #endif
 992   1              if(data_len<=512)
 993   1              {
 994   2      #ifdef DEBUG
                              printf("buf_pos=%d\n",(int)buf_pos);
              #endif
 997   2                      un_write_len=data_len;
 998   2                      for(i=buf_pos;(i<512)&&(un_write_len>0);i++)
 999   2                      {
1000   3                              write_buf[i]=file_data[i-buf_pos];
1001   3                              un_write_len--;
1002   3                      }
1003   2                      s_fileinfo.size=s_fileinfo.size+i-buf_pos;
1004   2                      buf_pos=i;
1005   2      #ifdef DEBUG
                              printf("buf_pos=%d\n",(int)buf_pos);
              #endif
1008   2                      if(buf_pos>=512)
1009   2                      {
1010   3      #ifdef DEBUG
                                      printf("start w\n");
              #endif
1013   3                              if(WriteOneSec(write_buf,(s_fileinfo.start_clustor-2)
1014   3                                      *bootinfo.sec_per_clust
1015   3                                      +(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512)
1016   3                                      +s_fileinfo.current_sector)==0)
1017   3                              {
1018   4      #ifdef DEBUG
                                              printf("www\n");
              #endif
1021   4                                      return -1;
1022   4                              }
1023   3      //                      s_fileinfo.size=s_fileinfo.size+buf_pos;
1024   3                              buf_pos=0;
1025   3                              s_fileinfo.current_sector++;
1026   3      #ifdef DEBUG
                              printf("_in_buf_current_sector=%d\n",(int)s_fileinfo.current_sector);
              #endif
1029   3      #ifdef DEBUG
                              printf("_in_buf_pos=%d\n",(int)buf_pos);
              #endif
1032   3                      }
1033   2                      if(un_write_len>0)
1034   2                      {
1035   3      #ifdef DEBUG
                                      printf("a\n");
              #endif
1038   3                              for(i=0;i<un_write_len;i++)
C51 COMPILER V7.07   MAIN                                                                  12/01/2005 22:22:50 PAGE 18  

1039   3                              {
1040   4                                      write_buf[i]=file_data[i+(data_len-un_write_len)];
1041   4                                      buf_pos++;
1042   4                              }
1043   3      
1044   3      #ifdef DEBUG
                              printf("un buf_pos=%d\n",(int)buf_pos);
              #endif
1047   3                              s_fileinfo.size=s_fileinfo.size+i;  
1048   3                      }
1049   2                      if(s_fileinfo.current_sector>=bootinfo.sec_per_clust)           //means one clustor written
1050   2                      {
1051   3      #ifdef DEBUG
                                      printf("b\n");
              #endif
1054   3                              s_fileinfo.current_sector=0;
1055   3      //                      if(SerchFreeFAT(&next_clustor,1,s_fileinfo.in_fat_sec)==0)              //search Fat table from last sector
1056   3                              if(SerchFreeFAT(&next_clustor,1,1)==0)          //search Fat table from last sector
1057   3                              {
1058   4                                      putchar('n');
1059   4      #ifdef DEBUG
                                              printf("no free fat\n");
              #endif
1062   4                                      return -1;
1063   4                              }
1064   3      #ifdef DEBUG
                                              printf("current clustor=%x\n",s_fileinfo.start_clustor);
                                              printf("next clustor=%x\n",next_clustor);
              #endif
1068   3                              if(ReadOneSec((s_fileinfo.start_clustor*2)/512+1)==0)
1069   3                              {
1070   4                                      return -1;
1071   4                              }
1072   3                              data_buf[(s_fileinfo.start_clustor*2)%512]=next_clustor%256;
1073   3                              data_buf[(s_fileinfo.start_clustor*2)%512+1]=next_clustor/256;
1074   3                              if(WriteOneSec(data_buf,(s_fileinfo.start_clustor*2)/512+1)==0)
1075   3                              {
1076   4                                      return -1;              //write fdt error
1077   4                              }
1078   3      
1079   3                              s_fileinfo.start_clustor=next_clustor; 
1080   3                              s_fileinfo.in_fat_sec=(next_clustor*2)/512+1;
1081   3      
1082   3                              if(ReadOneSec((s_fileinfo.start_clustor*2)/512+1)==0)           //read one sector in Fat table area
1083   3                              {
1084   4                                      return -1;
1085   4                              }
1086   3                              for(i=(s_fileinfo.start_clustor*2)%512;i<(s_fileinfo.start_clustor*2)%512+2;i++)        //wrire fat table
1087   3                              {
1088   4                                      data_buf[i]=0xff;
1089   4                              }
1090   3                              if(WriteOneSec(data_buf,(s_fileinfo.start_clustor*2)/512+1)==0)
1091   3                              {
1092   4                                      return -1;              //write fdt error
1093   4                              }
1094   3      //                      for(buf_pos=0;buf_pos<unuse_len;buf_pos++)
1095   3      //                      {
1096   3      //                              write_buf[buf_pos]=file_data[data_len-unuse_len+buf_pos];
1097   3      //                      }
1098   3      //                      s_fileinfo.size=s_fileinfo.size+buf_pos;
1099   3                      }
1100   2                      return 1;
C51 COMPILER V7.07   MAIN                                                                  12/01/2005 22:22:50 PAGE 19  

1101   2              }
1102   1              else
1103   1              {
1104   2                      return -1;              //data large then 512 bytes
1105   2              }
1106   1              //the data is more then one sector
1107   1      /*
1108   1              if(data_len>512)
1109   1              {
1110   1                      printf("c\n");
1111   1                      if(s_fileinfo.current_in_sector_pos!=0)         //the target sector is used
1112   1                      {
1113   1                              if(ReadOneSec(((s_fileinfo.start_clustor+s_fileinfo.current_sector)/2-2)*bootinfo.sec_per_clust
1114   1                                      +(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))==0)
1115   1                              {
1116   1                                      return -1;
1117   1                              }
1118   1                              for(i=0;i<512-s_fileinfo.current_in_sector_pos;i++)
1119   1                              {
1120   1                                      data_buf[s_fileinfo.current_in_sector_pos+i]=file_data[i];
1121   1                              }
1122   1                              if(WriteOneSec(data_buf,((s_fileinfo.start_clustor+s_fileinfo.current_sector)/2-2)*bootinfo.sec_per_clu
             -st
1123   1                                      +(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))==0)
1124   1                              {
1125   1                                      return -1;
1126   1                              }
1127   1                              s_fileinfo.current_sector++;
1128   1                              current_file_pt=512-s_fileinfo.current_in_sector_pos;
1129   1                              for(i=0;i<(data_len-current_file_pt)/512;i++)
1130   1                              {
1131   1                                      if(WriteOneSec(file_data+current_file_pt+i*512,
1132   1                                                      ((s_fileinfo.start_clustor+s_fileinfo.current_sector+i+1)/2-2)
1133   1                                                      *bootinfo.sec_per_clust
1134   1                                                      +(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))==0)
1135   1                                      {
1136   1                                              return -1;
1137   1                                      }
1138   1                                      s_fileinfo.current_sector++; 
1139   1      //                              if(s_fileinfo.current_sector%4==0)
1140   1      //                              {
1141   1      //                                      s_fileinfo.current_in_sector_pos=s
1142   1                              }
1143   1      
1144   1                      }
1145   1                      if(data_len-s_fileinfo.current_in_sector_pos>512*bootinfo.sec_per_clust)
1146   1                      {
1147   1                              if(WriteOneSec(file_data,(s_fileinfo.current_sector/2-2)*bootinfo.sec_per_clust
1148   1                                              +(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))==0)
1149   1                              {
1150   1                                      return -1;                      //Write file error
1151   1                              }
1152   1                              if(ReadOneSec(s_fileinfo.current_sector/512+1)==0)              //read one sector in Fat table area
1153   1                              {
1154   1                                      return 0xffff;
1155   1                              }
1156   1                              for(i=1;i<bootinfo.fat_size;i++)
1157   1                              {
1158   1                                      SerchFreeFAT(&next_clustor,1,i);
1159   1                              }
1160   1                              for(i=s_fileinfo.current_sector%512;i<s_fileinfo.current_sector%512+2;i++)      //wrire fat table
1161   1                              {
C51 COMPILER V7.07   MAIN                                                                  12/01/2005 22:22:50 PAGE 20  

1162   1                                      data_buf[i]=0xff;
1163   1                              }
1164   1                              if(WriteOneSec(data_buf,s_fileinfo.current_in_sector_pos/512+1)==0)
1165   1                              {
1166   1                                      return 0xffff;          //write fdt error
1167   1                              }
1168   1                              for(i=0;i<(data_len-512)/512;i++)
1169   1                              {
1170   1      //                              data_buf[i]=
1171   1                              }
1172   1                      }
1173   1      //              s_fileinfo.
1174   1      //              s_fileinfo.current_sector
1175   1              }
1176   1              else            //the data to be written is less then one clustor
1177   1              {
1178   1                      current_file_pt=0;
1179   1                      if(s_fileinfo.current_in_sector_pos>0)          //the target sector is used
1180   1                      {
1181   1                              if(ReadOneSec(((s_fileinfo.start_clustor+s_fileinfo.current_sector)/2-2)*bootinfo.sec_per_clust
1182   1                                      +(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))==0)
1183   1 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -