diff --git a/.cargo/config.toml b/.cargo/config.toml index 90b5161..23cf55c 100755 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -4,6 +4,8 @@ runner = 'probe-rs run --chip STM32L053R8' [alias] arun = "run --target thumbv6m-none-eabi" atest = "test --target x86_64-unknown-linux-gnu" +acheck = "check --target thumbv6m-none-eabi" +aclippy = "clippy --target thumbv6m-none-eabi" cflash = "flash --chip STM32L053R8" [env] diff --git a/README.md b/README.md index e69de29..5423f5c 100755 --- a/README.md +++ b/README.md @@ -0,0 +1,20 @@ +# Crcbench + +This is a benchmark for STM32 Microcontrollers, to compare the performance of +Rust programs with C programs. + +## Building + +See [./scripts/build.sh]. + +### Dependencies + +In addition to some crates from crates.io, this project requires the following +to be installed to compile and link to the algorithms-c implementations. + +``` +# on debian +apt install binutils-arm-none-eabi gcc-arm-none-eabi # compiler for the target architecture +apt install ruby # ceedling is the used build system for algorithms-c, and needs to be installed with gem, the ruby package manager +gem install ceedling +``` diff --git a/crates/algorithms/algorithms-c/src/algorithms.c b/crates/algorithms/algorithms-c/src/algorithms.c new file mode 100644 index 0000000..e1d52e4 --- /dev/null +++ b/crates/algorithms/algorithms-c/src/algorithms.c @@ -0,0 +1,3 @@ +#include "algorithms.h" + +bool algorithms_c_is_loaded() { return true; } diff --git a/crates/algorithms/algorithms-c/src/algorithms.h b/crates/algorithms/algorithms-c/src/algorithms.h new file mode 100644 index 0000000..dbb136b --- /dev/null +++ b/crates/algorithms/algorithms-c/src/algorithms.h @@ -0,0 +1,12 @@ + +#ifndef ALGORITHMS_H +#define ALGORITHMS_H + +#include "crc/crc.h" +#include "hash/hash.h" + +#include "stdbool.h" + +extern bool algorithms_c_is_loaded(); + +#endif // ALGORITHMS_H diff --git a/crates/algorithms/algorithms-c/src/benches.h b/crates/algorithms/algorithms-c/src/benches.h deleted file mode 100755 index 46e7e6f..0000000 --- a/crates/algorithms/algorithms-c/src/benches.h +++ /dev/null @@ -1,2 +0,0 @@ -#include "crc/crc.h" -#include "hash/hash.h" diff --git a/crates/algorithms/algorithms-c/test/test_algorithms.c b/crates/algorithms/algorithms-c/test/test_algorithms.c new file mode 100644 index 0000000..3489b08 --- /dev/null +++ b/crates/algorithms/algorithms-c/test/test_algorithms.c @@ -0,0 +1,10 @@ + +#include "unity.h" + +#include "algorithms.h" + +void setUp(void) {} + +void tearDown(void) {} + +void test_algorithms_loaded(void) { TEST_ASSERT(algorithms_c_is_loaded()); } diff --git a/crates/algorithms/build.rs b/crates/algorithms/build.rs index 68c43b6..b693c3c 100755 --- a/crates/algorithms/build.rs +++ b/crates/algorithms/build.rs @@ -2,13 +2,33 @@ fn env(s: &str) -> String { std::env::var(s).unwrap() } +// do yourself a favor if you can, don't use windows fn main() { - std::process::Command::new("ceedling") - .arg("clobber") - .current_dir("./algorithms-c/") - .status() - .expect("could not cleanup old algorithms-c files"); + #[cfg(target_os = "windows")] + { + println!(concat!( + "NOTE: Windows can't easily find what to execute for 'ceedling' (the build system for algorithms-c).\n", + "You may need to set it's path in crates/algorithms/build.rs" + )); + std::process::Command::new("ceedling.bat") + .arg("clobber") + .current_dir("./algorithms-c/") + .status() + .expect("could not cleanup old algorithms-c files"); + } + #[cfg(target_os = "linux")] + { + std::process::Command::new("ceedling") + .arg("clobber") + .current_dir("./algorithms-c/") + .status() + .expect("could not cleanup old algorithms-c files"); + } + + #[cfg(target_os = "windows")] + let mut cmd = std::process::Command::new("ceedling.bat"); + #[cfg(target_os = "linux")] let mut cmd = std::process::Command::new("ceedling"); cmd.arg("release"); @@ -19,14 +39,15 @@ fn main() { if arch == "arm" && pw == "32" && os == "none" { cmd.env("CC", "arm-none-eabi-gcc") .env("AR", "arm-none-eabi-ar"); - } else if os == "linux" { + } else if os == "linux" || os == "windows" { cmd.env("CC", "gcc").env("AR", "ar"); } else { panic!("Unsupported build target") } + println!("Cwd: {}", std::env::current_dir().unwrap().display()); let status = cmd - .current_dir("./algorithms-c/") + .current_dir("./algorithms-c") .status() .expect("could not make c stuff"); if !status.success() { diff --git a/crates/algorithms/src/ffi/mod.rs b/crates/algorithms/src/ffi/mod.rs index 5890b9e..376bcb6 100755 --- a/crates/algorithms/src/ffi/mod.rs +++ b/crates/algorithms/src/ffi/mod.rs @@ -1,6 +1,24 @@ use core::ffi::c_void; +unsafe extern "C" { + fn algorithms_c_is_loaded() -> bool; +} + +pub fn ffi_is_loaded() -> bool { + unsafe { algorithms_c_is_loaded() } +} + #[inline] pub(crate) fn ref_to_voidptr(r: &T) -> *const c_void { r as *const T as *const c_void } + +#[cfg(test)] +mod test { + use crate::ffi_is_loaded; + + #[test] + fn test_ffi_loaded() { + assert!(ffi_is_loaded()) + } +} diff --git a/crates/algorithms/src/lib.rs b/crates/algorithms/src/lib.rs index 28b4a53..f6da675 100755 --- a/crates/algorithms/src/lib.rs +++ b/crates/algorithms/src/lib.rs @@ -12,3 +12,4 @@ pub mod crc; pub(crate) mod ffi; +pub use ffi::ffi_is_loaded; diff --git a/scripts/copy_to_win.sh b/scripts/copy_to_win.sh new file mode 100755 index 0000000..34e1a35 --- /dev/null +++ b/scripts/copy_to_win.sh @@ -0,0 +1,6 @@ +#!/bin/bash +set -e +EPATH=/mnt/c/Users/cscherr/Documents/code/rust/nucleo-l053r8-crcbench +rsync --cvs-exclude --recursive --links --copy-links --filter=':- .gitignore' \ + --verbose --human-readable --partial --progress \ + "$PWD/" $EPATH diff --git a/src/main.rs b/src/main.rs index 7e64426..156f9eb 100755 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use algorithms::crc::{Crc, Crc32 as Crc32Rust, ffi::Crc32 as Crc32C}; -use defmt::{debug, error, info, println, trace, warn}; +use defmt::{debug, info}; use panic_probe as _; use defmt_rtt as _; // global logger @@ -38,22 +38,22 @@ fn main() -> ! { // Get the delay provider. let mut delay = cp.SYST.delay(rcc.clocks); + // BUG: this program seems to completely halt whenever a C function is called. + assert!(algorithms::ffi_is_loaded(), "ffi is not loaded?"); + let mut crc; #[allow(clippy::never_loop)] loop { - println!("Hello World!"); - trace!("trace log"); - debug!("debug log"); - info!("info log"); - warn!("warn log"); - error!("error log"); - led.set_high().unwrap(); delay.delay_ms(500_u16); + info!("Calculating CRCs for: {:?}", DATA); + + debug!("Now calculating Crc32Rust..."); crc = Crc32Rust::checksum(DATA); info!("CRC (Rust): {:08x}", crc); + debug!("Now calculating Crc32C..."); crc = Crc32C::checksum(DATA); info!("CRC (C) : {:08x}", crc);