Skip to content

Commit

Permalink
overhaul.
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-i1-h committed Jan 24, 2022
1 parent 4b3a894 commit eb2229d
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 114 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*~
target
Cargo.lock
avr-unknown-gnu-atmega32u4.json
options.mk
9 changes: 2 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
[package]
name = 'hello'
version = '1.0.0'
authors = ['Itoh Shimon <[email protected]>']
version = '2.0.0'
authors = ['Itoh Shimon <[email protected]>']

[lib]
crate-type = ['staticlib']
path = 'hello.rs'

[profile.release]
lto = true
panic = 'abort'
opt-level = 'z'
29 changes: 29 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM ubuntu:focal

RUN set -eux; \
groupadd -g 1005 builder; \
useradd -g builder -u 1005 -m -s /bin/bash builder

RUN set -eux; \
apt-get update; \
apt-get install -y --no-install-recommends \
# for Docker
gosu curl ca-certificates \
# for makefile
jq make \
# for rust's libcore
gcc libc6-dev

RUN set -eux; \
gosu builder mkdir /home/builder/arduboy-hello-rs; \
gosu builder mkdir /home/builder/bin

RUN set -eux; \
gosu builder bash -c "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y"; \
gosu builder bash -c "curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | BINDIR=/home/builder/bin sh"

COPY --chown=builder:builder Cargo.toml Makefile arduboy-hello-rs.ino hello.rs /home/builder/arduboy-hello-rs/

RUN set -eux; \
cd /home/builder/arduboy-hello-rs; \
gosu builder bash -c 'export PATH="/home/builder/bin:/home/builder/.cargo/bin:$PATH" && make PORT=fake setup && make'
67 changes: 34 additions & 33 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
#IDE_PATH := ${HOME}/opt/arduino-1.8.5
#PORT := /dev/ttyACM0
.PHONY: all setup cargo build upload verify

ifndef IDE_PATH
$(error IDE_PATH is not defined)
endif
-include options.mk

ifndef PORT
$(error PORT is not defined)
$(error PORT is not defined)
else
$(shell printf 'PORT = %s\n' $(PORT) > options.mk)
endif

SYSROOT := $(shell rustc +avr-toolchain --print sysroot)
BOARD = arduino:avr:leonardo
TARGET = avr-unknown-gnu-atmega32u4
RECIPE = "$$(arduino-cli compile -b $(BOARD) --show-properties \
| grep -E '^recipe\.c\.combine\.pattern=.*$$' \
| sed -r 's@(.*)@\1 target/$(TARGET)/release/libhello.a@')"

all: build

setup:
# workaround https://github.com/rust-lang/compiler-builtins/issues/400
rustup toolchain install nightly-2021-01-07
rustup override set nightly-2021-01-07
rustup component add rust-src --toolchain nightly-2021-01-07
rustc --print target-spec-json -Z unstable-options \
--target avr-unknown-gnu-atmega328 \
| sed 's/atmega328/atmega32u4/g' \
| jq '."is-builtin" = false' \
> $(TARGET).json
arduino-cli core install arduino:avr
arduino-cli lib install Arduboy

# TODO Better implementation
# see https://github.com/arduino/Arduino/pull/5338
IDE_PREF := $(shell grep -E '^recipe\.c\.combine\.pattern=.*$$' \
'$(IDE_PATH)/hardware/arduino/avr/platform.txt' \
| sed -r 's@(.*)@\1 target/arduboy/release/libhello.a@')
cargo:
cargo build -Z build-std=core --target $(TARGET).json --release

build: cargo
arduino-cli compile --fqbn $(BOARD) --build-property $(RECIPE)

verify:
$(call do_build,--verify)
upload:
$(call do_build,--upload)

define do_build
: IDE_PATH := $(IDE_PATH)
: PORT := $(PORT)
: SYSROOT := $(SYSROOT)
: IDE_PREF := $(IDE_PREF)
: ----------build-rust-program----------
RUST_BACKTRACE=1 \
XARGO_RUST_SRC='$(SYSROOT)/lib/rustlib/src/' \
RUSTC='$(SYSROOT)/bin/rustc' \
RUSTDOC='$(SYSROOT)/bin/rustdoc' \
xargo build -vvv --release --target=arduboy
: ----------build-arduboy-game----------
'$(IDE_PATH)/arduino' $1 -v --board arduboy:avr:arduboy \
--port '$(PORT)' --pref '$(IDE_PREF)' ffi.ino
endef

.PHONY: verify upload
arduino-cli upload --fqbn $(BOARD) --port $(PORT)

verify: cargo
arduino-cli compile --verify --fqbn $(BOARD) --build-property $(RECIPE)
61 changes: 15 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,17 @@
# arduboy-hello-rs

![hello, arduboy and rust](demo.gif)
## Install
### Install Rust with rustup
<https://www.rust-lang.org/en-US/install.html>
### Install Arduino IDE
<https://www.arduino.cc/en/Main/Software>
### Setup Arduboy
<https://community.arduboy.com/t/quick-start-guide>
Requirement: 「Arduboy」library
### Setup AVR-Rust
~~~~~~~~~~~~~~~~~~~~
mkdir -p ~/ws/avr-rust-build/build/
cd ~/ws/avr-rust-build/
git clone https://github.com/avr-rust/rust.git
(cd rust && git checkout b5a0d3b7b87eafed7499d69bd543648cfb139b0c)
cd build
../rust/configure \
--enable-debug \
--disable-docs \
--enable-llvm-assertions \
--enable-debug-assertions \
--enable-optimize
make # Wait for about an hour
rustup toolchain link avr-toolchain "$(realpath "$(find . -name stage2)")"
# ↓for xargo
RUST_SRC="$(rustc +avr-toolchain --print sysroot)/lib/rustlib/src/"
mkdir -p "${RUST_SRC}"
ln -s "$(realpath ../rust/)" "${RUST_SRC}"
~~~~~~~~~~~~~~~~~~~~
### Setup Xargo
~~~~~~~~~~~~~~~~~~~~
cargo install xargo
~~~~~~~~~~~~~~~~~~~~
### Build
~~~~~~~~~~~~~~~~~~~~
cd ~/ws
git clone https://github.com/simon-i1-h/arduboy-hello-rs.git
cd ~/ws/arduboy-hello-rs
rustup override set avr-toolchain
editor Makefile # edit `IDE_PATH' and `PORT'
make verify # check
make upload
~~~~~~~~~~~~~~~~~~~~
## Tips
<https://help.ubuntu.com/community/SerialConsoleHowto>
## License
MIT License

## Build

Main requirements:

- `rustup`
- `arduino-cli`

See also `Dockerfile` for details.

```
PORT=/dev/ttyACM0 # e.g.
make PORT=$PORT setup && make && make upload
```
3 changes: 0 additions & 3 deletions Xargo.toml

This file was deleted.

File renamed without changes.
12 changes: 0 additions & 12 deletions arduboy.json

This file was deleted.

18 changes: 6 additions & 12 deletions hello.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![feature(lang_items)]
#![feature(lang_items, core_intrinsics)]
#![no_std]
use core::intrinsics;
use core::panic::PanicInfo;

const BOTTOM: u8 = 63;
const RIGHT_END: u8 = 127;
Expand Down Expand Up @@ -163,21 +165,13 @@ fn tone(frequency: u16, duration: u16) {
unsafe { tunes_tone(frequency as c_uint, duration as c_ulong); }
}

// see https://doc.rust-lang.org/core/#how-to-use-the-core-library

#[lang = "eh_personality"]
#[no_mangle]
pub extern fn rust_eh_personality() {
}

#[lang = "panic_fmt"]
#[lang = "panic_impl"]
#[no_mangle]
pub extern fn rust_begin_panic(
_msg: core::fmt::Arguments,
_file: &'static str,
_line: u32,
_column: u32
) -> ! {
loop {
}
pub extern fn rust_begin_panic(_info: &PanicInfo) -> ! {
intrinsics::abort()
}

0 comments on commit eb2229d

Please sign in to comment.