diff --git a/components/rustlib/src/lib.rs b/components/rustlib/src/lib.rs index ad14f10..a2f83e6 100644 --- a/components/rustlib/src/lib.rs +++ b/components/rustlib/src/lib.rs @@ -1,8 +1,11 @@ #![cfg_attr(not(feature = "std"), no_std)] +#![feature(asm)] + #[cfg(not(feature = "std"))] use core::panic::PanicInfo; + /// Create aliases for FFI types for esp32c3, which doesn't have std. #[cfg(not(feature = "std"))] mod ffi { @@ -31,6 +34,19 @@ pub extern "C" fn add_in_rust(x: i32, y: i32) -> i32 { x + y } +#[no_mangle] +pub extern "C" fn add_in_rust_inline_asm(mut x: i32, y: i32) -> i32 { + unsafe { + sys::validate_param_in_c(0, x); + sys::validate_param_in_c(1, y); + } + unsafe { + // more detail available: https://doc.rust-lang.org/beta/unstable-book/library-features/asm.html + asm!("add {0}, {0}, {1}", inout(reg) x, in(reg) y); + } + x +} + #[cfg(not(feature = "std"))] #[panic_handler] fn panic(_info: &PanicInfo) -> ! { diff --git a/main/main.c b/main/main.c index 92e6838..eac3bba 100644 --- a/main/main.c +++ b/main/main.c @@ -40,6 +40,11 @@ void app_main(void) int sum = add_in_rust(x, y); printf("Rust calculated %d + %d = %d\n\n", x, y, sum); + x = 9; + y = 10; + sum = add_in_rust_inline_asm(x, y); + printf("Rust calculated (using asm!) %d + %d = %d\n\n", x, y, sum); + for (int i = 10; i >= 0; i--) { printf("Restarting in %d seconds...\n", i); vTaskDelay(1000 / portTICK_PERIOD_MS);