First of all this is not a working tutorial, just some meaningless web log of how a C/C++ n00b learn things.
To be able to work with ATMega microcontrollers under Linux operating systems, basically you need these things:
- built hardware with ATMega microcontroller (duh)
binutils-avr, avr-libc, and gcc-avr is needed to compile your code if you wrote it in C/C++ language, and avrdude is the main command to download/burn your compiled code onto the chip.
From yesterday I was tinkering about how to download ModularEEG P2 firmware into the ATMega8 microcontroller for my lab’s brand new ModularEEG digital board. Well, although I’ve been familiar with ModularEEG because I’m using it for my final year project, that EEG hardware actually was already built and prepared when I started my final year project last year. Other than dealing with electrodes and its connection, I don’t have real experience about hardware building. So when my lab had a project building another ModularEEG hardware, I’m volunteered to join the team.
I thought it was simple, compiling and downloading C program into microcontroller. I had a microcontroller laboratory course when I was on the 2nd grade, using Codevision AVR under Windows XP. And indeed it was simple at that time, because it was an IDE (Integrated Development Environment), which is a comprehensive solution for microcontroller programming. All we have to do is to write lines of code, selecting which microcontroller type we were using, several mouse clicks and voila, our code is written on the chip.
Back to ModularEEG, the source code for ModularEEG’s microcontroller is provided at OpenEEG website (http://openeeg.sf.net), and I’m intend to do it all on Linux. Well, it turns out it’s not as simple as on Windows with IDE software. I ran into compiling problems.
After installing all the needed programs as listed above, a quick way to compile your source code is by typing this command on terminal after navigating first to the directory where the source code located:
$ avr-gcc -g -Os -mmcu=atmega8 -c modeeg-p2.c
Unfortunately, it’s failed with this error output:
bin/../avr/include/avr/signal.h:36:2: warning: #warning "This header file is obsolete. Use <avr/interrupt.h>."
After several minutes of googling I found that this is occurred because it uses obsolete header and it’s been deprecated on my version of AVR C library. No wonder, because the ModularEEG microcontroller source code is coded on 2003, and it is very possible that C library used when the source code is made, is developed until now producing different guidance on coding C language for microcontroller application.
For that kind of problem (deprecation, obsolete header, etc.), I have two choices, either re-code the source with current header and coding rules, or just simply downgrade my C library along with my C compiler. Clearly I will choose second option, because I don’t really have practical knowledge about writing in C code (yet). 😀
Simple searching on the ubuntu packages website, I found the oldest package version for each needed programs:
Those packages are built for Ubuntu Dapper, which is a 2004 version. It’s close enough to 2003, when the original source code coded. Before installing those softwares I need to remove all current version software installed by typing following command:
$ sudo apt-get remove binutils-avr avr-libc gcc-avr avrdude
And make sure there’s no AVR related software installed by:
$ dpkg --list | grep avr
After that I’m good to go:
$ sudo dpkg -i binutils-avr_2.14-1_i386.deb avr-libc_1.0.3-1_all.deb gcc-avr_3.4.0-1_i386.deb avrdude_5.0-1_i386.deb
And then I call the compiler again:
$ avr-gcc -g -Os -mmcu=atmega8 -c modeeg-p2.c
Voila, success without any error. C source code is compiled to an output file (modeeg-p2.o), next step is to link it into a binary with ELF extension:
$ avr-gcc -g -mmcu=atmega8 -o modeeg-p2.elf modeeg-p2.o
We now have the binary of the program, but then how do we get it into microcontroller? Most microcontroller programmer/downloader don’t accept executables as input file, so we have to process it a little bit more to extract some bits out of it and save it into a hex file.
$ avr-objcopy -j .text -j .data -O ihex modeeg-p2.elf modeeg-p2.hex
Command above produces a hex file which suitable with the microcontroller. With this file I’m ready to download the program into the microcontroller using avrdude. But currently I haven’t tried to make connection between my ATmega microcontroller with my Linux box. So when it’s really done, I’ll update this post.
And hey, instead of doing all that stuff above over and over, they can all be placed in one file called Makefile, and we simply call “make” command to produce a hex file from C source code. For Makefile template, check out avr-libc demo project here.