Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support/avr platform #12

Merged
merged 12 commits into from
Apr 8, 2022
27 changes: 27 additions & 0 deletions avr-specs/avr-atmega328p.json
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to use such an old nightly?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Attempting to use a modern version of rust fails with
error: ran out of registers during register allocation
It may be covered by rust-lang/rust#88252 , but Rust on AVR has been waiting for a fix for a long time and has to muddle along with old tools.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I see. No problem then, was just curious :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it is possible to just rustup install nightly-2021-01-07 in the ci.sh, but I don't know how to test it without pushing another commit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment in the main thread. also feel free to push as many commits as you need! you can squash/amend later.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a way to make ci.sh work. I just did a rustup toolchain install nightly-2021-01-07 --component rust-src before I compile for AVR. There might be a more elegant way to do it, but I do not know what it would be.

1 change: 1 addition & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ targets = [
"thumbv7em-none-eabi",
"riscv32imc-unknown-none-elf",
"riscv32imac-unknown-none-elf",
"avr-specs/avr-atmpeg328p.json",
]
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![no_std]
#![cfg_attr(target_arch = "avr", feature(llvm_asm))]
#![cfg_attr(target_arch = "avr", feature(extended_key_value_attributes))]
#![doc = include_str!("../README.md")]

pub use bare_metal::CriticalSection;
Expand Down Expand Up @@ -118,6 +120,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!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to use the newer asm!, or is it not supported on AVR?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last I checked asm! was not supported by Rust for AVR target.

Also, the last time I dealt with assembly language was 1987 on an 8086.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, just wanted to confirm!

Also, the last time I dealt with assembly language was 1987 on an 8086.

😂

"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 {
Expand Down