Great help here, so I am trying again. PIC18 with SD Card FAT

Thread Starter

curiousaboutcircuits

Joined Mar 3, 2010
13
Hello Everyone,

I made a post here before about SD Card and PIC integration and received a lot of good advice, so I am posting again. I am using the CCS C compiler, and I am using the fat.h and mmcsd.c libraries to write the following code:

Rich (BB code):
#include <18F4550.h>
#device PASS_STRINGS = IN_RAM
#fuses NOWDT, HS, NOPROTECT
#use delay(clock=25000000)

#use rs232(baud=2400, parity=N, invert, brgh1ok, xmit=PIN_D0, stream=LCD, FORCE_SW)
#use rs232(baud=2400, parity=N, brgh1ok, rcv=PIN_C7, stream=RFID)

#include <stdlib.h> // for atoi32

//media library, a compatable media library is required for FAT.
#use fast_io(c)
#define MMCSD_PIN_SCL     PIN_B1 //o
#define MMCSD_PIN_SDI     PIN_B0 //i
#define MMCSD_PIN_SDO     PIN_C7 //o
#define MMCSD_PIN_SELECT  PIN_A0 //o
#include <mmcsd.c>

//FAT library.
#include <fat.h>

/*
Summary: Creates a file.
Param: The full path of the file to create.
Returns: None.
Example Usage: \> make "Log.txt"
*/
void MakeFile(char *fileName)
{
   fprintf(LCD, "\r\nMaking file '%s': ", fileName);
   if(mk_file(fileName) != GOODEC)
   {
      printf("Error creating file");
      return;
   }
   fprintf(LCD, "OK");
}

/*
Summary: Formats the media to a specified size.
Param: The size of the media, in kB, in string form.
Returns: None.
Example Usage: /> format 524288 (format a 512MB card)
*/
void FormatMedia(char *mediaSize)
{
   int32 size;
   
   size = atoi32(mediaSize);
   
   printf("\r\nFormatting media (size=%LU): ", size);
  
   if(format(size) != GOODEC)
   {
      printf("Error formatting media");
      return;
   }
   printf("OK");
}

/*
Summary: Prints either all of or the last 80 characters in a file.
Param: The full path of the file to print off.
Param: If true, this function will print off the last 80 characters in the file.
       If false, this funciton will print off the entire file.
Returns: None.
Example Usage: /> cat "Logs.txt" (this will display the entire file)
Example Usage: /> tail "Logs.txt" (this will display the last 80 characters in the file)
*/
void PrintFile(char *fileName, int1 startFromEnd)
{
   FILE stream;

   if(fatopen(fileName, "r", &stream) != GOODEC)
   {
      printf("\r\nError opening file");
      return;
   }

   printf("\r\n");

   if(startFromEnd)
      fatseek(&stream, 80, SEEK_END);

   fatprintf(&stream);
   fatclose(&stream);
}
/*
Summary: Append a string to a file.
Param: The full path of the file to append to.
Param: A pointer to a string to append to the file.
Returns: None.
Example Usage: \> append "Log.txt" "This will be appended to the end of Log.txt"
Note: A "\r\n" will be appended after the appendString.
*/
void AppendFile(char *fileName, char *appendString)
{
   FILE stream;
   printf("\r\nAppending '%s' to '%s': ", appendString, fileName);
   if(fatopen(fileName, "a", &stream) != GOODEC)
   {
      printf("Error opening file");
      return;
   }
   
   fatputs(appendString, &stream);
   fatputs("\r\n", &stream);

   if(fatclose(&stream) != GOODEC)
   {
      printf("Error closing file");
      return;
   }
   printf("OK");
}
/*
Summary: Deletes a file.
Param: The full path of the file to delete.
Returns: None.
*/
void DeleteFile(char *fileName)
{
   printf("\r\nDeleting '%s': ", fileName);
   if(rm_file(fileName) != GOODEC)
   {
      printf("Error deleting file");
      return;
   }
   printf("OK");
}

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

void main()
{

   char opt_buffer[255];
   
    
    strcpy(opt_buffer, "test.txt");
   
    fat_init();
    MakeFile(opt_buffer);    
    AppendFile(opt_buffer)
}

I've already formatted the 2GB SD Card, and I am thinking that this should create a file named test.txt on the card for me to view in PC. However, it's not working, and I just can't see why. Anybody have any experience with this? Thanks!
 
Last edited by a moderator:

Thread Starter

curiousaboutcircuits

Joined Mar 3, 2010
13
by the way, the fat.c has...

/*
signed int mk_file(char fname[])
Summary: Creates a file.
Param: The full path of the file to create.
Returns: GOODEC if everything went okay, EOF if there was a problem.
Note: This function will not create directories if parent directories don't exist.
Note: fname must be in the form of /filename.fil for a file in the root directory
/Directory/filename.fil for a file in a subdirectory of root
/Directory/Subdirectory/filename.fil and so on...
*/
signed int mk_file(char fname[])
{
char
filename[MAX_FILE_NAME_LENGTH], // the file name we're trying to make
mode[] = "r"; // reading is the safest mode to work in

int
buf, // buffer to hold values
entire_entry[0x20],// entire first entry
filename_pos = 0, // the current parse position of the file name we're trying to create
fname_pos; // the current parse position of the input the the function

int32 i; // pointer to memory

FILE stream; // the stream that we'll be working with

// attempt to open up to the directory
if(fatopen(fname, mode, &stream) == GOODEC)
return EOF; // we shouldn't get an GOODEC back from fatopen()

// check to see if the file is already there.
if(!(stream.Flags & File_Not_Found))
return EOF;

// make a file name
fname_pos = strrchr(fname, '/') - fname + 1;
while((fname[fname_pos] != '\0') && (filename_pos < MAX_FILE_NAME_LENGTH))
{
filename[filename_pos] = fname[fname_pos];
fname_pos += 1;
filename_pos += 1;
}
filename[filename_pos] = '\0';

// write the name
if(set_file_name(stream.Start_Addr, &i, filename) == EOF)
return EOF;

// throw in some values in the file's first entry
for(buf = 0; buf < 0x20; buf += 1)
entire_entry[buf] = 0;

// this is a file
entire_entry[0x0B] = 0x20;

// read what set_file_name gave us for the short name
if(mmcsd_read_data(i, 11, entire_entry) != GOODEC)
return EOF;

// write the entry
if(mmcsd_write_data(i, 0x20, entire_entry) != GOODEC)
return EOF;

return GOODEC;
}


this for mk_file().
 
Top