Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

write to mmc card with atmega

Status
Not open for further replies.

anhnha

Full Member level 6
Joined
Mar 8, 2012
Messages
322
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
3,684
Hello,
I have a project in which I need to write to mmc card from atmega128. In atmega128 I declared a buffer 256 bytes to received an image from computer (the size of image is about 2M). Then I want to store this image in a mmc card and I must read buffer and write it into the card. However, I am not familiar to this.
An image(2M bytes) from computer is sent to atmega128. I declared a buffer(256 bytes) to temporarily store it. Then I want to write the data in buffer to mmc card(2G). I want to use fatfs but I don't have any experience in this.
Could anyone help me about these steps that I need to do?
I know that the minimum data block that can be write in mmc card is 512 bytes but is this still necessary if I use fatfs? I am confused because I only have a buffer 256 bytes and the buffer is not full of data. Therefore is it possible for me to write to mmc with a buffer 256 bytes(assume only 230 bytes data and 26 bytes is null)?
 

Better to choose SPI interface for simplicity.
 

Yes, I use SPI but I am not familiar about FAT. Could you tell me what I should do? I have many things confused as above.
 

I am also handling similar task and found that Chans libraries are frequently referred as useful library pack. There are very good tutorials with different approaches at the following link

https://www.dharmanitech.com
 
  • Like
Reactions: anhnha

    anhnha

    Points: 2
    Helpful Answer Positive Rating
I am also handling similar task and found that Chans libraries are frequently referred as useful library pack. There are very good tutorials with different approaches at the following link

https://www.dharmanitech.com
Thank you, ark5230
And sorry for my ignorance, I have seen this before but I still can figure out how to make it work for me.
For example, I have a buffer 128 bytes in atmega and this buffer is received data continuously from computer. I want to transfer data from buffer to mmc card. Could you tell me how to do that?
The library FATFS has these functions but all of them deal with file not an array of buffer?
pf_mount - Mount/Unmount a Volume
pf_open - Open a File
pf_read - Read File
pf_write - Write File
pf_lseek - Move read/write Pointer
pf_opendir - Open a Directory
pf_readdir - Read a Directory Item
Disk I/O Interface
disk_initialize - Initialize disk drive
disk_readp - Read partial sector
disk_writep - Write partial sector
And another problem is that the buffer is only 128 bytes but one sector is 512bytes, is it mean that I will waste 128 bytes each sector?
 

Can you use mikroC Pro AVR/ It has Mmc Libraray. ver easy to use.
 

I use AVR Studio, I know that this code is too basic but I just don't know how to start. Hope someone can guide me.
 

I think you have to first use pf_write to write a 0kb file and then open that file and write the data you received from buffer.

zip and post your project files. I have Atmel Studio 6. I will try if I can do something.
 
  • Like
Reactions: anhnha

    anhnha

    Points: 2
    Helpful Answer Positive Rating
Thank you very much for your reply and kind help.
Bellow is the code that receive data continuously from computer and store it temporarily it to USART_RxBuf. I have test it successfully. Now I want to transfer the data from USART_RxBuf to MMC card each time buffer is full. I want to use ffsample for avr that provide for SPI and functions to interface with the card. However, I get stuck in how to start it. I see that all functions use files as input but in the code USART_RxBuf is only an array of memory and I don't know how to do it.
All functions are provided but I wonder how can I use these functions in this case.
(Sorry, below is the code about usart and ffsample for avr. I still can not figure out what to start with mmc)
 
Last edited:

You have to use fat16 or fat32. Is there such library in atmel studio 6?

Where is your .mmc image file for Proteus?

Did you try to create a dummy file on mmc card and then open that file and write your 128 bytes to it?
 
Last edited:

There is a library for this. It is ffsample in my attached file.
Where is your .mmc image file for Proteus?
I assume that there is a stream of data that is transfer from computer to USART_RxBuf. Then I have to write the data in this buffer to MMC card when the buffer is full.
Did you try to create a dummy file on mmc card and then open that file and write your 128 bytes to it?
In this example, I use terminal to type data to TX of atmega, then the data is transfer to USART_RxBuf. As above I want to transfer it to MMC card.
 

Attachments

  • FatFs library.zip
    783.9 KB · Views: 65
  • ffsample.zip
    1.9 MB · Views: 84

Are you having problem using ffsample library? I asked where is your .mmc file for Proteus?
Try to create a dummy file like filename.txt on SD card. Then open it and try to write your buffer (128 bytes) to it.

See if that works.

I have uploaded a .mmc file. There is a filetrf1.txt file in it. try to open it and write your 128bytes data to it. Let's see if it write the text into the file.
 

Attachments

  • mmc_card.rar
    719 bytes · Views: 68
Last edited:
  • Like
Reactions: anhnha

    anhnha

    Points: 2
    Helpful Answer Positive Rating
Are you having problem using ffsample library? I asked where is your .mmc file for Proteus?
Try to create a dummy file like filename.txt on SD card. Then open it and try to write your buffer (128 bytes) to it.

See if that works.
Yes, I think I have problem with this!
You mean that at first I have to create a dummy file like filename.txt on SD card? Could you tell me how can I do it? Sorry I still not get it, is I have to create
a file in main program then write the data in buffer to it. Next, I use fwrite() to write it to mmc?
 

Read my last post, its updated. I have created the mmc image file for proteus. download the .rar file and extract it. I have created a dummy filetrf1.txt file on the mmc image file. set that .mmc file as image file for MMC Card in Proteus. try to open the file from your code and write the data (128 bytes or so) to it.

Let's see if it writes data to it. It ia a plain ascii text file.

I use PIC and I don't use AVR. So you have to try it. For now just open the file and write the data to it.

use pf_open() to open the file and use pf_write to write the 128 bytes to the file.
 

I really don't get the steps to do it. Here are my thoughts, hope you can correct me when I go wrong.
1. Create a filename.txt in the main program
2. Open the file with fopen()
3. Write buffer (128) byte to filename.txt
4. Use disk_writep() to write this file to mmc
And is this code will write 128 bytes in the first sector. Then how can I know to write to the second sector,...?
PS: Sorry I don't see your last post.
 

For now just use f_open or pf_open to open the file then write the 128 bytes to it. I don't know about disk_writep(), what does it do?

For just now don't use disk_writep(), we can try that later. Data will be written from first sector. there will be a pointer in file write function. It will store the value of the bytes written. when writing data again you have to start from that pointer value and write the data. you have to save the value of that pointer somewhere.

Don't worry about file creation. I have created filetrf1.txt on the mmc image file. we can do the file creation later.

many times I have opened blank txt files in hex editors and added text to it and saved the files to see the actual text really written to the files.

Can you post your Atmel Studio 6 files of your project?
 
  • Like
Reactions: anhnha

    anhnha

    Points: 2
    Helpful Answer Positive Rating
Thank you!
I understand it more now. Could you tell me next step after I write 128 bytes to mmc_card.mmc?
Are the steps above correct? I use all function from this: http://elm-chan.org/fsw/ff/00index_e.html

- - - Updated - - -

Can you post your Atmel Studio 6 files of your project?
All file I did are in attached file at #9, I only test use terminal in Proteus.
 

I worked out your problem with PIC SD card and mikroC compiler. I wrote the text "Hello World!" to the opened file. Then I opened the file from the image file and saw that the data was actually written.

you have to use just f_open() and f_write() functions.

try to f_open filetrf1.txt and do f_write() 128 bytes to it and then do a f_close()

After writing 128 bytes to mmc do a f_close() on the opened file. Then rename your .mmc file to .IMA file
Then open the .IMA file in WinImage and then select filtrf1.txt and click Extract in Image menu
Extract the file to some folder and then open the file in notepad. See if the bytes written appear as text in the file.
 
Last edited:
  • Like
Reactions: anhnha

    anhnha

    Points: 2
    Helpful Answer Positive Rating
you have to use just f_open() and f_write() functions.

try to f_open filetrf1.txt and do f_write() 128 bytes to it and the do a f_close()
Please tell me what i need to do next?
1. Create a file filetrf1.txt
2. Write buffer (128 bytes) to filetrf1.txt
3. Close the file filetrf1.txt
Now I need to write this file to mmc card, what steps I should do now?

- - - Updated - - -

And is this way too waste memory in mmc card because each time I write filetrf1.txt (128bytes) the memory it take in mmc is one cluster?
 

After doing the 1, 2, and 3. you don't have to do anything. rename the .mmc file to .IMA file and extract the filetrf1.txt from it and see what is written in the text file. See my last post.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top