Multi Media Card Library
The Multi Media Card (MMC) is a Flash memory card standard. MMC cards are currently available in sizes up to and including 32 GB and are used in cellular phones, digital audio players, digital cameras and PDA’s.
mikroC PRO for AVR provides a library for accessing data on Multi Media Card via SPI communication. This library also supports SD (Secure Digital) and high capacity SDHC (Secure Digital High Capacity) memory cards .
Secure Digital Card
Secure Digital (SD) is a Flash memory card standard, based on the older Multi Media Card (MMC) format.
SD cards are currently available in sizes of up to and including 2 GB, and are used in digital cameras, digital camcorders, handheld computers, media players, mobile phones, GPS receivers, video games and PDAs.
Secure Digital High Capacity Card
SDHC (Secure Digital High Capacity, SD 2.0) is an extension of the SD standard which increases card's storage capacity up to 32 GB by using sector addressing instead of byte addressing in the previous SD standard.
SDHC cards share the same physical and electrical form factor as older (SD 1.x) cards, allowing SDHC-devices to support both newer SDHC cards and older SD-cards. The current standard limits the maximum capacity of an SDHC card to 32 GB.

- Routines for file handling can be used only with FAT16 file system.
- Library functions create and read files from the root directory only.
- Library functions populate both FAT1 and FAT2 tables when writing to files, but the file data is being read from the FAT1 table only; i.e. there is no recovery if the FAT1 table gets corrupted.
- If MMC/SD card has Master Boot Record (MBR), the library will work with the first available primary (logical) partition that has non-zero size. If MMC/SD card has Volume Boot Record (i.e. there is only one logical partition and no MBRs), the library works with entire card as a single partition. For more information on MBR, physical and logical drives, primary/secondary partitions and partition tables, please consult other resources, e.g. Wikipedia and similar.
- Before write operation, make sure you don’t overwrite boot or FAT sector as it could make your card on PC or digital camera unreadable. Drive mapping tools, such as Winhex, can be of a great assistance.
- Library uses SPI module for communication. The user must initialize the appropriate SPI module before using the MMC Library.
The SPI module has to be initialized through SPIx_Init_Advanced
routine with the following parameters:
- SPI Master
- Primary prescaler 64
- Data sampled in the middle of data output time
- Clock idle low
- Serial output data changes on transition form low to high edge

Library Dependency Tree

External dependencies of MMC Library
The following variable must be defined in all projects using MMC library: | Description : | Example : |
---|---|---|
extern sfr sbit Mmc_Chip_Select; |
Chip select pin. | sbit Mmc_Chip_Select at PORTG1_bit; |
extern sfr sbit Mmc_Chip_Select_Direction; |
Direction of the chip select pin. | sbit Mmc_Chip_Select_Direction at DDG1_bit; |
Library Routines
- Mmc_Init
- Mmc_Read_Sector
- Mmc_Write_Sector
- Mmc_Read_Cid
- Mmc_Read_Csd
- Mmc_Multi_Read_Start
- Mmc_Multi_Read_Sector
- Mmc_Multi_Read_Stop
Routines for file handling:
- Mmc_Fat_Init
- Mmc_Fat_QuickFormat
- Mmc_Fat_Assign
- Mmc_Fat_Reset
- Mmc_Fat_Read
- Mmc_Fat_Rewrite
- Mmc_Fat_Append
- Mmc_Fat_Delete
- Mmc_Fat_Write
- Mmc_Fat_Set_File_Date
- Mmc_Fat_Get_File_Date
- Mmc_Fat_Get_File_Date_Modified
- Mmc_Fat_Get_File_Size
- Mmc_Get_File_Write_Sector
- Mmc_Fat_Get_Swap_File
- Mmc_Fat_Tell
- Mmc_Fat_Seek
- Mmc_Fat_Rename
- Mmc_Fat_MakeDir
- Mmc_Fat_RenameDir
- Mmc_Fat_RemoveDir
- Mmc_Fat_ChangeDir
- Mmc_Fat_Exists
- Mmc_Fat_Dir
- Mmc_Fat_ReadDir
- Mmc_Fat_Activate
- Mmc_Fat_ReadN
- Mmc_Fat_Open
- Mmc_Fat_Close
- Mmc_Fat_EOF
Mmc_Init
Prototype |
unsigned char Mmc_Init(); |
---|---|
Returns |
|
Description |
Initializes MMC through hardware SPI interface. Mmc_Init needs to be called before using other functions of this library. |
Requires |
The appropriate hardware SPI module must be previously initialized. Global variables :
The appropriate hardware SPI module must be previously initialized. See the SPI1_Init, SPI1_Init_Advanced routines. |
Example |
// MMC module connections sfr sbit Mmc_Chip_Select at PORTG1_bit; sfr sbit Mmc_Chip_Select_Direction at DDG1_bit; // MMC module connections SPI1_Init_Advanced(_SPI_MASTER, _SPI_FCY_DIV2, _SPI_CLK_LO_LEADING); error = Mmc_Init(); // Init with CS line at PORTG1 |
Mmc_Read_Sector
Prototype |
unsigned char Mmc_Read_Sector(unsigned long sector, char *dbuff); |
---|---|
Returns |
|
Description |
The function reads one sector (512 bytes) from MMC card. Parameters:
|
Requires |
MMC/SD card must be initialized. See Mmc_Init. |
Example |
// read sector 510 of the MMC/SD card unsigned int error; unsigned long sectorNo = 510; char dataBuffer[512]; ... error = Mmc_Read_Sector(sectorNo, dataBuffer); |
Mmc_Write_Sector
Prototype |
unsigned char Mmc_Write_Sector(unsigned long sector, char *dbuff); |
---|---|
Returns |
|
Description |
The function writes 512 bytes of data to one MMC card sector. Parameters:
|
Requires |
MMC/SD card must be initialized. See Mmc_Init. |
Example |
// write to sector 510 of the MMC/SD card unsigned int error; unsigned long sectorNo = 510; char dataBuffer[512]; ... error = Mmc_Write_Sector(sectorNo, dataBuffer); |
Mmc_Read_Cid
Prototype |
unsigned char Mmc_Read_Cid(char *data_cid); |
---|---|
Returns |
|
Description |
The function reads 16-byte CID register. Parameters:
|
Requires |
MMC/SD card must be initialized. See Mmc_Init. |
Example |
unsigned int error; char dataBuffer[16]; ... error = Mmc_Read_Cid(dataBuffer); |
Mmc_Read_Csd
Prototype |
unsigned char Mmc_Read_Csd(char *data_csd); |
---|---|
Returns |
|
Description |
The function reads 16-byte CSD register. Parameters:
|
Requires |
MMC/SD card must be initialized. See Mmc_Init. |
Example |
unsigned int error; char dataBuffer[16]; ... error = Mmc_Read_Csd(dataBuffer); |
Mmc_Multi_Read_Start
Prototype |
unsigned int Mmc_Multi_Read_Start(unsigned long sector); |
---|---|
Description |
The function starts multi read mode, sectors are sequentially read starting from the sector given in the function argument. |
Parameters |
|
Returns |
|
Requires |
MMC/SD card must be initialized. See Mmc_Init. |
Example |
unsigned int error; char sector; ... error = Mmc_Multi_Read_Start(sector); |
Notes |
None. |
Mmc_Multi_Read_Sector
Prototype |
void Mmc_Multi_Read_Sector(char *dbuff); |
---|---|
Description |
The function reads sectors in multi read mode and places them in the buffer given as the function argument. Next function call reads the subsequent sector. Buffer size should be 512B. |
Parameters |
|
Returns |
Nothing. |
Requires |
MMC/SD card must be initialized. See Mmc_Init. |
Example |
unsigned int error; ... Mmc_Multi_Read_Sector(buffer); |
Notes |
None. |
Mmc_Multi_Read_Stop
Prototype |
unsigned int Mmc_Multi_Read_Stop(); |
---|---|
Description |
The function stops multi read mode sequence. |
Parameters |
None. |
Returns |
|
Requires |
MMC/SD card must be initialized. See Mmc_Init. |
Example |
Mmc_Multi_Read_Stop; |
Notes |
None. |
Mmc_Fat_Init
Prototype |
unsigned short Mmc_Fat_Init(); |
---|---|
Returns |
|
Description |
Initializes MMC/SD card, reads MMC/SD FAT16 boot sector and extracts necessary data needed by the library. ![]() |
Requires |
Global variables :
The appropriate hardware SPI module must be previously initialized. See the SPI1_Init, SPI1_Init_Advanced routines. |
Example |
// MMC module connections sfr sbit Mmc_Chip_Select at PORTG_bit1; sfr sbit Mmc_Chip_Select_Direction at DDG1_bit; // MMC module connections // Initialize SPI1 module SPI1_Init_Advanced(_SPI_MASTER, _SPI_FCY_DIV128, _SPI_CLK_LO_LEADING); // use fat16 quick format instead of init routine if a formatting is needed if (!Mmc_Fat_Init()) { // reinitialize SPI1 at higher speed SPI1_Init_Advanced(_SPI_MASTER, _SPI_FCY_DIV2, _SPI_CLK_LO_LEADING); ... } |
Mmc_Fat_QuickFormat
Prototype |
unsigned char Mmc_Fat_QuickFormat(char *mmc_fat_label); |
---|---|
Returns |
|
Description |
Formats to FAT16 and initializes MMC/SD card. Parameters:
![]()
|
Requires |
The appropriate hardware SPI module must be previously initialized. |
Example |
// Format and initialize MMC/SD card and MMC_FAT16 library globals if (!Mmc_Fat_QuickFormat(&mmc_fat_label)) { ... } |
Mmc_Fat_Assign
Prototype |
unsigned short Mmc_Fat_Assign(char *filename, char file_cre_attr); |
|||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
|||||||||||||||||||||||||||
Description |
Assigns file for file operations (read, write, delete...). All subsequent file operations will be applied on an assigned file. Parameters:
![]() |
|||||||||||||||||||||||||||
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. |
|||||||||||||||||||||||||||
Example |
// create file with archive attribute if it does not already exist Mmc_Fat_Assign("MIKRO007.TXT",0xA0); |
Mmc_Fat_Reset
Prototype |
void Mmc_Fat_Reset(unsigned long *size); |
---|---|
Returns |
Nothing. |
Description |
Procedure resets the file pointer (moves it to the start of the file) of the assigned file, so that the file can be read. Parameters:
|
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. The file must be previously assigned. See Mmc_Fat_Assign. |
Example |
unsigned long size; ... Mmc_Fat_Reset(&size); |
Mmc_Fat_Read
Prototype |
void Mmc_Fat_Read(unsigned short *bdata); |
---|---|
Returns |
Nothing. |
Description |
Reads a byte from the currently assigned file opened for reading. Upon function execution, file pointers will be set to the next character in the file. Parameters:
|
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. The file must be previously assigned. See Mmc_Fat_Assign. The file must be opened for reading. See Mmc_Fat_Reset. |
Example |
char character; ... Mmc_Fat_Read(&character); |
Mmc_Fat_Rewrite
Prototype |
void Mmc_Fat_Rewrite(); |
---|---|
Returns |
Nothing. |
Description |
Opens the currently assigned file for writing. If the file is not empty its content will be erased. |
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. The file must be previously assigned. See Mmc_Fat_Assign. |
Example |
// open file for writing Mmc_Fat_Rewrite(); |
Mmc_Fat_Append
Prototype |
void Mmc_Fat_Append(); |
---|---|
Returns |
Nothing. |
Description |
Opens the currently assigned file for appending. Upon this function execution file pointers will be positioned after the last byte in the file, so any subsequent file writing operation will start from there. |
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. The file must be previously assigned. See Mmc_Fat_Assign. |
Example |
// open file for appending Mmc_Fat_Append(); |
Mmc_Fat_Delete
Prototype |
char Mmc_Fat_Delete(); |
---|---|
Returns |
|
Description |
Deletes currently assigned file from MMC/SD card. |
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. The file must be previously assigned. See Mmc_Fat_Assign. |
Example |
// delete current file if (Mmc_Fat_Delete() == 0) ... |
Mmc_Fat_Write
Prototype |
void Mmc_Fat_Write(char *fdata, unsigned data_len); |
---|---|
Returns |
Nothing. |
Description |
Writes requested number of bytes to the currently assigned file opened for writing. Parameters:
|
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. The file must be previously assigned. See Mmc_Fat_Assign. The file must be opened for writing. See Mmc_Fat_Rewrite or Mmc_Fat_Append. |
Example |
Mmc_Fat_Write(txt,255); Mmc_Fat_Write("Hello world",255); |
Mmc_Fat_Set_File_Date
Prototype |
void Mmc_Fat_Set_File_Date(unsigned int year, unsigned short month, unsigned short day, unsigned short hours, unsigned short mins, unsigned short seconds); |
---|---|
Returns |
Nothing. |
Description |
Sets the date/time stamp. Any subsequent file writing operation will write this stamp to the currently assigned file's time/date attributs. Parameters:
|
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. The file must be previously assigned. See Mmc_Fat_Assign. The file must be opened for writing. See Mmc_Fat_Rewrite or Mmc_Fat_Append. |
Example |
// April 1st 2005, 18:07:00 Mmc_Fat_Set_File_Date(2005, 4, 1, 18, 7, 0); |
Mmc_Fat_Get_File_Date
Prototype |
void Mmc_Fat_Get_File_Date(unsigned int *year, unsigned short *month, unsigned short *day, unsigned short *hours, unsigned short *mins); |
---|---|
Returns |
Nothing. |
Description |
Reads time/date attributes of the currently assigned file. Parameters:
|
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. The file must be previously assigned. See Mmc_Fat_Assign. |
Example |
// get Date/time of file unsigned yr; char mnth, dat, hrs, mins; ... file_Name = "MYFILEABTXT"; Mmc_Fat_Assign(file_Name); Mmc_Fat_Get_File_Date(&yr, &mnth, &day, &hrs, &mins); |
Mmc_Fat_Get_File_Date_Modified
Prototype |
void Mmc_Fat_Get_File_Date_Modified(unsigned int *year, unsigned short *month, unsigned short *day, unsigned short *hours, unsigned short *mins); |
---|---|
Returns |
Nothing. |
Description |
Retrieves the last modification date/time for the currently selected file. Parameters:
|
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. The file must be previously assigned. See Mmc_Fat_Assign. |
Example |
// get modification Date/time of file unsigned yr; char mnth, dat, hrs, mins; ... file_Name = "MYFILEABTXT"; Mmc_Fat_Assign(file_Name); Mmc_Fat_Get_File_Date_Modified(&yr, &mnth, &day, &hrs, &mins); |
Mmc_Fat_Get_File_Size
Prototype |
unsigned long Mmc_Fat_Get_File_Size(); |
---|---|
Returns |
This function returns size of active file (in bytes). |
Description |
This function reads size of the currently assigned file in bytes. |
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. The file must be previously assigned. See Mmc_Fat_Assign. |
Example |
// get Date/time of file unsigned yr; char mnth, dat, hrs, mins; ... file_name = "MYFILEXXTXT"; Mmc_Fat_Assign(file_name); mmc_size = Mmc_Fat_Get_File_Size; |
Mmc_Get_File_Write_Sector
Prototype |
unsigned long Mmc_Get_File_Write_Sector(); |
---|---|
Description |
This function returns the current file write sector. |
Parameters |
None. |
Returns |
This function returns the current file write sector. |
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. The file must be previously assigned. See Mmc_Fat_Assign. |
Example |
unsigned long sector; ... sector = Mmc_Get_File_Write_Sector(); |
Notes |
None. |
Mmc_Fat_Get_Swap_File
Prototype |
unsigned long Mmc_Fat_Get_Swap_File(unsigned long sectors_cnt, char* filename, char file_attr); |
|||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
|||||||||||||||||||||||||||
Description |
This function is used to create a swap file of predefined name and size on the MMC/SD media. If a file with specified name already exists on the media, search for consecutive sectors will ignore sectors occupied by this file. Therefore, it is recommended to erase such file if it already exists before calling this function. If it is not erased and there is still enough space for a new swap file, this function will delete it after allocating new memory space for a new swap file. The purpose of the swap file is to make reading and writing to MMC/SD media as fast as possible, by using the Mmc_Read_Sector() and Mmc_Write_Sector() functions directly, without potentially damaging the FAT system. The swap file can be considered as a "window" on the media where the user can freely write/read data. Its main purpose in the library is to be used for fast data acquisition; when the time-critical acquisition has finished, the data can be re-written into a "normal" file, and formatted in the most suitable way. Parameters:
![]() |
|||||||||||||||||||||||||||
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. |
|||||||||||||||||||||||||||
Example |
//-------------- Tries to create a swap file, whose size will be at least 100 sectors. //If it succeeds, it sends the No. of start sector over UART void M_Create_Swap_File(){ size = Mmc_Fat_Get_Swap_File(100); if (size <> 0) { UART1_Write(0xAA); UART1_Write(Lo(size)); UART1_Write(Hi(size)); UART1_Write(Higher(size)); UART1_Write(Highest(size)); UART1_Write(0xAA); } } |
Mmc_Fat_Tell
Prototype |
unsigned long Mmc_Fat_Tell(); |
---|---|
Description |
This routine is used to retrieve the cursor position within an opened file. |
Parameters |
None. |
Returns |
Returns the cursor position in currently assigned file. |
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. The file must be previously assigned. See Mmc_Fat_Assign. |
Example |
unsigned long position; position = Mmc_Fat_Tell(); |
Notes |
None. |
Mmc_Fat_Seek
Prototype |
unsigned long Mmc_Fat_Seek(unsigned long position); |
---|---|
Description |
This routine is used to set the cursor position within an opened file and returns the cursor's new position within an opened file. |
Parameters |
|
Returns |
Returns the cursor's new position in currently assigned file. |
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. The file must be previously assigned. See Mmc_Fat_Assign. |
Example |
unsigned long position; position = Mmc_Fat_Seek(1000); |
Notes |
If the desired cursor position exceeds file size, the cursor will be placed at the end of the file. |
Mmc_Fat_Rename
Prototype |
char Mmc_Fat_Rename(char *newname); |
---|---|
Description |
This function renames the currently assigned file. |
Parameters |
|
Returns |
|
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. The file must be previously assigned. See Mmc_Fat_Assign. |
Example |
if (0 == Mmc_Fat_Rename("NEWNAME.TXT")) { // if rename operation was successful... ... } |
Notes |
None. |
Mmc_Fat_MakeDir
Prototype |
char Mmc_Fat_MakeDir(char *name, char attrib); |
|||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description |
This function creates a new directory. |
|||||||||||||||||||||||||||
Parameters |
|
|||||||||||||||||||||||||||
Returns |
|
|||||||||||||||||||||||||||
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. |
|||||||||||||||||||||||||||
Example |
if (0 == Mmc_Fat_MakeDir("DIR_A", 0xA0)) { // create DIR_A directory ... } |
|||||||||||||||||||||||||||
Notes |
None. |
Mmc_Fat_RenameDir
Prototype |
char Mmc_Fat_RenameDir(char *oldname, char *newname); |
---|---|
Description |
This function renames a directory. |
Parameters |
|
Returns |
|
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. |
Example |
if (0 == Mmc_Fat_RenameDir("DIR_A", "DIR_B")) { // if rename operation was successful... ... } |
Notes |
None. |
Mmc_Fat_RemoveDir
Prototype |
char Mmc_Fat_RemoveDir(char *name); |
---|---|
Description |
This function removes directory entry from current directory. |
Parameters |
|
Returns |
|
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. |
Example |
if (0 == Mmc_Fat_RemoveDir("DIR_A")) { // if removing operation was successful... ... } |
Notes |
Recursive removing is not supported, i.e. the directory must be empty before it can be removed. |
Mmc_Fat_ChangeDir
Prototype |
char Mmc_Fat_ChangeDir(char *name); |
---|---|
Description |
This function changes the current directory to |
Parameters |
|
Returns |
|
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. |
Example |
// enter DIR_A directory if (0 == Mmc_Fat_ChangeDir("DIR_A")) { ... } // go to parent directory if (0 == Mmc_Fat_ChangeDir("..")) { ... } // go to root directory if (0 == Mmc_Fat_ChangeDir("\\")) { ... } |
Notes |
Special directory names like " |
Mmc_Fat_Exists
Prototype |
char Mmc_Fat_Exists(char *name); |
---|---|
Description |
This function returns information on file/directory existence. |
Parameters |
|
Returns |
|
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. |
Example |
status = Mmc_Fat_Exists("X_FILES.TXT"); if (1 == status) { ... } // if the file was found... else if (0 == status) { ... } // or if the file was not found... else { ... } // or if there was an error during function call, // which needs to be handled separately. |
Notes |
None. |
Mmc_Fat_Dir
Prototype |
char Mmc_Fat_Dir(void (*print)(char ch)) |
---|---|
Description |
This routine displays contents of the current directory via user-defined medium (i.e. UART module, a file on FAT16 file system). The function displays character by character. |
Parameters |
|
Returns |
|
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. |
Example |
// Displaying routine void PrintChar(char ch) { UART1_Write(ch); } ... Mmc_Fat_Dir(PrintChar); |
Notes |
None. |
Mmc_Fat_ReadDir
Prototype |
static short Mmc_Fat_ReadDir(DIR *d); |
---|---|
Description |
This function gets next directory entry from current directory. |
Parameters |
|
Returns |
|
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. |
Example |
if (1 == Mmc_Fat_ReadDir("DIR_A")) { ... } |
Notes |
None. |
Mmc_Fat_Activate
Prototype |
char Mmc_Fat_Activate(short fileHandle); |
---|---|
Description |
This function selects active file of currently opened files. |
Parameters |
|
Returns |
|
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. |
Example |
short fhandle; fhandle = Mmc_Fat_Open("X_FILES.TXT", FILE_READ, 0x01); if (Mmc_Fat_Activate(fhandle)) == 0) { } |
Notes |
Use Mmc_Fat_Open function to get file handles. |
Mmc_Fat_ReadN
Prototype |
unsigned int Mmc_Fat_ReadN(char *fdata, unsigned int n); |
---|---|
Description |
This function reads multiple bytes. |
Parameters |
|
Returns |
Number of read bytes. |
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. |
Example |
char data_buffer[512]; unsigned int no_bytes; ... no_bytes = Mmc_Fat_ReadN(&data_buffer, 500); |
Notes |
None. |
Mmc_Fat_Open
Prototype |
short Mmc_Fat_Open(unsigned char *name, unsigned short mode, char attrib); |
|||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description |
This function opens a new file. |
|||||||||||||||||||||||||||
Parameters |
|
|||||||||||||||||||||||||||
Returns |
|
|||||||||||||||||||||||||||
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. |
|||||||||||||||||||||||||||
Example |
short fhandle; fhandle = Mmc_Fat_Open("X_FILES.TXT", FILE_READ, 0x01); |
|||||||||||||||||||||||||||
Notes |
None. |
Mmc_Fat_Close
Prototype |
char Mmc_Fat_Close(); |
---|---|
Description |
This function closes currently opened file. |
Parameters |
None. |
Returns |
|
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. |
Example |
if (Mmc_Fat_Close() == 0) { } |
Notes |
None. |
Mmc_Fat_EOF
Prototype |
short Mmc_Fat_EOF(); |
---|---|
Description |
This function check if the end of file is reached. |
Parameters |
None. |
Returns |
|
Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. |
Example |
if (Mmc_Fat_EOF() == 0) { } |
Notes |
None. |
Library Example
The following example demonstrates usage of the MMC and MMC_FAT routines.
// MMC module connections sbit Mmc_Chip_Select at PORTG1_bit; sbit Mmc_Chip_Select_Direction at DDG1_bit; // eof MMC module connections // Variables for MMC routines unsigned char SectorData[512]; // Buffer for MMC sector reading/writing unsigned char data_for_registers[16]; // buffer for CID and CSD registers // UART1 write text and new line (carriage return + line feed) void UART1_Write_Line(char *uart_text) { UART1_Write_Text(uart_text); UART1_Write(13); UART1_Write(10); } // Display byte in hex void PrintHex(unsigned char i) { unsigned char hi,lo; hi = i & 0xF0; // High nibble hi = hi >> 4; hi = hi + '0'; if (hi>'9') hi=hi+7; lo = (i & 0x0F) + '0'; // Low nibble if (lo>'9') lo=lo+7; UART1_Write(hi); UART1_Write(lo); } void main() { const char FILL_CHAR = 'm'; unsigned int i, SectorNo; char mmc_error; bit data_ok; // Initialize UART1 module UART1_Init(19200); Delay_ms(10); UART1_Write_Line("MCU-Started"); // MCU present report // Initialize SPI1 module SPI1_Init_Advanced(_SPI_MASTER, _SPI_FCY_DIV2, _SPI_CLK_LO_LEADING); // initialise a MMC card mmc_error = Mmc_Init(); if(mmc_error == 0) UART1_Write_Line("MMC Init-OK"); // If MMC present report else UART1_Write_Line("MMC Init-error"); // If error report // Fill MMC buffer with same characters for(i=0; i<=511; i++) SectorData[i] = FILL_CHAR; // Choose sector SectorNo = 590; // Write sector mmc_error = Mmc_Write_Sector(SectorNo, SectorData); if(mmc_error == 0) UART1_Write_Line("Write-OK"); else // if there are errors..... UART1_Write_Line("Write-Error"); // Reading of CID register mmc_error = Mmc_Read_Cid(data_for_registers); if(mmc_error == 0) { UART1_Write_Text("CID : "); for(i=0; i<=15; i++) PrintHex(data_for_registers[i]); UART1_Write_Line(""); } else UART1_Write_Line("CID-error"); // Reading of CSD register mmc_error = Mmc_Read_Csd(data_for_registers); if(mmc_error == 0) { UART1_Write_Text("CSD : "); for(i=0; i<=15; i++) PrintHex(data_for_registers[i]); UART1_Write_Line(""); } else UART1_Write_Line("CSD-error"); // Read sector mmc_error = Mmc_Read_Sector(SectorNo, SectorData); if(mmc_error == 0) { UART1_Write_Line("Read-OK"); // Chech data match data_ok = 1; for(i=0; i<=511; i++) { UART1_Write(SectorData[i]); if (SectorData[i] != FILL_CHAR) { data_ok = 0; break; } } UART1_Write_Line(""); if (data_ok) UART1_Write_Line("Content-OK"); else UART1_Write_Line("Content-Error"); } else // if there are errors..... UART1_Write_Line("Read-Error"); // Signal test end UART1_Write_Line("Test End."); }
HW Connection
MMC interface
MMC back view
What do you think about this topic ? Send us feedback!