kamkow1
kamkow1

Reputation: 501

Rust: build for atmega16

I would like to write a simple program and compile it for atmega16. How do I add atmega16 as a compilation target? There are little to no docs about this...

Thanks

Upvotes: 0

Views: 462

Answers (1)

harmic
harmic

Reputation: 30597

The only built-in AVR target is avr-unknown-gnu-atmega328. If you are using a different AVR microcontroller, you need a custom target.

The AVR-Rust Guidebook describes how to add custom targets for different AVR microcontrollers. The process boils down to:

  1. Export the JSON specification for the built-in atmega328 target, to use as a template

    rustc --print target-spec-json -Z unstable-options --target avr-unknown-gnu-atmega328 > avr-unknown-gnu-atmega16.json
    
  2. Edit this JSON file. I found that to get it to compile for atmega16, I had to change the following:

    "cpu": "atmega16",
    "is-builtin": false,
    "-mmcu=atmega16"
    

    Note:

    • The CPU is specified as "atmega16" not "atmega16p" as you had attempted to use
    • I don't have any atmega16 MCU to hand so I have not been able to test the resulting binary. You may need to tweak other elements of the file.
    • You will also need to install avr-gcc, as Rust uses it to link the resulting binary. You can get that here.
  3. You should then be able to build your project like this:

    cargo build -Z build-std=core --target ./avr-unknown-gnu-atmega16.json --release
    

    Note that --target is specified as the path to the JSON file. If in the same directory, prefix with ./.

Upvotes: 0

Related Questions