Hello,
I'm doing my first tries with STM32F4 and SD card using the ChibiOS driver + Chan's fatfs.
Looking at the logic analyzer, I can see the initialization phase with 128 clock pulses at low clock speed with NSS and MOSI high.
After that, NSS goes low and the driver remains in an infinite loop, trying to get a response (0xFF) from the card. I can see a single byte transfer every 1ms, sending the value of dummytx (which is 0xFF). MOSI is always high.
The wait() function in mmc_spi.c does not end, because MISO is always low (which means that 0x00 is received, but 0xFF is expected).
My questions:
1.
There are configureable timeouts and retry counters in the MMC driver - maybe this would also make sense for the wait() function?
2.
Why is MISO always low? All other signals look fine (NSS, MOSI, CLK). Does MISO need a pullup? I never saw this in applications. When the SD card is selected by NSS going low, what should be the default state of MISO?
I tried different SD cards. The pin configurations should be ok (alternate function set to SPI).
Regards, RuWe
Edit:
I just removed the call of wait() from send_hdr(), and the card is successfully mounted now. Have not tried file system operations yet.
SD card does not reply
Moderators: RoccoMarco, lbednarz, tfAteba
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: SD card does not reply
Hi, I am unable to reproduce the problem, I've just tested on a STM32-P103 and STM32'P107. I suggest to check the SPI config structures (look at those in the demos) and SPI pins settings. Note that NSS is not an alternate but a normal output.
Giovanni
Giovanni
-
rubenswerk
- Posts: 104
- Joined: Wed Feb 22, 2012 11:39 am
Re: SD card does not reply
Yes I know, NSS is switched manually.
But it's the same with all cards I tried - the wait() function directly after the 128 init clock pulses fails. Measured SPI clock is correct for both low speed and high speed SPI. I had to add a timeout. After this modification, the card is mounted and I get a valid result for f_getfree().
Sometimes (??) I can also create a file using f_open(&File, "test", FA_WRITE | FA_CREATE_ALWAYS) and f_close().
So I don't think that there's a HW problem with my board.
I cannot find the wait-after-insersion in Chan's documentation. It only says:
1. Power On (Insersion): >=74 clock pulses
2. Send CMD0
For all further communication with response R1, R2 or R3, the wait is needed.
Maybe I have a SW/HW bug, but maybe this code is not conform with Chan's recommendation.
http://elm-chan.org/docs/mmc/sdinit.png
I still have a problem that the remove handler is never called after using the SD card. The driver state does not go back to BLK_READY. What's the architecture of a fatfs operation? Does a call to a read/write function immediately return, or does it wait until the operation is finished? I couldn't find a periodic callback function (beside the card detection). Is the MMC driven by the DMA finished interrupt?
Concerning MMC_POLLING_INTERVAL and MMC_POLLING_DELAY defined in halconf.h - are they still needed? There are similar defines in main.c
Regards, RuWe
But it's the same with all cards I tried - the wait() function directly after the 128 init clock pulses fails. Measured SPI clock is correct for both low speed and high speed SPI. I had to add a timeout. After this modification, the card is mounted and I get a valid result for f_getfree().
Sometimes (??) I can also create a file using f_open(&File, "test", FA_WRITE | FA_CREATE_ALWAYS) and f_close().
So I don't think that there's a HW problem with my board.
I cannot find the wait-after-insersion in Chan's documentation. It only says:
1. Power On (Insersion): >=74 clock pulses
2. Send CMD0
For all further communication with response R1, R2 or R3, the wait is needed.
Maybe I have a SW/HW bug, but maybe this code is not conform with Chan's recommendation.
http://elm-chan.org/docs/mmc/sdinit.png
I still have a problem that the remove handler is never called after using the SD card. The driver state does not go back to BLK_READY. What's the architecture of a fatfs operation? Does a call to a read/write function immediately return, or does it wait until the operation is finished? I couldn't find a periodic callback function (beside the card detection). Is the MMC driven by the DMA finished interrupt?
Concerning MMC_POLLING_INTERVAL and MMC_POLLING_DELAY defined in halconf.h - are they still needed? There are similar defines in main.c
Regards, RuWe
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: SD card does not reply
The MMC driver does not have any background activity, block functions should fail after a while. You should try to single step into mmcConnect() and see where it fails.
Note that removing the call to wait() is a bad idea because the driver is not supposed to start a new operation while there is something still going on in the card. That would create problems in the operation after a write.
I'll make some more tests, anyway that MISO always low is a symptom of the problem, that should not happen. It could be one of the following:
1) HW
2) Pin programming
3) Problem in card initialization, but this does not happen with all cards I tested.
Giovanni
Note that removing the call to wait() is a bad idea because the driver is not supposed to start a new operation while there is something still going on in the card. That would create problems in the operation after a write.
I'll make some more tests, anyway that MISO always low is a symptom of the problem, that should not happen. It could be one of the following:
1) HW
2) Pin programming
3) Problem in card initialization, but this does not happen with all cards I tested.
Giovanni
-
rubenswerk
- Posts: 104
- Joined: Wed Feb 22, 2012 11:39 am
Re: SD card does not reply
The wait() is skipped only once directly after the 128 init clocks. With this modification, mmcConnect always works with all of my cards. Without the modification, it never works.
Inside mmcSequentialWrite(), when it comes to spiReceive(), the reply is checked with the condition
if ((b[0] & 0x1F) == 0x05) {
The logic analyzer shows me the response byte 0xE5 on MISO line, so the above condition would be fulfilled. But the value which is actually received in b[0] is 0xFF instead of 0xE5. Before calling the spiReceive, I write a random value into b[0], which is then overwritten with 0xFF during the reception. I don't know where this data comes from.
Is there a simple possibility to switch the MMC into SPI polled mode instead of DMA? MMC_USE_SPI_POLLING is nowhere used.
Inside mmcSequentialWrite(), when it comes to spiReceive(), the reply is checked with the condition
if ((b[0] & 0x1F) == 0x05) {
The logic analyzer shows me the response byte 0xE5 on MISO line, so the above condition would be fulfilled. But the value which is actually received in b[0] is 0xFF instead of 0xE5. Before calling the spiReceive, I write a random value into b[0], which is then overwritten with 0xFF during the reception. I don't know where this data comes from.
Is there a simple possibility to switch the MMC into SPI polled mode instead of DMA? MMC_USE_SPI_POLLING is nowhere used.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: SD card does not reply
Are the SPI settings correct? I mean CPOL=0 and CPHA=0.
It looks more like an HW problem, you see something on the bus that is different from what you see in the program. This is a strong hint that the problem is not with the MMC driver.
BTW, are you using the CCM memory? on the F4 you cannot do DMA/SPI transfers in buffers allocated in the CCM.
Giovanni
It looks more like an HW problem, you see something on the bus that is different from what you see in the program. This is a strong hint that the problem is not with the MMC driver.
BTW, are you using the CCM memory? on the F4 you cannot do DMA/SPI transfers in buffers allocated in the CCM.
Giovanni
Re: SD card does not reply
Sorry to interrupt here, but I remember sir, that in my project SPI1 on the F4 discovery board is shared between LIS302DL onboard and the SD Card in SPI mode, the sensor required CPHA=1 but the card required CPHA=0. I experienced errors in card init with CPHA=1 but sensor worked OK. With CPHA=0 it was the other way round. Ultimately I let CPHA=0, while reading from the Sensor, I set CPHA=1, decrease the SPI speed and clear it after completing the read. Both peacefully coexist now.
-
rubenswerk
- Posts: 104
- Joined: Wed Feb 22, 2012 11:39 am
Re: SD card does not reply
Thanks for your hints.
I'm using a custom board where SPI3 is exclusively connected to SD card.
My MMC/SPI configuration is based on the STM32P103 fatfs demo, so CPOL and CPHA are correct.
The strange thing is that mmcConnect works. Inside mmcConnect, data is sent and received at both low and high SPI clock speed, and everything is fine.
The f_open(FA_WRITE) command also succeeds (I guess this doesn't result in any physical SPI activity).
The problem occurs in f_close, spiReceive fails to receive valid data.
Some time ago, before I changed to ChibiOS, I tried this demo (which uses polled SPI) and it worked:
http://www.siwawi.arubi.uni-kl.de/avr_p ... nfat_stm32
So my analysis focuses rather on SW side than on HW side.
I'd like to use the perfectly integrated, DMA-powered ChibiOS MMC driver. It looks to me very optimized in matters of CPU usage. Let's see, maybe I'll do another try and start from scratch with the F103 demo, doing only the minimal adaptations for the F407.
I'm using a custom board where SPI3 is exclusively connected to SD card.
My MMC/SPI configuration is based on the STM32P103 fatfs demo, so CPOL and CPHA are correct.
The strange thing is that mmcConnect works. Inside mmcConnect, data is sent and received at both low and high SPI clock speed, and everything is fine.
The f_open(FA_WRITE) command also succeeds (I guess this doesn't result in any physical SPI activity).
The problem occurs in f_close, spiReceive fails to receive valid data.
Some time ago, before I changed to ChibiOS, I tried this demo (which uses polled SPI) and it worked:
http://www.siwawi.arubi.uni-kl.de/avr_p ... nfat_stm32
So my analysis focuses rather on SW side than on HW side.
I'd like to use the perfectly integrated, DMA-powered ChibiOS MMC driver. It looks to me very optimized in matters of CPU usage. Let's see, maybe I'll do another try and start from scratch with the F103 demo, doing only the minimal adaptations for the F407.
-
rubenswerk
- Posts: 104
- Joined: Wed Feb 22, 2012 11:39 am
Re: SD card does not reply
I'm such a dumb guy, when I adapted the FATFS demo to my board, I took the board.h of the Olimex E407 demo as basis, which connects to SDIO, but I use SPI. I completely mixed it up, then corrected the SPI pins, but forgot to activate the pullup on the MISO, which is essential during initialization.
Now, all of my cards are detected.
The other problem (DMA does not receive the correct reply from the card which I can see with the logic analyzer) remains. It depends on SPI speed. Using <=656kHz clock speed (APB clock /64) is ok. Using >=1.31MHz leads to receiving 0xFF instead of correct data. MISO signal on analog scope looks good. This applies for all tested cards.
Of course I'm talking about the final transmission speed. During initialization, it is 164kHz.
For the first step this is ok, SD is only needed to store some small configuration data.
Thanks for your patience.
Now, all of my cards are detected.
The other problem (DMA does not receive the correct reply from the card which I can see with the logic analyzer) remains. It depends on SPI speed. Using <=656kHz clock speed (APB clock /64) is ok. Using >=1.31MHz leads to receiving 0xFF instead of correct data. MISO signal on analog scope looks good. This applies for all tested cards.
Of course I'm talking about the final transmission speed. During initialization, it is 164kHz.
For the first step this is ok, SD is only needed to store some small configuration data.
Thanks for your patience.