Showing posts with label AVR. Show all posts
Showing posts with label AVR. Show all posts

Sunday, February 17, 2008

AVR STK500 Programming

In January i bought a new ATMEL STK500 board and a AVR Dragon board. I did not touch the Dragon board till now. But i could able to program STK500 board using the RS232 connector in the baord. You can view my boards here.

The Atmel AVR STK500 is a starter kit and development system for Atmel's AVR Flash microcontrollers. Most of the AVR microcontrollers are supported with this board. I got two microcontrollers with this board itself : ATMEGA8515L and ATMEGA16. Board is having some switches and LEDs connected to different ports of the microcontroller. This can be used for prototyping. 6 sockets are there for 8, 20 , 28 and 40 pin microcontrollers. Two RS-232 interfaces are there in the board. One for serial programming and the other one to interface with other boards. We can use 10-15 V DC supply.

The suggested way of programming STK500 programming board is using AVR Studio interface software for Windows. And my laptop is not having serial port. So, i bought one USB to serial converter from one electronics shop here. But i could not able to program using AVR microcontroller 8515L using AVR Studio. When programming it is not giving any error, but the load is not working at all..that means LEDs are not blinking(The program was for that purpose..:). Still i dont know what is the problem.

Then i connected the board using Linux. I compiled the program using avr-gcc. Read how to build cross compiler for AVR in my previous post. This time i was successfull. Without no issues, the program loaded to microcontroller and the LEDs started blinking...:)

The program is very simple one. LEDs are connected to PORTB in the microcontroller using switches in the board and connectors.

/* I/O PORT B, DATA DIRECTION REGISTER (0 -> in, 1 -> out) */
#define DDRB (*(volatile unsigned char *)(0x17 + 0x20))

/* I/O PORT B, DATA REGISTER */
#define PORTB (*(volatile unsigned char *)(0x18 + 0x20))

int main(void) {
int i;

/* Set the whole port (all bits) to "output" */
DDRB = 0xff;

while(1) {
/* Turn off all leds connected to port B */
PORTB = 0x00;

/* Delay */
for(i = 0; i < 0xffff; i++);

/* Turn on all leds connected to port B */
PORTB = 0xff;

/* Delay */
for(i = 0; i < 0xffff; i++);
}

return 0;
}

Then , execute following commands.

avr-gcc -mmcu=atmega8515 a.c
avr-objcopy -j .text -j .data -O ihex a.out a.hex
avrdude -p m8515 -c stk500v2 -P /dev/ttyUSB0 -U flash:w:a.hex -v
avr-objcopy is the tool for converting from one object file format to other. Here we are converting from ELF format to Intel Hex format. avrdude is a free software tool for loading the programs to the Flash of AVR microcontrollers.

Monday, January 21, 2008

Building a cross compiler for AVR Microcontrollers

Why we need a cross -compiler? Cross-compilers enable us to develop on one platform( host) while actually building for another platform( target). For building this what we need is a compiler that knows how to write machine code for our target.

Giving a basic introduction about what compiler does, what are the components of a compiler it will be easy to understand the build process. The basic goal of a compiler is to produce the bytecode for target CPU or microcontroller. The main components of a compiler are

parser: converts the language source code (eg: C, C++) into the target assembly langauge. So, Parser needs to know the target assembly langauage.

Assembler: converts the assembly language code into the bytecode (object file) that the CPU executes.

Linker: combines the object files into an executable application. Different operating system and CPu combinations generally use different encapsulation mechanism.
Eg: Executable Link Format(ELF) - usually used by PowerPC processors.

Standard C Library: If we are using functions from the C library( eg, printf) , we have to use some standard C library. Newlib and Glibc are the most common one. Newlib was developed by redhat and Glibc is GNU C library.

So, we need to build three components from the GNU tools available.

1. Binutils - includes the basic binary utilities such as the assembler, the linker, the strip utility etc..Strip utility is used for removing the unwanted information from object file and executable such as symbol table, debugging information.

2. GCC - have C preprocessor and assembly translator.

3. glibc or newlibc

Now, we can start building the cross compiler for AVR. For that first we have to create an installation folder in our linux system. We can use /usr/local/AVR

Now go to this link : http://www.nongnu.org/avr-libc/user-manual/install_tools.html

Downlaod the Binutils source, GCC source and AVR Libc source(Three Zip files )from the links provided there to /usr/local/AVR folder. Untar that Zip in the same folders. Now you will get 3 folders inside the /usr/local/AVR

binutils-x.xx
gcc-x.x.x
avr-libc-x.x.x

I used the following versions:
binutils-2.15, gcc-2.7.3, avr-libc-1.0.5. Now build the three components as explained below.

Build the Binutils:

Execute the following commands in the shell prompt to install the Bintuils for AVR.
configure -
Configure the build for AVR and specify the bin directory or install directory.
make - compile the code.
make install - Install in /usr/local/AVR/bin


$ cd binutils-2.15
$ ./configure --prefix=/usr/loca/AVR --target=avr
$ make
$ make install

The /usr/local/AVR/bin directory now contains AVR versions of ld, as, ar and the other binutils executables. Add the /usr/local/AVR/bin directory to your PATH now. You can apply the modification system-wide by adding:

PATH="$PATH:/usr/local/AVR/bin" to the /etc/profile file.

Build the GCC:

$ cd gcc-2.7.3
$ ./configure --prefix=/usr/loca/AVR --target=avr --enable-languages="c,c++" --disbale-nls
$ make
$ make install

Build the AVR Libc:

$ cd avr-libc-1.0.5
$ export PREFIX=/usr/local/AVR
$ sh ./doconf
$ ./domake
$ cd build
$ make install
So, we have installed the avr-gcc cross compiler for the AVR series microcontrollers succesfully..!!

Now after verifying that the PATH variable"/usr/local/AVR/bin" is set system wide( means from anyfolder we can use the avr-gcc and binutils tools), we can start compiling C and C++ files for the AVR.