void sd_read(char *a) { uint8_t cmd0_response = sd_cmd0(); if(cmd0_response == 1) sprintf(a, "SD: %02x OK ", cmd0_response); else sprintf(a, "SD: %02x FAIL", cmd0_response); }
rt_uint8_t sd_init(void) { //-- SD controller & card initialize int i; /* Important notice for MMC test condition */ /* Cmd & Data lines must be enabled by pull up resister */ SDIPRE = PCLK/(INICLK)-1; SDICON = (0<<4) | 1; // Type A, clk enable SDIFSTA = SDIFSTA | (1<<16); SDIBSIZE = 0x200; /* 512byte per one block */ SDIDTIMER=0x7fffff; /* timeout count */ /* Wait 74SDCLK for MMC card */ for(i=0; i<0x1000; i++); sd_cmd0(); /* Check SD card OCR */ if(sd_ocr() == RT_EOK) { rt_kprintf("In SD ready\n"); } else { rt_kprintf("Initialize fail\nNo Card assertion\n"); return RT_ERROR; } RECMD2: SDICARG = 0x0; SDICCON = (0x1<<10)|(0x1<<9)|(0x1<<8)|0x42; /* lng_resp, wait_resp, start, CMD2 */ if(sd_cmd_end(2, 1) == RT_ERROR) goto RECMD2; SDICSTA = 0xa00; /* Clear cmd_end(with rsp) */ RECMD3: SDICARG = 0<<16; /* CMD3(MMC:Set RCA, SD:Ask RCA-->SBZ) */ SDICCON = (0x1<<9)|(0x1<<8)|0x43; /* sht_resp, wait_resp, start, CMD3 */ if(sd_cmd_end(3, 1) == RT_ERROR) goto RECMD3; SDICSTA=0xa00; /* Clear cmd_end(with rsp) */ RCA = (SDIRSP0 & 0xffff0000 )>>16; SDIPRE=PCLK/(SDCLK)-1; /* Normal clock=25MHz */ if( SDIRSP0 & 0x1e00 != 0x600 ) goto RECMD3; sd_sel_desel(1); sd_delay(200); sd_setbus(); return RT_EOK; }
int sd_init() { UBYTE timeout; spi_set_speed(0x7f); // min speed // wait a bit spi_assert_cs(); for(timeout=0x10; timeout>0; --timeout) { spi_send_byte(0xff); } spi_deassert_cs(); spi_send_byte(0xff); spi_send_byte(0xff); spi_assert_cs(); // go into idle state timeout = 0xff; while (sd_cmd0() != 0x01) { if (timeout==0) { spi_deassert_cs(); return FALSE; } timeout = timeout-1; } if (sd_cmd8() == 0x01) { // SD card V2+ #ifdef DEBUG_SD debug_puts("SD card V2+\n"); #endif // initialize card timeout = 0xff; while ((sd_acmd41(0x40)&1)!=0) { if (timeout==0) { spi_deassert_cs(); return FALSE; } timeout = timeout-1; } // read OCR if (sd_cmd58()!=0) { spi_deassert_cs(); return FALSE; } } else { // SD card V1 or MMC if (sd_acmd41(0x00)<=1) { // SD V1 #ifdef DEBUG_SD debug_puts("SD V1\n"); #endif timeout = 0xff; while ((sd_acmd41(0x00)&1)!=0) { if (timeout==0) { spi_deassert_cs(); return FALSE; } timeout = timeout-1; } } else { // MM Card : fail #ifdef DEBUG_SD debug_puts("MMC\n"); #endif spi_deassert_cs(); return FALSE; } } spi_deassert_cs(); spi_set_speed(0x00); // max speed return TRUE; }