Prerequisites:
- Python3
- CMake
- An editor that uses clangd for intellisense/autocompletion of c/c++
- eg. VSCode with the c/c++ extension
- Clion
- vim/nvim with some lsp client installed and setup to use clangd
Open a terminal / command prompt window.
-
Run
pip install mbed-tools -
mbed-tools uses some optional libraries but it doesn't install them automatically, so run this too (optional):
pip install intelhex prettytable future -
Run
mbed-toolsto make sure the cli is installed properly and available! You should see a help menu and list of commands printed out. -
Run
mbed-tools initto create a new project, or runmbed-tools deployto setup mbed-os with an existing project (the project should have an mbed-os.lib file specifying which mbed-os version to deploy) -
Run
mbed-tools configure -t GCC_ARM -m NUCLEO_L432KCto set up the cmake build system for the NUCLEO_L432KC microcontroller (run this for as many MCU's as you want, it will create separate build directories in./cmake_build/for each one!) -
Now run the build system manually with CMake to generate the compile_commands.json file (necessary for clangd to find mbed-os):
cmake -S . -B cmake_build/NUCLEO_L432KC/develop/GCC_ARM -GNinja -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -
To make clangd find the generated compile_commands.json file, there's two options:
- Create a file named
.clangdin the project's root, in which you specify the path to the compile_commands.json file:CompileFlags: CompilationDatabase: ./cmake_build/NUCLEO_L432KC/develop/GCC_ARM
- Create a symlink (shortcut) to compile_commands.json in the project root.
Run
ln -s cmake_build/NUCLEO_L432KC/develop/GCC_ARM/compile_commands.json .for MacOS/Linux, or create a shortcut manually in file exporer on Windows
- Create a file named
-
Now, restart your editor. Clangd should have found and parsed your mbed-os configuration for your specified microcontroller! Eg. start typing pin aliases like PA_2 and wait for autocompletion to show up. Then go-to-definition on one and you should be brought into the mbed-os source code.
-
To actually compile the code, use the
mbed-tools compilecommand. It's flags are identical toconfigure's flags, with the addition of -f (flash code) and -s (open serial terminal). They can be compined like this:mbed-tools compile -t GCC_ARM -m NUCLEO_L432KC -f -s(compile code for NUCLEO_L432KC mcu, then flash it, then open a serial terminal)
Steps 5-8 will have to be repeated any time you want to switch microcontrollers.