flash_sdcard: ensure card goes in idle mode when reseting

This commit is contained in:
King Kévin 2017-08-17 15:59:50 +02:00
parent 09af8785e9
commit 110a2e4665
1 changed files with 6 additions and 3 deletions

View File

@ -92,8 +92,8 @@ static void flash_sdcard_spi_wait(void)
*/
static uint16_t flash_sdcard_spi_read(void)
{
spi_send(SPI(FLASH_SDCARD_SPI), 0xffff); // send not command token (i.e. starting with 1)
(void)SPI_DR(SPI(FLASH_SDCARD_SPI)); // clear RXNE flag (by reading previously received data (not done by spi_read or spi_xref)
spi_send(SPI(FLASH_SDCARD_SPI), 0xffff); // send not command token (i.e. starting with 1)
while (!(SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_TXE)); // wait until Tx buffer is empty before clearing the (previous) RXNE flag
while (!(SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_RXNE)); // wait for next data to be available
return SPI_DR(SPI(FLASH_SDCARD_SPI)); // return received adat
@ -415,8 +415,11 @@ bool flash_sdcard_setup(void)
// start card-identification (see section 7.2.1/4.2)
uint8_t r1 = 0;
// send CMD0 (GO_IDLE_START) to start the card identification (see section 7.2.1)
r1 = flash_sdcard_command_response(0, 0, NULL, 0); // (see table 7-3)
uint8_t tries = 32; // how often should we try to go to card identification mode (random large value which worked)
// send CMD0 (GO_IDLE_START) to put card in card identification mode (see section 7.2.1)
while (0x01!=r1 && tries-->0) {
r1 = flash_sdcard_command_response(0, 0, NULL, 0); // (see table 7-3)
}
if (0x01!=r1) { // error occurred, not in idle state
return false;
}