📄 mmc.lst
字号:
175 1 P[2]=(unsigned char)(Addr32>>8)&0xFE;
176 1 P[1]=(unsigned char)(Addr32>>16);
177 1 P[0]=(unsigned char)(Addr32>>32);
C51 COMPILER V7.01 MMC 06/30/2007 11:42:51 PAGE 4
178 1 MMC_CS=0;
179 1
180 1 SPI_SEND(0xff);
181 1 temp=MMC_SEND_CMD(MMC_WRITE_BLOCK,P[0],P[1],P[2],0);
182 1 for(counter=0;counter<255;counter++)
183 1 SPI_SEND(0xff);
184 1
185 1 SPI_SEND(0xfe); //发送起始字节
186 1
187 1 for(counter=0;counter<512;counter++) //发送数据
188 1 {
189 2 SPI_SEND(buf[counter]);
190 2 }
191 1
192 1 temp=SPI_SEND(0xff); //CRC码
193 1 temp=SPI_SEND(0xff);
194 1
195 1 counter=0;
196 1 do
197 1 {
198 2 temp=SPI_SEND(0xff);
199 2 counter++;
200 2 }
201 1 while( (temp&0x0f)!=5 && counter<255); //等待返回写入成功字节xxx00101
202 1
203 1 if(counter==255) return KO;
204 1
205 1 temp=0;
206 1 while((temp==0)&&counter<512)
207 1 {
208 2 temp=SPI_SEND(0xff);
209 2 counter++;
210 2 delay(10);
211 2 }
212 1 MMC_CS=1;
213 1 if(counter==512)
214 1 return KO;
215 1 return OK;
216 1 }
217
218 void CheckCard()
219 {
220 1 unsigned long ReservedSector=0;
221 1 unsigned long RootEntries=0;
222 1 unsigned long SectorsperFAT=0;
223 1 MMC_block_read(MMCDataBuf,0); //读入DBR,分析BPB
224 1
225 1 SecPClu=MMCDataBuf[0x0d];
226 1 BytePClu=SecPClu*512;
227 1
228 1 ReservedSector=MMCDataBuf[0x0e]+MMCDataBuf[0x0f]*256;
229 1 RootEntries=MMCDataBuf[0x11]+MMCDataBuf[0x12]*256;
230 1 SectorsperFAT=MMCDataBuf[0x16]+MMCDataBuf[0x17]*256;
231 1
232 1 FAT1_ORI=ReservedSector*512;
233 1 FAT2_ORI=FAT1_ORI+SectorsperFAT*512;
234 1 DIR_ORI=FAT2_ORI+SectorsperFAT*512;
235 1 DIR_LEN=RootEntries*32;
236 1 CLU_ORI=DIR_ORI+DIR_LEN-2*BytePClu;
237 1
238 1 return ;
239 1 }
C51 COMPILER V7.01 MMC 06/30/2007 11:42:51 PAGE 5
240
241 //////////////////////////////////////////////////////////////////////////////////////////////////
242 // 文件层数据读取 //
243 //////////////////////////////////////////////////////////////////////////////////////////////////
244 unsigned int SearchEmptyCluster() //在FAT表中搜寻未被占用的簇号
245 { //成功返回簇号,失败返回0
246 1 unsigned long Addr32=FAT1_ORI;
247 1 unsigned int counter=0;
248 1 int i;
249 1 while(Addr32<FAT2_ORI) //搜索FAT1表
250 1 {
251 2 MMC_block_read(MMCDataBuf,Addr32); //读入一块
252 2 for(i=0;i<512;i+=2)
253 2 {
254 3 if(MMCDataBuf[i]==0 && MMCDataBuf[i+1]==0) //判断是否为空白块
255 3 {
256 4 MMCDataBuf[i]=0xFF;
257 4 MMCDataBuf[i+1]=0xFF;
258 4
259 4 if(MMC_block_write(MMCDataBuf,Addr32)==OK) //将其占用
260 4 return counter*256+i/2;
261 4 else
262 4 return 0;
263 4 }
264 3 }
265 2 counter++;
266 2 Addr32+=512;
267 2 }
268 1 return 0;
269 1 }
270
271 unsigned int Find_NextN_Cluster(unsigned int First_Cluster,unsigned int n) //计算给定簇的下N个簇序号
272 { //超过文件界限返回0
273 1 int i;
274 1 unsigned int temp=First_Cluster;
275 1 if(n==0)
276 1 return First_Cluster;
277 1 for(i=0;i<n;i++)
278 1 {
279 2 MMC_block_read(MMCDataBuf,FAT1_ORI+temp*2);
280 2 temp=MMCDataBuf[(temp%256)*2]+MMCDataBuf[(temp%256)*2+1]*0x100;
281 2 if(temp==0xFFFF)
282 2 break;
283 2 }
284 1 if(i>=n-1)
285 1 return temp;
286 1 else
287 1 return 0;
288 1 }
289
290 int ReadFile(FILESTRUCT *fp,unsigned long offset) //读取文件fp偏移offset所在块到buf,
291 { //返回offset在buf中的偏移,失败返回-1
292 1 unsigned long sector_sn=offset/512;
293 1 unsigned int cluster_sn=sector_sn/SecPClu;
294 1 int sector_offset=offset%512;
295 1 char cluster_0ffset=sector_sn%SecPClu;
296 1 unsigned long temp=Find_NextN_Cluster(fp->firstcluster,cluster_sn);
297 1 if(fp->firstcluster==0)
298 1 {
299 2 if(fp->attribute==0) //读根目录文件
300 2 {
301 3 if(offset>=DIR_LEN)
C51 COMPILER V7.01 MMC 06/30/2007 11:42:51 PAGE 6
302 3 return -1;
303 3 MMC_block_read(MMCDataBuf,DIR_ORI+offset);
304 3 }
305 2 else //读的是空文件
306 2 {
307 3 return -1;
308 3 }
309 2 }
310 1 else //普通文件
311 1 {
312 2 if (temp==0xFFFF)
313 2 return -1;
314 2 MMC_block_read(MMCDataBuf,CLU_ORI+temp*BytePClu+cluster_0ffset*0x200);
315 2 }
316 1 SFRPAGE=0x0f;
317 1 return sector_offset;
318 1 }
319
320 int WriteFile(unsigned char *buf,FILESTRUCT *fp,unsigned long offset) //将512字节buf内容写入fp文件offset偏
-移处所在块
321 {
322 1 unsigned long sector_sn=offset/512;
323 1 unsigned int cluster_sn=sector_sn/SecPClu;
324 1 int sector_offset=offset%512;
325 1 char cluster_0ffset=sector_sn%SecPClu;
326 1 unsigned int Temp1,Temp2;
327 1
328 1 if(fp->firstcluster==0)
329 1 {
330 2 if(offset>DIR_LEN)
331 2 return -1;
332 2 MMC_block_write(buf,DIR_ORI+offset);
333 2 return 1;
334 2 }
335 1
336 1 Temp1=Find_NextN_Cluster(fp->firstcluster,cluster_sn);
337 1 if(Temp1==0) //偏移越界,写文件失败,返回
338 1 return -1;
339 1 if(Temp1==0xFFFF) //需要分配新簇
340 1 {
341 2 Temp1=SearchEmptyCluster();
342 2 //将新簇链接到FAT1表中
343 2 Temp2=Find_NextN_Cluster(fp->firstcluster,cluster_sn-1)*2;
344 2 MMC_block_read(MMCDataBuf,FAT1_ORI+Temp2);
345 2 MMCDataBuf[Temp2%512]=Temp1;
346 2 MMCDataBuf[Temp2%512+1]=Temp1>>8;
347 2 MMC_block_write(MMCDataBuf,FAT1_ORI+Temp2);
348 2 }
349 1 MMC_block_write(buf,CLU_ORI+Temp1*BytePClu+cluster_0ffset*0x200);
350 1 return 1;
351 1 }
352 //////////////////////////////////////////////////////////////////////////////////////////////////
353 // 应用层显示目录与浏览文本文件 //
354 //////////////////////////////////////////////////////////////////////////////////////////////////
355 void Get_File_List(unsigned long addr32,int From) //获取DataBuf中从From开始的目录信息,直到Found=13或查
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -