u8 sd_open( void ) { u8 i; u8 resp[ R1 + 16 + 2 ]; u24 deleteme; // Check for card if( !sd_card_present() ) return( SD_ERR_NOCARD ); // SPI to 400kHz SPI_BRG_H = 0x00; SPI_BRG_L = 0x7D; // Required delays before initialization sd_assert(); sd_delay( 100 ); sd_deassert(); sd_delay( 2 ); // Enter idle state if( sd_send_command( CMD0_GO_IDLE_STATE, NULL, NULL ) != SD_ERR_OK ) return( SD_ERR_GENERIC ); // Read OCR (for operating conditions) and verify 3.3V capability if( sd_send_command( CMD58_READ_OCR, NULL, resp ) != SD_ERR_OK ) return( SD_ERR_GENERIC ); if( !( resp[ 2 ] & MSK_OCR_33 ) ) return( SD_ERR_OCR ); // Initialize card i = 0; do { if( sd_send_command( ACMD41_APP_SEND_OP_COND, NULL, resp ) != SD_ERR_OK ) return( SD_ERR_GENERIC ); } while( ( resp[ 0 ] & SD_ERR_R1_IDLE ) && ++i != SD_IDLE_TIMEOUT ); // Set block length to 512 (compatible with SDHC) if( sd_set_blocklen( 512 ) != SD_ERR_OK ) return( SD_ERR_GENERIC ); // Read CSD //if( sd_send_command( CMD9_SEND_CSD, NULL, resp ) != SD_ERR_OK ) return( SD_ERR_GENERIC ); /* for( i = 0; i < 255; i++ ) { sd_readblock( i, block ); for( deleteme = 0; deleteme < 512; deleteme++ ) { if( block[ deleteme ] != 0 ) { asm( "NOP" ); asm( "NOP" ); asm( "NOP" ); asm( "NOP" ); asm( "NOP" ); asm( "NOP" ); asm( "NOP" ); asm( "NOP" ); break; } } } */ // All hail the initialized SD card! return( SD_ERR_OK ); }
int sd_initialize() { char i, j; sdc->busyflag = 0; for (i=0; i<4; i++) argument[i] = 0; /* Delay for at least 74 clock cycles. This means to actually * *clock* out at least 74 clock cycles with no data present on * the clock. In SPI mode, send at least 10 idle bytes (0xFF). */ spi_cs_assert(); sd_delay(100); spi_cs_deassert(); sd_delay(2); /* Put the card in the idle state */ if (sd_send_command(sdc, CMD0, CMD0_R, response, argument) == 0) return 0; /* Now wait until the card goes idle. Retry at most SD_IDLE_WAIT_MAX times */ j = 0; do { j++; /* Flag the next command as an application-specific command */ if (sd_send_command(sdc, CMD55, CMD55_R, response, argument) == 1) { /* Tell the card to send its OCR */ sd_send_command(sdc, ACMD41, ACMD41_R, response, argument); } else { /* No response, bail early */ j = SD_IDLE_WAIT_MAX; } } while ((response[0] & MSK_IDLE) == MSK_IDLE && j < SD_IDLE_WAIT_MAX); /* As long as we didn't hit the timeout, assume we're OK. */ if (j >= SD_IDLE_WAIT_MAX) return 0; if (sd_send_command(sdc, CMD58, CMD58_R, response, argument) == 0) return 0; /* At a very minimum, we must allow 3.3V. */ if ((response[2] & MSK_OCR_33) != MSK_OCR_33) return 0; /* Set the block length */ if (sd_set_blocklen (sdc, SD_BLOCKSIZE) != 1) return 0; /* If we got this far, initialization was OK. */ return 1; }
//initialization was successful, 0 otherwise. //sd_context_t *sdc -- pointer to a data structure containing //information about the card. For now, the //timeouts MUST be specified in advance. This //function does not yet calculate them from the //card data. int sd_initialize(sd_context_t *sdc){ char i, j; //SPI SD initialization sequence: CMD0->CMD55->ACMD41->CMD58. j = 0; //Note there is no CMD2 or CMD3 in SPI mode. These instructions are devoted to addressing on the SD bus. sdc->busyflag = 0; //SD memory card SD initialization sequence: CMD0->CMD55->ACMD41->CMD2->CMD3. for (i=0; i<4; i++){ argument[i] = 0; } //Delay for at least 74 clock cycles. This means to actually spi_cs_assert(); //*clock* out at least 74 clock cycles with no data present on sd_delay(100); //the clock. In SPI mode, send at least 10 idle bytes (0xFF). spi_cs_deassert(); sd_delay(2); if (sd_send_command(sdc, CMD0, CMD0_R, response, argument) == 0){ //Put the card in the idle state return 0; } //-------------------------------------Now wait until the card goes idle. Retry at most SD_IDLE_WAIT_MAX times.-------------------------------------- do{ j++; //Flag the next command as an application-specific command. if (sd_send_command(sdc, CMD55, CMD55_R, response, argument) == 1){ sd_send_command(sdc, ACMD41, ACMD41_R, response, argument); //Tell the card to send its OCR } else{ // No response, bail early. j = SD_IDLE_WAIT_MAX; } } while ((response[0] & MSK_IDLE) == MSK_IDLE && j < SD_IDLE_WAIT_MAX); //As long as we didn’t hit the timeout, assume we’re OK. if (j >= SD_IDLE_WAIT_MAX){ return 0; } if (sd_send_command(sdc, CMD58, CMD58_R, response, argument) == 0){ return 0; } if ((response[2] & MSK_OCR_33) != MSK_OCR_33){ //At a very minimum, we must allow 3.3V. return 0; } if (sd_set_blocklen (sdc, SD_BLOCKSIZE) != 1){ //Set the block length return 0; } return 1; //If we got this far, initialization was OK. }
/* * This function initializes the SD card. It returns 1 if * initialization was successful, 0 otherwise. * * sd_context_t *sdc -- Pointer to a data structure containing * information about the card. For now, the * timeouts must be specified in advance. It * is not yet calculated from the card data. */ int sd_initialize(sd_context_t *sdc) { u16 i; /* * SPI SDv2 initialization sequence: * CMD0 * CMD8 * CMD55+ACMD41 * CMD58 * (There is no CMD2 or CMD3 in SPI mode. These * instructions are devoted to addressing on the SD bus) */ sdc->busyflag = 0; /* * Delay for at least 74 clock cycles. This means to actually * *clock* out at least 74 clock cycles with no data present on * the clock. In SPI mode, send at least 10 idle bytes (0xFF) */ spi_cs_deassert(); sd_delay(100); spi_cs_assert(); sd_delay(2); /* Put the card in thee idle state */ if (sd_send_command(sdc, CMD0, CMD0_R, response, NULL) == 0) return 0; sd_packarg(argument, 0x000001AA); if (sd_send_command(sdc, CMD8, CMD8_R, response, argument) == 0) { return 0; } if (response[0] & MSK_ILL_CMD) { /* Illegal command, try SDv1 init */ sd_packarg(argument, 0x00000000); } else { /* Check if the voltage range is appropiate */ if (response[3] == 0x01 && response[4] == 0xAA) { sd_packarg(argument, 0x40000000); } else { return 0; } } /* Now wait until the card goes idle. Retry at most SD_IDLE_WAIT_MAX */ i = 0; do { i++; /* Flag the next command as an application-specific command */ if (sd_send_command(sdc, CMD55, CMD55_R, response, NULL) == 1) { /* Tell the card to send its OCR */ sd_send_command(sdc, ACMD41, ACMD41_R, response, argument); } else { /* No response, bail early */ i = SD_IDLE_WAIT_MAX; } } while ((response[0] & MSK_IDLE) == MSK_IDLE && i < SD_IDLE_WAIT_MAX); /* As long as we didn't hit the timeout, assume we're OK */ if (i >= SD_IDLE_WAIT_MAX) return 0; if (sd_send_command(sdc, CMD58, CMD58_R, response, NULL) == 0) return 0; /* At the very minimum, we must allow 3.3V */ if ((response[2] & MSK_OCR_33) != MSK_OCR_33) return 0; /* Set the block length */ if (sd_set_blocklen(sdc, SD_BLOCKSIZE) != 1) return 0; /* If we got this far, initialization was OK */ return 1; }