Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions Justfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
test:
test *args:
cd avr-tester-fixtures \
&& cargo build --release

cargo test --release --workspace
cargo test --release --workspace -- {{ args }}

test-ccb *args:
cd avr-tester-fixtures \
&& cargo build --release --features custom-compiler-builtins

cargo test --release --workspace -- {{ args }}

clean:
cargo clean
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use avr_tester::*;
fn avr() -> AvrTester {
AvrTester::atmega328p()
.with_clock_of_16_mhz()
.load("../../firmware/target/atmega328p/release/firmware.elf")
.load("../../firmware/target/avr-none/release/firmware.elf")
}

// Assuming `firmware` implements a ROT-13 encoder:
Expand Down Expand Up @@ -76,7 +76,7 @@ fn long_text() {
}
```

... having the tests ready, just run `cargo test` inside `firmware-tests` :-)
... having the tests ready, just run `cargo test` inside `firmware-tests`.

Since AvrTester emulates an actual AVR, you don't have to modify your firmware
at all - it can use timers, GPIOs etc. and everything should just work ™.
Expand Down
3 changes: 2 additions & 1 deletion avr-tester-fixtures/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[build]
target = "./.cargo/targets/atmega328p.json"
target = "avr-none"
rustflags = ["-C", "target-cpu=atmega328p"]

[unstable]
build-std = ["core"]
25 changes: 0 additions & 25 deletions avr-tester-fixtures/.cargo/targets/atmega328p.json

This file was deleted.

6 changes: 6 additions & 0 deletions avr-tester-fixtures/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions avr-tester-fixtures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ path = "src/acc_bits.rs"
name = "acc-eval"
path = "src/acc_eval.rs"

[[bin]]
name = "acc-fractal"
path = "src/acc_fractal.rs"

[[bin]]
name = "analog-pins"
path = "src/analog_pins.rs"
Expand Down Expand Up @@ -48,6 +52,18 @@ avr-hal-generic = { git = "https://github.com/Rahix/avr-hal" }
embedded-hal = "0.2.3"
panic-halt = "0.2"

[dependencies.custom-compiler-builtins]
package = "compiler_builtins"
optional = true

# Override this dependency if you want to test avr-tester with customized
# compiler-builtins - then use `just test-ccb`:
git = "https://github.com/rust-lang/compiler-builtins"
# path = "/home/pwy/t/compiler-builtins"

[features]
custom-compiler-builtins = ["dep:custom-compiler-builtins"]

[profile.release]
codegen-units = 1
lto = "fat"
Expand Down
3 changes: 3 additions & 0 deletions avr-tester-fixtures/src/acc_bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#![no_std]
#![no_main]

#[cfg(feature = "custom-compiler-builtins")]
extern crate custom_compiler_builtins;

use atmega_hal::{pins, Peripherals};
use avr_hal_generic::hal::digital::v2::OutputPin;
use panic_halt as _;
Expand Down
85 changes: 31 additions & 54 deletions avr-tester-fixtures/src/acc_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#![no_std]
#![no_main]

#[cfg(feature = "custom-compiler-builtins")]
extern crate custom_compiler_builtins;

use atmega_hal::clock::MHz16;
use atmega_hal::usart::{BaudrateExt, Usart0};
use atmega_hal::{pins, Peripherals};
Expand Down Expand Up @@ -39,6 +42,34 @@ fn main() -> ! {
}
}

fn eval<T>(uart: UartMut<'_>) -> T
where
T: Number,
{
let tok = Token::read(uart);

if let Token::Const = tok {
return T::read(uart);
}

let lhs = eval::<T>(uart);
let rhs = eval::<T>(uart);

match tok {
Token::Add => lhs + rhs,
Token::Sub => lhs - rhs,
Token::Mul => lhs * rhs,
Token::Div => lhs / rhs,
Token::Rem => lhs % rhs,

Token::Const => {
unreachable!()
}
}
}

type UartMut<'a> = &'a mut Usart0<MHz16>;

// ----

enum Type {
Expand Down Expand Up @@ -97,38 +128,6 @@ impl Readable for Token {
}
}

// ----

fn eval<T>(uart: UartMut<'_>) -> T
where
T: Number,
{
let tok = Token::read(uart);

if let Token::Const = tok {
return T::read(uart);
}

let lhs = eval::<T>(uart);
let rhs = eval::<T>(uart);

match tok {
Token::Add => lhs + rhs,
Token::Sub => lhs - rhs,
Token::Mul => lhs * rhs,
Token::Div => lhs / rhs,
Token::Rem => lhs % rhs,

Token::Const => {
unreachable!()
}
}
}

// -----

type UartMut<'a> = &'a mut Usart0<MHz16>;

// -----

trait Readable {
Expand Down Expand Up @@ -194,25 +193,3 @@ macro_rules! numbers {
}

numbers!([u8, i8, u16, i16, u32, i32, u64, i64, u128, i128]);

// -----

#[no_mangle]
extern "C" fn __divti3() {
panic!("not supported");
}

#[no_mangle]
extern "C" fn __modti3() {
panic!("not supported");
}

#[no_mangle]
extern "C" fn __udivti3() {
panic!("not supported");
}

#[no_mangle]
extern "C" fn __umodti3() {
panic!("not supported");
}
69 changes: 69 additions & 0 deletions avr-tester-fixtures/src/acc_fractal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//! See: [../../avr-tester/tests/acceptance/fractal.rs].

#![no_std]
#![no_main]

#[cfg(feature = "custom-compiler-builtins")]
extern crate custom_compiler_builtins;

use atmega_hal::clock::MHz16;
use atmega_hal::usart::{BaudrateExt, Usart0};
use atmega_hal::{pins, Peripherals};
use panic_halt as _;

#[atmega_hal::entry]
fn main() -> ! {
let dp = Peripherals::take().unwrap();
let pins = pins!(dp);

let mut uart = Usart0::<MHz16>::new(
dp.USART0,
pins.pd0,
pins.pd1.into_output(),
115200u32.into_baudrate(),
);

mandelbrot(&mut uart, 40, 20, -2.05, -1.12, 0.47, 1.12, 50);

loop {
//
}
}

fn mandelbrot(
uart: &mut Usart0<MHz16>,
width: i64,
height: i64,
x1: f32,
y1: f32,
x2: f32,
y2: f32,
max_iters: i64,
) {
for viewport_y in 0..height {
let y0 = y1 + (y2 - y1) * ((viewport_y as f32) / (height as f32));

for viewport_x in 0..width {
let x0 = x1 + (x2 - x1) * ((viewport_x as f32) / (width as f32));

let mut x = 0.0;
let mut y = 0.0;
let mut iters = max_iters;

while x * x + y * y <= 4.0 && iters > 0 {
let xtemp = x * x - y * y + x0;

y = 2.0 * x * y + y0;
x = xtemp;
iters -= 1;
}

let ch = (8.0 * ((iters as f32) / (max_iters as f32))) as usize;
let ch = b"#%=-:,. "[ch];

uart.write_byte(ch);
}

uart.write_byte(b'\n');
}
}
3 changes: 3 additions & 0 deletions avr-tester-fixtures/src/analog_pins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#![no_std]
#![no_main]

#[cfg(feature = "custom-compiler-builtins")]
extern crate custom_compiler_builtins;

use atmega_hal::adc::{AdcSettings, ReferenceVoltage};
use atmega_hal::clock::MHz16;
use atmega_hal::{pins, Adc, Peripherals};
Expand Down
3 changes: 3 additions & 0 deletions avr-tester-fixtures/src/digital_pins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#![no_std]
#![no_main]

#[cfg(feature = "custom-compiler-builtins")]
extern crate custom_compiler_builtins;

use atmega_hal::clock::MHz16;
use atmega_hal::delay::Delay;
use atmega_hal::{pins, Peripherals};
Expand Down
Loading