//ENSURE FILE IS NOT OPEN
if (our_file != 0)
ffs_fclose(our_file);
Secure Digital SD Card Source Code Driver Project
Section 07. Usage Examples
07. Usage Examples
Contents:
Ensure File Is Not Open
Fast Increase And Decrease Of File Size
FFS_FILE *our_file;
BYTE filename[13];
BYTE *b_pointer;
if (!ffs_card_ok)
return;
//----- FAST INCREASE FILE SIZE -----
b_pointer = &filename[0];
*b_pointer++ = 'I';
*b_pointer++ = 'N';
*b_pointer++ = 'C';
*b_pointer++ = 'F';
*b_pointer++ = 'I';
*b_pointer++ = 'L';
*b_pointer++ = 'E';
*b_pointer++ = '1';
*b_pointer++ = '.';
*b_pointer++ = 'T';
*b_pointer++ = 'X';
*b_pointer++ = 'T';
*b_pointer++ = 0x00;
//TRY AND CREATE FILE
our_file = ffs_fopen((const char*)filename, (const char*)"w");
if (our_file != 0)
{
//THE FILE WAS CREATED
//Write Bytes
ffs_fputc((int)'H', our_file);
ffs_fputc((int)'e', our_file);
ffs_fputc((int)'l', our_file);
ffs_fputc((int)'l', our_file);
ffs_fputc((int)'o', our_file);
ffs_fputc((int)' ', our_file);
ffs_fputc((int)'W', our_file);
ffs_fputc((int)'o', our_file);
ffs_fputc((int)'r', our_file);
ffs_fputc((int)'l', our_file);
ffs_fputc((int)'d', our_file);
//CLOSE THE FILE
ffs_fclose(our_file);
//INCREASE THE FILE SIZE
if (ffs_change_file_size((const char*) filename, (DWORD)12345) != 0)
{
//ERROR
}
//The new section of the file will be garbage, but is ready for your application to write over without the driver
//having to constantly having to update the FAT table every time it needs a new cluster
}
//----- FAST DECREASE FILE SIZE -----
//This can be useful if increasing a file size to more than you need so you can quickly reduce it down to the size actually used after writing. This example
//is basic and won't actually change the clusters allocated as the file is small, but it demonstrates how it is used.
b_pointer = &filename[0];
*b_pointer++ = 'D';
*b_pointer++ = 'E';
*b_pointer++ = 'C';
*b_pointer++ = 'F';
*b_pointer++ = 'I';
*b_pointer++ = 'L';
*b_pointer++ = 'E';
*b_pointer++ = '1';
*b_pointer++ = '.';
*b_pointer++ = 'T';
*b_pointer++ = 'X';
*b_pointer++ = 'T';
*b_pointer++ = 0x00;
//TRY AND CREATE FILE
our_file = ffs_fopen((const char*)filename, (const char*)"w");
if (our_file != 0)
{
//THE FILE WAS CREATED
//Write Bytes
ffs_fputc((int)'H', our_file);
ffs_fputc((int)'e', our_file);
ffs_fputc((int)'l', our_file);
ffs_fputc((int)'l', our_file);
ffs_fputc((int)'o', our_file);
ffs_fputc((int)' ', our_file);
ffs_fputc((int)'W', our_file);
ffs_fputc((int)'o', our_file);
ffs_fputc((int)'r', our_file);
ffs_fputc((int)'l', our_file);
ffs_fputc((int)'d', our_file);
//CLOSE THE FILE
ffs_fclose(our_file);
//DECREASE THE FILE SIZE
if (ffs_change_file_size((const char*) filename, (DWORD)10) != 0)
{
//ERROR
}
}
How To Read A File
static FFS_FILE *our_file; //Use static if reading parts of the file on sucessive calls
static DWORD bytes_to_go; //Use static if reading parts of the file on sucessive calls
BYTE filename[13];
BYTE *b_pointer;
BYTE data;
if (!ffs_card_ok)
return;
b_pointer = &filename[0];
*b_pointer++ = 'M';
*b_pointer++ = 'Y';
*b_pointer++ = 'F';
*b_pointer++ = 'I';
*b_pointer++ = 'L';
*b_pointer++ = 'E';
*b_pointer++ = '0';
*b_pointer++ = '1';
*b_pointer++ = '.';
*b_pointer++ = 'T';
*b_pointer++ = 'X';
*b_pointer++ = 'T';
*b_pointer++ = 0x00;
//TRY AND OPEN FILE
our_file = ffs_fopen((const char*)filename, (const char*)"r");
if (our_file != 0)
{
//THE FILE DOES EXIST
//Get the file size
bytes_to_go = our_file->file_size;
//Read each byte
while (bytes_to_go--)
{
data = (BYTE)ffs_fgetc(our_file);
//Check for error
if (ffs_feof(our_file) || ffs_ferror(our_file))
{
//ERROR OR END OF FILE
}
}
//CLOSE THE FILE
ffs_fclose(our_file);
}
How To Read The Contents Of The Root Directory
BYTE start_from_beginning;
BYTE found_file_name[8];
BYTE found_file_extension[3];
BYTE found_file_attribute_byte;
DWORD found_file_size;
DWORD found_file_cluster_number,
BYTE start_from_beginning;
DWORD found_file_directory_entry_sector;
BYTE found_file_directory_entry_within_sector;
BYTE find_next_entry_response;
//----- LIST ALL FILES IN THE DIRECTORY -----
start_from_beginning = 1;
do
{
find_next_entry_response = ffs_read_next_directory_entry(&found_file_name[0], &found_file_extension[0], &found_file_attribute_byte, &found_file_size,
&found_file_cluster_number, start_from_beginning, &found_file_directory_entry_sector, &found_file_directory_entry_within_sector);
start_from_beginning = 0;
if ((find_next_entry_response == 0) || (found_file_name[0] == 0x00)) //If 1st value is 0x00 then entry has never been used (erased entries are 0xe5) so 0x00 is the end of used directory marker). If find_next_entry_response = 0 then reached end of directory
{
//----- NO MORE FILES FOUND - END OF DIRECTORY LISTING -----
break;
}
if (
(found_file_name[0] != 0xe5) && //First byte value of 0xe5 = deleted file
((found_file_attribute_byte & 0x18) == 0) //Ignore directory and volume label entries
)
{
//----- NEXT FILE FOUND -----
//ADD THE FILE DETAILS TO THE PACKET
if (count >= tx_sd_card_list_next_file)
{
// = found_file_name[#];
// =found_file_extension[#];
// = found_file_size;
}
}
} while (found_file_name[0] != 0x00);
How To Write A File
static FFS_FILE *our_file; //Use static if reading parts of the file on sucessive calls
BYTE filename[13];
BYTE *b_pointer;
if (!ffs_card_ok)
return;
b_pointer = &filename[0];
*b_pointer++ = 'M';
*b_pointer++ = 'Y';
*b_pointer++ = 'F';
*b_pointer++ = 'I';
*b_pointer++ = 'L';
*b_pointer++ = 'E';
*b_pointer++ = '0';
*b_pointer++ = '1';
*b_pointer++ = '.';
*b_pointer++ = 'T';
*b_pointer++ = 'X';
*b_pointer++ = 'T';
*b_pointer++ = 0x00;
//TRY AND CREATE FILE
our_file = ffs_fopen((const char*)filename, (const char*)"w");
if (our_file != 0)
{
//THE FILE WAS CREATED
//Write Bytes
ffs_fputc((int)'H', our_file);
ffs_fputc((int)'e', our_file);
ffs_fputc((int)'l', our_file);
ffs_fputc((int)'l', our_file);
ffs_fputc((int)'o', our_file);
ffs_fputc((int)' ', our_file);
ffs_fputc((int)'W', our_file);
ffs_fputc((int)'o', our_file);
ffs_fputc((int)'r', our_file);
ffs_fputc((int)'l', our_file);
ffs_fputc((int)'d', our_file);
//CLOSE THE FILE
ffs_fclose(our_file);
}


