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.
FAT32 library for MMC/SD cards
- dejan.odabasic
- mikroElektronika team
- Posts: 2649
- Joined: 30 Apr 2012 14:20
Re: FAT32 library for MMC/SD cards
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.
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.
Re: FAT32 library for MMC/SD cards
Thanks. The problem seems to be fixed.
Re: FAT32 library for MMC/SD cards
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
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
Re: FAT32 library for MMC/SD cards
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:
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;
}
}
-
- Posts: 20
- Joined: 09 Nov 2017 09:13
- Location: Russia, Ufa
Re: FAT32 library for MMC/SD cards
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".
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...
-
- Posts: 20
- Joined: 09 Nov 2017 09:13
- Location: Russia, Ufa
Re: FAT32 library for MMC/SD cards
Problem complete. One more question - Fast Format SD-card is possible?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".
Ignoramus et ignorabimus...
-
- Posts: 20
- Joined: 09 Nov 2017 09:13
- Location: Russia, Ufa
Re: FAT32 library for MMC/SD cards
FAT32_DeleteRec("foldername") don't work - folder is not deleted. Why?
Ignoramus et ignorabimus...
Re: FAT32 library for MMC/SD cards
Hi,
Regards,
Filip
No, only full format is supported.One more question - Fast Format SD-card is possible?
Please, can you tell me how did you use the function exactly ?FAT32_DeleteRec("foldername") don't work - folder is not deleted. Why?
Regards,
Filip
-
- Posts: 20
- Joined: 09 Nov 2017 09:13
- Location: Russia, Ufa
Re: FAT32 library for MMC/SD cards
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.
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...