📄 sd.lst
字号:
236
237 //********************************************************************************************************
-***************
238 //函数作用:读取当前扇区的某个字节
239 //参数说明:
240 //注意事项:
241 //返回说明:无
242 //********************************************************************************************************
-***************
243 uchar SD_Read_SectorByte(uint ByteAddr, uchar *ch)
244 {
245 1 uint data a;
246 1 uchar data retry=0;
247 1
248 1 //Send the read command
249 1 while(SD_Send_Cmd()!=0)
250 1 {
251 2 dog();
252 2 if(retry++>200)return 1;
253 2 }
254 1 //Send the start byte
255 1 retry=0;
256 1 Data=0;
257 1 while(Data!=0xfe)
258 1 {
259 2 sd_read();
260 2 if(++retry>=200)return 1;
261 2 dog();
262 2 }
263 1 //Read off all the bytes in the block
264 1 for(a=0;a<512;a++)
265 1 {
266 2 sd_read();
267 2 if(a==ByteAddr)*ch=Data;
268 2 }
269 1 //Read CRC byte
270 1 sd_read();
271 1 sd_read();
272 1
273 1 // Set SD_Chip_Select to high
274 1 SD_CS=1;
275 1 return 0;
276 1 }
277
278 //********************************************************************************************************
-***************
279 //函数作用:读取当前扇区的某些字节
280 //参数说明:
281 //注意事项:
282 //返回说明:无
283 //********************************************************************************************************
-***************
284 uchar SD_Read_Sector_Offset(uint Offset, uchar *buf, uint length)
285 {
286 1 uint data a;
C51 COMPILER V7.20 SD 11/03/2007 17:08:47 PAGE 6
287 1 uchar data retry=0;
288 1
289 1 //Send the read command
290 1 while(SD_Send_Cmd()!=0)
291 1 {
292 2 dog();
293 2 if(retry++>200)return 1;
294 2 }
295 1 //Send the start byte
296 1 retry=0;
297 1 Data=0;
298 1 while(Data!=0xfe)
299 1 {
300 2 sd_read();
301 2 if(++retry>=200)return 1;
302 2 dog();
303 2 }
304 1 //Read all the bytes in the block
305 1 for(a=0;a<512;a++)
306 1 {
307 2 sd_read();
308 2 if(a>=Offset && length)
309 2 {
310 3 *buf=Data;
311 3 buf++;
312 3 length--;
313 3 }
314 2 }
315 1 //Read CRC byte
316 1 sd_read();
317 1 sd_read();
318 1
319 1 // Set SD_Chip_Select to high
320 1 SD_CS=1;
321 1 return 0;
322 1 }
323
324 //9.write 512 bytes to a given sector from a Byte_byte_long Buffer
325 /*
326 uchar SD_Write_Sector(ulong addr,uchar *Buffer)
327 {
328 uint xdata a;
329 uchar xdata retry,temp;
330 uchar xdata CMD[]={0x58,0x00,0x00,0x00,0x00,0xFF};//cmd24
331
332 addr = addr << 9; //addr = addr * 512
333 CMD[1]=((addr&0xFF000000)>>24);
334 CMD[2]=((addr&0x00FF0000)>>16);
335 CMD[3]=((addr&0x0000FF00)>>8);
336 CMD[4]=(addr&0x000000FF);
337 //Send the write command
338 while(SD_Send_Cmd(CMD)!=0)
339 {
340 if (retry++>50)return 1;
341 }
342 //Send the start byte
343 sd_write(0xfe);
344 //Read off all the bytes in the block
345 for(a=0;a<512;a++)
346 {
347 sd_write(*Buffer++);
348 }
C51 COMPILER V7.20 SD 11/03/2007 17:08:47 PAGE 7
349 while((temp=sd_read())&0x10);
350 if((temp&0x0f)!=0x05)return 2;
351 //Read CRC byte
352 while(!SD_DO);//detect if data_in pb.4 is still busy(high)
353
354 // Set SD_Chip_Select to high
355 SD_CS=1;
356 return 0;
357 }
358
359 uchar SD_Read_Sector(ulong addr, uchar *Buffer)
360 {
361 uint xdata a;
362 uchar xdata retry;
363 uchar xdata CMD[]={0x51,0x00,0x00,0x00,0x00,0xFF};//cmd17
364
365 addr = addr << 9; //addr = addr * 512
366 CMD[1]=((addr&0xFF000000)>>24);
367 CMD[2]=((addr&0x00FF0000)>>16);
368 CMD[3]=((addr&0x0000FF00)>>8);
369 CMD[4]=(addr&0x000000FF);
370 //Send the read command
371 while(SD_Send_Cmd(CMD)!=0)
372 {
373 if (retry++>200)return 1;
374 }
375 //Send the start byte
376 while(sd_read()!=0xfe)
377 {
378
379 }
380 //Read off all the bytes in the block
381 for(a=0;a<512;a++)
382 {
383 *Buffer=sd_read();
384 Buffer++;
385 }
386 //Read CRC byte
387 sd_read();
388 sd_read();
389
390 // Set SD_Chip_Select to high
391 SD_CS=1;
392 return 0;
393 }
394 */
395
396
397
398
399
400 /*
401 //6.硬spi写指令及读响应
402 uchar SD_Send_Cmd(char *CMD)
403 {
404 uchar a;
405 uchar tmp=0xff;
406 uchar Timeout=0;
407 // Raise chip select -----/ss=1
408 SD_CS=1();
409 // Send an 8 bit pulse
410 Write_Byte_SD(0xFF);
C51 COMPILER V7.20 SD 11/03/2007 17:08:47 PAGE 8
411
412 // Lower chip select
413 SD_CS=0;
414 //Send the 6 byte command
415 for(a=0;a<0x06;a++)
416 {
417 Write_Byte_SD(*CMD++);
418 }
419 //Wait for the response
420 while(tmp==0xff)//
421 {
422 tmp=Read_Byte_SD();
423 if(Timeout++>100)
424 {
425 break;
426 }
427 }
428 //for some reason we need to delay here
429 //delay_1ms();
430 return(tmp);//the respone of the byte_write_operation
431 }
432
433
434
435
436
437
438
439 //read xx bytes no matter of misalignment!!
440 uchar read_antimisaliment(unsigned long addr_temp,uchar *p_buffer, uint length)
441 {
442 uint _length=0x0000;
443 SD_CS=0;
444 while(SD_read_sector(addr_temp,p_buffer,length))
445 {
446 SD_CS=0;//
447 length-=0x0001;//to find a suuitable length to avoid misalignment
448 _length+=0x0001;// _length+length==xx
449 SD_set_length(length);
450 }
451 ///
452 if(_length==0x0000)
453 {
454 return 0;
455 }
456 ///
457 addr_temp+=length;
458 SD_CS=0;//
459 SD_set_length(_length);
460 SD_CS=0;//
461 while(SD_read_sector(addr_temp,p_buffer,_length))
462 {
463 SD_CS=0;
464 }
465 SD_CS=0;//
466 SD_set_length(length+_length);//to read the rest bytes of the xx bytes
467 return 0;
468 /////////////////
469 }
470
471
472
C51 COMPILER V7.20 SD 11/03/2007 17:08:47 PAGE 9
473 */
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 785 ----
CONSTANT SIZE = 6 ----
XDATA SIZE = 6 19
PDATA SIZE = ---- ----
DATA SIZE = 1 6
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -