lucasw / arduino_qemu_test

Project wombat
1 stars 0 forks source link

Run arduino mega 2560 elfs in qemu #1

Open lucasw opened 3 years ago

lucasw commented 3 years ago

Use qemu 5.2.0 or later

https://www.qemu.org/

Install ninja

sudo apt install ninja-build
mkdir build_qemu_5.2.0
cd build_qemu_5.2.0
../configure --prefix=$HOME/other/install --target-list=avr-softmmu,arm-softmmu
make

(arm-softmmu also of interest, add it to the target list, comma no spaces)

2557 files to build including arm-softmmu, 1931 without.

lucasw commented 3 years ago
qemu-system-avr -machine help
Supported machines are:
2009                 Arduino Duemilanove (ATmega168) (alias of arduino-duemilanove)
arduino-duemilanove  Arduino Duemilanove (ATmega168)
mega2560             Arduino Mega 2560 (ATmega2560) (alias of arduino-mega-2560-v3)
arduino-mega-2560-v3 Arduino Mega 2560 (ATmega2560)
mega                 Arduino Mega (ATmega1280) (alias of arduino-mega)
arduino-mega         Arduino Mega (ATmega1280)
uno                  Arduino UNO (ATmega328P) (alias of arduino-uno)
arduino-uno          Arduino UNO (ATmega328P)
none                 empty machine
lucasw commented 3 years ago

Now run the arduino ide, build a sample project.

ide doesn't respect the 200% scaling, it's tiny.

https://github.com/arduino/Arduino/issues/6472

built files go into /tmp/build1703114282366706709.tmp and similar.

qemu-system-avr -machine mega -bios DigitalReadSerial.cpp.elf 

A new window opens, can set it to serial0 and see a single loop of output from the example code that I added:

FOO

but the delay doesn't work, or the system is stopped?

Try out -serial dev and use gtkterm.

Figure out how to build arduino project from command line.

lucasw commented 3 years ago
qemu-system-avr -M mega2560 -bios test.elf -nographic -serial tcp::5678,server=on,wait=off
telnet localhost 5678

https://qemu.readthedocs.io/en/latest/system/target-avr.html

But want to leave serial monitor running in another window, automatically reconnect when server goes up and down

Thought udp would work, but locks up qemu sometimes, doesn't like recycling the server

qemu-system-avr -M mega2560 -bios test.elf -nographic -serial udp::5678
nc -u -l -p 5678
lucasw commented 3 years ago

-S to pause on start, c to continue, stop to pause again

lucasw commented 3 years ago

The arduino delay() locks up, while delayMicroseconds() may not be delaying at all, but util/delay.h + delay_ms() runs ~10x faster than real time

#include <util/delay.h>

_delay_ms(1000);

(also millis() just returns 0- some clock on the arduino isn't getting emulated?)

lucasw commented 3 years ago

Want to slow down the emulator, cpulimit looks to work:

cpulimit -l 2 -- qemu-system-avr -M mega2560 -bios test.elf -nographic -serial tcp::5678,server=on,wait=off

But this crashes hard on typing exit into qemu, the terminal tab goes down.

May need to free up the port if cycling rapidly:

fuser -k 5678/tcp

Actually can do ctrl-c instead of quit, then fuser will close the process that is using that port, presserving the terminal.