From 7808fb55e3fe7c471db76bdbb376783ed350d2ee Mon Sep 17 00:00:00 2001 From: cscherr Date: Mon, 14 Jul 2025 10:56:45 +0200 Subject: [PATCH] fix: can't call C code from the stm32 chore: vscode debug stuff --- .cargo/config.toml | 1 + .vscode/launch.json | 20 +++++++++ .vscode/tasks.json | 25 +++++++++++ crates/algorithms/algorithms-c/project.yml | 5 +++ .../algorithms/algorithms-c/src/algorithms.c | 2 +- .../algorithms/algorithms-c/src/algorithms.h | 2 +- .../algorithms-c/test/test_algorithms.c | 4 +- crates/algorithms/src/ffi/mod.rs | 4 +- src/main.rs | 43 ++++++++++++++++++- 9 files changed, 99 insertions(+), 7 deletions(-) create mode 100755 .vscode/launch.json create mode 100755 .vscode/tasks.json diff --git a/.cargo/config.toml b/.cargo/config.toml index 23cf55c..74d2107 100755 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -3,6 +3,7 @@ runner = 'probe-rs run --chip STM32L053R8' [alias] arun = "run --target thumbv6m-none-eabi" +abuild = "build --target thumbv6m-none-eabi" atest = "test --target x86_64-unknown-linux-gnu" acheck = "check --target thumbv6m-none-eabi" aclippy = "clippy --target thumbv6m-none-eabi" diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100755 index 0000000..ff9a3ba --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,20 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "preLaunchTask": "${defaultBuildTask}", + "type": "probe-rs-debug", + "request": "launch", + "name": "probe_rs Run and Debug", + "flashingConfig": { + "flashingEnabled": true, + }, + "chip": "STM32L053R8", + "coreConfigs": [ + { + "programBinary": "${workspaceFolder}/target/thumbv6m-none-eabi/debug/nucleo-l053r8-benches", + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100755 index 0000000..63722f6 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,25 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "cargo run", + "type": "shell", + "command": "cargo arun", + "problemMatcher": [ + "$rustc" + ], + }, + { + "label": "cargo build", + "type": "shell", + "command": "cargo build --target thumbv6m-none-eabi", + "problemMatcher": [ + "$rustc" + ], + "group": { + "kind": "build", + "isDefault": true + } + } + ] +} \ No newline at end of file diff --git a/crates/algorithms/algorithms-c/project.yml b/crates/algorithms/algorithms-c/project.yml index f1f38a6..a30b0e4 100755 --- a/crates/algorithms/algorithms-c/project.yml +++ b/crates/algorithms/algorithms-c/project.yml @@ -200,6 +200,11 @@ :exclude_setjmp_h: false # Don't use setjmp when running CMock. Note that this might result in late reporting or out-of-order failures. # Configuration options specific to Unity. + :compile: + - -Wall # all warnings on + - -O3 # optimizations + - -g # debug info + - -march=armv6-m # NOTE: This is essential! It tells the compiler which target architecture to compile for!!! :unity: :defines: - UNITY_EXCLUDE_FLOAT diff --git a/crates/algorithms/algorithms-c/src/algorithms.c b/crates/algorithms/algorithms-c/src/algorithms.c index e1d52e4..da68e00 100644 --- a/crates/algorithms/algorithms-c/src/algorithms.c +++ b/crates/algorithms/algorithms-c/src/algorithms.c @@ -1,3 +1,3 @@ #include "algorithms.h" -bool algorithms_c_is_loaded() { return true; } +int algorithms_c_is_loaded() { return 1; } diff --git a/crates/algorithms/algorithms-c/src/algorithms.h b/crates/algorithms/algorithms-c/src/algorithms.h index dbb136b..a101378 100644 --- a/crates/algorithms/algorithms-c/src/algorithms.h +++ b/crates/algorithms/algorithms-c/src/algorithms.h @@ -7,6 +7,6 @@ #include "stdbool.h" -extern bool algorithms_c_is_loaded(); +extern int algorithms_c_is_loaded(); #endif // ALGORITHMS_H diff --git a/crates/algorithms/algorithms-c/test/test_algorithms.c b/crates/algorithms/algorithms-c/test/test_algorithms.c index 3489b08..dc59782 100644 --- a/crates/algorithms/algorithms-c/test/test_algorithms.c +++ b/crates/algorithms/algorithms-c/test/test_algorithms.c @@ -7,4 +7,6 @@ void setUp(void) {} void tearDown(void) {} -void test_algorithms_loaded(void) { TEST_ASSERT(algorithms_c_is_loaded()); } +void test_algorithms_loaded(void) { + TEST_ASSERT(algorithms_c_is_loaded() == 1); +} diff --git a/crates/algorithms/src/ffi/mod.rs b/crates/algorithms/src/ffi/mod.rs index 376bcb6..1adb782 100755 --- a/crates/algorithms/src/ffi/mod.rs +++ b/crates/algorithms/src/ffi/mod.rs @@ -1,11 +1,11 @@ use core::ffi::c_void; unsafe extern "C" { - fn algorithms_c_is_loaded() -> bool; + fn algorithms_c_is_loaded() -> i32; } pub fn ffi_is_loaded() -> bool { - unsafe { algorithms_c_is_loaded() } + unsafe { algorithms_c_is_loaded() == 1 } } #[inline] diff --git a/src/main.rs b/src/main.rs index 156f9eb..43a2d7a 100755 --- a/src/main.rs +++ b/src/main.rs @@ -3,12 +3,12 @@ use algorithms::crc::{Crc, Crc32 as Crc32Rust, ffi::Crc32 as Crc32C}; -use defmt::{debug, info}; +use defmt::{debug, error, info}; use panic_probe as _; use defmt_rtt as _; // global logger -use cortex_m_rt::entry; +use cortex_m_rt::{ExceptionFrame, entry}; use hal::{pac, prelude::*, rcc::Config}; // same panicking *behavior* as `panic-probe` but doesn't print a panic message @@ -39,6 +39,7 @@ fn main() -> ! { let mut delay = cp.SYST.delay(rcc.clocks); // BUG: this program seems to completely halt whenever a C function is called. + debug!("Asserting that libalgorithms is correctly loaded"); assert!(algorithms::ffi_is_loaded(), "ffi is not loaded?"); let mut crc; @@ -61,3 +62,41 @@ fn main() -> ! { delay.delay_ms(500_u16); } } + +fn display_ef(ef: &ExceptionFrame) { + struct Wrapper(ExceptionFrame); + impl defmt::Format for Wrapper { + fn format(&self, fmt: defmt::Formatter) { + defmt::write!( + fmt, + "ExceptionFrame(\n\tr0: {:08x}\n\tr1: {:08x}\n\tr2: {:08x}\n\tr3: {:08x}\n\tr12: {:08x}\n\tlr: {:08x}\n\tpc: {:08x}\n\txpsr: {:08x}\n)", + self.0.r0(), + self.0.r1(), + self.0.r2(), + self.0.r3(), + self.0.r12(), + self.0.lr(), + self.0.pc(), + self.0.xpsr() + ); + } + } + + error!("HardFault: Exception occured!"); + error!("Exception occured at: {:08x}", ef.pc()); + error!("XSPR: {:032b}", ef.xpsr()); + error!("LR: {:08x}", ef.lr()); + + error!("HardFault: {:#?}", Wrapper(*ef)) +} + +#[allow(unreachable_code)] +#[cortex_m_rt::exception(trampoline = true)] +unsafe fn HardFault(ef: &ExceptionFrame) -> ! { + static mut EXCEPTION: Option = None; + unsafe { EXCEPTION = Some(*ef) }; + + display_ef(ef); + #[allow(clippy::empty_loop)] + loop {} +}