summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-06-05 01:42:12 +0200
committerGustav Sörnäs <gustav@sornas.net>2021-06-05 01:42:12 +0200
commit1fd1fa7d4d84abc93cf657c70c0ff1afaa2cdd93 (patch)
tree309ea34f46042d3aec0375d4ff67e1f87167fcce
downloadjoystick-1fd1fa7d4d84abc93cf657c70c0ff1afaa2cdd93.tar.gz
initial commit
-rw-r--r--.cargo/config.toml8
-rw-r--r--Cargo.toml29
-rw-r--r--avr-atmega328p.json27
-rw-r--r--rust-toolchain1
-rw-r--r--src/main.rs42
-rwxr-xr-xuno-runner.sh58
6 files changed, 165 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..8911320
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,29 @@
+[package]
+name = "joystick"
+version = "0.1.0"
+authors = ["Gustav Sörnäs <gustav@sornas.net>"]
+edition = "2018"
+
+[dependencies]
+panic-halt = "0.2.0"
+
+embedded-hal = "0.2"
+nb = "1"
+avr-device = "0.3"
+ufmt = "0.1"
+
+[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
+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..68e1d38
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,42 @@
+#![no_std]
+#![no_main]
+
+use arduino_uno::{adc::{Adc, AdcSettings}, hal::{clock::MHz16, port::{mode::{Floating, Input, Output}, portd::{PD0, PD1}}, usart::Usart}, pac::USART0, prelude::*};
+use panic_halt as _;
+
+type _Serial = Usart<USART0, PD0<Input<Floating>>, PD1<Output>, MHz16>;
+
+#[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();
+
+ let mut serial = arduino_uno::Serial::new(
+ dp.USART0,
+ pins.d0,
+ pins.d1.into_output(&mut pins.ddr),
+ 57600.into_baudrate(),
+ );
+
+ let mut adc = Adc::new(dp.ADC, AdcSettings::default());
+
+ let mut a0 = pins.a0.into_analog_input(&mut adc);
+ let mut a1 = pins.a1.into_analog_input(&mut adc);
+ let mut a2 = pins.a2.into_analog_input(&mut adc);
+
+ loop {
+ let a0: u16 = nb::block!(adc.read(&mut a0)).void_unwrap();
+ let a1: u16 = nb::block!(adc.read(&mut a1)).void_unwrap();
+ let a2: u16 = nb::block!(adc.read(&mut a2)).void_unwrap();
+ // let b = nb::block!(serial.read()).void_unwrap();
+ match serial.read() {
+ Ok(_) => (),
+ Err(_) => (),
+ }
+ arduino_uno::delay_ms(100);
+ }
+}
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"