Ok, I've got a v1.0 release of working code now at 50 MHz!
Many thanks to Hamster for the help!
You can find the code (with readme) here: http://www.rulecity.com/browsetmp/sd_card_writer-17Feb2013.7z
Please feel free to suggest improvements to the stupid bone-headed newbie VHDL mistakes I make!
VHDL SD card writer v1.0
16 Feb 2013
This is VHDL code (for the Papillio One FPGA board) that shows how to write to an SD card in a high-speed mode.
- Uses 4-bit SD mode, not 1-bit SPI mode (faster)
- Uses 50 MHz "high speed" mode, not 25 MHz "normal speed" mode (also faster)
- Logs to Papillio USB serial port (230400 bps) so you can see what is going on
- An SDHC card (older probably won't work) that supports high-speed aka 50 MHz mode.
How to use:
- Get the pins of an SD card (or adapter) somehow connected to Papillio pins. Not discussed here as you are expected to have enough expertise to figure this out on your own
What you will see on serial port (230400 bps) if everything is working correctly (this is very terse since string handling is a pain in VHDL):
Explanation of above data:
"-" gets sent on reset, it means the process is starting over
"C00" is the reset command sent to the SD card. It expects no response.
"C08" is the voltage-request command and is only supported on newer cards. The Papillio requests normal 3.3V operation. If I/O is not working, or if the SD card does not support this command, this is the last line you will see.
"C55/A41" is the init command. It usually needs to be repeated over and over again until initialization is finished which is normal.
"C02" is the "get SD card CID" command. It is required otherwise I wouldn't bother sending it.
"C03" is the "get SD card's relative address" command. It is also required.
"C07" puts the SD card into transfer mode.
"C55/A06" switches the SD card from a 1-bit bus into a 4-bit bus (4x speed increase).
"C06" switches the SD card from 25 MHz mode to 50 MHz mode (2x speed increase). If you see an "E" after this, it means that the CRC check failed (this happened to me occasionally during development).
"C24" starts writing a single 512 byte block at address 0 (beginning of card).
"C13" requests the card's status and is optional but it makes me feel better to see it working since at this point we are communicating at 50 MHz.
"S" means 'success' and means that the SD card responded that the write operation was completely successful. If you see an "E" here it means that the CRC check probably failed.
If you see anything with "X" in front of it, it means that an unexpected response was received and the unexpected response has been dumped out in ASCII hex format. This is for troubleshooting purposes.