summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.cargo/config.toml8
-rw-r--r--Cargo.toml25
-rw-r--r--avr-atmega328p.json27
-rw-r--r--rust-toolchain1
-rw-r--r--src/main.rs22
-rwxr-xr-xuno-runner.sh58
6 files changed, 141 insertions, 0 deletions
diff --git a/.cargo/config.toml b/.cargo/config.toml
new file mode 100644
index 0000000..7f8c45b
--- /dev/null
+++ b/.cargo/config.toml
@@ -0,0 +1,8 @@
+[build]
+target = "avr-atmega328p.json"
+
+[unstable]
+build-std = ["core"]
+
+[target.'cfg(target_arch = "avr")']
+runner = "./uno-runner.sh"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..84e5b4d
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,25 @@
+[package]
+name = "arduino-uno-template"
+version = "0.0.0"
+authors = ["Gustav Sörnäs <gustav@sornas.net>"]
+edition = "2018"
+
+[dependencies]
+panic-halt = "0.2.0"
+
+[dependencies.arduino-uno]
+git = "https://github.com/rahix/avr-hal"
+rev = "a202778"
+
+# Configure the build for minimal size
+[profile.dev]
+panic = "abort"
+lto = true
+opt-level = "s"
+
+[profile.release]
+panic = "abort"
+codegen-units = 1
+debug = true
+lto = true
+opt-level = "s"
diff --git a/avr-atmega328p.json b/avr-atmega328p.json
new file mode 100644
index 0000000..e236b08
--- /dev/null
+++ b/avr-atmega328p.json
@@ -0,0 +1,27 @@
+{
+ "arch": "avr",
+ "atomic-cas": false,
+ "cpu": "atmega328p",
+ "data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
+ "eh-frame-header": false,
+ "exe-suffix": ".elf",
+ "executables": true,
+ "late-link-args": {
+ "gcc": [
+ "-lgcc"
+ ]
+ },
+ "linker": "avr-gcc",
+ "linker-is-gnu": true,
+ "llvm-target": "avr-unknown-unknown",
+ "max-atomic-width": 8,
+ "no-default-libraries": false,
+ "pre-link-args": {
+ "gcc": [
+ "-mmcu=atmega328p",
+ "-Wl,--as-needed"
+ ]
+ },
+ "target-c-int-width": "16",
+ "target-pointer-width": "16"
+}
diff --git a/rust-toolchain b/rust-toolchain
new file mode 100644
index 0000000..cef4e16
--- /dev/null
+++ b/rust-toolchain
@@ -0,0 +1 @@
+nightly-2021-01-07
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..0059fde
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,22 @@
+#![no_std]
+#![no_main]
+
+use arduino_uno::prelude::*;
+use panic_halt as _;
+
+#[arduino_uno::entry]
+fn main() -> ! {
+ let dp = arduino_uno::Peripherals::take().unwrap();
+
+ let mut pins = arduino_uno::Pins::new(dp.PORTB, dp.PORTC, dp.PORTD);
+
+ let mut led = pins.d13.into_output(&mut pins.ddr);
+ led.set_low().void_unwrap();
+
+ loop {
+ led.set_high().void_unwrap();
+ arduino_uno::delay_ms(50);
+ led.set_low().void_unwrap();
+ arduino_uno::delay_ms(950);
+ }
+}
diff --git a/uno-runner.sh b/uno-runner.sh
new file mode 100755
index 0000000..066a598
--- /dev/null
+++ b/uno-runner.sh
@@ -0,0 +1,58 @@
+#!/usr/bin/env sh
+set -e
+
+case "$(uname -s)" in
+ Linux*) OS="Linux";;
+ Darwin*) OS="Mac";;
+ *) OS="Unknown";;
+esac
+
+if ! command -v numfmt > /dev/null 2>&1
+then
+ echo "numfmt is needed for human-readable sizes." >&2
+ echo "please install https://command-not-found.com/numfmt" >&2
+ alias numfmt=true
+fi
+
+if ! command -v avrdude > /dev/null 2>&1
+then
+ echo "required avrdude could not be found!" >&2
+ echo "please install https://command-not-found.com/avrdude" >&2
+ exit 1
+fi
+
+if [ $OS = "Linux" ]; then
+ SERIAL_PORT="/dev/ttyACM0"
+elif [ $OS = "Mac" ]; then
+ SERIAL_PORT="/dev/cu.usbmodem146201"
+else
+ echo "unsupported OS, things might not work" >&2
+ SERIAL_PORT="/dev/ttyACM0"
+fi
+
+if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
+ echo "usage: $0 <application.elf>" >&2
+ exit 1
+fi
+
+if [ "$#" -lt 1 ]; then
+ echo "$0: no ELF file given" >&2
+ exit 1
+fi
+
+NAME="$(basename "$1")"
+SIZE_TEXT="$(avr-size "$1" | tail -1 | cut -f1)"
+SIZE_DATA="$(avr-size "$1" | tail -1 | cut -f2)"
+SIZE_BSS="$(avr-size "$1" | tail -1 | cut -f3)"
+
+printf "\n"
+printf "Program: %s\n" "$NAME"
+printf "Size:\n"
+printf " .text %s (exact: %d)\n" "$(numfmt --to=si --padding=9 "$SIZE_TEXT")" "$SIZE_TEXT"
+printf " .data %s (exact: %d)\n" "$(numfmt --to=si --padding=9 "$SIZE_DATA")" "$SIZE_DATA"
+printf " .bss %s (exact: %d)\n" "$(numfmt --to=si --padding=9 "$SIZE_BSS")" "$SIZE_BSS"
+printf "\n"
+printf "Attempting to flash ...\n"
+printf "\n"
+
+avrdude -q -patmega328p -carduino -P"${SERIAL_PORT}" -D "-Uflash:w:$1:e"