#LyX 1.4.3 created this file. For more info see http://www.lyx.org/ \lyxformat 245 \begin_document \begin_header \textclass book \language english \inputencoding auto \fontscheme default \graphics default \paperfontsize default \spacing single \papersize default \use_geometry false \use_amsmath 1 \cite_engine basic \use_bibtopic false \paperorientation portrait \secnumdepth 3 \tocdepth 3 \paragraph_separation indent \defskip medskip \quotes_language english \papercolumns 1 \papersides 1 \paperpagestyle default \tracking_changes false \output_changes false \end_header \begin_body \begin_layout Title Hardware Hacking with GNU/Linux and Phoenix-MDK \end_layout \begin_layout Author Pramode C.E \end_layout \begin_layout Standard \begin_inset LatexCommand \tableofcontents{} \end_inset \end_layout \begin_layout Chapter* Preface \end_layout \begin_layout Standard This book is a `work\InsetSpace ~ in\InsetSpace ~ progress'! \end_layout \begin_layout Standard Part 1 will get you started with the Debian GNU/Linux programming environment. Many of the ideas covered are generic and apply equally well to most other GNU/Linux distributions. If you are really impatient, you can dive straight into part 2! \end_layout \begin_layout Standard In Part 2, we shall look at how to conduct experiments with the Phoenix MDK (Microcontroller Development kit). The MDK and associated software tools (a GNU/Linux Live CD, a library of routines) will help you to get started with hardware hacking effortlessly. \end_layout \begin_layout Standard The Phoenix MDK library is designed in such a way that the user need not be aware of the complexities involved in programming the underlying microcontro ller (an Atmel ATMega16). For example, by invoking a simple C function and passing it certain parameters, the user will be able to generate a square waveform on a particular pin of the controller. Understanding how these functions are implemented will go a long way towards helping the user craft new routines for his own special requirements; that is the objective of the third part of this book. \end_layout \begin_layout Standard The Debian GNU/Linux system is packed with plenty of high quality tools of great utility to the hardware hacker - examples include mathematical tools like Octave and schematic entry and PCB design tools like gEDA. The last part of this book will cover a few of these tools. \end_layout \begin_layout Part Debian GNU/Linux \end_layout \begin_layout Chapter Introduction to Debian GNU/Linux \end_layout \begin_layout Standard If your only previous experience is with proprietary operating systems \begin_inset Foot status collapsed \begin_layout Standard Which come packaged with such cutting-edge applications like card games and paint programs and with doors and windows always open for wild creatures like viruses, worms and trojan horses! \end_layout \end_inset , you will find Debian\InsetSpace ~ GNU/Linux to be a breath of fresh air! The three DVD's which constitute it's latest release contain literally thousands of programs for doing everything from wordprocessing to electronic circuit design, PCB fabrication and complex mathematical computations. It is in fact a huge collection of the very best which the Free Software world can offer! \end_layout \begin_layout Standard This chapter gets you quickly on your feet installing and using Debian. Whether you are a hobbyist, student or Engineer, the one thing that differentia tes you from an ordinary user is the fact that you really wish to understand what is going on in your machine. This book doesn't try to hide the complexity of the Operating System by showing you pretty GUI's - instead, the idea is to help you master that complexity! \end_layout \begin_layout Section Philosophy of Free Software \end_layout \begin_layout Standard Technology doesn't exist in a vaccum - it has great social, ethical, economic, political and environmental implications. The GNU/Linux operating system is much more than a technological achievement - new users should spend some time to understand things like what the \begin_inset Quotes eld \end_inset Free \begin_inset Quotes erd \end_inset in Free Software really stands for, how Free Software is different from Open Source and so on. The best place to get started is: \end_layout \begin_layout Quotation http://www.gnu.org/philosophy/philosophy.html \end_layout \begin_layout Section The GNU/Linux command line interface \end_layout \begin_layout Standard You should have a working GNU/Linux system (preferably Debian - but some other distribution will also do) to get the most out of this book. If not, you can follow the instructions given in the Appendix and set up Debian on your own (it is highly recommended that you do it at least once). Once Debian boots up, you will see a graphical login screen. \end_layout \begin_layout Standard Graphical User Interfaces are undoubtedy a great boon - but if you wish to get the real `feel' of the system, you should be prepared to get acquainted with the `classical' text mode interface. And, unlike what some people might tell you, the command line interface is NOT the epitome of complexity - it is in fact simple and amazingly well designed. \end_layout \begin_layout Standard But how do you get into this text mode? Type Ctrl-Alt-F1 and there you are \begin_inset Foot status collapsed \begin_layout Standard You can go back to graphics mode any time by typing Alt-F7 \end_layout \end_inset ! \end_layout \begin_layout Subsection The GNU/Linux prompt \end_layout \begin_layout Standard You need a login name and password to enter into the system. If the login is succesfull, you will see the famous GNU/Linux prompt (assuming that you are logging in as user `guest' on a machine whose name is `debian'): \end_layout \begin_layout LyX-Code guest@debian:~$ \end_layout \begin_layout Standard This is where you will be entering commands. Note that the prompt gives you information like your login name and machine name. \end_layout \begin_layout Standard Try running one or two simple commands: \end_layout \begin_layout LyX-Code guest@debian:~$ echo hello \end_layout \begin_layout LyX-Code hello \end_layout \begin_layout LyX-Code guest@debian:~$ date \end_layout \begin_layout LyX-Code Thu Feb 7 20:52:11 IST 2008 \end_layout \begin_layout LyX-Code guest@debian:~$ \end_layout \begin_layout Subsection File System Tour \end_layout \begin_layout Standard A typical GNU/Linux installation will have tens of thousands of files spread out over hundreds of nested directories (folders). The very first step in understanding your system is getting a general idea of the layout of these files and directories. \end_layout \begin_layout Subsubsection The root directory \end_layout \begin_layout Standard The `cd' command can be used to move from one directory to another. The `root\InsetSpace ~ directory' (denoted by the symbol `/') is the where the heirarchy begins - you can do a `cd' and move into the root directory: \end_layout \begin_layout LyX-Code guest@debian:~$ cd / \end_layout \begin_layout LyX-Code guest@debian:/$ pwd \end_layout \begin_layout LyX-Code / \end_layout \begin_layout LyX-Code guest@debian:/$ \end_layout \begin_layout Standard Note that the prompt has changed - it now displays a `/' after the ':'. The `pwd' command prints the current directory, which is /. \end_layout \begin_layout Standard The `ls' (list) command displays contents of a directory. Here are some of the names which you might see if you run `ls' in the root directory: \end_layout \begin_layout Quotation bin boot cdrom etc dev lost+found sbin \end_layout \begin_layout Standard How do you differentiate between files and directories? By running the `ls' command with the option `-l' (ie, you type \emph on `ls\InsetSpace ~ \InsetSpace ~ -l \emph default '). This option gives you a few other details regarding a file - like its size, timestamp, permissions etc. If the entry corresponding to the file which you are looking for starts with the character `d', you can take it to be a directory; if you see a `-' sign as the first character, it is an ordinary file. Because a `file' is a rather complex entity, you might see a few symbols other than these two - the exact meaning of these symbols will be explained later. \end_layout \begin_layout Standard Most of the names in the root directory represent directories - it's a good idea to get into these folders and understand the kind of data they store. \end_layout \begin_layout Subsubsection The directories /bin and /sbin \end_layout \begin_layout Standard Assuming that you are currently under the `root' directory, execute the following command: \end_layout \begin_layout LyX-Code guest@debian:/$ cd bin \end_layout \begin_layout LyX-Code guest@debian:/bin$ \end_layout \begin_layout Standard You are now in the folder `/bin' \begin_inset Foot status collapsed \begin_layout Standard What we mean by `/bin' is - the folder bin under the root directory. \end_layout \end_inset . You will see plenty of files when you do an `ls' (almost all of them would be files - you can verify by running `ls\InsetSpace ~ \InsetSpace ~ -l'). What kind of data do these files contain? We can find out by using the `file' command: \end_layout \begin_layout LyX-Code guest@debian:/bin$ file chown \end_layout \begin_layout LyX-Code chown: ELF 32-bit LSB executable, Intel 80386, \end_layout \begin_layout LyX-Code version 1 (SYSV), for GNU/Linux 2.4.1, \end_layout \begin_layout LyX-Code dynamically linked (uses shared libs), \end_layout \begin_layout LyX-Code for GNU/Linux 2.4.1, stripped \end_layout \begin_layout Standard One thing is clear, `chown' is an executable file - and it contains machine code which runs on an Intel 80386 compatible processor. \end_layout \begin_layout Standard All the files under /bin contain executable machine code of standard GNU/Linux commands. The file `ls' contains the machine code for the command `ls' and the file `chown' contains machine code for the `chown' command. \end_layout \begin_layout Standard /sbin is similar - only difference is that most of the commands under this folder (like say `rmmod') are needed only by the system administrator. \end_layout \begin_layout Subsubsection /etc \end_layout \begin_layout Standard One of the files seen under /etc is `passwd'. Each line of this file contains information about a particular user on the system. Type: \end_layout \begin_layout LyX-Code guest@debian:/etc$ man 5 passwd \end_layout \begin_layout Standard to get more information about the structure of this file. \end_layout \begin_layout Standard Another very important file under /etc is `inittab'. When a GNU/Linux system boots up, lots of programs are started automatically. Deciding which all programs to start depends on what is called a `run level'. The default run level will be specified in this file. Type `man\InsetSpace ~ 5\InsetSpace ~ inittab' to know more about the structure of this file. \end_layout \begin_layout Standard In general, /etc contains configuration files stored in plain ASCII form \begin_inset Foot status collapsed \begin_layout Standard Some propreitary systems prefer to store configuration data in binary form. This is a bad idea. Text files are better than binary files - you can use a whole lot of standard text processing utilities to manipulate these files. \end_layout \end_inset . \end_layout \begin_layout Subsubsection /boot \end_layout \begin_layout Standard This folder usually contains the machine code of the OS kernel and a few other related files. \end_layout \begin_layout Subsubsection /dev \end_layout \begin_layout Section Using the `nano' editor \end_layout \begin_layout Standard GNU\InsetSpace ~ Nano is an easy to use text editor. You can invoke it by simply typing `nano' at the command prompt. \end_layout \begin_layout Standard Enter a few lines of text and save it by typing `Ctrl-O' (WriteOut). Nano will ask you for a file name. Ctrl-X takes you out of Nano - there are many other `Ctrl' commands - Nano displays a list at the bottom of the screen. \end_layout \begin_layout Chapter The GNU/Linux development environment \end_layout \begin_layout Section Compiling C programs \end_layout \begin_layout Standard A C program is compiled by invoking the GNU C Compiler \begin_inset Foot status collapsed \begin_layout Standard It might be better to say that GCC stands for the GNU Compiler Collection \end_layout \end_inset - gcc. The resuting executable will be stored in a file `a.out' which you can execute by simply typing its name: \end_layout \begin_layout LyX-Code guest@debian:~$ cc hello.c \end_layout \begin_layout LyX-Code guest@debian:~$ ./a.out \end_layout \begin_layout LyX-Code hello, world \end_layout \begin_layout LyX-Code guest@debian \end_layout \begin_layout Standard Compilation is a complex 4 stage process - here is a brief description of the stages. The file being compiled contains the following code: \end_layout \begin_layout LyX-Code #define MAX 10 \end_layout \begin_layout LyX-Code main() \end_layout \begin_layout LyX-Code { \end_layout \begin_layout LyX-Code int i = MAX; \end_layout \begin_layout LyX-Code } \end_layout \begin_layout Subsection Preprocessing \end_layout \begin_layout Subsection Compilation Proper \end_layout \begin_layout Subsection Assembling \end_layout \begin_layout Subsection Linking \end_layout \begin_layout LyX-Code \end_layout \begin_layout Section Debugging C programs \end_layout \begin_layout Standard [To be written] \end_layout \begin_layout Section Building a program from source \end_layout \begin_layout Standard [To be written] \end_layout \begin_layout Section Elementary shell scripting \end_layout \begin_layout Standard [To be written] \end_layout \begin_layout Section Scripting in Python \end_layout \begin_layout Standard [To be written] \end_layout \begin_layout Part The Phoenix MDK \end_layout \begin_layout Chapter Quickstart \end_layout \begin_layout Standard Phoenix\InsetSpace ~ MDK is a complete GNU/Linux based platform for rapid prototyping of microcontroller applications. The hardware part consists of a small `main\InsetSpace ~ board' carrying an ATMega16 microcontroller from Atmel and several small `daughter\InsetSpace ~ boards' which can be used for things like serial communication (with the PC), amplifying electrical signals, interfacing with small DC motors etc. The software part consists of a bootable \begin_inset Quotes eld \end_inset Live \begin_inset Quotes erd \end_inset CD containing Debian GNU/Linux, a software library which simplifies microcontro ller programming significantly and a collection of tools and utilities which will be of great use to the hardware hacker. \end_layout \begin_layout Standard The hardware design, including schematics and PCB layout are freely available from the project home page. All the PCB's are single sided and have been carefully designed so as to make it easy for even inexperienced users to solder the parts himself, if he so desires. The software library is licensed under GPLv3. \end_layout \begin_layout Section Hardware \end_layout \begin_layout Subsection The main board \end_layout \begin_layout Standard Figure \begin_inset LatexCommand \ref{fig:The-Phoenix-MDK-main} \end_inset shows the layout of the Phoenix-MDK main board. You can see the Atmega16 controller at the centre. There are sockets marked `Digital\InsetSpace ~ I/O' and `Analog\InsetSpace ~ I/O' \begin_inset Foot status collapsed \begin_layout Standard You will also see alternate markings PORTA, PORTB, PORTC, PORTD - the next part of this book will explain what these are. \end_layout \end_inset . You will note that the sockets are numbered on both sides - on one side, the numbering is uniformly zero to seven - this can be ignored for the moment. There are three sets of sockets for Digital I/O (marked 0 to 4, 5 to 12 and 13 to 20) and one set for Analog I/O (marked 0 to 7). The Digital I/O lines can be made HIGH (5V) or LOW (0V) by invoking a function from a C program (that is, the lines act as `output'). It is also possible to check the logic levels on these pins, again by calling some C function (ie, the lines act as `inputs'). \end_layout \begin_layout Standard \begin_inset Float figure wide false sideways false status open \begin_layout Caption The Phoenix-MDK main board \end_layout \begin_layout Standard \begin_inset LatexCommand \label{fig:The-Phoenix-MDK-main} \end_inset \end_layout \begin_layout Standard \begin_inset Graphics filename mega16_silk.eps \end_inset \end_layout \begin_layout Standard \end_layout \end_inset \end_layout \begin_layout Standard The `Analog\InsetSpace ~ I/O' lines are connected to the ADC (Analog to digital converter) unit within the ATMega16 microcontroller. They can be used to measure analog voltages (more on this later). \end_layout \begin_layout Standard The board has a small switch adjacent to the analog I/O lines which can be used to reset the microcontroller. \end_layout \begin_layout Standard There is a 16 pin LCD connector to which we can attach small text LCD units. \end_layout \begin_layout Standard The unit can be powered by an external DC 9V supply (which can even be a 9V battery). \end_layout \begin_layout Standard \begin_inset Float figure wide false sideways false status open \begin_layout Caption Phoenix MDK Main board photo \end_layout \begin_layout Standard \begin_inset Graphics filename phmdk.eps \end_inset \end_layout \end_inset \end_layout \begin_layout Subsection The daughter boards \end_layout \begin_layout Standard The functionality of Phoenix-MDK is enhanced by several `daughter\InsetSpace ~ boards'. The following boards are currently available: \end_layout \begin_layout Enumerate A MAX232 based serial port interface (Figure \begin_inset LatexCommand \ref{fig:Connection-MAX232-daughter} \end_inset ) \end_layout \begin_layout Enumerate USB-RS232 link using AT90S2313 \end_layout \begin_layout Enumerate H-Bridge (for motor control) \end_layout \begin_layout Enumerate An eight LED board (Figure \begin_inset LatexCommand \ref{fig:LED-I/O-board} \end_inset ) \end_layout \begin_layout Enumerate A set of op-amps \end_layout \begin_layout Enumerate A photosensor board \end_layout \begin_layout Enumerate A stepper motor/relay driver board \end_layout \begin_layout Enumerate Piezo Tx/Rx board \end_layout \begin_layout Standard \begin_inset Float figure wide false sideways false status open \begin_layout Caption MAX232 communication board \end_layout \begin_layout Standard \begin_inset LatexCommand \label{fig:MAX232-communication-board} \end_inset \end_layout \begin_layout Standard \begin_inset Graphics filename max232.eps \end_inset \end_layout \end_inset \end_layout \begin_layout Standard \begin_inset Float figure wide false sideways false status open \begin_layout Caption LED I/O board \end_layout \begin_layout Standard \begin_inset LatexCommand \label{fig:LED-I/O-board} \end_inset \end_layout \begin_layout Standard \begin_inset Graphics filename io.eps \end_inset \end_layout \end_inset \end_layout \begin_layout Section Software \end_layout \begin_layout Subsection The Live CD \end_layout \begin_layout Standard All the tools required to use Phoenix-MDK are included in a Debian GNU/Linux Live\InsetSpace ~ CD. The Live\InsetSpace ~ CD helps you try out Phoenix-MDK without installing anything on the computer. You can use it in both text mode and graphics mode. Let's first see how to compile and run a simple program from text mode. \end_layout \begin_layout Subsubsection Text mode blinking LED \end_layout \begin_layout Standard Once the Debian Live\InsetSpace ~ CD boots up, you will see the standard Unix prompt: \end_layout \begin_layout Quotation user@debian:~$ \end_layout \begin_layout Standard This is where you will be typing commands. Type the command `ls' to view the files in the current folder. \end_layout \begin_layout Quotation user@debian:~$ ls \end_layout \begin_layout Quotation blink.c \end_layout \begin_layout Quotation user@debian:~$ \end_layout \begin_layout Standard There is a file called `blink.c' which contains the source code of a simple `LED blinking' application. You can see the contents of the file by using the `cat' command: \end_layout \begin_layout Quotation user@debian:~$ cat blink.c \end_layout \begin_layout Quotation #include \end_layout \begin_layout Quotation main() \end_layout \begin_layout Quotation { \end_layout \begin_layout Quotation \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ pin_mode(PIN0, OUT); \end_layout \begin_layout Quotation \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ while(1) { \end_layout \begin_layout Quotation \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ set_high(PIN0); \end_layout \begin_layout Quotation \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ delay(30000); \end_layout \begin_layout Quotation \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ set_low(PIN0); \end_layout \begin_layout Quotation \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ \InsetSpace ~ } \end_layout \begin_layout Quotation } \end_layout \begin_layout Quotation user@debian:~$ \end_layout \begin_layout Standard The header file `pmdk.h' has to be included in all programs. The digital I/O pins numbered 0 to 20 (and referred to as PIN0 ... PIN20 in the program) have to be configured as either input or as output before they can be used. The `pin_mode' function does exactly this - we are making PIN0 act as an output pin. The function `set_high' makes the corresponding pin go high (5V) and the function `set_low' makes the pin go low (0V). The `delay' function accepts a value between 0 and 65535 and generates a delay in microseconds equal to twice that value. \end_layout \begin_layout Standard The code can be compiled by typing: \end_layout \begin_layout Quotation compile blink \end_layout \begin_layout Standard at the prompt. The compilation process generates a file called `blink.hex' - this is the machine code which we will load into the microcontroller. \end_layout \begin_layout Standard But, before we do that, we have to set up the hardware properly. \end_layout \begin_layout Standard One of the daughter\InsetSpace ~ boards coming with the Phoenix-MDK kit lets you connect the main board with the PC's serial port. Connect the daughter board accoriding to figure \begin_inset LatexCommand \ref{fig:Connection-MAX232-daughter} \end_inset . Use the provided serial cable to link the daughter board with the PC. \end_layout \begin_layout Standard \begin_inset Float figure wide false sideways false status collapsed \begin_layout Caption Connection MAX232 daughter board \end_layout \begin_layout Standard \begin_inset LatexCommand \label{fig:Connection-MAX232-daughter} \end_inset \end_layout \end_inset \end_layout \begin_layout Standard Now, take an LED and a 1K resistor. Connect the longer leg of the LED (+ve terminal) to PIN0 on the main board. Twist the shorter leg of the LED with one of the ends of the resistor and connect the other end of the resistor to GND (which is adjacent to PIN0). Note that the resistor may not plug into GND tightly - you may have to keep the contact by pressing. \end_layout \begin_layout Standard Our hardware is now ready. Power up the board and press the `reset' switch. Immediately, type the command: \end_layout \begin_layout Quotation run blink \end_layout \begin_layout Standard at the prompt. The code will get loaded into the controller and it will start running. You will see the LED blinking! \end_layout \begin_layout Standard The micrcocontroller will wait for maximum 10 seconds (after reset) to get the machine code from the PC. If it doesn't get the new code in time, whatever was previously loaded will start running. So it is essential that you type `run\InsetSpace ~ blink' within 10 seconds after reset. \end_layout \begin_layout Chapter Simple Experiments \end_layout \begin_layout Standard This chapter takes you on a detailed tour of the Phoenix MDK library. We do this by writing small programs and observing their output with the help of the various daughter boards. \end_layout \begin_layout Section Output on LED's \end_layout \begin_layout Section Output on the LCD display \end_layout \begin_layout Section Switch input \end_layout \begin_layout Section Waveform generation \end_layout \begin_layout Section Frequency measurement \end_layout \begin_layout Section PWM Brightness control \end_layout \begin_layout Section Reading a potentiometer \end_layout \begin_layout Section Temperature sensing \end_layout \begin_layout Section Controlling a DC motor \end_layout \begin_layout Section Serial communication with the PC \end_layout \begin_layout Part AVR Microcontroller Architecture and Programming \end_layout \begin_layout Chapter AVR microcontroller Architecture \end_layout \begin_layout Standard The modern day microcontroller is a sort of `single\InsetSpace ~ chip\InsetSpace ~ computer'. If you try designing a computer system (say for some control application) using a traditional microprocessor like the 8086, you are likely to abandon the project in frustration very soon. The trouble is that the microprocessor is just one component of the system and you need to add many more (RAM, ROM, timers, counters, interrupt circuitry , I/O ports) to make it complete. This is a non trivial job and requires sophisticated knowledge of processor internals. It's here that the microcontroller comes to our rescue. A mid range 8 bit microcontroller (like the ATMega16, which belongs to the AVR family from Atmel) comes packed with all the above mentioned component s (plus a few others like Analog Comparators, Serial USART's, Analog to Digital Converters, Watchdog timers) in a single, 40 pin package. Often, the only extra component required to make the microcontroller work is a power supply! \end_layout \begin_layout Section Hardware description \end_layout \begin_layout Chapter Programming AVR microcontrollers using GNU tools \end_layout \begin_layout Standard The ATMega16 as well as a lot of other microcontrollers from major vendors like Microchip and Atmel comes equipped with a good amount of `flash' memory - non volatile memory which can be erased using electrical signals. The basic idea is to write programs (mostly in assembly language or C) on the PC and convert them to machine code belonging to the target microcontrol ler's instruction set. Once this is done, the machine code can be transferred to the non-volatile memory of the microcontroller via simple circuits connected to the serial or parallel port. This process is called `programming' or `burning' the micro. \end_layout \begin_layout Standard The Debian GNU/Linux installation DVD's (CD's) contain all the tools required to program Atmel's AVR family of microcontrollers. We just have to install them using `synaptic'. \end_layout \begin_layout Section Setting up the GNU toolchain \end_layout \begin_layout Standard Fire up synaptic and search for `avr'. Install the following packages: \end_layout \begin_layout Itemize binutils-avr \end_layout \begin_layout Itemize gcc-avr \end_layout \begin_layout Itemize avr-libc \end_layout \begin_layout Itemize uisp \end_layout \begin_layout Itemize avrdude \end_layout \begin_layout Standard The `binutils-avr' package contains tools like the assembler and the linker. The core compiler is in the package `gcc-avr' ;`avr-libc' contains some library functions. The `uisp' package contains a code burning program called `uisp' and the `avrdude' package contains a more powerful burning program. That's all the software you need to get started! Of course, if you are using the Live CD which comes with the Phoenix MDK, you have all these tools pre-installed! \end_layout \begin_layout Standard \begin_inset ERT status collapsed \begin_layout Standard \backslash appendix \end_layout \end_inset \end_layout \begin_layout Chapter Installing Debian GNU/Linux \end_layout \begin_layout Standard [To be written] \end_layout \begin_layout Chapter More about code burning \end_layout \begin_layout Standard Getting the compiled machine code into the microcontroller can be done in two ways: \end_layout \begin_layout Enumerate You build a dedicated `programming' circuit and use one of the several free software tools available. \end_layout \begin_layout Enumerate Use a boot loader \end_layout \begin_layout Section Using a programming circuit \end_layout \begin_layout Standard The AVR microcontrollers have three dedicated pins MISO, MOSI and SCK using which we can download code. All that is required is a simple direct connection of these pins with the PC parallel port. You will find schematic for such a direct-avr-parallel-access programmer here: \end_layout \begin_layout Quotation http://www.captain.at/electronics/atmel-programmer \end_layout \begin_layout Standard Once the programmer is built and powered up, connect it to the PC parallel port and test run with `uisp' (you should be logged in as root): \end_layout \begin_layout Quotation uisp -dprog=dapa -dlpt=0x378 \end_layout \begin_layout Standard And uisp should display the name of the microcontroller connected to the circuit. \end_layout \begin_layout Standard Programming tools like `uisp' and `avrdude' are compatible with a variety of burning circuits. The option -dprog is used to identify the circuit currently in use. The -dlpt option specifies the address of the parallel port, which will be 0x378 on all PC's. Note that `uisp' can also access the parallel port via its standard GNU/Linux driver - but direct I/O port access might be simpler because you will not encounter errors caused by the required drivers not being loaded properly. Read the manual page of `uisp' to know more about the various options. \end_layout \begin_layout Standard If you are using `avrdude', you can verify whether your circuit is working properly by running (as root): \end_layout \begin_layout Quotation avrdude -p m16 -c dapa \end_layout \begin_layout Standard The -p option chooses the processor (m16 is ATMega16) and the -c option specifies the programming circuit in use. \end_layout \begin_layout Standard Avrdude needs the GNU/Linux parallel port driver to function properly. Verify that the parallel port driver is loaded by running the command `lsmod' and verifying that the modules ppdev, parport, parport_pc are present. Also, make sure that there exists a device file called `parport0' under /dev/. If this file is not present, you can create it by typing (as root): \end_layout \begin_layout Quotation mknod /dev/parport0 c 99 0 \end_layout \begin_layout Subsection Reading fuse bits \end_layout \begin_layout Standard The AVR microcontrollers have two fuse bytes (fuse low and fuse high) which control many things like what type of clock the controller runs on, what is the size of the memory area allotted for the boot loader, whether control should get transferred to the bootloader upon reset etc. Here are the individual bits of the low fuse byte expressed as two 4 bit nibbles. \end_layout \begin_layout Standard Lower 4 bits (bits 0 to 3, leftmost is bit 3): \end_layout \begin_layout Quotation \begin_inset Tabular \begin_inset Text \begin_layout Standard CKSEL3 \end_layout \end_inset \begin_inset Text \begin_layout Standard CKSEL2 \end_layout \end_inset \begin_inset Text \begin_layout Standard CKSEL1 \end_layout \end_inset \begin_inset Text \begin_layout Standard CKSEL0 \end_layout \end_inset \end_inset \end_layout \begin_layout Standard Higher 4 bits (bits 4 to 7, leftmost is bit 7): \end_layout \begin_layout Quotation \begin_inset Tabular \begin_inset Text \begin_layout Standard BODLEVEL \end_layout \end_inset \begin_inset Text \begin_layout Standard BODEN \end_layout \end_inset \begin_inset Text \begin_layout Standard SUT1 \end_layout \end_inset \begin_inset Text \begin_layout Standard SUT0 \end_layout \end_inset \end_inset \end_layout \begin_layout Standard A value of 0xef (binary 1110 1111) can be used if the processor clock is based on an external crystal with frequency between 3 and 8 MHz (the value of CLKSEL1-3 decides the clock source). A value of 0xe1 (1110 0001) should be used to select the internal RC oscillator with frequency 1MHz as the clock source. This is the factory default. \end_layout \begin_layout Standard Here are the individual bits of the high fuse byte. \end_layout \begin_layout Standard Lower 4 bits (bits 0 to 3, leftmost bit is bit 3): \end_layout \begin_layout Quotation \begin_inset Tabular \begin_inset Text \begin_layout Standard EESAVE \end_layout \end_inset \begin_inset Text \begin_layout Standard BOOTSZ1 \end_layout \end_inset \begin_inset Text \begin_layout Standard BOOTSZ0 \end_layout \end_inset \begin_inset Text \begin_layout Standard BOOTRST \end_layout \end_inset \end_inset \end_layout \begin_layout Standard Higher 4 bits (bits 4 to 7, leftmost bit is bit 7): \end_layout \begin_layout Quotation \begin_inset Tabular \begin_inset Text \begin_layout Standard OCDEN \end_layout \end_inset \begin_inset Text \begin_layout Standard JTAGEN \end_layout \end_inset \begin_inset Text \begin_layout Standard SPIEN \end_layout \end_inset \begin_inset Text \begin_layout Standard CKOPT \end_layout \end_inset \end_inset \end_layout \begin_layout Standard The BOOTRST bit is by default 1, make it 0 if you want the controller to jump to the `boot loader flash' section upon reset. \end_layout \begin_layout Standard BOOTSZ0 and BOOTSZ1 have relevance only if you are using a bootloader. Both should be made equal to zero to reserve the maximum amount of memory (2 kilo bytes) for the boot loader and they can be set to one to reserve the least amount of memory (256 bytes) for the boot loader. Refer table 100 of the controller datasheet (page number 257) for more details. \end_layout \begin_layout Standard Clear EESAVE to zero if you want to make sure that the EEPROM data memory is preserved during a chip erase. \end_layout \begin_layout Standard Set OCDEN and JTAGEN to one to disable both on-chip-debug as well as JTAG (which is also used for debugging). Usually, we won't be needing them. \end_layout \begin_layout Standard Clear SPIEN to zero to enable serial program and data downloading. \end_layout \begin_layout Standard CKOPT is usually set to one. \end_layout \begin_layout Standard A typical value to use for this fuse byte is 0xd9 (1101 1001). \end_layout \begin_layout Standard The fuse bits can be read by running: \end_layout \begin_layout Quotation uisp -dprog=dapa -dlpt=0x378 --rd_fuses \end_layout \begin_layout Standard Fuse bits can also be accessed using `avrdude' - but it is a bit trickier. \end_layout \begin_layout Subsection Writing fuse bits \end_layout \begin_layout Standard If you are using uisp: \end_layout \begin_layout Quotation uisp -dprog=dapa -dlpt=0x378 --wr_fuse_h=0xd9 \end_layout \begin_layout Quotation uisp -dprog=dapa -dlpt=0x378 --wr_fuse_l=0xef \end_layout \begin_layout Standard If you are using avrdude: \end_layout \begin_layout Quotation avrdude -p m16 -c dapa -U hfuse:w:0xd9:m \end_layout \begin_layout Quotation avrdude -p m16 -c dapa -U lfuse:w:0xef:m \end_layout \begin_layout Standard The -U option is to specify that what we are going to do next is a memory operation. The next argument, hfuse:w:0xd9:m says - write(w) the high fuse(hfuse) byte with the value 0xd9 specified as an immediate (m) value. \end_layout \begin_layout Standard The fuse bits have to properly written if the controller is to function as expected. \end_layout \begin_layout Subsection Writing machine code \end_layout \begin_layout Standard The compiled machine code (stored as a hex file) can be written either using `uisp' or `avrdude'. \end_layout \begin_layout Standard Using `uisp': \end_layout \begin_layout Quotation uisp -dprog=dapa -dlpt=0x378 --erase --upload if=a.hex --verify \end_layout \begin_layout Standard Using `avrdude': \end_layout \begin_layout Quotation avrdude -p m16 -c dapa -U flash:w:a.hex \end_layout \begin_layout Subsection Writing lock bits \end_layout \begin_layout Standard The AVR controllers have lock bits using which we can protect regions of memory from accidental erasure. \end_layout \begin_layout Standard [To be continued] \end_layout \end_body \end_document