| Author |
Message |
|
|
Posted: Apr 01, 2012 - 11:37 PM |
|

Joined: Apr 07, 2011
Posts: 38
|
|
After coming across quite many SD tutorials that are based on Petit FS or FatFS and guide the user through modifying these libraries, I thought it would be interesting to make one that would really start from the fundamentals of reading FAT file systems, so you'd actually understand all of the code, not just the pins you connected to your MCU.
The first part of this effort is now available, I'm planning a similar 3-5 part layout as my previous V-USB tutorial I posted here a while ago:
http://codeandlife.com/2012/04/02/simpl ... al-part-1/
The second part covers reading longer files using the file allocation table, and converts everything into a compact and portable library:
http://codeandlife.com/2012/04/07/simpl ... al-part-2/
The third part introduces the hardware built around ATmega88 and communicating with the SD card using SPI.
http://codeandlife.com/2012/04/25/simpl ... al-part-3/
The last part combines the information in parts 2 and 3 to create a simple FAT16 library for reading any 1-2 GB card.
http://codeandlife.com/2012/04/27/simpl ... al-part-4/
I might also do additional parts covering FAT32, SDHC or file writing, depending on reader interest. All and any feedback is appreciated, if you have some specific things you'd like me to cover in the future, please let me know! |
_________________ http://codeandlife.com
Last edited by jokkebk on Apr 27, 2012 - 08:14 PM; edited 3 times in total
|
| |
|
|
|
|
|
Posted: Apr 04, 2012 - 01:59 AM |
|

Joined: Sep 03, 2005
Posts: 795
Location: Christchurch, NZ
|
|
| Excellent communications skills and very well organised. Look forward to parts 2 and 3. |
|
|
| |
|
|
|
|
|
Posted: Apr 11, 2012 - 10:12 PM |
|

Joined: Apr 07, 2011
Posts: 38
|
|
Thanks! I do like writing tutorials a lot, it's a great way to organize my own thoughts on a given subject.
The part 2 has been published, ending up with a FAT16 library that should be easily ported to AVR (doing that in part 3). I ended up with a memory footprint of 51 bytes instead of the 44 in Petit FatFS. On the other hand, I prefer my version for code readability.  |
_________________ http://codeandlife.com
|
| |
|
|
|
|
|
Posted: Apr 12, 2012 - 04:20 AM |
|


Joined: Dec 11, 2010
Posts: 166
|
|
|
jokkebk wrote:
The part 2 has been published, [...]
FYI, the updated project ZIP file that you reference in part 2 seems to be missing from your website.
Thank you for writing these tutorials. I haven't had a chance yet to thoroughly read them and put them to proper use with some hardware, but playing around with SD cards and the FAT filesystem is on my AVR MCU "to do" list. Your tutorials appear to be a great guide for when I do finally get around to trying that. Nice work!
Regards,
Bill |
|
|
| |
|
|
|
|
|
Posted: Apr 13, 2012 - 09:12 AM |
|

Joined: Apr 07, 2011
Posts: 38
|
|
|
meteor wrote:
jokkebk wrote:
The part 2 has been published, [...]
FYI, the updated project ZIP file that you reference in part 2 seems to be missing from your website.
Whoops, fixed that. And thanks for the kind words!
I'll look in to the hardware part this weekend (I already succeeded in communicating with SD card using bus pirate and setting up ATtiny as a SPI slave so I'm halfway there), so the part 3 might even get finished this Sunday, or at least in the beginning of next week. |
_________________ http://codeandlife.com
|
| |
|
|
|
|
|
Posted: Apr 24, 2012 - 11:41 PM |
|

Joined: Apr 07, 2011
Posts: 38
|
|
| Part 3 is now available. I also suggested readers to post questions to this forum instead of just to my blog, that way there's more audience for both the questions and answers. |
_________________ http://codeandlife.com
|
| |
|
|
|
|
|
Posted: May 02, 2012 - 09:37 AM |
|

Joined: Aug 25, 2010
Posts: 4
|
|
just about all what I need..
thanks dude
very appreciate that... |
|
|
| |
|
|
|
|
|
Posted: May 14, 2012 - 09:43 PM |
|

Joined: Jan 31, 2012
Posts: 10
|
|
Awesome tutorial!
Quote:
I might also do additional parts covering FAT32, SDHC or file writing, depending on reader interest
highly interested!
Currently I am wrapping it into a C++ singleton, but I still have to write the writing routines...
EDIT: I totally forgot to say that I was able to write too a long time ago with the help of this and the Bus Pirate tutorial. |
|
|
| |
|
|
|
|
|
Posted: Jun 23, 2012 - 11:01 AM |
|

Joined: Oct 30, 2010
Posts: 49
|
|
This is a great tutorial ! I agree, most of the resources available for interfacing SD cards are just instructions to get the FatFS libraries working.
Thanks a lot for this!
(I also love the way the tut is presented) |
|
|
| |
|
|
|
|
|
Posted: Feb 07, 2013 - 07:02 AM |
|

Joined: Jan 27, 2012
Posts: 66
|
|
There is now a fourth part at
http://codeandlife.com/2012/04/27/simpl ... al-part-4/
however it is still sector based.
Does anyone know of something a litle higher level?
Specifically, I want to
CheckFileExists
OpenFile
fgets() // read text lines, each lind ending with \n
CloseFil
I also want to be able to write text files a line at a time ....
all of the standard fopen(), fgetc() fgets(), fprintf(), fclose() stuff
If you can help, please reply to my thread at http://www.avrfreaks.net/index.php?name ... p;t=125733
menawhile, I will continue to google |
|
|
| |
|
|
|
|
|
Posted: Feb 07, 2013 - 08:08 AM |
|

Joined: Apr 07, 2011
Posts: 38
|
|
|
jamawg wrote:
There is now a fourth part at
http://codeandlife.com/2012/04/27/simpl ... al-part-4/
however it is still sector based.
Does anyone know of something a litle higher level?
Specifically, I want to
CheckFileExists
OpenFile
fgets() // read text lines, each lind ending with \n
CloseFil
Short answer also here, in hopes that it will help other readers of the tutorial...
The reading part of your question should be trivial to implement. The fat16_open_file() returns error value if file is not found, so CheckFileExists is almost 1:1 covered (you can make a wrapper function if you don't mind calling the exact same function again after you've made sure that file exists). Of course it doesn't understand complete paths (say CheckFileExists("some_dir/subdir/somefile.txt"), but writing a recursive function to handle those is quite easy.
Separate fopen() and fclose() are not needed because your AVR has no concept of file handles unless you implement them yourself. Only reason to do that would be so that you can have multiple files open - in that case you're right and some refactoring of the code would be needed (make a file handle structure to store sector and offset of open file, and create reading function that uses the data in handle instead of global library variables).
fgets() is basically reading the file byte at a time, and stopping when you get to the end or encounter '\n':
Code:
char line[20];
int len = 0;
while(len<20 && fat16_state.file_left) {
if(fat16_read_file(1)) {
line[len++] = *fat16_buffer;
if(*fat16_buffer == '\n')
break;
} else break; // EOF or some other error
}
if(len >= 20) // too long line
deal_with_error();
else
line[len] = NULL; // terminate string
Quite easy, right? Note that most AVRs don't have enough memory to read long lines of text at one go. Due to memory limitations, higher level file functions are usually not feasible.
I might write an additional tutorial on writing files this month.
Joonas |
_________________ http://codeandlife.com
|
| |
|
|
|
|
|
Posted: Feb 07, 2013 - 11:40 AM |
|

Joined: Jan 27, 2012
Posts: 66
|
|
Thank you *very* mcuh for such a quick response - and for the code sample.
Glad that FileExists() is so simple.
I am new to such smallprocessors, though I can appreciate what you are saying. Personally, I use a UC3 which is 32-bit so things might be easier for me. But I guess that I won't find an existign library to make things look just liek a larger processor and standard C system calls(?)
Just to show you my mindset, I was wanting to process an INI file. I can see that I might have to rethink that ...
I look forward to the next part of your tutorial. |
|
|
| |
|
|
|
|
|
Posted: Feb 07, 2013 - 03:43 PM |
|


Joined: Jul 18, 2005
Posts: 62230
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
Quote:
But I guess that I won't find an existign library to make things look just liek a larger processor and standard C system calls(?)
Eh? Surely FatFs does exactly that? |
_________________
|
| |
|
|
|
|
|
Posted: Feb 08, 2013 - 02:44 AM |
|

Joined: Jan 27, 2012
Posts: 66
|
|
|
clawson wrote:
Quote:
But I guess that I won't find an existing library to make things look just like a larger processor and standard C system calls(?)
Eh? Surely FatFs does exactly that?
Thanks very much for replying. Obviously, I am missing something.
Sorry to be such a n00b, but can you point me at the FatFs you speak of? With a URL?
All that I could find was http://asf.atmel.com/docs/latest/uc3a/h ... __fat.html
Is that the one? Can I use it open a file? To get a line with fgets() or even fscanf() & similar for writing? It doesn't seem so.
I am sure that this is do-able, but can't find where. This is my first Atmel project - I guess we all have to start somewhere.
Thanks again for any help. |
|
|
| |
|
|
|
|
|
Posted: Feb 08, 2013 - 04:00 AM |
|


Joined: Aug 01, 2004
Posts: 39
Location: Orange County, California
|
|
|
|
|
|
|
Posted: Feb 08, 2013 - 11:22 AM |
|


Joined: Jul 18, 2005
Posts: 62230
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
| I only said "fatfs" because I know for a fact that if you type nothing but that one word into Google it's the top hit. |
_________________
|
| |
|
|
|
|
|