You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: src/003-building-a-crate-for-avr.md
+31-9
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,24 @@
2
2
3
3
After setting up the compiler, you may use it to generate assembly or machine code targeting a specific AVR microcontroller model.
4
4
5
+
## Choosing a `--target`
6
+
7
+
The Rust nightly compiler includes a built-in target for ATmega328 named `avr-unknown-gnu-atmega328`
8
+
9
+
If you wish to target a microcontroller other than ATmega328, or you want to change any of the
10
+
default builtin options like the linking parameters, then you will need to export the builtin
11
+
`avr-unknown-gnu-atmega328` target to a custom target specification JSON file and modify
12
+
it to suit your needs.
13
+
14
+
This target can be adapted to other microcontrollers as per the instructions in [3.1. The built-in `avr-unknown-gnu-atmega328` target](./003.1-the-avr-unknown-gnu-atmega328-target.md).
15
+
16
+
In summary, there are two options:
17
+
18
+
* Use `rustc --target=avr-unknown-gnu-atmega328` to use the default, builtin GCC based target for ATmega328
19
+
* Or use `rustc --target=my-custom-avr-target.json` with either a JSON file adapted from the builtin
20
+
`avr-unknown-gnu-atmega328` target above, or otherwise build the file manually you wish to avoiding the
21
+
default path entirely.
22
+
5
23
## Make sure you use the nightly version of Rust, not the default stable channel
6
24
7
25
The best way to ensure a crate is using the Nightly compiler is to run `rustup override set nightly` inside a terminal
@@ -12,25 +30,29 @@ any time `cargo` is used within the directory tree of the crate.
12
30
13
31
To compile and link an executable crate for AVR, run the following:
14
32
33
+
Using the builtin `avr-unknown-gnu-atmega328` target:
This will generate an AVR ELF file that can be subsequently flashed to a real device or ran inside a simulator.
20
-
The ELF file will be available at `target/<TARGET JSON NAME>/release/<CRATE NAME>.elf`.
46
+
Either or these generate an AVR ELF file that can be subsequently flashed to a real device or ran inside a simulator.
47
+
The ELF file will be available at `target/<TARGET NAME>/release/<CRATE NAME>.elf`.
21
48
22
49
Notes:
23
50
24
51
***`-Z build-std=core` is required whenever AVR is being targeted**. See [3.1. A note about the required Rust `-Z build-std=<CRATE,>` flag](./003.1-note-about-rust-build-std-flag.md) for more details.
25
-
***A target specification JSON file should almost always be specified**. There is a default target of `avr-unknown-unknown`, but this
26
-
target should be avoided in virtually all real-life usecases[CITATION NEEDED]. The `avr-unknown-unknown` target is equivalent to the AVR-GCC default, partially-microcontroller-independent mode where the lowest common denominator - the `avr2` family - is targeted.
27
52
*`--release` is not strictly required - debug mode should be as correct as release mode - however, **debug mode generates SLOW CODE, especially on AVR**. Release mode is much better.
28
53
29
54
**Example**: An in-context example of compiling a crate is given for the LED blinking example in [3.2. Example - Building the `blink` program for AVR](./003.2-example-building-blink.md).
30
55
31
56
### Targeting a different microcontroller model
32
57
33
-
Other models of AVR can be targeted by simply modifying the `cpu` field inside the target specification JSON. Each desired target microcontroller
34
-
variant requires its own target specification JSON file differing only by the `cpu` field. You will find many of the existing AVR projects
35
-
provide an-tree target specification JSON file only for the popular `atmega328p`, so you will in general need to duplicate the file and edit the `cpu`
36
-
to get a crate compiling on a non-atmega328p microcontroler.
58
+
The recommended way to do this is with a custom target specification JSON file per the instructions in [3.1. The built-in `avr-unknown-gnu-atmega328` target](./003.1-the-avr-unknown-gnu-atmega328-target.md).
0 commit comments