Electronic – Does an SD card in SPI mode respect chip select/slave select? Seems to be resetting in the application

fpganxpsdspi

I have an application where I have a microcontroller (NXP LPC1343) which is connected to an FPGA via 16-bit SPI. There is also an SD card using the same SPI port (MISO/MOSI) but with a different CS/SS pin (both are active low, as per the SPI specification). One of the things I need to do is write data from the FPGA onto a file on the SD Card using FAT32, and this is the job of the microcontroller. The microcontroller is running FatFS, which I have gotten to work reliably by itself.

Because the microcontroller only has a small amount of RAM, only a small amount of data can be buffered at once. Hence, the micro has to read a buffer from the FPGA, change the SPI mode to 8-bit, and then write that data to the FATFS. Recall that in order to configure the SD card for SPI mode, a command must be sent while the SPI bus is running at 400 kHz, and a certain amount of waiting has to happen. Therefore, I would like to have to only perform the initialization once.

However, performing transactions on the FPGA even while holding CS high on the SD card seems to put the SD card into a weird state such that it needs to go through initialization again. This of course is undesirable, since the initialization may take several milliseconds, in order to write just 4 kB or so of data (again limited by the small RAM capability of my micro). As I need to write several megabytes as quickly as possible, this reduces performance from about 500 kB/s to less than 100 kB/s.

I'm aware that SD cards aren't technically fully SPI compliant, but how can this problem be fixed?

Best Answer

Okay, I figured it out actually. I should have googled a bit deeper. It turns out that SD cards don't act exactly like SPI devices when sharing a bus according to How to Use MMC/SDC:

In the SPI bus, each slave device is selected with separated CS signals, and plural devices can be attached to an SPI bus. Generic SPI slave device drives/releases its DO signal by CS signal asynchronously to share an SPI bus.

However, MMC/SDC drives/releases DO signal in synchronising to the SCLK. This means there is a possibility of bus conflict with MMC/SDC and any other SPI slaves that attached to an SPI bus. The right image shows the drive/release timing of the MMC/SDC (the DO signal is pulled to 1/2 Vcc to see the bus state). Therefore to make MMC/SDC release DO signal, the master device must send a byte after CS signal is deasserted.

The SD card and FPGA were probably both trying to drive DO and the SD card lost out, so it reset. Sending an extra byte seems to have fixed it.