Embedded Programming

Group Assignment

For this weeks group assignment we compared performance and development workflow of different processor architectures. The full documentation can be found here

Reading a datasheet

I read the ATtiny25/45/85 Datasheet.

Arduino and ArduinoIDE

The most straightforward way to program a board is using the ArduinoIDE. Support for other microcontrollers like the ATtiny45 can be added using the board manager. But for some reason the default arduino programmer never worked for me. So I turned on the verbose output of the ArduinoIDE, copied the command line call the ArduinoIDE makes whenever I try to upload my code and changed -carduino to -cstk500v1, which means that we will be using the STK500 protocol by Atmel instead.

C

For programming the controller directly in C, I first installed some basics tools. sudo apt-get install gcc-avr binutils-avr gdb-avr avr-libc avrdude and created a file named blink.c:

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
  DDRB = 0x08;

  while (1) {
    PORTB = 0x00; _delay_ms(500);
    PORTB = 0x08; _delay_ms(500);
  }
}

Next I compiled the code with the command:

avr-gcc -g -DF_CPU=8000000 -Wall -Os -Werror -Wextra -mmcu=attiny45 -Wa,-ahlmns=blink.lst -c -o blink.o blink.c

Now I could create an ELF file using the command:

avr-gcc -g -DF_CPU=8000000 -Wall -Os -Werror -Wextra -mmcu=attiny45 -o blink.elf blink.o

Now I used avr-objcopy to create the hex file that could be flashed on to the controller:

avr-objcopy -j .text -j .data -O ihex blink.elf blink.flash.hex

That file can be flashed using avrdude:

avrdude -pattiny45 -cstk500v1 -P/dev/ttyACM0 -b19200 -v -v -v -v -U flash:w:blink.flash.hex

All of these commands could also be added to a makefile so they dont have to be typed out or copy-pasted every time a new version is beeing built.

AVR4L IDE

I also wanted to try the AVR4L IDE, which is an Open Source alternative to Atmel Studio. Sadly the installation instructions did not work for me, as I couldn't find the specific build of the rxtx library that is required for the software to run. I'm not sure if that is the problem or if there is some general incompatibilty. For me the program just gets stuck on a ClassCastException right after starting it.

Assembler

To get a look at the assembly code of the blink program I used avr-gcc -S -g -DF_CPU=8000000 -Wall -Os -Werror -Wextra -mmcu=attiny45 -Wa,-ahlmns=blink.lst -c blink.c to compile to assembly, which left me with a file named blink.s containing the instructions.

Assemblers that can be used to generate machine code from such assembly code are avr-as, avra and gavrasm.

AVR-Rust

I also wanted to try out AVR-Rust, which adds support for the AVR architecture to Rust. For testing I copied AVR-Rust Blink and tried to compile and flash it. Because this example only works for the ATMega329P - the controller of the Arduino Uno - I decided to use an Arduino this time, so I would not have to try adding ATTiny45 support by myself.

First I compiled Rust with AVR support according to the instructions on the AVR-Rust GitHub and installed xargo using cargo install xargo. My computer is not the most powerful and I had to restart the build a few times until it completed succesfully. Because of this, this part took arround two days. To get make install to work I had to edit the config.toml, changing the prefix to the following: prefix = '/usr/local/avr-rust'.

Now It seemed like I had everything installed, but the blink code did not compile, because rustc returned the error: "error: couldn't load codegen backend [...]". So I removed the .cargo directory from my $HOME, uninstalled all rust packages, cleaned the build directories and recompiled rustc using x.py instead of the makefile. Then I reinstalled rustup and xargo and registered the new toolchain. When I tried to compile the blink program I got another error which could be fixed by adding the line #![feature(cfg_target_vendor)] to the file ~/.cargo/registry/src/github.com-1ecc6299db9ec823/compiler_builtins-0.1.9/src/lib.rs.

Now it failed saying "thread 'main' panicked at 'unexpected -vV format'". When I tried to run rustc -vV it gave me the error message "error: 'rustc' is not installed for the toolchain 'avr-toolchain'", which probably means that my current toolchain got messed up somehow.

⬅ Back to overview