Version: 1.0.0
This repository can be used as a starting template for mbed projects with sane defaults:
- simple directory structure
- CMake based with USCRPL/mbed-cmake
- Code completion with VSCode + CMake Tools
- format with
.clang-format
and.editorconfig
Note: you should be able to use mbed-cli if you want.
Before starting, make sure you've read the mbed documentation.
To start with:
# Clone the repository
$ git clone https://github.com/ladislas/mbed-cmake-template && cd mbed-cmake-template
# Clone Mbed OS using the Makefile
$ make clone_mbed
# You can specify a branch (default is master)
$ make clone_mbed BRANCH=mbed-os-6.3.0
I recommend the manual install. Make sure to follow the instructions from mbed:
https://os.mbed.com/docs/mbed-os/v6.3/build-tools/install-and-set-up.html
# For the latest stable version
$ python3 -m pip install -U --user mbed-cli
# For HEAD
$ python3 -m pip install -U --user https://github.com/ARMmbed/mbed-cli/archive/master.zip
Then, install the required tools:
# First, important packages
$ python3 -m pip install -U --user pyserial intelhex prettytable
# And finally mbed-cli requirements
$ python3 -m pip install -U --user -r lib/_vendor/mbed-os/requirements.txt
# Install arm-gcc
$ brew tap ArmMbed/homebrew-formulae
$ brew install arm-none-eabi-gcc
# Install Ninja (for building)
$ brew install ninja
# Install coreutils and add to path
$ brew install coreutils
You can download the toolchain here:
And for Ninja, follow the documentation:
We put together an handy Makefile
to make it easier to configure and compile your projects.
# First, you need to clone mbed-os
$ make clone_mbed
# You can specify a branch (default is master)
$ make clone_mbed BRANCH=mbed-os-6.3.0
# Then configure for your target
$ make config TARGET_BOARD=DISCO_F769NI
# You can also specify a build type (default is Release)
$ make config TARGET_BOARD=DISCO_F769NI BUILD_TYPE=Debug
# Finally, to compile your project
$ make
To speed up compilation time, we've setup an .mbedignore
file that removes some files from the compilation process.
If you get error about missing headers when compiling, make sure the header's directory is not set in the .mbedignore
file.
You can edit the .mbedignore
file here: (it is symlinked to the mbed-os directory)
If you want to use mbed-cli, you can set your target and favorite toolchain in the .mbed
file:
MBED_OS_DIR=./lib/_vendor/mbed-os
TARGET=DISCO_F769NI
TOOLCHAIN=GCC_ARM
ROOT=.
In the Makefile
we also provide a flash
target. It simply copies the .bin
file to your Mbed Enabled board.
flash:
@diskutil list | grep "DIS_F769NI" | awk '{print $$5}' | xargs -I {} diskutil unmount '/dev/{}'
@diskutil list | grep "DIS_F769NI" | awk '{print $$5}' | xargs -I {} diskutil mount '/dev/{}'
cp build/$(PROGRAM) /Volumes/DIS_F769NI
You must edit the Makefile to suit your needs:
- change the
DIS_F769NI
to your board id - change the path of the volume
- use
fdisk
on Linux instead ofdiskutil
If you need something more complex, it should be easier to provide a script that you can call from the Makefile.
The main idea behind this template is to have your main source files (those for the big project you're working on) under the ./src
directory.
Now, you sometimes need to create a simple, very basic example project to test a new features, investigate a bug or try a different solution to a problem.
These can be added to the spike
directory inside their own directory. You'll need at leat a main.cpp
and a CMakeLists.txt
. See blinky
for a working example.
If you need to use a custom target, you can add it to the target
directory and edit the custom_targets.json
file.
Then, you need to configure the project with the following:
# Run config script with "-x TARGET_BOARD_NAME"
$ make config TARGET_BOARD="-x DISCO_ORIGINAL"
Don't forget the -x
, it's really important as it tells the config script to look for headers and sources files in the targets
directory.
The template comes with a simple HelloWorld
library that you can use as an example and/or use to make sure your program works.
Please open an issue or create a PR.
Made with ❤️ by:
- Ladislas de Toldi - ladislas
This work is heavily inspired by USCRPL/mbed-cmake and code written by:
- Jamie Smith - multiplemonomials
- ProExpertProg - ProExpertProg
Apache 2.0 @ Ladislas de Toldi (Ladislas [at] detoldi dot me)