How should I Write data in a SD memory and read it through SPI ?

Status
Not open for further replies.
Hi, there is no idea ?
today I am sure that I have done the writing process correct because, I wrote 512 byte "110" in a sector with my codes and after that I used the read function of mikroc and fortunately the sd return 512 byte "110" and that was so exciting. now just I am challenging with reading part. if any body have a solution please fell free to share it
 

Hi every body
finally I could wrote and read from SD successfully, I wrote one sector and read it again. now I want to make a text file in the SD memory , so I want to know does any body can guide me what should I do ? I didn't find any thing in the datasheet !!!!!!!!!!!
 

I think U like to do every thing your own, without depending on library...That is good for a hobbyist and some one who is interested to know more about what exactly happening in the background....
Now I think you need to learn about a file system, and it's structure etc (for example FAT) and need to try to write a code for read and write from and to a file system...
I have done an MMC card based wav player, there I did the code for fat reading from MMC. You can see it at my blog link
https://blog.vinu.co.in/2012/02/tv-remote-controller-high-quality.html
Any way I cannot say my code is perfect, but it works fine for me...

---------- Post added at 13:35 ---------- Previous post was at 13:20 ----------

You can check out this link for knowing about FAT file system.
**broken link removed**
 
Last edited:

THX, Yes your right I would like to do every thing my own, Thx for your codes but as I said before I am amateur in C language so, It's difficult for me to understand your codes so at first for continuing the project I want to make a txt file in the SD card is there any Pdf related to my request in the Internet ? main while is it possible for you, to explain what have done with SD? and after that explain your codes line by line ? I am so confused why there isn't mentioned any thing in the datasheet !!!!!!!!

---------- Post added at 15:44 ---------- Previous post was at 15:41 ----------

THX, Yes your right I would like to do every thing my own, Thx for your codes but as I said before I am amateur in C language so, It's difficult for me to understand your codes so at first for continuing the project I want to make a txt file in the SD card is there any Pdf related to my request in the Internet ? main while is it possible for you, to explain what have done with SD? and after that explain your codes line by line ? I am so confused why there isn't mentioned any thing in the datasheet !!!!!!!!
 

I am so confused why there isn't mentioned any thing in the datasheet !!!!!!!!
MMC/SD data sheets will not contain any thing about writing a *.txt file into it... Because it comes under the section - "file system"....
And it is not like writing a sector or few sectors of raw data.. Just assume your memory card as a blank note book...
;;;;;;;
Now you started to reserve and make an index page (first few pages) for title, page number, date, remark etc for future use. Then you can easily open the exact topic by searching the index page. is it?
Like that, in SD card, you need to format it at first. Formating will reserve some block of memory for maintaining the records like the file name, file size, date, real address of the file starting block(cluster), file attributes(read only, system etc) etc etc,,, (beyond that, but I am trying to make it simple to understand)
;;;;;;;

Now you are writing a new topic to ur book, then you will update the index page also. Similarly you need to update many things on the previously reserved block and at the same time you will write the actual file data into the memory... In your case the txt file...

Hope you understood, and now I think you will not ask why it is not there in the data sheet of SD...


There are many file system like FAT, NTFS, EXT3, ETX4 etc etc... It is user choice to select it. Most commonly, FAT (16/32) is used in MMC/SD...
If your intention is to learn things then it's okay, otherwise it will be a time waste to do these low level codings because there will be libraries for these things and people generally depends on those....
 
Last edited:
Reactions: mf1364

    mf1364

    Points: 2
    Helpful Answer Positive Rating
Thanks for your guidance, my aim from this project is just for learning basically. Is there any PDF or article that can help me to achieve my intention ? you know I am looking forward to finding the sources of the ready libraries till can realize any thing happen during communicating with SD card.
 

Hello!

As said several times in this thread, all the info you need is on the net. You can find
extremely accurate documentation (you like PDF? There is a full SD card specs manual in PDF).
I have also developed my own driver some years ago using a document published by
Sandisk (title = Sandisk mini SD card, version 1.1, document no 80-36-00235). In this document,
there are flow charts for both initialization and data access. I think it's the ideal document if you
want to write a driver on your own as you write above.
I have attached a part of this document to show you how accurate it is.
Well, you need another version because you are apparently using SDHC cards and this one
is for plain SD. But there are certainly more recent version for SDHC and SDXC.

If you read the document carefully, you will get accurate info about the timings, transfer rates,
delays, etc...

Just in case: there is also another document which I used, explaining how to use an SD card
with MSP 430. I remember that the author name was Foust or Faust, something like that.
I'm sure you can find the document on the net. It contains a feature restricted but working
implementation of the SD card software. It might be worth having a look at it.
From there, you can build your own implementation, that's what I have done. And if I did it
you can certainly do it.



Dora.
 

Thx, but I think you didn't get my need exactly, as I said before I can write and read from SD successfully for example I wrote "110" in one sector and read it again, but now in the next step I want to make a text file in the SD and write some thing in it but your suggested PDFs don't help me .
 

Hello!

There is also a lot of documentation about FAT 16 ad FAT 32 on the net.
Google FAT specs and you will find anything you need.

Dora.
 

I don't know if some one is already prepared a pdf for exactly writing txt file to SD card with the full explanation for the low level coding...

In my case, I learned about fat from this link **broken link removed**

Then I tried to access a file system on the MMC card, I read first sector (boot sector) and from there I get some important data like fat start, sector/cluster, root directory start, data start etc etc and so on...

So, you can try the above link and learn about FAT16 now. Then format your SD with FAT16 and try to read the first sector (sector 0) to a buffer and find the importand stuff which u learned from the link provided.... This is the way I followed...
Reading a data from a filesystem is easier than writing . So at first just write data from compurer and try to read it using ur own code...

In that link, start from the "FAT16 Boot Record" section and just skip those first few topics like partition etc because you don't need it in your case...


[Also, it will be better if you post your working initialization code here,because it may be helpful for others....]
 
Last edited:
Reactions: mf1364

    mf1364

    Points: 2
    Helpful Answer Positive Rating
Thx for your suggestions and in the next step, making a text file in the SD without using existed libraries I am struggling with some questions:
1- what are the parts of the a text file ?and where is the place that information related to a txt file are stored ? I mean the full details
2- what is the relationship between a txt file and the SD card format ? is there any logical relationship ?
3- I opened my SD memory in the "winhex" software and there are lots of vague codes that I can't understand them but I think they involve the TXT file information so I want to know do you have any advice for finding the meaning of them ? here is some of them : have a look plz View attachment winhex2G.bmp
4- according to the datasheet of the SD there are "block" and "sector" and the size of block is depends on commands and the size of sectors usually is 512 bytes, in your link there is a new unite that has called "cluster" and I didn't get how much is the capacity of cluster . do you know ?
5- could you please guide me more that for achieving my aim "making a txt file in the SD " how can I use your link ?
at last about your request for my source code, It's Ok but at first I should finish my project as you know more than one month is passing and I am struggling with this project and right after I finish it I put an article related to my achievement.
 

You have dumped sector 0, a disk boot sector. It hasn't to do with text files anyhow.

Text files contain printable characters and some control characters, at least carriage return (0x0d) and line feed (0x0a). Binary files contain all existing character codes. The file type (text, binary) has no meaning for the file system.

Writing a text file to a SD card isn't different from writing a binary file.

The cluster size is coded in the boot sector, refer to a FAT documentation. For FAT16, the cluster size is usually commanded by the available cluster count.
 

Hello!

You were saying a few posts earlier: "THX, Yes your right I would like to do every thing my own,...".
In this case, I think you may want to find information using google and actually do an implementation
on YOUR own, not on OUR own...

That said...


These answers are for FAT 16. But as FAT16 is valid up to 2 GB only (if I remember correctly) and as you
are apparently using an SDHC, I think you need FAT32. Anyway FAT32 works more or less on the same principle.

1- what are the parts of the a text file ?and where is the place that information related to a txt file are stored ?
The parts of a TXT file:
Assuming you don't create subfolders, you will have a root folder made of 32-byte records. (*)
In this 32-byte record, you can read the start cluster and the length of the file. (*)
Beside this, there are FAT tables (usually 2) which have exactly the same contents.
The FAT tables (File allocation tables) contain the sequences of clusters used for file referencing (*)
I mean the full details
See (*).
2- what is the relationship between a txt file and the SD card format ? is there any logical relationship ?
There is absolutely no relation between the txt file format and the SD card format. That's why you can also
store pictures or whatever on a SD card, and everything is stored the same way. Similarily, you can write
a text file into a SD, a hard-disk , a floppy , a CD or a tape, this is absolutely independent.
If you want to understand the contents of what you see in winhex, please refer to (*) hereafter.
Winhex shows the MBR of the card (Master boot record). What I can see is that it's a FAT32, that your
card has no name, that it has 512 bytes per sector, that there are 8 sectors per cluster, that there are
34 reserved sectors, etc. If you want to read and understand this info yourself, see (*) hereafter.
Just a question: do you read the documents that other people post? In the link vinodstanur posted, there
is a paragraph titled "cluster meaning". Now if you don't understand what it says, just google a few more
times to get more accurate information. And if you have a question you cannot solve, then you can ask
others' opinions(*). By the way, all the questions you are asking are answered in vinodstanur's link.
Your question is too wide. There is too much to do to reply in a single post. Start by writing your code,
and ask only when you are really stuck.
For creating a file:
a. - Create a file record;
b. - Find an empty cluster;
c. - Record the cluster ID in the FAT tables;
d. - Write your txt (or other data) to the cluster you have found
e. - At the end, write the actual data length to the file record you have created at point a.

d-bis Everytime you cross a cluster border, allocate another cluster (points b and c) and
write the data to it.

Now please DON'T ASK how do I create a file record, how do I find an empty cluster, how do
I record the cluster ID, etc... DO IT YOURSELF!!! And as usual, please refer to (*).

(*): fully documented on the net.

Dora.
 
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
Writing a text file to a SD card isn't different from writing a binary file.
Yes I agree with you, but I am confusing because I don't how should I start for writing a txt file in the SD, I followed the link of vinodstanur that was good but in frankly speaking I don't know how should I use it ? which information should I pick up from that ????? Do you have any advice ?
 

I think, steps a - e listed by doraemon are the brief answer. They refer to three storage areas that have to be written respectively modified to write a file: FAT, directory and data area.

In addition, to do so, you would have read and decoded the boot sector information before.
 

Hello!

I followed the link of vinodstanur that was good but in frankly speaking
I don't know how should I use it ? which information should I pick up from that ?????

OK, you're lost? Let's do an example together: read the FAT32 boot record.
1. Go to the link vinodstanur provided. Click the link at first line to get FAT32 info
2.Open winhex (the picture you posted). Setup winhex to show the addresses in hexa
since the documentation is in hexa.
3. Look at the documentation at FAT32 boot record.
There is a table with 3 columns, offset, description, size.
00: ignore
03: OEM name (8 bytes). It's written "MSDOS5.0" in your case
0B: bytes per sector (2 bytes). At 11 and 12, I read 00 02. But this is little
endian, which means that it should be interpreted as 0x0200 or 512. 512 bytes/sector.
0D: sectors per cluster: 8
0E: reserved clusters: 0x0022 : 2 x 16 + 2 = 34 reserved sectors
10: 2 FAT tables
The 2 next values are ignored for FAT32
etc... etc...

Now exercise:
- Once you have read all that stuff, calculate the first sector ID of the first and
second FAT tables.
- Calculate the first sector iD of the root.
- Calculate the fist sector ID of the first data cluster
Hint : use the table FAT2 drive layout.

Dora.
 
Reactions: mf1364

    mf1364

    Points: 2
    Helpful Answer Positive Rating
Thx every body, till now I could find the 32 byte related to file specification such as date and time and so on but when I look the root directory there are more than 32 byte related to a txt file and I don't know what are they ? please look at the pictureView attachment untitled.bmp and answer me as soon as possible
 

What makes you think, that entry 1 to 3 are related to entry 4? They aren't.

You need to read the FAT specification more thoroughly. In this case, entry 1 to 3 or deleted file records, marked by 0xE5. But there may be also LFN entries in addition to the basic entries.
 

Hello!


1. Never say "as soon as possible" when your ask for help. Most of the people who help
you do it on their spare time.
2. Will you please read the docs that people point to you?

You have been asking question after question for about 2 months. We are close to
80 messages for this issue, and about 40 messages are from you. Sometimes 3 messages
in a row just to drop a dime in the juke-box and play the same question. And as FvM
pointed out this time, you didn't understand what was happening because you didn't
read the docs other people sent to you (for the records marked E5). It was already
the case for many earlier questions.
What is the point of sending you hints if you don't read the docs we send you (by the
way, do you know how we find these docs? With Google. Repeat after me: Google!).

As far as I'm concerned, I got a little bit tired to have to explain every bit of
the file system to fill the gaps of what you didn't read. So please understand that
I will reefing the sails on this subject.

Best regards,

Dora.
 

Once you have read all that stuff, calculate the first sector ID of the first and
second FAT tables. for FAT1 first data sector is 1740/512 = 34 and for FAT2 table first data sector is 1980928/512 = 3869
- Calculate the first sector iD of the root.3944448/512=7704
- Calculate the fist sector ID of the first data cluster? I have NO idea!!!
for calculating the reserved sector, this way isn't better ? 0x0022 convert to decimal is 34. please check the above answers to your question and correct me if I made a mistake. notice : the above answers are according to FAT32 and the capacity of the SD is 2Gbyte.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…