
Secure Digital (SD) is a standard flash memory card based on the earlier Multi Media Card (MMC) standard. Similarly to the CF memory cards, the SD memory cards have a high memory capacity, up to 2GB, and have found the application in the mobile phones, MP3 players, digital cameras, and PDA computers. Access to an SD memory card is realized by using the SPI communication interface.
This example consists of several blocks that demonstrat 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 USART terminal); Creating and modifying several files at once; Reading file contents; Deleting file(s); Creating the swap file (see Help for details);
program Mmc_Fat16_Test;
const
SWAP_FILE_MSG: string[15] = 'Swap file at: ';
var
fat_txt: string[20];
file_contents : string[50];
filename: array[14] of byte; // File names
loop, loop2 : word;
caracter : byte;
i, size : longint;
Buffer : array[512] of byte;
procedure Glob_Init;
begin
fat_txt := 'FAT16 not found';
file_contents := 'XX MMC/SD FAT16 library by Anton Rieckert' + #10 + #0;
filename := 'MIKRO00xTXT'; // File names
end;
//M-M-M--------- Creates new file and writes some data to it
procedure M_Create_New_File;
begin
filename[7] := 'A';
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_Char('.');
file_contents[0] := (loop div 10) + 48;
file_contents[1] := (loop mod 10) + 48;
// Uart1_Write_Text(file_contents);
Mmc_Fat_Write(file_contents, 42); // write data to the assigned file
end;
end;//~
//M-M-M--------- Creates many new files and writes data to them
procedure M_Create_Multiple_Files;
begin
for loop2 := 'B' to 'Z' do
begin
Uart1_Write_Char(loop2); // signal the progress
filename[7] := loop2; // set filename
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] := (loop div 10) + 48;
file_contents[1] := (loop mod 10) + 48;
Mmc_Fat_Write(file_contents, 42); // write data to the assigned file
end;
end;
end;//~
//M-M-M--------- Opens an existing file and rewrites it
procedure M_Open_File_Rewrite;
begin
filename[7] := 'C';
Mmc_Fat_Assign(filename, 0);
Mmc_Fat_Rewrite();
for loop := 1 to 55 do
begin
file_contents[0] := loop div 10 + 64;
file_contents[1] := loop mod 10 + 64;
Mmc_Fat_Write(file_contents, 42); // write data to the assigned file
end;
end;//~
//M-M-M--------- 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(2005,6,21,10,35,0);
Mmc_Fat_Append(); // Prepare file for append
Mmc_Fat_Write(' for mikroElektronika 2005n', 27); // Write data to assigned file
end;//~
//M-M-M--------- 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
for i := 1 to size do
begin
Mmc_Fat_Read(caracter);
Uart1_Write_Char(caracter); // Write data to USART
end;
end;//~
//M-M-M--------- 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;//~
//M-M-M--------- Tests whether file exists, and if so sends its creation date
// and file size via USART
procedure M_Test_File_Exist(fLetter: byte);
var
fsize: longint;
year: word;
month, day, hour, minute: byte;
outstr: array[12] of byte;
begin
filename[7] := fLetter;
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);
WordToStr(year, outstr);
Uart1_Write_Text(outstr);
WordToStr(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(longint(fsize), outstr);
Uart1_Write_Text(outstr);
end
else
begin
//--- file was not found - signal it
Uart1_Write_Char(0x55);
Delay_ms(1000);
Uart1_Write_Char(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, fat_txt);
Uart1_Write_Text(fat_txt);
for i:=0 to 4999 do
begin
Mmc_Write_Sector(size, Buffer);
Inc(size);
Uart1_Write_Char('.');
end;
end;
end;//~
begin
//-------------- Main. Uncomment the function(s) to test the desired operation(s)
Glob_Init;
//--- prepare PORTD for signalling
PORTD := 0;
TRISD := 0;
ADPCFG := 0xFFFF;
//--- set up USART for the file read
Uart1_Init(19200);
U1MODE.10 := 1; // clear the way for the SPI
//--- init the FAT library
Spi1_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, _SPI_PRESCALE_SEC_1, _SPI_PRESCALE_PRI_64,
_SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH, _SPI_ACTIVE_2_IDLE);
//--- (primary prescale := 64:1), secondary prescale := 1:1
// use fat16 quick format instead of init routine if a format is needed
if (Mmc_Fat_Init(PORTB, 8) = 0) then // EasydsPIC3 board
begin
// reinitialize spi at higher speed
Spi1_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, _SPI_PRESCALE_SEC_1, _SPI_PRESCALE_PRI_4,
_SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH, _SPI_ACTIVE_2_IDLE);
//--- Test start
PORTD := 0x0005;
//--- Test routines. Uncomment them one-by-one to test certain features
M_Create_New_File;
M_Create_Multiple_Files;
Uart1_Write_Char(10);
M_Open_File_Rewrite;
M_Open_File_Append;
M_Delete_File;
M_Create_Swap_File;
Uart1_Write_Char(10);
M_Open_File_Read;
Uart1_Write_Char(10);
M_Test_File_Exist('F'); // this file will not exist here
Uart1_Write_Char(10);
M_Test_File_Exist('B'); // this file will exist here
Uart1_Write_Char(10);
end
else
begin
Uart1_Write_Text(fat_txt);
end;
//--- Test termination
PORTD := 0xFFFF;
end.//~!