Tuesday, May 20, 2008

Hacking microcontroller

Our first step(Hello World program) is to set up a burning circuit for Atmega8 microcontroller.GNU/LINUX provide all the tools required to make this journey fun and exciting.The first question that comes to our mind is

'What is a microcontroller?'In a single sentence it can be defined as a single chip computer.Atmega8 is a 28 pinned IC,where each pin has a dual function.We preferred it because it is the cheapest microcontroller available.A microcontroller is packed with RAM, ROM, Timers, Counters,Internal Circuitry,Analog comparators,ADC etc.In addition,it is also equipped with a good amount of 'flash' memory which can be erased with electric signals.The only extra component required for its functioning is the power supply.


Next question that arises in our mind is 'What is meant by a burning circuit?'
The basic idea is to write programs (mostly in assembly language or C.We used C as it is easy to code) on the PC and convert them to machine code belonging to the target microcontroller'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.

Burning circuit helps u to familiarise with atmega8 and the softwares required to burn a program from pc to microcontroller.

Four software packages that we used to compile and burn program to a microcontroller:
# binutils-2.15.tar.bz2. This is the source code for tools like the assembler, linker etc.
# gcc-core-3.4.2.tar.bz2. This package contains the source of the GNU C compiler.
# avr-libc-1.0.4.tar.bz2. Source code for the AVR C library containing many utility functions.
#uisp-20050207.tar.gz.This is the program used to transfer machine code to the microcontroller.

The first three packages binutils,gcc-core and avr-libc are interconnected and version specific.Even higher versions can't take the place of lower versions.This is just one such set of interconnected packages.You can use any one of such sets.This set of packages support coding in C.But uisp is just a program which can be selected without any version constraints.There is another program that replaces uisp which is known by sp12.However,uisp is more convenient to use.


Before keeping the first footstep into the vast world of microcontrollers,some points need to be kept in mind:
*before installation see whether the following packages are installed or not
-lex packages otherwise linker will not be properly installed.
-g++ packages for proper installation of gcc-core.
-BSD for avr-libc(avr library).
*PATH must be specified each time when a new terminal is opened to compile programs.

Now we can start our exciting journey to the world of microcontroller....

Packages can be acquired either from a bootable CD(GNU/LINUX) or by downloading from internet.As you can see, booting from CD(GNU/LINUX) is the easiest method where no software installation is required.But as enthusiastic by nature we selected the round about way,that is,download and then install.Its,of course, a bit difficult at the same time exciting too.

Platform we work is the LINUX OS...

Following are the steps that we took to accomplish the above task:

We installed all the programs to /usr/local/avr. This is to keep the program separate from your normal Linux C compiler. Create this directory with the command:

mkdir /usr/local/avr

You can add it already now to your PATH:
mkdir /usr/local/avr/bin
export PATH=/usr/local/avr/bin:${PATH}


Software installation: GNU binutils-

The binutils package provides all the low-level utilities needed for building object files. It includes an AVR assembler (avr-as), linker (avr-ld), library handling tools (avr-ranlib, avr-ar), programs to generate object files loadable to the microcontroller's EEPROM (avr-objcopy), disassembler (avr-objdump) and utilities such as avr-strip and avr-size.

Run the following commands to build and install the binutils :

tar jxvf binutils-2.15.tar.bz2
cd binutils-2.15/
mkdir obj-avr
cd obj-avr
../configure --target=avr --prefix=/usr/local/avr --disable-nls
make

# as root:
make install



Add the line /usr/local/avr/lib to the file /etc/ld.so.conf and run the command /sbin/ldconfig to rebuild the linker cache.


Software installation: AVR gcc-

avr-gcc will be our C compiler.

Run the following command to build and install it:

tar jxvf gcc-core-3.4.2.tar.bz2
cd gcc-3.4.2

mkdir obj-avr
cd obj-avr
../configure --target=avr --prefix=/usr/local/avr --disable-nls --enable-language=c

make

# as root:
make install



Software installation: The AVR C-library-

Run the following command to build and install it:

tar jxvf avr-libc-1.0.4.tar.bz2
cd avr-libc-1.0.4
PREFIX=/usr/local/avr
export PREFIX
sh -x ./doconf
./domake

cd build
#as root:
make install


Software installation: The Programmer-

The programmer software loads the specially prepared object code into the EEPROM of our microcontroller.

The uisp programmer for Linux is a very good programmer. It can be used directly from within a Makefile. You just add a "make load" rule and you can compile and load the software in one go.

uisp is installed as follows:

tar jxvf uisp-20050207.tar.bz2.tar
cd uisp-20050207
./configure --prefix=/usr/local/avr
make

# as root:
make install

Now we have completed with all software installations.To see whether it's properly installed,all we need to do is run the simple program that follows.

"Hello, world" program for Embedded system-

PC programmers have it easy; once they install a compiler/interpreter for their favourite programming language, they can write a `hello,world' program and have it running instantly. It's not so simple for an embedded system programmer. Compiling the program is just half the job - the other half is downloading the code to the target microcontroller and getting it running. We will do things one step at a time - we will first write a simple C program and convert it to machine code, just to make sure that our newly installed development environment is working fine.

Here is our test program:

/* a.c, simple test program */
#include
main()
{
uint8_t c = 1;
}


We will compile it as shown below(make sure that /usr/local/avr/bin is in the path):

avr-gcc -mmcu=atmega8 -Os a.c


Try running the resulting `a.out'. You will get an error message:

./a.out: cannot execute binary file


The reason is that `a.out' contains machine code which will work only on the ATMega8 microcontroller and not on the host PC. This machine code has to be downloaded to the microcontroller using a special program (we will use a very capable tool called `uisp')

If you have reached here successfully ,it means the first three packages are successfully installed.Installation of uisp can be confirmed by:
# export PATH=$PATH:/usr/local/avr/bin
# uisp
uisp: No commands specified. Try 'uisp --help' for list of commands.


We use the parallel port and uisp to program the AVR. Uisp uses the ppdev interface of the kernel. Therefore you need to have the following kernel modules loaded:

# /sbin/lsmod
parport_pc
ppdev
parport

Check with the command /sbin/lsmod that they are loaded otherwise load them (as root) with:

modprobe parport
modprobe parport_pc
modprobe ppdev


It is a good idea to execute these commands automatically during startup. You can add them to a rc script (e.g for Redhat /etc/rc.d/rc.local).
To use the ppdev interface as normal user root needs to give you write access by once running the command

chmod 666 /dev/parport0

Make as well sure that no printer daemon is running on the parallel port. If you have one running then stop it before you connect the programmer cable. Now everything is ready to compile and program our microcontroller.


Error message like above confirms the proper installation of uisp.

Awaiting for suggestions...
We apologize for errors(if any) has occured as we ourselves are fresh to this field.We will be thankfull to those who points out our errors(if any)....

1 comment:

Unknown said...

Thanks for the instructions...Like to point out two errors..... There is no '.h' file included in the sample program used to test if three packages are correctly installed......Not able to untar the uisp package using the command given in the blog ....downloadable format from the site is .gz...had to use other command...