Qemu environment problem for ESP32-C3

habib123
Posts: 3
Joined: Sat Jan 13, 2024 6:04 pm

Qemu environment problem for ESP32-C3

Postby habib123 » Sat Jan 13, 2024 9:35 pm

Hello
First of all, it's my first post here, so welcome everyone.
What I'm trying to do is to prepare an environment for biggier project but instead of flashing all the time, I want to run qemu to test first.
The real/future target is ESP32-C3, custom hardware provided by external party, so no way to change chip type.
IDE is VSCode/P.IO, code using Arduino framework (that's why it's posted here)
So... I've created a simple project

Code: Select all

[env:esp32-c3-devkitm-1-qemu]
platform = espressif32
board = esp32-c3-devkitm-1
framework = arduino
monitor_speed = 115200
upload_speed = 921600
upload_protocol = custom
extra_scripts = qemu_build.py

Code: Select all

#include <Arduino.h>
void setup() {
  Serial.print("setup\n");
}
int cnt = 0;
void loop() {
  Serial.printf("counter=%d\n", cnt++);
  delay(1000);
}
In the qemu_build.py I have custom actions for creating an image and running the qemu

esptool.py --chip esp32c3 merge_bin --flash_mode dio --flash_freq 80m --flash_size 4MB --fill-flash-size 4MB -o qemu_firmware.bin 0x0 bootloader.bin 0x8000 partitions.bin 0xe000 C:\Users\Szefu\.platformio\packages\framework-arduinoespressif32\tools\partitions\boot_app0.bin 0x10000 firmware.bin

qemu-system-riscv32.exe -nographic -no-reboot -machine esp32c3 -icount 3 -drive file=D:\PlatformIO\Projects\qemu_test\.pio\build\esp32-c3-devkitm-1-qemu\qemu_firmware.bin,if=mtd,format=raw

Build log is

Code: Select all

platformio.exe run --environment esp32-c3-devkitm-1-qemu 

Processing esp32-c3-devkitm-1-qemu (platform: espressif32; board: esp32-c3-devkitm-1; framework: arduino)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32-c3-devkitm-1.html
PLATFORM: Espressif 32 (6.5.0) > Espressif ESP32-C3-DevKitM-1
HARDWARE: ESP32C3 160MHz, 320KB RAM, 4MB Flash
DEBUG: Current (cmsis-dap) External (cmsis-dap, esp-bridge, esp-builtin, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)   
PACKAGES:
 - framework-arduinoespressif32 @ 3.20014.231204 (2.0.14)
 - tool-esptoolpy @ 1.40501.0 (4.5.1)
 - tool-openocd-esp32 @ 2.1100.20220706 (11.0)
 - toolchain-riscv32-esp @ 8.4.0+2021r2-patch5
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 33 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio\build\esp32-c3-devkitm-1-qemu\src\main.cpp.o
Building .pio\build\esp32-c3-devkitm-1-qemu\bootloader.bin
Generating partitions .pio\build\esp32-c3-devkitm-1-qemu\partitions.bin
esptool.py v4.5.1
Creating esp32c3 image...
Merged 1 ELF section
Successfully created esp32c3 image.
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\Esp.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\FirmwareMSC.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\FunctionalInterrupt.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\HWCDC.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\HardwareSerial.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\IPAddress.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\IPv6Address.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\MD5Builder.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\Print.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\Stream.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\StreamString.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\Tone.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\USB.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\USBCDC.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\USBMSC.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\WMath.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\WString.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\base64.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\cbuf.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-adc.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-bt.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-cpu.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-dac.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-gpio.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-i2c-slave.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-i2c.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-ledc.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-matrix.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-misc.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-psram.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-rgb-led.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-rmt.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-sigmadelta.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-spi.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-time.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-timer.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-tinyusb.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-touch.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\esp32-hal-uart.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\firmware_msc_fat.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\libb64\cdecode.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\libb64\cencode.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\main.cpp.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\stdlib_noniso.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\wiring_pulse.c.o
Compiling .pio\build\esp32-c3-devkitm-1-qemu\FrameworkArduino\wiring_shift.c.o
C:/Users/szefu/.platformio/packages/framework-arduinoespressif32/cores/esp32/esp32-hal-uart.c: In function 'uartSetPins':
C:/Users/szefu/.platformio/packages/framework-arduinoespressif32/cores/esp32/esp32-hal-uart.c:153:9: warning: 'return' with no value, in function returning non-void
         return;
         ^~~~~~
C:/Users/szefu/.platformio/packages/framework-arduinoespressif32/cores/esp32/esp32-hal-uart.c:149:6: note: declared here
 bool uartSetPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin)
      ^~~~~~~~~~~
Archiving .pio\build\esp32-c3-devkitm-1-qemu\libFrameworkArduino.a
Linking .pio\build\esp32-c3-devkitm-1-qemu\firmware.elf
Retrieving maximum program size .pio\build\esp32-c3-devkitm-1-qemu\firmware.elf
Checking size .pio\build\esp32-c3-devkitm-1-qemu\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   4.2% (used 13676 bytes from 327680 bytes)
Flash: [==        ]  17.3% (used 226262 bytes from 1310720 bytes)
Building .pio\build\esp32-c3-devkitm-1-qemu\firmware.bin
esptool.py v4.5.1
Creating esp32c3 image...
Merged 2 ELF sections
Successfully created esp32c3 image.
post_program_action([".pio\build\esp32-c3-devkitm-1-qemu\firmware.bin"], [".pio\build\esp32-c3-devkitm-1-qemu\firmware.elf"])
>>>>>>>>>>>>>>>>>>>>> executing post_program_action
Using esptool.py --chip esp32c3 merge_bin --flash_mode dio --flash_freq 80m --flash_size 4MB --fill-flash-size 4MB -o qemu_firmware.bin 0x0 bootloader.bin 0x8000 partitions.bin 0xe000 C:\Users\Szefu\.platformio\packages\framework-arduinoespressif32\tools\partitions\boot_app0.bin 0x10000 firmware.bin
esptool.py v4.7.0
Wrote 0x400000 bytes to file qemu_firmware.bin, ready to flash to offset 0x0
========================================================================================= [SUCCESS] Took 10.07 seconds =========================================================================================

Environment              Status    Duration
-----------------------  --------  ------------
esp32-c3-devkitm-1-qemu  SUCCESS   00:00:10.074
========================================================================================= 1 succeeded in 00:00:10.074 ========================================================================================= 
So 4MB qemu_firmware.bin is preapred, then the "fake" upload which runs the qemu...

Code: Select all

platformio.exe run --target upload --environment esp32-c3-devkitm-1-qemu 

Processing esp32-c3-devkitm-1-qemu (platform: espressif32; board: esp32-c3-devkitm-1; framework: arduino)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32-c3-devkitm-1.html
PLATFORM: Espressif 32 (6.5.0) > Espressif ESP32-C3-DevKitM-1
HARDWARE: ESP32C3 160MHz, 320KB RAM, 4MB Flash
DEBUG: Current (cmsis-dap) External (cmsis-dap, esp-bridge, esp-builtin, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES: 
 - framework-arduinoespressif32 @ 3.20014.231204 (2.0.14) 
 - tool-esptoolpy @ 1.40501.0 (4.5.1) 
 - tool-mkfatfs @ 2.0.1 
 - tool-mklittlefs @ 1.203.210628 (2.3) 
 - tool-mkspiffs @ 2.230.0 (2.30) 
 - tool-openocd-esp32 @ 2.1100.20220706 (11.0) 
 - toolchain-riscv32-esp @ 8.4.0+2021r2-patch5
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 33 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Retrieving maximum program size .pio\build\esp32-c3-devkitm-1-qemu\firmware.elf
Checking size .pio\build\esp32-c3-devkitm-1-qemu\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   4.2% (used 13676 bytes from 327680 bytes)
Flash: [==        ]  17.3% (used 226262 bytes from 1310720 bytes)
before_upload(["upload"], [".pio\build\esp32-c3-devkitm-1-qemu\firmware.bin"])
>>>>>>>>>>>>>>>>>>>>> executing before_upload
###=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-###
Starting:  qemu-system-riscv32.exe -nographic -no-reboot -machine esp32c3 -icount 3 -drive file=D:\PlatformIO\Projects\qemu_test\.pio\build\esp32-c3-devkitm-1-qemu\qemu_firmware.bin,if=mtd,format=raw
Adding SPI flash device
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd5810,len:0x438
load:0x403cc710,len:0x90c
load:0x403ce710,len:0x25f4
entry 0x403cc710

assert failed: do_core_init startup.c:328 (flash_ret == ESP_OK)
Core  0 register dump:
MEPC    : 0x40381cf6  RA      : 0x40383ff2  SP      : 0x3fcde190  GP      : 0x3fc8aa00
TP      : 0x00000000  T0      : 0x37363534  T1      : 0x7271706f  T2      : 0x33323130
S0/FP   : 0x3fcde2ff  S1      : 0x00000088  A0      : 0x3fcde1f4  A1      : 0x3fc8b73d
A2      : 0x00000001  A3      : 0x00000029  A4      : 0x00000001  A5      : 0x3fc8d000
A6      : 0x7a797877  A7      : 0x76757473  S2      : 0x3fcde1e8  S3      : 0x00000001
S4      : 0x3fcde1e8  S5      : 0x42008a1c  S6      : 0x42000020  S7      : 0x3c030020
S8      : 0x00010020  S9      : 0x00000000  S10     : 0x00000000  S11     : 0x00000000
T3      : 0x6e6d6c6b  T4      : 0x6a696867  T5      : 0x66656463  T6      : 0x62613938
MSTATUS : 0x00001880  MTVEC   : 0x40380001  MCAUSE  : 0x00000007  MTVAL   : 0x00000000  
MHARTID : 0x00000000

Stack memory:
3fcde190: 0x42000020 0x00000000 0x3c032174 0x40388db0 0xffffffff 0x00000014 0x00000001 0x3fc8b73c
3fcde1b0: 0x00000000 0x3fcde1e4 0x60002000 0x00383233 0x42000020 0x3fc8b824 0x3c032174 0x3fc8b808
3fcde1d0: 0x3c0321d7 0x3fc8b834 0x3fcde1bc 0x3fc8b838 0x3c03222c 0x3fc8b73c 0x00000000 0x00000000
3fcde1f0: 0x3f000000 0x65737361 0x66207472 0x656c6961 0x64203a64 0x6f635f6f 0x695f6572 0x2074696e
3fcde210: 0x72617473 0x2e707574 0x32333a63 0x66282038 0x6873616c 0x7465725f 0x203d3d20 0x5f505345
3fcde230: 0x00294b4f 0x00000000 0x3fc8a684 0x40381686 0x00000000 0x00000000 0x3fc8a664 0x40380ba8
3fcde250: 0x00000054 0x00000000 0x00000005 0x00000108 0x3fc8a638 0x00000000 0x3fc8a664 0x40380f20
3fcde270: 0x00000000 0x00000000 0x00c84016 0x00400000 0x42000020 0x00000000 0x4038192c 0x3fc8cdc0
3fcde290: 0x3fcdf5c0 0x3fc8a664 0x00000000 0x42004582 0x42000020 0x00000000 0x00000000 0x00000001
3fcde2b0: 0x00000000 0x00000001 0x00000000 0x00000000 0x00000000 0x00000000 0x4038192c 0x3fc8cdc0
3fcde2d0: 0x3c032000 0x3c032000 0x3fce0000 0x42008a1c 0x00000001 0x00000140 0x00000101 0x4200cc7a
3fcde2f0: 0x00000001 0x00000000 0x4038192c 0x00020020 0x0000003e 0x00000001 0x00000000 0x40381b1c
3fcde310: 0x693b1752 0x3ff1c14c 0x000000bf 0x00000007 0x00000000 0x3fcd0101 0x00000000 0x7fffffff
3fcde330: 0xffff0000 0x00031fd3 0x00031ff3 0x403cf5b6 0x00010000 0x00140000 0x00000000 0x3fcdf000
3fcde350: 0x00003368 0x3ff1c14c 0x0000ffff 0xffffffff 0x00000000 0x3fcde4b0 0x00000000 0x403cf976
3fcde370: 0x00000000 0x00000000 0x00010000 0x00140000 0x00010000 0x2f0205e9 0x4038192c 0x000000ee
3fcde390: 0x00000005 0x00ffff00 0x01000000 0x3c030020 0x0000c188 0x3fc8a200 0x00001c6c 0x40380000
3fcde3b0: 0x000021f4 0x42000020 0x00021fd4 0x403821f4 0x00007f04 0x00000000 0x00000000 0x00000000
3fcde3d0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcde3f0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcde410: 0x00000000 0x00000000 0x00000000 0x00010020 0x0001c1b0 0x0001de24 0x00020020 0x00041ffc
3fcde430: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcde450: 0x00000000 0x00000000 0x00000000 0x00039f30 0x693b1752 0x0c44eeb0 0xac454093 0x3f006e92
3fcde470: 0x6ef20158 0x7b668979 0x4840204d 0x2f760424 0xffffffff 0xffffffff 0xffffffff 0x3fce0000
3fcde490: 0x00000000 0x0000002b 0x00003370 0x403cc770 0x00000000 0x00000000 0x00000000 0x00000000
3fcde4b0: 0x0000e000 0x00002000 0x00000000 0x00000000 0x00000000 0x00000000 0x00010000 0x00140000
3fcde4d0: 0x00150000 0x00140000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcde4f0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcde510: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcde530: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000002 0x00000000
3fcde550: 0x600c5200 0x600c5200 0x00876713 0x4004a296 0x00000000 0x00000000 0x00000000 0x00000000
3fcde570: 0x00000038 0x00000000 0x00010000 0x2f0203e9 0x403cc710 0x403ce710 0x000025f4 0x000000ee



ELF file SHA256: e512671310877e32

E (84) esp_core_dump_flash: Core dump flash config is corrupted! CRC=0x7bd5c66f instead of 0x0
Rebooting...
Exit code: 0
Qemu has ended
###=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-###
Configuring upload protocol...
AVAILABLE: cmsis-dap, custom, esp-bridge, esp-builtin, esp-prog, espota, esptool, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa     
CURRENT: upload_protocol = custom
Uploading .pio\build\esp32-c3-devkitm-1-qemu\firmware.bin
========================================================================================= [SUCCESS] Took 2.80 seconds =========================================================================================

Environment              Status    Duration
-----------------------  --------  ------------
esp32-c3-devkitm-1-qemu  SUCCESS   00:00:02.801
========================================================================================= 1 succeeded in 00:00:02.801 ========================================================================================= 
Now the error is
assert failed: do_core_init startup.c:328 (flash_ret == ESP_OK)

which I googled already and only found information that this is mismatch on flash size, but the merge_bin and the git hub doc on C3 qemu saying 4MB both, so i don't see any issue here.

I would greatly appreciate any help with this.

lbernstone
Posts: 829
Joined: Mon Jul 22, 2019 3:20 pm

Re: Qemu environment problem for ESP32-C3

Postby lbernstone » Sun Jan 14, 2024 3:16 am

It is trying to init the SPI flash device, and failing. How is the emulator presenting the flash?
And, as a side note, have you tried out https://wokwi.com ?

habib123
Posts: 3
Joined: Sat Jan 13, 2024 6:04 pm

Re: Qemu environment problem for ESP32-C3

Postby habib123 » Mon Jan 15, 2024 5:59 pm

lbernstone wrote:
Sun Jan 14, 2024 3:16 am
It is trying to init the SPI flash device, and failing. How is the emulator presenting the flash?
And, as a side note, have you tried out https://wokwi.com ?
Hi lbernstone
I did some further testing and when i build the project in VSC/P.IO but with ESP-IDF framework it works fine.
Merge and qemu commands are extaclty the same.
So apparently some "spells" are needed for Arduino build and Wokwi knows them.
I didn't find anything about it, so I'll have to go IDF way.

lbernstone
Posts: 829
Joined: Mon Jul 22, 2019 3:20 pm

Re: Qemu environment problem for ESP32-C3

Postby lbernstone » Wed Jan 17, 2024 12:32 pm

If you are working from a command line, building with 'framework = espidf, arduino' may get you the necessary magic.

habib123
Posts: 3
Joined: Sat Jan 13, 2024 6:04 pm

Re: Qemu environment problem for ESP32-C3

Postby habib123 » Wed Jan 17, 2024 5:23 pm

WOW :shock:
I would never think you can mix those.
But now this works with arduino code, big thanks!!!

Who is online

Users browsing this forum: No registered users and 135 guests