2

So I wrote this code for Arduino Uno:

// file qemu.ino
void setup() {
    Serial.begin(9600);
}

void loop() {
    Serial.println(millis());
}

And then compiled and uploaded it to the real microcontoller. Everything works as it should, printing milliseconds elapsed. The millis() value updates regularly.

Then, I tried to execute the same code in QEMU by using these commands:

arduino-cli compile -b arduino:avr:uno -e .
qemu-system-avr -M uno -nographic -bios build/arduino.avr.uno/qemu.ino.with_bootloader.bin

Serial prints work, but millis() stays at 0 all the time. So just a bunch of 0s getting printed.

Further investigation of Arduino source code revealed the problem: millis() reads the variable timer0_millis, which gets updated in ISR(TIMER0_OVF_vect), that is, in the 16th interrupt vector. The init() function also makes sure that this interrupt is enabled.

Also because millis() never gets updated, delay() and Serial.read() hang forever.

So the conclusion is: interrupts work on a real device, but for some reason not on QEMU. Maybe I am missing something?

2
  • 1
    The documentation of qemu-system-avr says “[...] ATmega controller, which model is limited to USART & 16-bit timer devices”. Not sure I understand that sentence. It could mean the emulator does not support the 8-bit Timer 0, which is used by the Arduino timing functions like millis(). Commented Apr 6 at 15:55
  • 1
    you came to the correct conclusion
    – jsotola
    Commented Apr 6 at 19:24

1 Answer 1

1

The documentation of qemu-system-avr says:

[...] ATmega controller, which model is limited to USART & 16-bit timer devices, enough to run FreeRTOS based applications [...]

It means the emulator does not support the 8-bit timer 0, which is used by the Arduino timing functions like millis().

3
  • That's weird since Arduino is almost unusable without timer0. Commented Apr 7 at 16:39
  • @AlexanderPerechnev You did not read the linked documentation, did you? :-D It clearly states that this suffices to run FreeRTOS (added to the answer now). Some Arduinos are apparently used outside the Arduino firmware environment. Not everyone is happy with the limits of it. Commented Apr 7 at 16:53
  • No, I didn't. I opened this topic just because it's interesting that Qemu can emulate AVR-based devices as well. Commented Apr 7 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.