loading
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.

Step 1: Git It

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.

Step 2: Try a Test

There's tons of tests that come with the software.  They're located in the (surprise!) tests subdirectory in the code.  You can run one like this:
./run_avr ../tests/atmega88_example.axf
You should see something like this:
AVR_MMCU_TAG_VCD_TRACE 00c6:00 - UDR0
AVR_MMCU_TAG_VCD_TRACE 00c0:20 - UDRE0
Loaded 1760 .text
Loaded 114 .data
Loaded 4 .eeprom
Starting atmega88 - flashend 1fff ramend 04ff e2end 01ff
atmega88 init
avr_eeprom_ioctl: AVR_IOCTL_EEPROM_SET Loaded 4 at offset 0
Creating VCD trace file 'gtkwave_trace.vcd'
Read from eeprom 0xdeadbeef -- should be 0xdeadbeef..
Read from eeprom 0xcafef00d -- should be 0xcafef00d..
simavr: sleeping with interrupts off, quitting gracefully
Notice it made a VCD trace file?  Let's see what that looks like in GTKWave!

Step 3:

gtkwave gtkwave_trace.vcd
You can drag and drop the Signals on the left into the Signals list by the Waves dialog to make them show up.  You can also right click the Signals and change their properties (like making them display in binary).

Step 4: How It Worked for Me

You specify the chip you want to simulate on the command line, and you can give it the object file created by avr-gcc (ELF format) to make it run.  I also specified the frequency here as 8Mhz.
run_avr -mcu attiny85 -freq 8000000 -t ~/repos/life/code/avr/tutorials/ctc_ledblink.o
This produced this wonderful output:
AVR_MMCU_TAG_VCD_TRACE 0053:00 - TCCR0B
AVR_MMCU_TAG_VCD_TRACE 004a:00 - TCCR0A
AVR_MMCU_TAG_VCD_TRACE 0038:01 - tick
AVR_MMCU_TAG_VCD_TRACE 0038:02 - reset_timer
AVR_MMCU_TAG_VCD_TRACE 0038:08 - OC0A
Loaded 136 .text
Starting attiny85 - flashend 1fff ramend 025f e2end 01ff
attiny85 init
Creating VCD trace file 'gtkwave_trace.vcd'
avr_timer_configure-0 TOP 31250.00Hz = 256 cycles
avr_timer_configure-0 TOP 488.28Hz = 16384 cycles
avr_timer_configure-0 TOP 488.28Hz = 16384 cycles
avr_timer_configure-0 A 2450.98Hz = 3264 cycles
avr_timer_configure-0 TOP 31250.00Hz = 256 cycles
avr_timer_configure-0 A 156862.75Hz = 51 cycles
simavr: sleeping with interrupts off, quitting gracefully
Now that I had the trace file, I looked at it with gtkwave like in step 3.  I used my datasheet and compared the registers with what I expected, and I was setting my timer registers backwards!  Whoops!  Anyway, it would have taken me a much longer amount of time to solve it without simavr - this way it only took a few minutes to get it up and running and find my problem.  I highly recommend you try it out!

If you'd like to see some sample code, here's the source for the test I did above:

https://github.com/hank/life/blob/master/code/avr/tutorials/ctc_ledblink.c
<p>Please update GIT repository, gitorious is no longer maintained.<a href="https://github.com/buserror/simavr" rel="nofollow"> https://github.com/buserror/simavr </a> </p>
<p>Great! You shed light to something really dark for me!</p><p>Thank you a bunch!</p>
<p>Thanks!!! Nice tut!!!</p>
make -C simavr RELEASE=0<br>make[1]: Betrete Verzeichnis '/home/sebastian/simavr/simavr'<br>make obj config<br>make[2]: Betrete Verzeichnis '/home/sebastian/simavr/simavr'<br>make[2]: F&uuml;r das Ziel &raquo;obj&laquo; ist nichts zu tun.<br>make[2]: F&uuml;r das Ziel &raquo;config&laquo; ist nichts zu tun.<br>make[2]: Verlasse Verzeichnis '/home/sebastian/simavr/simavr'<br>make libsimavr run_avr<br>make[2]: Betrete Verzeichnis '/home/sebastian/simavr/simavr'<br>make[2]: F&uuml;r das Ziel &raquo;libsimavr&laquo; ist nichts zu tun.<br>LD obj-x86_64-linux-gnu/run_avr.elf<br>/home/sebastian/simavr/simavr/../simavr/obj-x86_64-linux-gnu/libsimavr.so: Nicht definierter Verweis auf `gelf_getshdr'<br>/home/sebastian/simavr/simavr/../simavr/obj-x86_64-linux-gnu/libsimavr.so: Nicht definierter Verweis auf `elf_begin'<br>/home/sebastian/simavr/simavr/../simavr/obj-x86_64-linux-gnu/libsimavr.so: Nicht definierter Verweis auf `elf_end'<br>/home/sebastian/simavr/simavr/../simavr/obj-x86_64-linux-gnu/libsimavr.so: Nicht definierter Verweis auf `elf_strptr'<br>/home/sebastian/simavr/simavr/../simavr/obj-x86_64-linux-gnu/libsimavr.so: Nicht definierter Verweis auf `elf_nextscn'<br>/home/sebastian/simavr/simavr/../simavr/obj-x86_64-linux-gnu/libsimavr.so: Nicht definierter Verweis auf `elf_version'<br>/home/sebastian/simavr/simavr/../simavr/obj-x86_64-linux-gnu/libsimavr.so: Nicht definierter Verweis auf `elf_getdata'<br>/home/sebastian/simavr/simavr/../simavr/obj-x86_64-linux-gnu/libsimavr.so: Nicht definierter Verweis auf `gelf_getsym'<br>collect2: error: ld returned 1 exit status<br>make[2]: *** [obj-x86_64-linux-gnu/run_avr.elf] Fehler 1<br>make[2]: Verlasse Verzeichnis '/home/sebastian/simavr/simavr'<br>make[1]: *** [all] Fehler 2<br>make[1]: Verlasse Verzeichnis '/home/sebastian/simavr/simavr'<br>make: *** [build-simavr] Fehler 2<br><br>Hey, can you tell me were is my fault?<br><br>MfG<br>
Hello! I have problems running tests (I am using Fedora 17):<br> [adam@deathstar simavr]$ ./simavr/run_avr tests/atmega48_disabled_timer.axf<br> Loaded 152 .text<br> Loaded 0 .data<br> avr_make_mcu_by_name: AVR 'atmega48' not known<br> ./simavr/run_avr: AVR 'atmega48' not known<br> <br> This error happens on each test that I try to run, and on my own .hex files as well.<br> Do you have any suggestions?
Hmm I'm not sure. Have you done a top-level make to build all the cores? Make sure they're available.
Interesting tutorial, especially since it is so hard to find anything else written up about simavr.<br> <br> 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?<br> <br> <em>#include &quot;avr_mcu_section.h&quot;<br> AVR_MCU(F_CPU, &quot;atmega88&quot;);<br> <br> /*<br> * This small section tells simavr to generate a VCD trace dump with changes to these<br> * registers.<br> * Opening it with gtkwave will show you the data being pumped out into the data register<br> * UDR0, and the UDRE0 bit being set, then cleared<br> */<br> const struct avr_mmcu_vcd_trace_t _mytrace[]&nbsp; _MMCU_ = {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { AVR_MCU_VCD_SYMBOL(&quot;UDR0&quot;), .what = (void*)&amp;UDR0, },<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { AVR_MCU_VCD_SYMBOL(&quot;UDRE0&quot;), .mask = (1 &lt;&lt; UDRE0), .what = (void*)&amp;UCSR0A, },<br> };</em><br> <br> <br> I am assuming that some sort of wrapper is required, since when I use run_avr on my plain .elf file, I just get:&nbsp;<br> <br> <em>Loaded 1892 .text<br> Loaded 6 .data<br> Starting attiny85 - flashend 1fff ramend 025f e2end 01ff<br> attiny85 init</em><br> <br> and simavr just sits there.
Apparently (I had to look it up), I did do something like that as can be seen here: <br> <br>https://github.com/hank/life/blob/master/code/avr/tutorials/ctc_ledblink.c <br> <br>It's commented out in that code, but I apparently had it there specifically for SIMAVR. Cheers!
Quite amazing! Thank you. I have just started working with ATtiny85 and this may come in handy.
Great instructable. Gave me just the handle I needed to begin wrapping my head around this pretty cool AVR simulator. Super job!<br><br>Thanks!

About This Instructable

38,924views

32favorites

License:

Bio: Just a dude who reads a lot of Instructables.
More by hardwarehank:Archiving Super Audio CDs (SACDs) with an old PS3 Slim DVD case tablet stand AVR LCD Namebadge 
Add instructable to: