2016-11-30 07:21 AM
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 slave1
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-hal2016-11-30 07:29 AM
[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¤tviews=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.2018-03-05 11:28 AM
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.