Skip to content

Running the firmware in a simulator

esspe2 edited this page May 19, 2019 · 4 revisions

Before we begin

Heads up

Please note that this is an work in progress, aimed at slightly more advanced users. I'm working to wrap everything in convenient scripts, but it's not quite there yet.

If you want to try it out, I'd love to get feedback and hear your experience and suggestions.

Just bring an extra dose of patience and adventurous spirit :)

You already have a real Maslow, why would you need a simulator?

When developing and testing firmware, you can get a spare Arduino and run experimental versions on it. Running it in a simulator offers several advantages, though:

  • No need to buy that spare Arduino.
  • Tests (once you write them :) can run in a CI environment, ease testing, and prevent regressions
  • Arduinos are between hard and impossible to debug, but the simulator interfaces directly with GDB and most GUI wrappers.

Here's a teaser pic maslow_in_gdbgui

Want to try it out? Here's how

Install simavr

From the Simavr docs: On Mac OS, homebrew is the easiest route:

brew tap osx-cross/avr
brew install --HEAD simavr

On Linux, you'll need a few extra steps.

First, install some dev pacakges. On my Ubuntu MinimalCD setup, this meant running: sudo apt install build-essential git make gcc-avr gcc libelf-dev picocom avr-libc freeglut3-dev pkg-config gdb-avr

Now clone the Simavr repo:

git clone https://github.com/buserror/simavr.git

And install:

make install RELEASE=1

Install PlatformIO

If you don't already have a favorite IDE for developing Maslow, I recommend checking out PlatfomIO IDE. If you're not on Mac OS, I think this is also the easiest way to get the command line tools.

Install the IDE, then follow these instructions to get the command line tools in your PATH.

If you just want the minimal command line build tools on Mac OS:

brew install platformio

If you just want the minimal command line build tools on Linux, I think this will work for you:

apt-get python2.7 python-pip
pip install platformio

Build simduino

The simduino example that comes with simavr has a few issues, so we have our own variant. Let's build it:

cd simduino
make

Alright, let's do some simulatin'!

Build and run the simulator for the simavr variant of the firmware:

cd ..
platformio run -t simulate -e simavr

Once this is running, you can open another terminal window and talk to it directly over the serial port:

platformio device monitor -e simavr

Or fire up Ground Controler, set the serial port to /tmp/simavr-uart0, and... well... let me know if you figured out how to get GC to talk with the simluator properly, cause I haven't quite.

Debugging

I haven't wrapped this yet with a neat little PlatformIO command, but if you sort of know what you're doing, this might get you going.

In terminal window 1, run the simulator in debug mode:

./simduino/simduino .pioenvs/simavr/firmware.elf -d

In terminal window 2, run the serial monitor:

platformio device monitor -e simavr

In terminal window 3, run the debugger:

avr-gdb .pioenvs/simavr/firmware.elf

Then tell gdb to connect to the simduino process:

target remote localhost:1234

Note: The firmware will run slower than normal under the debugger. Way slower. Also note: If you're a gdb novice like me, check out gdbgui.

Testing

This is just a placeholder/teaset for now. I hope that we can get this thing working smoothly, then add automated unit tests to the code, which will run in a CI environment.