How to simulate serial port on debugger simulator?

I’m debugging Arduino AT2560 without JTAG, but with default Platformio simulator.
I don’t know how to emulate a serial port to read serial output or write some serial input.

Any hint?

thanks

The underlying simulator is simavr and externally developed. Check out its manual.

Also checkout related topics Atmel AVR dev/platform v2.1.0: new boards, AVR simulator, updated Arduino cores - #8 by Sambo007 and especially AVR simulator as aid tool for debugging code - #35 by msquirogac.

Note that you can also start simavr manually the firmware and any switches you like.

E.g., a firmware like

#include <Arduino.h>

void setup() {
  Serial.begin(115200);
}

uint16_t count = 0;

void loop() {
  Serial.println("The count is: " + String(count++));
  delay(200);
}

with a platformio.ini

[env:uno]
platform = atmelavr
board = uno
framework = arduino
debug_tool = simavr

after doing a pio debug to build the binary, we can start simavr in a new Powershell session

PS C:\Users\Maxi\.platformio\packages\tool-simavr\bin> .\simavr.exe -m atmega328p -f 16000000  "C:\Users\Maxi\Documents\PlatformIO\Projects\arduino-simavr-test\.pio\build\uno\firmware.elf"   | Out-Host

to get

1 Like

Hi
@maxgerhardt

I am using Windows 7 32 bit, with VSCode 1.57 and PlatformIO 2.3.2.
I tried your above example but I get this error in Powershell.

Program ‘simavr.exe’ failed to execute: This version of %1 is not compatible with the version of Windows you’re running …

I think simavr.exe file is for 64 bit version.

How can I get simavr package for 32 bit version. If it is available or not?
If not, how can I simulate?

Thanks

You are still on a 32-bit operating system? o_O

Well anyways, report this as a bug to Issues · platformio/platform-atmelavr · GitHub, the package information for tool-simavr should have said x64 Windows only and should have prevented you from getting the program in the first place.

You can try and build from source. The repo GitHub - buserror/simavr: simavr is a lean, mean and hackable AVR simulator for linux & OSX has instructions for MinGW (you should ofc choose MinGW32 version)

With some modifications I managed to compile the latest simavr with MinGW for Windows (64-bit). See instructions at GitHub - maxgerhardt/simavr: simavr is a lean, mean and hackable AVR simulator for linux & OSX, you should be able to do it for 32-bit too.

Hi
@maxgerhardt
Yes, I am using 32 bit windows and I am comfortable with it.
Question is, when PaltformIO is available on 32 bit why simavr installed by PlatformIO is for 64 bit. Rather I should say copied(simavr).

I think, when PlatformIO is installed, it should be considered.

I am a programmer and using High Level Languages with RDBMS. But I am very much new to Microcontrollers, so I would need ready made solutions. I have little knowledge in C/C++ and it is very much difficult for me to get the source code and compile.

Anyways, thanks for your reply and help.

As said above, the package metainfo (https://api.registry.platformio.org/v3/packages/platformio/tool/tool-simavr) is wrongly marked as

"system":["windows_amd64","windows_x86","windows"]

when the compiled binary actually isn’t.

Alright then, if you can’t compile it, I’ll try for you with the instructions I’ve linked above.

Thanks for your quick reply sir.
If possible, please provide me compiled 32 bit simavr

After running into some issues ([avr-gcc] mingw-w64-i686-avr-toolchain fails to install: Maximum file size exceeded · Issue #2585 · msys2/MSYS2-packages · GitHub) and solving them, I was able to compile simavr in 32-bit mode.

See https://drive.google.com/file/d/1vCoSAP9eQygr-IpVIaGYS-1yaE0eJW2h/view?usp=sharing for a download. You should grab the simavr.exe from it and put it in C:\Users\<user>\.platformio\packages\tool-simavr\bin\, replacing the old one. The zip also contains a firmware.hex test firmware.

>simavr --mcu atmega328p --freq 16000000 -v firmware.hex
Loaded 1 section of ihex
Load HEX flash 00000000, 1636
Hello, world!..
Hello, world!..
Hello, world!..

(Ctrl+C to abort)

Issue is open in tool-simavr does not run on 32-bit Windows although advertised · Issue #258 · platformio/platform-atmelavr · GitHub btw.

Thanks very very much. It is running perfect. Appreciate your sense and tenancy to help every one.

@maxgerhardt
I have implemented some codes in Arduino and these are working as expected.

I want to implement the same outputs (like Serial.println) in C code for Atmega328p microcontroller to see how my code is working.

I have gone through the HardwareSerial.cpp for Arduino Code. As per my knowledge, the code is implementing UART.

How it will be possible for Atmega328p with C Code.

There are dozens of tutorials for this that just use the compiler’s built in avr/io.h header for this.

To start a project without the Arduino framework compiled in, you need to remove the framework = .. line in the platformio.ini to make it a baremetal project. See e.g. native-blink.