From e0527bf91b0add70f753c2a51033e5e9ee5638b2 Mon Sep 17 00:00:00 2001 From: Ulrich Mohr Date: Fri, 27 Oct 2023 17:02:48 +0200 Subject: [PATCH] rust lib --- CMakeLists.txt | 3 ++- mission/main.cpp | 6 +++++ mission_rust/.cargo/config.toml | 5 ++++ mission_rust/.gitignore | 1 + mission_rust/CMakeLists.txt | 11 +++++++++ mission_rust/Cargo.lock | 16 ++++++++++++ mission_rust/Cargo.toml | 12 +++++++++ mission_rust/src/lib.rs | 43 +++++++++++++++++++++++++++++++++ 8 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 mission_rust/.cargo/config.toml create mode 100644 mission_rust/.gitignore create mode 100644 mission_rust/CMakeLists.txt create mode 100644 mission_rust/Cargo.lock create mode 100644 mission_rust/Cargo.toml create mode 100644 mission_rust/src/lib.rs diff --git a/CMakeLists.txt b/CMakeLists.txt index 0452f9e..4cb09a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -97,6 +97,7 @@ add_subdirectory(fsfw) add_subdirectory(common) add_subdirectory(${MISSION_PATH}) +add_subdirectory(mission_rust) # ############################################################################## # Post-Sources preparation @@ -105,7 +106,7 @@ add_subdirectory(${MISSION_PATH}) # Add libraries for all sources. target_link_libraries(lwip PUBLIC freertos_kernel) target_link_libraries(fsfw PUBLIC lwip) -target_link_libraries(${TARGET_NAME} PUBLIC fsfw lwip) +target_link_libraries(${TARGET_NAME} PUBLIC fsfw lwip mission_rust) target_include_directories( ${TARGET_NAME} PUBLIC ${BSP_PATH}) diff --git a/mission/main.cpp b/mission/main.cpp index ef8e589..29c6390 100644 --- a/mission/main.cpp +++ b/mission/main.cpp @@ -189,10 +189,16 @@ int main(void) { void testIp(); +extern "C" { +void rust_main(double i); +} + void mission(void *){ printf("Starting Mission\n"); + rust_main(123456879123456789); + testIp(); sif::debug << "OStreams working" << std::endl; diff --git a/mission_rust/.cargo/config.toml b/mission_rust/.cargo/config.toml new file mode 100644 index 0000000..9941ccc --- /dev/null +++ b/mission_rust/.cargo/config.toml @@ -0,0 +1,5 @@ +[build] +target = "armv7a-none-eabihf" + +[unstable] +build-std = ["core"] \ No newline at end of file diff --git a/mission_rust/.gitignore b/mission_rust/.gitignore new file mode 100644 index 0000000..9f97022 --- /dev/null +++ b/mission_rust/.gitignore @@ -0,0 +1 @@ +target/ \ No newline at end of file diff --git a/mission_rust/CMakeLists.txt b/mission_rust/CMakeLists.txt new file mode 100644 index 0000000..7093c55 --- /dev/null +++ b/mission_rust/CMakeLists.txt @@ -0,0 +1,11 @@ +add_custom_target( + mission_rust_internal + COMMAND cargo build $<$:--release> + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) + +add_library(mission_rust INTERFACE) + +add_dependencies(mission_rust mission_rust_internal) + +target_link_libraries(mission_rust INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/target/armv7a-none-eabihf/$,release,debug>/libmission_rust.a) \ No newline at end of file diff --git a/mission_rust/Cargo.lock b/mission_rust/Cargo.lock new file mode 100644 index 0000000..4427a22 --- /dev/null +++ b/mission_rust/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cty" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" + +[[package]] +name = "mission_rust" +version = "0.1.0" +dependencies = [ + "cty", +] diff --git a/mission_rust/Cargo.toml b/mission_rust/Cargo.toml new file mode 100644 index 0000000..a625263 --- /dev/null +++ b/mission_rust/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "mission_rust" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["staticlib"] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +cty = "0.2.2" diff --git a/mission_rust/src/lib.rs b/mission_rust/src/lib.rs new file mode 100644 index 0000000..1ca9189 --- /dev/null +++ b/mission_rust/src/lib.rs @@ -0,0 +1,43 @@ +#![no_std] + +use core::panic::PanicInfo; + +#[panic_handler] +fn panic(_panic: &PanicInfo<'_>) -> ! { + loop {} +} + +extern "C" { + pub fn outbyte(c: cty::c_char); +} + +#[no_mangle] +pub extern "C" fn rust_main(i: cty::c_double) { + mission(i); +} + +struct Outbytes {} + +use core::fmt::{Error, Write}; + +impl Write for Outbytes { + fn write_str(&mut self, s: &str) -> Result<(), Error> { + for c in s.as_bytes() { + unsafe { + outbyte(*c); + } + } + Ok(()) + } +} + +fn mission(i: f64) { + for byte in "abcd\n".bytes() { + unsafe { + outbyte(byte); + } + } + let mut stdout = Outbytes {}; + + let _ok = writeln!(&mut stdout, "I got {}!", i); +}