wokwi / rp2040js

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

How to read from serial #140

Closed ArduinoManager closed 4 months ago

ArduinoManager commented 4 months ago

I'm try to simulate a very simple program:

#include <stdio.h>
#include "pico/stdlib.h"

int led_state = 0;

int main()
{
    const uint LED_PIN = PICO_DEFAULT_LED_PIN;

    gpio_init(LED_PIN);
    gpio_set_dir(LED_PIN, GPIO_OUT);
    stdio_init_all();

    while (1)
    {
        printf("Enter 1 to turn the LED on, 0 to turn it off\n");
        led_state = getchar();
        led_state -= '0';
        gpio_put(LED_PIN, led_state);
        sleep_ms(100);
    }
}

I was able to figure out how to show the serial output.

const hex = fs.readFileSync('Test2.hex', 'utf-8');
const mcu = new RP2040();
mcu.loadBootrom(bootromB1);
loadHex(hex, mcu.flash, 0x10000000);

mcu.uart[0].onByte = (value) => {
  process.stdout.write(new Uint8Array([value]));
};

mcu.gpio[25].addListener ( (value, lastValue) => {
    process.stdout.write("--- PIN 25 ---")
    process.stdout.write("Changed from "+lastValue+" to "+value)
  }
)

mcu.core.PC = 0x10000000;
mcu.execute();

How do I read from the serial?

urish commented 4 months ago

You mean how to send data to the serial port?

mcu.uart[0].feedByte(value);