Skip to content

Instantly share code, notes, and snippets.

@jonahbron
Last active October 31, 2020 16:30
Show Gist options
  • Save jonahbron/640a825dd9a526d3a1c24f2c20ca44ac to your computer and use it in GitHub Desktop.
Save jonahbron/640a825dd9a526d3a1c24f2c20ca44ac to your computer and use it in GitHub Desktop.
Install AVR Rust and Compile Blink
git clone [email protected]:avr-rust/rust.git avr-rust
cd avr-rust
git checkout avr-support # 8405b30 at time of writing
cp config.toml.example config.toml
# Manually uncomment experimental-targets setting in config.toml
./x.py build
rustup toolchain link avr $(realpath $(find build -name 'stage2'))
cd ../../
wget https://downloads.arduino.cc/arduino-1.8.12-linux64.tar.xz
tar -xf arduino-1.8.12-linux64.tar.xz
git clone [email protected]:avr-rust/blink.git
cd blink
# set the linker to the toolchain from Arduino, and explicitly disable -pie
# use cargo built from avr-rust. Path will be different depending on system.
XARGO_RUST_SRC="$(realpath ../avr-rust/src)" \
RUSTFLAGS="-C linker=$(realpath $(../arduino-1.8.12/hardware/tools/avr/bin/avr-gcc)) -C link-args=-no-pie" \
rustup run avr ../avr-rust/build/x86_64-unknown-linux-gnu/stage0/bin/cargo xbuild --target avr-unknown-unknown --verbose
@bburky
Copy link

bburky commented Apr 9, 2020

The reason for using the stage0 version of cargo is that it's version (1.36.0-beta) is compatible with the 1.37.0 rustc from the avr-support branch.

If you use a current (1.42.0 for example) version of cargo it will pass incompatible flags --error-format=json --json=diagnostic-rendered-ansi to 1.37.0 rustc:

$ rustup run avr-toolchain  cargo xbuild  --target avr-atmega328p --release
    Updating crates.io index
   Compiling core v0.0.0 (C:\Users\Blake\Code\Others\rust-avr\src\libcore)
   Compiling compiler_builtins v0.1.26
error: Unrecognized option: 'json'

error: Unrecognized option: 'json'

error: could not compile `core`.
warning: build failed, waiting for other jobs to finish...
error: could not compile `compiler_builtins`.

The error: Unrecognized option: 'json' errors refer to the incompatible --json flags.

@bburky
Copy link

bburky commented Apr 9, 2020

The compiled .elf can be flashed with something along the lines of:

avr-objcopy -O ihex .\target\avr-atmega328p\release\blink.elf blink.hex
avrdude -patmega328p  -b 57600 -c stk500v1 -PCOM4 -U flash:w:blink.hex

@bburky
Copy link

bburky commented Apr 9, 2020

Installing the gcc avr toolchain can be done on Ubuntu with:

apt install gcc-avr avr-libc binutils-avr avrdude

@Rahix
Copy link

Rahix commented Apr 9, 2020

avr-objcopy -O ihex .\target\avr-atmega328p\release\blink.elf blink.hex

I added a script for this in avr-hal: mkhex.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment