diff --git a/docs/emulation.md b/docs/emulation.md new file mode 100644 index 00000000..0a920bae --- /dev/null +++ b/docs/emulation.md @@ -0,0 +1,12 @@ +# Emulation (development) + +TODO: write this + +Build the program using `env:emulation`, this is required to re-route `Serial.print` to qemu's stdout. + +To run it: + +```sh +cd scripts/emulation +docker compose up +``` diff --git a/platformio.ini b/platformio.ini index 7f42637d..b99d8690 100644 --- a/platformio.ini +++ b/platformio.ini @@ -20,7 +20,6 @@ board_upload.offset_address = 0x10000 build_flags = -DARDUINO_USB_MODE=1 - -DARDUINO_USB_CDC_ON_BOOT=1 -DMINIZ_NO_ZLIB_COMPATIBLE_NAMES=1 -DEINK_DISPLAY_SINGLE_BUFFER_MODE=1 -DDISABLE_FS_H_WARNING=1 @@ -54,9 +53,21 @@ extends = base build_flags = ${base.build_flags} -DCROSSPOINT_VERSION=\"${crosspoint.version}-dev\" + -DCROSSPOINT_EMULATED=0 + -DARDUINO_USB_CDC_ON_BOOT=1 + +[env:emulation] +extends = base +build_flags = + ${base.build_flags} + -DCROSSPOINT_VERSION=\"${crosspoint.version}-emu\" + -DCROSSPOINT_EMULATED=1 + -DARDUINO_USB_CDC_ON_BOOT=0 # Disable USB CDC on boot for Serial output via UART [env:gh_release] extends = base build_flags = ${base.build_flags} -DCROSSPOINT_VERSION=\"${crosspoint.version}\" + -DCROSSPOINT_EMULATED=0 + -DARDUINO_USB_CDC_ON_BOOT=1 diff --git a/scripts/emulation/Dockerfile b/scripts/emulation/Dockerfile new file mode 100644 index 00000000..9439c9f2 --- /dev/null +++ b/scripts/emulation/Dockerfile @@ -0,0 +1,25 @@ +FROM debian:12 + +WORKDIR /root + +ARG DEBIAN_FRONTEND=noninteractive + +# Update container +RUN apt-get update && apt-get upgrade -y + +# Install dependencies +RUN apt-get install git wget flex bison gperf python3 python3-pip python3-setuptools python3-venv cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0 \ + libgcrypt20 libglib2.0-0 libpixman-1-0 libsdl2-2.0-0 libslirp0 -y + +# Install ESP-IDF +ENV PATH="~/.local/bin:${PATH}" +RUN mkdir -p ~/esp && cd ~/esp && git clone --quiet --recursive https://github.com/espressif/esp-idf.git && ln -s /usr/bin/python3 /usr/bin/python +RUN cd ~/esp/esp-idf && export IDF_GITHUB_ASSETS="dl.espressif.com/github_assets" && ./install.sh && /bin/bash -c "source ./export.sh" + +# Install qemu +RUN cd ~/esp/esp-idf && python tools/idf_tools.py install qemu-riscv32 + +RUN echo "alias idf='source /root/esp/esp-idf/export.sh'" >> .bashrc + +# Start from a Bash prompt +CMD [ "/bin/bash" ] diff --git a/scripts/emulation/docker-compose.yml b/scripts/emulation/docker-compose.yml new file mode 100644 index 00000000..ffc7489d --- /dev/null +++ b/scripts/emulation/docker-compose.yml @@ -0,0 +1,33 @@ +services: + crosspoint-emulator: + build: . + container_name: crosspoint-emulator + volumes: + - ../..:/app:ro + tmpfs: + - /tmp + ports: + - "8080:80" + stdin_open: true + tty: true + stop_signal: SIGKILL + entrypoint: + - /bin/bash + - -c + - | + set -x; + source /root/esp/esp-idf/export.sh + + cd /tmp + + cp /app/.pio/build/emulation/*.bin . + dd if=/dev/zero bs=1M count=16 of=./flash.bin + dd if="bootloader.bin" bs=1 count=$(stat -c%s "bootloader.bin") seek=0 conv=notrunc of=./flash.bin + dd if="partitions.bin" bs=1 count=$(stat -c%s "partitions.bin") seek=$((16#8000)) conv=notrunc of=./flash.bin + dd if="firmware.bin" bs=1 count=$(stat -c%s "firmware.bin") seek=$((16#10000)) conv=notrunc of=./flash.bin + + qemu-system-riscv32 \ + -nographic \ + -M esp32c3 \ + -drive \ + file=flash.bin,if=mtd,format=raw diff --git a/src/main.cpp b/src/main.cpp index c0222e0d..f848c8a2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -278,7 +278,7 @@ void setupDisplayAndFonts() { bool isUsbConnected() { // U0RXD/GPIO20 reads HIGH when USB is connected - return digitalRead(UART0_RXD) == HIGH; + return CROSSPOINT_EMULATED || digitalRead(UART0_RXD) == HIGH; } bool isWakeupAfterFlashing() {