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

Discussion in 'Programmer's Corner' started by curiousaboutcircuits, Apr 14, 2010.

  1. curiousaboutcircuits

    Thread Starter New Member

    Mar 3, 2010
    13
    0
    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:

    Code ( (Unknown Language)):
    1.  
    2. #include <18F4550.h>
    3. #device PASS_STRINGS = IN_RAM
    4. #fuses NOWDT, HS, NOPROTECT
    5. #use delay(clock=25000000)
    6.  
    7. #use rs232(baud=2400, parity=N, invert, brgh1ok, xmit=PIN_D0, stream=LCD, FORCE_SW)
    8. #use rs232(baud=2400, parity=N, brgh1ok, rcv=PIN_C7, stream=RFID)
    9.  
    10. #include <stdlib.h> // for atoi32
    11.  
    12. //media library, a compatable media library is required for FAT.
    13. #use fast_io(c)
    14. #define MMCSD_PIN_SCL     PIN_B1 //o
    15. #define MMCSD_PIN_SDI     PIN_B0 //i
    16. #define MMCSD_PIN_SDO     PIN_C7 //o
    17. #define MMCSD_PIN_SELECT  PIN_A0 //o
    18. #include <mmcsd.c>
    19.  
    20. //FAT library.
    21. #include <fat.h>
    22.  
    23. /*
    24. Summary: Creates a file.
    25. Param: The full path of the file to create.
    26. Returns: None.
    27. Example Usage: \> make "Log.txt"
    28. */
    29. void MakeFile(char *fileName)
    30. {
    31.    fprintf(LCD, "\r\nMaking file '%s': ", fileName);
    32.    if(mk_file(fileName) != GOODEC)
    33.    {
    34.       printf("Error creating file");
    35.       return;
    36.    }
    37.    fprintf(LCD, "OK");
    38. }
    39.  
    40. /*
    41. Summary: Formats the media to a specified size.
    42. Param: The size of the media, in kB, in string form.
    43. Returns: None.
    44. Example Usage: /> format 524288 (format a 512MB card)
    45. */
    46. void FormatMedia(char *mediaSize)
    47. {
    48.    int32 size;
    49.    
    50.    size = atoi32(mediaSize);
    51.    
    52.    printf("\r\nFormatting media (size=%LU): ", size);
    53.  
    54.    if(format(size) != GOODEC)
    55.    {
    56.       printf("Error formatting media");
    57.       return;
    58.    }
    59.    printf("OK");
    60. }
    61.  
    62. /*
    63. Summary: Prints either all of or the last 80 characters in a file.
    64. Param: The full path of the file to print off.
    65. Param: If true, this function will print off the last 80 characters in the file.
    66.        If false, this funciton will print off the entire file.
    67. Returns: None.
    68. Example Usage: /> cat "Logs.txt" (this will display the entire file)
    69. Example Usage: /> tail "Logs.txt" (this will display the last 80 characters in the file)
    70. */
    71. void PrintFile(char *fileName, int1 startFromEnd)
    72. {
    73.    FILE stream;
    74.  
    75.    if(fatopen(fileName, "r", &stream) != GOODEC)
    76.    {
    77.       printf("\r\nError opening file");
    78.       return;
    79.    }
    80.  
    81.    printf("\r\n");
    82.  
    83.    if(startFromEnd)
    84.       fatseek(&stream, 80, SEEK_END);
    85.  
    86.    fatprintf(&stream);
    87.    fatclose(&stream);
    88. }
    89. /*
    90. Summary: Append a string to a file.
    91. Param: The full path of the file to append to.
    92. Param: A pointer to a string to append to the file.
    93. Returns: None.
    94. Example Usage: \> append "Log.txt" "This will be appended to the end of Log.txt"
    95. Note: A "\r\n" will be appended after the appendString.
    96. */
    97. void AppendFile(char *fileName, char *appendString)
    98. {
    99.    FILE stream;
    100.    printf("\r\nAppending '%s' to '%s': ", appendString, fileName);
    101.    if(fatopen(fileName, "a", &stream) != GOODEC)
    102.    {
    103.       printf("Error opening file");
    104.       return;
    105.    }
    106.    
    107.    fatputs(appendString, &stream);
    108.    fatputs("\r\n", &stream);
    109.  
    110.    if(fatclose(&stream) != GOODEC)
    111.    {
    112.       printf("Error closing file");
    113.       return;
    114.    }
    115.    printf("OK");
    116. }
    117. /*
    118. Summary: Deletes a file.
    119. Param: The full path of the file to delete.
    120. Returns: None.
    121. */
    122. void DeleteFile(char *fileName)
    123. {
    124.    printf("\r\nDeleting '%s': ", fileName);
    125.    if(rm_file(fileName) != GOODEC)
    126.    {
    127.       printf("Error deleting file");
    128.       return;
    129.    }
    130.    printf("OK");
    131. }
    132.  
    133. //////////////////////////////////////////////////////////////////////////////
    134. //////////////////////////////////////////////////////////////////////////////
    135.  
    136. void main()
    137. {
    138.  
    139.    char opt_buffer[255];
    140.    
    141.    
    142.     strcpy(opt_buffer, "test.txt");
    143.    
    144.     fat_init();
    145.     MakeFile(opt_buffer);    
    146.     AppendFile(opt_buffer)
    147. }
    148.  
    149.  

    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: Apr 14, 2010
  2. bertus

    Administrator

    Apr 5, 2008
    15,646
    2,345
    Helli,

    What fat format did you use?
    There are fat and fat32.

    Bertus
     
  3. curiousaboutcircuits

    Thread Starter New Member

    Mar 3, 2010
    13
    0
    Hi Bertus,

    I am using FAT32
     
  4. curiousaboutcircuits

    Thread Starter New Member

    Mar 3, 2010
    13
    0
    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().
     
Loading...