|
| 1 | +# `avr-none` |
| 2 | + |
| 3 | +Series of microcontrollers from Atmel: ATmega8, ATmega328p etc. |
| 4 | + |
| 5 | +**Tier: 3** |
| 6 | + |
| 7 | +## Target maintainers |
| 8 | + |
| 9 | +- [Patryk Wychowaniec ](https://github.com/Patryk27) <[email protected]> |
| 10 | + |
| 11 | +## Requirements |
| 12 | + |
| 13 | +This target is only cross-compiled; x86-64 Linux, x86-64 macOS and aarch64 macOS |
| 14 | +hosts are confirmed to work, but in principle any machine able to run rustc and |
| 15 | +avr-gcc should be good. |
| 16 | + |
| 17 | +Compiling for this target requires `avr-gcc` installed, because a couple of |
| 18 | +intrinsics (like 32-bit multiplication) rely on [`libgcc`](https://github.com/gcc-mirror/gcc/blob/3269a722b7a03613e9c4e2862bc5088c4a17cc11/libgcc/config/avr/lib1funcs.S) |
| 19 | +and can't be provided through `compiler-builtins` yet. This is a limitation that |
| 20 | +[we hope to lift in the future](https://github.com/rust-lang/compiler-builtins/issues/711). |
| 21 | + |
| 22 | +You'll also need to setup the `.cargo/config` file - see below for details. |
| 23 | + |
| 24 | +## Building the target |
| 25 | + |
| 26 | +Rust comes with AVR support enabled, you don't have to rebuild the compiler. |
| 27 | + |
| 28 | +## Building Rust programs |
| 29 | + |
| 30 | +Install `avr-gcc`: |
| 31 | + |
| 32 | +```console |
| 33 | +# Ubuntu: |
| 34 | +$ sudo apt-get install gcc-avr |
| 35 | + |
| 36 | +# Mac: |
| 37 | +$ brew tap osx-cross/avr && brew install avr-gcc |
| 38 | + |
| 39 | +# NixOS (takes a couple of minutes, since it's not provided through Hydra): |
| 40 | +$ nix shell nixpkgs#pkgsCross.avr.buildPackages.gcc11 |
| 41 | +``` |
| 42 | + |
| 43 | +... setup `.cargo/config` for your project: |
| 44 | + |
| 45 | +```toml |
| 46 | +[build] |
| 47 | +target = "avr-none" |
| 48 | +rustflags = ["-C", "target-cpu=atmega328p"] |
| 49 | + |
| 50 | +[unstable] |
| 51 | +build-std = ["core"] |
| 52 | +``` |
| 53 | + |
| 54 | +... and then simply run: |
| 55 | + |
| 56 | +```console |
| 57 | +$ cargo build --release |
| 58 | +``` |
| 59 | + |
| 60 | +The final binary will be placed into |
| 61 | +`./target/avr-none/release/your-project.elf`. |
| 62 | + |
| 63 | +Note that since AVRs have rather small amounts of registers, ROM and RAM, it's |
| 64 | +recommended to always use `--release` to avoid running out of space. |
| 65 | + |
| 66 | +Also, please note that specifying `-C target-cpu` is required - here's a list of |
| 67 | +the possible variants: |
| 68 | + |
| 69 | +https://github.com/llvm/llvm-project/blob/093d4db2f3c874d4683fb01194b00dbb20e5c713/clang/lib/Basic/Targets/AVR.cpp#L32 |
| 70 | + |
| 71 | +## Testing |
| 72 | + |
| 73 | +You can use [`simavr`](https://github.com/buserror/simavr) to emulate the |
| 74 | +resulting firmware on your machine: |
| 75 | + |
| 76 | +```console |
| 77 | +$ simavr -m atmega328p ./target/avr-none/release/your-project.elf |
| 78 | +``` |
| 79 | + |
| 80 | +Alternatively, if you want to write a couple of actual `#[test]`s, you can use |
| 81 | +[`avr-tester`](https://github.com/Patryk27/avr-tester). |
0 commit comments