Let's try to do something real with the Xilinx Spartan-6 FPGA: write a set of data to an SD card.
To boost the FPGA skills, I'm refreshing theory and checking out some real designs. For a standalone XuLA2 board, talking to SD cards is a good practical example. |
There's a Micro SD slot on the XuLA2 models. The only other component you need is a spare Micro SD card.
Don't use an SD card with your marriage photo shoot on it. You'll very likely loose that when you test this project. |
The Xess XuLA2 github has two SD card projects. We'll use the SD Card Control Test example.
In this project, the FPGA has two main duties:
- Read and Write SD data
- Communicate with your PC over USB
A python script on your PC will generate test data and send it to the FPGA over USB.
The FPGA writes the data to your SD card. We're using low level protocol here, no filesystem.
When finished, it reads the data back off the SD card and verifies the results.
VHDL Libraries
Xess made a set of common VHDL libraries. The example uses several those libs.
The communication with the SD slot is via :
- SDCard.vhd
Clocking is handled by these two:
- ClkGen.vhd
- SyncToClk.vhd
Talk to the PC over USB happens with this one (and the on-board PIC microcontroller):
- HostIo.vhd
These libs are not only useful, but also a great source to learn reusable VHDL.
How it Works
The XESS blog explains the example in detail. It explains both the electrical connections and how the Micro SD protocol is handled.
The header comments of the SdCard.vhd source file document many implementation details.
Open that file by double-clicking the u3 - SdCardCtrl node in the Implementation view.
If you've ever tried to understand (or port) a microcontroller SDCard lib - maybe the one from Arduino - you'll recognise much of the logic.
Let's now synthesize the project and generate the programming file.
Insert the SD card, connect the XuLA2 with your laptop and load the bitstream:
xsload --fpga sdcardctrltest.bit
Once you've loaded the bitstream, your XuLA2 board sits idle. You need to tell it to read and write data.
There's a python scrip that does exactly that.
If you have retrieved the latest XuLA2 FPGA sources from GIT, you'll find a python file named SDcardTest.py in the project directory.
If you're using the examples that were installed by the XESS installer, you can retrieve the testbed from here:
https://raw.githubusercontent.com/xesscorp/XuLA2/master/FPGA/SdcardCtrlTest/SdcardTest.py
Run the script to write a random set of data to the SD card, read it back and check if everything is correct:
SDcardTest.py
The data is written using low level protocol. You will not be able to read the data when you insert the SD card into your PC.
The design doesn't use a filesystem or any other advanced disk management protocol. It's SD access for real blokes.
Windows 8 and Windows 10 with Xilinx ISE |
---|
If you are running WIndows 10 64-bit, you may encounter several ISE problems:
Switching to the 32-bit version has solved most of them for me: To switch,
After that, I still have one issue left: when in Simulator, the Relaunch functionality doesn't work. I have to close iSIM and restart it from within ISE. |
I've also tested the design with a known defect card - one that's rejected by any known operating system and can't be formatted.
This test was successful. The test flagged that the FPGA wasn't able to write data, as expected:
The core of the example can be used in your own design as a persistent storage area.
If you want to use it as a data exchange mechanism, you'll have to find a way to read the raw data from the card. That can be done with an Arduino.
Another - advanced - option is to implement a supported file systems (fat32?) in HDL,
Whatever you do, make it a nice design and share your work.