Hacking Mynewt in Eclipse

Do you want to hack Apache Mynewt but do not know which IDE to use? Here is a short tutorial on how to configure Eclipse for working with Mynewt.

Using this setup you will have nicely working code indexer, integrated debugger and some other tools. Actually you will probably not need to use console anymore except for project configuration 🙂

Before you start, make sure you have Mynewt environment set properly and Eclipse CDT installed. This tutorial was created with Eclipse Neon (4.6.2), but previous versions should work as well – we also use it with Eclipse Mars without problems.

And final important note: it was tested on develop branch only.

Create project

First, you need to create new project in Eclipse:

  • Go to FileNew and select Makefile Project with Existing Code
  • Enter project name and select location of your project created with Newt tool
  • Select <none> as toolchain
  • Click Finish

Now you should see your project in Project Explorer window.

I recommend to change default build configuration name to one matching target name in your Newt project. This makes working with multiple targets easier later in the process, but you can skip this if you e.g. work with only one target. In this case remember to adjust configuration accordingly (I mention this in the description where applicable). Build configurations names can be changed by going to ProjectBuild ConfigurationsManage…:

Build and index

Now we can setup new project to build and index properly:

  • Go to ProjectProperties
  • Go to C/C++ Build section
  • Open Builder Settings tab
  • Uncheck Use default build command
  • Set Build command to path to newt binary (can be newt only if tool is available in system search path)
  • Open Behavior tab
  • Set Build (incremental build) to build -l debug ${ConfigName}
  • Set Clean to clean ${ConfigName}
  • Click Apply

${ConfigName} will be replaced with the name of current build configuration so this will only work if you named build configurations as I suggested at the beginning. If you decided not to do this, simply replace ${ConfigName} with any valid target of your Newt project.

The -l debug option in the build command is important as it makes Newt print debug logs during build process. These logs also include a complete command line for each command invoked. This is then used by build output parser to figure out build options for each file in the project so indexer can index file properly. The build output parser needs to be configured before it can understand debug logs from Newt:

  • Go to C/C++ GeneralPreprocessor Include Paths, Macros etc. section
  • Open Providers tab
  • Select CDT GCC Build Output Parser provider
  • Set Compiler command pattern to .*\[DEBUG\] arm-none-eabi-gcc
  • Select CDT GCC Built-in Compiler Settings provider
  • In Command to get compiler specs replace ${COMMAND} with arm-none-eabi-gcc

Finally, you may also tune some indexer settings to make sure only files included in the current build are indexed:

  • Go to C/C++ GeneralIndexer section
  • Check Enable project specific settings
  • Uncheck Index source files not included in the build
  • Uncheck Index unused headers
  • Uncheck Allow heuristic resolution of includes
  • Select Use active build configuration (this actually makes difference only if you use multiple build configurations, but it is worth checking it anyway)

And that’s all. You can build and clean (…and build and clean…) your project as any other project in Eclipse. If everything was configured properly, you should see that .c files included in the build have custom settings captured from the build output parser and this means project should be nicely indexed:

No more errors due to unresolved stuff in code 🙂

Debug

Once you can write and build your code, it is time to debug. Start with creating new debug configuration for your project:

  • Go to RunDebug Configurations…
  • Create new C/C++ Remote Application
  • Click Select other… link at the bottom to switch to manual GDB launcher
  • In the C/C++ Application you need to specify .elf for your application (which should be already filled in if you built your project already). However, you may want to make this one a bit more generic and let Eclipse figure out application binary for current build configuration (i.e. current target). In this case set it to bin/targets/${config_name:${project_name}}/app/apps/${config_name:${project_name}}/${config_name:${project_name}}.elf (yes, this is a single line of text 🙂 ). Note that this will only work only if you have build configuration name the same as target name in your Newt project and also the same as application name built by this target.
  • Select Disable auto build (optional, but soon you will see that having auto build enabled is not necessary)
  • Open Debugger tab
  • On Main sub-tab set GDB Debugger to arm-none-eabi-gdb
  • On Connection sub-tab set Port number to 3333

Before starting debug session in Eclipse you need to run GDB Server for selected target in shell console. This can be done in two ways:

  • newt run -n <target> 0 will build and load new image to your device, then start GDB Server
  • newt debug -n <target> will just start GDB Server

Note the -n parameter in both cases – this will stop Newt from running GDB in shell console.

Click Debug now and you will be switched to debug session. You may now want to reset your device to start debugging from beginning. To do this use Debugger Console window and execute interrupt, monitor reset and then continue (or just suspend from UI, execute monitor reset and then continue form UI).

To make things even easier, it is possible to launch both newt run and debug session with a single click in Eclipse – this way you do not need to switch to shell console every time you want to start debugging. Start by creating launcher for newt run:

  • Go to RunExternal ToolsExternal Tools Configurations…
  • Create new program
  • Set Location to path to newt binary
  • Set Working Directory to location of your project, e.g. ${workspace_loc:/mynewt} (or use Browse Workspace… button)
  • Set Arguments to run -n ${config_name:${project_name}} 0 (enter valid target name instead of ${config_name:${project_name}} if not using build configurations)
  • On Build tab uncheck Build before lauch option

Now just need to define launch group which runs both newt run external tool and debug configuration:

  • Go to RunDebug Configurations…
  • Create new launch group
  • Use Add… button to add two launch configurations
    • Run newt run external tool
    • Run debug configuration

Your final setup should look as on image below:

Now just use this launch group instead of plain debug configuration and you will not need to open shell when debugging Mynewt anymore 🙂

Extras

You can also use external tools to integrate more Newt goodies with Eclipse. For example, I have a separate launch group which only loads image onto target. It uses two external tools configured to launch newt create-image <target> 0 and newt load <target>. The process is exactly the same as for debug launch group so I will not describe it here in details again. The only thing to remember is to select proper action as Post launch action since both tools need to be run in sequence:

The final configuration looks like this:

And that is all… at least for now 😉