diff options
| author | zhang san <[email protected]> | 2025-12-08 18:34:25 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-12-08 18:34:25 +0800 |
| commit | 69980f8b9d29b38dbd4ddf6373b4b45f3a138409 (patch) | |
| tree | 21dd9a8ce80e3d30192e9ffa56ea39a199bae309 /components/rust/examples/application | |
| parent | cd1d47b87c5d47e131730e5506eb3ad77d357653 (diff) | |
Adding Rust Language Support for RT-Thread #10910
Diffstat (limited to 'components/rust/examples/application')
15 files changed, 480 insertions, 0 deletions
diff --git a/components/rust/examples/application/SConscript b/components/rust/examples/application/SConscript new file mode 100644 index 0000000000..c6c4de3db8 --- /dev/null +++ b/components/rust/examples/application/SConscript @@ -0,0 +1,70 @@ +import os +import sys +from building import * + +cwd = GetCurrentDir() + +# Import usrapp build module and build support +sys.path.append(os.path.join(cwd, '../../tools')) +from build_usrapp import build_example_usrapp +from build_support import clean_rust_build + + +def _has(sym: str) -> bool: + try: + return bool(GetDepend([sym])) + except Exception: + return bool(GetDepend(sym)) + + +def load_extended_feature_configs(): + """ + Load extended feature configurations if available. + This allows users to add custom configuration mappings. + """ + try: + from feature_config_examples import setup_all_example_features + setup_all_example_features() + print("Extended feature configurations loaded successfully.") + except ImportError: + print("Using default feature configurations.") + +group = [] + +if not _has('RT_RUST_BUILD_APPLICATIONS'): + Return('group') + +# Load extended feature configurations +load_extended_feature_configs() + +src = [] +LIBS = [] +LIBPATH = [] +LINKFLAGS = "" + +if GetOption('clean'): + app_build_dir = clean_rust_build(Dir('#').abspath, "example_usrapp") + if os.path.exists(app_build_dir): + print(f'Registering {app_build_dir} for cleanup') + Clean('.', app_build_dir) + else: + print('No example_usrapp build artifacts to clean') +else: + import rtconfig + LIBS, LIBPATH, LINKFLAGS = build_example_usrapp( + cwd=cwd, + has_func=_has, + rtconfig=rtconfig, + build_root=os.path.join(Dir('#').abspath, "build", "example_usrapp") + ) + +group = DefineGroup( + 'example_usrapp', + src, + depend=['RT_USING_RUST'], + LIBS=LIBS, + LIBPATH=LIBPATH, + LINKFLAGS=LINKFLAGS +) + +Return('group')
\ No newline at end of file diff --git a/components/rust/examples/application/fs/Cargo.toml b/components/rust/examples/application/fs/Cargo.toml new file mode 100644 index 0000000000..2556b5f0a4 --- /dev/null +++ b/components/rust/examples/application/fs/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "em_fs" +version = "0.1.0" +edition = "2021" + +[lib] +name = "em_fs" +crate-type = ["staticlib"] + +[features] +default = [] +enable-log = ["em_component_log/enable-log"] + +[dependencies] +rt-rust = { path = "../../../core" } +rt_macros = { path = "../../../rt_macros" } +em_component_log = { path = "../../component/log"}
\ No newline at end of file diff --git a/components/rust/examples/application/fs/src/lib.rs b/components/rust/examples/application/fs/src/lib.rs new file mode 100644 index 0000000000..53cf8561d1 --- /dev/null +++ b/components/rust/examples/application/fs/src/lib.rs @@ -0,0 +1,73 @@ + +/* + * Copyright (c) 2006-2025, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author notes + * 2025-10-20 foxglove Rust file operation test. + * 2025-10-29 foxglove Updated to demonstrate new modular macro interface + */ +#![no_std] + +extern crate alloc; + +use rt_macros::msh_cmd_export; +use rt_rust::{fs, println}; +use rt_rust::param::Param; +use em_component_log::{info, error}; + +#[msh_cmd_export(name = "rust_file_demo", desc = "Rust example app.")] +fn main(_param: Param) { + println!("[rust_file_test] start"); + + let mut file = match fs::OpenOptions::new() + .read(true) + .write(true) + .create(true) + .append(false) + .truncate(true) + .open("test.txt") + { + Ok(f) => { + info!("open test.txt ok"); + f + } + Err(e) => { + error!("open error: {:?}", e); + return; + } + }; + + if let Err(e) = file.write_all("Hello from FS wrapper!\n") { + error!("write_all error: {:?}", e); + return; + } + info!("write_all done"); + + if let Err(e) = file.flush() { + error!("flush error: {:?}", e); + } else { + info!("flush ok"); + } + + match file.read_to_string() { + Ok(s) => info!("read_back: {}", s), + Err(e) => error!("read_to_string error: {:?}", e), + } + + if let Err(e) = file.set_len(5) { + error!("truncate error: {:?}", e); + } else { + info!("truncate to 5 ok"); + } + + if let Err(e) = file.close() { + error!("close error: {:?}", e); + } else { + info!("close ok"); + } + + info!("[rust_file_test] end"); +} diff --git a/components/rust/examples/application/loadlib/Cargo.toml b/components/rust/examples/application/loadlib/Cargo.toml new file mode 100644 index 0000000000..1e1065f91a --- /dev/null +++ b/components/rust/examples/application/loadlib/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "em_loadlib" +version = "0.1.0" +edition = "2021" + +[lib] +name = "em_loadlib" +crate-type = ["staticlib"] + +[dependencies] +rt-rust = { path = "../../../core" } +rt_macros = { path = "../../../rt_macros" }
\ No newline at end of file diff --git a/components/rust/examples/application/loadlib/src/lib.rs b/components/rust/examples/application/loadlib/src/lib.rs new file mode 100644 index 0000000000..d60175f5fa --- /dev/null +++ b/components/rust/examples/application/loadlib/src/lib.rs @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2006-2025, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author notes + * 2025-10-10 foxglove load library example + * 2025-10-29 foxglove Updated to demonstrate new modular macro interface + */ +#![no_std] + +extern crate alloc; + +use rt_macros::msh_cmd_export; +use rt_rust::println; +use rt_rust::get_libfn; +use core::ffi::{c_int, c_char}; +use rt_rust::param::Param; + +#[msh_cmd_export(name = "rust_dl_demo", desc = "Rust dynamic library demo")] +fn main(_param: Param) { + println!("\n=== Dynamic Library Demo ==="); + + get_libfn!("/hello.mo", "main", my_hello, ()); + my_hello(); + + get_libfn!("/libmylib.mo", "rust_mylib_add", my_add, c_int, a: c_int, b: c_int); + let s = my_add(15, 20); + println!("my_add(15, 20) = {}", s); + + get_libfn!("/libmylib.mo", "rust_mylib_println", my_println, (), s: *const c_char); + my_println(b"rustlib: Hello World\0".as_ptr() as *const c_char); +} diff --git a/components/rust/examples/application/mutex/Cargo.toml b/components/rust/examples/application/mutex/Cargo.toml new file mode 100644 index 0000000000..188ad1ba1d --- /dev/null +++ b/components/rust/examples/application/mutex/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "em_mutex" +version = "0.1.0" +edition = "2021" + +[lib] +name = "em_mutex" +crate-type = ["staticlib"] + +[dependencies] +rt-rust = { path = "../../../core" } +rt_macros = { path = "../../../rt_macros" } diff --git a/components/rust/examples/application/mutex/src/lib.rs b/components/rust/examples/application/mutex/src/lib.rs new file mode 100644 index 0000000000..3160ab6b76 --- /dev/null +++ b/components/rust/examples/application/mutex/src/lib.rs @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2006-2025, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author notes + * 2025-10-10 foxglove mutex test demo + * 2025-10-29 foxglove Updated to demonstrate new modular macro interface + */ +#![no_std] + +extern crate alloc; + +use alloc::sync::Arc; +use core::time::Duration; +use rt_macros::msh_cmd_export; +use rt_rust::mutex::Mutex; +use rt_rust::param::Param; +use rt_rust::println; +use rt_rust::thread; +use rt_rust::time; + +#[msh_cmd_export(name = "rust_mutex_demo", desc = "Rust example app.")] +fn main(_param: Param) { + let counter = Arc::new(Mutex::new(0).unwrap()); + let run = move || loop { + time::sleep(Duration::new(2, 0)); + { + let mut c = counter.lock().unwrap(); + *c += 1; + println!("{}", *c); + } + }; + + let _ = thread::Thread::new() + .name("thread 1") + .stack_size(2048) + .start(run.clone()); + time::sleep(Duration::new(1, 0)); + let _ = thread::Thread::new() + .name("thread 2") + .stack_size(2048) + .start(run.clone()); +} diff --git a/components/rust/examples/application/param/Cargo.toml b/components/rust/examples/application/param/Cargo.toml new file mode 100644 index 0000000000..aa3daae8c9 --- /dev/null +++ b/components/rust/examples/application/param/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "em_param" +version = "0.1.0" +edition = "2021" + +[lib] +name = "em_param" +crate-type = ["staticlib"] + +[dependencies] +rt-rust = { path = "../../../core" } +rt_macros = { path = "../../../rt_macros" } + diff --git a/components/rust/examples/application/param/src/lib.rs b/components/rust/examples/application/param/src/lib.rs new file mode 100644 index 0000000000..209daa14f1 --- /dev/null +++ b/components/rust/examples/application/param/src/lib.rs @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2006-2025, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author notes + * 2025-10-10 foxglove param test demo + * 2025-10-29 foxglove Updated to demonstrate new modular macro interface + */ +#![no_std] + +extern crate alloc; + +use alloc::string::String; +use rt_macros::{rt_app_export, msh_cmd_export}; +use rt_rust::param::Param; +use rt_rust::println; + +// 演示新的模块化宏接口 - 使用 msh_cmd_export 导出参数测试命令 +#[msh_cmd_export(name = "rust_param_demo", desc = "Rust parameter demo")] +fn main(param: Param) { + for i in param { + println!("{}", String::from_utf8_lossy(&*i)) + } +} diff --git a/components/rust/examples/application/queue/Cargo.toml b/components/rust/examples/application/queue/Cargo.toml new file mode 100644 index 0000000000..4ea390d183 --- /dev/null +++ b/components/rust/examples/application/queue/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "em_queue" +version = "0.1.0" +edition = "2021" + +[lib] +name = "em_queue" +crate-type = ["staticlib"] + +[dependencies] +rt-rust = { path = "../../../core" } +rt_macros = { path = "../../../rt_macros" } diff --git a/components/rust/examples/application/queue/src/lib.rs b/components/rust/examples/application/queue/src/lib.rs new file mode 100644 index 0000000000..fda7c7c0f3 --- /dev/null +++ b/components/rust/examples/application/queue/src/lib.rs @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2006-2025, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author notes + * 2025-10-10 foxglove queue test demo + * 2025-10-29 foxglove Updated to demonstrate new modular macro interface + */ +#![no_std] + +extern crate alloc; + +use alloc::string::String; +use alloc::sync::Arc; +use core::time::Duration; +use rt_macros::msh_cmd_export; +use rt_rust::queue::Queue; +use rt_rust::param::Param; +use rt_rust::println; +use rt_rust::thread; +use rt_rust::time; + +#[msh_cmd_export(name = "rust_queue_demo", desc = "Rust example app.")] +fn main(_param: Param) { + let send = Arc::new(Queue::new(2).unwrap()); + let recv = send.clone(); + + let _ = thread::Thread::new() + .name("thread 1") + .stack_size(1024) + .start(move || { + loop { + time::sleep(Duration::new(1, 0)); + send.send(String::from("msg"), 0).unwrap(); + } + }); + time::sleep(Duration::new(1, 0)); + let _ = thread::Thread::new() + .name("thread 2") + .stack_size(1024) + .start(move || { + loop { + println!("waiting!"); + let a = recv.recv_wait_forever().unwrap(); + println!("recv {}", a); + } + }); +} + diff --git a/components/rust/examples/application/semaphore/Cargo.toml b/components/rust/examples/application/semaphore/Cargo.toml new file mode 100644 index 0000000000..16aa670145 --- /dev/null +++ b/components/rust/examples/application/semaphore/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "em_sem" +version = "0.1.0" +edition = "2021" + +[lib] +name = "em_sem" +crate-type = ["staticlib"] + +[dependencies] +rt-rust = { path = "../../../core" } +rt_macros = { path = "../../../rt_macros" } diff --git a/components/rust/examples/application/semaphore/src/lib.rs b/components/rust/examples/application/semaphore/src/lib.rs new file mode 100644 index 0000000000..986acd1957 --- /dev/null +++ b/components/rust/examples/application/semaphore/src/lib.rs @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2006-2025, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author notes + * 2025-10-10 foxglove semaphore test demo + * 2025-10-29 foxglove Updated to demonstrate new modular macro interface + */ +#![no_std] + +extern crate alloc; + +use alloc::sync::Arc; +use core::time::Duration; +use rt_macros::msh_cmd_export; +use rt_rust::sem::Semaphore; +use rt_rust::param::Param; +use rt_rust::println; +use rt_rust::thread; +use rt_rust::time; + +#[msh_cmd_export(name = "rust_sem_demo", desc = "Rust example app.")] +fn main(_param: Param) { + let send = Arc::new(Semaphore::new().unwrap()); + let recv = send.clone(); + + let _ = thread::Thread::new() + .name("thread 1") + .stack_size(1024) + .start(move || { + loop { + time::sleep(Duration::new(1, 0)); + send.release() + } + }); + time::sleep(Duration::new(1, 0)); + let _ = thread::Thread::new() + .name("thread 2") + .stack_size(1024) + .start(move || { + loop { + println!("waiting!"); + recv.take_wait_forever().unwrap(); + println!("recv a sem!") + } + }); +} + diff --git a/components/rust/examples/application/thread/Cargo.toml b/components/rust/examples/application/thread/Cargo.toml new file mode 100644 index 0000000000..a85e275c77 --- /dev/null +++ b/components/rust/examples/application/thread/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "em_thread" +version = "0.1.0" +edition = "2021" + +[lib] +name = "em_thread" +crate-type = ["staticlib"] + +[dependencies] +rt-rust = { path = "../../../core" } +rt_macros = { path = "../../../rt_macros" } diff --git a/components/rust/examples/application/thread/src/lib.rs b/components/rust/examples/application/thread/src/lib.rs new file mode 100644 index 0000000000..7f2cdf04c3 --- /dev/null +++ b/components/rust/examples/application/thread/src/lib.rs @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2006-2025, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author notes + * 2025-10-10 foxglove thread test demo + * 2025-10-29 foxglove Updated to demonstrate new modular macro interface + */ +#![no_std] + +use core::time::Duration; +use rt_macros::msh_cmd_export; +use rt_rust::param::Param; +use rt_rust::println; +use rt_rust::thread; +use rt_rust::time; + +#[msh_cmd_export(name = "rust_thread_demo", desc = "Rust example app.")] +fn main(_param: Param) { + let _ = thread::Thread::new() + .name("thread 1") + .stack_size(1024) + .start(move || { + loop { + println!("thread a will sleep 1s"); + time::sleep(Duration::new(1, 0)); + } + }); + + let _ = thread::Thread::new() + .name("thread 2") + .stack_size(1024) + .start(move || { + loop { + println!("thread b will sleep 3s"); + time::sleep(Duration::new(3, 0)); + } + }); +} |
