From 928471f4594f2eeace551f699e28cd1acccd0f7e Mon Sep 17 00:00:00 2001 From: Tray Torrance Date: Sun, 17 Oct 2021 20:28:35 -0700 Subject: [PATCH 01/11] Add support for AVR platforms This commit adds support for AVR (Arduino) platforms via direct implementation of interrupt enable/disable functionality via AVR assembly. The underlying motivation for this support is to enable further integration with the greater Embedded Rust ecosystem. As an example, `embedded-nal` depends upon `heapless`, which in turn depends upon atomic support, either natively or via `atomic-polyfill`. `atomic-polyfill` uses `critical-section`, and so enabling AVR support in this crate has great benefits to the downstream packages that hvae come to depend upon `critical-section`. --- src/lib.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index d459f53..d0730d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![no_std] +#![feature(llvm_asm)] pub use bare_metal::CriticalSection; @@ -92,6 +93,25 @@ cfg_if::cfg_if! { cortex_m::interrupt::enable() } } + } else if #[cfg(target_arch = "avr")] { + #[no_mangle] + unsafe fn _critical_section_acquire() -> u8 { + let mut sreg: u8; + llvm_asm!( + "in $0, 0x3F + cli" + : "=r"(sreg) + ::: "volatile" + ); + sreg + } + + #[no_mangle] + unsafe fn _critical_section_release(token: u8) { + if token & 0x80 == 0x80 { + llvm_asm!("sei" :::: "volatile"); + } + } } else if #[cfg(target_arch = "riscv32")] { #[no_mangle] unsafe fn _critical_section_acquire() -> u8 { From 9c4aa28e1c7258a449702d55c6da6140a8d84da3 Mon Sep 17 00:00:00 2001 From: Robert Forsman Date: Thu, 7 Apr 2022 15:28:30 -0400 Subject: [PATCH 02/11] perform changes requested by Dirbaio re: ci.sh, rust-toolchain.toml, and feature(llvm_asm). Also had to disable #![doc because the AVR compiler can't handle it --- ci.sh | 1 + rust-toolchain.toml | 1 + src/lib.rs | 4 ++-- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ci.sh b/ci.sh index e9ba8b4..1b768f2 100755 --- a/ci.sh +++ b/ci.sh @@ -7,3 +7,4 @@ cargo build --target thumbv6m-none-eabi cargo build --target thumbv7em-none-eabi cargo build --target riscv32imc-unknown-none-elf cargo build --target riscv32imac-unknown-none-elf +cargo +nightly-2021-01-07 build -Zbuild-std=core --target avr-specs/avr-atmega328p.json diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 89f0d2e..a407ee4 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -8,4 +8,5 @@ targets = [ "thumbv7em-none-eabi", "riscv32imc-unknown-none-elf", "riscv32imac-unknown-none-elf", + "avr-specs/avr-atmpeg328p.json", ] diff --git a/src/lib.rs b/src/lib.rs index ed0344f..d4d790a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ #![cfg_attr(docsrs, feature(doc_cfg))] #![no_std] -#![feature(llvm_asm)] -#![doc = include_str!("../README.md")] +#![cfg_attr(target_arch = "avr", feature(llvm_asm))] +//#![doc = include_str!("../README.md")] pub use bare_metal::CriticalSection; From 780cebfbbc8e96b854db089eaffa9fb099ac10cf Mon Sep 17 00:00:00 2001 From: Robert Forsman Date: Thu, 7 Apr 2022 15:34:08 -0400 Subject: [PATCH 03/11] oops, forgot the target spec --- avr-specs/avr-atmega328p.json | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 avr-specs/avr-atmega328p.json diff --git a/avr-specs/avr-atmega328p.json b/avr-specs/avr-atmega328p.json new file mode 100644 index 0000000..e236b08 --- /dev/null +++ b/avr-specs/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" +} From c9b6ff4517d94d777681c49599101f5a1260c50d Mon Sep 17 00:00:00 2001 From: Robert Forsman Date: Thu, 7 Apr 2022 15:36:33 -0400 Subject: [PATCH 04/11] it turns out this is how i can make the doc= work --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index d4d790a..747fb92 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,8 @@ #![cfg_attr(docsrs, feature(doc_cfg))] #![no_std] #![cfg_attr(target_arch = "avr", feature(llvm_asm))] -//#![doc = include_str!("../README.md")] +#![cfg_attr(target_arch = "avr", feature(extended_key_value_attributes))] +#![doc = include_str!("../README.md")] pub use bare_metal::CriticalSection; From 397330689090a2c3eb17ce72f1880b091c019b84 Mon Sep 17 00:00:00 2001 From: Robert Forsman Date: Thu, 7 Apr 2022 16:20:19 -0400 Subject: [PATCH 05/11] try to fix how we use github's workflow for compiling with an alternate rust toolchain --- .github/workflows/rust.yml | 11 ++++++++++- ci.sh | 1 - ci2.sh | 5 +++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100755 ci2.sh diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 822777f..8f24dc2 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -16,4 +16,13 @@ jobs: steps: - uses: actions/checkout@v2 - name: Build - run: ./ci.sh \ No newline at end of file + run: ./ci.sh + + - uses: actions/checkout@v2 + - name: Build for AVR with old toolchain + uses: actions-rs/toolchain@v1 + with: + toolchain: nightly-2021-01-07 + components : rust-src + profile : minimal + run: ./ci2.sh diff --git a/ci.sh b/ci.sh index 1b768f2..e9ba8b4 100755 --- a/ci.sh +++ b/ci.sh @@ -7,4 +7,3 @@ cargo build --target thumbv6m-none-eabi cargo build --target thumbv7em-none-eabi cargo build --target riscv32imc-unknown-none-elf cargo build --target riscv32imac-unknown-none-elf -cargo +nightly-2021-01-07 build -Zbuild-std=core --target avr-specs/avr-atmega328p.json diff --git a/ci2.sh b/ci2.sh new file mode 100755 index 0000000..75c38ee --- /dev/null +++ b/ci2.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +set -euxo pipefail + +cargo +nightly-2021-01-07 build -Zbuild-std=core --target avr-specs/avr-atmega328p.json From 7b2111a923cf4ce93841870d2195775bae690805 Mon Sep 17 00:00:00 2001 From: Robert Forsman Date: Thu, 7 Apr 2022 16:27:31 -0400 Subject: [PATCH 06/11] again, try to fix how we use github's workflow for compiling with an alternate rust toolchain --- .github/workflows/rust.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 8f24dc2..2a26d76 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -24,5 +24,7 @@ jobs: with: toolchain: nightly-2021-01-07 components : rust-src + target: avr-specs/avr-atmega328p.json profile : minimal - run: ./ci2.sh + command : build + args: -Zbuild-std=core From 1569ee40e929bd4507ea46c2a7ca77774bf0fed5 Mon Sep 17 00:00:00 2001 From: Robert Forsman Date: Thu, 7 Apr 2022 16:30:36 -0400 Subject: [PATCH 07/11] still trying to zero in on the syntax --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 2a26d76..87a5747 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -27,4 +27,4 @@ jobs: target: avr-specs/avr-atmega328p.json profile : minimal command : build - args: -Zbuild-std=core + args: -- -Zbuild-std=core From 4fe1867ac6e5b8e260a000a1a2b6c67323640489 Mon Sep 17 00:00:00 2001 From: Robert Forsman Date: Thu, 7 Apr 2022 16:33:09 -0400 Subject: [PATCH 08/11] bang on it with a much larger rock --- .github/workflows/rust.yml | 11 ----------- ci.sh | 3 +++ ci2.sh | 5 ----- 3 files changed, 3 insertions(+), 16 deletions(-) delete mode 100755 ci2.sh diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 87a5747..ad95726 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -17,14 +17,3 @@ jobs: - uses: actions/checkout@v2 - name: Build run: ./ci.sh - - - uses: actions/checkout@v2 - - name: Build for AVR with old toolchain - uses: actions-rs/toolchain@v1 - with: - toolchain: nightly-2021-01-07 - components : rust-src - target: avr-specs/avr-atmega328p.json - profile : minimal - command : build - args: -- -Zbuild-std=core diff --git a/ci.sh b/ci.sh index e9ba8b4..eba7a29 100755 --- a/ci.sh +++ b/ci.sh @@ -7,3 +7,6 @@ cargo build --target thumbv6m-none-eabi cargo build --target thumbv7em-none-eabi cargo build --target riscv32imc-unknown-none-elf cargo build --target riscv32imac-unknown-none-elf + +rustup install nightly-2021-01-07 +cargo +nightly-2021-01-07 build -Zbuild-std=core --target avr-specs/avr-atmega328p.json diff --git a/ci2.sh b/ci2.sh deleted file mode 100755 index 75c38ee..0000000 --- a/ci2.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -set -euxo pipefail - -cargo +nightly-2021-01-07 build -Zbuild-std=core --target avr-specs/avr-atmega328p.json From 1266d6df809168a9f038558f97b4d04a4c56f00f Mon Sep 17 00:00:00 2001 From: Robert Forsman Date: Thu, 7 Apr 2022 16:36:39 -0400 Subject: [PATCH 09/11] workflow needs rust-src --- ci.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ci.sh b/ci.sh index eba7a29..15bffa5 100755 --- a/ci.sh +++ b/ci.sh @@ -2,11 +2,12 @@ set -euxo pipefail +rustup install nightly-2021-01-07 +rustup component add rust-src +cargo +nightly-2021-01-07 build -Zbuild-std=core --target avr-specs/avr-atmega328p.json + cargo build cargo build --target thumbv6m-none-eabi cargo build --target thumbv7em-none-eabi cargo build --target riscv32imc-unknown-none-elf cargo build --target riscv32imac-unknown-none-elf - -rustup install nightly-2021-01-07 -cargo +nightly-2021-01-07 build -Zbuild-std=core --target avr-specs/avr-atmega328p.json From e933171eb022bc5d17641378e49ceca65dc4612f Mon Sep 17 00:00:00 2001 From: Robert Forsman Date: Thu, 7 Apr 2022 16:38:50 -0400 Subject: [PATCH 10/11] maybe this is the right invocation --- ci.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ci.sh b/ci.sh index 15bffa5..9a07690 100755 --- a/ci.sh +++ b/ci.sh @@ -2,8 +2,7 @@ set -euxo pipefail -rustup install nightly-2021-01-07 -rustup component add rust-src +rustup install nightly-2021-01-07 --component rust-src cargo +nightly-2021-01-07 build -Zbuild-std=core --target avr-specs/avr-atmega328p.json cargo build From 7f915c4adc14140a7b6fc04dd3f869ce4a10c687 Mon Sep 17 00:00:00 2001 From: Robert Forsman Date: Thu, 7 Apr 2022 16:39:56 -0400 Subject: [PATCH 11/11] oops, forgot to say toolchain --- ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci.sh b/ci.sh index 9a07690..b020404 100755 --- a/ci.sh +++ b/ci.sh @@ -2,7 +2,7 @@ set -euxo pipefail -rustup install nightly-2021-01-07 --component rust-src +rustup toolchain install nightly-2021-01-07 --component rust-src cargo +nightly-2021-01-07 build -Zbuild-std=core --target avr-specs/avr-atmega328p.json cargo build