This section describes how to implement Efsl on a AVR $\mu C$ connected to
an SD-Card (SPI). For getting efsl to compile, the avr-gcc compiler and 
avr-libc library are required. On Windows you should install WinAVR 
(http://winavr.sourceforge.net/), on Linux you can install the packages 
separately (see http://www.nongnu.org/avr-libc/user-manual/install\_tools.html
for a nice howto).
\subsubsection{Hardware}
First, you need set up a prototype in which you connect the CD, CMD, DAT0
\& CLK lines from the SD-Card to /CS, MOSI, MISO \& SCK from the Atmega.
\newline
\includegraphics[scale=0.65]{pics/sdcard.eps}
\newline
%\parbox[c]{.4\textwidth}{\begin{center}\includegraphics[width=.4\textwidth]{pics/sdconnection}\end{center}}
\parbox[c]{.5\textwidth}{
Connect the following lines on the SD-card:
\begin{itemize}
	\item{Pin 9 (DAT2) - NC\\(or pull-up to 3.3V)}
	\item{Pin 1 (CD) - Any pin on the Atmega128}
	\item{Pin 2 (CMD) - MOSI\\(pin 12 on the Atmega128)}
	\item{Pin 3 (Vss) - GND}
	\item{Pin 4 (Vdd) - +3.3V}
	\item{Pin 5 (CLK) - SCK\\(pin 11 on the Atmega128)}
	\item{Pin 6 (Vss) - GND}
	\item{Pin 7 (DAT0) - MISO\\(pin 12 on the Atmega128)}
	\item{Pin 8 (DAT1) - NC\\(or pull-up to 3.3V)}
\end{itemize}
}
\parbox[c]{.5\textwidth}{\begin{center}
	\includegraphics[width=.5\textwidth]{pics/sdconnection}
	\newline\newline
	Remark: this schematic includes pull-up's to 3.3V, which
	can be left off.
\end{center}}
\newline
Remark 1: Make sure that your $\mu C$ is running on 3,3V, so you don't
damage your SD-Card.\newline
\newline
Remark 2: CD is currently static set to PB0, but will become variable
in future releases.
\subsubsection{Download \& Compile}
Let's get started:
\begin{enumerate}
	\item{Get the latest release of efsl on http://www.sf.net/projects/efsl/}
	\item{Unpack the library (on Windows, you can use WinACE or WinRAR)}
	\item{Copy in directory \filename{conf} the file 
		\filename{config-avr.h} to \filename{config.h}}
	\item{Copy in directory \filename{conf} the file 
		\filename{config-avr.makefile} to \filename{config.makefile}}
	\item{Compile the library (\code{make avr})}
\end{enumerate}
Now you should have the following files in a directory called {lib}:
\begin{itemize}
	\item{\filename{libefsl-base.a}}
	\item{\filename{libefsl-fs-vfat.a}}
	\item{\filename{libefsl-prot-sdspi.a}}
	\item{\filename{libefsl-hwd-atmega\_spi.a}}
\end{itemize}
\subsubsection{Example}
Since Efsl itself is only a library, it's not supposed to do anything out of 
the box, than just compile. To get started, we'll show here a small example
program that opens an existing file and writes the content to a new file.
\newline\newline
First, create a new directory in which you put the compiled efsl-library 
(\filename{libefsl.a}) and create a new file called \filename{avrtest.c} containing:
\lstset{numbers=left, stepnumber=1, numberstyle=\small, numbersep=5pt, tabsize=4}
\begin{lstlisting}
	#include <efs.h>
	#include <sd.h>
	#include <atmega_spi.h>

	void hang(void);

	void main(void)
	{
		efsl_storage_conf storage_conf;
		efsl_fs_conf fs_conf;

		efsl_storage storage;
		efsl_fs fs;
		File file_r;
		File file_w;

		atmegaSpiInterface spi_interface;
		SdSpiProtocol sd_protocol;

		char buf[512];
		unsigned short e;

		/* Init */
		spi_interface.pinSelect=0x01;

		sd_protocol.spiHwInterface=&spi_interface;
		sd_protocol.spiHwInit=(void *)atmega_spi_init;
		sd_protocol.spiSendByte=(void *)atmega_spi_send;

		storage_conf.hwObject=&sd_protocol;
		storage_conf.if_init_fptr=(void *)sd_Init;
		storage_conf.if_read_fptr=(void *)sd_readSector;
		storage_conf.if_write_fptr=(void *)sd_writeSector;
		storage_conf.if_ioctl_fptr=(void *)sd_ioctl;
		storage_conf.ioman_bufmem=0;

		fs_conf.no_partitions=0;
		fs_conf.storage=&storage;

		if(efsl_initStorage(&storage,&storage_conf)){
			hang();
		}

		if(efsl_initFs(&fs,&fs_conf)){
			hang();
		}

		if(file_fopen(&file_r,&fs.filesystem,"orig.txt",'r')!=0){
			hang();
		}

		if(file_fopen(&file_w,&fs.filesystem,"copy.txt",'w')!=0){
			hang();
		}
		
		if(file_fopen(&file_r,&efs.myFs,"orig.txt",'r')!=0){
			hang();
		}

		while((e=file_read(&file_r,512,buf))){
			file_write(&file_w,e,buf);
		}

		file_fclose(&file_r);
		file_fclose(&file_w);

		fs_umount(&fs.filesystem);

		hang();
	}

	void hang(void)
	{
		while((1))
			_NOP();
	}
\end{lstlisting}
$ $\newline
Some extra information on the code above: TODO
%\begin{itemize}
%	\item{Line 1: The header file for efsl is included here. When using the
%		basic efsl functions, \filename{efs.h} is the only header file on the 
%		efsl library that needs to be included.}
%	\item{Line 7: The object efs is created, this object will contain
%		information about the hardware layer, the partition table and
%		the disc.}
%	\item{Line 8: The objects \code{file\_r} and \code{file\_w} are created, these objects 
%		will contain information about the files that we will open on the 
%		efs-object.}
%	\item{Line 9: A buffer of 512 bytes is allocated. This buffer will be
%		used for reading and writing blocks of data.}
%	\item{Line 12: Call of \code{efs\_init()}, which will initialize the efs-object.
%		To this function we pass:
%		\begin{enumerate}
%			\item{A pointer to the efs-object.}
%			\item{A pointer to the file that contains the partition table /
%				file system (in this example, we select a device as file).}
%		\end{enumerate}
%		If this function returns 0, it means that a valid fat partition is
%		found on the SD-card connected.
%		If no valid fat-filesystem is found, or the file does not exist, the
%		function returns a negative value. In this example we then go to an
%		infinite loop to prevent the program to continue.}
%	\item{Line 16 \& 20: Call of \code{file\_fopen()}, which will initialize the
%		file-objects. To this function we pass:
%		\begin{enumerate}
%			\item{A pointer to the file-object.}
%			\item{A pointer to the filesystem-object.}
%			\item{A pointer to the filename.}
%			\item{A char containing the the mode (read, write, append).}
%		\end{enumerate}
%		If this function returns 0, it means the file has successfully been
%		opened for reading / writing / appending.
%	 	If the file could not be opened (because for example a file already 
%		exists), a negative value is returned.}
%	\item{Line 24: Call of \code{file\_read()}, which will read a given value of
%		bytes (in this example 512) from a file and put it's content into
%		the buffer passed (in this example called buf). This function returns
%		the amount of bytes read, so the while-loop will be executed as long
%		as there are bytes left in the file.}
%	\item{Line 25: Call of \code{file\_write()}, which will write a given value
%		of bytes (in this example, the amount of bytes that was read
%		by \code{file\_read()}) from the buffer passed to a file. This function returns
%		the amount of bytes written.}
%	\item{Line 28 \& 29: Call of \code{file\_fclose()}, which will close the
%		file-objects.}
%	\item{Line 31: Call of \code{fs\_umount()}, which will write all buffers to
%		the the SD-card.}
%\end{itemize}
\subsubsection{Testing}
So now let's test the program:
\begin{enumerate}
	\item
	{	Compile the program:
		\begin{itemize}
			\item{On Linux (with avr-gcc): avr-gcc -I/home/user/src/base/include -I/home/user/src/include -I/home/user/src/fs/vfat/include -I/home/user/src/hwdrivers/atmega\_spi/include -I/home/user/src/protocols/sdcard\_spi/include -I/home/user/conf -ffreestanding -mmcu=atmega128 -Os -o avrtest.o avrtest.c -L/home/user/lib -lefsl-base -lefsl-fs-vfat -lefsl-hwd-atmega\_spi -lefsl-prot-sdspi}
			\item{On Windows (with WinAVR): replace all slashes with backslashes} 
		\end{itemize}
	}
	\item{Generate a hexfile 
		(avr-objcopy -j .text -j .data -O ihex avrtest.o avrtest.hex)}
	\item{Connect an SD-card to your Atmega128 with a file called 
		\filename{orig.txt} on it.}
	\item
	{
		Flash the hex file into your $\mu C$.
		\begin{itemize}
			\item{On Linux: avrdude -P /dev/ttyUSB0 -c stk500 -p m128 -Uflash:w:avrtest.hex}
			\item{On Windows: use Atmel AVR-Studio}
		\end{itemize}
	}
	\item{Reset your $\mu C$ and wait some time (depending on how big
		the file \filename{orig.txt} is).}
	\item{Disconnect the SD-card, so you can put it in your card reader
		and find out if the file \filename{orig.txt} is copied to 
		\filename{copy.txt}.}
\end{enumerate}
