Create a New Project

This page makes the following assumptions:

  1. You have already installed the newt and newtmgr tools on your system (see the Native Installation pages in this learning guide for details)
  2. The various build tools (arm-none-eabi-gcc, etc.) are available from the command-line
  3. You are using Mynewt version 1.1 for the tools and core codebase

This project will have the following features:

  • newtmgr support will be enabled over the serial port
  • Shell support will be included, allowing you to enter commands over the serial connection

Create a Project Skeleton

The first thing to do is create a project skeleton via the following command:

We'll use 'myproject' as a project name here, but you are free to change it to something else
$ newt new myproject

This should give you the following output:

Downloading project skeleton from apache/mynewt-blinky...
Installing skeleton in myproject...
Project myproject successfully created.

Download Project Dependencies

Next download the project dependencies from the associated repos.

Since this is a new project, only apache-mynewt-core will be downloaded:

$ cd myproject
$ newt install -v

Depending on your system state, this should give you the following output:

$ newt install -v
[apache-mynewt-core]:
Downloading repository description
Download successful!
Downloading repository mynewt-core (branch: master; commit: mynewt_1_1_0_tag) at https://github.com/apache/mynewt-core.git
Cloning into '/var/folders/86/hb2vp14n5_5_yvdz_z8w9x_c0000gn/T/newt-repo021712424'...
remote: Counting objects: 65349, done.
remote: Compressing objects: 100% (151/151), done.
remote: Total 65349 (delta 130), reused 162 (delta 95), pack-reused 65099
Receiving objects: 100% (65349/65349), 80.03 MiB | 1.94 MiB/s, done.
Resolving deltas: 100% (39656/39656), done.
Will create new branch mynewt_1_1_0_tag from tag tags/mynewt_1_1_0_tag
apache-mynewt-core successfully installed version 1.1.0-none

This should give you the following project structure:

.
├── LICENSE
├── NOTICE
├── README.md
├── apps
│   └── blinky
│       ├── pkg.yml
│       └── src
├── project.state
├── project.yml
├── repos
│   └── apache-mynewt-core
└── targets
    ├── my_blinky_sim
    │   ├── pkg.yml
    │   └── target.yml
    └── unittest
        ├── pkg.yml
        └── target.yml

Create a New Application

Now that you have a project skeleton, you can start adding applications to it.

A Mynewt app requires at least a main() function, a pkg.yml file, and a most of the time a syscfg.yml file, as well as a new 'app' folder where the files should be stored.

To get started, create a new app folder as follows:

$ mkdir apps/first
We'll call this application 'first', but you can use any name you find appropriate

Newt create the core apps/first/pkg.yml file with the following text (for example via $ nano apps/first/pkg.yml, or using your favorite text editor):

This file lists the external dependencies for your project
pkg.name: apps/first
pkg.type: app

pkg.deps:
    - "@apache-mynewt-core/libc/baselibc"
    - "@apache-mynewt-core/kernel/os"
    - "@apache-mynewt-core/sys/sysinit"
    - "@apache-mynewt-core/sys/shell"
    - "@apache-mynewt-core/sys/console/full"
    - "@apache-mynewt-core/sys/log/full"
    - "@apache-mynewt-core/sys/stats/full"
    - "@apache-mynewt-core/hw/hal"
    - "@apache-mynewt-core/mgmt/imgmgr"
    - "@apache-mynewt-core/mgmt/newtmgr"
    - "@apache-mynewt-core/mgmt/newtmgr/transport/nmgr_shell"
    - "@apache-mynewt-core/boot/split"
    - "@apache-mynewt-core/boot/bootutil"

Next create a apps/first/syscfg.yml file with the following content (for example $ nano apps/first/syscfg.yml):

This file contains config settings for your application to control what features and used or enabled during the build process
      syscfg.vals:
    # Use INFO log level to reduce code size.  DEBUG is too large for nRF51.
    LOG_LEVEL: 1
    REBOOT_LOG_CONSOLE: 1

    # Enable the shell task.
    SHELL_TASK: 1

    # Include names for statistics.
    STATS_NAMES: 1

    # Enable shell commands.
    STATS_CLI: 1
    LOG_CLI: 1
    CONFIG_CLI: 1

    # Enable newtmgr commands.
    STATS_NEWTMGR: 1
    LOG_NEWTMGR: 1
    CONFIG_NEWTMGR: 1
    

Finally create a apps/first/src/main.c file where the main source code will be stored:

$ mkdir -p apps/first/src
$ nano apps/first/src/main.c

... and enter the following code:

#include <assert.h>
#include <string.h>

#include "os/os.h"
#include "bsp/bsp.h"
#include "hal/hal_gpio.h"
#include "sysinit/sysinit.h"

int
main(int argc, char **argv)
{
    int rc;

    /* Initialize the OS */
    sysinit();

    /* Configure the LED GPIO as an output and HIGH (On) */
    hal_gpio_init_out(LED_BLINK_PIN, 1);

    while (1) {
        /* Run the event queue to process background events */
        os_eventq_run(os_eventq_dflt_get());
    }

    return rc;
}

Create a New Target

Next, you need to create a new target that points to your app via the following command:

$ newt target create first
Target targets/first successfully created

... and then configure the new target with the following mandatory fields:

1. Set the Target's app Field

Point the new target to the app we created above via:

$ newt target set first app=apps/first
Target targets/first successfully set target.app to apps/first

2. Set the Target's bsp Field

Next set the bsp (board support package) for the new target, which indicates the HW that the app will be running on.

If you are using the Adafruit nRF52 Pro Feather this should be:

$ newt target set first [email protected]/hw/bsp/ada_feather_nrf52
Target targets/first successfully set target.bsp to @apache-mynewt-core/hw/bsp/ada_feather_nrf52

3. Set the build_profile Field

Finally set the build profile for the new target (debug or optimized):

$ newt target set first build_profile=debug
Target targets/first successfully set target.build_profile to debug

4. Test the Target Settings

You can review the target settings via the following command:

'my_blinky_sim' is an artifact of the default project creation process and can be ignored
$ newt target show first
targets/first
    app=apps/first
    [email protected]/hw/bsp/ada_feather_nrf52
    build_profile=debug
targets/my_blinky_sim
    app=apps/blinky
    [email protected]/hw/bsp/native
    build_profile=debug

Final Project Structure

The final project structure should look like this:

apps/first
├── LICENSE
├── NOTICE
├── README.md
├── apps
│   └── blinky
│       ├── pkg.yml
│       └── src
│           └── main.c
├── pkg.yml
├── project.yml
├── src
│   └── main.c
└── syscfg.yml

Build and Flash the Target

Finally, you can build your target, pointing to the new application, and using the specified target BSP and build profile:

$ newt build first
Building target targets/first
Compiling repos/apache-mynewt-core/boot/bootutil/src/loader.c
...
Compiling repos/apache-mynewt-core/util/mem/src/mem.c
...
Archiving util_mem.a
Linking [PATH]/bin/targets/first/app/apps/first/first.elf
Target successfully built: targets/first

Sign the Build with a Version Number

You can prepare the image to be flashed to the device with a mandatory version number and signing details via the newt create-image command:

$ newt create-image first 0.1.5
App image succesfully generated: [PATH]/bin/targets/first/app/apps/first/first.img

And finally you can flash the image via one of two methods:

Flash the Image via a Segger J-Link

With the J-Link connected to the nRF52, run the following command to flash the signed image:

This command assumes that you have the Segger J-Link drivers installed on your system, as described in the Native Installation pages.
$ newt load first
Loading app image into slot 1

Flash the Image via the Serial Port and newtmgr

For details on how to flash the image over newtmgr using only the serial port (no J-Link required), see Uploading Application Images with newtmgr.

Last updated on 2017-08-18 at 01.28.42 PM Published on 2017-08-18 at 03.37.04 PM