SD card controller

The SD card controller handles the low-level details of reading and writing data on the DE2-115's SD card. The SD controller is implemented in sd.v.

sd_command and sd_response implement the standard I/O protocol. The command parameters are sd_write, sd_address, and sd_data_write. sd_address specifies the address that the E100 is asking to access.

If sd_write=1, this signifies that the program wants to write the value contained in sd_data_write to the specified location on the SD card.

If sd_write=0, this signifies that the program wants to read the specified location on the SD card. In this case, the data read from the SD card will be returned in the response parameter sd_data_read.

To initialize the contents of an SD card, create a file with the desired contents, then carry out the following steps at a computer in 2331 or 2431 EECS:

  1. Login to the computer.
  2. Insert the SD card into the SD slot of the built-in media card writer on the front panel of the computer. If the operating system mounts any partitions from the SD card, unmount them before continuing.
  3. Wait at least 15 seconds. During this time, the operating system detects the SD card and changes permissions on the device file.
  4. Identify which device is being used for the SD card writer by running the following command and looking for the device that's owned by you:
    ls -l /dev/sd*
    
    Usually, the computers in 2331 EECS use /dev/sdd, and the computers in 2431 EECS use /dev/sdb. E.g., the following shows that /dev/sdb is owned by pmchen.
    eecs2431p02% ls -l /dev/sd*
    brw-r----- 1 root     disk 8,  0 Feb 23 19:32 /dev/sda
    brw-r----- 1 root     disk 8,  1 Feb 23 19:32 /dev/sda1
    brw-r----- 1 root     disk 8,  2 Feb 24 00:32 /dev/sda2
    brw-r----- 1 root     disk 8,  3 Feb 23 19:32 /dev/sda3
    brw-r----- 1 root     disk 8,  4 Feb 23 19:32 /dev/sda4
    brw-r----- 1 root     disk 8,  5 Feb 24 00:32 /dev/sda5
    brw-r----- 1 root     disk 8,  6 Feb 24 00:32 /dev/sda6
    brw-r----- 1 pmchen   disk 8, 16 Feb 23 19:32 /dev/sdb
    brw-r----- 1 root     disk 8, 32 Feb 23 19:32 /dev/sdc
    brw-r----- 1 root     disk 8, 48 Feb 23 19:32 /dev/sdd
    brw-r----- 1 root     disk 8, 64 Feb 23 19:32 /dev/sde
    
  5. Run the following command. FILE refers to the name of the file that has the contents you want to write to the SD card, and DEVICE refers to the device for the SD card writer that you identified in the prior step.
    dd if=FILE of=DEVICE
    
  6. Wait for the dd command to finish and the SD card writer light to stop flashing. Wait another 15 seconds to let the data reach the SD card. To be safe, run sync.

ase100 simulates the SD card controller accurately enough for you to test your device driver and to run assembly-language programs. ase100 simulates the SD card by reading data from a file, which you select when your program runs.

Many of you will use the SD card to store the sound samples and image data needed by your E100 program. The best way to store this data on the SD card is as a binary data file; see the handout on manipulating binary data files for how to create such files.

Performance

SD cards are slower than other forms of memory on the DE2-115 board because they must be read and written in sector-sized units (a sector is 128 words). The SD controller enables programs to read and write individual words by maintaining a sector-sized buffer and by translating word-level accesses into sector-level reads and writes to the SD card. When the E100 program reads or writes a word, the SD controller must first ensure that the sector containing that word is in its buffer. If the sector is not in the buffer, the SD controller will first read that sector from the SD card; this incurs a delay of about 0.5 ms.

When an E100 program writes a word, the SD controller stores this word in the buffer. It does not immediately write this word to the SD card, as this would incur a delay on each write. Later, when the SD controller needs to use the buffer to hold another sector, it will write the modified data stored in the buffer back to the SD card; this incurs a delay of 0.7 ms. If an E100 program wants to force the modified buffer to be written back to the SD card, it should read a different sector than the last sector accessed.

Thus, when an E100 program accesses a sector other than the one it last accessed, the SD controller will take 0.5 or 1.2 ms to satisfy the request: 0.5 ms if the prior sector has not been written to, 1.2 ms if the prior sector has been written to.

If you are playing audio by reading samples directly from the SD card, you will need to buffer about four audio samples to hide the 0.5 ms delay you will encounter when crossing sector boundaries.

Lab 7 task

Write a device driver for the SD card controller that a program can call to read a value from the SD card. Then, write a program that reads a sequence of values from the SD card and displays each value on LED_RED. Initialize the SD card with the contents of this data file; it has a slowly changing sequence of numbers that will generate a recognizable pattern on the LEDs (the first 8192 numbers are 1, the second 8192 numbers are 2, the third 8192 numbers are 4, the fourth 8192 numbers are 8, and so forth).