DSTATUS disk_initialize ( BYTE drv /* Physical drive nmuber (0) */ ) { UINT cmd, n; DWORD resp[4]; BYTE ty; if (drv) return STA_NOINIT; /* Supports only single drive */ if (Stat & STA_NODISK) return Stat; /* No card in the socket */ power_on(); /* Force socket power on */ MCI_CLOCK = 0x100 | (PCLK/MCLK_ID/2-1); /* Set MCICLK = MCLK_ID */ for (Timer[0] = 2; Timer[0]; ); LEDR_ON(); send_cmd(CMD0, 0, 0, NULL); /* Enter idle state */ CardRCA = 0; /*---- Card is 'idle' state ----*/ Timer[0] = 1000; /* Initialization timeout of 1000 msec */ if (send_cmd(CMD8, 0x1AA, 1, resp) /* SDC Ver2 */ && (resp[0] & 0xFFF) == 0x1AA) { /* The card can work at vdd range of 2.7-3.6V */ do { /* Wait while card is busy state (use ACMD41 with HCS bit) */ if (!Timer[0]) goto di_fail; } while (!send_cmd(ACMD41, 0x40FF8000, 1, resp) || !(resp[0] & 0x80000000)); ty = (resp[0] & 0x40000000) ? CT_SD2 | CT_BLOCK : CT_SD2; /* Check CCS bit in the OCR */ } else { /* SDC Ver1 or MMC */ if (send_cmd(ACMD41, 0x00FF8000, 1, resp)) { ty = CT_SD1; cmd = ACMD41; /* ACMD41 is accepted -> SDC Ver1 */ } else { ty = CT_MMC; cmd = CMD1; /* ACMD41 is rejected -> MMC */ } do { /* Wait while card is busy state (use ACMD41 or CMD1) */ if (!Timer[0]) goto di_fail; } while (!send_cmd(cmd, 0x00FF8000, 1, resp) || !(resp[0] & 0x80000000)); } CardType = ty; /* Save card type */ bswap_cp(&CardInfo[32], resp); /* Save OCR */ /*---- Card is 'ready' state ----*/ if (!send_cmd(CMD2, 0, 2, resp)) goto di_fail; /* Enter ident state */ for (n = 0; n < 4; n++) bswap_cp(&CardInfo[n * 4 + 16], &resp[n]); /* Save CID */ /*---- Card is 'ident' state ----*/ if (ty & CT_SDC) { /* SDC: Get generated RCA and save it */ if (!send_cmd(CMD3, 0, 1, resp)) goto di_fail; CardRCA = (WORD)(resp[0] >> 16); } else { /* MMC: Assign RCA to the card */ if (!send_cmd(CMD3, 1 << 16, 1, resp)) goto di_fail;
static T_uezError SDCard_MS_MCI_Init(void *aWorkspace, TUInt32 aAddress) { T_MassStorage_SDCard_MCI_Workspace *p = (T_MassStorage_SDCard_MCI_Workspace *)aWorkspace; TUInt16 cmd; TUInt32 n; TUInt32 response[4]; TUInt8 type; TUInt32 timeStart; T_msSizeInfo si; T_uezError error; /* No card in the socket? */ if (p->iStat & STA_NODISK) return UEZ_ERROR_DEVICE_NOT_FOUND; // If already initialized, we are done! if (p->iInitPerformed) return UEZ_ERROR_NONE; (*p->iMCI)->PowerOn(p->iMCI); /* Force socket power on */ (*p->iMCI)->SetClockRate(p->iMCI, SDCARD_MCI_RATE_FOR_ID_STATE, EFalse); UEZTaskDelay(2); (*p->iMCI)->SendCommand(p->iMCI, MCI_CMD0, 0, 0, NULL); /* Put the card into idle state */ p->iRCA = 0; /*---- Card is 'idle' state ----*/ /* Initialization timeout of SDCARD_INIT_TIMEOUT msec */ // Timer[0] = 1000; timeStart = UEZTickCounterGet(); if ((*p->iMCI)->SendCommand(p->iMCI, MCI_CMD8, 0x1AA, 1, response) && (response[0] & 0xFFF) == 0x1AA) { /* Card is SDC Ver2 */ /* The card can work at vdd range of 2.7-3.6V */ /* Wait while card is busy state (use MCI_ACMD41 with HCS bit) */ do { /* This loop will take a time. Insert task rotation here for multitask envilonment. */ UEZTaskDelay(1); if (UEZTickCounterGetDelta(timeStart) >= SDCARD_INIT_TIMEOUT) goto di_fail; } while (!(*p->iMCI)->SendCommand(p->iMCI, MCI_ACMD41, 0x40FF8000, 1, response) || !(response[0] & 0x80000000)); type = (response[0] & 0x40000000) ? CT_SD2 | CT_BLOCK : CT_SD2; /* Check CCS bit in the OCR */ } else { /* Card is SDC Ver1 or MMC */ if ((*p->iMCI)->SendCommand(p->iMCI, MCI_ACMD41, 0x00FF8000, 1, response)) { type = CT_SD1; cmd = MCI_ACMD41; /* MCI_ACMD41 is accepted -> SDC Ver1 */ } else { type = CT_MMC; cmd = MCI_CMD1; /* MCI_ACMD41 is rejected -> MMC */ } /* Wait while card is busy state (use MCI_ACMD41 or MCI_CMD1) */ do { /* This loop will take a time. Insert task rotation here for multitask envilonment. */ UEZTaskDelay(1); if (UEZTickCounterGetDelta(timeStart) >= SDCARD_INIT_TIMEOUT) goto di_fail; } while (!(*p->iMCI)->SendCommand(p->iMCI, cmd, 0x00FF8000, 1, response) || !(response[0] & 0x80000000)); } /* Save card type */ p->iCardType = type; bswap_cp(&p->iCardInfo[32], response); /* Save OCR */ /*---- Card is 'ready' state ----*/ if (!(*p->iMCI)->SendCommand(p->iMCI, MCI_CMD2, 0, 2, response)) goto di_fail; /* Enter ident state */ for (n = 0; n < 4; n++) bswap_cp(&p->iCardInfo[n * 4 + 16], &response[n]); /* Save CID */ /*---- Card is 'ident' state ----*/ if (type & CT_SDC) { /* SDC: Get generated RCA and save it */ if (!(*p->iMCI)->SendCommand(p->iMCI, MCI_CMD3, 0, 1, response)) goto di_fail; p->iRCA = (TUInt16)(response[0] >> 16); } else { /* MMC: Assign RCA to the card */ if (!(*p->iMCI)->SendCommand(p->iMCI, MCI_CMD3, 1 << 16, 1, response))
DSTATUS disk_initialize ( BYTE drv /* Physical drive nmuber (0) */ ) { // BYTE n, cmd, ty, ocr[4]; UINT cmd, n; DWORD resp[4]; BYTE ty; SDC_PRMS_T params; if (!sdcard_init(¶ms)) { return STA_NOINIT; } if (drv) return STA_NOINIT; /* Supports only single drive */ if (Stat & STA_NODISK) return Stat; /* No card in the socket */ power_on(); /* Force socket power on */ for (Timer1 = 2; Timer1; ); send_cmd(CMD0, 0, 0, NULL); /* Enter idle state */ CardRCA = 0; Timer1 = 10; while(Timer1); /*---- Card is 'idle' state ----*/ Timer1 = 100; /* Initialization timeout of 1000 msec */ if (send_cmd(CMD8, 0x1AA, 1, resp) /* SDC Ver2 */ && (resp[0] & 0xFFF) == 0x1AA) { /* The card can work at vdd range of 2.7-3.6V */ do { /* Wait while card is busy state (use ACMD41 with HCS bit) */ /* This loop will take a time. Insert wai_tsk(1) here for multitask envilonment. */ if (!Timer1) goto di_fail; } while (!send_cmd(ACMD41, 0x40FF8000, 1, resp) || !(resp[0] & 0x80000000)); ty = (resp[0] & 0x40000000) ? CT_SD2|CT_BLOCK : CT_SD2; /* Check CCS bit in the OCR */ } else { /* SDC Ver1 or MMC */ if (send_cmd(ACMD41, /*0x00FF8000*/0, 1, resp)) { ty = CT_SD1; cmd = ACMD41; /* ACMD41 is accepted -> SDC Ver1 */ } else { ty = CT_MMC; cmd = CMD1; /* ACMD41 is rejected -> MMC */ } do { /* Wait while card is busy state (use ACMD41 or CMD1) */ /* This loop will take a time. Insert wai_tsk(1) here for multitask envilonment. */ if (!Timer1) { Timer1 = Timer1; goto di_fail; } } while (!send_cmd(cmd, 0x00FF8000, 1, resp) || !(resp[0] & 0x80000000)); } CardType = ty; /* Save card type */ bswap_cp(&CardInfo[32], resp); /* Save OCR */ /*---- Card is 'ready' state ----*/ if (!send_cmd(CMD2, 0, 2, resp)) goto di_fail; /* Enter ident state */ for (n = 0; n < 4; n++) bswap_cp(&CardInfo[n * 4 + 16], &resp[n]); /* Save CID */ /*---- Card is 'ident' state ----*/ if (ty & CT_SDC) { /* SDC: Get generated RCA and save it */ if (!send_cmd(CMD3, 0, 1, resp)) goto di_fail; CardRCA = (WORD)(resp[0] >> 16); } else { /* MMC: Assign RCA to the card */ if (!send_cmd(CMD3, 1 << 16, 1, resp)) goto di_fail;