Skip to content

janjongboom/mbed-simulator

Repository files navigation

New repository

I have left Arm (and started Edge Impulse!) and Arm has taken over development of the simulator. See ArmMbed/mbed-simulator for more information.

Experimental simulator for Mbed OS 5 applications

Demo: https://labs.mbed.com/simulator

Screenshot

While we have worked hard to improve embedded development tooling in Mbed (e.g. via the Online Compiler), the development for microcontrollers is still very similar to how it was in the 90s. Compilation is slow, and flashing is even slower. When fixing a bug, you need to get the device into the exact state as before encountering the bug. This makes for a very slow feedback loop, which hinders productivity and often pulls you out of the zone.

To make this feedback loop much shorter, we're releasing an alpha version of the Mbed Simulator. The simulator allows you to run your Mbed OS 5 applications directly on your computer, so that you can quickly test and verify applications without flashing them on a real board. This is a valuable learning tool, as you quickly learn how Mbed works. It is also very useful for developing complex applications. Within Arm, we have been using the simulator for work on mbed-http, the Mbed LoRaWAN stack and uTensor.

Note: The Mbed Simulator is part of Mbed Labs. The Mbed Labs projects showcase interesting side projects developed by Mbed engineers. However, these projects are not actively supported by Arm, and may be added, removed or break at any time.

More information in the introductionary blog post

Docs

Installation

Prerequisites

  1. Install Mbed CLI.
  2. Install Python 2.7 - not Python 3!.
  3. Install Git.
  4. Install Mercurial.
  5. Install Node.js v8 or higher.

Make sure that all of these are in your PATH. Verify this by opening a command prompt or terminal, and running:

$ where mbed
C:\Python27\Scripts\mbed.exe

$ where node
C:\Program Files\nodejs2\node.exe

$ where git
C:\Program Files\Git\cmd\git.exe

$ where hg
C:\Program Files\TortoiseHg\hg.exe

On Linux and macOS use which instead of where.

If one of the where / which commands does not yield a path, the utility is not in your PATH.

Installing Emscripten

To install the Emscripten cross-compilation toolchain, open a command prompt and:

  1. Clone the repository and install SDK version 1.38.21:

    $ git clone https://github.com/emscripten-core/emsdk.git
    $ cd emsdk
    $ emsdk install emscripten-1.38.21
    $ emsdk install sdk-fastcomp-tag-1.38.21-64bit
    $ emsdk activate emscripten-1.38.21
    $ emsdk activate fastcomp-clang-tag-e1.38.21-64bit
    
    # on Windows only:
    $ emsdk_env.bat --global
    
  2. Verify that the installation was successful:

    $ emcc -v
    emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 1.38.21
    
  3. Find the folder where emcc was installed:

    Windows

    $ where emcc
    C:\simulator\emsdk\emscripten\1.38.21\emcc
    

    macOS and Linux

    $ which emcc
    ~/toolchains/emsdk/emscripten/1.38.21/emcc
    
  4. Add this folder to your PATH.

    • On Windows:

      • Go to System Properties > Advanced > Environmental variables.
      • Find PATH.
      • Add the folder you found in the previous step, and add it prefixed by ;. E.g.: ;C:\simulator\emsdk\emscripten\1.38.21\
    • On macOS / Linux:

      • Open ~/.bash_profile or ~/.bashrc and add:
      PATH=$PATH:~/toolchains/emsdk/emscripten/1.38.21
      
  5. Open a new command prompt and verify that emcc can still be found by running:

    $ where emcc
    C:\simulator\emsdk\emscripten\1.38.21\emcc
    
  6. All set!

Installing the simulator through npm

Last, install the simulator. Easiest is through npm:

  1. Install the simulator:

    $ npm install mbed-simulator -g
    
  2. Clone an Mbed OS example program:

    $ mbed import mbed-os-example-blinky
    $ cd mbed-os-example-blinky
    
  3. Run the simulator:

    $ mbed-simulator .
    

    Note that this will download all dependencies (including Mbed OS) and will build the common libmbed library so this'll take some time.

Installing the simulator from source

  1. Install the simulator through git:

    $ git clone https://github.com/janjongboom/mbed-simulator.git
    $ cd mbed-simulator
    $ npm install
    $ npm install . -g
    
  2. Build your first example:

    $ node cli.js -i demos\blinky -o out --launch
    

    Note that this will download all dependencies (including Mbed OS) and will build the common libmbed library so this'll take some time.

  3. Done! The Mbed Simulator should now launch in your default browser.

Troubleshooting

Windows: [Error 87] The parameter is incorrect

This error is thrown on Windows systems when the path length limit is hit. Move the mbed-simulator folder to a folder closer to root (e.g. C:\mbed-simulator).

How to run the hosted version

  1. Install all dependencies, and clone the repository from source (see above).

  2. Run:

    $ npm install
    
    # Windows
    $ build-demos.bat
    
    # macOS / Linux
    $ sh build-demos.sh
    
  3. Then, start a web server:

    $ node server.js
    
  4. Open http://localhost:7829 in your browser.

  5. Blinky runs!

CLI

The simulator comes with a CLI to run any Mbed OS 5 project under the simulator.

Running

To run an Mbed OS 5 project:

$ mbed-simulator .

The project will build and a web browser window will open for you.

To see if your program runs in the simulator, check the TARGET_SIMULATOR macro.

Running in headless mode

You can also run the simulator in headless mode, which is great for automated testing. All output (through printf and traces) will be routed to your terminal. To run in headless mode, add the --launch-headless option. You might also want to limit the amount of logging the server does through --disable-runtime-logs to keep the output clean.

Changing mbed-simulator-hal

After changing anything in the simulator HAL, you need to recompile the libmbed library:

  1. Run:

    $ rm mbed-simulator-hal/libmbed.bc
    
  2. Rebuild your application. libmbed will automatically be generated.

Updating demo's

In the out folder a number of pre-built demos are listed. To upgrade them:

macOS and Linux

$ sh build-demos.sh

Windows

$ build-demos.bat

Attribution

About

Experimental simulator for Mbed OS 5 applications

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •