背景
気になっていたrustでのstm32h753zi開発環境の作り方を把握しました。備忘録として記事に残します。
使ったもの
- nucleo-stm32h753zi
- ubuntu24.04をインストールしたPC
- rust
- VSCode + rust-analyzer
rustの関連プログラムをインストール
ビルド環境 thumbv7em-none-eabihf
stm32h753ziは組み込み向けの環境thumbv7em-none-eabihfとしてビルドするので、それ用のビルドに必要な環境をインストールします。rustup target add thumbv7em-none-eabihf
その環境をインストールしていないと下記のエラーが出ます。
error[E0463]: can't find crate for `core`
|
= note: the `thumbv7em-none-eabihf` target may not be installed
= help: consider downloading the target with `rustup target add thumbv7em-none-eabihf`
書き込みプログラム probe-rs
書き込みにprobe-rsを利用するのでインストールします。下記のページのLinux向けのscriptの実行でインストールしました。
Installation - probe-rs
インストールに加えて下記のページを参考にしてudev rulesの設定とuserへのplugdevグループ追加も行います。
Probe Setup - probe-rs
プロジェクト
githubにも上げています。https://github.com/asukiaaa/rust-stm32h753zi-practice
ファイルを示しつつ要所を説明します。
stm32h753v向けのCargoファイルです。
2019年以後に製造されたICは、末尾にvが付いたfeatureを利用します。
参考: rust stm32h7-halでstm32h753zi向けビルドはstm32h753vを使わないと速度が1/10になる
Cargo.toml
[package]
name = "rust-stm32h753zi-practice"
version = "0.1.0"
edition = "2024"
[dependencies]
cortex-m = "0.7.7"
cortex-m-rt = "0.7.5"
embedded-hal = "1.0.0"
panic-halt = "0.2.0"
[dependencies.stm32h7xx-hal]
version = "^0"
features = ["stm32h753v", "rt"]
[[bin]]
# to avoid error of vscode for test env https://github.com/rust-lang/vscode-rust/issues/729#issuecomment-1102050361
name = "rust-stm32h753zi-practice"
test = false
bench = false
上記のCargoのbinの設定は、vscodeのエラー回避のためです。
その設定がないとtest環境のビルドに対するエラー「can't find crate for `test`」がmain.rsの最初の行に出てきます。
参考: no_std Can't find crate for 'test' #729
mainプログラムです。
nucleo-h753vのボタンが押されたら緑のLEDを点滅、押されてなければ赤のLEDを光らせます。
stm32h7-halのblinkyを参考にしました。
src/main.rs
#![no_main]
#![no_std]
use cortex_m_rt::entry;
use stm32h7xx_hal::{pac, prelude::*};
use panic_halt as _;
#[entry]
fn main() -> ! {
let cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();
let rcc = dp.RCC.constrain();
let pwr = dp.PWR.constrain();
let pwrcfg = pwr.freeze();
let ccdr = rcc
.freeze(pwrcfg, &dp.SYSCFG); // Freeze configuration, apply voltage scaling and flash latency
let gpiob = dp.GPIOB.split(ccdr.peripheral.GPIOB);
let gpioc = dp.GPIOC.split(ccdr.peripheral.GPIOC);
let mut led_red = gpiob.pb14.into_push_pull_output();
let mut led_green = gpiob.pb0.into_push_pull_output();
let button = gpioc.pc13.into_pull_down_input();
let mut delay = cp.SYST.delay(ccdr.clocks);
loop {
if button.is_high() {
led_red.set_low();
led_green.set_high();
} else {
// pressed
led_red.set_high();
led_green.set_low();
}
delay.delay_ms(1000_u16);
led_red.set_low();
led_green.set_low();
delay.delay_ms(1000_u16);
}
}
組み込み向けのビルド設定です。
probe-rsを使う場合はrunnerにprobe-rsの記述を、そうでなければrunnerにarm-none-eabi-gdbを設定します。
.cargo/config.toml
[target.thumbv7em-none-eabihf]
# runner = 'arm-none-eabi-gdb'
runner = "probe-rs run --chip STM32H753ZITx --connect-under-reset"
rustflags = [
# LLD (shipped with the Rust toolchain) is used as the default linker
"-C", "link-arg=-Tlink.x",
]
[build]
target = "thumbv7em-none-eabihf"
stm32h753zi向けのメモリ設定です。
stm32h7-halのmemory.xがstm32h753ziで使える設定だったのでそのまま使いました。
コメントを見て設定変更が必要なマイコンの場合は書き換えてください。
memory.x
MEMORY
{
/* FLASH and RAM are mandatory memory regions */
/* STM32H742xI/743xI/753xI */
/* STM32H745xI/747xI/755xI/757xI */
/* STM32H7A3xI/7B3xI */
FLASH : ORIGIN = 0x08000000, LENGTH = 2M
/* STM32H742xG/743xG */
/* STM32H745xG/STM32H747xG */
/* STM32H7A3xG */
/* FLASH : ORIGIN = 0x08000000, LENGTH = 512K */
/* FLASH1 : ORIGIN = 0x08100000, LENGTH = 512K */
/* STM32H750xB */
/* STM32H7B0 */
/* FLASH : ORIGIN = 0x08000000, LENGTH = 128K */
/* DTCM */
RAM : ORIGIN = 0x20000000, LENGTH = 128K
/* AXISRAM */
AXISRAM : ORIGIN = 0x24000000, LENGTH = 512K
/* SRAM */
SRAM1 : ORIGIN = 0x30000000, LENGTH = 128K
SRAM2 : ORIGIN = 0x30020000, LENGTH = 128K
SRAM3 : ORIGIN = 0x30040000, LENGTH = 32K
SRAM4 : ORIGIN = 0x38000000, LENGTH = 64K
/* Backup SRAM */
BSRAM : ORIGIN = 0x38800000, LENGTH = 4K
/* Instruction TCM */
ITCM : ORIGIN = 0x00000000, LENGTH = 64K
}
/* The location of the stack can be overridden using the
`_stack_start` symbol. Place the stack at the end of RAM */
_stack_start = ORIGIN(RAM) + LENGTH(RAM);
/* The location of the .text section can be overridden using the
`_stext` symbol. By default it will place after .vector_table */
/* _stext = ORIGIN(FLASH) + 0x40c; */
/* These sections are used for some of the examples */
SECTIONS {
.axisram (NOLOAD) : ALIGN(8) {
*(.axisram .axisram.*);
. = ALIGN(8);
} > AXISRAM
/* The SRAM1 and SRAM2 section are commonly used as the stack and heap for the
CM4 core in dualcore versions and should thus not be used in examples*/
.sram3 (NOLOAD) : ALIGN(4) {
*(.sram3 .sram3.*);
. = ALIGN(4);
} > SRAM3
.sram4 (NOLOAD) : ALIGN(4) {
*(.sram4 .sram4.*);
. = ALIGN(4);
} > SRAM4
};
書き込んで動作確認
cargo runコマンドの実行でビルドと書き込みが行われます。cargo run
書き込みが成功するとstlinkとの通信状態を維持したままプログラムが実行されます。
stlinkの通信LEDが頻繁に光ります。
期待通りに書き込めれば、ボタンを押している間は緑点滅、ボタンを押していなければ赤点滅します。
stlinkのファームウェア更新が必要というエラーが表示された場合は、指摘の通りファームウェアを更新すると書き込み可能になる場合があります。
ubuntuからSTM32CubeProgrammerを利用してstlinkのファームウェアを更新する
probe-rsを使わずarm-none-eabi-objcopyとopenocdで書き込む
stlinkのファームウェアが古くてエラーが出るもののstlinkを更新したくなかったり更新環境が用意できず現状を維持したい場合は、arm-none-eabi-objcopyでファイルを変換してopenocdを使って書き込みも可能です。ubuntu24.04の場合は下記のコマンドでarm-none-eabi-objcopyをインストールできます。
sudo apt install gcc-arm-none-eabi
openocdもaptコマンドでインストールできますが、udev rulesの設定が別途必要です。
sudo apt install openocd
curl -fsSL https://raw.githubusercontent.com/openocd-org/openocd/refs/heads/master/contrib/60-openocd.rules | sudo tee /etc/udev/rules.d/60-openocd.rules
arm-none-eabi-objcopyとopenocdをインストールできたら、下記のコマンド実行でビルド、binファイル作成、書き込みを行えます。
script/upload
#!/bin/bash
DIR_PROJ=$(realpath $(dirname $(realpath $0))/../)
cd $DIR_PROJ
echo $DIR_PROJ
echo "${DIR_PROJ}/target/thumbv7em-none-eabihf/debug/rust-stm32h753zi-practice"
cargo build
if [[ $? != 0 ]]; then
echo "failed building"
exit 1
fi
arm-none-eabi-objcopy -O binary "${DIR_PROJ}/target/thumbv7em-none-eabihf/debug/rust-stm32h753zi-practice" "${DIR_PROJ}/target/thumbv7em-none-eabihf/debug/firmware.bin"
if [[ $? != 0 ]]; then
echo "failed building hex file"
exit 1
fi
FILE_FIRMWARE=${DIR_PROJ}/target/thumbv7em-none-eabihf/debug/firmware.bin
openocd \
-f interface/stlink.cfg \
-f target/stm32h7x.cfg \
-c init \
-c "reset halt" \
-c "flash write_image erase $FILE_FIRMWARE 0x08000000" \
-c "verify_image $FILE_FIRMWARE 0x08000000" \
-c "reset run" \
-c shutdown
参考: STM32F103 に Rust に stm32f1xx-hal を使ってビルドして書き込む
おわり
halで末尾がvのfeatureを使わないと速度が遅くなったり、probe-rsの使い方把握に時間がかかりましたが、目標通りstm32h753zi向けのrust開発環境を把握できて良かったです。参考
probe-rsの関連リンクです。Installation - probe-rs
Probe Setup - probe-rs
今回解説したプログラムを含むリポジトリです。
https://github.com/asukiaaa/rust-stm32h753zi-practice
利用したhalです。
stm32h7-hal
halのマイコン設定として2019年以後製造品には末尾vのfeatureを使う必要があると気づいた時の記事です。
rust stm32h7-halでstm32h753zi向けビルドはstm32h753vを使わないと速度が1/10になる
vscodeのエラー回避方法を把握したスレッドです。
no_std Can't find crate for 'test' #729
stlinkのファームウェア更新方法を把握した時の記事です。
ubuntuからSTM32CubeProgrammerを利用してstlinkのファームウェアを更新する
stm向けのrustのhalの組み立て方の参考にした記事です。
STM32F103 に Rust に stm32f1xx-hal を使ってビルドして書き込む



0 件のコメント :
コメントを投稿