Rust で組み込みプログラムを CH32V203 にもやってみます。
@verylowfreq (三峰 スズ) さんの記事「CH32V203をRustで書く小さなサンプルプロジェクト」
https://qiita.com/verylowfreq/items/787b84bc9997520037f9
では、Tier 2 サポートしているだけの環境で、CH32V203 の初期化やレジスタ操作などを自前でやって動かしてました。
本記事ではもう少し楽なやり方として、こちらで公開されている HAL を試してみることにします。
https://github.com/ch32-rs/ch32-hal

HAL を使うことで、初期化やペリフェラルの駆動、メモリの配置などをイチイチ設定しなくても動作できるようになります。
HAL の README には以下のようになっていて、
V2 が CH32V20x のことかな? 充実した対応になっているっぽいです。
環境
- WeAct Studio BluePill + ( CH32V203C8T6 )
Bit Trade One で販売しているものです
- Ubuntu 24.04 LTS
https://qiita.com/nanbuwks/items/6de8abef92028974460d
「Rust を Ubuntu で試してみる」
でインストールしたものです。
Rust 環境として LinuxPC 用の環境が入っていますが、その他は以下のようにリストアップはされていますがインストールはされていない状態です。
$ rustup target list
aarch64-apple-darwin
aarch64-apple-ios
aarch64-apple-ios-macabi
aarch64-apple-ios-sim
aarch64-apple-tvos
aarch64-apple-tvos-sim
.
.
.
riscv32i-unknown-none-elf
riscv32im-unknown-none-elf
riscv32imac-unknown-none-elf
riscv32imafc-unknown-none-elf
riscv32imc-unknown-none-elf
.
.
.
x86_64-pc-windows-gnu
x86_64-pc-windows-gnullvm
x86_64-pc-windows-msvc
x86_64-unknown-freebsd
x86_64-unknown-fuchsia
x86_64-unknown-illumos
x86_64-unknown-linux-gnu (installed)
x86_64-unknown-linux-gnuasan
x86_64-unknown-linux-gnumsan
x86_64-unknown-linux-gnutsan
x86_64-unknown-linux-gnux32
x86_64-unknown-linux-musl
x86_64-unknown-linux-ohos
x86_64-unknown-netbsd
x86_64-unknown-none
x86_64-unknown-redox
x86_64-unknown-uefi
RISC-V への対応
「Platform Support - The rustc book」
https://doc.rust-lang.org/nightly/rustc/platform-support.html
で詳細を調べると、以下のようになっています。
ここで「*」とあるのは
- indicates the target only supports no_std development.
ということで、スタンダードライブラリが使えません。
スタンダードライブラリには、String 型、slice 処理、ファイルシステム、ネットワークなどの処理が含まれています。
さて、CH32V203 は riscv32imac-unknown-none-elf なのだそうです。ということで、以下のようにしてアーキテクチャをインストールします。
$ rustup target add riscv32imac-unknown-none-elf
info: downloading component rust-std
rust-std installed 11.40 MiB
この時点で riscv32imac-unknown-none-elf をインストールしましたが、結果的にこれとは別のアーキテクチャをインストールして使いました。
ch32-hal を導入する
まずはレポジトリをコピー。
$ git clone https://github.com/ch32-rs/ch32-hal.git
$ cd ch32-hal
$ ls
Cargo.toml LICENSE-MIT build.rs rustfmt.toml
LICENSE-APACHE README.md examples src
$ cd examples
$ ls
ch32l103 ch32v006 ch32v203 ch32v305 ch32x035 ch643
ch32v003 ch32v103 ch32v208 ch32v307 ch641
ch32v203 の対応はどのようになっているかな?
$ cd ch32v203
$ tree
.
├── Cargo.toml
├── README.md
├── build.rs
├── memory.x
└── src
└── bin
├── adc.rs
├── blinky.rs
├── can.rs
├── flash_sections.rs
├── sdi_print.rs
├── spi-lcd-st7735.rs
├── uart.rs
└── usbd.rs
3 directories, 12 files
$ cat Cargo.toml
[package]
name = "ch32v203-examples"
version = "0.1.0"
edition = "2021"
[[bin]]
name = "usbd"
path = "src/bin/usbd.rs"
required-features = ["embassy-executor/arch-spin"]
[dependencies]
ch32-hal = { path = "../../", features = [
"ch32v203g6u6",
"memory-x",
"embassy",
"rt",
"time-driver-tim2",
], default-features = false }
embassy-executor = { version = "0.10.0", features = [
"platform-spin",
"executor-thread",
] }
embassy-time = "0.5.1"
embassy-usb = { version = "0.5.1" }
embassy-usb-driver = "=0.2.0"
embassy-futures = { version = "0.1.2" }
# This is okay because we should automatically use whatever ch32-hal uses
qingke-rt = "0.7"
qingke = "0.7"
panic-halt = "1.0"
display-interface-spi = "0.5.0"
embedded-hal = "1.0.0"
embedded-graphics = "0.8.1"
embedded-can = "0.4"
ssd1306 = "0.8.4"
heapless = "0.8.0"
micromath = { version = "2.1.0", features = ["num-traits"] }
# mipidsi = "0.7.1"
# embedded-hal-bus = "0.1.0"
[profile.release]
strip = w # symbols are not flashed to the microcontroller, so don't strip them.
lto = true
opt-level = "z" # Optimize for size.
試しにこのままビルドしてみます。
$ cargo build
Updating crates.io index
Updating git repository `https://github.com/ch32-rs/ch32-metapac`
Locking 137 packages to latest compatible versions
Adding az v1.2.1 (available: v1.3.0)
Adding embassy-usb v0.5.1 (available: v0.6.0)
Adding embassy-usb-driver v0.2.0 (available: v0.2.2)
Adding heapless v0.8.0 (available: v0.9.3)
Adding ssd1306 v0.8.4 (available: v0.10.0)
Downloaded cfg-if v1.0.4
Downloaded cordyceps v0.3.4
Downloaded embassy-time-driver v0.2.2
Downloaded ahash v0.8.12
Downloaded version_check v0.9.5
Downloaded futures-core v0.3.32
.
.
Downloaded 106 crates (6.0MiB) in 0.82s (largest was `embedded-graphics` at 2.4MiB)
warning: invalid feature `embassy-executor/arch-spin` in required-features of target `usbd`: feature `arch-spin` does not exist in package `embassy-executor v0.10.0`
Compiling proc-macro2 v1.0.106
Compiling unicode-ident v1.0.24
Compiling quote v1.0.45
Compiling version_check v0.9.5
Compiling byteorder v1.5.0
Compiling critical-section v1.2.0
Compiling paste v1.0.15
Compiling stable_deref_trait v1.2.1
error[E0463]: can't find crate for `core`
|
= note: the `riscv32imc-unknown-none-elf` target may not be installed
= help: consider downloading the target with `rustup target add riscv32imc-unknown-none-elf`
For more information about this error, try `rustc --explain E0463`.
error: could not compile `stable_deref_trait` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `byteorder` (lib) due to 1 previous error
error: could not compile `critical-section` (lib) due to 1 previous error
おや、先程はriscv32imac-unknown-none-elf をインストールしましたが、riscv32imc-unknown-none-elf が必要だったらしいですね。 atomic 命令をサポートしているか否か、の違いだそうです。
今回は改めて riscv32imc-unknown-none-elf をインストールして試してみます。
$ rustup target add riscv32imc-unknown-none-elf
info: downloading component rust-std
rust-std installed 11.21 MiB
( うーん??? rust-std って スタンダードライブラリをインストールしているのかな??? )
改めてビルド。
$ cargo build
warning: invalid feature `embassy-executor/arch-spin` in required-features of target `usbd`: feature `arch-spin` does not exist in package `embassy-executor v0.10.0`
Compiling proc-macro2 v1.0.106
Compiling byteorder v1.5.0
Compiling critical-section v1.2.0
Compiling stable_deref_trait v1.2.1
Compiling quote v1.0.45
Compiling paste v1.0.15
.
.
.
Compiling ch32v203-examples v0.1.0 (/home/owner/Downloads/rusttest/ch32-hal/examples/ch32v203)
Compiling cordyceps v0.3.4
Compiling byte-slice-cast v1.2.3
Compiling sdio-host v0.5.0
.
.
.
Compiling ch32-hal v0.1.0 (/home/owner/Downloads/rusttest/ch32-hal)
warning: type `EndpointBufferAllocator<'d, NR_EP, 8>` is more private than the item `EndpointBufferAllocator8`
--> /home/owner/Downloads/rusttest/ch32-hal/src/usb/mod.rs:109:1
|
109 | pub type EndpointBufferAllocator8<'d, const NR_EP: usize> = EndpointBu...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type alias `EndpointBufferAllocator8` is reachable at visibility `pub`
|
note: but type `EndpointBufferAllocator<'d, NR_EP, 8>` is only usable at visibility `pub(crate)`
--> /home/owner/Downloads/rusttest/ch32-hal/src/usb/mod.rs:3:1
|
3 | pub(crate) struct EndpointBufferAllocator<'d, const NR_EP: usize, const SIZE: usize> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: `#[warn(private_interfaces)]` on by default
warning: type `EndpointBufferAllocator<'d, NR_EP, 16>` is more private than the item `EndpointBufferAllocator16`
--> /home/owner/Downloads/rusttest/ch32-hal/src/usb/mod.rs:110:1
|
110 | pub type EndpointBufferAllocator16<'d, const NR_EP: usize> = EndpointB...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type alias `EndpointBufferAllocator16` is reachable at visibility `pub`
|
note: but type `EndpointBufferAllocator<'d, NR_EP, 16>` is only usable at visibility `pub(crate)`
--> /home/owner/Downloads/rusttest/ch32-hal/src/usb/mod.rs:3:1
|
3 | pub(crate) struct EndpointBufferAllocator<'d, const NR_EP: usize, const SIZE: usize> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
661be865fa5a3137/23d6672/src/chips/ch32v203g6u6/memory_x/" "-o" "/home/owner/Downloads/rusttest/ch32-hal/examples/ch32v203/target/riscv32imc-unknown-none-elf/debug/deps/sdi_print-450942f714cf8ac8" "--gc-sections" "-Tlink.x"
= note: some arguments are omitted. use `--verbose` to show all linker arguments
= note: rust-lld: error: section '.rodata' will not fit in region 'FLASH': overflowed by 6692 bytes
rust-lld: error: section '.rodata' will not fit in region 'FLASH': overflowed by 6692 bytes
rust-lld: error: section '.data' will not fit in region 'FLASH': overflowed by 6720 bytes
.
.
.
= note: some arguments are omitted. use `--verbose` to show all linker arguments
= note: rust-lld: error: section '.text' will not fit in region 'FLASH': overflowed by 57224 bytes
rust-lld: error: section '.text' will not fit in region 'FLASH': overflowed by 57224 bytes
rust-lld: error: section '.rodata' will not fit in region 'FLASH': overflowed by 83792 bytes
rust-lld: error: section '.rodata' will not fit in region 'FLASH': overflowed by 83792 bytes
rust-lld: error: section '.data' will not fit in region 'FLASH': overflowed by 83840 bytes
error: could not compile `ch32v203-examples` (bin "spi-lcd-st7735") due to 1 previous error
いろいろ直さないといけません。レポジトリディレクトリでの作業はここまでとして、これ以降別のプロジェクトフォルダで進めていきます。
blinky プロジェクトを作成
blinky ソースを抜き出し、その他必要なファイルを抜き出して、新しいプロジェクトフォルダに格納します。
$ mkdir ../../../ch32v203rsblink
$ cp Cargo.toml build.rs .cargo/config.toml memory.x src/bin/blinky.rs ../../../ch32v203rsblink/
$ cd ../../../ch32v203rsblink/
$ ls
Cargo.toml blinky.rs build.rs config.toml memory.x
$ mkdir .cargo
$ mkdir src
$ mv config.toml .cargo/
$ mv blinky.rs src/main.rs
おっと、もうひとつ必要でした。
$ cp ../ch32-hal/rustfmt.toml .
blinky.rs から作った src/main.rs です。ポート番号を修正しています。
#![no_std]
#![no_main]
use hal::gpio::{Level, Output};
use qingke::riscv;
use {ch32_hal as hal, panic_halt as _};
#[qingke_rt::entry]
fn main() -> ! {
let p = hal::init(Default::default());
let mut led = Output::new(p.PB2, Level::Low, Default::default());
loop {
led.toggle();
riscv::asm::delay(1000000);
}
}
Cargo.toml
を以下のように直します。
[package]
name = "ch32v203rs-blinktest"
version = "0.1.0"
edition = "2021"
[dependencies]
ch32-hal = { git = "https://github.com/ch32-rs/ch32-hal.git", features = [
"ch32v203c8t6",
"memory-x",
"embassy",
"rt",
"time-driver-tim2",
], default-features = false }
embassy-executor = { version = "0.10.0", features = [
"platform-spin",
"executor-thread",
] }
embassy-time = "0.5.1"
qingke-rt = "0.7"
qingke = "0.7"
panic-halt = "1.0"
embedded-hal = "1.0.0"
[profile.release]
strip = false # symbols are not flashed to the microcontroller, so don't strip them.
lto = true
opt-level = "z" # Optimize for size.
ビルドしてみます。
$ cargo build
Updating git repository `https://github.com/ch32-rs/ch32-hal.git`
Updating crates.io index
Updating git repository `https://github.com/ch32-rs/ch32-metapac`
Locking 105 packages to latest compatible versions
Downloaded embassy-usb-driver v0.2.2
Downloaded 1 crate (11.4KiB) in 0.28s
Compiling proc-macro2 v1.0.106
Compiling unicode-ident v1.0.24
Compiling quote v1.0.45
Compiling paste v1.0.15
Compiling critical-section v1.2.0
Compiling version_check v0.9.5
Compiling riscv v0.12.1
.
.
.
Compiling ch32-hal v0.1.0 (https://github.com/ch32-rs/ch32-hal.git#71a29858)
error: linking with `rust-lld` failed: exit status: 1
|
= note: "rust-lld" "-flavor" "gnu" "/home/owner/Downloads/rusttest/ch32v203rsblink/target/riscv32imc-unknown-none-elf/debug/deps/rustci1Rzws/symbols.o" "<8 object files omitted>" "--as-needed" "-Bstatic" "/home/owner/Downloads/rusttest/ch32v203rsblink/target/riscv32imc-unknown-none-elf/debug/deps/{libpanic_halt-e1545946558c55a9,libch32_hal-5440b03017a03d2b,libembassy_time-db9666b0511dc8e9,libembedded_hal-da6b72f3938bd3ce,libvoid-3f8949af3f85d0fb,libnb-edb4b9e4758868c4,libembedded_hal_async-3d828612cfa88ef6,libembassy_time_queue_utils-ab216abdb0b00382,libembassy_executor_timer_queue-bb60e13516b06f7a,libembassy_time_driver-c8e6419f9b89097f,libembedded_storage-8488bf678fa158c6,libembedded_can-e7da4584cbeb2e3c,libnb-8e3a7ba1c8d061ca,libembassy_usb_driver-2e1de28ecf03df70,libembedded_io_async-578d59ecad731a2b,libembedded_io-ee5b5d8cb94e759b,libembassy_futures-5f3f0f6cdf97c6cc,libembassy_sync-b2794305091c82cc,libembedded_io_async-9d06dd3e0433d7de,libembedded_io-067f2571242fc0c9,libheapless-f6f303e550f31b0e,libhash32-4dd32fd29ec05af3,libbyteorder-a97cad72fd1918c8,libfutures-de99d56aa40e3314,libfutures_util-0ac71138c67436a2,libpin_project_lite-ff4470f831795412,libfutures_sink-c5ba711f6f3ab092,libfutures_task-38c5f1539187e583,libfutures_core-00cd16a3523367ba,libqingke_rt-51b1ffcabc5316db,libembassy_hal_internal-74da989a38f7d479,libnum_traits-89c49f8c6e84c8bc,libch32_metapac-58bcc0db01c7ef80,libqingke-c5f90e4c7244cd28,libcfg_if-7e684c7e43716361,libriscv-a44ce04664e13de4,libcritical_section-7b31f0059655b18f,libriscv_pac-ea21c91de2a29cc5,libembedded_hal-90d13ed40f37aa30,libbit_field-2fbe198d856807b9}.rlib" "<sysroot>/lib/rustlib/riscv32imc-unknown-none-elf/lib/{libcore-*,libcompiler_builtins-*}.rlib" "-L" "/home/owner/Downloads/rusttest/ch32v203rsblink/target/riscv32imc-unknown-none-elf/debug/deps/rustci1Rzws/raw-dylibs" "-Bdynamic" "-z" "noexecstack" "-L" "/home/owner/Downloads/rusttest/ch32v203rsblink/target/riscv32imc-unknown-none-elf/debug/build/ch32v203rs-blinktest-dc6c67d7753b6424/out" "-L" "/home/owner/Downloads/rusttest/ch32v203rsblink/target/riscv32imc-unknown-none-elf/debug/build/qingke-rt-01edffc739210633/out" "-L" "/home/owner/.cargo/git/checkouts/ch32-metapac-661be865fa5a3137/23d6672/src/chips/ch32v203c8t6" "-L" "/home/owner/.cargo/git/checkouts/ch32-metapac-661be865fa5a3137/23d6672/src/chips/ch32v203c8t6/memory_x/" "-o" "/home/owner/Downloads/rusttest/ch32v203rsblink/target/riscv32imc-unknown-none-elf/debug/deps/ch32v203rs_blinktest-6c73ba84014572c7" "--gc-sections" "-Tlink.x"
= note: some arguments are omitted. use `--verbose` to show all linker arguments
= note: rust-lld: error: section '.text' will not fit in region 'FLASH': overflowed by 3644 bytes
rust-lld: error: section '.text' will not fit in region 'FLASH': overflowed by 3644 bytes
rust-lld: error: section '.rodata' will not fit in region 'FLASH': overflowed by 14504 bytes
rust-lld: error: section '.rodata' will not fit in region 'FLASH': overflowed by 14504 bytes
rust-lld: error: section '.data' will not fit in region 'FLASH': overflowed by 14552 bytes
error: could not compile `ch32v203rs-blinktest` (bin "ch32v203rs-blinktest") due to 1 previous error
このエラーはFLASHに収まりきらなくなったためです。 --release オプションをつけてサイズを削減します。
$ cargo build --release
Compiling proc-macro2 v1.0.106
Compiling quote v1.0.45
Compiling unicode-ident v1.0.24
Compiling paste v1.0.15
.
.
.
Finished `release` profile [optimized] target(s) in 15.57s
今度は無事ビルドできました。
ファームウェアを .elf に変換して書き込み
できたファームウェアは target/riscv32imc-unknown-none-elf/release/ にあります。
結果的に必要ありませんでしたが、 .elf ファイルを作成するために必要なプログラムをインストールします。
$ rustup component add llvm-tools-preview
$ cargo install cargo-binutils
.elf ファイルに変換
$ cargo objcopy --release -- -O elf32-littleriscv firmware.elf
書き込みとエラー
$ wchisp flash firmware.elf
07:32:06 [INFO] Opening USB device #0
07:32:06 [INFO] Chip: CH32V203C8T6[0x3119] (Code Flash: 64KiB)
07:32:06 [INFO] Chip UID: CD-AB-25-61-0E-BC-00-C9
07:32:06 [INFO] BTVER(bootloader ver): 02.70
07:32:06 [INFO] Code Flash protected: true
07:32:06 [INFO] Current config registers: ff003fc000ff00ffffffffff00020700cdab25610ebc00c9
RDPR_USER: 0xC03F00FF
[7:0] RDPR 0xFF (0b11111111)
`- Protected
[16:16] IWDG_SW 0x1 (0b1)
`- IWDG enabled by the software, and disabled by hardware
[17:17] STOP_RST 0x1 (0b1)
`- Disable
[18:18] STANDBY_RST 0x1 (0b1)
`- Disable, entering standby-mode without RST
[23:22] SRAM_CODE_MODE 0x0 (0b0)
`- CODE-192KB + RAM-128KB / CODE-128KB + RAM-64KB depending on the chip
DATA: 0xFF00FF00
[7:0] DATA0 0x0 (0b0)
[23:16] DATA1 0x0 (0b0)
WRP: 0xFFFFFFFF
`- Unprotected
07:32:06 [INFO] Read firmware.elf as ELF format
07:32:06 [INFO] Found loadable segment, physical address: 0x00000000, virtual address: 0x00000000, flags: 0x5
07:32:06 [INFO] Section names: [".vector_table", ".text"]
07:32:06 [INFO] Found loadable segment, physical address: 0x000008ec, virtual address: 0x000008ec, flags: 0x4
07:32:06 [INFO] Section names: [".rodata"]
07:32:06 [INFO] Found loadable segment, physical address: 0x00000930, virtual address: 0x20000000, flags: 0x6
07:32:06 [INFO] Section names: [".data"]
07:32:06 [INFO] Firmware size: 3072
07:32:06 [INFO] Erasing...
07:32:06 [WARN] erase_code: set min number of erased sectors to 8
07:32:06 [INFO] Erased 8 code flash sectors
07:32:07 [INFO] Erase done
07:32:07 [INFO] Writing to code flash...
█████████████████████████████████████████████████████████████████████████████████████████████ 3072/307207:32:07 [INFO] Code flash 3072 bytes written
07:32:07 [INFO] Verifying...
Error: Verify failed, mismatch
Verify failed が出たので、こちらを見て対処。
「arduino_core_ch32_sz を使って BluePill+ を L チカする」
https://qiita.com/nanbuwks/items/0963b7a87469192240de
$ wchisp config reset
07:34:26 [INFO] Opening USB device #0
07:34:26 [INFO] Current config registers: ff003fc000ff00ffffffffff
07:34:26 [INFO] Reset config registers: a55aff0000ff00ffffffffff
07:34:26 [INFO] Config register restored to default value(non-protected, debug-enabled)
改めて書き込んだらOK.
$ wchisp flash firmware.elf
07:34:50 [INFO] Opening USB device #0
07:34:50 [INFO] Chip: CH32V203C8T6[0x3119] (Code Flash: 64KiB)
07:34:50 [INFO] Chip UID: CD-AB-25-61-0E-BC-00-C9
07:34:50 [INFO] BTVER(bootloader ver): 02.70
07:34:50 [INFO] Code Flash protected: false
07:34:50 [INFO] Current config registers: a55aff0000ff00ffffffffff00020700cdab25610ebc00c9
RDPR_USER: 0x00FF5AA5
[7:0] RDPR 0xA5 (0b10100101)
`- Unprotected
[16:16] IWDG_SW 0x1 (0b1)
`- IWDG enabled by the software, and disabled by hardware
[17:17] STOP_RST 0x1 (0b1)
`- Disable
[18:18] STANDBY_RST 0x1 (0b1)
`- Disable, entering standby-mode without RST
[23:22] SRAM_CODE_MODE 0x3 (0b11)
`- CODE-228KB + RAM-32KB / CODE-160KB + RAM-32KB depending on the chip
DATA: 0xFF00FF00
[7:0] DATA0 0x0 (0b0)
[23:16] DATA1 0x0 (0b0)
WRP: 0xFFFFFFFF
`- Unprotected
07:34:50 [INFO] Read firmware.elf as ELF format
07:34:50 [INFO] Found loadable segment, physical address: 0x00000000, virtual address: 0x00000000, flags: 0x5
07:34:50 [INFO] Section names: [".vector_table", ".text"]
07:34:50 [INFO] Found loadable segment, physical address: 0x000008e8, virtual address: 0x000008e8, flags: 0x4
07:34:50 [INFO] Section names: [".rodata"]
07:34:50 [INFO] Found loadable segment, physical address: 0x00000928, virtual address: 0x20000000, flags: 0x6
07:34:50 [INFO] Section names: [".data"]
07:34:50 [INFO] Firmware size: 3072
07:34:50 [INFO] Erasing...
07:34:50 [WARN] erase_code: set min number of erased sectors to 8
07:34:50 [INFO] Erased 8 code flash sectors
07:34:51 [INFO] Erase done
07:34:51 [INFO] Writing to code flash...
█████████████████████████████████████████████████████████████████████████████████████████████ 3072/307207:34:51 [INFO] Code flash 3072 bytes written
07:34:52 [INFO] Verifying...
█████████████████████████████████████████████████████████████████████████████████████████████ 3072/307207:34:52 [INFO] Verify OK
07:34:52 [INFO] Now reset device and skip any communication errors
07:34:52 [INFO] Device reset
ファームウェアの書き込み方法の改善と Config.toml の変更
以下のようにしたら .elf にしていなくても書き込めました。
$ wchisp flash target/riscv32imc-unknown-none-elf/release/ch32v203rs-blinktest
これを .cargo/config.toml に反映します。
以下のように、runnner = に記述します。
[build]
target = "riscv32imc-unknown-none-elf"
[target."riscv32imc-unknown-none-elf"]
rustflags = [
# "-C", "link-arg=-Tlink.x",
]
# runner = "riscv64-unknown-elf-gdb -q -x openocd.gdb"
# runner = "riscv-none-embed-gdb -q -x openocd.gdb"
# runner = "gdb -q -x openocd.gdb"
# runner = "wlink -v flash"
runner = "wchisp flash"
# runner = "wlink -v flash --enable-sdi-print --watch-serial --erase"
# runner = "wlink -v flash"
以下のようにすると、ビルドと書き込みが同時に行われます。
$ cargo run --release

