📄 sd.lst
字号:
505 4
506 4 //Status = SdReadExtCSD();
507 4 }
508 3 }
509 2 else // MMC 3.1 只支持1bit模式
510 2 {
511 3 SDMI_CTL&=~bmSD_BUS_4BIT_EN;
512 3 SDMI_CTL&=~bmSD_BUS_8BIT_EN;
513 3 }
514 2 }
515 1 return Status;
516 1 }
517 #ifdef DEBUG_FLASH_ERASE
STATUS SdReadExtCSD(void)
{
STATUS Status;
int RetryCount1 , RetryCount2;
BOOL ReadyForData;
// if (!SdCardExist())
// return STATUS_FLASH_ERROR;
SdWaitCard();
for(RetryCount1 = 0; RetryCount1 < 5; RetryCount1++)
{
ReadyForData = FALSE;
for(RetryCount2 = 0; RetryCount2 < 20; RetryCount2++)
{
*((INT32U *)(&SdCmdBuf[1])) = RCA;
Status = SdSendCmd(SEND_STATUS, SdCmdBuf);
if(Status != STATUS_SUCCESS)
continue;
if(SdRespBuf[3] & 0x01) // READY_FOR_DATA in Card Status
{
ReadyForData = TRUE;
break;
}
}
if(!ReadyForData){
CmdTimer=3;
while(CmdTimer);
continue;
}
SDMI_BUFBASE=0xEC; //FIFOB0--11.30 DEREK
C51 COMPILER V7.02b SD 03/24/2008 15:52:29 PAGE 10
//注意:EXT CSD的512字节数据在FIFO中从低到高排列~!
*((INT32U *)(&SdCmdBuf[1])) = 0;
Status = SdSendCmd(SEND_EXT_CSD, SdCmdBuf);
if(Status != STATUS_SUCCESS)
continue;
//*******************************
if(SDMI_ST&bmSD_CRC16_ERR)
{
SDMI_ST &=~bmSD_CRC16_ERR;
continue;
}
else
{
*((INT32U *)(&SdCmdBuf[1])) = RCA;
Status = SdSendCmd(SEND_STATUS, SdCmdBuf);
return STATUS_SUCCESS;
}
}
return STATUS_FLASH_ERROR;
}
#endif
574 //----------------------------------------------------------------------------
575 STATUS SdReset()
576 {
577 1 STATUS Status;
578 1 int i;
579 1
580 1 RCA = 0;
581 1
582 1 SdLoClk();
583 1 Delay(5); // Wait 5ms for stable clock
584 1 for(i = 1; i <= 4; i++)
585 1 SdCmdBuf[i] = 0;
586 1 Status = SdSendCmd(GO_IDLE_STATE, SdCmdBuf);
587 1 return Status;
588 1 }
589
590 //----------------------------------------------------------------------------
591 STATUS SdDetect(void)
592 {
593 1 STATUS Status;
594 1 UINT16 RetryCount;
595 1
596 1 *((UINT32 *)(&SdCmdBuf[1])) = OPERATION_VOL_RANGE;
597 1 if(SdVer2Flag)SdCmdBuf[1] = 0x40; //sd2.0卡支持high capacity support功能。Derek 2007.06.29
598 1 Status = SdSendAppCmd(SD_SEND_OP_COND, SdCmdBuf);
599 1
600 1 if(Status == STATUS_SUCCESS) //如果返回成功标志,说明是SD卡
601 1 {
602 2 CardType = CARD_SD;
603 2
604 2 // Check if card power-up operation finished
605 2 if(SdRespBuf[1] & CARD_PWRUP_FINISH)
606 2 return STATUS_SUCCESS;
607 2
608 2 // Wait until card power-up operation finished
609 2 for(RetryCount = 0; RetryCount < 500; RetryCount++)
610 2 {
611 3 *((UINT32 *)(&SdCmdBuf[1])) = OPERATION_VOL_RANGE; //2007.06.29
612 3 if(SdVer2Flag)SdCmdBuf[1] = 0x40; //Derek 2007.06.29
C51 COMPILER V7.02b SD 03/24/2008 15:52:29 PAGE 11
613 3 Status = SdSendAppCmd(SD_SEND_OP_COND, SdCmdBuf);
614 3
615 3 if(Status != STATUS_SUCCESS)
616 3 continue;
617 3
618 3 if(SdRespBuf[1] & CARD_PWRUP_FINISH)
619 3 return STATUS_SUCCESS;
620 3 }
621 2 return STATUS_FLASH_ERROR;
622 2 }
623 1 else //否则,是MMC卡
624 1 {
625 2 CardType = CARD_MMC;
626 2
627 2 // Wait until card power-up operation finished
628 2 for(RetryCount = 0; RetryCount < 500; RetryCount++)
629 2 {
630 3
631 3 *((UINT32 *)(&SdCmdBuf[1])) = OPERATION_VOL_RANGE;
632 3
633 3 Status = SdSendCmd(SEND_OP_COND, SdCmdBuf);
634 3
635 3 if(Status != STATUS_SUCCESS)
636 3 continue;
637 3
638 3 if(SdRespBuf[1] & CARD_PWRUP_FINISH)
639 3 return STATUS_SUCCESS;
640 3 }
641 2 return STATUS_FLASH_ERROR;
642 2 }
643 1 }
644
645 //-----用来检测是否Sd 2.0卡-----------------------------------------------------
646 STATUS SdSendIFCon(void)
647 {
648 1 STATUS Status;
649 1
650 1 SdCmdBuf[3] = 0X01;
651 1 SdCmdBuf[4] = 0XAA;
652 1
653 1 Status = SdSendCmd(SEND_IF_COND, SdCmdBuf);
654 1
655 1 return Status;
656 1 }
657
658 //----------------------------------------------------------------------------
659 STATUS SdGoIdentifyState(void)
660 {
661 1 STATUS Status;
662 1 int i;
663 1
664 1 for(i = 1; i <= 4; i++)
665 1 SdCmdBuf[i] = 0;
666 1 Status = SdSendCmd(ALL_SEND_CID, SdCmdBuf);
667 1 return Status;
668 1 }
669
670 //--------------------------------------------------------------------
671 STATUS SdGoStandbyState(void)
672 {
673 1 STATUS Status;
674 1 int i;
C51 COMPILER V7.02b SD 03/24/2008 15:52:29 PAGE 12
675 1
676 1 if(CardType == CARD_SD)
677 1 {
678 2 for(i = 1; i <= 4; i++)
679 2 SdCmdBuf[i] = 0;
680 2 }
681 1 else
682 1 {
683 2 RCA = 0x00010000;
684 2 *((UINT32 *)(&SdCmdBuf[1])) = RCA;
685 2 }
686 1
687 1 Status = SdSendCmd(SEND_RELATIVE_ADDR, SdCmdBuf);
688 1
689 1 if(CardType == CARD_SD)
690 1 RCA = *((UINT32 *)(&SdRespBuf[1])) & 0xffff0000;
691 1
692 1 return STATUS_SUCCESS;
693 1
694 1 // SD and MMC adopt different ways to set their relative card
695 1 // address. That is, they have different response to CMD3
696 1 // (SEND_RELATIVE_ADDR).
697 1 //
698 1 // According to SD Standard Version 1.01 page 25:
699 1 // the host issues CMD3 (SEND_RELATIVE_ADDR) asks the
700 1 // card to publish a new relative card address (RCA).
701 1 //
702 1 // According to MMC Standard Version 3.1 page 24:
703 1 // the host issues CMD3 (SEND_RELATIVE_ADDR) to assign
704 1 // to this card a relative card address (RCA).
705 1 //
706 1 // This means : when host send CMD3 to SD, it is SD telling host
707 1 // its new address, that is, SD is the one giving the new address
708 1 // (and host decides whether to accept it). Meanwhile, when host
709 1 // sends CMD3 to MMC, it is host assigning the card a new address,
710 1 // that is, host is the one decides the address (and MMC must
711 1 // accept it).
712 1 //
713 1 // For SD, CMD3 has a response of type R6, which contains address
714 1 // value returns by SD. For MMC, CMD3 has a response of type R1,
715 1 // which tells host if MMC accepts the address or error happens.
716 1 }
717
718 //--------------------------------------------------------------------
719 STATUS SdReadCSD(void)
720 {
721 1 STATUS Status;
722 1 BYTE READ_BL_LEN;
723 1 BYTE C_SIZE_MUTI;
724 1 UINT32 C_SIZE;
725 1 //SD 2.0和SD1.01格式不一样。C_SIZE【69:48】
726 1 *((UINT32 *)(&SdCmdBuf[1])) = RCA;
727 1
728 1 Status = SdSendCmd(SEND_CSD, SdCmdBuf); //读取SD卡的CSD信息
729 1 if(Status != STATUS_SUCCESS)
730 1 return Status;
731 1
732 1 if(CardType == CARD_MMC) {
733 2 if(((SdRespBuf[1]&0x3f)>>2)==4) MmcProtol = 1; //判断是何种卡4.0 ?3.1?
734 2 else MmcProtol = 0; //MMC 3.1
735 2 } else {
736 2 if(SdRespBuf[1]&0xC0) //2007.08.16-从SD CSD中判断是否HC SD。
C51 COMPILER V7.02b SD 03/24/2008 15:52:29 PAGE 13
737 2 SdHcFlag = 1;
738 2 else
739 2 SdHcFlag = 0;
740 2 }
741 1 if(SdHcFlag == 0) { //Derek 2007.07.02 V1.0,1.01的SD卡CSD
742 2 //Derek 06.09.20
743 2 READ_BL_LEN = SdRespBuf[6] & 0xf;
744 2 C_SIZE = SdRespBuf[7] & 0x3;
745 2 C_SIZE <<= 8;
746 2 C_SIZE |= SdRespBuf[8];
747 2 C_SIZE <<= 2;
748 2 C_SIZE |= (SdRespBuf[9] >> 6);
749 2 C_SIZE_MUTI = SdRespBuf[10] & 0x3;
750 2 C_SIZE_MUTI <<= 1;
751 2 C_SIZE_MUTI |= (SdRespBuf[11] >> 7);
752 2
753 2 SdAvailableBlocks = (UINT32)((C_SIZE + 1) << (C_SIZE_MUTI + 2 +
754 2 (READ_BL_LEN - BYTES_PER_SECTOR_SHIFT)));
755 2 } else { //V1.1,2.0的SD卡CSD CSIZE---CSD[69:48]
756 2 C_SIZE = SdRespBuf[8] & 0x3F;
757 2 C_SIZE <<= 8;
758 2 C_SIZE |= SdRespBuf[9];
759 2 C_SIZE <<= 8;
760 2 C_SIZE |= SdRespBuf[10];
761 2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -