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.
mikroPascal PRO for PIC 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 :
- 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.
- For MCUs with multiple SPI modules it is possible to initialize all of them and then switch by using the
SPI_Set_Active()function. See the SPI Library functions. - MMC FAT 16 Library works with PIC18 family only.
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
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
External dependencies of MMC Library
| The following variable must be defined in all projects using MMC library: | Description : | Example : |
|---|---|---|
var Mmc_Chip_Select : sbit; sfr; external; |
Chip select pin. | var Mmc_Chip_Select : sbit at RC0_bit; |
var Mmc_Chip_Select_Direction : sbit; sfr; external; |
Direction of the chip select pin. | var Mmc_Chip_Select_Direction : sbit at TRISC0_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 |
function Mmc_Init(): byte; |
|---|---|
| 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 var Mmc_Chip_Select : sbit at RC0_bit; var Mmc_Chip_Select_Direction : sbit at TRISC0_bit; // MMC module connections error = Mmc_Init(); // Init with CS line at RC.0 var i : byte; ... SPI1_Init(); i = Mmc_Init(); |
Mmc_Read_Sector
| Prototype |
function Mmc_Read_Sector(sector: longint; var dbuff: array[512] of byte): byte; |
|---|---|
| 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
var error : byte;
sectorNo : longint;
dataBuffer : array[512] of byte;
...
sectorNo := 510;
error := Mmc_Read_Sector(sectorNo, dataBuffer);
|
Mmc_Write_Sector
| Prototype |
function Mmc_Write_Sector(sector: longint; var data_: array[512] of byte): byte; |
|---|---|
| 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
var error : byte;
sectorNo : longint;
dataBuffer : array[512] of byte;
...
sectorNo := 510;
error := Mmc_Write_Sector(sectorNo, dataBuffer);
|
Mmc_Read_Cid
| Prototype |
function Mmc_Read_Cid(var data_cid: array[16] of byte): byte; |
|---|---|
| Returns |
|
| Description |
The function reads 16-byte CID register. Parameters:
|
| Requires |
MMC/SD card must be initialized. See Mmc_Init. |
| Example |
var error : byte;
dataBuffer : array[16] of byte;
...
error := Mmc_Read_Cid(dataBuffer);
|
Mmc_Read_Csd
| Prototype |
function Mmc_Read_Csd(var data_for_registers: array[16] of byte): byte; |
|---|---|
| Returns |
|
| Description |
The function reads 16-byte CSD register. Parameters:
|
| Requires |
MMC/SD card must be initialized. See Mmc_Init. |
| Example |
var error : word;
data_csd : array[16] of byte;
...
error := Mmc_Read_Csd(data_csd);
|
Mmc_Multi_Read_Start
| Prototype |
function Mmc_Multi_Read_Start(sector : dword) : byte; |
|---|---|
| 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 |
var start_sector : dword; var error : byte; ... error := Mmc_Multi_Read_Start(start_sector); |
| Notes |
None. |
Mmc_Multi_Read_Sector
| Prototype |
procedure Mmc_Multi_Read_Sector(var dbuff : array[512] of byte); |
|---|---|
| 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 |
var ext_buffer : array[512] of byte; ... Mmc_Multi_Read_Sector(ext_buffer); |
| Notes |
None. |
Mmc_Multi_Read_Stop
| Prototype |
function Mmc_Multi_Read_Stop() : byte; |
|---|---|
| Description |
The function stops multi read mode sequence. |
| Parameters |
None. |
| Returns |
|
| Requires |
MMC/SD card must be initialized. See Mmc_Init. |
| Example |
var error : byte; ... error := Mmc_Multi_Read_Stop(); |
| Notes |
None. |
Mmc_Fat_Init
| Prototype |
function Mmc_Fat_Init(): byte; |
|---|---|
| Returns |
|
| Description |
Initializes MMC/SD card, reads MMC/SD FAT16 boot sector and extracts necessary data needed by the library. Note :
MMC/SD card has to be formatted to FAT16 file system.
|
| Requires |
Global variables :
The appropriate hardware SPI module must be previously initialized. See the SPI1_Init, SPI1_Init_Advanced routines. |
| Example |
// MMC module connections
var Mmc_Chip_Select : sbit at RC0_bit;
var Mmc_Chip_Select_Direction : sbit at TRISC0_bit;
// MMC module connections
SPI1_Init();
// init the FAT library
if (Mmc_Fat_Init() = 0) then
begin
SPI1_Init();
...
end
|
Mmc_Fat_QuickFormat
| Prototype |
function Mmc_Fat_QuickFormat(var mmc_fat_label : string[11]) : byte; |
|---|---|
| Returns |
|
| Description |
Formats to FAT16 and initializes MMC/SD card. Parameters:
Note :
|
| Requires |
The appropriate hardware SPI module must be previously initialized. |
| Example |
// format and initialize the FAT library
if (Mmc_Fat_QuickFormat('mikroE') = 0) then
begin
...
end;
|
Mmc_Fat_Assign
| Prototype |
function Mmc_Fat_Assign(var filename: array[12] of char; file_cre_attr: byte): byte; |
|||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Returns |
|
|||||||||||||||||||||||||||
| Description |
Assigns file for file operations (read, write, delete...). All subsequent file operations will be applied on an assigned file. Parameters:
Note :
Long File Names (LFN) are not supported.
|
|||||||||||||||||||||||||||
| Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. |
|||||||||||||||||||||||||||
| Example |
// create file with archive attribut if it does not already exist
Mmc_Fat_Assign('MIKRO007.TXT',0xA0);
|
Mmc_Fat_Reset
| Prototype |
procedure Mmc_Fat_Reset(var size: dword); |
|---|---|
| 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 |
var size : dword; ... Mmc_Fat_Reset(size); |
Mmc_Fat_Read
| Prototype |
procedure Mmc_Fat_Read(var bdata: byte); |
|---|---|
| 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 |
var character : byte; ... Mmc_Fat_Read(character); |
Mmc_Fat_Rewrite
| Prototype |
procedure 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 |
procedure 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 |
function Mmc_Fat_Delete() : byte; |
|---|---|
| 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) thenbegin end; |
Mmc_Fat_Write
| Prototype |
procedure Mmc_Fat_Write(var fdata: array[512] of byte; data_len: word); |
|---|---|
| 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 |
var file_contents : array[42] of byte; ... Mmc_Fat_Write(file_contents, 42); // write data to the assigned file |
Mmc_Fat_Set_File_Date
| Prototype |
procedure Mmc_Fat_Set_File_Date(year: word; month: byte; day: byte; hours: byte; mins: byte; seconds: byte); |
|---|---|
| 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 |
Mmc_Fat_Set_File_Date(2005,9,30,17,41,0); |
Mmc_Fat_Get_File_Date
| Prototype |
procedure Mmc_Fat_Get_File_Date(var year: word; var month: byte; var day: byte; var hours: byte; var mins: byte); |
|---|---|
| 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 |
var year : word;
month, day, hours, mins : byte;
...
Mmc_Fat_Get_File_Date(year, month, day, hours, mins);
|
Mmc_Fat_Get_File_Date_Modified
| Prototype |
procedure Mmc_Fat_Get_File_Date_Modified(var year: word; var month: byte; var day: byte; var hours: byte; var mins: byte); |
|---|---|
| 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 |
var year : word;
month, day, hours, mins : byte;
...
Mmc_Fat_Get_File_Date_Modified(year, month, day, hours, mins);
|
Mmc_Fat_Get_File_Size
| Prototype |
function Mmc_Fat_Get_File_Size(): dword; |
|---|---|
| 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 |
var my_file_size : dword; ... my_file_size := Mmc_Fat_Get_File_Size(); |
Mmc_Get_File_Write_Sector
| Prototype |
function Mmc_Get_File_Write_Sector() : dword; |
|---|---|
| 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 |
var sector : dword; ... sector := Mmc_Get_File_Write_Sector(); |
| Notes |
None. |
Mmc_Fat_Get_Swap_File
| Prototype |
function Mmc_Fat_Get_Swap_File(sectors_cnt: dword; var filename : string[12]; file_attr : byte) : dword; |
|||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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:
Note :
Long File Names (LFN) are not supported.
|
|||||||||||||||||||||||||||
| Requires |
MMC/SD card and MMC library must be initialized for file operations. See Mmc_Fat_Init. |
|||||||||||||||||||||||||||
| Example |
//-------------- Try to create a swap file with archive atribute, whose size will be at least 1000 sectors. // If it succeeds, it sends No. of start sector over USART var size : dword; ... size := Mmc_Fat_Get_Swap_File(1000, 'mikroE.txt', 0x20); if (size <> 0) then begin UART1_Write(0xAA); UART1_Write(Lo(size)); UART1_Write(Hi(size)); UART1_Write(Higher(size)); UART1_Write(Highest(size)); UART1_Write(0xAA); end; |
Mmc_Fat_Tell
| Prototype |
function Mmc_Fat_Tell() : dword; |
|---|---|
| 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 |
var position : dword; position := Mmc_Fat_Tell(); |
| Notes |
None. |
Mmc_Fat_Seek
| Prototype |
function Mmc_Fat_Seek(position : dword) : dword; |
|---|---|
| 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 |
var position : dword; 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 |
function Mmc_Fat_Rename(newname : ^char) : char; |
|---|---|
| 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')) then // if rename operation was successful...
begin
...
end;
|
| Notes |
None. |
Mmc_Fat_MakeDir
| Prototype |
function Mmc_Fat_MakeDir(name : ^char; attrib : char) : char; |
|---|---|
| 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')) then
begin // create DIR_A directory
...
end;
|
| Notes |
None. |
Mmc_Fat_RenameDir
| Prototype |
function Mmc_Fat_RenameDir(oldname : ^char; newname : ^char) : char; |
|---|---|
| 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...
begin
...
end;
|
| Notes |
None. |
Mmc_Fat_RemoveDir
| Prototype |
function Mmc_Fat_RemoveDir(name : ^char) : char; |
|---|---|
| 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')) then // if removing operation was successful...
begin
...
end;
|
| Notes |
Recursive removing is not supported, i.e. the directory must be empty before it can be removed. |
Mmc_Fat_ChangeDir
| Prototype |
function Mmc_Fat_ChangeDir(name : ^char) : char; |
|---|---|
| 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')) then
begin
...
end;
// go to parent directory
if (0 = Mmc_Fat_ChangeDir('..')) then
begin
...
end;
// go to root directory
if (0 = Mmc_Fat_ChangeDir('\')) then
begin
...
end;
|
| Notes |
Special directory names like " |
Mmc_Fat_Exists
| Prototype |
function Mmc_Fat_Exists(name : ^char) : char; |
|---|---|
| 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 process was successful...
if (0 = status) then
begin
...
end;
// if an error occurred...
if (255 = status) then
begin
...
end;
|
| Notes |
None. |
Mmc_Fat_Dir
| Prototype |
function Mmc_Fat_Dir(print : ^TProcedure) : byte; |
|---|---|
| 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 |
type TProcedure = procedure(ch : char); // Displaying routine procedure PrintChar(ch : char); begin UART1_Write(ch); end; ... Mmc_Fat_Dir(@PrintChar); |
| Notes |
None. |
Mmc_Fat_ReadDir
| Prototype |
function Mmc_Fat_ReadDir(var d : F16_DIR) : short; |
|---|---|
| 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')) then
begin
...
end;
|
| Notes |
None. |
Mmc_Fat_Activate
| Prototype |
function Mmc_Fat_Activate(fileHandle: short) : byte; |
|---|---|
| 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 |
var fhandle : short;
fhandle := Mmc_Fat_Open('X_FILES.TXT', FILE_READ, 0x01);
if (Mmc_Fat_Activate(fhandle)) = 0) then
begin
end;
|
| Notes |
Use Mmc_Fat_Open function to get file handles. |
Mmc_Fat_ReadN
| Prototype |
function Mmc_Fat_ReadN(fdata: ^byte; n: word): word; |
|---|---|
| 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 |
var data_buffer : array[512] of byte; var no_bytes : word; ... no_bytes := Mmc_Fat_ReadN(@data_buffer, 500); |
| Notes |
None. |
Mmc_Fat_Open
| Prototype |
function Mmc_Fat_Open(var name : array[12] of char; mode, attrib : byte) : short; |
|||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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 |
var fhandle : short;
fhandle := Mmc_Fat_Open('X_FILES.TXT', FILE_READ, 0x01);
|
|||||||||||||||||||||||||||
| Notes |
None. |
Mmc_Fat_Close
| Prototype |
function Mmc_Fat_Close() : byte; |
|---|---|
| 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) then begin end; |
| Notes |
None. |
Mmc_Fat_EOF
| Prototype |
function Mmc_Fat_EOF() : short; |
|---|---|
| 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) then begin end; |
| Notes |
None. |
Library Example
The following example demonstrates various aspects of usage of the Mmc_Fat16 library. These are:
- Creation of new file and writing down to it;
- Opening existing file and re-writing it (writing from start-of-file);
- Opening existing file and appending data to it (writing from end-of-file);
- Opening a file and reading data from it (sending it to UART terminal);
- Creating and modifying several files at once;
- Reading file contents;
- Deleting file(s);
- Creating the swap file (see Help for details);
program MMC_FAT_Test;
var
Mmc_Chip_Select : sbit at LATC0_bit; // for writing to output pin always use latch (PIC18 family)
Mmc_Chip_Select_Direction : sbit at TRISC0_bit;
const LINE_LEN = 43;
var
err_txt : string[20];
file_contents : string[LINE_LEN];
filename : string[14]; // File names
character : byte;
loop, loop2 : byte;
size : longint;
buffer : array[512] of byte;
// UART write text and new line (carriage return + line feed)
procedure UART_Write_Line( var uart_text : string );
begin
UART1_Write_Text(uart_text);
UART1_Write(13);
UART1_Write(10);
end;
//-------------- Creates new file and writes some data to it
procedure M_Create_New_File();
begin
filename[7] := 'A'; // Set filename for single-file tests
Mmc_Fat_Set_File_Date(2005,6,21,10,35,0); // Set file date & time info
Mmc_Fat_Assign(filename, 0xA0); // Will not find file and then create file
Mmc_Fat_Rewrite; // To clear file and start with new data
for loop:=1 to 99 do // We want 5 files on the MMC card
begin
UART1_Write('.');
file_contents[0] := loop div 10 + 48;
file_contents[1] := loop mod 10 + 48;
Mmc_Fat_Write(file_contents, LINE_LEN-1); // write data to the assigned file
end;
end;
//-------------- Creates many new files and writes data to them
procedure M_Create_Multiple_Files();
begin
for loop2 := 'B' to 'Z' do
begin
UART1_Write(loop2); // signal the progress
filename[7] := loop2; // set filename
Mmc_Fat_Set_File_Date(2005,6,21,10,35,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 to 44 do
begin
file_contents[0] := byte(loop div 10 + 48);
file_contents[1] := byte(loop mod 10 + 48);
Mmc_Fat_Write(file_contents, LINE_LEN-1); // write data to the assigned file
end;
end;
end;
//-------------- Opens an existing file and rewrites it
procedure M_Open_File_Rewrite();
begin
filename[7] := 'C'; // Set filename for single-file tests
Mmc_Fat_Assign(filename, 0);
Mmc_Fat_Rewrite;
for loop := 1 to 55 do
begin
file_contents[0] := byte(loop div 10 + 48);
file_contents[1] := byte(loop mod 10 + 48);
Mmc_Fat_Write(file_contents, 42); // write data to the assigned file
end;
end;
//-------------- Opens an existing file and appends data to it
// (and alters the date/time stamp)
procedure M_Open_File_Append();
begin
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
file_contents := ' for mikroElektronika 2010'; // Prepare file for append
file_contents[26] := 13; // CR
file_contents[27] := 10; // LF
Mmc_Fat_Write(file_contents, 27); // Write data to assigned file
end;
//-------------- Opens an existing file, reads data from it and puts it to USART
procedure M_Open_File_Read();
begin
filename[7] := 'B';
Mmc_Fat_Assign(filename, 0);
Mmc_Fat_Reset(size); // To read file, procedure returns size of file
while size > 0 do
begin
Mmc_Fat_Read(character);
UART1_Write(character); // Write data to UART
Dec(size);
end;
end;
//-------------- Deletes a file. If file doesn't exist, it will first be created
// and then deleted.
procedure M_Delete_File();
begin
filename[7] := 'F';
Mmc_Fat_Assign(filename, 0);
Mmc_Fat_Delete;
end;
//-------------- Tests whether file exists, and if so sends its creation date
// and file size via USART
procedure M_Test_File_Exist;
var
fsize: longint;
year: word;
month, day, hour, minute: byte;
outstr: array[12] of char;
begin
filename[7] := 'B';
if Mmc_Fat_Assign(filename, 0) <> 0 then begin
//--- file has been found - get its 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;
LongIntToStr(fsize, outstr);
UART_Write_Line(outstr);
end
else
begin
//--- file was not found - signal it
UART1_Write(0x55);
Delay_ms(1000);
UART1_Write(0x55);
end;
end;
//-------------- Tries to create a swap file, whose size will be at least 100
// sectors (see Help for details)
procedure M_Create_Swap_File() ;
var i : word;
begin
for i:=0 to 511 do
Buffer[i] := i;
size := Mmc_Fat_Get_Swap_File(5000, 'mikroE.txt', 0x20); // see help on this function for details
if (size <> 0) then
begin
LongIntToStr(size, err_txt);
UART_Write_Line(err_txt);
for i:=0 to 4999 do
begin
Mmc_Write_Sector(size, Buffer);
Inc(size);
UART1_Write('.');
end;
end;
end;
//-------------- Main. Uncomment the function(s) to test the desired operation(s)
begin
err_txt := 'FAT16 not found';
file_contents := 'XX MMC/SD FAT16 library by Anton Rieckert#';
file_contents[41] := 10; // newline
filename := 'MIKRO00xTXT';
ADCON1 := ADCON1 or 0x0F; // Configure AN pins as digital
CMCON := CMCON or 7; // Turn off comparators
Delay_ms(2000);
// Initialize UART1 module and set pointer(s) to UART1 functions
UART1_Init(19200);
Delay_ms(10);
UART_Write_Line('PIC-Started'); // PIC present report
// Initialize SPI1 module
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
// use fat16 quick format instead of init routine if a formatting is needed
if Mmc_Fat_Init() = 0 then
begin
PORTC := 0xF0;
// reinitialize spi at higher speed
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
//--- signal start-of-test
//--- test functions
M_Create_New_File();
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();
//--- signal end-of-test
UART_Write_Line('Test End.');
end
else
begin
UART_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)
end;
end.
HW Connection

Pin diagram of MMC memory card
What do you think about this topic ? Send us feedback!




