Select Page

File creation in SD Card

Objective:The main objective of the project is to interface a Secure Digital Card (SD CARD) with the STR912FA microcontroller which is an ARM966E-S RISC core based microcontroller. The preliminary objective is to read the SD-Card descriptors which specify the manufacture details and the operating values of the given SD-Card. Raw memory access i.e. Writing, Reading and Erasing the memory contents of SD-Card are also achieved in this project. And secondary objective to create a new file in SD-Card and update FAT file system with new file information.

SPI Bus Configuration:

SPI is to be configured as follows

Serial clock rate of 150 kHz i.e. SCR = 0 and Prescaler PR = 150 for 25 MHz master clock. Format Motorola Mode – Master CPHA – LOW CPOL- LOW Data size – 8 Bits

Reading Descriptors:

The details of an SD-Card can be determined from following registers.

CID register contains the card identification information specific to the SD-Card. Manufacturer’s ID (MID), Product code (PNM), Manufacturing date (MDT) are the extracted from the contents of this register. To read the 16 byte CID register, send the command cmd10 and wait for R1 response. If no error bits are set, the SD-Card sends the 16 bytes starting with a start token i.e. 0xfe. Hence wait for 0xfe byte and read 16 bytes cumulatively. The MSB byte of 16 bytes is send first by the card.

CSD register is the Card Specific Data register which contains the information required to access the data on card. CSD_STRUCTURE, READ_BL_LEN, WRITE_BL_LEN, C_SIZE, C_SIZE_MULT, SECTOR_SIZE are the parameters of interest. To read the 16 byte CSD register, send the command cmd9 and wait for R1 response. If no error bits are set, the SD-Card sends the 16 bytes starting with a start token i.e. 0xfe. Hence wait for 0xfe byte and read 16 bytes cumulatively. The MSB byte of 16 bytes is send first by the card. The required fields can be read using bit wise operations and shifting them.

FAT16 File System

FAT16 file system structure contains the following regions

Reserved Region (Boot Sector) File Allocation Table(FAT) Root Directory Data Region (Clusters)

1. The first sector, boot sector contains information which is used to calculate the sizes and locations of the other regions.

2. FAT regions has all the information about clusters. Each cluster has an entry in FAT table.

3. Root directory and its sub-directories contains filename,dates,attribute flags and starting cluster information about the file system objects.

4. Data regions are called Clusters. This contains data of the file. The cluster can either contains a value of the next cluster which contain data for the file, or an end-of-file value which means that there are no more clusters which contain data.

File Creation:

1. Read sector zero (boot sector) to get SD-Card media information. Like bytespersector, sectorsPerCluster etc.

2. Add new file entry in root directory with zero length and reserve next cluster entry for writing file data.

3. Write file data and update data length in root directory.


Source Code: On moodle


Pseudo Code:

 SD_init();
 SD_ReadCardInfo(SEND_CSD,SD_CSD);
 SD_ReadCardInfo(SEND_CID,SD_CID);
 SD_SetDescriptor();
 Display_Descriptors();
 create_FileInSDCard();