Objective:

In this tutorial we will discuss how to interface SD CARD with lpc1768 to read and write the fat32 files.


References

The original SD card library for AVR controllers can be downloaded from http://www.dharmanitech.com/.
These libraries have been modified to provide the standard interfaces like File_Write/File_read and also supports multiple file access.
Check the below links for fat32 basics:
https://www.win.tue.nl/~aeb/linux/fs/fat/fat-1.html
http://www.tavi.co.uk/phobos/fat.html



Configuration

The number of files to be handled simultaneously can be configured in fat32.h using the constant C_MaxFilesOpening_U8.
To print the debug messages set FAT32_DEBUG_ENABLE to 1.


Limitations and Warning

The file name should max 11-chars including the file type.
There are chances of SD card getting corrupted if the libraries are not used properly.
Below are some tips to avoid the sd card corruption.

  1. Ensure the file is terminated with EOF before closing.
  2. Do not disconnect the SD card until the files are closed in software.
  3. While writing the data to file. Open the file, write the data and close it. Ensure these steps are followed in one go.



File Handling Functions

The below table shows the list of functions to access the sd card.
Refer fat32.c/fat32.h for more info.

Function Description
FILE_Open This functions opens the specified file in requested mode(READ/WRITE/APPEND).
FILE_Close This functions closes the file and releases the memory used by the file.
FILE_Delete This functions deletes the file from SD card.
FILE_GetCh This functions reads a byte of data from the file, Once the End of File is reached it returns EOF(26)
FILE_PutCh This functions writes a byte of data to the file, EOF(26) needs to be passed to mark the end of file and save it to SD card.
FILE_GetList This functions traverses through the SD card and lists the available files with their size.
FILE_GetMemoryStatics This functions calculates the total and free memory of the SD card. It takes upto 10secs(for 32GB) to calculate the memory statics depending on the size of CARD





Formatting SD Card

As this Sd Crad library is for fat32, the card needs to be formatted using fat32 settings as shown below.

Fat32Formatting.png

Connection Diagram

code

Below example shows the usage of all the fat32 functions.

#include <lpc17xx.h>
#include "sdcard.h"
#include "uart.h"
#include "fat32.h"
#include "stdutils.h"
#include "delay.h"
#include "spi.h"
#define FILE_READ '1'
#define FILE_WRITE '2'
#define FILE_COPY '3'
#define FILE_DELETE '4'
#define FILE_LIST '5'
#define MEMORY_STATICS '6'
int main()
{
uint8_t returnStatus,sdcardType,option;
char ch,sourceFileName[12],destFileName[12];
fileConfig_st *srcFilePtr,*destFilePtr;
fileInfo fileList;
uint32_t totalMemory,freeMemory;
SystemInit();
UART0_Init(9600);
returnStatus = SD_Init(&sdcardType);
if(returnStatus)
{
if(returnStatus == SDCARD_NOT_DETECTED)
{
UART0_TxString("\n\r SD card not detected..");
}
else if(returnStatus == SDCARD_INIT_FAILED)
{
UART0_TxString("\n\r Card Initialization failed..");
}
else if(returnStatus == SDCARD_FAT_INVALID)
{
UART0_TxString("\n\r Invalid Fat filesystem");
}
while(1);
}
else
{
UART0_TxString("\n\rSD Card Detected!");
}
while(1)
{
UART0_TxString("\n\r\n\rPress any key to continue");
UART0_RxChar();
UART0_TxString("\n\r\n\r------File options---------\n\r");
UART0_TxString("1: Read File \n\r");
UART0_TxString("2: Write File \n\r");
UART0_TxString("3: File Copy \n\r");
UART0_TxString("4: Delete File \n\r");
UART0_TxString("5: Get File List \n\r");
UART0_TxString("6: Memory Statics \n\r");
UART0_TxString("--------------------------- \n\r");
UART0_TxString("Choose one of the options: \n\r");
do
{
option = UART0_RxChar();
}while((option<'1') || (option>'6'));
UART0_TxChar(option);
UART0_TxString("\n\r\n\r");
switch(option)
{
case FILE_READ: /* Read a File */
UART0_TxString("\n\rEnter File name max 11 chars including file type: ");
UART0_RxString(sourceFileName);
srcFilePtr = FILE_Open(sourceFileName,READ,&returnStatus);
if(srcFilePtr == 0)
{
UART0_TxString("\n\rFile Opening Failed");
}
else
{
UART0_TxString("File Content: ");
while(1)
{
ch = FILE_GetCh(srcFilePtr);
if(ch == EOF)
break;
UART0_TxChar(ch);
}
FILE_Close(srcFilePtr);
}
break;
case FILE_WRITE: /* Write to a File */
UART0_TxString("\n\rEnter File name max 11 chars including file type: ");
UART0_RxString(sourceFileName);
srcFilePtr = FILE_Open(sourceFileName,WRITE,&returnStatus);
if(srcFilePtr == 0)
{
UART0_TxString("\n\rFile Opening Failed");
}
else
{
UART0_TxString("\n\rEnter text ending with '>' :");
while(1)
{
ch = UART0_RxChar();
if(ch == '>')
{
FILE_PutCh(srcFilePtr,EOF);
break;
}
else
{ UART0_TxChar(ch);
FILE_PutCh(srcFilePtr,ch);
}
}
UART0_TxString("\n\rData saved to file, closing the file.");
FILE_Close(srcFilePtr);
}
break;
case FILE_COPY: /* File Copy */
UART0_TxString("\n\rEnter source File name max 11 chars including file type: ");
UART0_RxString(sourceFileName);
srcFilePtr = FILE_Open(sourceFileName,READ,&returnStatus);
if(srcFilePtr == 0)
{
UART0_TxString("\n\rSource File Opening Failed");
}
else
{
UART0_TxString("\n\rEnter destination File name max 11 chars including file type: ");
UART0_RxString(destFileName);
destFilePtr = FILE_Open(destFileName,WRITE,&returnStatus);
if(destFilePtr == 0)
{
UART0_TxString("\n\rDestination File Opening Failed");
}
else
{
UART0_TxString("\n\rCopying the file.");
while(1)
{
ch = FILE_GetCh(srcFilePtr);
FILE_PutCh(destFilePtr,ch);
if(ch == EOF)
{
break;
}
}
UART0_TxString("\n\rDone Copying..");
FILE_Close(destFilePtr);
}
FILE_Close(srcFilePtr);
}
break;
case FILE_DELETE: // FIle Delete
UART0_TxString("\n\rEnter File name to be deleted max 11 chars including file type: ");
UART0_RxString(sourceFileName);
UART0_TxString("\n\rDeleting File: ");
returnStatus = FILE_Delete(sourceFileName);
if(returnStatus == FAT32_FILE_OPENED_CANNOT_BE_DELETED)
{
UART0_TxString("\n\rFile is open cannot be deleted");
}
else
{
UART0_TxString("\n\rDone! File Deleted");
}
break;
case FILE_LIST: //Print the files with size
while(1)
{
returnStatus = FILE_GetList(&fileList);
if(returnStatus != FAT32_END_OF_FILE_LIST)
{
UART0_Printf("\n\r%s %Ubytes",fileList.fileName,fileList.fileSize);
}
else
{
break;
}
}
break;
case MEMORY_STATICS: //Print the SD CARD memory Total/Free in bytes
UART0_TxString("\n\rMemory Statics is being calculated..");
FILE_GetMemoryStatics(&totalMemory,&freeMemory);
UART0_Printf("\n\rTotal memory:%Ubytes Free memory:%Ubytes",totalMemory,freeMemory);
break;
}// End of switch
}// End of while
}// End of main



0SdCardInterface.gif

Downloads

Download the complete project folder from the below link: https://codeload.github.com/ExploreEmbedded/Explore-Cortex-M3-LPC1768-Stick-DVB-14001/zip/master




Have a opinion, suggestion , question or feedback about the article let it out here!