wokwi / rp2040js

A Raspberry Pi Pico Emulator in JavaScript
MIT License
384 stars 40 forks source link

How to do serial over USB with `demo/emulator-run.ts`? #133

Open pguyot opened 11 months ago

pguyot commented 11 months ago

First, let me write this is very cool. I'd like to use @mingpepe 's core1 branch to run CI tests for AtomVM which extensively uses core1.

I got AtomVM's hello_pico sample to work but I had to alter compilation flags to use uart instead of usb for serial.

The demo/emulator-run.ts script works well with hello_uart.hex, but I can't seem to get it working with hello_usb.hex. I tried to adapt the code from demo/micropython-run.ts, but failed: nothing is being printed. What am I missing?

Also as a side note, why is demo/emulator-run.ts using a hex file instead of uf2 file(s)?

diff --git a/demo/emulator-run.ts b/demo/emulator-run.ts
index e18cc9f..c268dd8 100644
--- a/demo/emulator-run.ts
+++ b/demo/emulator-run.ts
@@ -3,10 +3,11 @@ import { RP2040 } from '../src';
 import { bootromB1 } from './bootrom';
 import { loadHex } from './intelhex';
 import { GDBTCPServer } from '../src/gdb/gdb-tcp-server';
+import { USBCDC } from '../src/usb/cdc';

 // Create an array with the compiled code of blink
 // Execute the instructions from this array, one by one.
-const hex = fs.readFileSync('hello_uart.hex', 'utf-8');
+const hex = fs.readFileSync('hello_usb.hex', 'utf-8');
 const mcu = new RP2040();
 mcu.loadBootrom(bootromB1);
 loadHex(hex, mcu.flash, 0x10000000);
@@ -18,5 +19,10 @@ mcu.uart[0].onByte = (value) => {
   process.stdout.write(new Uint8Array([value]));
 };

+const cdc = new USBCDC(mcu.usbCtrl);
+cdc.onSerialData = (value) => {
+  process.stdout.write(value);
+};
+
 mcu.core.PC = 0x10000000;
 mcu.execute();
coffeemakingtoaster commented 7 months ago

Hey I just came across this. I don´t know if this solves your issue as well but I had to downgrade to the pico sdk 1.4.0 to get USB output working.

So my code looked like this in the end. Mind you: The sendByte calls are just implementation specific. They are used to ensure that the USB device will receive the output of the program running as the emulator tends to start running the compiled program before the USB device is connected and ready.