Rust

2020年10月 5日 (月)

プログラミング言語RustをXIAOで動かす(v2)

2020/10/5:
初版

Rust XIAO v2

Rust XIAO v2

概要

プログラミング言語Rustを以下のXIAOで動かす(v2)。
前の記事をubuntu_20.04に対応して改版した。
なお、ホストPCとしてはubuntu_20.04を想定している。

Seeeduino XIAO

関連ツールのインストール

(0)udev設定
以下を実行する:

curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core/master/scripts/99-platformio-udev.rules | sudo tee /etc/udev/rules.d/99-platformio-udev.rules sudo udevadm control --reload-rules sudo usermod -a -G dialout $USER sudo usermod -a -G plugdev $USER

(1)bossac(書き込み)ツールのインストール
以下で必要なツールをインスールする:

sudo apt-get update sudo apt-get install bossa sudo apt-get install bossa-cli

bossaでGUIツール、bossacでCLIツールが起動する。
参照:https://www.shumatech.com/web/products/bossa

(2)rustツールのインストール

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh #入力を促すので、「1」を入力する source $HOME/.cargo/env

(3)Cコンパイラのインストール
binを作成するのに、これに含まれるツールを使用するのでインストールする

sudo apt-get install gcc-arm-none-eabi

(4)picocom(通信ソフト)のインストール

sudo apt-get install picocom

(5)rust関連ツールのインストール

# for samd11, samd21: rustup target add thumbv6m-none-eabi # for samd51, same54: rustup target add thumbv7em-none-eabihf # uf2conv etc install rustup component add llvm-tools-preview cargo install uf2conv cargo-binutils

blinkを実行する

# download git clone https://github.com/atsamd-rs/atsamd.git cd atsamd/boards/xiao_m0 # build cargo build --example blink # conv elf to bin arm-none-eabi-objcopy -O binary target/thumbv6m-none-eabi/debug/examples/blink firmware.bin # bootloader-modeにする(resetを2度押す) # flash bossac -p /dev/ttyACM0 -i -e -w -v -R -o 0x2000 firmware.bin

実行結果として、オンボードの3つのLEDが点滅する。 (グリーンのLEDは通電表示のLEDなので点滅せずに光っている)

出力例:

$ bossac -p /dev/ttyACM0 -i -e -w -v -R -o 0x2000 firmware.bin Device : ATSAMD21x18 Version : v1.1 [Arduino:XYZ] Nov 27 2019 16:35:59 Address : 0x0 Pages : 4096 Page Size : 64 bytes Total Size : 256KB Planes : 1 Lock Regions : 16 Locked : none Security : false BOD : true BOR : true Erase flash Done in 0.834 seconds Write 13260 bytes to flash (208 pages) [==============================] 100% (208/208 pages) Done in 0.087 seconds Verify 13260 bytes of flash [==============================] 100% (208/208 pages) Verify successful Done in 0.080 seconds

usb_serial

# download git clone https://github.com/atsamd-rs/atsamd.git cd atsamd/boards/xiao_m0 # build cargo build --example usb_serial --features="usb" # conv elf to bin arm-none-eabi-objcopy -O binary target/thumbv6m-none-eabi/debug/examples/usb_serial firmware.bin # bootloader-modeにする(resetを2度押す) # flash bossac -p /dev/ttyACM0 -i -e -w -v -R -o 0x2000 firmware.bin

書き込みが終了すると、USBシリアルのエコープログラムが動作する。
「picocom /dev/ttyACM0 -b115200」で通信プログラムを起動して キー入力すると、入力したキーが表示される。キーの入力に応じて、 オンボードの青いLEDが点滅する。

参照情報

旧版:プログラミング言語RustをXIAOで動かす

atsamd & atsame support for Rust
Running bossac on the command line

STM32 Nucleo Board STM32F103RB

The Embedded Rust Book
The Embedded Rust Book - Semihosting

UbuntuにRustをインストールする

Adding a new board

以上

続きを読む "プログラミング言語RustをXIAOで動かす(v2)"

| | コメント (0)

2020年6月 9日 (火)

プログラミング言語RustをXIAOで動かす

2020/6/9:
初版

Rust XIAO

Rust XIAO

概要

プログラミング言語Rustを以下のXIAOで動かす。 (ホストPCとしてはubuntuを想定している)

Seeeduino XIAO

関連ツールのインストール

以下で必要なツールをインスールする:
(0)bossac(書き込み)ツールのインストール

sudo apt-get install build-essential git clone https://github.com/shumatech/BOSSA/tree/arduino cd BOSSA make bin/bossac -j4 sudo cp bin/bossac /bin/

(1)rustツールのインストール

curl https://sh.rustup.rs -sSf | sh

(2)Cコンパイラのバージョンアップ
binを作成するのに、これに含まれるツールを使用するのでインストールする

以下でインストールしたコンパイラが古くてビルドエラーが出たのでバージョンアップする。 sudo apt-get install gcc-arm-none-eabi (1)以下のurlから最新版をダウンロードする: https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads 解凍したものを以下のフォルダに置く: $HOME/Downloads/gcc-arm-none-eabi-8-2019-q3-update (2)パス設定 #古いコンパイラを削除する sudo apt-get remove gcc-arm-none-eabi #パスを設定する export PATH=$PATH:$HOME/Downloads/gcc-arm-none-eabi-8-2019-q3-update/bin

exportは、.bashrcに登録する

(3)picocom(通信ソフト)のインストール

sudo apt-get install picocom

(4)rust関連ツールのインストール

# for samd11, samd21: rustup target add thumbv6m-none-eabi # for samd51, same54: rustup target add thumbv7em-none-eabihf # uf2conv etc install rustup component add llvm-tools-preview cargo install uf2conv cargo-binutils

blinkを実行する

# download git clone https://github.com/atsamd-rs/atsamd.git cd atsamd/boards/xiao_m0 # build cargo build --example blink # conv elf to bin arm-none-eabi-objcopy -O binary target/thumbv6m-none-eabi/debug/examples/blink firmware.bin # bootloader-modeにする(resetを2度押す) # flash bossac -p /dev/ttyACM0 -i -e -w -v -R -o 0x2000 firmware.bin

実行結果として、オンボードの3つのLEDが点滅する。 (グリーンのLEDは通電表示のLEDなので点滅せずに光っている)

出力例:

$ bossac -p /dev/ttyACM0 -i -e -w -v -R -o 0x2000 firmware.bin Device : ATSAMD21x18 Version : v1.1 [Arduino:XYZ] Nov 27 2019 16:35:59 Address : 0x0 Pages : 4096 Page Size : 64 bytes Total Size : 256KB Planes : 1 Lock Regions : 16 Locked : none Security : false BOD : true BOR : true Erase flash Done in 0.834 seconds Write 13260 bytes to flash (208 pages) [==============================] 100% (208/208 pages) Done in 0.087 seconds Verify 13260 bytes of flash [==============================] 100% (208/208 pages) Verify successful Done in 0.080 seconds

usb_serial

# download git clone https://github.com/atsamd-rs/atsamd.git cd atsamd/boards/xiao_m0 # build cargo build --example usb_serial --features="usb" # conv elf to bin arm-none-eabi-objcopy -O binary target/thumbv6m-none-eabi/debug/examples/usb_serial firmware.bin # bootloader-modeにする(resetを2度押す) # flash bossac -p /dev/ttyACM0 -i -e -w -v -R -o 0x2000 firmware.bin

書き込みが終了すると、USBシリアルのエコープログラムが動作する。
「picocom /dev/ttyACM0 -b115200」で通信プログラムを起動して キー入力すると、入力したキーが表示される。キーの入力に応じて、 オンボードの青いLEDが点滅する。

参照情報

atsamd & atsame support for Rust
Running bossac on the command line

STM32 Nucleo Board STM32F103RB

The Embedded Rust Book
The Embedded Rust Book - Semihosting

UbuntuにRustをインストールする

Adding a new board

以上

続きを読む "プログラミング言語RustをXIAOで動かす"

| | コメント (0)

2020年5月28日 (木)

プログラミング言語RustをFeather-M4-Expressで動かす

2020/5/30:
書き込みツール(bossac)の説明を追加した。

2020/5/28
初版

Rust Feather-M4-Express

Rust Feather-M4-Express

概要

プログラミング言語Rustを以下のFeather-M4-Expressで動かす。 (ホストPCとしてはubuntuを想定している)

Feather-M4-Express

関連ツールのインストール

以下で必要なツールをインスールする:
(1)rustツールのインストール

curl https://sh.rustup.rs -sSf | sh

(2)Cコンパイラのバージョンアップ
binを作成するのに、これに含まれるツールを使用するのでインストールする

以下でインストールしたコンパイラが古くてビルドエラーが出たのでバージョンアップする。 sudo apt-get install gcc-arm-none-eabi (1)以下のurlから最新版をダウンロードする: https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads 解凍したものを以下のフォルダに置く: $HOME/Downloads/gcc-arm-none-eabi-8-2019-q3-update (2)パス設定 #古いコンパイラを削除する sudo apt-get remove gcc-arm-none-eabi #パスを設定する export PATH=$PATH:$HOME/Downloads/gcc-arm-none-eabi-8-2019-q3-update/bin

exportは、.bashrcに登録する

(3)picocom(通信ソフト)のインストール

sudo apt-get install picocom

(4)rust関連ツールのインストール

# for samd11, samd21: rustup target add thumbv6m-none-eabi # for samd51, same54: rustup target add thumbv7em-none-eabihf # uf2conv etc install rustup component add llvm-tools-preview cargo install uf2conv cargo-binutils

blinkyを実行する

# download git clone https://github.com/atsamd-rs/atsamd.git cd atsamd/boards/feather_m4/feather_m4 # build cargo build --example blinky_basic # conv elf to bin arm-none-eabi-objcopy -O binary target/thumbv7em-none-eabihf/debug/examples/blinky_basic firmware.bin # conv bin to uf2 uf2conv firmware.bin --base 0x4000 --output firmware.uf2 # flash # bootloader-modeにする(resetを2度押す) cp firmware.uf2 /media/USER_NAME/FEATHERBOOT/

「USER_NAME」は、自分の環境に合わせて変更すること。
実行結果として、オンボードのLEDが点滅する。

オンボードのnexpixelを動かす

# build cargo build --example neopixel_rainbow cargo build --example neopixel_rainbow --release # debugでビルドした後、releaseでビルドする必要がある(そうしないととエラーになる) # conv elf to bin arm-none-eabi-objcopy -O binary target/thumbv7em-none-eabihf/release/examples/neopixel_rainbow firmware.bin # 性能問題があるのでdebugではなくreleaseを採用する # conv bin to uf2 uf2conv firmware.bin --base 0x4000 --output firmware.uf2 # flash # bootloader-modeにする(resetを2度押す) cp firmware.uf2 /media/USER_NAME/FEATHERBOOT/

「USER_NAME」は、自分の環境に合わせて変更すること。
実行結果として、オンボードのneopixelが色を変えながら光る。

serialを動かす

# build cargo build --example serial # conv elf to bin arm-none-eabi-objcopy -O binary target/thumbv7em-none-eabihf/debug/examples/serial firmware.bin # conv bin to uf2 uf2conv firmware.bin --base 0x4000 --output firmware.uf2 # flash # bootloader-modeにする(resetを2度押す) cp firmware.uf2 /media/USER_NAME/FEATHERBOOT/

「USER_NAME」は、自分の環境に合わせて変更すること。
USBシリアルを、RX,TX,GNDと接続して、実行すると 文字列がシリアル出力される。

出力例:

picocom /dev/ttyUSB0 -b19200 ... Terminal ready Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hell ...

ソース上の文字列に改行などが入っていないので、'Hello, world!'が繋がって出力される。

注意:シリアルが動作中だと「bootloader-mode」に入りにくいようだ。 その場合、resetを押した後、再度、resetを2度押すと、そのモードに入るようだ。

serial_echoを動かす

# build cargo build --example uart_poll_echo # conv elf to bin arm-none-eabi-objcopy -O binary target/thumbv7em-none-eabihf/debug/examples/uart_poll_echo firmware.bin # conv bin to uf2 uf2conv firmware.bin --base 0x4000 --output firmware.uf2 # flash # bootloader-modeにする(resetを2度押す) cp firmware.uf2 /media/USER_NAME/FEATHERBOOT/

「USER_NAME」は、自分の環境に合わせて変更すること。
USBシリアルを、RX,TX,GNDと接続して、実行して 通信ソフト「picocom /dev/ttyUSB0 -b19200」を起動する。 そこで、文字をタイプすると、そのまま、文字が表示される。.

注意:シリアルが動作中だと「bootloader-mode」に入りにくいようだ。 その場合、resetを押した後、再度、resetを2度押すと、そのモードに入るようだ。

timerを動かす

# build cargo build --example timers # conv elf to bin arm-none-eabi-objcopy -O binary target/thumbv7em-none-eabihf/debug/examples/timers firmware.bin # conv bin to uf2 uf2conv firmware.bin --base 0x4000 --output firmware.uf2 # flash # bootloader-modeにする(resetを2度押す) cp firmware.uf2 /media/USER_NAME/FEATHERBOOT/

「USER_NAME」は、自分の環境に合わせて変更すること。
実行結果として、タイマーの周期でオンボードのLEDが点滅する。
なお、周期がわかりにくかったので、ソースの「timer.start(50u32.hz());」を 「timer.start(5u32.hz());」に変更した。

乱数生成を動かす

# build cargo build --example trng # conv elf to bin arm-none-eabi-objcopy -O binary target/thumbv7em-none-eabihf/debug/examples/trng firmware.bin # conv bin to uf2 uf2conv firmware.bin --base 0x4000 --output firmware.uf2 # flash # bootloader-modeにする(resetを2度押す) cp firmware.uf2 /media/USER_NAME/FEATHERBOOT/

「USER_NAME」は、自分の環境に合わせて変更すること。
実行結果として、生成した乱数の周期でオンボードのLEDが点滅する。

bossacによる書き込み

uf2ファイルに変換せずにbinのまま書き込みができるbossacというツールがあるので それの使い方について説明する。
(1)bossacツールのインストール

sudo apt-get install build-essential git clone https://github.com/shumatech/BOSSA/tree/arduino cd BOSSA make bin/bossac -j4 sudo cp bin/bossac /bin/

(2)bossacによる書き込み(例)

# build cargo build --example blinky_basic # conv elf to bin arm-none-eabi-objcopy -O binary target/thumbv7em-none-eabihf/debug/examples/blinky_basic firmware.bin #バイナリを作るところまでは同じ # flash # bootloader-modeにする(resetを2度押す) bossac -p /dev/ttyACM0 -i -e -w -v -R -o 0x4000 firmware.bin

出力例:

$ bossac -p /dev/ttyACM0 -i -e -w -v -R -o 0x4000 firmware.bin Device : ATSAMD51x19 Version : v1.1 [Arduino:XYZ] May 17 2020 17:56:23 Address : 0x0 Pages : 1024 Page Size : 512 bytes Total Size : 512KB Planes : 1 Lock Regions : 32 Locked : none Security : false BOD : false BOR : true Erase flash Done in 2.383 seconds Write 13852 bytes to flash (28 pages) [==============================] 100% (28/28 pages) Done in 0.072 seconds Verify 13852 bytes of flash [==============================] 100% (28/28 pages) Verify successful Done in 0.029 seconds

参照情報

atsamd & atsame support for Rust
Running bossac on the command line

STM32 Nucleo Board STM32F103RB

The Embedded Rust Book
The Embedded Rust Book - Semihosting

UbuntuにRustをインストールする

Adding a new board

以上

続きを読む "プログラミング言語RustをFeather-M4-Expressで動かす"

| | コメント (0)

プログラミング言語RustをCircuit-Playground-Expressで動かす

2020/5/30:
書き込みツール(bossac)の説明を追加した。

2020/5/27:
初版

Rust Circuit-Playground-Express

Rust Circuit-Playground-Express

概要

プログラミング言語Rustを以下のCircuit-Playground-Expressで動かす。 (ホストPCとしてはubuntuを想定している)

Circuit-Playground-Express

関連ツールのインストール

以下で必要なツールをインスールする:
(1)rustツールのインストール

curl https://sh.rustup.rs -sSf | sh

(2)Cコンパイラのバージョンアップ
binを作成するのに、これに含まれるツールを使用するのでインストールする

以下でインストールしたコンパイラが古くてビルドエラーが出たのでバージョンアップする。 sudo apt-get install gcc-arm-none-eabi (1)以下のurlから最新版をダウンロードする: https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads 解凍したものを以下のフォルダに置く: $HOME/Downloads/gcc-arm-none-eabi-8-2019-q3-update (2)パス設定 #古いコンパイラを削除する sudo apt-get remove gcc-arm-none-eabi #パスを設定する export PATH=$PATH:$HOME/Downloads/gcc-arm-none-eabi-8-2019-q3-update/bin

exportは、.bashrcに登録する

(3)picocom(通信ソフト)のインストール

sudo apt-get install picocom

(4)rust関連ツールのインストール

# for samd11, samd21: rustup target add thumbv6m-none-eabi # for samd51, same54: rustup target add thumbv7em-none-eabihf # uf2conv etc install rustup component add llvm-tools-preview cargo install uf2conv cargo-binutils

blinkyを実行する

# download git clone https://github.com/atsamd-rs/atsamd.git cd atsamd/boards/circuit_playground_express # build cargo build --example blinky_basic # conv elf to bin arm-none-eabi-objcopy -O binary target/thumbv6m-none-eabi/debug/examples/blinky_basic firmware.bin # conv bin to uf2 uf2conv firmware.bin --base 0x2000 --output firmware.uf2 # flash # bootloader-modeにする(resetを2度押す) cp firmware.uf2 /media/USER_NAME/CPLAYBOOT/

「USER_NAME」は、自分の環境に合わせて変更すること。 実行結果として、オンボードのLEDが点滅する。

bossacによる書き込み

uf2ファイルに変換せずにbinのまま書き込みができるbossacというツールがあるので それの使い方について説明する。
(1)bossacツールのインストール

sudo apt-get install build-essential git clone https://github.com/shumatech/BOSSA/tree/arduino cd BOSSA make bin/bossac -j4 sudo cp bin/bossac /bin/

(2)bossacによる書き込み

... # conv elf to bin arm-none-eabi-objcopy -O binary target/thumbv6m-none-eabi/debug/examples/blinky_basic firmware.bin #バイナリを作るところまでは同じ # flash # bootloader-modeにする(resetを2度押す) bossac -p /dev/ttyACM0 -i -e -w -v -R -o 0x2000 firmware.bin

出力例:

$ bossac -p /dev/ttyACM0 -i -e -w -v -R -o 0x2000 firmware.bin Device : ATSAMD21x18 Version : v1.1 [Arduino:XYZ] May 17 2020 17:56:16 Address : 0x0 Pages : 4096 Page Size : 64 bytes Total Size : 256KB Planes : 1 Lock Regions : 16 Locked : none Security : false BOD : true BOR : true Erase flash Done in 0.756 seconds Write 13392 bytes to flash (210 pages) [==============================] 100% (210/210 pages) Done in 0.085 seconds Verify 13392 bytes of flash [==============================] 100% (210/210 pages) Verify successful Done in 0.084 seconds

参照情報

atsamd & atsame support for Rust
Running bossac on the command line

STM32 Nucleo Board STM32F103RB

The Embedded Rust Book
The Embedded Rust Book - Semihosting

UbuntuにRustをインストールする

Adding a new board

以上

続きを読む "プログラミング言語RustをCircuit-Playground-Expressで動かす"

| | コメント (0)

2020年5月11日 (月)

プログラミング言語RustをNucleo-F103RBボードで動かす(STM32F1xx)

2020/5/11

Rust STM32F1xx

Rust STM32F1xx

概要

プログラミング言語Rustを以下のNucleo-F103RBボードで動かす。 (ホストPCとしてはubuntuを想定している)

STM32 Nucleo Board STM32F103RB

関連ツールのインストール

以下で必要なツールをインスールする:
(0)rustツールのインストール

curl https://sh.rustup.rs -sSf | sh

(1)書き込みツール(st-flash)のインストール

sudo apt install cmake sudo apt install libusb-1.0 git clone https://github.com/texane/stlink.git cd stlink make cd build/Release sudo make install sudo ldconfig

(2)Cコンパイラのバージョンアップ
binを作成するのに、これに含まれるツールを使用するのでインストールする

以下でインストールしたコンパイラが古くてビルドエラーが出たのでバージョンアップする。 sudo apt-get install gcc-arm-none-eabi (1)以下のurlから最新版をダウンロードする: https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads 解凍したものを以下のフォルダに置く: $HOME/Downloads/gcc-arm-none-eabi-8-2019-q3-update (2)パス設定 #古いコンパイラを削除する sudo apt-get remove gcc-arm-none-eabi #パスを設定する export PATH=$PATH:$HOME/Downloads/gcc-arm-none-eabi-8-2019-q3-update/bin

exportは、.bashrcに登録する

(3)その他の必要ツールのインストール

sudo apt-get install openocd sudo apt-get install picocom

sample#0(blinky)を実行する

最初の設定のターゲットボードがblue-pillになっているので、Nucleo-F103RBの設定に変更して動かす。

(1)rust関連の設定

git clone https://github.com/stm32-rs/stm32f1xx-hal.git cd stm32f1xx-hal rustup default nightly rustup target add thumbv7m-none-eabi

(2)memory.xの変更
以下のように変更する:
(Flash容量を64Kから128Kに変更する)

cat memory.x /* Linker script for the NUCLEO-F103RB */ MEMORY { FLASH : ORIGIN = 0x08000000, LENGTH = 128K RAM : ORIGIN = 0x20000000, LENGTH = 20K }

(3)examples/blinky.rsの変更
(オンボードのLEDのポートをpc13からpa5に変更する)

37行付近: let mut gpioc = dp.GPIOC.split(&mut rcc.apb2); → let mut gpioa = dp.GPIOA.split(&mut rcc.apb2);
40行付近: let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh); → let mut led = gpioa.pa5.into_push_pull_output(&mut gpioa.crl);

点滅周期を変更したい場合、以下のような変更を行なう:

42行付近: let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(1.hz()); → let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(10.hz());

(4)ビルド

cargo clean cargo build --example blinky --release --features=stm32f103

(5)elfをbinに変換する

arm-none-eabi-objcopy -O binary target/thumbv7m-none-eabi/release/examples/blinky F103_blinky.bin

(6)書き込む

#ボートとホストPCをUSBで接続する st-flash write F103_blinky.bin 0x8000000

ボードのグリーンLED(LD2)が点滅すれば動作としてはOKとなる。

その他のサンプル

以下に、その他のサンプルがあるのでボードに依存するポートなどを変更すれば、Nucleo-F103RBで動作すると思われる。

$ ls examples adc-dma-circ.rs itm.rs serial-dma-peek.rs adc-dma-rx.rs led.rs serial-dma-rx.rs adc.rs mfrc522.rs serial-dma-tx.rs adc_temperature.rs motor.rs.disabled serial-fmt.rs blinky.rs mpu9250.rs.disabled serial.rs blinky_generic.rs nojtag.rs serial_config.rs blinky_rtc.rs panics.rs spi-dma.rs blinky_timer_irq.rs pwm.rs timer-interrupt-rtfm.rs delay.rs pwm_custom.rs usb_serial.rs enc28j60-coap.rs.disabled pwm_input.rs usb_serial_interrupt.rs enc28j60.rs.disabled qei.rs usb_serial_rtfm.rs exti.rs rtc.rs hello.rs serial-dma-circ.rs

ターゲット情報

Cortex-Mプロセッサとターゲットは以下の対応になっている:

thumbv6m-none-eabi - Cortex-M0 と M0+ thumbv7m-none-eabi - Cortex M3 thumbv7em-none-eabi - Cortex M4 と M7、FPU なし thumbv7em-none-eabihf - Cortex M4 と M7、FPU あり

参照情報

STM32 Nucleo Board STM32F103RB

The Embedded Rust Book
The Embedded Rust Book - Semihosting

UbuntuにRustをインストールする

以上

続きを読む "プログラミング言語RustをNucleo-F103RBボードで動かす(STM32F1xx)"

| | コメント (0)

2020年5月10日 (日)

プログラミング言語Rustをmicro:bitボードで動かす

2020/5/10

Rust micro:bit

Rust micro:bit

概要

プログラミング言語Rustを以下のmicro:bitボードで動かす。 (ホストPCとしてはubuntuを想定している)

micro:bit

関連ツールのインストール

以下で必要なツールをインスールする:
(0)rustツールのインストール

curl https://sh.rustup.rs -sSf | sh

(1)Cコンパイラのバージョンアップ
binを作成するのに、これに含まれるツールを使用するのでインストールする

以下でインストールしたコンパイラが古くてビルドエラーが出たのでバージョンアップする。 sudo apt-get install gcc-arm-none-eabi (1)以下のurlから最新版をダウンロードする: https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads 解凍したものを以下のフォルダに置く: $HOME/Downloads/gcc-arm-none-eabi-8-2019-q3-update (2)パス設定 #古いコンパイラを削除する sudo apt-get remove gcc-arm-none-eabi #パスを設定する export PATH=$PATH:$HOME/Downloads/gcc-arm-none-eabi-8-2019-q3-update/bin

exportは、.bashrcに登録する

(2)その他の必要ツールのインストール

sudo apt-get install openocd sudo apt-get install picocom

事前準備

openocdとgdbを使用してビルドしたelfを自動的に書き込むので 以下のように起動時の.gdbinitを用意する:

(1)~/.gdbinit
以下の内容に編集する:

add-auto-load-safe-path /home/user/rust_ws/mb_work/microbit/.gdbinit

上の「/home/user/rust_ws/mb_work/microbit/」は、gdb実行時のカレント・ディレクトリになるので、自分の環境に合わせる。

(2)~/rust_ws/mb_work/microbit/.gdbinit
以下の内容に編集する:

target remote :3333 monitor arm semihosting enable load continue

(3)別の端末でopenocdを起動しておく。
micro:bitをホストPCと接続して 以下を実行する。
(gdbの接続待ちになる)

openocd -f interface/cmsis-dap.cfg -f target/nrf51.cfg

シリアルを利用したサンプルを実行する場合、 さらに別端末でシリアル通信を起動しておく。

picocom /dev/ttyACM0 -b115200

samplesを実行する

以下を実行する:

rustup update rustup target add thumbv6m-none-eabi #サンプルをgitでダウンロードする git clone https://github.com/therealprof/microbit.git cd microbit #たとえば、以下のうち一つを実行すると #gdbが自動的に起動しプログラムを書き込んで実行される cargo run --example gpio_hal_blinky cargo run --example serial_direct_helloworld cargo run --example gpio_hal_ledbutton cargo run --example gpio_hal_printbuttons cargo run --example led_rtfm cargo run --example serial_hal_blocking_echo #以下、gdbの出力例: .. semihosting is enabled Loading section .vector_table, size 0xa8 lma 0x0 Loading section .text, size 0x486c lma 0xa8 Loading section .rodata, size 0xc14 lma 0x4920 Start address 0x4768, load size 21800 #この時点で、書き込んだプログラムが実行される

なお、gdbから抜ける場合は、quitを実行する。

実行結果

1.gpio_hal_blinky
micro:bitのLEDの一つが点滅する。

2.gpio_hal_ledbutton
ボタンを押すとLEDが光る。

3.led_rtfm
LEDがハートマークに光る。

4.serial_direct_helloworld
シリアル出力で'hello world'が出力される。

5.serial_hal_blocking_echo
エコープログラムになっており シリアル通信の画面で入力すると 入力した文字が返ってくる。

6.gpio_hal_printbuttons
ボタンを押すと、その状態を示す テキストがシリアル出力される。

なお、上のものは、色々あるサンプルのうちの一部である。

参照情報

micro:bit

The Embedded Rust Book
The Embedded Rust Book - Semihosting

UbuntuにRustをインストールする

以上

続きを読む "プログラミング言語Rustをmicro:bitボードで動かす"

| | コメント (0)

2020年5月 9日 (土)

プログラミング言語RustをLongan-Nanoボードで動かす(GD32VF103CBT6)

2020/5/9

Rust Longan Nano

Rust Longan Nano

概要

プログラミング言語Rustを以下のLongan-Nanoボードで動かす。 (ホストPCとしてはubuntuを想定している)

Sipeed Longan Nano RISC-V GD32VF103CBT6開発ボード

関連ツールのインストール

以下で必要なツールをインスールする:
(1)rustツールのインストール

curl https://sh.rustup.rs -sSf | sh

(2)その他の必要ツールのインストール

sudo apt-get install picocom

Rust(RISC-V)関係ツールのインストール

(1)Installing dependencies

rustup default stable rustup target add riscv32imac-unknown-none-elf rustup default nightly rustup target add riscv32imac-unknown-none-elf

(2)RISC-V toolchain (e.g. from SiFive) install

mkdir riscv-gnu-toolchain cd riscv-gnu-toolchain wget https://static.dev.sifive.com/dev-tools/riscv64-unknown-elf-gcc-8.3.0-2019.08.0-x86_64-linux-ubuntu14.tar.gz tar -zxvf riscv64-unknown-elf-gcc-8.3.0-2019.08.0-x86_64-linux-ubuntu14.tar.gz export PATH=$PATH:~/riscv-gnu-toolchain/riscv64-unknown-elf-gcc-8.3.0-2019.08.0-x86_64-linux-ubuntu14/bin

(3)GD32対応(GD32VF103 support)のdfu-utilのインストール

mkdir gd32-tools cd gd32-tools git clone https://github.com/riscv-mcu/gd32-dfu-utils.git cd gd32-dfu-utils ./autogen.sh ./configure make sudo cp src/dfu-util /opt/bin/gd32-dfu-util export PATH=$PATH:/opt/bin

「dfu-util 0.9」でGD32対応済みの可能性があるが ここでは、GD32でフォークしたものを使用する。

(参考)古いバージョンでの問題点:
https://github.com/riscv-rust/longan-nano/issues/5

以上で、exportとしたものは、.bashrcに登録すること。

Build

以下を実行してサンプルコードをビルドする。

git clone https://github.com/riscv-rust/longan-nano.git cd longa-nano nano .cargo/config 以下のように修正する: "link-arg=-Tmemory-c8.x" → "link-arg=-Tmemory-cb.x" # To build all the provided examples cargo build --examples --release --all-features

make bin & flash it

以下を実行してbin(複数)を作成してボードに書き込む。

riscv64-unknown-elf-objcopy -O binary target/riscv32imac-unknown-none-elf/release/examples/blinky blinky.bin riscv64-unknown-elf-objcopy -O binary target/riscv32imac-unknown-none-elf/release/examples/ferris ferris.bin riscv64-unknown-elf-objcopy -O binary target/riscv32imac-unknown-none-elf/release/examples/display display.bin riscv64-unknown-elf-objcopy -O binary target/riscv32imac-unknown-none-elf/release/examples/hello_world hello_world.bin riscv64-unknown-elf-objcopy -O binary target/riscv32imac-unknown-none-elf/release/examples/scan scan.bin # Keep the BOOT0 button pressed while power-up or # while pressing and releaseing the reset button. #以下から実行したいbinを1つ選び実行する: gd32-dfu-util -a 0 -s 0x08000000:leave -D blinky.bin gd32-dfu-util -a 0 -s 0x08000000:leave -D ferris.bin gd32-dfu-util -a 0 -s 0x08000000:leave -D display.bin gd32-dfu-util -a 0 -s 0x08000000:leave -D hello_world.bin gd32-dfu-util -a 0 -s 0x08000000:leave -D scan.bin

書き込み後、(再起動しないようなので)[reset]ボタンを押すこと。

実行結果

(1)blinky
実行すると、LEDが赤、緑、青と繰り返し変化して光る。

(2)ferris
実行すると、LCDに、ferris(蟹)の画像が表示される。
蟹がRust言語のマスコットになっているらしい。。。

以下にFerrisの画像がある:
https://rustacean.net/

(3)display
実行すると、LCDに" Hello Rust! "のテキストが表示される。

(4)hello_world
USBシリアルを接続して、実行すると以下のように"Hello, world"がシリアルに出力される。

picocom /dev/ttyUSB0 -b115200 Terminal ready Hello, world

(5)scan
USBシリアルを接続して、実行すると以下がシリアルに出力される。

picocom /dev/ttyUSB0 -b115200 Terminal ready scan started 00000000..20007fff 40000000..40017fff 40020000..400207ff 40021000..400213ff 40022000..400223ff 40023000..400233ff 50000000..5003ffff 60000000..a0000fff d1000000..d1000fff d2000000..d200ffff e0000000..efffffff ==> ffffff00 scan finished

なお、USBシリアルは、GNG/T0/R0にクロスで接続する。(3V3は接続しない)

参照情報

Sipeed Longan Nano RISC-V GD32VF103CBT6開発ボード

The Embedded Rust Book
The Embedded Rust Book - Semihosting

UbuntuにRustをインストールする

以上

続きを読む "プログラミング言語RustをLongan-Nanoボードで動かす(GD32VF103CBT6)"

| | コメント (0)

2020年5月 8日 (金)

プログラミング言語RustをNucleo-F401REボードで動かす(STM32F4xxx)

2020/5/8

Rust STM32F4xx

Rust STM32F4xx

概要

プログラミング言語Rustを以下のNucleo-F401REボードで動かす。 (ホストPCとしてはubuntuを想定している)

STM32 Nucleo Board STM32F401

関連ツールのインストール

以下で必要なツールをインスールする:
(0)rustツールのインストール

curl https://sh.rustup.rs -sSf | sh

(1)書き込みツール(st-flash)のインストール

sudo apt install cmake sudo apt install libusb-1.0 git clone https://github.com/texane/stlink.git cd stlink make cd build/Release sudo make install sudo ldconfig

(2)Cコンパイラのバージョンアップ
binを作成するのに、これに含まれるツールを使用するのでインストールする

以下でインストールしたコンパイラが古くてビルドエラーが出たのでバージョンアップする。 sudo apt-get install gcc-arm-none-eabi (1)以下のurlから最新版をダウンロードする: https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads 解凍したものを以下のフォルダに置く: $HOME/Downloads/gcc-arm-none-eabi-8-2019-q3-update (2)パス設定 #古いコンパイラを削除する sudo apt-get remove gcc-arm-none-eabi #パスを設定する export PATH=$PATH:$HOME/Downloads/gcc-arm-none-eabi-8-2019-q3-update/bin

exportは、.bashrcに登録する

(3)その他の必要ツールのインストール

sudo apt-get install openocd sudo apt-get install picocom

sample#0( gpio_hal_blinky)をビルドして実行する

最初の設定がターゲットボードとしてSTM32F401REになっているので、そのまま動かす。

git clone https://github.com/jkristell/nucleo-f401re.git cd nucleo-f401re rustup target add thumbv7em-none-eabihf cargo clean cargo build --example gpio_hal_blinky arm-none-eabi-objcopy -O binary target/thumbv7em-none-eabihf/debug/examples/gpio_hal_blinky firmware.bin #ボードのUSBコネクタをホストPCに接続する st-flash write firmware.bin 0x8000000

正常に動作した場合、ボードのグリーンLEDが点滅する。

sample#1(delay-blinky)をビルドして実行する

上とは別のgitから持ってきたものを実行してみる。
ターゲットボードをSTM32F401REにするために memory.xを以下に変更する。
(gitでダウンロード後)

memory.x

MEMORY { /* NOTE K = KiBi = 1024 bytes */ FLASH : ORIGIN = 0x08000000, LENGTH = 512K RAM : ORIGIN = 0x20000000, LENGTH = 96K } /* This is where the call stack will be allocated. */ /* The stack is of the full descending type. */ /* NOTE Do NOT modify `_stack_start` unless you know what you are doing */ _stack_start = ORIGIN(RAM) + LENGTH(RAM);

以下の手順を実行する。
(memory.xは、上の内容に変更すること)

git clone https://github.com/stm32-rs/stm32f4xx-hal.git cd stm32f4xx-hal rustup target add thumbv7em-none-eabihf cargo clean cargo build --example delay-blinky --release --features="rt stm32f411" # elfをbinに変換する arm-none-eabi-objcopy -O binary target/thumbv7em-none-eabihf/release/examples/delay-blinky firmare.bin # binを書き込む #ボードのUSBコネクタをホストPCに接続する st-flash write firmware.bin 0x8000000 以下のようなエラーがでて書き込めない場合、OpenOCDで書き込む。 $ st-flash write firmware.bin 0x8000000 st-flash 1.6.0 2020-05-08T20:53:06 INFO common.c: Loading device parameters.... 2020-05-08T20:53:06 INFO common.c: Device connected is: F4 device (Dynamic Efficency), id 0x10006433 2020-05-08T20:53:06 INFO common.c: SRAM size: 0x18000 bytes (96 KiB), Flash: 0x80000 bytes (512 KiB) in pages of 16384 bytes open(firmware.bin) == -1 2020-05-08T20:53:06 ERROR common.c: map_file() == -1 stlink_fwrite_flash() == -1

正常に動作した場合、ボードのグリーンLEDが点滅する。(sample#0と同様)

OpenOCDによる書き込み

今までは、st-flashで書き込んだが、以下の手順でOpenOCDで書き込むこともできる。
別の端末で以下を実行する:

openocd -f interface/stlink-v2-1.cfg -f target/stm32f4x.cfg

エラーになった場合、stlinkのファームウェアが古い可能性があるので そのときは「tlink-v2-1.cfg」を「tlink-v2.cfg」に変更して実行する。 実行するとopenocdがサーバーとして動作して、gdb(やtelnet)を待ち受ける状態になる。

buildの作業で使っている端末で以下を実行する: (delay-blinkyの場合)

cargo run --example delay-blinky --release --features="rt stm32f411" #ビルドが完了すると自動的にgdbが起動するので #以下の手順でopenocdと接続してファームウェアを書き込む。 (gdb) target remote :3333 Remote debugging using :3333 0x00000000 in ?? () (gdb) load Loading section .vector_table, size 0x198 lma 0x8000000 Loading section .text, size 0x50c lma 0x8000198 Loading section .rodata, size 0x13c lma 0x80006b0 Start address 0x800055c, load size 2016 Transfer rate: 3 KB/sec, 672 bytes/write. (gdb) quit Quit anyway? (y or n) y #[reset]ボタンは無効になっているようなので、 #USB接続を切って電源をオフした後、再度、USBを接続して再起動すること

openocdの起動用スクリプトを設定すると書き込みを自動化できるようだが、ここでは、それを使用していない。

ターゲット情報

Cortex-Mプロセッサとターゲットは以下の対応になっている:

thumbv6m-none-eabi - Cortex-M0 と M0+ thumbv7m-none-eabi - Cortex M3 thumbv7em-none-eabi - Cortex M4 と M7、FPU なし thumbv7em-none-eabihf - Cortex M4 と M7、FPU あり

参照情報

STM32 Nucleo Board STM32F401

The Embedded Rust Book
The Embedded Rust Book - Semihosting

UbuntuにRustをインストールする

以上

続きを読む "プログラミング言語RustをNucleo-F401REボードで動かす(STM32F4xxx)"

| | コメント (0)

プログラミング言語RustをSTM32F3Discovery/Nucleo-F303K8ボードで動かす(STM32F3xx)

2020/5/8+

Rust STM32F3xx

Rust STM32F3xx

概要

プログラミング言語Rustを以下のSTM32F3Discovery/Nucleo-F303K8ボードで動かす。 (ホストPCとしてはubuntuを想定している)

STM32F3DISCOVERY
STM32 Nucleo Board STM32F303K8

関連ツールのインストール

以下で必要なツールをインスールする:
(0)rustツールのインストール

curl https://sh.rustup.rs -sSf | sh

(1)書き込みツール(st-flash)のインストール

sudo apt install cmake sudo apt install libusb-1.0 git clone https://github.com/texane/stlink.git cd stlink make cd build/Release sudo make install sudo ldconfig

(2)Cコンパイラのバージョンアップ
binを作成するのに、これに含まれるツールを使用するのでインストールする

以下でインストールしたコンパイラが古くてビルドエラーが出たのでバージョンアップする。 sudo apt-get install gcc-arm-none-eabi (1)以下のurlから最新版をダウンロードする: https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads 解凍したものを以下のフォルダに置く: $HOME/Downloads/gcc-arm-none-eabi-8-2019-q3-update (2)パス設定 #古いコンパイラを削除する sudo apt-get remove gcc-arm-none-eabi #パスを設定する export PATH=$PATH:$HOME/Downloads/gcc-arm-none-eabi-8-2019-q3-update/bin

exportは、.bashrcに登録する

(3)その他の必要ツールのインストール

sudo apt-get install openocd sudo apt-get install picocom

sample#0(usb_serial)をビルドする

最初の設定がターゲットボードとしてSTM32F3Discoveryになっているので、まずは、STM32F3Discoveryで動かす。

git clone https://github.com/stm32-rs/stm32f3xx-hal.git cd stm32f3xx-hal rustup target add thumbv7em-none-eabihf cargo clean cargo build --example usb_serial --release --features="rt stm32f303xc stm32-usbd"

ビルドしたfirmwareを書き込む

# elfをbinに変換する arm-none-eabi-objcopy -O binary target/thumbv7em-none-eabihf/release/examples/usb_serial firmware.bin # binを書き込む #[USB ST-LINK]と印刷したあるUSBコネクタをホストPCに接続する st-flash write firmware.bin 0x8000000 出力例: $ st-flash write firmware.bin 0x8000000 st-flash 1.6.0 2020-05-08T09:42:28 INFO usb.c: -- exit_dfu_mode 2020-05-08T09:42:28 INFO common.c: Loading device parameters.... 2020-05-08T09:42:28 INFO common.c: Device connected is: F3 device, id 0x10036422 2020-05-08T09:42:28 INFO common.c: SRAM size: 0xa000 bytes (40 KiB), Flash: 0x40000 bytes (256 KiB) in pages of 2048 bytes 2020-05-08T09:42:28 INFO common.c: Attempting to write 17148 (0x42fc) bytes to stm32 address: 134217728 (0x8000000) Flash page at addr: 0x08004000 erased 2020-05-08T09:42:29 INFO common.c: Finished erasing 9 pages of 2048 (0x800) bytes 2020-05-08T09:42:29 INFO common.c: Starting Flash write for VL/F0/F3/F1_XL core id 2020-05-08T09:42:29 INFO flash_loader.c: Successfully loaded flash loader in sram 9/9 pages written 2020-05-08T09:42:30 INFO common.c: Starting verification of write complete 2020-05-08T09:42:30 INFO common.c: Flash written and verified! jolly good! #USBシリアルのデモプログラムなので、[USB USER]と印刷してあるUSBコネクタをホストPCに接続する picocom /dev/ttyACM0 -b115200 #エコープログラムなので、キーボードから入力し、そのまま文字が表示される。 #(ただし、英字はシフトに関係なく、全て大文字になる) #以上のような動きになればOKとなる #以下でUSBデバイスの状況を確認する dmesg 出力例: <省略> [38985.161676] usb 1-1.2: Product: Serial port [38985.161680] usb 1-1.2: Manufacturer: Rust Embedded company [38985.161683] usb 1-1.2: SerialNumber: STM32F3Disco [38985.162359] cdc_acm 1-1.2:1.0: ttyACM0: USB ACM device # 以上のように、Product/Manufacturer/SerialNumberが返ってくることが確認できる # ただし、上の例では、コンパイルの確認のためにManufacturer/SerialNumberを変更している

sample#1(toggle)

以下の手順で別のサンプルを実行できる:

cargo clean cargo build --example toggle --release --features="rt stm32f303xc" arm-none-eabi-objcopy -O binary target/thumbv7em-none-eabihf/release/examples/toggle firmware.bin #[USB ST-LINK]と印刷してあるUSBコネクタをホストPCに接続する st-flash write firmware.bin 0x8000000

ボードのLD10が点滅すれば動作OKとなる。

なお、以下の対応関係があるので、examples/toggle.rsの「.pe13」の部分を 別のポートに変更すれば、他のLEDを点滅させることができる。

User LD3: red LED: PE9 of the STM32F303VCT6. User LD4: blue LED: PE8 of the STM32F303VCT6. User LD5: orange LED: PE10 of the STM32F303VCT6. User LD6: green LED: PE15 of the STM32F303VCT6. User LD7: green LED: PE11 of the STM32F303VCT6. User LD8: orange LED: PE14 of the STM32F303VCT6. User LD9: blue LED: PE12 of the STM32F303VCT6. User LD10: red LED: PE13 of the STM32F303VCT6.

sample#2(pwm)

以下の手順で別のサンプルを実行できる:

cargo clean cargo build --example pwm --release --features="stm32f303xc" arm-none-eabi-objcopy -O binary target/thumbv7em-none-eabihf/release/examples/pwm firmware.bin #[USB ST-LINK]と印刷してあるUSBコネクタをホストPCに接続する st-flash write firmware.bin 0x8000000

PA4,PA7,PB5に接続したLEDがPWMで光れば動作としてはOKとなる。

OpenOCDによる書き込み

今までは、st-flashで書き込んだが、以下の手順でOpenOCDで書き込むこともできる。
別の端末で以下を実行する:

openocd -f interface/stlink-v2.cfg -f target/stm32f3x.cfg

エラーになった場合、stlinkのファームウェアが新しい可能性があるので そのときは「tlink-v2.cfg」を「tlink-v2-1.cfg」に変更して実行する。 実行するとopenocdがサーバーとして動作して、gdb(やtelnet)を待ち受ける状態になる。

buildの作業で使っている端末で以下を実行する: (usb_serialの場合)

cargo run --example usb_serial --release --features="rt stm32f303xc stm32-usbd" #ビルドが完了すると自動的にgdbが起動するので #以下の手順でopenocdと接続してファームウェアを書き込む。 (gdb) target remote :3333 Remote debugging using :3333 0x00000000 in ?? () (gdb) load Loading section .vector_table, size 0x194 lma 0x8000000 Loading section .text, size 0x34f4 lma 0x8000194 Loading section .rodata, size 0xc6c lma 0x8003688 Start address 0x8000e8e, load size 17140 Transfer rate: 15 KB/sec, 5713 bytes/write. (gdb) quit

openocdの起動用スクリプトを設定すると書き込みを自動化できるようだが、ここでは、それを使用していない。

memory.x

stm32f3xx-halディレクトリにあるmemory.xはリンク用スクリプトで以下のように設定されている:

/* Linker script for the STM32F303VCT6 */ MEMORY { CCRAM : ORIGIN = 0x10000000, LENGTH = 8K FLASH : ORIGIN = 0x08000000, LENGTH = 256K RAM : ORIGIN = 0x20000000, LENGTH = 40K } _stack_start = ORIGIN(CCRAM) + LENGTH(CCRAM);

これを他のボード(F303xxxx)用に変更すれば、 他のボードでもビルドできるはずである。

ターゲット情報

Cortex-Mプロセッサとターゲットは以下の対応になっている:

thumbv6m-none-eabi - Cortex-M0 と M0+ thumbv7m-none-eabi - Cortex M3 thumbv7em-none-eabi - Cortex M4 と M7、FPU なし thumbv7em-none-eabihf - Cortex M4 と M7、FPU あり

Nucleo-F303K8対応

ここからは、設定をNucleo-F303K8に変更して、Nucleo-F303K8で動かしてみる。
そのために以下の変更/修正を行なう:
(1)memory.xの変更
以下のように変更する:

/* Linker script for the STM32F303K8 */ MEMORY { FLASH : ORIGIN = 0x08000000, LENGTH = 64K RAM : ORIGIN = 0x20000000, LENGTH = 12K } _stack_start = ORIGIN(RAM) + LENGTH(RAM);

(2)example/toggle.rsの修正
(LEDのポートを変更する)

20行付近: let mut gpioe = dp.GPIOE.split(&mut rcc.ahb); → let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
25行付近: let mut led = gpioe .pe13 .into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper); → let mut led = gpiob //gpioe .pb3 .into_push_pull_output(&mut gpiob.moder, &mut gpiob.otyper);

(3)ビルド&書き込み&実行

cargo clean cargo build --example toggle --release --features="rt stm32f303xc" arm-none-eabi-objcopy -O binary target/thumbv7em-none-eabihf/release/examples/toggle firmware.bin #F303K8のUSBコネクタをホストPCに接続する st-flash write firmware.bin 0x8000000

F303K8ボードのグリーンLEDが点滅すれば動作としてはOKとなる。

参照情報

STM32F3DISCOVERY
STM32 Nucleo Board STM32F303K8

The Embedded Rust Book
The Embedded Rust Book - Semihosting

UbuntuにRustをインストールする

以上

続きを読む "プログラミング言語RustをSTM32F3Discovery/Nucleo-F303K8ボードで動かす(STM32F3xx)"

| | コメント (0)

2020年4月18日 (土)

Rustの出力したwasmを動かす

2020/4/18

Rust WASM

Rust WASM

概要

Rustの出力したwasmを動かす。

準備

Rustのインストール (インストール済みであれば不要)

curl https://sh.rustup.rs -sSf | sh rustup update

手順1

# WebAssemblyへのコンパイル機能を有効化 rustup target add wasm32-unknown-unknown mkdir rust_wasm cd rust_wasm # 新しいプロジェクト wasam-test を作成する cargo new --lib wasm-test nano Cargo.toml # Cargo.tomlを編集して以下を追加する:
[lib] crate-type = ["cdylib"]

続き

nano src/lib.rs # src/lib.rs を編集して以下の内容にする:

src/lib.rs

#[no_mangle] pub fn add(a: i32, b: i32) -> i32 { a + b }

続き:

# 以下を実行してビルドする cargo build --target=wasm32-unknown-unknown --release # 以下を実行して wasm ができていることを確認する ls target/wasm32-unknown-unknown/release/*.wasm target/wasm32-unknown-unknown/release/wasm_test.wasm # 注意:「wasm-test」が「wasm_test」になるので注意すること nano index.html # 以下の内容のindex.htmlを作成する:

index.html

<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>Hello, WebAssembly!</title> <script> const wasm = './target/wasm32-unknown-unknown/release/wasm_test.wasm' fetch(wasm) .then(response => response.arrayBuffer()) .then(bytes => WebAssembly.instantiate(bytes, {})) .then(results => { console.log('wasm output:') console.log(results.instance.exports.add(34, 56)) }) </script> </head> </html>

続き:

# http-serverを起動する python3 -m http.server 8080 # web-browserで「localhost:8080」にアクセスする。 # consoleに以下が表示されていればOK: # wasm output: # 90

consoleに以下のようなエラーが出た場合、MIME-typeにwasmが登録されていないことになる。

TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'.

そのときは、/etc/mime.typesにwasmのMIMEtypeを登録する。

sudo leafpad /etc/mime.types 以下を末尾に追加する:
application/wasm wasm

登録後、http-serverを再起動する。

手順2

上の手順1の方法では文字列などをJavascript側に渡すことができない。 文字列を渡したい場合は、変換ツール(wasm-bindgen-cli)によってwasmをjsのモジュールに変換し そのモジュールを使うことにより文字列を引き渡すことができる。

以下、そのやり方について記する;

# 変換ツールのインストール cargo install wasm-bindgen-cli # 新しいプロジェクト wasam-test2 を作成する cargo new --lib wasm-test nano Cargo.toml # Cargo.tomlを編集して以下を追加する:
[dependencies] wasm-bindgen = "0.2" [lib] crate-type = ["cdylib"]

続き:

nano src/lib.rs # src/lib.rs を編集して以下の内容にする:

src/lib.rs

extern crate wasm_bindgen; use wasm_bindgen::prelude::*; #[wasm_bindgen] pub fn hello() -> String { //"Hello WASM world!".to_string() ←こちらでも良い String::from("Hello WASM world!") } #[wasm_bindgen] pub fn add(a: i32, b: i32) -> i32 { a + b }

続き:

# 以下を実行してビルドする cargo build --target=wasm32-unknown-unknown --release # 以下を実行して、wasmをjsに変換する wasm-bindgen target/wasm32-unknown-unknown/release/wasm_test2.wasm --out-dir ./pkg --target web # jsが生成されていることの確認 ls pkg/*.js pkg/wasm_test2.js nano index.html # 以下の内容のindex.htmlを作成する:

index.html

<html> <head> <meta content="text/html;charset=utf-8" http-equiv="Content-Type"/> </head> <body> <script type="module" src='./index.js'></script> Rust -> Wasm -> Js test </body> </html>

注意:「<script type="module" 」であることに注意のこと

nano index.js # 以下の内容を作成する:

index.js

// in script[type=module] import init, {add, hello } from './pkg/wasm_test2.js'; (async () => { await init(); // call Rust func console.log(hello()); console.log('wasm->js output:') console.log(add(34,56)); })();

続き:

# http-serverを起動する python3 -m http.server 8080 # web-browserで「localhost:8080」にアクセスする。 # consoleに以下が表示されていればOK: # Hello WASM world! # wasm->js output: # 90

サイズ情報

$ ls -l target/wasm32-unknown-unknown/release/wasm_test2.wasm -rwxrwxr-x 2 xxxx 1794765 4月 18 10:49 target/wasm32-unknown-unknown/release/wasm_test2.wasm $ ls -l pkg 合計 32 -rw-rw-r-- 1 xxxx 881 4月 18 10:49 wasm_test2.d.ts -rw-rw-r-- 1 xxxx 2831 4月 18 10:49 wasm_test2.js -rw-rw-r-- 1 xxxx 235 4月 18 10:49 wasm_test2_bg.d.ts -rw-rw-r-- 1 xxxx 16946 4月 18 10:49 wasm_test2_bg.wasm

wasm_test2.wasmと(変換後の)wasm_test2.jsのサイズを比較すると、ずいぶん小さくなっている印象があるが、該当モジュールのなかで、wasm_test2_bg.wasmを呼び出しているようだ。

参考情報

GO/TinyGOの出力したwasmを動かす

WebAssembly入門
バンドラを使わずにRustをWASMにする
爆速でjsでWebAssemblyを動かす

Incorrect response MIME type. Expected 'application/wasm'エラーの対応

以上

続きを読む "Rustの出力したwasmを動かす"

| | コメント (0)