Skip to main content

xwrust/
lib.rs

1//! XWOS RUST
2//! ========
3//!
4//! # 简介
5//!
6//! XWOS RUST是Rust语言 `#![no_std]` 环境下的XWOS框架,可让用户使用RUST语言开发RTOS的应用。
7//!
8//! 在编写XWOS RUST时,作者充分阅读并参考了RUST语言std库的代码,尽量仿照std库的形式提供API。
9//!
10//!
11//! 传统RUST程序的入口是 `main.rs` 的 `fn main()` ,
12//! XWOS RUST的入口是 `pub unsafe extern "C" fn xwrust_main()` 。
13//!
14//!
15//! 编写XWOS RUST的应用时,需要以下步骤:
16//!
17//! + 1. 在工程目录(例如电路板模块 `bm` 文件夹 或 `$(OEM)` 文件夹)建立一个独立的 [**玄武模块**](../../Docs/TechRefManual/BuildSystem/#玄武模块) ,
18//!   并创建 `makefile` 。
19//!
20//! ```makefile
21//! include $(XWOS_WKSPC_DIR)/XWOS.cfg
22//! include xwbs/functions.mk
23//! include xwbs/xwmo.rust.mk
24//! ```
25//!
26//! + 2. 创建 `Cargo.toml` 。
27//!
28//! ```toml
29//! [package]
30//! name = "rustapp"
31//! version = "0.1.0"
32//! edition = "2021"
33//!
34//! [lib]
35//! name = "rustapp"
36//! crate-type = ["staticlib"]
37//!
38//! [dependencies]
39//! cortex-m = "0.7"
40//! xwrust = {path = "../../../../xwmd/xwrust"}
41//! ```
42//!
43//! + 3. 创建 `.cargo/config.toml` 。
44//!
45//! ```toml
46//! [build]
47//! rustflags = [
48//!  "--cfg", "unix",
49//!  "--cfg", "target_env=\"newlib\"",
50//! ]
51//! target-dir = "target"
52//!
53//! [unstable]
54//! build-std = ["core", "alloc"]
55//! ```
56//!
57//! + 4. 在 `src/lib.rs` 中实现 `pub unsafe extern "C" fn xwrust_main()` 。
58//!
59//! ```rust
60//! #![no_std]
61//!
62//! use xwrust::xwmm::allocator::AllocatorMempool;
63//!
64//! #[global_allocator]
65//! pub static GLOBAL_ALLOCATOR: AllocatorMempool = AllocatorMempool;
66//!
67//! #[no_mangle]
68//! pub unsafe extern "C" fn xwrust_main() {
69//!     // 用户代码
70//! }
71//! ```
72//!
73//! + 5. 在XWOS的C语言入口 `xwos_main()` 中创建一个线程来调用 `xwrust_main()` 。
74//!
75//! ```C
76//! extern void xwrust_main(void);
77//! xwer_t xwrust_task(void * arg);
78//!
79//! xwos_thd_d xwrust_thd;
80//! xwer_t xwos_main(void)
81//! {
82//!         struct xwos_thd_attr attr;
83//!
84//!         xwos_thd_attr_init(&attr);
85//!         attr.name = "xwrust.thd";
86//!         attr.stack = NULL;
87//!         attr.stack_size = 8192;
88//!         attr.priority = XWRUST_THD_PRIORITY;
89//!         attr.detached = true;
90//!         attr.privileged = true;
91//!         xwos_thd_create(&xwrust_thd, &attr, xwrust_task, NULL);
92//! }
93//!
94//! xwer_t xwrust_task(void * arg)
95//! {
96//!         xwrust_main();
97//! }
98//! ```
99//!
100//!
101//! # XWOS RUST 的功能
102//!
103//! XWOS RUST提供了RTOS的基本功能:
104//!
105//! + [内存管理](crate::xwmm)
106//!   + [全局内存分配器](crate::xwmm::allocator)
107//! + [调度器](crate::xwos::skd)
108//! + [电源管理](crate::xwos::pm)
109//! + [线程](crate::xwos::thd)
110//! + [软件定时器](crate::xwos::swt)
111//! + [锁](crate::xwos::lock)
112//!   + [互斥锁](crate::xwos::lock::mtx)
113//!   + [自旋锁](crate::xwos::lock::spinlock)
114//!   + [顺序锁](crate::xwos::lock::seqlock)
115//! + [同步机制](crate::xwos::sync)
116//!   + [信号量](crate::xwos::sync::sem)
117//!   + [条件量](crate::xwos::sync::cond)
118//!   + [事件标志](crate::xwos::sync::flg)
119//!   + [线程栅栏](crate::xwos::sync::br)
120//!   + [信号选择器](crate::xwos::sync::sel)
121//! + 线程间通讯
122//!   + [消息队列](crate::xwmd::xwmq)
123//!   + [循环队列](crate::xwmd::xwcq)
124//! + 其他
125//!   + [时间](crate::xwtm)
126//!   + [类型](crate::types)
127//!   + [错误码](crate::errno)
128//!   + [位图](crate::xwbmp)
129//!
130//! XWOS RUST所有功能都提供了 [`static`] 创建的方法,如果不使用 [`Arc<T>`], [`Box<T>`] 等,
131//! 即可实现完全静态内存分配的代码。
132//!
133//!
134//! # 返回XWOS首页
135//!
136//! [**XWOS首页**](/)
137//!
138//!
139//! [`static`]: <https://doc.rust-lang.org/std/keyword.static.html>
140//! [`Box<T>`]: <https://doc.rust-lang.org/alloc/boxed/struct.Box.html>
141//! [`Arc<T>`]: <https://doc.rust-lang.org/alloc/sync/struct.Arc.html>
142
143#![no_std]
144#![feature(alloc_error_handler)]
145#![feature(negative_impls)]
146#![feature(generic_const_exprs)]
147#![allow(incomplete_features)]
148
149pub mod cfg;
150
151#[macro_use]
152pub mod macros;
153
154pub mod types;
155
156pub mod errno;
157
158pub mod panic;
159
160#[allow(dead_code)]
161pub mod xwbmp;
162
163pub mod xwtm;
164
165pub mod xwmm;
166
167#[allow(dead_code)]
168pub mod xwos;
169
170#[allow(dead_code)]
171pub mod xwmd;
172
173#[macro_use]
174pub mod xwds;