chore: it works and flashing is possible, even on windows

This commit is contained in:
cscherr 2025-07-14 10:01:05 +02:00
parent f57aa91880
commit aaab15e72d
Signed by: cscherrNT
GPG key ID: 8E2B45BC51A27EA7
11 changed files with 108 additions and 17 deletions

View file

@ -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]

View file

@ -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
```

View file

@ -0,0 +1,3 @@
#include "algorithms.h"
bool algorithms_c_is_loaded() { return true; }

View file

@ -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

View file

@ -1,2 +0,0 @@
#include "crc/crc.h"
#include "hash/hash.h"

View file

@ -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()); }

View file

@ -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() {

View file

@ -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<T: ?Sized>(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())
}
}

View file

@ -12,3 +12,4 @@
pub mod crc;
pub(crate) mod ffi;
pub use ffi::ffi_is_loaded;

6
scripts/copy_to_win.sh Executable file
View file

@ -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

View file

@ -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);