Interfacing AVR and SD(Fat)

Thread Starter

Shagas

Joined May 13, 2013
804
Hello

I'm trying to interface my atmega328 to an SD card and after reading many articles and tutorials I still have absolutely NO clue where to begin..

Someone suggested using fatFs which will make writing/reading to the sd card 'extraordinarily simple " but it seems to me that every tutorial on the fatFs or SD card interfacing in general assumes that the reader is a computer wizard or has years of experience with embedded systems.

Can someone tell me where to start? I'm starting to get desperate here. I've spent more than 20 hours in total trying to interface an SD card to my avr with absolutely ZERO success.

------------

I'm currently looking at this website which explains how to use petite Fatfs.
I downloaded the " Petit FatFs R0.02a" module at the bottom of the page which contains a bunch of functions and from what I understood I'm going to have to write my own SPI routines.
I know how to use SPI but I have absolutely no clue as to how to interface it to this module/sd card.

Could someone help me out please?
 

sirch2

Joined Jan 21, 2013
1,037
First off, 20 hours doesn't sound overly long if you don't have much experience in this area, expect to spend quite a bit longer.

Are you using a bare atmega328 or an Arduino? If its just a 328 then how are you programming it, what language, etc.

SDFat works fine in Arduino land and also works (as you would expect) as an included library in AtmelStudio. There are quite a lot of examples, wiring diagrams, etc. on the Web for SD to Arduino and it should not be hard to translate these into SD - ATMega328. What specific problems are you having?
 

ActivePower

Joined Mar 15, 2012
155
Someone suggested using fatFs which will make writing/reading to the sd card 'extraordinarily simple " but it seems to me that every tutorial on the fatFs or SD card interfacing in general assumes that the reader is a computer wizard or has years of experience with embedded systems.
While you might know this I'd like to point it out nonetheless in the off chance that you're not aware of it.

SD cards are a storage medium like any other kind of memory. They follow a particular protocol to be written to or read from. Almost all SD/MMC cards support two standard protocols for this purpose: a proprietary one from Secure Digital (less common in hobbyist circles) or SPI (much more abundantly found). Clearly, you're better off with the latter as it has hardware support in most 8-bit processors (ATmega328 too).

FatFS is a user-friendly library of functions to implement the FAT filesystem on an embedded memory device. Having a filesystem is in no way necessary if all you need is to interface an SD card for some memory. You could write raw data to it via SPI and it'll happily store it for you. More commonly though, you'd need the SD card to be read/written via a PC too. That's where the FAT filesystem comes in - it is relatively easy to implement and almost universal.

Knowing this your roadmap should be clear. Write an SPI driver for your microcontroller - the hardware unit makes this considerably easier. After testing to see if it works, get the FatFs library and understand the FAT filesystem in general (sectors, tables and stuff). Once you do this, use the library routines to create a FAT filesystem on your SD device.
There are plenty of examples and documentation available on this on the web - not just for experts. After you've gotten the basics down, it should be pretty straightforward.

EDIT: I'm sorry I didn't read the OP completely the first time. You say you know how to use SPI. Usually, understanding basics of the FAT filesystem is the only other prerequisite but as you're having trouble with this we'd probably need to know a little more about your hardware/software setup.

Hope this helps.
 

Thread Starter

Shagas

Joined May 13, 2013
804
Thanks alot for the answers.

I am using a bare atmega328 (in C)- no arduino.
I actually don't need to be able to read the card with a pc. I just need it as a storage device for the avr. I tried just writing 'raw data' and that didn't work either and I couldn't troubleshoot it because I don't understand alot of the terminology used in the documentation.
Concerning the hardware setup , well I don't know what there is to say. It's an Atmega328 connected to an sd card breakout board through SPI.

In any case I abandoned the SD card for this project and got myself a bunch of parallel SRAM so I can finish this project with abit of hair left on my head.
I'm going to take your advice and try to work on the Fat a few hours a week until I manage to get it working or until I find someone in my locale who can help me out because as much as I like to fiddle with electronics I don't want to spend half of my semester/time buried in the flash memory protocol.
 

kubeek

Joined Sep 20, 2005
5,795
It's been a long time since I wrote the code, but if I remeber correctly all you needed to make fatfs work was to provide a function to read a byte and to write a byte to spi, and some buffer in ram.
Here's my bachelors project that stores data on SD card and allows you to read it back over UART. Ask if you need anything explained. Code is for ATmega128, but the differences should be very minor.
 

Attachments

Last edited:

takao21203

Joined Apr 28, 2012
3,702
Hello

I'm trying to interface my atmega328 to an SD card and after reading many articles and tutorials I still have absolutely NO clue where to begin..

Someone suggested using fatFs which will make writing/reading to the sd card 'extraordinarily simple " but it seems to me that every tutorial on the fatFs or SD card interfacing in general assumes that the reader is a computer wizard or has years of experience with embedded systems.

Can someone tell me where to start? I'm starting to get desperate here. I've spent more than 20 hours in total trying to interface an SD card to my avr with absolutely ZERO success.

------------

I'm currently looking at this website which explains how to use petite Fatfs.
I downloaded the " Petit FatFs R0.02a" module at the bottom of the page which contains a bunch of functions and from what I understood I'm going to have to write my own SPI routines.
I know how to use SPI but I have absolutely no clue as to how to interface it to this module/sd card.

Could someone help me out please?
No there is code supplied for SPI. When you look all the folders, generic will contain a C source.

You need to add all the C sources and H files into a project, and then modify them until all error messages go away. That should take no longer than one hour.

You need to replace the port bits access with your own too but these are just defines.
 

THE_RB

Joined Feb 11, 2008
5,438
If you use MikroC compiler it has inbuilt libraries for both direct read/write to SD card, and also FAT file read/write etc.

You can just use their C example to read the card and modify it to your needs. :)
 

Thread Starter

Shagas

Joined May 13, 2013
804
Nice ,thanks alot for the help.
I'll be trying it out again with the sd card once I finish the project using the Parallel SRam chips that I got.
I'll let you know if I have any issues , Kubek
 
Top