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+
}

0 commit comments

Comments
 (0)