Skip to content

Commit 363f701

Browse files
authored
feat: simulate a given UF2 firmware using npm start (#143)
The existing implementtaion of `start` does not accept a path on the command line while it has the feature of relaying UART0 to the console. Add the command line parameter to specify an arbitrary image, also accept UF2 file, besides hex, preserve backward compat. As a result, images that send output to UART0 can be benefit from that. Co-authored-by: kromych <[email protected]>
1 parent dc5a8ff commit 363f701

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ npm install
2828
npm start
2929
```
3030

31+
You can also specify the path to the image on the command line and/or load an UF2 image:
32+
33+
```sh
34+
npm run start -- --image ./my-pico-project.uf2
35+
```
36+
37+
A GDB server will be available on port 3333, and the data written to UART0 will be printed
38+
to the console.
39+
3140
### MicroPython code
3241

3342
To run the MicroPython demo, first download [RPI_PICO-20230426-v1.20.0.uf2](https://micropython.org/resources/firmware/RPI_PICO-20230426-v1.20.0.uf2), place it in the rp2040js root directory, then run:

demo/emulator-run.ts

+27-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,37 @@ import { GDBTCPServer } from '../src/gdb/gdb-tcp-server.js';
33
import { Simulator } from '../src/simulator.js';
44
import { bootromB1 } from './bootrom.js';
55
import { loadHex } from './intelhex.js';
6+
import { loadUF2 } from './load-flash.js';
7+
import minimist from 'minimist';
8+
9+
const args = minimist(process.argv.slice(2), {
10+
string: [
11+
'image', // An image to load, hex and UF2 are supported
12+
],
13+
});
614

7-
// Create an array with the compiled code of blink
8-
// Execute the instructions from this array, one by one.
9-
const hex = fs.readFileSync('hello_uart.hex', 'utf-8');
1015
const simulator = new Simulator();
1116
const mcu = simulator.rp2040;
1217
mcu.loadBootrom(bootromB1);
13-
loadHex(hex, mcu.flash, 0x10000000);
18+
19+
const imageName = args.image ?? 'hello_uart.hex'
20+
21+
// Check the extension of the file
22+
const extension = imageName.split('.').pop();
23+
if (extension === 'hex') {
24+
// Create an array with the compiled code of blink
25+
// Execute the instructions from this array, one by one.
26+
const hex = fs.readFileSync(imageName, 'utf-8');
27+
28+
console.log(`Loading hex image ${imageName}`);
29+
loadHex(hex, mcu.flash, 0x10000000);
30+
} else if (extension === 'uf2') {
31+
console.log(`Loading uf2 image ${imageName}`);
32+
loadUF2(imageName, mcu);
33+
} else {
34+
console.log(`Unsupported file type: ${extension}`);
35+
process.exit(1);
36+
}
1437

1538
const gdbServer = new GDBTCPServer(simulator, 3333);
1639
console.log(`RP2040 GDB Server ready! Listening on port ${gdbServer.port}`);

0 commit comments

Comments
 (0)