Skip to content

Commit e120b2f

Browse files
committed
zephyr: Add docs to the Zephyr crate
Place documentation into this crate, so that the generated doc is more complete. Signed-off-by: David Brown <[email protected]>
1 parent 4f7b3c8 commit e120b2f

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

zephyr/src/lib.rs

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,85 @@
11
// Copyright (c) 2024 Linaro LTD
22
// SPDX-License-Identifier: Apache-2.0
33

4-
//! Zephyr application support for Rust
4+
//! # Zephyr application support for Rust
55
//!
6-
//! This crates provides the core functionality for applications written in Rust that run on top of
7-
//! Zephyr.
6+
//! Rust is a systems programming language focused on safety, speed, and concurrency. Designed to
7+
//! prevent common programming errors such as null pointer deferencing and buffer overflows, Rust
8+
//! emphasizes memory safety without sacrificing performance. Its powerful type system and
9+
//! ownership model ensure thread-safe programming, making it an ideal choice for developing
10+
//! reliable and efficient low-level code. Rust's expressive syntax and modern features make it a
11+
//! robust alternative for developers working on embedded systems, operating systems, and other
12+
//! performance-critial applications.
13+
//!
14+
//! # Enabling Rust Support
15+
//!
16+
//! Zephyr currently supports applications that are written in Rust and C. To enable Rust support,
17+
//! you must select the `CONFIG_RUST` option in the application configuration file.
18+
//!
19+
//! The Rust toolchain is separate from the rest of the Zephyr SDK. It is recommended to use the
20+
//! [rustup](https://rustup.rs) tool to install the Rust toolchain. In addition to the base
21+
//! compiler, you will need to install core libraries for the target(s) you wish to compile. The
22+
//! easiest way to determine what needs to be installed is to attempt a build. Compilation
23+
//! diagnostics will indicate the `rustup` command needed to install the appropriate target
24+
//! support:
25+
//!
26+
//! ```text
27+
//! $ west build ...
28+
//! ...
29+
//! error[E0463]: can't find crate for `core`
30+
//! |
31+
//! = note: the `thumbv7m-none-eabi` target may not be installed
32+
//! = help: consider downloading the target with `rustup target add thumbv7-none-eabi`
33+
//! ```
34+
//!
35+
//! in this case, the provided `rustup` command will install the necessary target support. The
36+
//! target required depends on both the board selected and certain configuration choices, such as
37+
//! whether floating point is enabled.
38+
//!
39+
//! # Writing a Rust Application
40+
//!
41+
//! See the `samples` directory in the zephyr-lang-rust repo for examples of Rust applications. The
42+
//! cmake build system is used to build the majority of Zephyr. The declarations in the sample
43+
//! `CMakeLists.txt` files will show how to have cmake invoke `cargo build` at the right time, with
44+
//! the right settings to build for your target. The rest of the directory is a typical Rust
45+
//! crate, with a few special caveats:
46+
//!
47+
//! * The crate must currently be named `"rustapp"`. This is so that the cmake rules can find the
48+
//! build.
49+
//! * The crate must be a staticlib crate.
50+
//! * The crate must depend on a "zephyr" crate. This crate does not come from crates.io, but will
51+
//! be located by cmake. This documentation is for this "zephyr" crate.
52+
//!
53+
//! cmake and/or the `west build` command will place the build in a directory (`build` by default)
54+
//! and the rules will direct cargo to also place its build output there (`build/rust/target` for
55+
//! the majority of the output).
56+
//!
57+
//! The build process will also generate a template for a `.cargo/config.toml` that will configure
58+
//! cargo so that tools such as `cargo check` and `rust analyzer` will work with your project. You
59+
//! can make a symlink for this file to allow these tools to work
60+
//!
61+
//! ```bash
62+
//! $ mkdir .cargo
63+
//! $ cd .cargo
64+
//! $ ln -s ../build/rust/sample-cargo-config.toml config.coml
65+
//! $ cd ..
66+
//! $ cargo check
67+
//! ```
68+
//!
69+
//! # Zephyr Functionality
70+
//!
71+
//! The bindings to Rust for Zephyr are still under development and are currently minimal. However,
72+
//! some Zephyr functionality is available.
73+
//!
74+
//! ## Bool Kconfig Settings
75+
//!
76+
//! Boolean Kconfig settings can be accessed from within Rust code. However, since Rust requires
77+
//! certain compilation decisions, accessing these with `#[cfg...]` directives requires a small
78+
//! addition to `build.rs`. See the docs in the [`zephyr-build` crate](../zephyr-build/index.html).
79+
//!
80+
//! # Other Kconfig Settings
81+
//!
82+
//! All boolean, numeric, and string Kconfig settings are available through the `kconfig` module.
883
984
#![no_std]
1085
#![allow(unexpected_cfgs)]

zephyr/src/printk.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33

44
//! Printk implementation for Rust.
55
//!
6+
//! The Zephyr configuration `CONFIG_PRINTK` must be enabled for this functionality to be present.
7+
//!
8+
//! This module provides two macros: [`printk`] and [`printkln`]. These are analagous to
9+
//! [`std::print`](https://doc.rust-lang.org/std/macro.print.html) and
10+
//! [`std::println`](https://doc.rust-lang.org/std/macro.println.html) macros.
11+
//!
12+
//! These will print messages through Zephyr's `printk` functionality.
13+
//!
614
//! This uses the `k_str_out` syscall, which is part of printk to output to the console.
715
816
use core::fmt::{
@@ -12,6 +20,14 @@ use core::fmt::{
1220
write,
1321
};
1422

23+
/// Print to Zephyr's console.
24+
///
25+
/// Equivalent to the [`printkln!`] macro except that a newline is not printed at the end of the
26+
/// message.
27+
///
28+
/// Note that printk access in Zephyr are uncoordinated unless `CONFIG_PRINTK_SYNC` is enabled.
29+
/// Even in this case, because of mismatches between Rust and C's message formatting, messages from
30+
/// Rust may not be as well synchronized, even when this is defined.
1531
#[macro_export]
1632
macro_rules! printk {
1733
($($arg:tt)*) => {{

0 commit comments

Comments
 (0)