Skip to content

Commit cfa2b28

Browse files
committed
Add PlatformIO example
1 parent b94a83f commit cfa2b28

28 files changed

+1244
-0
lines changed

examples_pio/Wasm_Advanced/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.pio

examples_pio/Wasm_Advanced/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## PlatformIO example (advanced)
2+
3+
`./wasm_apps` contains sample apps in `C/C++`, `Rust`, `AssemblyScript`, `TinyGo`
4+
5+
`./wasm_vm` contains the host interpreter/VM.
6+
You can add your device type in `platformio.ini`.
7+
Adjust the LED pin number with `-DLED_PIN` option.
8+
9+
To run the example:
10+
```sh
11+
pio run -e <device> -t upload && pio device monitor
12+
```
13+
Where `<device>` is one of:
14+
`ESP32`, `ESP8266`, `Arduino101`, `MKR1000`, `NucleoWB55RG`, `BluePill`, `TinyBLE`, `Teensy31`, `WildFireV3`
15+
16+
**Note:** This example uses Wasm Linear Memory. You should be able to run it on any device that can afford to allocate 1-2 pages of Wasm Linear Memory (i.e. have >= 128KiB RAM).
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
; Common configuration
12+
13+
[platformio]
14+
src_dir = wasm_vm
15+
16+
[env]
17+
framework = arduino
18+
monitor_speed = 115200
19+
20+
lib_deps = Wasm3
21+
22+
src_build_flags =
23+
-Wno-unused-function -Wno-unused-variable -Wno-unused-parameter
24+
-Wno-missing-field-initializers
25+
26+
; Device-specific configuration
27+
28+
[env:ESP32]
29+
platform = espressif32
30+
board = esp32dev
31+
board_build.f_cpu = 240000000L
32+
33+
src_build_flags =
34+
${env.src_build_flags}
35+
-DLED_PIN=19
36+
-DESP32
37+
-O3 -flto
38+
39+
[env:ESP8266]
40+
platform = espressif8266
41+
board = nodemcuv2
42+
board_build.f_cpu = 160000000L
43+
44+
upload_speed = 460800
45+
46+
src_build_flags =
47+
${env.src_build_flags}
48+
-DLED_PIN=13
49+
-DESP8266 -Dd_m3FixedHeap=0x6000
50+
-O3 -flto
51+
52+
[env:Arduino101]
53+
platform = intel_arc32
54+
board = genuino101
55+
56+
src_build_flags =
57+
${env.src_build_flags}
58+
-DLED_PIN=13
59+
-Os -flto
60+
61+
[env:MKR1000]
62+
platform = atmelsam
63+
board = mkr1000USB
64+
65+
src_build_flags =
66+
${env.src_build_flags}
67+
-DLED_PIN=6
68+
-O3 -flto
69+
70+
[env:NucleoWB55RG]
71+
platform = ststm32
72+
board = nucleo_wb55rg_p
73+
upload_protocol = mbed
74+
75+
src_build_flags =
76+
${env.src_build_flags}
77+
-DLED_PIN=PB5
78+
-O3 -flto
79+
80+
[env:BluePill]
81+
platform = ststm32
82+
board = bluepill_f103c8
83+
upload_protocol = stlink
84+
85+
src_build_flags =
86+
${env.src_build_flags}
87+
-DLED_PIN=PC13
88+
-Os -flto
89+
90+
[env:TinyBLE]
91+
platform = nordicnrf51
92+
board = seeedTinyBLE
93+
94+
src_build_flags =
95+
${env.src_build_flags}
96+
-DLED_PIN=23
97+
-Os -flto
98+
99+
[env:Teensy31]
100+
platform = teensy
101+
board = teensy31
102+
upload_protocol = teensy-cli
103+
104+
src_build_flags =
105+
${env.src_build_flags}
106+
-DLED_PIN=13
107+
-O3 -flto
108+
109+
# d_m3SkipMemoryBoundsCheck was needed here,
110+
# as 64-bit operations seem to be broken on AVR
111+
[env:WildFireV3]
112+
platform = atmelavr
113+
board = wildfirev3
114+
115+
src_build_flags =
116+
${env.src_build_flags}
117+
-DLED_PIN=6
118+
-Dd_m3SkipMemoryBoundsCheck=1
119+
-Dd_m3CodePageAlignSize=512
120+
-Os -flto
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
*.wat
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as dev from "./arduino";
2+
3+
let LED = dev.getPinLED();
4+
5+
function cbk(size: usize): usize {
6+
dev.println("!CBK!");
7+
return 123;
8+
}
9+
10+
function setup(): void {
11+
dev.pinMode(LED, dev.OUTPUT);
12+
13+
dev.print('Hello from AssemblyScript 😊\n');
14+
}
15+
16+
function run(): void {
17+
const t = dev.millis();
18+
dev.println('[' + t.toString() + '] ' + dev.getString());
19+
20+
//dev.testCallback(cbk);
21+
22+
dev.digitalWrite(LED, dev.HIGH);
23+
dev.delay(100);
24+
dev.digitalWrite(LED, dev.LOW);
25+
dev.delay(900);
26+
}
27+
28+
/*
29+
* Entry point
30+
*/
31+
export function _start(): void {
32+
setup();
33+
while (1) {
34+
run();
35+
}
36+
}
37+
Binary file not shown.

examples_pio/Wasm_Advanced/wasm_apps/assemblyscript/app.wasm.h

Lines changed: 403 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export const LOW = 0;
2+
export const HIGH = 1;
3+
4+
export const INPUT = 0x0;
5+
export const OUTPUT = 0x1;
6+
export const INPUT_PULLUP = 0x2;
7+
8+
9+
export declare function millis(): u32;
10+
export declare function delay(ms: u32): void;
11+
export declare function pinMode(pin: u32, mode: u32): void;
12+
export declare function digitalWrite(pin: u32, value: u32): void;
13+
export declare function getPinLED(): u32;
14+
15+
@external("print") declare function _print(ptr: usize): void;
16+
@external("getString") declare function _getString(ptr: usize, len: i32): void;
17+
18+
export function print(str: string): void {
19+
_print(changetype<usize>(String.UTF8.encode(str, true)))
20+
}
21+
22+
export function println(str: string): void {
23+
print(str + '\n')
24+
}
25+
26+
export function getString(): string {
27+
const arr = new ArrayBuffer(64)
28+
_getString(changetype<usize>(arr), 64)
29+
return String.UTF8.decode(arr, true)
30+
}
31+
32+
/*
33+
type testFunc = ((size: usize) => usize) | null;
34+
35+
export declare function testCallback(f: testFunc): void;
36+
*/

examples_pio/Wasm_Advanced/wasm_apps/assemblyscript/package-lock.json

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"license": "MIT",
3+
"scripts": {
4+
"build": "npm run asbuild:optimized && xxd -i app.wasm > app.wasm.h",
5+
"asbuild:optimized": "npx asc app.ts -b app.wasm -t app.wat -O3z --runtime half --noAssert --use abort="
6+
},
7+
"devDependencies": {
8+
"assemblyscript": "0.8.1-nightly.20200123"
9+
}
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./node_modules/assemblyscript/std/assembly.json",
3+
"include": [
4+
"./**/*.ts"
5+
]
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
6+
version "0.8.1-nightly.20200123"
7+
resolved "https://registry.yarnpkg.com/assemblyscript/-/assemblyscript-0.8.1-nightly.20200123.tgz#5440a6f891d6417259339f52484d4dee53ae92b6"
8+
integrity sha512-rYNvQWQF8CsiNx8HYUHX9F4KsI69xUuunDY96PPjebTQHoZLjhJ9ow2DaKYJ1Py74+N6QiVwcHzZJC4ECOecAw==
9+
dependencies:
10+
binaryen "90.0.0-nightly.20200101"
11+
long "^4.0.0"
12+
13+
14+
version "90.0.0-nightly.20200101"
15+
resolved "https://registry.yarnpkg.com/binaryen/-/binaryen-90.0.0-nightly.20200101.tgz#75fb065e4a2dd88852c2872085e05eee0883106c"
16+
integrity sha512-64lgruunUnkJD4YVD0DLCSRskqfo3xhoErM1H22GfkkWNT16/7tPTLG+MYqKLMALTiIyLL6L9UqmDaxOxDv0rw==
17+
18+
long@^4.0.0:
19+
version "4.0.0"
20+
resolved "https://registry.yarnpkg.com/long/-/long-4.0.0.tgz#9a7b71cfb7d361a194ea555241c92f7468d5bf28"
21+
integrity sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
#include "arduino_api.h"
3+
4+
int LED_BUILTIN;
5+
6+
void setup() {
7+
LED_BUILTIN = getPinLED();
8+
9+
// initialize digital pin LED_BUILTIN as an output.
10+
pinMode(LED_BUILTIN, OUTPUT);
11+
}
12+
13+
// the loop function runs over and over again forever
14+
void loop() {
15+
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
16+
delay(100); // wait 100ms
17+
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
18+
delay(900); // wait 900ms
19+
}
20+
21+
/*
22+
* Entry point
23+
*/
24+
25+
WASM_EXPORT
26+
void _start() {
27+
setup();
28+
while (1) { loop(); }
29+
}
197 Bytes
Binary file not shown.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
unsigned char app_wasm[] = {
2+
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x11, 0x04, 0x60,
3+
0x02, 0x7f, 0x7f, 0x00, 0x60, 0x00, 0x00, 0x60, 0x01, 0x7f, 0x00, 0x60,
4+
0x00, 0x01, 0x7f, 0x02, 0x4e, 0x04, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69,
5+
0x6e, 0x6f, 0x09, 0x67, 0x65, 0x74, 0x50, 0x69, 0x6e, 0x4c, 0x45, 0x44,
6+
0x00, 0x03, 0x07, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x07, 0x70,
7+
0x69, 0x6e, 0x4d, 0x6f, 0x64, 0x65, 0x00, 0x00, 0x07, 0x61, 0x72, 0x64,
8+
0x75, 0x69, 0x6e, 0x6f, 0x0c, 0x64, 0x69, 0x67, 0x69, 0x74, 0x61, 0x6c,
9+
0x57, 0x72, 0x69, 0x74, 0x65, 0x00, 0x00, 0x07, 0x61, 0x72, 0x64, 0x75,
10+
0x69, 0x6e, 0x6f, 0x05, 0x64, 0x65, 0x6c, 0x61, 0x79, 0x00, 0x02, 0x03,
11+
0x02, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07, 0x13, 0x02, 0x06,
12+
0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x06, 0x5f, 0x73, 0x74,
13+
0x61, 0x72, 0x74, 0x00, 0x04, 0x0a, 0x3a, 0x01, 0x38, 0x01, 0x01, 0x7f,
14+
0x41, 0x80, 0x08, 0x10, 0x00, 0x22, 0x00, 0x36, 0x02, 0x00, 0x20, 0x00,
15+
0x41, 0x01, 0x10, 0x01, 0x03, 0x40, 0x41, 0x80, 0x08, 0x28, 0x02, 0x00,
16+
0x41, 0x01, 0x10, 0x02, 0x41, 0xe4, 0x00, 0x10, 0x03, 0x41, 0x80, 0x08,
17+
0x28, 0x02, 0x00, 0x41, 0x00, 0x10, 0x02, 0x41, 0x84, 0x07, 0x10, 0x03,
18+
0x0c, 0x00, 0x0b, 0x00, 0x0b
19+
};
20+
unsigned int app_wasm_len = 197;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef arduino_wasm_api_h
2+
#define arduino_wasm_api_h
3+
4+
#include <stdint.h>
5+
6+
#define WASM_EXPORT extern "C" __attribute__((used)) __attribute__((visibility ("default")))
7+
#define WASM_EXPORT_AS(NAME) WASM_EXPORT __attribute__((export_name(NAME)))
8+
#define WASM_IMPORT(MODULE,NAME) __attribute__((import_module(MODULE))) __attribute__((import_name(NAME)))
9+
#define WASM_CONSTRUCTOR __attribute__((constructor))
10+
11+
#define LOW 0x0
12+
#define HIGH 0x1
13+
14+
#define INPUT 0x0
15+
#define OUTPUT 0x1
16+
#define INPUT_PULLUP 0x2
17+
18+
extern "C" {
19+
20+
WASM_IMPORT("arduino", "millis") uint32_t millis (void);
21+
WASM_IMPORT("arduino", "delay") void delay (uint32_t ms);
22+
WASM_IMPORT("arduino", "pinMode") void pinMode (uint32_t pin, uint32_t mode);
23+
WASM_IMPORT("arduino", "digitalWrite") void digitalWrite (uint32_t pin, uint32_t value);
24+
25+
// This is a convenience function
26+
WASM_IMPORT("arduino", "getPinLED") uint32_t getPinLED (void);
27+
28+
}
29+
30+
#endif // arduino_wasm_api_h
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
millis
2+
delay
3+
pinMode
4+
digitalWrite
5+
getPinLED
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Compile
2+
wasicc -Os \
3+
-z stack-size=4096 -Wl,--initial-memory=65536 \
4+
-Wl,--allow-undefined-file=arduino_api.syms \
5+
-Wl,--strip-all -nostdlib \
6+
-o app.wasm app.cpp
7+
8+
# Optimize (optional)
9+
wasm-opt -O3 app.wasm -o app.wasm
10+
wasm-strip app.wasm
11+
12+
# Convert to WAT
13+
#wasm2wat app.wasm -o app.wat
14+
15+
# Convert to C header
16+
xxd -i app.wasm > app.wasm.h

0 commit comments

Comments
 (0)