fpga4fun.comwhere FPGAs are fun

SD card 2 - Protocol

SD cards work with a command/response scheme. For example, command "17" allows reading one sector (512 bytes) of the card memory. All communication is synchronous to a clock provided by the host (FPGA in our case). The clock should run below 400KHz at startup and can go faster after some card initialization.

All commands and most responses are 48bits long (6 bytes). Sector data come in multiples of 512 bytes. For example, here's a simple code that allows sending commands to the SD card.

// we use the Xylo-E FX2 FIFO2 as data source for "commanding" an SD card
// the SD card is used in one-bit SD mode

// first we are going to drive the SD card at a much slower speed than the FPGA itself
// let's create a "shift" signal that is asserted once every 64 clock periods
reg [5:0] cnt=0;  always @(posedge clk) cnt <= cnt+1;
reg shift=0;  always @(posedge clk) shift <= &cnt;

// now we serialize every byte we get from the FIFO2
reg [2:0] cntbit=0;
reg shifting=0;
reg [7:0] data=0;
always @(posedge clk) if(shift) shifting <= shifting ? ~(&cntbit & ~FIFO2_data_available) : FIFO2_data_available;
always @(posedge clk) if(shift & shifting) cntbit <= cntbit+1;
always @(posedge clk) if(shift) data <= (FIFO2_data_available & (~shifting | &cntbit)) ? FIFO_DATAIN : {data[6:0],1'b0};
assign FIFO_RD = shift & (~shifting | &cntbit);

// and send the serial data to the SD card
assign SD_CLK = cnt[5];
assign SD_CMD = shifting ? data[7] : 1'bZ;

Some commands have no reply, while some other will issue a response on the SD_DAT line. For example, to initialize the card, we start with a CMD0 followed by CMD8:

Here's a session recorded using Xylo-E's demo software:

USB driver opened CMD0 400000000095 CMD8 48000001AA87... OK CMD55 770000000065... OK CMD41 694018000019... OK CMD55 770000000065... OK CMD41 694018000019... OK SDHC/SDXC (high capacity) card CMD2 42000000004D... OK ASTC 3.4 CMD3 430000000021... OK RCA=0100 CMD7 4700010000DD... OK CMD13 4D0001000053... OK CMD17 510000000055... OK FAT32 detected Reading 1 sector(s) starting at 8192 CMD17 5100002000B1... OK Directory /

After some initialization, the card accepts CMD17 "READ_SINGLE_BLOCK" so that files can be read from the card.

That's all folks... check the links below and experiment.