<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Rust Development Notes on XWOS</title>
    <link>/en/Docs/Note/Rust/</link>
    <description>Recent content in Rust Development Notes on XWOS</description>
    <generator>Hugo</generator>
    <language>en</language>
    <atom:link href="/en/Docs/Note/Rust/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Integration and Build</title>
      <link>/en/Docs/Note/Rust/Build/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>/en/Docs/Note/Rust/Build/</guid>
      <description>&lt;h2 id=&#34;concept&#34;&gt;Concept&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Compile as &lt;strong&gt;staticlib&lt;/strong&gt; via &lt;code&gt;cargo&lt;/code&gt;, then link with XWOS;&lt;/li&gt;&#xA;&lt;li&gt;Since XWOS supports ARM/PowerPC/RISC-V, cross-compilation for multiple platforms is required via the &lt;code&gt;--target=&lt;/code&gt; option.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;approach&#34;&gt;Approach&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;XWOS starts an independent thread that calls Rust&amp;rsquo;s main function, thus entering the world of Rust;&lt;/li&gt;&#xA;&lt;li&gt;Rust&amp;rsquo;s main function and middleware are an independently compiled XWOS module;&lt;/li&gt;&#xA;&lt;li&gt;Since Rust compilation depends on the &lt;code&gt;cargo&lt;/code&gt; tool, a new XWOS module build rule can be designed to invoke &lt;code&gt;cargo build&lt;/code&gt;;&lt;/li&gt;&#xA;&lt;li&gt;The Rust XWOS module is compiled as &lt;strong&gt;staticlib&lt;/strong&gt; and ultimately statically linked with XWOS;&lt;/li&gt;&#xA;&lt;li&gt;Since some &lt;strong&gt;unstable&lt;/strong&gt; features are used, the nightly version of Rust must be installed.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;build-approach&#34;&gt;Build Approach&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;The &lt;code&gt;cargo build&lt;/code&gt; target should be set to &lt;code&gt;PHONY&lt;/code&gt;, so that &lt;code&gt;cargo build&lt;/code&gt; is invoked every time &lt;code&gt;make&lt;/code&gt; is run,&#xA;and &lt;code&gt;cargo&lt;/code&gt; manages the compilation of &lt;code&gt;rust&lt;/code&gt; source code;&lt;/li&gt;&#xA;&lt;li&gt;The environment variable &lt;code&gt;$(RUST_TARGET)&lt;/code&gt; is defined in &lt;code&gt;cpu.mk&lt;/code&gt;;&lt;/li&gt;&#xA;&lt;li&gt;Other build options are defined in &lt;code&gt;.cargo/config&lt;/code&gt; or &lt;code&gt;build.rs&lt;/code&gt;;&lt;/li&gt;&#xA;&lt;li&gt;After &lt;code&gt;cargo build&lt;/code&gt; completes, copy the output .a file to the output directory;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;installing-the-rust-toolchain&#34;&gt;Installing the Rust Toolchain&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Install Rust according to the &lt;a href=&#34;https://www.rust-lang.org/tools/install&#34;&gt;official guide&lt;/a&gt;;&lt;/li&gt;&#xA;&lt;li&gt;Switch mirror sources:&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Ubuntu configuration file path: &lt;code&gt;~/.cargo/config&lt;/code&gt;&lt;/li&gt;&#xA;&lt;li&gt;Windows configuration file path: &lt;code&gt;C:\Users\username\.cargo\config&lt;/code&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-toml&#34; data-lang=&#34;toml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[source.crates-io]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;registry = &lt;span style=&#34;color:#b44&#34;&gt;&amp;#34;https://github.com/rust-lang/crates.io-index&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;replace-with = &lt;span style=&#34;color:#b44&#34;&gt;&amp;#39;sjtu&amp;#39;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[source.ustc]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;registry = &lt;span style=&#34;color:#b44&#34;&gt;&amp;#34;http://mirrors.ustc.edu.cn/crates.io-index&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[source.sjtu]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;registry = &lt;span style=&#34;color:#b44&#34;&gt;&amp;#34;http://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;&#xA;&lt;li&gt;Ubuntu: Install unstable version&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;rustup install nightly&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;rustup +nightly target add thumbv8m.main-none-eabihf&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;rustup +nightly target add thumbv8m.main-none-eabi&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;rustup +nightly target add thumbv8m.base-none-eabi&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;rustup +nightly target add thumbv7m-none-eabi&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;rustup +nightly target add thumbv7em-none-eabihf&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;rustup +nightly target add thumbv7em-none-eabi&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;rustup +nightly target add thumbv6m-none-eabi&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;rustup +nightly target add riscv32imac-unknown-none-elf&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;rustup +nightly component add rust-src&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;&#xA;&lt;li&gt;Windows: Install unstable version&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Note: Windows only supports using the &lt;code&gt;gnu&lt;/code&gt; version of the toolchain&lt;/p&gt;</description>
    </item>
    <item>
      <title>Rust Initial Validation</title>
      <link>/en/Docs/Note/Rust/Bringup/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>/en/Docs/Note/Rust/Bringup/</guid>
      <description>&lt;h2 id=&#34;rust-std-library&#34;&gt;Rust std Library&lt;/h2&gt;&#xA;&lt;p&gt;The Rust standard library includes:&lt;/p&gt;&#xA;&lt;p&gt;&lt;img alt=&#34;img&#34; src=&#34;/en/Docs/Note/Rust/Bringup/std.svg&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;In embedded systems, &lt;code&gt;core&lt;/code&gt; and &lt;code&gt;alloc&lt;/code&gt; are the most useful.&lt;/p&gt;&#xA;&lt;p&gt;Although in embedded environments you can forcibly use Rust&amp;rsquo;s std library through &lt;code&gt;#![feature(restricted_std)]&lt;/code&gt;,&#xA;as functionality increases, problems also increase, so we switched to using &lt;code&gt;#![no_std]&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;h2 id=&#34;build&#34;&gt;Build&lt;/h2&gt;&#xA;&lt;p&gt;Add the following build configuration in &lt;code&gt;.cargo/config.toml&lt;/code&gt;:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-toml&#34; data-lang=&#34;toml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[unstable]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;build-std = [&lt;span style=&#34;color:#b44&#34;&gt;&amp;#34;core&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#b44&#34;&gt;&amp;#34;alloc&amp;#34;&lt;/span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;global_allocator&#34;&gt;global_allocator&lt;/h2&gt;&#xA;&lt;p&gt;An attribute that can be used to implement your own memory allocation functions. Refer to the documentation &lt;a href=&#34;https://doc.rust-lang.org/std/alloc/index.html&#34;&gt;std::alloc&lt;/a&gt;.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Rust Development Environment Installation Notes</title>
      <link>/en/Docs/Note/Rust/Env/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>/en/Docs/Note/Rust/Env/</guid>
      <description>&lt;h2 id=&#34;installing-lsp&#34;&gt;Installing LSP&lt;/h2&gt;&#xA;&lt;p&gt;LSP (Language Server Protocol) is a new generation tool for browsing source code.&#xA;For Rust, rust-analyzer is the recommended LSP.&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;git clone https://github.com/rust-analyzer/rust-analyzer.git&#xA;cd rust-analyzer&#xA;cargo xtask install&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;vscode&#34;&gt;VScode&lt;/h2&gt;&#xA;&lt;p&gt;After installing VSCode, you also need to install &lt;code&gt;rust-analyzer&lt;/code&gt;. Once installed, VSCode works out of the box.&lt;/p&gt;&#xA;&lt;h2 id=&#34;stm32cubeide&#34;&gt;STM32CubeIDE&lt;/h2&gt;&#xA;&lt;p&gt;STM32CubeIDE is Eclipse-based, so you only need to install the Rust plugin.&lt;/p&gt;&#xA;&lt;h3 id=&#34;rust-plugin&#34;&gt;Rust Plugin&lt;/h3&gt;&#xA;&lt;p&gt;A relatively new version of the Rust plugin must be installed, otherwise it cannot support rust-analyzer.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Supporting Multiple Crates</title>
      <link>/en/Docs/Note/Rust/MultiCrates/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>/en/Docs/Note/Rust/MultiCrates/</guid>
      <description>&lt;h2 id=&#34;limitations&#34;&gt;Limitations&lt;/h2&gt;&#xA;&lt;p&gt;During the development of XWOS Rust, it became necessary to separate library and application code into different crates. In practice,&#xA;the following Rust limitations were discovered:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Two crates cannot both be compiled as staticlib. A staticlib crate cannot be listed in &lt;code&gt;[dependencies]&lt;/code&gt;,&#xA;indicating that staticlib is meant to be called by C/C++;&lt;/li&gt;&#xA;&lt;li&gt;Only rlib crates can be listed in &lt;code&gt;[dependencies]&lt;/code&gt;. When compiling staticlib,&#xA;all crates in &lt;code&gt;[dependencies]&lt;/code&gt; will be packaged within the staticlib;&lt;/li&gt;&#xA;&lt;li&gt;When linking with C/C++ code, there cannot be two or more staticlibs, unless these staticlibs&amp;rsquo; dependencies are completely&#xA;unrelated (which is difficult to achieve in practice), otherwise duplicate definition linker errors will occur.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;solution&#34;&gt;Solution&lt;/h2&gt;&#xA;&lt;p&gt;Through practice, the following solution was developed:&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
