Skip to content

Commit 716c6f2

Browse files
committed
Initial commit
0 parents  commit 716c6f2

File tree

10 files changed

+295
-0
lines changed

10 files changed

+295
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
7+
[Makefile]
8+
indent_style = tab
9+
indent_size = 4

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
build
3+
pioasm.d.ts

LICENSE

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2021 Uri Shaked
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
22+
23+
----
24+
25+
pioasm license:
26+
27+
Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd.
28+
29+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
30+
following conditions are met:
31+
32+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
33+
disclaimer.
34+
35+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
36+
disclaimer in the documentation and/or other materials provided with the distribution.
37+
38+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
39+
derived from this software without specific prior written permission.
40+
41+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
42+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
45+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
46+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
47+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Makefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# pioasm.js
2+
# Copyright (C) 2021, Uri Shaked
3+
#
4+
# To compile:
5+
# 1. Install emscripten sdk (2.x) and set the corresponding environment variables
6+
# 2. Install the pi-pico-sdk, and set PICO_SDK_PATH to point at it, e.g.
7+
# export PICO_SDK_PATH=~/pico/pico-sdk
8+
9+
EMCC_FLAGS = -s WASM=1 -s MODULARIZE=1 -s EXPORTED_RUNTIME_METHODS=callMain,FS_writeFile -O3
10+
OBJ_FILES = CMakeFiles/pioasm.dir/main.cpp.o
11+
12+
all: build/pioasm.js build/pioasm-browser.js
13+
14+
build/pioasm.js: $(OBJ_FILES)
15+
emcc -o build/pioasm.js $(EMCC_FLAGS) `find build -name "*.o"`
16+
17+
build/pioasm-browser.js: $(OBJ_FILES) | build/pioasm.js build/browser
18+
emcc -o build/browser/pioasm.js $(EMCC_FLAGS) -s ENVIRONMENT=web `find build -name "*.o"`
19+
mv build/browser/pioasm.js build/pioasm-browser.js
20+
21+
clean:
22+
rm -rf build
23+
24+
CMakeFiles/pioasm.dir/main.cpp.o: build/Makefile
25+
cd build && emmake make
26+
27+
build/Makefile: build
28+
cd build && emcmake cmake ${PICO_SDK_PATH}/tools/pioasm
29+
30+
build:
31+
mkdir build
32+
echo '{"type": "commonjs"}' > build/package.json
33+
34+
build/browser:
35+
mkdir build/browser

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# pioasm-wasm
2+
3+
Raspberry Pi Pico pioasm tool compiled to Web Assembly.
4+
5+
Usage example:
6+
7+
```javascript
8+
import { pioasm } from 'pioasm';
9+
10+
const source = `
11+
.program blink
12+
pull block
13+
out y, 32
14+
`;
15+
16+
pioasm(source).then(result => {
17+
console.log(result);
18+
})
19+
```
20+
21+
## License
22+
23+
This project, excluding pioasm, is released under the MIT license. Consult [the LICENSE file](LICENSE) for more details.

package-lock.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "pioasm",
3+
"version": "1.0.0",
4+
"description": "Raspberry Pi Pico pioasm compiled to Web Assembly",
5+
"main": "pioasm.js",
6+
"type": "module",
7+
"files": [
8+
"pioasm.js",
9+
"pioasm.d.ts",
10+
"build/pioasm.js",
11+
"build/pioasm-browser.js",
12+
"build/pioasm.wasm",
13+
"LICENSE"
14+
],
15+
"browser": {
16+
"#pioasm-emcc": "./build/pioasm-browser.js"
17+
},
18+
"imports": {
19+
"#pioasm-emcc": {
20+
"browser": "./build/pioasm-browser.js",
21+
"default": "./build/pioasm.js"
22+
}
23+
},
24+
"scripts": {
25+
"prepublish": "npm run build",
26+
"build": "tsc --declaration --emitDeclarationOnly --allowJS pioasm.js",
27+
"test": "node test"
28+
},
29+
"keywords": [
30+
"Raspberry Pi Pico",
31+
"PIO",
32+
"Programmable IO",
33+
"Web Assembly",
34+
"WASM",
35+
"Emscripten"
36+
],
37+
"author": "Uri Shaked",
38+
"license": "MIT",
39+
"devDependencies": {
40+
"typescript": "^4.3.2"
41+
},
42+
"dependencies": {
43+
"@types/emscripten": "^1.39.4"
44+
}
45+
}

pioasm.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/** pioasm in Web Assembly
2+
* Copyright (C) 2021, Uri Shaked
3+
*/
4+
5+
/// <reference types="emscripten" />
6+
7+
import wasm from '#pioasm-emcc';
8+
9+
let instance = null;
10+
let outputBuffer = [];
11+
12+
/**
13+
* PIO Output format:
14+
* - c-sdk: C header suitable for use with the Raspberry Pi Pico SDK
15+
* - python: Python file suitable for use with MicroPython
16+
* - hex: Raw hex output (only valid for single program inputs)
17+
* - ada: Ada specification
18+
*
19+
* @typedef {'c-sdk'|'python'|'hex'|'ada'} PioOutputFormat
20+
*/
21+
22+
/**
23+
* Loads the pioasm Web Assembly module. Normally, `pioasm()` will load the module for
24+
* you, but you can use the `load()` method to pre-loader the Web Assembly module, or
25+
* if you need to provide custom options to EMScripten.
26+
*
27+
* For instance, you can override the `locateFile(url: string, scriptDirectory: string)`
28+
* method to configure the URL for the compiled web assembly module.
29+
*
30+
* @param {Partial<EmscriptenModule>} [options]
31+
* @returns {Promise<EmscriptenModule>}
32+
*/
33+
export async function load(options) {
34+
if (!instance) {
35+
instance = wasm({
36+
noInitialRun: true,
37+
print(msg) {
38+
outputBuffer.push(msg);
39+
},
40+
...options,
41+
});
42+
}
43+
return await instance;
44+
}
45+
46+
/**
47+
* Compiles the given PIO source file.
48+
*
49+
* @param {string} source PIO source to compile
50+
* @param {PioOutputFormat} [format='c-sdk'] Output format
51+
* @param {string} [outputParam] Add a parameter to be passed to the output format generator
52+
* @returns Promise<String>
53+
*/
54+
export async function pioasm(source, format = 'c-sdk', outputParam) {
55+
const runtime = await load();
56+
runtime.FS_writeFile('/input.pio', source);
57+
outputBuffer = [];
58+
const argv = ['-o', format];
59+
if (outputParam) {
60+
argv.push('-p', outputParam);
61+
}
62+
argv.push('input.pio');
63+
runtime.callMain(argv);
64+
return outputBuffer.join('\n');
65+
}

prettier.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
arrowParens: 'always',
3+
printWidth: 100,
4+
singleQuote: true,
5+
tabWidth: 2,
6+
endOfLine: 'auto',
7+
};

test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* pioasm-wasm test code
3+
*/
4+
5+
import assert from 'assert';
6+
import { pioasm } from './pioasm.js';
7+
8+
const testInput = `
9+
.program blink
10+
pull block
11+
out y, 32
12+
`;
13+
14+
const expectedOutput = `# -------------------------------------------------- #
15+
# This file is autogenerated by pioasm; do not edit! #
16+
# -------------------------------------------------- #
17+
18+
import rp2
19+
from machine import Pin
20+
# ----- #
21+
# blink #
22+
# ----- #
23+
24+
@rp2.asm_pio()
25+
def blink():
26+
wrap_target()
27+
pull(, block) # 0
28+
out(y, 32) # 1
29+
wrap()
30+
31+
`;
32+
33+
async function main() {
34+
assert.strictEqual(await pioasm(testInput, 'python'), expectedOutput);
35+
assert.strictEqual(await pioasm(testInput, 'hex'), '80a0\n6040');
36+
console.log('Test passed successfully!');
37+
}
38+
39+
main().catch((err) => {
40+
console.error(err);
41+
process.exit(1);
42+
});

0 commit comments

Comments
 (0)