Skip to content

Commit 151e504

Browse files
committed
ci: add micropython test
1 parent 82f2e06 commit 151e504

File tree

6 files changed

+82
-2
lines changed

6 files changed

+82
-2
lines changed

.github/workflows/ci-micropython.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Test MicroPython Releases
2+
on:
3+
push:
4+
pull_request:
5+
branches: [master]
6+
jobs:
7+
test-micropython:
8+
name: Test and Lint
9+
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
micropython_version: [20210618-v1.16, 20210902-v1.17, 20220117-v1.18, 20220618-v1.19.1]
13+
steps:
14+
- uses: actions/checkout@v3
15+
- uses: actions/setup-python@v4
16+
with:
17+
python-version: '3.10'
18+
- name: Install packages
19+
run: npm ci
20+
- name: Install Python requirements
21+
run: cd test && pip install -r requirements.txt
22+
- name: Download Micropython
23+
run: curl -o micropython.uf2 "https://micropython.org/resources/firmware/rp2-pico-${VERSION}.uf2"
24+
env:
25+
VERSION: ${{ matrix.micropython_version }}
26+
- name: Create filesystem
27+
run: python test/mklittlefs.py
28+
- name: Test Micropython
29+
run: timeout 10 npm run start:micropython -- --image micropython.uf2 --expect-text "Hello, MicroPython!"

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ A GDB server on port 3333 can be enabled by specifying the `--gdb` flag:
4949
npm run start:micropython -- --gdb
5050
```
5151

52+
For using the MicroPython demo code in tests, the `--expect-text` can come handy: it will look for the given text in the serial output and exit with code 0 if found, or 1 if not found. You can find an example in [the MicroPython CI test](./github/workflows/ci-micropython.yml).
53+
54+
#### Filesystem support
55+
5256
With MicroPython – and probably also CircuitPython – you can use the filesystem on the Pico. This becomes useful as more than one script file is used in your code. Just put a [LittleFS](https://github.com/littlefs-project/littlefs) formatted filesystem image called `littlefs.img` into the rp2040js root directory, and your `main.py` will be automatically started from there.
5357

5458
A simple way to create a suitable LittleFS image containing your script files is outlined in [create_littlefs_image.py](https://github.com/tomods/GrinderController/blob/358ad3e0f795d8cc0bdf4f21bb35f806871d433f/tools/create_littlefs_image.py).
@@ -77,4 +81,4 @@ Currently, the filesystem is not writeable, as the SSI peripheral required for f
7781

7882
## License
7983

80-
Released under the MIT licence. Copyright (c) 2021, Uri Shaked.
84+
Released under the MIT licence. Copyright (c) 2021-2023, Uri Shaked.

demo/micropython-run.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ import fs from 'fs';
88
import minimist from 'minimist';
99

1010
const args = minimist(process.argv.slice(2), {
11-
string: 'image', // UF2 image to load; defaults to "rp2-pico-20210902-v1.17.uf2"
11+
string: [
12+
'image', // UF2 image to load; defaults to "rp2-pico-20210902-v1.17.uf2"
13+
'expect-text', // Text to expect on the serial console, process will exit with code 0 if found
14+
],
1215
boolean: 'gdb', // start GDB server on 3333
1316
});
17+
const expectText = args['expect-text'];
1418

1519
const mcu = new RP2040();
1620
mcu.loadBootrom(bootromB1);
@@ -37,8 +41,24 @@ cdc.onDeviceConnected = () => {
3741
cdc.sendSerialByte('\r'.charCodeAt(0));
3842
cdc.sendSerialByte('\n'.charCodeAt(0));
3943
};
44+
45+
let currentLine = '';
4046
cdc.onSerialData = (value) => {
4147
process.stdout.write(value);
48+
49+
for (const byte of value) {
50+
const char = String.fromCharCode(byte);
51+
if (char === '\n') {
52+
if (expectText && currentLine.includes(expectText)) {
53+
console.log(`Expected text found: "${expectText}"`);
54+
console.log('TEST PASSED.');
55+
process.exit(0);
56+
}
57+
currentLine = '';
58+
} else {
59+
currentLine += char;
60+
}
61+
}
4262
};
4363

4464
if (process.stdin.isTTY) {

test/micropython/main.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import time
2+
import sys
3+
4+
version = sys.implementation.version
5+
versionstr = "{}.{}.{}".format(version[0], version[1], version[2])
6+
7+
while True:
8+
print("Hello, MicroPython! version: {}".format(versionstr))
9+
time.sleep(1)

test/mklittlefs.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Simple script to create a littlefs image for running the MicroPython test
2+
3+
from littlefs import LittleFS
4+
from os.path import join
5+
6+
files = ['main.py']
7+
source_dir = 'test/micropython'
8+
output_image = 'littlefs.img'
9+
10+
lfs = LittleFS(block_size=4096, block_count=352, prog_size=256)
11+
for filename in files:
12+
with open(join(source_dir, filename), 'r') as src_file, lfs.open(filename, 'w') as lfs_file:
13+
lfs_file.write(src_file.read())
14+
with open(output_image, 'wb') as fh:
15+
fh.write(lfs.context.buffer)
16+
17+
print('Created littlefs image: {}'.format(output_image))

test/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
littlefs-python==0.4.0

0 commit comments

Comments
 (0)