Project 19 Report
Project 19 Report
-- SD CARD LIBRARY
Submitted by
Pei Xu (px29)
Cornell University
Abstract: This project aims to design and develop a secure digital (SD) card library based
on PIC32 microcontroller. The main function of this system is to read and store files from
the SD card. In addition, this system gives PIC32 developers access to large memory to
store image and files. It also serves for later projects need SD card implementation. Thus,
by using the library, the later PIC32 developers can get the information and write data to
the SD card easily. The basic functions in the SD card library are write and read functions.
The user can access the file stored in the SD card with calling a read function in the library.
2
Individual Contribution
Chang Liu
He researched the way how SD card communicate with the microcontroller. He created the
SPI communication from PIC32 to SD card. Then he coded sd_routines.c, sd_routines.h
and created a user test interface using UART.
Pei Xu
She set up the hardware connection of SD card and PIC32. Together with Chang, Pei
researched the way of implementing FAT file system on the SD card. Pei and Chang worked
together finishing the code of fat32.c and fat32.h.
3
Executive Summary
The current situation in ECE4760 PIC32 developers is that there is a lack of library for
them to directly access the file stored in an SD card. To enhance the feasibility and capability
of the use of PIC32, a SD card library is needed to be created. Therefore, the developers are
According to our research, including the secondary research on the internet, we find that
it is feasible and potential to enrich this peripheral for PIC32 developers. This improvement
will contribute to the convenience for PIC32 developers in their work. Thus, this project aims
to design and develop a secure digital (SD) card library based on PIC32 microcontroller.
The SD card library offers a place to store data, images, sound and other information
which needs of large memory space. The main function of the library is to read and store files
from the SD card. In addition, this library provides the functionality to get the file list from the
root directory.
A user test interface is built based on the communication from computer and PIC32
via UART. Read or Write function selection and other basic functions can be selected from
the user interface. By typing the command on the test console, users can choose the mode,
select the files to open, read or write data to the file. As the SD card library is implemented
separately with the TFT screen, an independent SD card slot is used to design the hardware.
Various tests are designed to verify the functionality of the SD card library system.
By checking the data and information from both computer and PIC32, the tests guaranteed
4
Table of Contents
1 Introduction................................................................................................................. 6
2 Design Alternatives..................................................................................................... 6
2.1 Components............................................................................................................. 6
3 System Design……………………………………………………………………….8
3.2.2 SD initialization…………………………………………………………………13
5 Conclusion…………………………………………………………………………...21
6 Appendix……………………………………………………………………………..22
5
1. Introduction
SD card is a common daily life erasable storage device, because of its large storage capacity
and low price, it is widely used in digital cameras, mobile phones and other digital products.
SD card supports two bus modes: SD mode and SPI mode. SD mode using 6-wire buses,
the use of CLK, CMD, DAT0, DAT1, DAT2, DAT3 for data communication, which has the
data transform rate at 4bits at a time. SPI mode using 4-wire buses, the use of CS, CLK,
DataIn, DataOut, these four ports for exchanging data only has 1 bit at a time which is
slower than the SD mode, but the communication protocol is simple and there is no need
to check the CRC, which is desirable for this project to read and write operations on the
SD card。
2. Design Alternatives
2.1 Components
MicrostickII
6
8GB SDHC card SD card socket
UART Cable
are needed to build the circuit. And the total cost of the project has a budget of 50$.
3. System Design
The SD card contains two basic semiconductor sections, a ‘memory core’ and a ‘SD card
controller’. The ‘memory core’ is the flash memory region where the actual data of the file
is saved. When we format the SD card a file system will be written into this region. Hence
this is the region where the file system exists. The ‘SD card controller’ helps to
communicate the ‘memory core’ with the external devices like microcontrollers. It can
respond to certain set of standard SD commands and read or write data from the memory
core in for the external device. Thus, the ‘SD card controller’ is the device our PIC32 would
communicate with.
As the figure listed above, to read or write data into the SD card. Our team divide the
project into three sections. They are SPI section, SD command section and FAT32 file
system section.
8
FAT 32 File System
SD Command
SPI
SPI buses. The data transmitted and received via SPI can be written and read
II. The internal SD card controller can decode the commands transmitted using SPI.
Those commands are called standard SD command which can read the registers of
III. A FAT32 file system is mapped into the flash memory. This enables the user to
directly access or modify the files. With FAT32 file system, it will be very useful
that the files can be read directly not only from PIC32 microcontroller but in
The pin out for SD card and PIC32 for the SPI interfacing mode is shown in the following
figures.
Number on PIC32
9
27 (GND) GND Ground
card
3 VSS1 Ground
5 CLK Clock
6 VSS2 Ground
10
Figure 4. Connection for PIC32 and SD card
After setting up the circuit, the SPI communication needs to be initialized. The following
code is used to set up SPI. It is mandatory to set up the SPI with a lower clock rate, since
the initialization of SD card only allow low clock frequency. After initialization, the SPI
After initialization, our group is able to transmit and receive data by reading/writing to the
register called SPI1BUF in PIC32. Therefore, two functions are generated with the purpose
11
which are called SPI_transmit and SPI_receive.
SPI_transmit(unsigned char data) Transmit the 8 bits data to the spi buffer
SPI_receive(unsigned char data) Get the 8 bits data from the spi buffer
All the SD commands supported in the SPI mode are 6 bytes long. The MSB is transmitted
first and the actual command occupies the first byte. The command byte is followed by its
4 bytes long arguments. The last byte is the CRC byte respective of the command and the
argument bytes.
When the host sends a command to the SD card, the SD card will first send a corresponding
respond to the host, if the command is not wrong SD card will be followed by the
The structure of a command block in the SPI interface mode of a SD card is shown in the
following figure.
Below is a list of the basic commands our team uses in the project.
12
#define GO_IDLE_STATE 0 #define WRITE_MULTIPLE_BLOCKS 25
SD card default read and write mode is SD mode. To use the SPI mode, our team need to
write CMD0 and CMD1 command to SD controller. After the two commands are written
successfully, we can use SPI mode, which can be easily used for microcontroller to read
and write operations. Our team follows listed below steps for SD initialization.
decimal is preferred)
II. Send the CMD0 command to the SD card (since the highest order of the
command number is always 0 and the second bit is 1, the command sent to the
SD card is the result of 0 or 0x40 operation). The first, third, fifth, and fifth
bytes of the command word are 0x00. The sixth byte of the command word is
III. Checking the response of CMD58, then we can verify whether the SD card is
IV. If (0x00 && cmd == 58) is true, we send 8 extra clock cycles, and then desert
3.2.2 SD initialization
13
I. First send the instruction number CMD1 (0x01 | 0x40 = 0x41), and then send
four 0x00 bytes, and finally send the CRC check code, here 0xFF.
II. Since SD card has been working in SPI mode, SD card does not default to
III. When the CMD1 instruction is sent to the SD card, we send 8 clock cycles
IV. After receiving the response byte of the SD card, the CS line is pulled high
II. Write four address parameters, 4 bytes into a 32-bit address value, the first byte
is 32-bit address value of the highest 8-bit data, the first four bytes is the lowest
VIII. The SD card sends a 512-byte data block with the specified address.
IX. Since the SPI mode does not require the default CRC check, so the two bytes of
II. Write four address parameters, 4 bytes into a 32-bit address value, the first byte
is the lowest 8-bit address 8-bit data, the fourth byte is the highest 32-bit
14
address value 8-bit data.
X. The CS line pulled low if the SD card writes 512 bytes of data to the specified
SD_sendCommand(unsigned char
Send SD command to SD controller
cmd, unsigned long arg);
SD_readSingleBlock(unsigned long
Read data from a specific block
startBlock);
SD_writeSingleBlock(unsigned long
Write data to a specific block
startBlock);
FAT file system is widely used in the windows operating system. FAT32 file system is
employed to store the files in this project. With FAT32 file system, it will be very useful
15
that the files can be read directly not only from PIC32 microcontroller but in windows
From our research and reading FAT file system manual, our group define the
consecutive 8 bit memory locations into ‘Sectors’ and The consecutive Sectors are
grouped to form ‘Clusters’ by regulation. Our team implement FAT32 file system inside
the Memory Core in a particular defined format. There are certain defined Sectors at
the beginning of the Memory Core which are then followed by Clusters. The format of
The very first Sector is the MBR (Master Boot Record) which follows significant number
of Unused Sectors. The Unused Sectors are followed by Reserved Sectors among which
the first Sector is the BOOT Sector. The Reserved Sectors are followed by the FAT Sectors.
The number of FAT Sectors depends upon the size of the file system. The FAT sectors are
followed by few Hidden Sectors. The Hidden Sectors are followed by the Clusters. A File
with a specific name can be read from the FAT32 formatted file system using the logic
shown below; Take a closer look and it can be found that every process finally ends with a
Sector read. This Sector read from the Memory Core of the SD card can be achieved by
16
Figure 7. Logic workflow from FAT32
data
17
deleteFile (unsigned char *fileName); Delete the file
To test the accuracy and reliability of the SD card library two major tests are performed in
I. Winhex is employed to read the information from the SD card on the personal
computer. For instance, if we write data to a specific block on the SD card from
UART of PIC32, the information can be checked using winhex. To evaluate the
accuracy and debug during the design, our team verify the information of SD card
library read and write functions by checking the block data using winhex.
Following is figure when we read the detailed information from a specific block on
18
Figure 8. SD data information read from Winhex
II. To evaluate the reliability of the SD card library, a testing user interface is designed
to test important function in the library. The test interface can communicate from
the computer to PIC32 using UART. In our test baud rate of 9600 is chosen from
putty to transmit and receive data from PIC32. The user interface is shown as
follows including read/ write function to a single block and read/ write data from a
19
Figure 9. Test interface from Putty
As a result, all the functions are running properly without exceptions and all the
information read from Winhex is identical as the information our team write using the
library.
measured by running each function 1000 times. A timer is opened to measure the total
running time. The function used to read or write from a single block has the data size
Single block
Total running Read/ Write
Function name Write/Read
time rate(Bytes/s)
time
SD_readSingleBlock(unsigned
1225ms 1.225ms 417.96KB/s
long startBlock);
SD_writeSingleBlock(unsigned
1343ms 1.343ms 381.23KB/s
long startBlock);
20
5. Conclusion
Overall, SD card library is reliable and is accurate enough to store large files using PIC32.
During the design process, our team faced with various issues and bugs. One of the biggest
issue was that when define the struct using MPLAB IDE, the compiler didn’t allocate each
variable in the struct within a consecutive memory location. To fix the problem, #pragma
pack(1) is needed to be set, so that the compiler would compile the struct in a correct
allocation. The measured speed of write or read data from PIC32 to SD card is roughly
400KB per second. This result is decent, since the 4 bit high speed SD protocol was not
employed. Given 400KB/s read/ write speed from SPI mode, a speed of 2MB/s from SD
mode could be estimated. This is identical as the rate provided from the datasheet driven
by SPI clock frequency of 20MHz by PIC32. To summarize the project design, the
functions our team designed could be easily implemented. With the reliability and decent
transmit speed, the SD card library for PIC32 will enrich design alternatives for PIC users
21
6. Code Appendix
6.1 sd_routines.c
22
23
24
25
26
27
28
29
6.2 sd_routines.h
30
6.3 fat32.c
31
32
33
34
35
36
37
38
39
40
41
42
43
44
6.4 fat32.h
45
46
47
6.5 meng_test.c
48
49
50
51
52
53