Debugging AVR code in Linux with simavr by hardwarehank
simavr-by-buserror.jpg
I recently started programming AVR chips, namely the ATTiny85.  They can be programmed using C, compilers are readily available in Ubuntu, and you can do a LOT with them - just search for avr on this site!  Anyway, I was having some trouble with my project today - the LED wouldn't flash!  I couldn't figure out what was going on, as debugging these things is non-trivial.  That is, until I discovered simavr.

It's a simulator for AVR in Linux that can take your ELF or HEX code and run it as if it's actually on-chip, but gives you hooks and the possibility of dumping trace files.  I'll go through a simple example.
 
Remove these adsRemove these ads by Signing Up

Step 1: Git it

git-logo.png
There are a few dependencies on Ubuntu (which is what I use all the time):
sudo apt-get install avr-libc libelf-dev \
   libglut3-dev gtkwave git build-essential
I had to remove some crappy Mesa symlink for OpenGL in 10.10.  Only do this if you have problems compiling related to -lGL,
sudo rm /usr/lib/libGL.so
cd /usr/lib
sudo ln -s libGL.so.1 libGL.so
cd
Finally, use git to download the source code:
git clone git://gitorious.org/simavr/simavr.git
Now, build it.
cd simavr
make
If you get any errors, leave me a comment.  I'd be happy to help.  You should have a new program called run_avr in the simavr subdirectory once that completes.
Freaken says: Feb 28, 2013. 7:28 AM
Hello! I have problems running tests (I am using Fedora 17):
[adam@deathstar simavr]$ ./simavr/run_avr tests/atmega48_disabled_timer.axf
Loaded 152 .text
Loaded 0 .data
avr_make_mcu_by_name: AVR 'atmega48' not known
./simavr/run_avr: AVR 'atmega48' not known

This error happens on each test that I try to run, and on my own .hex files as well.
Do you have any suggestions?
hardwarehank (author) says: Mar 1, 2013. 6:33 AM
Hmm I'm not sure. Have you done a top-level make to build all the cores? Make sure they're available.
vace117 says: Feb 12, 2013. 11:43 AM
Interesting tutorial, especially since it is so hard to find anything else written up about simavr.

I do have a question though. How did you specify what you want to trace? I am assuming that you had to wrap your original .hex/.elf file in some sort of wrapper code that was linked against simavr? Did you have to do something like this?

#include "avr_mcu_section.h"
AVR_MCU(F_CPU, "atmega88");

/*
* This small section tells simavr to generate a VCD trace dump with changes to these
* registers.
* Opening it with gtkwave will show you the data being pumped out into the data register
* UDR0, and the UDRE0 bit being set, then cleared
*/
const struct avr_mmcu_vcd_trace_t _mytrace[]  _MMCU_ = {
        { AVR_MCU_VCD_SYMBOL("UDR0"), .what = (void*)&UDR0, },
        { AVR_MCU_VCD_SYMBOL("UDRE0"), .mask = (1 << UDRE0), .what = (void*)&UCSR0A, },
};



I am assuming that some sort of wrapper is required, since when I use run_avr on my plain .elf file, I just get: 

Loaded 1892 .text
Loaded 6 .data
Starting attiny85 - flashend 1fff ramend 025f e2end 01ff
attiny85 init


and simavr just sits there.
hardwarehank (author) says: Feb 13, 2013. 5:59 AM
Apparently (I had to look it up), I did do something like that as can be seen here:

https://github.com/hank/life/blob/master/code/avr/tutorials/ctc_ledblink.c

It's commented out in that code, but I apparently had it there specifically for SIMAVR. Cheers!
celem says: Feb 6, 2012. 6:38 AM
Quite amazing! Thank you. I have just started working with ATtiny85 and this may come in handy.
nevdull says: Dec 16, 2011. 1:34 PM
Great instructable. Gave me just the handle I needed to begin wrapping my head around this pretty cool AVR simulator. Super job!

Thanks!
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!