cancel
Showing results for 
Search instead for 
Did you mean: 

Problem reading SD card

freya17365
Associate II
Posted on March 11, 2016 at 20:08

Hi to all,

I have a big issue with SD cards.

I initialize correctly my sd card, but when I try to read a block, I can read it only the first time.

This is my routine:

=====================

uint32_t MYSD::ReadSector(uint32_t sector, uint8_t *block)

{

    uint8_t t;

    uint16_t i;

    

    

    GPIOA->BRR = 0x01<<SDSS0;    // Set SDSS0 low

    t = CommandAndResponse(SPICMD17, sector);    //Read sector command

    if(t != 0)

    {

        GPIOA->BSRR = 0x01<<SDSS0;    // Set SDSS0 high

        myusart1.PrintString((uint8_t *) ''Cannot read (CMD17)'', ' ', 0);

        myusart1.PrintHex8(t);

        myusart1.PrintChar('\n');

        return 1;

    }

    

    do

    {

        t = sp->Transfer8(0xFF, 0, 0);    // Wait for card response

    }

    while(t == 0xFF);

    if(t != 0xFE)

    {

        GPIOA->BSRR = 0x01<<SDSS0;    // Set SDSS0 high

        myusart1.PrintString((uint8_t *) ''Cannot read (Token response)'', ' ', 0);

        myusart1.PrintHex8(t);

        myusart1.PrintChar('\n');

        return 2;

    }

    

    for(i=0; i<512; i++) block[i] = sp->Transfer8(0xFF, 0, 0);    //Read data

    

    t = sp->Transfer8(0xFF, 0, 1);    // Read CRC

    t = sp->Transfer8(0xFF, 0, 1);    // Read CRC

    GPIOA->BSRR = 0x01<<SDSS0;    // Set SDSS0 high

    

    return 0;

}

=====================

and this is the tesponse:

Iteration 1: CMD17 response 0

Iteration 2: CMD17 response 4

Iteration 3: CMD17 response 0

Iteration 4: CMD17 response 4

Iteration 5: CMD17 response 0

Iteration 6: CMD17 response 4

Iteration 7: CMD17 response 0

Why I can read block in odd iterations and not  in even? The routine is always the same.

Anyone encountered the same problem?

Thank you

Freya

2 REPLIES 2
viteks
Associate II
Posted on March 13, 2016 at 18:52

STM32F107? STM32F437?

Card capasity?

freya17365
Associate II
Posted on March 14, 2016 at 07:47

STM32F030 at 8MHz

SPI at 4 MHz (fclk/2)

Micro SD card capacity 8GB