Skip to content

New Project Setup Guide

Jamie Smith edited this page Oct 19, 2022 · 36 revisions

This page will walk you through the process of setting up a totally blank project that uses Mbed CE. Note that this already assumes you have the development environment set up.

  1. Create empty Git repo: git init
  2. Add Mbed OS as a submodule: git submodule add --depth 1 https://github.com/mbed-ce/mbed-os.git mbed-os
  3. Make sure that only the most recent commit of Mbed OS is cloned: git config -f .gitmodules submodule.mbed-os.shallow true
  4. Create a basic mbed_app.json:
{
    "target_overrides": {
        "*": {
            "platform.stdio-baud-rate": 115200,
            "platform.stdio-buffered-serial": 1
        }
    }
}
  1. Make sure you have the needed Python requirements: python3 -m pip install -r mbed-os/tools/requirements.txt (use python instead of python3 on Windows)
  2. Create a basic CMakeLists.txt:
cmake_minimum_required(VERSION 3.19)
cmake_policy(VERSION 3.19)

# Initialize Mbed OS build system. 
# Note: This block must be before the include of app.cmake
set(MBED_APP_JSON_PATH mbed_app.json)

include(mbed-os/tools/cmake/app.cmake)
add_subdirectory(mbed-os)

project(MyMbedApp)

# add subdirectories and build targets here

mbed_finalize_build()
  1. Create an empty program, MyProgram.cpp:
#include "mbed.h"

int main()
{
	while(true) {}

	// main() is expected to loop forever.
	// If main() actually returns the processor will halt
	return 0;
}
  1. Create build rules for your program by adding this to your CMakeLists just before the end:
add_executable(MyProgram MyProgram.cpp)
target_link_libraries(MyProgram mbed-os) # Can also link to mbed-baremetal here
mbed_set_post_build(MyProgram)

Note: Unlike Mbed OS 6, Mbed CE lets you create multiple executables, as well as STATIC and OBJECT libraries, in one CMake project.

  1. Now, you are ready to set up the CMake project for editing. We have three ways to do this:

Note: Converting the Mbed OS Submodule

These instructions cause the Mbed OS submodule to be cloned in shallow mode, which reduces the amount of space consumed by its history. However, this prevents you from making normal commits and contributions to Mbed OS. If you want to make contributions to Mbed, you will need to convert this repo to a full Git repo.

To do that, go into the mbed-os folder and run the following commands (from here):

git fetch --unshallow
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch origin
git checkout master
git pull origin master

That will get you on a master branch that matches master branch on this repo, and you can make commits, PRs, and forks like normal.