void DRV_INIT() { INIT_SPI(); TRISE1=0; TRISA5=0; EN_GATE=1; }
//-------------------------------------------------------------------------- // Initialize Disk Drive //-------------------------------------------------------------------------- DSTATUS disk_initialize (void) { BYTE n, cmd, ty, ocr[4]; WORD tmr; INIT_SPI(); if ((PINB&_BV(SD_INS))!=0x00) return STA_NOINIT; #if _WRITE_FUNC if (MMC_SEL) disk_writep(0, 0); // Finalize write process if it is in progress #endif for (n = 100; n; n--) rcv_spi(); // Dummy clocks ty = 0; if (send_cmd(CMD0, 0) == 1) { // Enter Idle state if (send_cmd(CMD8, 0x1AA) == 1) { // SDv2 for (n = 0; n < 4; n++) ocr[n] = rcv_spi(); // Get trailing return value of R7 resp if (ocr[2] == 0x01 && ocr[3] == 0xAA) { // The card can work at vdd range of 2.7-3.6V for (tmr = 12000; tmr && send_cmd(ACMD41, 1UL << 30); tmr--) ; // Wait for leaving idle state (ACMD41 with HCS bit) if (tmr && send_cmd(CMD58, 0) == 0) { // Check CCS bit in the OCR for (n = 0; n < 4; n++) ocr[n] = rcv_spi(); ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; // SDv2 (HC or SC) } } } else { // SDv1 or MMCv3 if (send_cmd(ACMD41, 0) <= 1) { ty = CT_SD1; cmd = ACMD41; // SDv1 } else { ty = CT_MMC; cmd = CMD1; // MMCv3 } for (tmr = 25000; tmr && send_cmd(cmd, 0); tmr--) ; // Wait for leaving idle state if (!tmr || send_cmd(CMD16, 512) != 0) // Set R/W block length to 512 ty = 0; } } CardType = ty; release_spi(); return ty ? 0 : STA_NOINIT; }
DSTATUS mmc_initialize (void) { BYTE n, cmd, ty, ocr[4]; WORD tmr; GREENLEDON(); INIT_SPI(); #if _WRITE_FUNC if (MMC_SEL) disk_writep(0, 0); /* Finalize write process if it is in progress */ #endif for (n = 11; n; --n) XFER_SPI(0xff); /* Dummy clocks */ ty = 0; if (send_cmd(CMD0, 0) == 1) { /* Enter Idle state */ if (send_cmd(CMD8, 0x1AA) == 1) { /* SDv2 */ for (n = 0; n < 4; n++) { ocr[n] = XFER_SPI(0xff); /* Get trailing return value of R7 resp */ } if (ocr[2] == 0x01 && ocr[3] == 0xAA) { /* The card can work at vdd range of 2.7-3.6V */ for (tmr = 12000; tmr && send_cmd(ACMD41, 1UL << 30); tmr--) ; /* Wait for leaving idle state (ACMD41 with HCS bit) */ if (tmr && send_cmd(CMD58, 0) == 0) { /* Check CCS bit in the OCR */ for (n = 0; n < 4; n++) ocr[n] = XFER_SPI(0xff); ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* SDv2 (HC or SC) */ } } } else { /* SDv1 or MMCv3 */ if (send_cmd(ACMD41, 0) <= 1) { ty = CT_SD1; cmd = ACMD41; /* SDv1 */ } else { ty = CT_MMC; cmd = CMD1; /* MMCv3 */ } for (tmr = 25000; tmr && send_cmd(cmd, 0); tmr--) ; /* Wait for leaving idle state */ if (!tmr || send_cmd(CMD16, 512) != 0) /* Set R/W block length to 512 */ { ty = 0; } } } CardType = ty; release_spi(); GREENLEDOFF(); return ty ? 0 : STA_NOINIT; }
//-------------------------------------------------------------------------- // Initialize Disk Drive //-------------------------------------------------------------------------- DSTATUS CMMC::Initialize() { BYTE n, cmd, ty, ocr[4]; WORD tmr; INIT_SPI(); if ( ( PINB & _BV( SD_INS ) ) != 0x00 ) return STA_NOINIT; #if _WRITE_FUNC // Finalize write process if it is in progress if ( MMC_SEL ) Write( ( const BYTE * ) 0, 0 ); #endif // Dummy clocks for ( n = 100; n; n-- ) SPIRead(); ty = 0; // Enter Idle state if ( SendCommand( CMD0, 0 ) == 1 ) { // SDv2 if ( SendCommand( CMD8, 0x1AA ) == 1 ) { // Get trailing return value of R7 resp for ( n = 0; n < 4; n++ ) ocr[n] = SPIRead(); // The card can work at vdd range of 2.7-3.6V if ( ocr[2] == 0x01 && ocr[3] == 0xAA ) { // Wait for leaving idle state (ACMD41 with HCS bit) for ( tmr = 12000; tmr && SendCommand( ACMD41, 1UL << 30 ); tmr-- ) ; // Check CCS bit in the OCR if ( tmr && SendCommand( CMD58, 0 ) == 0 ) { for ( n = 0; n < 4; n++ ) ocr[n] = SPIRead(); // SDv2 (HC or SC) ty = ( ocr[0] & 0x40 ) ? CT_SD2 | CT_BLOCK : CT_SD2; } } // SDv1 or MMCv3 } else { if ( SendCommand( ACMD41, 0 ) <= 1 ) { ty = CT_SD1; cmd = ACMD41; // SDv1 } else { ty = CT_MMC; cmd = CMD1; // MMCv3 } // Wait for leaving idle state for ( tmr = 25000; tmr && SendCommand( cmd, 0 ); tmr-- ) ; // Set R/W block length to 512 if ( !tmr || SendCommand( CMD16, 512 ) != 0 ) ty = 0; } } CardType = ty; SPIRelease(); return ( ty != CT_NONE ) ? 0 : STA_NOINIT; }