FAT32 library for MMC/SD cards

Discuss with MikroElektronika software developers about current library development.
Author
Message
LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

Re: FAT32 library for MMC/SD cards

#16 Post by LGR » 13 Feb 2012 16:22

I had one SD card that didn't work with SPI, and it was a very old 16MB card. Any made in the past few years should work, even the generic ones. Mikroelectronika sells them. Or get a good Sandisk or Kingston to be sure.
If you know what you're doing, you're not learning anything.

borris
Posts: 219
Joined: 22 Aug 2011 07:13

Re: FAT32 library for MMC/SD cards

#17 Post by borris » 23 Mar 2012 08:57

Is it possible to have multiple SD cards on the same bus with this library?

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

Re: FAT32 library for MMC/SD cards

#18 Post by LGR » 23 Mar 2012 16:24

You have to declare an enable pin in the initialization. You might be able to reinitialize every time you change cards. You just have to try it. I would bet that it would work.

You certainly should be able to have two separate cards at SPI1 and SPI2.
If you know what you're doing, you're not learning anything.

Berkl
Posts: 124
Joined: 05 May 2009 13:09
Location: Russia
Contact:

Re: FAT32 library for MMC/SD cards

#19 Post by Berkl » 12 Apr 2012 13:35

Hello guys,

There is short FAT32_Dir(void) function for extract file names over some user-defined medium.

I don't need any mediums! I need read files names from my SD card to MCU's RAM. How I can do that?

And please explain, how I can select a file on FAT32 file system as the medium.

Best regards.
___________________________________________
http://www.techno-spectr.ru/en/main-en/

Yaroslavl, Russia

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: FAT32 library for MMC/SD cards

#20 Post by janko.kaljevic » 12 Apr 2012 15:28

Hello,

Please take a look at following topic.
It is for FAT16 but DIR function operates the same way:
http://www.mikroe.com/forum/viewtopic.p ... ir#p183856

Best regards.

bartt
Posts: 156
Joined: 20 Jul 2012 18:52
Location: Left coast

Re: FAT32 library for MMC/SD cards

#21 Post by bartt » 02 Aug 2012 17:35

So it's been several months now and many of us are in need of Fat32 for USB thumbdrives.
Is there any work underway on this?
Thanks in advance..
Bart

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: FAT32 library for MMC/SD cards

#22 Post by janko.kaljevic » 03 Aug 2012 10:08

Hello,

We do have in plans to develop USB stack and provide support for MSD.

But I can not make any promises regarding release dates.

Best regards.

ebeinecke
Posts: 32
Joined: 20 Apr 2010 21:54

Re: FAT32 library for MMC/SD cards

#23 Post by ebeinecke » 05 Aug 2012 00:25

Hi ! I need some help using FAT32 library in libstock.com, I am trying to use the library to constantly acquire data from a stormscope sensor (by serial port 1) save latitude and longitude etc acquired by a second serial port and save the data in a file in a SD Card, my code works fine, but after 10 ~ 15 min of operation the hardware stuck and when I try to see the data I notice that the FAT is corrupted and all the data deleted.
I am using a dspic33fj128mc202

After some research I found that the use of a swap file could solve the problem, but i don´t comprehend the use of the swap file by the help in the library, actually I can´t comprehend the method of re-writing the swap data in another file.

Could someone please help with examples ?


Thanks in advance, sorry for my poor english.

Part of my code (the swap file is configured but not used)

Code: Select all

storm=stormlogger(); //data aquisition 
       
       FAT32_Delete("SWAP.TXT");                 //delete previous swap file
       if (FAT32_MakeSwap("SWAP.TXT", 5000, &swapStartSc) == 0) {  // make swap file
          //for (d=0; d<5000; d++) {                               // to all swap file sectors
          //  FAT32_Dev_Write_Sector(swapStartSc + d, buf);        // write buffer content
          //    FAT32_Dev_Write_Sector(swapStartSc, buf);            // write buffer content
          //}
       }
       Lcd_Cmd(_LCD_CLEAR);
       Lcd_Out(1,1,"Logando");
       status=FAT32_Exists(posi.nomearquivo);
       if (status==0) {           //if the file doesn't exist write a new file
              fhandle = FAT32_Open(posi.nomearquivo, FILE_WRITE);

              FAT32_Write(fhandle, posi.lat1, 16);
              FAT32_Write(fhandle, posi.lon1, 17);                     
              FAT32_Write(fhandle, posi.date1, 14);
              FAT32_Write(fhandle, posi.hora1, 14);
              FAT32_Close(fhandle);
              //i=0;
              //while (fhandle != 0 || i<5) {
              //      FAT32_Close(fhandle);
              //      Delay_ms(10);
              //      i++;
              //}
       }
       i=0;
       while (storm.ret[i]!=0) {
           i++;
       }
       fhandle = FAT32_Open(posi.nomearquivo, FILE_APPEND);
       FAT32_Write(fhandle, storm.ret, i+2);
       FAT32_Close(fhandle);
       Delay_ms(1500);
       Lcd_Cmd(_LCD_CLEAR);                // Clear display
       Lcd_Out(1,1,posi.lat1);       //display latitude
       Lcd_Out(2,1,posi.lon1);      //display longitude

ebeinecke
Posts: 32
Joined: 20 Apr 2010 21:54

Re: FAT32 library for MMC/SD cards

#24 Post by ebeinecke » 05 Aug 2012 15:24

am using dspic33fj128mc202 at 79.2275 Mhz and this is my mmc initialization

Code: Select all

 mmcinit();

 do {
      Spi1_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, _SPI_PRESCALE_SEC_1, _SPI_PRESCALE_PRI_64, 
      _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH, _SPI_ACTIVE_2_IDLE);
      Delay_ms(20);

      i=FAT32_init();
      inttostr(i,ed);
      
      Lcd_Cmd(_LCD_CLEAR);                // Clear display
      Lcd_Out(1,4,"MMC Found ?");
      if (i == 0) {
              Lcd_Out(2,2,ed);
              Lcd_Out(2,9,"YES !");
              Delay_ms(1000);
      }
      else if (i == 1) {
              Lcd_Out(2,2,ed);
              Lcd_Out(2,9,"No FAT");
              Delay_ms(1000);
      }
 }while (i != 0);
 
 Spi1_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, _SPI_PRESCALE_SEC_1, _SPI_PRESCALE_PRI_16,
 _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH, _SPI_ACTIVE_2_IDLE);


LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

Re: FAT32 library for MMC/SD cards

#25 Post by LGR » 05 Aug 2012 18:14

I'm not sure if I'm following all the details correctly, but how frequently are are you writing to the SD card? How many total writes are there? From what I understand, a given sector of these cards can be written to a maximum of 10,000 - 100,000 times depending on the quality of the card (the better name brands like Kingston are closer to 100,000). If your application exceeds the maximum writes, the sector will fail permanently.

The reason why swap files are recommended is that they allow you to buffer the data and minimize the writes to a sector. If this is your problem, keeping a RAM buffer until a complete sector is full will fix this.

If it's essential to keep everything in nonvolatile memory, you might want to consider adding a Ramtron FRAM chip to the design, and buffer the files in it. They're good for 10^14 writes. I've used them on several projects, and the I2C and SPI ones are exact drop-in replacements for eeproms. This way, you can write each sector only once.
If you know what you're doing, you're not learning anything.

ebeinecke
Posts: 32
Joined: 20 Apr 2010 21:54

Re: FAT32 library for MMC/SD cards

#26 Post by ebeinecke » 06 Aug 2012 00:06

Thx for helping me, but thinking about that I changed a bit my code and now I use only one buffer[1024] and save on the end of the routine, this is done in each 2 or 3 seconds it saves about 1,5Kb max. of data. But again after 20 ~ 25 min of operation the hardware stuck and all data erased (fat32 is damaged). Any hints ? I changed the SD to another one and slowed down the SPI write speed, but no changes in the FAT problem at all . (sorry for my poor english)
My new code.

posi=gprmc(); // capta dados do gps (lat,lon,data,hora)

for (i=0; i<801; i++) { storm.ret = 0; } // Cleans buffer ... Limpar buffer da leitura anterior
for (i=0; i<1024; i++) { buf = 0; } // Cleans buffer ... Limpar buffer da leitura anterior
storm=stormlogger();
i=0;
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Logando");
d=0;
status=FAT32_Exists(posi.nomearquivo);
if (status==0) { //if file does not exist save lat/lon data
strcat(buf,posi.lat1);
strcat(buf,posi.lon1);
strcat(buf,posi.date1);
strcat(buf,posi.hora1);
}
strcat(buf,storm.ret);
i=0;
while (buf!=0) { i++; }

fhandle = FAT32_Open(posi.nomearquivo, FILE_APPEND);
FAT32_Write(fhandle, buf, i+2);
FAT32_Close(fhandle);
[/code]

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

Re: FAT32 library for MMC/SD cards

#27 Post by LGR » 06 Aug 2012 00:18

I think that the original author of the FAT32 is Dany, and he wrote it in Pascal. You might be able to get his attention on the Pascal forum, or if you look there for the very long thread (something like "pic website"), you can find a link to his website. He might have some insight into what might be the problem.

The other thing that you might do is download the Pascal source code, and see what's actually happening inside the library. It's a very large library, though.
If you know what you're doing, you're not learning anything.

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: FAT32 library for MMC/SD cards

#28 Post by janko.kaljevic » 06 Aug 2012 15:46

Hello,

From the code snippet I really can not see what could be the cause of the problem.
First thing that comes to me is that writing to some buffer can exceed it's size and thus you can overwrite random variables in RAM.
This can cause unpredicted behavior.

As LGR said, you can use swap file to save time. Writing to swap reduces writes, since you do not update FAT table.
When you do FAT32_MakeSwap, you will get start sector of your swap file. And now you can write/read to these sectors with MMC routines without damaging FAT system.

Best regards.

ebeinecke
Posts: 32
Joined: 20 Apr 2010 21:54

Re: FAT32 library for MMC/SD cards

#29 Post by ebeinecke » 08 Aug 2012 02:28

Thanks for the reply and for helping me,

My buffer size is 1024 (FAT32 formatted in 1024 bytes per sector) and the max usage of the buffer is 862 positions, everytime I use the buffer the program fill it with zeros, I'll try to find something in the others sub routines that could be causing this and to be sure I will revert my code for FAT 16 and try running to see if the problem happens again.

I noticed that before the hardware stuck it resets itself for 3 or 4 times and then finally stuck.

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: FAT32 library for MMC/SD cards

#30 Post by janko.kaljevic » 08 Aug 2012 09:17

Hello,

This is strange behavior and it will need further inspection.
If you can locate a exact place of reset or freeze it would be helpful.

But without your entire project and explanation how to reproduce it, I can not be sure what could be cause of this.

Best regards.

Post Reply

Return to “Library Development Discussion”