# Is it possible to use 4-pin mode for an SD card with a microcontroller instead of SPI mode?

Is it possible to use the 4-pin mode of an SD card when designing an interface with a microcontroller, or must you always use SPI mode? I am particularly thinking of a microcontroller such as a PIC24. The SPI mode is slower, obviously, and I am trying to think of better ways to improve the device's throughput.

That's what I've found:

The SD 1-bit protocol is a synchronous serial protocol with one data line, used for bulk data transfers, one clock line for synchronization, and one command line, used for sending command frames. The SD 1-bit protocol explicitly supports bus sharing. A simple single-master arbitration scheme allows multiple SD cards to share a single clock and DAT0 line. The SD 4-bit protocol is nearly identical to the SD 1-bit protocol. The main dierence is the bus width bulk data transfers occur over a 4-bit parallel bus instead of a single wire. With proper design, this has the potential to quadruple the throughput for bulk data transfers. Both the SD 1-bit and 4-bit protocols by default require CRC protection of bulk data transfers. A CRC, or Cyclic Redundancy Check, is a simple method for detecting the presence of simple bit-inversion errors in a transmitted block of data. In SD 4-bit mode, the input data is multiplexed over the four bus (DAT) lines and the 16-bit CRC is calculated independently for each of the four lines. In an all-software implementation, calculating the CRC under these conditions can be so complex that the computational overhead may mitigate the benets of the wider 4-bit bus. A 4-bit parallel CRC is trivial to implement in hardware, however, so custom ASIC or programmable-logic solutions are more likely to benet from the wider bus.

I believe you wouldn't have enough power to calculate crc32 in 4-bit mode. But without CRC it might be possible.

• Some of the PIC24F series have 16-bit CRC generators. I imagine it would be possible to combine these in some manner to get 32-bit CRCs. – Thomas O Feb 25 '11 at 12:45
• In fact CRC-16 is needed here, so it looks much better. – BarsMonster Feb 25 '11 at 13:37
• A bit-banged four-bit SD interface will not be faster than a hardware-assisted SPI interface. (According to Wikipedia, an SD card can be clocked at 208MHz. That speed will be difficult for a PIC microprocessor.) – markrages Feb 25 '11 at 16:10
• @markrages: I think one could with perform reads in 4-bit mode, without CRC, about 3x as fast as SPI. Put the clock wire on a PWM pin, and set things so that if one repeatedly performs the sequence swapf PORTC,w,c / iorwf PORTA,w,c / movwf POSTINC0,c the SD card will get clocked during each movwf. Getting the timing right for the start and end of each burst might be tricky (of course, interrupts would absolutely positively have to be disabled) but probably not impossible. Targeting a rate of one byte per four cycles might simplify things. – supercat Jan 12 '13 at 21:35
• Adding CRC computation would probably cut the speed by about half (adding xorwf crc,w,c / movf PLUSW1,w,c / movwf crc,f,c should work at a cost of 3 more cycles/byte if one has a suitable 256-byte table in RAM; if one worked with pairs of bytes one could possibly do better. – supercat Jan 12 '13 at 21:41

improving throughput in 1-bit mode

There is a huge variation in performance from SD card to another.

(a) All cards give a "busy" response to one command until it is ready to handle the next. A card can still comply with the standards even if it takes a second to write a block of data. Perhaps you could try a few different cards and see which one works the best for you -- the best net throughput, the lowest latency, the least amount of energy to wake up and store the data and go back to sleep, or whatever is most important for your project. If throughput is the most important, then you'll want to pick the appropriate SD Card Speed Class Rating -- the highest speed you can afford, or at least the lowest class that gives adequate performance.

(b) While the standard requires all SD cards to handle communications at up to 25 Mbit/s in 1-bit mode, a few cards seem to work with much higher bit rates -- perhaps you could try a few different cards and bump up your bit rate to something faster that works with that particular card. (Is there something like "automatic baud rate detection" to automatically use the maximum speed supported by whatever card is currently plugged in?)

using 4-pin mode

A microcontroller can support using four-bit SDI transfer mode. The 2 options I know about are:

(a) Plug the card into a USB SD card adapter that uses four-bit SDI transfer mode. Plug the adapter into a USB host connector attached to a microcontroller that can act as a USB host, such as the PIC24FJ32GB002.

(b) Plug the card into a SD card slot directly attached to a microcontroller. Do I need to pay royalties to the SD card people if my device “just happens” to be compatible with SD cards? For a long time various patents and trade secrets required a person designing such a host controller interface to sign an NDA and buy a patent licence from the SD Card Association. But I hear that the SD interface standard is becoming more open since 2006.