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 ARM 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.

  Important :

The SPI module has to be initialized through SPIx_Init_Advanced routine with the following parameters:

  Tip : Once the MMC/SD card is initialized, SPI module can be reinitialized at higher a speed. See the Mmc_Init and Mmc_Fat_Init routines.

Library Dependency Tree

MMC Library Dependency Tree

External dependencies of MMC Library

Stellaris

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 GPIO_PORTA_DATA7_bit;
extern sfr sbit Mmc_Chip_Select_Direction; Direction of the chip select pin. sbit Mmc_Chip_Select_Direction at GPIO_PORTA_DIR7_bit;

STM32

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 GPIOD_ODR.B3;

Library Routines

Routines for file handling:

Mmc_Init

Prototype

unsigned int Mmc_Init();

Description

Initializes MMC through hardware SPI interface.

Mmc_Init needs to be called before using other functions of this library.

Parameters

None.

Returns

  • 0 - if MMC/SD card was detected and successfully initialized
  • 1 - otherwise

Requires

External dependencies of the library from the top of the page must be defined before using this function.

The appropriate hardware SPI module must be previously initialized.
See the SPIx_Init, SPIx_Init_Advanced routines.

Example

Stellaris

// MMC module connections
sbit Mmc_Chip_Select           at GPIO_PORTA_DATA7_bit;  // for writing to output pin always use latch
sbit Mmc_Chip_Select_Direction at GPIO_PORTA_DIR7_bit;
// eof MMC module connections
...
//--- initialize SPI0
SPI0_Init_Advanced(1000000, _SPI_MASTER, _SPI_8_BIT | _SPI_CLK_IDLE_HIGH | _SPI_SECOND_CLK_EDGE_TRANSITION, &_GPIO_MODULE_SPI0_A245);

// initialize a MMC card
mmc_error = Mmc_Init();

STM32

// MMC module connections
sbit Mmc_Chip_Select           at GPIOD_ODR.B3;
// eof MMC module connections
...
//--- initialize SPI3
SPI3_Init_Advanced(_SPI_FPCLK_DIV4, _SPI_MASTER | _SPI_8_BIT |
                   _SPI_CLK_IDLE_LOW | _SPI_FIRST_CLK_EDGE_TRANSITION |
                   _SPI_MSB_FIRST | _SPI_SS_DISABLE | _SPI_SSM_ENABLE | _SPI_SSI_1,
                   &_GPIO_MODULE_SPI3_PC10_11_12);

// initialize a MMC card
mmc_error = Mmc_Init();
Notes

None.

Mmc_Read_Sector

Prototype

unsigned int Mmc_Read_Sector(unsigned long sector, char *dbuff);

Description

The function reads one sector (512 bytes) from MMC card.

Parameters
  • sector: MMC/SD card sector to be read.
  • dbuff: buffer of minimum 512 bytes in length for data storage.
Returns

  • 0 - if reading was successful
  • 1 - if an error occurred

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);
Notes

None.

Mmc_Write_Sector

Prototype

unsigned int Mmc_Write_Sector(unsigned long sector, char *dbuff);

Description

The function writes 512 bytes of data to one MMC card sector.

Parameters
  • sector: MMC/SD card sector to be written to.
  • dbuff: data to be written (buffer of minimum 512 bytes in length).
Returns
  • 0 - if writing was successful
  • 1 - if there was an error in sending write command
  • 2 - if there was an error in writing (data rejected)
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);
Notes

None.

Mmc_Read_Cid

Prototype

unsigned int Mmc_Read_Cid(char *data_cid);

Description

The function reads 16-byte CID register.

Parameters
  • data_cid: buffer of minimum 16 bytes in length for storing CID register content.
Returns
  • 0 - if CID register was read successfully
  • 1 - if there was an error while reading
Requires

MMC/SD card must be initialized. See Mmc_Init.

Example
unsigned int error;
char dataBuffer[16];
...
error = Mmc_Read_Cid(dataBuffer);
Notes

None.

Mmc_Read_Csd

Prototype

unsigned int Mmc_Read_Csd(char *data_csd);

Description

The function reads 16-byte CSD register.

Parameters
  • data_csd: buffer of minimum 16 bytes in length for storing CSD register content.
Returns
  • 0 - if CSD register was read successfully
  • 1 - if there was an error while reading
Requires

MMC/SD card must be initialized. See Mmc_Init.

Example
unsigned int error;
char dataBuffer[16];
...
error = Mmc_Read_Csd(dataBuffer);
Notes

None.

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
  • sector: starting sector number.
Returns
  • 0 - if multi read start was successful.
  • 1 - ir error occured.
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
  • dbuff: buffer for holding the sector data.
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
  • 0 - if stop was successful.
  • 1 - ir error was detected.
Requires

MMC/SD card must be initialized. See Mmc_Init.

Example
Mmc_Multi_Read_Stop;
Notes

None.

Mmc_Fat_Init

Prototype

unsigned int Mmc_Fat_Init();

Description

Initializes MMC/SD card, reads MMC/SD FAT16 boot sector and extracts necessary data needed by the library.

Parameters

None.

Returns
  • 0 - if MMC/SD card was detected and successfully initialized
  • 1 - if FAT16 boot sector was not found
  • 255 - if MMC/SD card was not detected
Requires

External dependencies of the library from the top of the page must be defined before using this function.

The appropriate hardware SPI module must be previously initialized.
See the SPIx_Init, SPIx_Init_Advanced routines.

Example

Stellaris

// MMC module connections
sbit Mmc_Chip_Select           at GPIO_PORTA_DATA7_bit;  // for writing to output pin always use latch
sbit Mmc_Chip_Select_Direction at GPIO_PORTA_DIR7_bit;
// eof MMC module connections

...
// set up SPI for MMC init (low speed)
SPI0_Init_Advanced(SPI_400kHz, _SPI_MASTER, _SPI_8_BIT | _SPI_CLK_IDLE_HIGH | _SPI_SECOND_CLK_EDGE_TRANSITION, &_GPIO_MODULE_SPI0_A245);
// Loop until MMC is initialized
while (Mmc_Init())
  ;  
// reinitialize spi at higher speed
SPI0_Init_Advanced(SPI_10MHz, _SPI_MASTER, _SPI_8_BIT | _SPI_CLK_IDLE_HIGH | _SPI_SECOND_CLK_EDGE_TRANSITION, &_GPIO_MODULE_SPI0_A245);

STM32

// MMC module connections
sbit Mmc_Chip_Select           at GPIOD_ODR.B3;
// eof MMC module connections
...
//--- set up SPI for MMC init (low speed)
SPI3_Init_Advanced(_SPI_FPCLK_DIV64, _SPI_MASTER | _SPI_8_BIT |
                   _SPI_CLK_IDLE_LOW | _SPI_FIRST_CLK_EDGE_TRANSITION |
                   _SPI_MSB_FIRST | _SPI_SS_DISABLE | _SPI_SSM_ENABLE | _SPI_SSI_1,
                   &_GPIO_MODULE_SPI3_PC10_11_12);

// Loop until MMC is initialized
while (Mmc_Init())
  ;  
// reinitialize spi at higher speed
SPI3_Init_Advanced(_SPI_FPCLK_DIV2, _SPI_MASTER | _SPI_8_BIT |
                   _SPI_CLK_IDLE_LOW | _SPI_FIRST_CLK_EDGE_TRANSITION |
                   _SPI_MSB_FIRST | _SPI_SS_DISABLE | _SPI_SSM_ENABLE | _SPI_SSI_1,
                   &_GPIO_MODULE_SPI3_PC10_11_12); 
Notes

MMC/SD card has to be formatted to FAT16 file system.

Mmc_Fat_QuickFormat

Prototype

unsigned int Mmc_Fat_QuickFormat(char *mmc_fat_label);

Description

Formats to FAT16 and initializes MMC/SD card.

Parameters
  • mmc_fat_label: volume label (11 characters in length). If less than 11 characters are provided, the label will be padded with spaces. If null string is passed volume will not be labeled
Returns
  • 0 - if MMC/SD card was detected, successfully formated and initialized
  • 1 - if FAT16 format was unseccessful
  • 255 - if MMC/SD card was not detected
Requires

The appropriate hardware SPI module must be previously initialized.
See the SPIx_Init, SPIx_Init_Advanced routines.

Example

Stellaris

// set up SPI for MMC init (low speed)
SPI0_Init_Advanced(SPI_400kHz, _SPI_MASTER, _SPI_8_BIT | _SPI_CLK_IDLE_HIGH | _SPI_SECOND_CLK_EDGE_TRANSITION, &_GPIO_MODULE_SPI0_A245);

Mmc_Fat_QuickFormat("mikroE");

// reinitialize spi at higher speed
SPI0_Init_Advanced(SPI_10MHz, _SPI_MASTER, _SPI_8_BIT | _SPI_CLK_IDLE_HIGH | _SPI_SECOND_CLK_EDGE_TRANSITION, &_GPIO_MODULE_SPI0_A245);

STM32

// set up SPI for MMC init (low speed)
SPI3_Init_Advanced(_SPI_FPCLK_DIV64, _SPI_MASTER | _SPI_8_BIT |
                   _SPI_CLK_IDLE_LOW | _SPI_FIRST_CLK_EDGE_TRANSITION |
                   _SPI_MSB_FIRST | _SPI_SS_DISABLE | _SPI_SSM_ENABLE | _SPI_SSI_1,
                   &_GPIO_MODULE_SPI3_PC10_11_12);

Mmc_Fat_QuickFormat("mikroE");

// reinitialize spi at higher speed
SPI3_Init_Advanced(_SPI_FPCLK_DIV2, _SPI_MASTER | _SPI_8_BIT |
                   _SPI_CLK_IDLE_LOW | _SPI_FIRST_CLK_EDGE_TRANSITION |
                   _SPI_MSB_FIRST | _SPI_SS_DISABLE | _SPI_SSM_ENABLE | _SPI_SSI_1,
                   &_GPIO_MODULE_SPI3_PC10_11_12); 
Notes

This routine can be used instead or in conjunction with Mmc_Fat_Init routine.

If MMC/SD card already contains a valid boot sector, it will remain unchanged (except volume label field) and only FAT and ROOT tables will be erased. Also, the new volume label will be set.

Mmc_Fat_Assign

Prototype

unsigned int Mmc_Fat_Assign(char *filename, char file_cre_attr);

Description

Assigns file for file operations (read, write, delete...). All subsequent file operations will be applied on an assigned file.

Parameters
  • filename: name of the file that should be assigned for file operations. File name should be in DOS 8.3 (file_name.extension) format. The file name and extension will be automatically padded with spaces by the library if they have less than length required (i.e. "mikro.tx" -> "mikro .tx "), so the user does not have to take care of that. The file name and extension are case insensitive. The library will convert them to proper case automatically, so the user does not have to take care of that.
  • file_cre_attr: file creation and attributes flags. Each bit corresponds to the appropriate file attribute:
  • Bit Mask Description
    0 0x01 Read Only
    1 0x02 Hidden
    2 0x04 System
    3 0x08 Volume Label
    4 0x10 Subdirectory
    5 0x20 Archive
    6 0x40 Device (internal use only, never found on disk)
    7 0x80 File creation flag. If file does not exist and this flag is set, a new file with specified name will be created.

Returns
  • 2 - if there are no more free file handlers, currently opened file is closed in order to free space.
  • 1 - if file already exists or file does not exist but a new file is created.
  • 0 - if file does not exist and no new file is created.
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);
Notes

Long File Names (LFN) are not supported.

Mmc_Fat_Reset

Prototype

void Mmc_Fat_Reset(unsigned long *size);

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
  • size: buffer to store file size to. After file has been opened for reading, its size is returned through this parameter.
Returns

Nothing.

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);
Notes

None.

Mmc_Fat_Read

Prototype

void Mmc_Fat_Read(unsigned short *bdata);

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
  • bdata: buffer to store read byte to. Upon this function execution read byte is returned through this parameter.
Returns

Nothing.

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);
Notes

None.

Mmc_Fat_Rewrite

Prototype

void Mmc_Fat_Rewrite();

Description

Opens the currently assigned file for writing. If the file is not empty its content will be erased.

Parameters

None.

Returns

Nothing.

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();
Notes

None.

Mmc_Fat_Append

Prototype

void Mmc_Fat_Append();

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 write operation will start from there.

Parameters

None.

Returns

Nothing.

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();
Notes

None.

Mmc_Fat_Delete

Prototype

void Mmc_Fat_Delete();

Description

Deletes currently assigned file from MMC/SD card.

Parameters

None.

Returns

Nothing.

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
Mmc_Fat_Delete();
Notes

None.

Mmc_Fat_Write

Prototype

void Mmc_Fat_Write(char *fdata, unsigned data_len);

Description

Writes requested number of bytes to the currently assigned file opened for writing.

Parameters
  • fdata: data to be written.
  • data_len: number of bytes to be written.
Returns

Nothing.

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
char file_contents[42];
...
Mmc_Fat_Write(file_contents, 42); // write data to the assigned file
Notes

None.

Mmc_Fat_Set_File_Date

Prototype

void Mmc_Fat_Set_File_Date(unsigned int year, unsigned short day,unsigned short hours, unsigned short mins, unsigned short seconds);

Description

Sets the date/time stamp. Any subsequent file write operation will write this stamp to the currently assigned file's time/date attributes.

Parameters
  • year: year attribute. Valid values: 1980-2107
  • month: month attribute. Valid values: 1-12
  • day: day attribute. Valid values: 1-31
  • hours: hours attribute. Valid values: 0-23
  • mins: minutes attribute. Valid values: 0-59
  • seconds: seconds attribute. Valid values: 0-59
Returns

Nothing.

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);
Notes

None.

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);

Description

Reads time/date attributes of the currently assigned file.

Parameters
  • year: buffer to store year attribute to. Upon function execution year attribute is returned through this parameter.
  • month: buffer to store month attribute to. Upon function execution month attribute is returned through this parameter.
  • day: buffer to store day attribute to. Upon function execution day attribute is returned through this parameter.
  • hours: buffer to store hours attribute to. Upon function execution hours attribute is returned through this parameter.
  • mins: buffer to store minutes attribute to. Upon function execution minutes attribute is returned through this parameter.
Returns

Nothing.

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);
Notes

None.

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);

Description

Retrieves the last modification date/time for the currently selected file. Seconds are not being retrieved since they are written in 2-sec increments.

Parameters
  • year: buffer to store year attribute to. Upon function execution year attribute is returned through this parameter.
  • month: buffer to store month attribute to. Upon function execution month attribute is returned through this parameter.
  • day: buffer to store day attribute to. Upon function execution day attribute is returned through this parameter.
  • hours: buffer to store hours attribute to. Upon function execution hours attribute is returned through this parameter.
  • mins: buffer to store minutes attribute to. Upon function execution minutes attribute is returned through this parameter.
Returns

Nothing.

Requires

The file must be 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();

Description

This function reads size of the currently assigned file in bytes.

Parameters

None.

Returns

This function returns size of active 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
unsigned long my_file_size;
...
my_file_size = Mmc_Fat_Get_File_Size();
Notes

None.

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);

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. It's main purpose in this 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
  • sectors_cnt: number of consecutive sectors that user wants the swap file to have.
  • filename: name of the file that should be assigned for file operations. File name should be in DOS 8.3 (file_name.extension) format. The file name and extension will be automatically padded with spaces by the library if they have less than length required (i.e. "mikro.tx" -> "mikro .tx "), so the user does no have to take care of that. The file name and extension are case insensitive. The library will convert them to proper case automatically, so the user does not have to take care of that.

    Also, in order to keep backward compatibility with the first version of this library, file names can be entered as UPPERCASE string of 11 bytes in length with no dot character between file name and extension (i.e. "MIKROELETXT" -> MIKROELE.TXT). In this case last 3 characters of the string are considered to be file extension.

  • file_attr: file creation and attributes flags. Each bit corresponds to the appropriate file attribute:
  • Bit Mask Description
    0 0x01 Read Only
    1 0x02 Hidden
    2 0x04 System
    3 0x08 Volume Label
    4 0x10 Subdirectory
    5 0x20 Archive
    6 0x40 Device (internal use only, never found on disk)
    7 0x80 Not used

Returns

  • Number of the start sector for the newly created swap file, if there was enough free space on the MMC/SD card to create file of required size.
  • 0 - otherwise.

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);
  } 
}
Notes

Long File Names (LFN) are not supported.

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
  • position: desired position on which we want to place the cursor.
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
  • newname: new file name.
Returns
  • 1 - if there are no assigned files
  • 2 - if the new name in invalied
  • 3 - if the file with the new name already exists
  • 4 - if an error occurred during renaming
  • 0 - if renaming was successful
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
  • name: directory name.
  • attrib: directory attribute.
Returns
  • 0 - directory creation was successful
  • 255 - if an error occurred
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")) {  // create DIR_A directory
  ...
}
Notes

None.

Mmc_Fat_RenameDir

Prototype

char Mmc_Fat_RenameDir(unsigned char *oldname, unsigned char *newname);

Description

This function renames a directory.

Parameters
  • oldname: old directory name.
  • newname: new directory name.
Returns
  • 1 - if directory name is invalid
  • 2 - if there is no directry with the old name
  • 3 - if an entry with the new name already exits
  • 4 - if an error occurred during renaming
  • 0 - if renaming was successful
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(unsigned char *name);

Description

This function removes directory entry from current directory.

Parameters
  • name: directory name.
Returns
  • 1 - if directory name is invalid
  • 2 - if directory name is non-existant
  • 3 - if directory is not empty
  • 4 - if an error occurred while writing
  • 0 - if operation was successful
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(unsigned char *name);

Description

This function changes the current directory to name.

Parameters
  • name: directory name.
Returns
  • 0 - if operation was successful
  • 1 - if there is no entry with given directory name
  • 2 - if an entry with the given name is not a directory
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 "." and ".." are also supported.

Mmc_Fat_Exists

Prototype

char Mmc_Fat_Exists(char *name);

Description

This function returns information on file/directory existence.

Parameters
  • name: file/directory name.
Returns
  • 0 - if file/directory doesn't exist
  • 1 - if file/directory exists
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
  • ch: function pointer to a routine which will display contents of the current directory.
Returns
  • 1 - if file nema is invalid
  • 2 - if file already exists
  • 3 - if error occured while writing
  • 0 - if operation was successful
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
  • d: directory entry (valid if return value equals 1), consisted of the following fields :

    typedef struct               
    {                                   
        unsigned char name[13];	 // directory name
        unsigned char attrib;  	 // directory attribute
        unsigned char ctime[6];	 // create time and date
        unsigned char mtime[6];	 // modification time and date
        unsigned long size;    	 // directory size
        unsigned int  first;   	 // directory start cluster
        unsigned long sect;    	 // directory entry sector
        unsigned int  entry;   	 // derectory entry number in the entry sector
    } DIR;                              
    
Returns
  • < 0 - if an error occurred
  • 0 - if no more entries
  • 1 - if a valid entry is returned
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
  • fileHandle: file handle of the file which need to be activated.
Returns
  • 0 - if activation was successful
  • 1 - if file handle is out of scope
  • 2 - if file handle is empty
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
  • fdata: data buffer.
  • n: number of bytes to read.
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 file for manipulation.

Parameters
  • name: file name.
  • mode: file handling mode, FILE_WRITE, FILE_READ or FILE_APPEND.
  • attrib: file creation and attributes flags. Each bit corresponds to the appropriate file attribute :
  • Bit Mask Description
    0 0x01 Read Only
    1 0x02 Hidden
    2 0x04 System
    3 0x08 Volume Label
    4 0x10 Subdirectory
    5 0x20 Archive
    6 0x40 Device (internal use only, never found on disk)
    7 0x80 File creation flag. If file does not exist and this flag is set, a new file with specified name will be created.

Returns
  • < 0 - if an error occurred
  • file handle for opened file otherwise
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
  • 0 - if closing was successful
  • 1 - if there are no assigned files
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
  • -1 - if an error occurred
  • 0 - if end of file was not reached
  • 1 - if end of file was reached
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.

Stellaris

// MMC module connections
sbit Mmc_Chip_Select           at GPIO_PORTA_DATA7_bit;  // for writing to output pin always use latch
sbit Mmc_Chip_Select_Direction at GPIO_PORTA_DIR7_bit;
// eof MMC module connections

const LINE_LEN = 43;
const unsigned long SPI_400kHz = 400000;
const unsigned long SPI_10MHz  = 10000000;

char err_txt[20]             = "FAT16 not found";
char filename[14]            = "MIKRO00x.TXT";          // File names
char file_contents[LINE_LEN] = "XX MMC/SD FAT16 library by Anton Rieckertn";

unsigned long  i, size;
unsigned short loop, loop2;
char           Buffer[512];

// UART0 write text and new line (carriage return + line feed)
void UART0_Write_Line(char *uart_text) {
  UART0_Write_Text(uart_text);
  UART0_Write(13);
  UART0_Write(10);
}

// Creates new file and writes some data to it
void M_Create_New_File() {
  filename[7] = 'A';
  Mmc_Fat_Set_File_Date(2011,1,12,11,9,0);   // Set file date & time info
  Mmc_Fat_Assign(&filename, 0xA0);            // Find existing file or create a new one
  Mmc_Fat_Rewrite();                          // To clear file and start with new data
  for(loop = 1; loop <= 99; loop++) {
    UART0_Write('.');
    file_contents[0] = loop / 10 + 48;
    file_contents[1] = loop % 10 + 48;
    Mmc_Fat_Write(file_contents, LINE_LEN-1); // write data to the assigned file
  }
}

// Creates many new files and writes data to them
void M_Create_Multiple_Files() {
  for(loop2 = 'B'; loop2 <= 'Z'; loop2++) {
    UART0_Write(loop2);                       // signal the progress
    filename[7] = loop2;                      // set filename
    Mmc_Fat_Set_File_Date(2011,1,12,11,9,0);  // Set file date & time info
    Mmc_Fat_Assign(&filename, 0xA0);          // find existing file or create a new one
    Mmc_Fat_Rewrite();                        // To clear file and start with new data
    for(loop = 1; loop <= 44; loop++) {
      file_contents[0] = loop / 10 + 48;
      file_contents[1] = loop % 10 + 48;
      Mmc_Fat_Write(file_contents, LINE_LEN-1);  // write data to the assigned file
    }
  }
}

// Opens an existing file and rewrites it
void M_Open_File_Rewrite() {
  filename[7] = 'C';
  Mmc_Fat_Assign(&filename, 0);
  Mmc_Fat_Rewrite();
  for(loop = 1; loop <= 55; loop++) {
    file_contents[0] = loop / 10 + 48;
    file_contents[1] = loop % 10 + 48;
    Mmc_Fat_Write(file_contents, LINE_LEN-1);    // write data to the assigned file
  }
}

// Opens an existing file and appends data to it
//               (and alters the date/time stamp)
void M_Open_File_Append() {
   filename[7] = 'B';
   Mmc_Fat_Assign(&filename, 0);
   Mmc_Fat_Set_File_Date(2009, 1, 23, 17, 22, 0);
   Mmc_Fat_Append();                                    // Prepare file for append
   Mmc_Fat_Write(" for mikroElektronika 2009n", 27);   // Write data to assigned file
}

// Opens an existing file, reads data from it and puts it to UART
void M_Open_File_Read() {
  char character;

  filename[7] = 'B';
  Mmc_Fat_Assign(&filename, 0);
  Mmc_Fat_Reset(&size);            // To read file, procedure returns size of file
  for (i = 1; i <= size; i++) {
    Mmc_Fat_Read(&character);
    UART0_Write(character);        // Write data to UART
  }
}

// Deletes a file. If file doesn't exist, it will first be created
// and then deleted.
void M_Delete_File() {
  filename[7] = 'F';
  Mmc_Fat_Assign(filename, 0);
  Mmc_Fat_Delete();
}

// Tests whether file exists, and if so sends its creation date
// and file size via UART
void M_Test_File_Exist() {
  unsigned long  fsize;
  unsigned int   year;
  unsigned short month, day, hour, minute;
  unsigned char  outstr[12];
  
  Mmc_Fat_Get_File_Date(&year, &month, &day, &hour, &minute);

  filename[7] = 'B';       //uncomment this line to search for file that DOES exists
//  filename[7] = 'F';       //uncomment this line to search for file that DOES NOT exist
  if (Mmc_Fat_Assign(filename, 0)) {
    //--- file has been found - get its create date
    Mmc_Fat_Get_File_Date(&year, &month, &day, &hour, &minute);
    UART0_Write_Text(" created: ");
    WordToStr(year, outstr);
    UART0_Write_Text(outstr);
    ByteToStr(month, outstr);
    UART0_Write_Text(outstr);
    WordToStr(day, outstr);
    UART0_Write_Text(outstr);
    WordToStr(hour, outstr);
    UART0_Write_Text(outstr);
    WordToStr(minute, outstr);
    UART0_Write_Text(outstr);

    //--- file has been found - get its modified date
    Mmc_Fat_Get_File_Date_Modified(&year, &month, &day, &hour, &minute);
    UART0_Write_Text(" modified: ");
    WordToStr(year, outstr);
    UART0_Write_Text(outstr);
    ByteToStr(month, outstr);
    UART0_Write_Text(outstr);
    WordToStr(day, outstr);
    UART0_Write_Text(outstr);
    WordToStr(hour, outstr);
    UART0_Write_Text(outstr);
    WordToStr(minute, outstr);
    UART0_Write_Text(outstr);

    //--- get file size
    fsize = Mmc_Fat_Get_File_Size();
    LongToStr((signed long)fsize, outstr);
    UART0_Write_Line(outstr);
  }
  else {
    //--- file was not found - signal it
    UART0_Write(0x55);
    Delay_ms(1000);
    UART0_Write(0x55);
  }
}


// Tries to create a swap file, whose size will be at least 100
// sectors (see Help for details)
void M_Create_Swap_File() {
  unsigned int i;

  for(i=0; i<512; i++)
    Buffer[i] = i;

  size = Mmc_Fat_Get_Swap_File(5000, "mikroE.txt", 0x20);   // see help on this function for details

  if (size) {
    LongToStr((signed long)size, err_txt);
    UART0_Write_Line(err_txt);

    for(i=0; i<5000; i++) {
      Mmc_Write_Sector(size++, Buffer);
      UART0_Write('.');
    }
  }
}

      unsigned long clk;
//-------------- Main. Uncomment the function(s) to test the desired operation(s)
void main() {
  #define COMPLETE_EXAMPLE               // comment this line to make simpler/smaller example
  // Initialize UART0 module
  UART0_Init(19200);
  Delay_ms(10);
  
  UART0_Write_Line("MCU-Started"); // MCU present report
  
  //--- set up SPI for MMC init (low speed)
  SPI0_Init_Advanced(SPI_400kHz,
                     _SPI_MASTER,
                     _SPI_8_BIT | _SPI_CLK_IDLE_HIGH | _SPI_SECOND_CLK_EDGE_TRANSITION,
                     &_GPIO_MODULE_SPI0_A245);

  Delay_ms(10);

  //--- init the FAT library
  if (!Mmc_Fat_Init()) {
    // reinitialize spi at higher speed
    SPI0_Init_Advanced(SPI_10MHz,
                       _SPI_MASTER,
                       _SPI_8_BIT | _SPI_CLK_IDLE_HIGH | _SPI_SECOND_CLK_EDGE_TRANSITION,
                       &_GPIO_MODULE_SPI0_A245);
    //--- Test start
    UART0_Write_Line("Test Start.");
    //--- Test routines. Uncomment them one-by-one to test certain features
    M_Create_New_File();
    #ifdef COMPLETE_EXAMPLE
      M_Create_Multiple_Files();
      M_Open_File_Rewrite();
      M_Open_File_Append();
      M_Open_File_Read();
      M_Delete_File();
      M_Test_File_Exist();
      M_Create_Swap_File();
    #endif
    UART0_Write_Line("Test End.");

  }
  else {
    UART0_Write_Line(err_txt); // Note: Mmc_Fat_Init tries to initialize a card more than once.
                               //       If card is not present, initialization may last longer (depending on clock speed)
  }

}

STM32

// MMC module connections
sbit Mmc_Chip_Select           at GPIOD_ODR.B3;
// eof MMC module connections

const LINE_LEN = 43;

char err_txt[20]             = "FAT16 not found";
char filename[14]            = "MIKRO00x.TXT";          // File names
char file_contents[LINE_LEN] = "XX MMC/SD FAT16 library by Anton Rieckertn";

unsigned long  i, size;
unsigned short loop, loop2;
char           Buffer[512];

// 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);
}

// Creates new file and writes some data to it
void M_Create_New_File() {
  filename[7] = 'A';
  Mmc_Fat_Set_File_Date(2011,1,12,11,9,0);   // Set file date & time info
  Mmc_Fat_Assign(&filename, 0xA0);            // Find existing file or create a new one
  Mmc_Fat_Rewrite();                          // To clear file and start with new data
  for(loop = 1; loop <= 99; loop++) {
    UART1_Write('.');
    file_contents[0] = loop / 10 + 48;
    file_contents[1] = loop % 10 + 48;
    Mmc_Fat_Write(file_contents, LINE_LEN-1); // write data to the assigned file
  }
}

// Creates many new files and writes data to them
void M_Create_Multiple_Files() {
  for(loop2 = 'B'; loop2 <= 'Z'; loop2++) {
    UART1_Write(loop2);                       // signal the progress
    filename[7] = loop2;                      // set filename
    Mmc_Fat_Set_File_Date(2011,1,12,11,9,0);  // Set file date & time info
    Mmc_Fat_Assign(&filename, 0xA0);          // find existing file or create a new one
    Mmc_Fat_Rewrite();                        // To clear file and start with new data
    for(loop = 1; loop <= 44; loop++) {
      file_contents[0] = loop / 10 + 48;
      file_contents[1] = loop % 10 + 48;
      Mmc_Fat_Write(file_contents, LINE_LEN-1);  // write data to the assigned file
    }
  }
}

// Opens an existing file and rewrites it
void M_Open_File_Rewrite() {
  filename[7] = 'C';
  Mmc_Fat_Assign(&filename, 0);
  Mmc_Fat_Rewrite();
  for(loop = 1; loop <= 55; loop++) {
    file_contents[0] = loop / 10 + 48;
    file_contents[1] = loop % 10 + 48;
    Mmc_Fat_Write(file_contents, LINE_LEN-1);    // write data to the assigned file
  }
}

// Opens an existing file and appends data to it
//               (and alters the date/time stamp)
void M_Open_File_Append() {
   filename[7] = 'B';
   Mmc_Fat_Assign(&filename, 0);
   Mmc_Fat_Set_File_Date(2009, 1, 23, 17, 22, 0);
   Mmc_Fat_Append();                                    // Prepare file for append
   Mmc_Fat_Write(" for mikroElektronika 2009n", 27);   // Write data to assigned file
}

// Opens an existing file, reads data from it and puts it to UART
void M_Open_File_Read() {
  char character;

  filename[7] = 'B';
  Mmc_Fat_Assign(&filename, 0);
  Mmc_Fat_Reset(&size);            // To read file, procedure returns size of file
  for (i = 1; i <= size; i++) {
    Mmc_Fat_Read(&character);
    UART1_Write(character);        // Write data to UART
  }
}

// Deletes a file. If file doesn't exist, it will first be created
// and then deleted.
void M_Delete_File() {
  filename[7] = 'F';
  Mmc_Fat_Assign(filename, 0);
  Mmc_Fat_Delete();
}

// Tests whether file exists, and if so sends its creation date
// and file size via UART
void M_Test_File_Exist() {
  unsigned long  fsize;
  unsigned int   year;
  unsigned short month, day, hour, minute;
  unsigned char  outstr[12];
  
  Mmc_Fat_Get_File_Date(&year, &month, &day, &hour, &minute);

  filename[7] = 'B';       //uncomment this line to search for file that DOES exists
//  filename[7] = 'F';       //uncomment this line to search for file that DOES NOT exist
  if (Mmc_Fat_Assign(filename, 0)) {
    //--- file has been found - get its create date
    Mmc_Fat_Get_File_Date(&year, &month, &day, &hour, &minute);
    UART1_Write_Text(" created: ");
    WordToStr(year, outstr);
    UART1_Write_Text(outstr);
    ByteToStr(month, outstr);
    UART1_Write_Text(outstr);
    WordToStr(day, outstr);
    UART1_Write_Text(outstr);
    WordToStr(hour, outstr);
    UART1_Write_Text(outstr);
    WordToStr(minute, outstr);
    UART1_Write_Text(outstr);

    //--- file has been found - get its modified date
    Mmc_Fat_Get_File_Date_Modified(&year, &month, &day, &hour, &minute);
    UART1_Write_Text(" modified: ");
    WordToStr(year, outstr);
    UART1_Write_Text(outstr);
    ByteToStr(month, outstr);
    UART1_Write_Text(outstr);
    WordToStr(day, outstr);
    UART1_Write_Text(outstr);
    WordToStr(hour, outstr);
    UART1_Write_Text(outstr);
    WordToStr(minute, outstr);
    UART1_Write_Text(outstr);

    //--- get file size
    fsize = Mmc_Fat_Get_File_Size();
    LongToStr((signed long)fsize, outstr);
    UART1_Write_Line(outstr);
  }
  else {
    //--- file was not found - signal it
    UART1_Write(0x55);
    Delay_ms(1000);
    UART1_Write(0x55);
  }
}


// Tries to create a swap file, whose size will be at least 100
// sectors (see Help for details)
void M_Create_Swap_File() {
  unsigned int i;

  for(i=0; i<512; i++)
    Buffer[i] = i;

  size = Mmc_Fat_Get_Swap_File(5000, "mikroE.txt", 0x20);   // see help on this function for details

  if (size) {
    LongToStr((signed long)size, err_txt);
    UART1_Write_Line(err_txt);

    for(i=0; i<5000; i++) {
      Mmc_Write_Sector(size++, Buffer);
      UART1_Write('.');
    }
  }
}

      unsigned long clk;
//-------------- Main. Uncomment the function(s) to test the desired operation(s)
void main() {
  #define COMPLETE_EXAMPLE               // comment this line to make simpler/smaller example
  // Initialize UART1 module
  UART1_Init(19200);
  Delay_ms(10);
  
  UART1_Write_Line("MCU-Started"); // MCU present report
  
  //--- set up SPI for MMC init (low speed)
  SPI3_Init_Advanced(_SPI_FPCLK_DIV64, _SPI_MASTER | _SPI_8_BIT |
                     _SPI_CLK_IDLE_LOW | _SPI_FIRST_CLK_EDGE_TRANSITION |
                     _SPI_MSB_FIRST | _SPI_SS_DISABLE | _SPI_SSM_ENABLE | _SPI_SSI_1,
                     &_GPIO_MODULE_SPI3_PC10_11_12);

  Delay_ms(10);

  //--- init the FAT library
  if (!Mmc_Fat_Init()) {
    // reinitialize spi at higher speed
    SPI3_Init_Advanced(_SPI_FPCLK_DIV2, _SPI_MASTER | _SPI_8_BIT |
                       _SPI_CLK_IDLE_LOW | _SPI_FIRST_CLK_EDGE_TRANSITION |
                       _SPI_MSB_FIRST | _SPI_SS_DISABLE | _SPI_SSM_ENABLE | _SPI_SSI_1,
                       &_GPIO_MODULE_SPI3_PC10_11_12);
    //--- Test start
    UART1_Write_Line("Test Start.");
    //--- Test routines. Uncomment them one-by-one to test certain features
    M_Create_New_File();
    #ifdef COMPLETE_EXAMPLE
      M_Create_Multiple_Files();
      M_Open_File_Rewrite();
      M_Open_File_Append();
      M_Open_File_Read();
      M_Delete_File();
      M_Test_File_Exist();
      M_Create_Swap_File();
    #endif
    UART1_Write_Line("Test End.");

  }
  else {
    UART1_Write_Line(err_txt); // Note: Mmc_Fat_Init tries to initialize a card more than once.
                               //       If card is not present, initialization may last longer (depending on clock speed)
  }

}
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code