fat writing to sd

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jim, OK, back in the forum now.

There is a project here that i copied, so that i could learn more about avr's. Its the simplest mp3 player i could find as a project. Anyway it uses a lirbrary FAT.h to read from the SD card, (pretty sure it is anyway...)

So that i can learn a little more i am trying to change the mp3 player so that it now can give out warnings or messages based on sensor inputs.

I have succesfully got it to respond by playing mp3's based on sensor inputs such as it says too hot, when a thermometer readches over 30 degrees C.

This was pretty simple to be honest, I studied the code to work out how it decided what mp3 to play (its done on the index number of the actual mp3 file on the sd card), and modifed the code to play certain mp3's at certain times...

Now though I want to be able to store a number in a text file on the SD card, so that i can be called back if the machine was ever turned off.

So I can read from the card, but I am not sure how to write to the SD card.

In windows I know it is fopen, fprintf, and fclose,
but windows takes care of alot. How do i do the equivelent with the SD card, if all the libraries for FAT are there and FAT_Init() has been initiased.

To be honest i am not sure (and wouldnt bank on it) that it is FATfs, because the library is called FAT.h

Thanks Guys

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you're not sure, why don't you check it? Download Fatfs and see!
http://elm-chan.org/fsw/ff/00ind...

You'll see writing to the SD card is much the same as under Windows.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So i have decided to try and put the fatfs libraries into my program so that i can write to SD card. In avrstudio I included ff.h I ran it and had an error about integer.h and included that too. Now I am having errors about stuff in integer.h, I didnt know how to add them into the "external depecnies" tab on the left. Maybe this is my problem,

But then once i have all that up and running and it accepting the lbraries, can i just use the f_open etc as i do in windows?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'd say it is similar - but you'd want to read the manual! From my recollection, you call fopen to open the file in a particular mode read,write etc, then call fwrite to put data into the file then fclose to seal it up.
There will be a limit on the number of open files you have as ram is limited in the AVR.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i'm still struggling just adding the correct libraries, the integer.h seems to be trouble!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hold on, I'm trying to put together a small prj with all the required libs for you.

Jim

Jim Brain

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here's a sample FatFs-LFN project that compiles, runs, and writes and reads a file. It is set up for a M1281, but the defines are in for a 644 and 644P.

  • Set your F_CPU and CPU in Makefile
  • In config.h, follwo instructions to map to your config. If you do not have card_detect and card_wp set up, then just set the defines to 1 or 0, to test.
  • The SPI code assumes you're using SS as the SD card select pin.

The code will open a file, write 256 bytes, and then open and read them back.

Acknowledgements:

  • FatFs is derived from CHaN's FatFs, but has many more options, and can handle multiple files with only 1 512 byte buffer.
  • uart code is modified from Ingo Korb's sd2iec codebase
  • sdcard and spi files are straight from sd2iec. His sdcard.c file handles SDHC cards seamlessly.
I have a disk_mux and ata files that can be added to this project, if anyone sees this later and cares.

Jim

Attachment(s): 

Jim Brain

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cheers Jim, did you make it with avrstudio, because it doesnt like it...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, I created it by hand. You should be able to add the source files into a Makefile that AVRStudio, just compile them all and set F_CPU to 8000000 and set the avr CPU type accordingly.

I don't have AVRStudio here, so I can't provide a suitable Makefile.

However, you should be able to unzip and compile with 'make' if you have WinAVR installed.

Jim

Jim Brain