fix: can't call C code from the stm32

chore: vscode debug stuff
This commit is contained in:
cscherr 2025-07-14 10:56:45 +02:00
parent aaab15e72d
commit 7808fb55e3
Signed by: cscherrNT
GPG key ID: 8E2B45BC51A27EA7
9 changed files with 99 additions and 7 deletions

View file

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

20
.vscode/launch.json vendored Executable file
View file

@ -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",
}
]
}
]
}

25
.vscode/tasks.json vendored Executable file
View file

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

View file

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

View file

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

View file

@ -7,6 +7,6 @@
#include "stdbool.h"
extern bool algorithms_c_is_loaded();
extern int algorithms_c_is_loaded();
#endif // ALGORITHMS_H

View file

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

View file

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

View file

@ -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<ExceptionFrame> = None;
unsafe { EXCEPTION = Some(*ef) };
display_ef(ef);
#[allow(clippy::empty_loop)]
loop {}
}