#ifdef CSR_SPISDCARD_BASE
-#define SPISDCARD_DEBUG
+//#define SPISDCARD_DEBUG
#ifndef SPISDCARD_CLK_FREQ_INIT
-#define SPISDCARD_CLK_FREQ_INIT 400000
+#define SPISDCARD_CLK_FREQ_INIT 200000
#endif
#ifndef SPISDCARD_CLK_FREQ
-#define SPISDCARD_CLK_FREQ 10000000
+#define SPISDCARD_CLK_FREQ 25000000
#endif
/*-----------------------------------------------------------------------*/
/* SDCard Select/Deselect functions */
/*-----------------------------------------------------------------------*/
-static void spisdcarddeselect(void) {
+static void spisdcard_deselect(void) {
/* Set SPI CS High */
spisdcard_cs_write(SPI_CS_HIGH);
/* Generate 8 dummy clocks */
spi_xfer(0xff);
}
-static int spisdcardselect(void) {
+static int spisdcard_select(void) {
uint16_t timeout;
/* Set SPI CS Low */
}
/* Deselect card on error */
- spisdcarddeselect();
+ spisdcard_deselect();
return 0;
}
/* Select the card and wait for it, except for CMD12: STOP_TRANSMISSION */
if (cmd != CMD12) {
- spisdcarddeselect();
- if (spisdcardselect() == 0)
+ spisdcard_deselect();
+ if (spisdcard_select() == 0)
return 0xff;
}
/* Set SPI clk freq to initialization frequency */
spi_set_clk_freq(SPISDCARD_CLK_FREQ_INIT);
- /* Wait 10ms */
- busy_wait(10);
-
- /* Set SDCard in SPI Mode (generate 80 dummy clocks) */
- for (i=0; i<10; i++)
- spi_xfer(0xff);
+ timeout = 1000;
+ while (timeout) {
+ /* Set SDCard in SPI Mode (generate 80 dummy clocks) */
+ spisdcard_cs_write(SPI_CS_HIGH);
+ for (i=0; i<10; i++)
+ spi_xfer(0xff);
+ spisdcard_cs_write(SPI_CS_LOW);
+
+ /* Set SDCard in Idle state */
+ if (spisdcardsend_cmd(CMD0, 0) == 0x1)
+ break;
- /* Set SDCard in Idle state */
- if (spisdcardsend_cmd(CMD0, 0) != 0x1)
+ timeout--;
+ }
+ if (timeout == 0)
return 0;
/* Set SDCard voltages, only supported by ver2.00+ SDCards */
if (drv) return RES_NOTRDY;
r = spisdcard_init();
- spisdcarddeselect();
+ spisdcard_deselect();
spisdcardstatus = r ? 0 : STA_NOINIT;
return spisdcardstatus;
if (cmd == CMD18)
spisdcardsend_cmd(CMD12, 0); /* STOP_TRANSMISSION */
}
- spisdcarddeselect();
+ spisdcard_deselect();
if (count)
return RES_ERROR;