Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

For Beginners...
Goto page Previous  1, 2
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> KokkeKat FAT-free SD card lib
View previous topic :: View next topic  
Author Message
KokkeKat

Bascom Member



Joined: 08 May 2011
Posts: 59
Location: Stockholm

sweden.gif
PostPosted: Sun Sep 11, 2011 3:40 pm    Post subject: Reply with quote

Hi Manans

I can only answer for the one single MicroSDHC card I have used. It's a Kingston SDC4/8GB card (and it works - but only tested in the full-size adapter). I don't know if Kingston is selling various cards under the same part number.

http://www.kingston.com/flash/sdhc_micro.asp

Apart from this, I can only say that I found differences in the implementation of the variuos full-size SD cards I tried during development. Some cards require the full initialization sequence, while others allowed "shortcuts". All of the tested cards support the specified initialization sequence.

Also, every microcontroller SD library I have seen is using SPI mode, so all libraries would suffer from this potential limitation.

I hope this helps.

Kind regards

Niclas
Back to top
View user's profile
manans

Bascom Member



Joined: 30 Jan 2009
Posts: 18

india.gif
PostPosted: Tue Oct 11, 2011 12:28 pm    Post subject: Reply with quote

Hi Niclas,
I have connected a 2Gb microSD card to meg32 and I am able to write the example file into SD card Very Happy , moreover I copied the write_file_example code into the Main file and commented the $include statement, and again it worked. Very Happy

Now, I want to explain my situation. I want to make a datalogger which will be continuosly acquiring data and write it on sd card. Now if accidently one switch off the power during writing into the file then in that case whole file will be corrupted as proper file closing is not performed?

One more thing, in append file you have given two else condition suggesting " Handle partition full ", can you elaborate on that.. I mean writing into the file when will partition becomes full when using 2Gb card.?

Thnaks. Because of you I am able to do till now...
Back to top
View user's profile
KokkeKat

Bascom Member



Joined: 08 May 2011
Posts: 59
Location: Stockholm

sweden.gif
PostPosted: Tue Oct 11, 2011 8:26 pm    Post subject: Reply with quote

Hi Manans

I am happy to hear of your success. Smile

You are right about data corruption in case you eject the card or switch off the power during a write. If that happens, you will have fragments of a file. In this case the best you can do is to read the SD card with a HEX editor such as Winhex. As long as the partitioning or the fundamental file system isn't destroyed, you should be able to manually fix it with Winhex. Please note that this can occur in a PC as well.

"Handle partition full" means that I haven't included a check. If your application runs the risk of filling the card, you should write an additional routine that checks if you are writing to the last sector in the partition. In that case, you must stop adding data and finalize the file (prematurely).

To answer your question "when will partition becomes full when using 2Gb card.?":
For this you need to understand how many sectors make up one cluster. Each file or directory requires at least one cluster, so if you write large files you will use a high percentage of your card's real storage space. If you instead write many small files, you will use only a fraction of each cluster. For example if you have a 4GB card with 8-sector clusters and create a 100-byte file, you will need 8*512 = 4096 bytes to store 100 bytes of data. (Plus, each file takes up 32 bytes of the cluster(s) directory storage, so creating many files require a lot of directory space.)

Kind regards

Niclas
Back to top
View user's profile
manans

Bascom Member



Joined: 30 Jan 2009
Posts: 18

india.gif
PostPosted: Fri Oct 14, 2011 7:22 am    Post subject: Reply with quote

Hi Niclas,

I want to ask you one thing. The data which I want to write in the file is type of "single" having one decimal point and 3 digit max. I mean its positive no. having max value of 999.9. As we write bytes so what can be best method so we have to write least amount of bytes for one datapoint. I found maximum 5 bytes to write for 1 datapoint. (one for each digit and one for decimal point.)
Back to top
View user's profile
KokkeKat

Bascom Member



Joined: 08 May 2011
Posts: 59
Location: Stockholm

sweden.gif
PostPosted: Fri Oct 14, 2011 10:11 pm    Post subject: Reply with quote

Hi Manans,

One way is to multiply by 10 and store the result in a word variable that is an overlay over two separate bytes. Saving these two bytes to the card probably requires the least SD card space.

Another way is to multiply by 10 and then place it in a 4-char string. (Overlay of a 4-byte array over a 4-char string.)

Kind regards

Niclas
Back to top
View user's profile
manans

Bascom Member



Joined: 30 Jan 2009
Posts: 18

india.gif
PostPosted: Sat Oct 15, 2011 6:50 am    Post subject: Reply with quote

Hi Niclas,
I initially thought about converting it into word, but user have to do modification in the saved file.
Anyway, tell me one thing, I have to move to different controller because my current controller is out of sram..so apart from spi declaration do I need to change any major thing?
And moreover, I am still clueless about how to handle partition full? If appending into file ,I encounter partition full then should I create new file and then write inside. Again until my sd card becomes full...Is this correct way?
Back to top
View user's profile
KokkeKat

Bascom Member



Joined: 08 May 2011
Posts: 59
Location: Stockholm

sweden.gif
PostPosted: Sat Oct 15, 2011 2:16 pm    Post subject: Reply with quote

Hi Manans

You can use either HW or SW SPI. Just make sure that the initialization SPI clock is 100-400 kHz and then increase to the maximum. As far as I know, there is no specified lowest maximum clock in SPI mode, but the lowest standard speed seems to be 25 MHz. It seems that the ATMegas can do a maximum of half the CPU frequency = 0,5* 20 MHz. I don't know if the XMegas can do 1 * 32 MHz or if they are also limited to 0,5 * 32 MHz. In that case, you could just set the SPI speed ti maximum. Apart from that, I don't think there is anything you must think of.

This lib creates a directory entry first, then saves the file and finally goes back to update the file size in the directory entry. This means that you can prevent data corruption, so long as you stop trying to write or append to the file once you would exceed the last sector in the partition. Please see gosub Sdwipe for some examples:

The last sector of the partition = Sdtotsecsb + Sdpartstart - 1. I'm not sure that the last sector of the partition is always the last sector of a cluster, so you might want to adjust for this, in a calculation just after the initialization: Last cluster = last sector DIV Sdsecspercluster. Adjusted last sector = last cluster * Sdsecspercluster + sdsecspercluster - 1.

So, when trying to create a new file or subdirectory, make sure that the cluster number < last cluster - 1. (So you could both fit in a directory extension and the file or subdirectory.) Otherwise, abort trying to create file or subdirectory.
When writing or appending, make sure that the sector <= adjusted last sector. Otherwise, finalize the file.

I didn't implement this in the library simply in order to reduce the code and RAM usage. Trade-offs, trade-offs... Wink

Kind regards

Niclas
Back to top
View user's profile
manans

Bascom Member



Joined: 30 Jan 2009
Posts: 18

india.gif
PostPosted: Wed Oct 19, 2011 12:38 pm    Post subject: Reply with quote

Hi, Niclas,
I want to ask you that while writing on sd card is in loop then which part of main file needs to be executed for each iteration and which part is required to be there only once.?

One more thing ..what is the difference between "Example_DirList_Read_File" and "Example_DirList_Read_File_LFN" ???
If I want to search a particular file in the card which of above I should use?
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> KokkeKat FAT-free SD card lib All times are GMT + 1 Hour
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum