cancel
Showing results for 
Search instead for 
Did you mean: 

SD card using SPI initialization stm32f303 issue

alkuor
Associate II
Posted on November 30, 2016 at 16:21

Hello, I am sending the correct command and I can see it on the scope.However, I don't get 0x01 response(I keep getting 0xff) from SDcard(SDHC)(SanDisk Ultra 40MB/s ...16GB) I am using the following code:

uint8_t c=0;

uint8_t cmd0[9]={0x40,0x00,0x00,0x00,0x00,0x95};

uint8_t dumb=0xff;

FATFS_CS_HIGH;

for (int i = 0; i < 10; i++) {

HAL_SPI_Transmit(&hspi1, &dumb, 1, 10);

while (HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);

}

FATFS_CS_LOW;

HAL_SPI_Transmit(&hspi1, cmd0, 6, 10);

while (HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);

for(int i = 0; i < 2; i++) {

HAL_SPI_Transmit(&hspi1, &dumb, 1, 10);

while (HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);

}

HAL_SPI_Receive(&hspi1, &c, 1, 10); I keep getting 0xff from slave

1

http://stackoverflow.com/questions/40873237/sd-card-using-spi-initialization-stm32f303-issue-with-cmd0#

I am sending the correct command and I can see it on the scope.However, I don't get 0x01 response(I keep getting 0xff) from SDcard(SDHC)(SanDisk Ultra 40MB/s ...16GB) I am using the following code:

#no-hablo-hal

2 REPLIES 2
Posted on November 30, 2016 at 16:29

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/SD%20card%20using%20SPI%20initialization%20stm32f303%20issue%20with%20CMD0&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/AllItems.aspx&currentviews=13]Duplicative thread

I would double check the connections/pins, and confirm chip select. All my SPI SD stuff uses the SPL, not HAL, so can't help you much with that. Would probably use a logic analyzer over a scope, but if the card is not responsive you need to review the connection/communications.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Daniel Koster
Associate II
Posted on March 05, 2018 at 20:28

I got stuck with this problem for days also, very annoying!

So i wanted to give my bit of input for the rest that have the same problem.

I tried to initialize the SD card through SPI interface and only 1 sd card of many worked fine, the rest were impossible to initialize. 

Tried bit-banging with elm-chan code, and all the cards worked fine.

The porblem is in the SPI HAL function for receiving.

The SD cards are expecting an 0xFF value on the MOSI Pin, so the SPI has to send 0xFF when receiving the byte answer from the card (or MOSI has to be kept HIGH for the 8 clocks) and that is not necessarily the case with the HAL_SPI_Receive function.

To solve it, one should use the HAL_SPI_TransmitReceive function like this:

uint8_t MySPI1_ReadByte(void){

   uint8_t ReceivedByte = 0, Dummy = 0xFF;

   while ((HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY));

   HAL_SPI_TransmitReceive(&hspi1, &Dummy, &ReceivedByte, 1, 5000);

   return ReceivedByte;

}

These pages helped me to find it out:

https://ralimtek.com/Stm32_SPI_SD/

 

https://blog.domski.pl/using-fatfs-with-hal/

 

I hope it helps.