FAT32 library for MMC/SD cards

Discuss with MikroElektronika software developers about current library development.
Author
Message
Gilbert N
Posts: 2
Joined: 30 Oct 2013 18:24

Re: FAT32 library for MMC/SD cards

#76 Post by Gilbert N » 05 Nov 2013 19:24

Hello all, I am new here and this is my first time developing on a PIC microcontroller. I’ve been stuck on this problem for a couple of weeks and no luck finding a solution. I was hoping I can get some help here.

So I am using the PIC18F27J13 and FAT32 library from Libstock to write at most 1500 bytes of data to a 4GB micro SD card. The idea is to create a new file of 1500 bytes every minute for as long as it’s on. Right now I have it writing a file every minute and storing them in directories separated by date.

It seems to be working great, but I have this problem that files within the directories are disappearing. When I inspected the SD card memory space was used up, so I think the files didn’t just disappear, but got corrupted somehow. It is difficult to determine when it happens, since sometimes it could take minutes sometimes hours.

I’m hoping someone can help me out. Any insight will help.

User avatar
dejan.odabasic
mikroElektronika team
Posts: 2649
Joined: 30 Apr 2012 14:20

Re: FAT32 library for MMC/SD cards

#77 Post by dejan.odabasic » 06 Nov 2013 16:56

Hello,

Problem which you explained is usually caused by not closing the file after writing.
Make sure that you close file properly after writing the data, like shown in example project.

If you don't solve your problem, please attach your project or post code snippet of your project.

Best regards.

Gilbert N
Posts: 2
Joined: 30 Oct 2013 18:24

Re: FAT32 library for MMC/SD cards

#78 Post by Gilbert N » 11 Nov 2013 19:49

Thanks. The problem seems to be fixed.

Stiive
Posts: 7
Joined: 22 Feb 2014 05:33

Re: FAT32 library for MMC/SD cards

#79 Post by Stiive » 18 Apr 2014 02:14

Hi,
I'm having a problem with the FAT32 library on the Mikromedia plus STM32.

I am datalogging using swap files.
Once I fill up the amount of sectors available in the swap file, I create a new swap file and continue logging to the new file.

Everything works well until I have created about ~125 logs, and then the system freezes on the FAT32_MakeSwap command and the SDcard becomes corrupted.

I am using a 8Gb SD card, there is plenty of room remaining on the SD card when this happens. As a test, I have changed the amount of sectors per swap file, so the total amount of data written is very different - yet is still freezes somewhere between 123 and 126 swap log files created. I guess this removes the possibility of a bad sector causing the freeze.

When I remove the SDCard while its frozen - it then exits the MakeSwap function and returns the error -36 (file is not opened). (FYI - I open the file after the making the swap file otherwise my code doesn't work at all). Once I have removed the card after it had frozen, the data is generally corrupted and I can't see any of the swap files or they are all blank. I have assigned all my values to uint16/32, so should not be caused by an overflow, unless it is something internal to the MakeSwap function (signed short?). Sometimes, If i remove the SD card before the error, and then reset, it will make it to ~255logs then freeze. Weird.

Any thoughts?
If this cause of this problem is unknown, it would be great if this function could include a timeout?

Cheers

feuerwolf
Posts: 95
Joined: 09 Nov 2012 16:00
Location: Switzerland

Re: FAT32 library for MMC/SD cards

#80 Post by feuerwolf » 10 Nov 2014 18:19

Error -12 == invalid cluster number!

Hallo All,
i programmed a data logger which is saving GPS Coordinates on a SD Card using your FAT32 library.
Since a while I randomly got errors on windows that the files on the sd card are corrupted.
Free translated windwos error is: bad linkage in lost chain at cluster.

Trying to debug the error I used your int8 FAT32_GetError(void); function from Fat32 Lib.
After days I found two errors I can^t explain why they occured.
First one was -37 which means END OF File Reached.
But the second Error was -12 which says invalid cluster number!

I tried different SD Cards but the error occurred anyway, so I am pretty sure it is an error in my program.

I think maybe a file is not closed correctly after writing process. Could this be? Because the program used to work fine and works good for hours until the error occurs and a file is corrupted.
Is there a function which shows me any opened files? Even Files which wasn’t opened/closed correctly in other functions?
Could somebody please explain what Error -12 means? How could i fix it?

Thank you very much

here is the function in which the error occured:

Code: Select all

char write_into_File(char *filename_intern, char *message){

  char stn_out[100];
  short err, err2, errX;
  short fhandle;
  unsigned int seconds_uint;
  __Time fileCreationTime_intern;
  __Time modificationTime_intern;

  err = FAT32_Init();

  errX = FAT32_Exists(filename_intern);
  if(errX == 1){ // file was FOUND
    err2 = FAT32_GetCTime(filename_intern,&fileCreationTime_intern);
    seconds_uint  = (unsigned int) (  fabs ((double)( fileCreationTime_intern.Year - CurrentTime.Year   )) * 86400 * 30 * 12);
    seconds_uint += (unsigned int) (  fabs ((double)( fileCreationTime_intern.Month - CurrentTime.Month )) * 86400 * 30);
    seconds_uint += (unsigned int) ( fabs ((double)( fileCreationTime_intern.Day - CurrentTime.Day   )) * 24 * 3600 );
    seconds_uint += (unsigned int) ( fabs ((double)( fileCreationTime_intern.Hour - CurrentTime.Hour )) * 3600 );
    seconds_uint += (unsigned int) ( fabs ((double)( fileCreationTime_intern.Minute - CurrentTime.Minute )) * 60 );
    seconds_uint += (unsigned int) ( fabs ((double)( fileCreationTime_intern.Second - CurrentTime.Second ))  );
    err2 = FAT32_InCTime(seconds_uint);
  }
  if(errX == 0){ // file was NOT found
     err2 = FAT32_SetTime(&CurrentTime);
  }
  
  if (err == 0)
  {
    fhandle = FAT32_Open(filename_intern, FILE_APPEND);
    err2 = FAT32_GetError();

    if (fhandle < 0)         //    ist eins wenn ein Fehler passiert
       {
       UART2_Write_Text("\x0DFile open error\x0D");
       LED_monitor_STATUS_flag_int = 3;
       return 0;
       }
    else
      {
      err = FAT32_Write(fhandle, message, (unsigned int)strlen(message));  
      err2 = FAT32_Close(fhandle); 
      if(err >= 0){
          LED_monitor_STATUS_flag_int = 1;
          err = FAT32_GetError();
          if(err < 0){
              sprinti(stn_out, "\x0DFAT32_GetError(void) - FAT32 ERROR in write_into_File detected!!! ERROR: %d  ", (int)err );  // Stunden
              UART2_Write_Text(stn_out);
              UART2_Write(CR);
              fhandle = FAT32_Open("ERROR.TXT", FILE_APPEND);
              FAT32_Write(fhandle, stn_out, (unsigned int) strlen(stn_out));   
              FAT32_Close(fhandle);
          }
          err = FAT32_GetMTime(filename_intern,&modificationTime_intern);
          err = FAT32_GetCTime(filename_intern,&fileCreationTime_intern);
          return 1;
          }
      else
          {
          UART2_Write_Text("\x0DError wrinting to SD Card!\x0D");
          LED_monitor_STATUS_flag_int = 3;
          return 0;
          }
      }
   }
   else
     {
     UART2_Write_Text("\x0DSD Card Init ERROR\x0D");
     LED_monitor_STATUS_flag_int = 3;
     return 0;
     }
}

Chipmaster
Posts: 20
Joined: 09 Nov 2017 09:13
Location: Russia, Ufa

Re: FAT32 library for MMC/SD cards

#81 Post by Chipmaster » 05 Apr 2018 05:55

MikroC PRO For dsPIC v.7.0.1, FAT32 library.

How to add characters array in to the file?
For example,

unsigned char [] = "MikroElektronika";
fhandle[0] = FAT32_Open("Test.txt", FILE_WRITE);
???..........???

I need the text "MikroElektronika" from characters array was displayed in the file "Text.txt".
Ignoramus et ignorabimus...

Chipmaster
Posts: 20
Joined: 09 Nov 2017 09:13
Location: Russia, Ufa

Re: FAT32 library for MMC/SD cards

#82 Post by Chipmaster » 06 Apr 2018 09:41

Chipmaster wrote:MikroC PRO For dsPIC v.7.0.1, FAT32 library.

How to add characters array in to the file?
For example,

unsigned char [] = "MikroElektronika";
fhandle[0] = FAT32_Open("Test.txt", FILE_WRITE);
???..........???

I need the text "MikroElektronika" from characters array was displayed in the file "Text.txt".
Problem complete. One more question - Fast Format SD-card is possible?
Ignoramus et ignorabimus...

Chipmaster
Posts: 20
Joined: 09 Nov 2017 09:13
Location: Russia, Ufa

Re: FAT32 library for MMC/SD cards

#83 Post by Chipmaster » 06 Apr 2018 10:53

FAT32_DeleteRec("foldername") don't work - folder is not deleted. Why?
Ignoramus et ignorabimus...

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: FAT32 library for MMC/SD cards

#84 Post by filip » 10 Apr 2018 15:18

Hi,
One more question - Fast Format SD-card is possible?
No, only full format is supported.
FAT32_DeleteRec("foldername") don't work - folder is not deleted. Why?
Please, can you tell me how did you use the function exactly ?

Regards,
Filip

Chipmaster
Posts: 20
Joined: 09 Nov 2017 09:13
Location: Russia, Ufa

Re: FAT32 library for MMC/SD cards

#85 Post by Chipmaster » 22 Apr 2018 14:22

MikroC PRO For dsPIC v.7.0.1, FAT32 library.

How can I delete all files and folders from the SD card if I do not know what files and folders are on the card? The FAT32_DeleteRec function deletes only the folders with the name that is defined. The FAT32_Format function is working too long.
Ignoramus et ignorabimus...

Post Reply

Return to “Library Development Discussion”