<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>sd-reader: MMC/SD card example application</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.5.3-20071008 -->
<div class="tabs">
  <ul>
    <li class="current"><a href="index.html"><span>Main&nbsp;Page</span></a></li>
    <li><a href="modules.html"><span>Modules</span></a></li>
    <li><a href="annotated.html"><span>Data&nbsp;Structures</span></a></li>
    <li><a href="files.html"><span>Files</span></a></li>
  </ul>
</div>
<h1>MMC/SD card example application</h1>
<p>
This project is a small test application which implements read and write support for MMC and SD cards. It includes<ul>
<li>low-level <a class="el" href="group__sd__raw.html">MMC read/write routines </a></li><li><a class="el" href="group__partition.html">partition table support </a></li><li>a simple <a class="el" href="group__fat16.html">FAT16 read/write implementation </a></li></ul>
<h2><a class="anchor" name="circuit">
The circuit</a></h2>
The curcuit board is a self-made and self-soldered board consisting of a single copper layer and standard DIL components, except of the MMC/SD card connector.<p>
The connector is soldered to the bottom side of the board. It has a simple eject button which, when a card is inserted, needs some space beyond the connector itself. As an additional feature the connector has two electrical switches to detect wether a card is inserted and wether this card is write-protected.<p>
I used two microcontrollers during development, the Atmel ATmega8 with 8kBytes of flash, and its pin-compatible alternative, the ATmega168 with 16kBytes flash. The first one is the one I started with, but when I implemented FAT16 write support, I ran out of flash space and switched to the ATmega168.<h2><a class="anchor" name="pictures">
Pictures</a></h2>
<div align="center">
<img src="pic01.jpg" alt="pic01.jpg">
<p><strong>The circuit board used to implement and test this application.</strong></p></div>
 <div align="center">
<img src="pic02.jpg" alt="pic02.jpg">
<p><strong>The MMC/SD card connector on the soldering side of the circuit board.</strong></p></div>
 <h2><a class="anchor" name="software">
The software</a></h2>
The software is written in pure standard ANSI-C. Sure, it might not be the smallest or the fastest one, but I think it is quite flexible.<p>
I implemented a simple command prompt which is accessible via the UART at 9600 Baud. With commands similiar to the Unix shell you can browse different directories, read and write files, create new ones and delete them again. Not all commands are available in all software configurations.<ul>
<li><code>cat &lt;file&gt;</code><br>
 Writes a hexdump of &lt;file&gt; to the terminal.</li><li><code>cd &lt;directory&gt;</code><br>
 Changes current working directory to &lt;directory&gt;.</li><li><code>disk</code><br>
 Shows card manufacturer, status, filesystem capacity and free storage space.</li><li><code>ls</code><br>
 Shows the content of the current directory.</li><li><code>mkdir &lt;directory&gt;</code><br>
 Creates a directory called &lt;directory&gt;.</li><li><code>rm &lt;file&gt;</code><br>
 Deletes &lt;file&gt;.</li><li><code>sync</code><br>
 Ensures all buffered data is written to the card.</li><li><code>touch &lt;file&gt;</code><br>
 Creates &lt;file&gt;.</li><li><code>write &lt;file&gt; &lt;offset&gt;</code><br>
 Writes text to &lt;file&gt;, starting from &lt;offset&gt;. The text is read from the UART, line by line. Finish with an empty line.</li></ul>
<p>
 
 <p>
 The following table shows some typical code sizes in bytes, using the 20061101 release with malloc()/free():
 </p>

 <table border="1" cellpadding="2">
     <tr>
         <th>layer</th>
         <th>code size</th>
         <th>static RAM usage</th>
     </tr>
     <tr>
         <td>MMC/SD (read-only)</td>
         <td align="right">1576</td>
         <td align="right">0</td>
     </tr>
     <tr>
         <td>MMC/SD (read-write)</td>
         <td align="right">2202</td>
         <td align="right">517</td>
     </tr>
     <tr>
         <td>Partition</td>
         <td align="right">418</td>
         <td align="right">0</td>
     </tr>
     <tr>
         <td>FAT16 (read-only)</td>
         <td align="right">3834</td>
         <td align="right">0</td>
     </tr>
     <tr>
         <td>FAT16 (read-write)</td>
         <td align="right">7932</td>
         <td align="right">0</td>
     </tr>
 </table>

 <p>
 The static RAM in the read-write case is used for buffering memory card
 access. Without this buffer, implementation would have been much more complicated.
 </p>
 
 <p>
 Please note that the numbers above do not include the C library functions
 used, e.g. malloc()/free() and some string functions. These will raise the
 numbers somewhat if they are not already used in other program parts.
 </p>
 
 <p>
 When opening a partition, filesystem, file or directory, a little amount
 of dynamic RAM is used, as listed in the following table. Alternatively,
 the same amount of static RAM can be used.
 </p>

 <table border="1" cellpadding="2">
     <tr>
         <th>descriptor</th>
         <th>dynamic/static RAM</th>
     </tr>
     <tr>
         <td>partition</td>
         <td align="right">17</td>
     </tr>
     <tr>
         <td>filesystem</td>
         <td align="right">26</td>
     </tr>
     <tr>
         <td>file</td>
         <td align="right">51</td>
     </tr>
     <tr>
         <td>directory</td>
         <td align="right">47</td>
     </tr>
 </table>
 
 <h2><a class="anchor" name="adaptation">
Adapting the software to your needs</a></h2>
The only hardware dependent part is the communication layer talking to the memory card. The other parts like partition table and FAT16 support are completely independent, you could use them even for managing Compact Flash cards or standard ATAPI hard disks.<p>
By changing the MCU* variables in the Makefile, you can use other Atmel microcontrollers or different clock speeds. You might also want to change the configuration defines in the files <a class="el" href="fat16__config_8h.html" title="FAT16 configuration (license: GPLv2 or LGPLv2.1).">fat16_config.h</a>, <a class="el" href="partition__config_8h.html" title="Partition configuration (license: GPLv2 or LGPLv2.1).">partition_config.h</a>, <a class="el" href="sd__raw__config_8h.html" title="MMC/SD support configuration (license: GPLv2 or LGPLv2.1).">sd_raw_config.h</a> and <a class="el" href="sd-reader__config_8h.html" title="Common sd-reader configuration used by all modules (license: GPLv2 or LGPLv2.1).">sd-reader_config.h</a>. For example, you could disable write support completely if you only need read support.<h2><a class="anchor" name="bugs">
Bugs or comments?</a></h2>
If you have comments or found a bug in the software - there might be some of them - you may contact me per mail at <a href="mailto:feedback@roland-riegel.de">feedback@roland-riegel.de</a>.<h2><a class="anchor" name="acknowledgements">
Acknowledgements</a></h2>
Thanks go to Ulrich Radig, who explained on his homepage how to interface MMC cards to the Atmel microcontroller (<a href="http://www.ulrichradig.de/">http://www.ulrichradig.de/</a>). I adapted his work for my circuit. Although this is a very simple solution, I had no problems using it.<h2><a class="anchor" name="copyright">
Copyright 2006-2007 by Roland Riegel</a></h2>
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation (<a href="http://www.gnu.org/copyleft/gpl.html">http://www.gnu.org/copyleft/gpl.html</a>). At your option, you can alternatively redistribute and/or modify the following files under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation (<a href="http://www.gnu.org/copyleft/lgpl.html">http://www.gnu.org/copyleft/lgpl.html</a>):<ul>
<li><a class="el" href="fat16_8c.html" title="FAT16 implementation (license: GPLv2 or LGPLv2.1).">fat16.c</a></li><li><a class="el" href="fat16_8h.html" title="FAT16 header (license: GPLv2 or LGPLv2.1).">fat16.h</a></li><li><a class="el" href="fat16__config_8h.html" title="FAT16 configuration (license: GPLv2 or LGPLv2.1).">fat16_config.h</a></li><li><a class="el" href="partition_8c.html" title="Partition table implementation (license: GPLv2 or LGPLv2.1).">partition.c</a></li><li><a class="el" href="partition_8h.html" title="Partition table header (license: GPLv2 or LGPLv2.1).">partition.h</a></li><li><a class="el" href="partition__config_8h.html" title="Partition configuration (license: GPLv2 or LGPLv2.1).">partition_config.h</a></li><li><a class="el" href="sd__raw_8c.html" title="MMC/SD raw access implementation (license: GPLv2 or LGPLv2.1).">sd_raw.c</a></li><li><a class="el" href="sd__raw_8h.html" title="MMC/SD raw access header (license: GPLv2 or LGPLv2.1).">sd_raw.h</a></li><li><a class="el" href="sd__raw__config_8h.html" title="MMC/SD support configuration (license: GPLv2 or LGPLv2.1).">sd_raw_config.h</a></li><li><a class="el" href="sd-reader__config_8h.html" title="Common sd-reader configuration used by all modules (license: GPLv2 or LGPLv2.1).">sd-reader_config.h</a> </li></ul>
<hr size="1"><address style="text-align: right;"><small>Generated on Thu Dec 13 19:38:48 2007 for sd-reader by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.3-20071008 </small></address>
</body>
</html>