Skip to content

Commit

Permalink
Add Makefile targets to program MCU
Browse files Browse the repository at this point in the history
Also include the codgen-units=1 workaround for a rustc segfault when building
this crate in release mode. Some details here:

rust-lang/rust#62896
  • Loading branch information
rodrigovalle committed Sep 17, 2020
1 parent ed154b8 commit de39296
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ edition = "2018"
[dependencies]
# this is broken
#ruduino = "0.2"

[profile.release]
codegen-units = 1
lto = true
debug = false
opt-level = "s"
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.PHONY: upload

BIN=rust-test
DEVICE=/dev/ttyUSB0
RELEASE_DIR=target/avr-unknown-gnu-atmega2560/release
ELF=$(RELEASE_DIR)/$(BIN).elf
HEX=$(RELEASE_DIR)/$(BIN).hex
EEP=$(RELEASE_DIR)/$(BIN).eep
SRC=$(wildcard **/*.rs)

build: $(HEX)

upload: $(HEX)
avrdude -patmega2560 -cwiring -P$(DEVICE) -b115200 -D -Uflash:w:$(HEX):i

# rust builds a .elf file
$(ELF): $(SRC)
cargo build --release

# convert elf to hex for the programmer, pull .eeprom out because that is
# programmed separately
# ihex is short for "intel hex", which is recognized by avrdude
$(HEX): $(ELF)
avr-objcopy -O ihex -R .eeprom $(ELF) $(HEX)

# pull .eeprom from the elf and create a separate file representing what should
# be written to the eeprom (this file is currently unused in the build process)
# eeprom can also be programmed by avrdude's -U
$(EEP): $(ELF)
avr-objcopy -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 $(ELF) $(EEP)
2 changes: 0 additions & 2 deletions README.md

This file was deleted.

6 changes: 4 additions & 2 deletions avr-unknown-gnu-atmega2560.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"late-link-args": {
"gcc": [
"-lgcc",
"-lc"
"-lc",
"-flto"
]
},
"linker": "avr-gcc",
Expand All @@ -24,7 +25,8 @@
"pre-link-args": {
"gcc": [
"-mmcu=atmega2560",
"-Wl,--as-needed"
"-Wl,--as-needed",
"-Wl,--gc-sections"
]
},
"target-c-int-width": "16",
Expand Down

0 comments on commit de39296

Please sign in to comment.