• 0
Ajay Ghodke

How to read and write to sd card in Artix 7 Nexys4 board? If i want to access image and work on it and store it back on sd card. Please help me in terms of coding advice i'm new for this.

Question

4 answers to this question

Recommended Posts

  • 0

@Ajay Ghodke,

For being new, you've picked a pretty challenging project!  :P

Reading an image from an SD card requires several layers of processing.  The first part is the physical layer.  The specification outlines one of two means of communicating with a cards physical layer: using the SDIO protocol (a four-wire, bidirectional data/command channel), and using the SPI protocol.  I have personally built a Verilog core that communicates over the SPI protocol, and you are welcome to use my core, while other's have done their work with SDIO and ... I haven't tried their cores so I cannot speak to it.

Across this physical layer, and to get things up and running, a protocol handshake must take place between the card and the controller.  This handshake is outlined in the physical specification above, and also in the instructions for the controller I just mentioned.  For a given card, the handshake isn't all that difficult.  Indeed, for a given class of cards it's probably not that bad.  To process every possible SD card, finding appropriate examples to test with can be a challenge--just to prove that you can handle all the corner cases.

Once you can handle communications at this level, you will then be able to read and write sectors from the SD card.

The next level, beyond sectors, involves reading and understanding the partition table.  This table will tell you where the file systems are on the SD card, and more specifically, what file systems are on the SD card.  In general, most SD cards have only one file system on them so partition processing is pretty simple.  That file system is a FAT filesystem--whether FAT16, FAT32, etc. I'm not certain.  (I haven't gotten this far yet.)

After the partition layer, you will need to process the file system.  Assuming that your SD card has a FAT filesystem on it, there will be two types of files on the system: file containing directory entries, and other.  These files may be found in any order on the SD card, as specified by the file allocation table.  That table contains one entry per cluster on the file system, telling you that after you read the given cluster, where to find the next one.  (Clusters are similar to sectors, but may be implemented as groups of sectors.)  If the filesystem is in proper order, the last cluster will identify itself as the last cluster. 

So, the steps to processing this filesystem involve:

  1. Identifying which version of the FAT system you are working with
  2. Finding, from that information, the first cluster of the root directory
  3. Reading through the directory for the file you want.  (Keep in mind, other clutsers to this directory may be out of order--you'll need to look up their locations in the table.)
  4. If your file is in a subdirectory, you'll have to find the subdirectory entry.
  5. Once you have the file (subdirectory) you want, you'll find a location on the disk where that file begins.  (IIRC, it's the cluster number of the first sector of the file)  You'll also find the length of the file (or subdirectory) you are interested in.
  6. If what you found was a subdirectory, and if it's the subdirectory your file is in (assuming it is in a subdirectory and not the main directory), you'll then need to repeat this process, reading the subdirectory "file" and looking for your file's entry.  (Or perhaps looking for another subdirectory entry.)
  7. From the final entry, you will now know where to find the first cluster of your file, and the full length of the file in bytes.  (It may be longer or shorter than the number of clusters allocated for it in the allocation table.)
  8. The file allocation table will tell you where to find subsequent clusters.
  9. If all you wish to do is to change the image in place, then you now know where to find all the pieces.  At this point, you can change the file as it exists on the SD card at will.

Creating new files on the SD card requires creating a directory entry that file, find empty clusters to hold the file, placing the number of the first cluster into the directory, adjusting the directory length, etc.

It doesn't take long and this becomes an in-depth software task.

I have seen some approaches where individuals have created their own partitions containing their own file system with their own format just to avoid all of this hassle, and to be successful doing this within a microcontroller.  While doable, such solutions tend to be application specific.

Hope this helps, ;)

Dan

 

 

Share this post


Link to post
Share on other sites
  • 0

Hi Cass7771,

This a very challenging project as D@n describes. Unfortunately, we do not have VHDL example code for the PmodSD. I would suggest to built up to this project if you are new to FPGAs due to its complexity.  Here is a link to fpgas4fun which is a great starting point for learning FPGAs.  Here is our resource page for the Nexys 4 DDR. I found 2 demos on our resource page that uses VHDL but they are more complicated projects.  Here is the User demo is what comes loaded in the QSPI for the Nexys 4 DDR. Here is the Nexys4DDR VGA Test Pattern with Mouse Overlay.

Now if you do not have your heart set on using VHDL or Verilog you could use Microblaze with our PmodSD IP.Here is the getting started with Microblaze tutorial. Here is the tutorial on adding our IP's. Here is the PmodSD IP at our Github. Hope this helps!

cheers,

Jon

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now