Try it here: https://lupyuen.github.io/bl602-simulator/
Read the article...
Follow the updates on Twitter: https://twitter.com/MisterTechBlog/status/1423169766080933891
Let's Simulate BL602 / BL604 Rust Firmware in a Web Browser with WebAssembly...
-
We take this BL602 / BL604 Blinky Firmware in Rust...
-
Which calls the Rust Wrapper for BL602 IoT SDK...
-
We compile to WebAssembly the Rust Firmware and Rust Wrapper
-
In WebAssembly we intercept calls to BL602 IoT SDK with Shim Functions
(Like for the BL602 GPIO HAL)
-
Add a Simulator UI (HTML + JavaScript) to simulate a PineCone BL602 or PineDio Stack BL604...
(Without the Blockly part, since we can't compile Rust in a Web Browser)
Why do this in Rust?
-
Because we have already parsed the BL602 IoT SDK interfaces with
bindgen
(While creating the BL602 Rust Wrapper)
-
Which lets us manipulate the BL602 SDK interfaces with Rust in interesting ways
(Like our
safe_wrap
Procedural Macro in Rust) -
More about BL602 Rust Wrapper...
Why are we doing this? What problem are we solving?
-
Shorten the Code - Build - Flash - Test Cycle for BL602 and BL604
(Because flashing BL602 via UART is kinda cumbersome)
-
We could potentially catch BL602 SDK Calling Errors for new devs and explain the errors in a friendly way
(Invalid parameters or usage, like reading a GPIO Pin configured for output)
-
Make it easier to Learn Embedded Programming
(Even without any Embedded Hardware)
-
Automated Testing of BL602 Firmware
-
Trace Calls to BL602 IoT SDK for debugging
We might be able to Simulate C Firmware too, if we...
-
Tweak the BL602 C Firmware to build with Emscripten
-
And call the Shim Functions
To compile BL602 Rust Firmware into WebAssembly...
# Configure emscripten. See https://emscripten.org/docs/getting_started/downloads.html
# For Windows: emsdk\emsdk_env.bat
. ~/emsdk/emsdk_env.sh
# Download source code
git clone --recursive https://github.com/lupyuen/bl602-simulator
cd bl602-simulator
# Compile the Rust Firmware, Rust Simulator Library and link with Emscripten
make
# Produces outputs in the `docs` folder: wasm.js, wasm.wasm
To run the BL602 Simulator...
-
Start a Local Web Server
-
Browse to
docs/wasm.html
-
Click
Run
# Build the Rust Firmware and Rust Simulator Library
cargo build --target wasm32-unknown-emscripten
Compiling proc-macro2 v1.0.28
Compiling unicode-xid v0.2.2
Compiling syn v1.0.74
Compiling memchr v2.4.0
Compiling serde_derive v1.0.127
Compiling cty v0.2.1
Compiling serde v1.0.127
Compiling ryu v1.0.5
Compiling heapless v0.7.4
Compiling rustc-serialize v0.3.24
Compiling lazy_static v1.4.0
Compiling serde_json v1.0.66
Compiling cstr_core v0.2.4
Compiling quote v1.0.9
Compiling bl602-macros v0.0.2
Compiling bl602-sdk v0.0.6
Compiling app v0.0.1 (/mnt/c/pinecone/bl602-simulator/sdk_app_rust_gpio/rust)
Compiling bl602-simulator v0.0.1 (/mnt/c/pinecone/bl602-simulator/bl602-simulator)
Finished dev [unoptimized + debuginfo] target(s) in 1m 43s
# Link the Rust Firmware and Rust Simulator Library with Emscripten
emcc -o wasm/wasm.html \
target/wasm32-unknown-emscripten/debug/libapp.a \
target/wasm32-unknown-emscripten/debug/libbl602_simulator.a \
wasm/wasm.o \
-g \
-s WASM=1 \
-s DISABLE_EXCEPTION_CATCHING=0 \
-s "EXPORTED_FUNCTIONS=[ '_rust_main', '_clear_simulation_events', '_get_simulation_events' ]" \
-s "EXTRA_EXPORTED_RUNTIME_METHODS=[ 'cwrap', 'allocate', 'intArrayFromString', 'UTF8ToString' ]"
# Copy the WebAssembly outputs to the docs folder for GitHub Pages
cp wasm/wasm.js docs
cp wasm/wasm.wasm docs