There are currently 0 users and 4 guests online.
Compiling with WebbotLib without using AVR Studio.
Submitted by dunk on March 23, 2010 - 6:30am.
This will be mostly relevant to people using Linux, BSD or Apple Mac operating systems.
It will also be useful background reading for anyone using AVR Studio who want's to know what is going on in the background or anyone setting up a different IDE.
The instructions in this document explain how to combine code written for AVRs using WebbotLib with the compiled processor specific static library without using AVR Studio.
If you are new to this and running a windows PC, do your self a favor and use AVR Studio as documented in the PDF included with WebbotLib.
This document does not contain information on uploading compiled code to an AVR.
This document will presume you can decide which parts of WebbotLib's official documentation are still relevant. For a new user to WebbotLib it is recommended you build the same sample "Hello World" program found in the WebbotLib .pdf and use these intructions as a replacment for the AVR Studio specific parts.
a few ways of acheiving building code for AVRs but they all involve
manipulating the avr-gcc compiler and associated tools (an AVR specific
compiler based on Gnu Compiler Collection).
Before you start make sure the following software tools are installed:
1. The Gnu Compiler (AKA GCC.)
2. BinUtils (AVR specific versions of GCC tools.)
3. AVR LibC (AVR C libraries.)
These tools are all available under some sort of Open Source license so are free to use.
Depending on your distribution use your package manager to search for the 3 tools listed above.
On Ubuntu for example the requirements can be met as follows:
$ sudo apt-get install binutils-avr gcc-avr avr-libc
Next unpack WebbotLib somewhere that makes sense on your system.
Follow the advice in WebbotLib-x-xx.pdf: install WebbotLib once and link to it rather than copying it into every project.
On my Linux system i keep it here: /usr/lib/avr/lib/ but it is equally correct to keep it in your home directory. The important thing is it stays in the same place and has the same folder name when you upgrade WebbotLib so it does not break your build scripts.
$ sudo unzip /tmp/webbotavrclib-1.14a.zip -d /usr/lib/avr/lib/WebbotLib/
To install WebbotLib,
*** Need Volunteer for this bit. Please contact me if you can fill in the deatails. mrdunk(at)gmail.com***
Free Windows packages can be found for all these tools as well. Search and ye shall find.
Unpack WebbotLib somewhere that makes sense on your system.
For your first WebbotLib
program read WebbotLib-x-xx.pdf included in your WebbotLib package.
Read the stuff on AVR Studio as well so you get an idea of what we are
trying to achieve.
Implement avr-gcc and associated tools from the command line.
All other methods of building code for the AVR use this method in the background so it makes sense to start here.
Information you need to know at this point are:
1. The name and directory of your project file. (~/Geek_projects/avr_projects/WebbotLib_test/main.c for this example.)
2. The Install directory of WebbotLib. (/usr/lib/avr/lib/WebbotLib/ for this example.)
3. The speed your AVR is running at. You may need to refer to your MCU board documentation to find this out. (16000000Hz or 16MHz in this example.)
4. The AVR type in a format the avr-gcc tools will understand.
This command will give you a list with the correct format:
$ avr-as --help
5. The name of the pre-compiled WebbotLib static library to match your AVR.
These can be found in the top directory of your WebbotLib source file using:
$ ls -l /usr/lib/avr/lib/WebbotLib/*.a
Pick the one that matches your target. (You may need to refer to your MCU board documentation to see which AVR it uses.)
** Important Point.** When you use these static libraries (the .a files) on the command line avr-gcc will expect them *without* the leading "lib" and *without* the trailing ".a".
So "libWebbot-ATMega2560.a" becomes "Webbot-ATMega2560".
Ok, we are all set.
Change to your project directory. ("~/Geek_projects/avr_projects/WebbotLib_test/" in this example.)
1. Compile your code:
$ cd ~/Geek_projects/avr_projects/WebbotLib_test
$ avr-gcc -I"/usr/lib/avr/lib/WebbotLib" -c main.c -mmcu=atmega2560 -Wall -gdwarf-2 -std=gnu99 \
-DF_CPU=16000000UL -O0 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums \
-MD -MP -MT main.o -MF main.d
If all goes well avr-gcc should return with no message. If you see errors then something has gone wrong.
You are looking for the creation of the new file "main.o".
2. Link your code. This step links your new "main.o" with the pre-compiled "libWebbot-ATMegaXXXX.a".
**Remember, "libWebbot-ATMega2560.a" becomes "Webbot-ATMega2560" for the "-l" flag.**
$ avr-gcc -mmcu=atmega2560 -Wl,-Map=WebbotTest.map main.o -L"/usr/lib/avr/lib/WebbotLib" \
-lWebbot-ATMega2560 -lm -lc -o main.elf
Again avr-gcc will quit with no errors if things work. No news = Good news.
Check for the creation of "main.elf".
3. Convert your .elf file into an AVR readable Intel HEX file.
$ avr-objcopy -O ihex main.elf main.hex
avr-objcopy will quit with no errors if things work.
Check you have a file called main.hex.
If it exists you are done. That's the file to upload to your AVR.
A point to note: When linking the Webbot-ATMegaXXXX library with your "main.o" file, the order *is* important. The "main.o" comes first.
A point to note: avr-gcc *will not* overwrite files. To rebuild or relink files you will have to delete the old ones first.
A point to note: Everything you do on Linux or Mac is case sensitive.
Q. What does this do that Method 1. did not?
Method 2.Use a Makefile to automate the gcc build process.
A. It simplifies the process making you less likely to make a mistake while compiling but at the expense of setting up a Makefile.
The following is a Makefile in it's simplest form. Copy and paste it into a file called "Makefile" in your project's directory.
The bits you have to worry about are the Lines labeled PROJECT, TARGET, LIBRARY, CPU_SPEED and WEBBOTLIB. Edit these to match your project.
If you have called your first program "main.c" and installed WebbotLib in "/usr/lib/avr/lib/" you are already set on 2 of them.
Next build your project by typing:
You will see avr-gcc commands like in Method 1. Hopefully no errors.
$ make clean
$ make compile
I have had good success with KontrollerLab on Linux. It integrates with most hardware programmers and has a serial console built in.
The down side of KontrollerLab is it is not possible to include external libaries (like WebbotLib) by default so you will be left hacking about with external Makefiles. It is posible to use external Makefiles from within KontrolerLab but it's a messy solution.
It is very very good once set up, integrates with Apache Ant, most hardware programmers and many other plug-ins.