The previous tutorial showed how to get to a working c program and introduced some nice abstractions. In this tutorial, we will show a way to put common code into separate libraries and re-using them across projects.
A common way to re-use code libraries is through a package manager. We are going to use CPM: the C Package Manager. CPM is implemented in CMake, so we will switch from Make to CMake as our build system.
The basic principle is this: in the CMake build system, we include a file that implements CPM:. We can then list our dependencies, for example:
CPM_AddModule("chip_lpc11uxx" GIT_REPOSITORY "https://github.com/JitterCompany/chip_lpc11uxx.git" GIT_TAG "1.1")
The example above lists the package “chip_lpc11uxx” as a dependency. In this case we specified version
1.1, but this can be any git tag or simply
master. Another way to list a dependency would be this:
CPM_AddModule("my_library" SOURCE_DIR "/local/filesystem/path/to/my_library")
This is very usefull during debugging or developing such a library. Otherwise you would constantly need to push your git changes.
A CPM module itself is not very complicated. As an example, take a look at the chip_lpc11uxx package. It contains:
A typical firmware project using the CPM system looks like this:
All these packages are open-source, sou you can check them out on github if you want. If you want to create your own package, the easiest way is to model it after a simple existing package, for example c_utils.
This project (as most cpm firmware packages) has a separate file where you can customize some settings. Create the config file as below, then edit it with your favorite text editor:
cp config.cmake.example config.cmake
As stated in the README for the cpm project, you can build the firmware like this:
mkdir build cd build cmake .. make
Untill now, we had to copy-paste quite long commands to flash the firmware to our board. In the cpm-based project this is much simpler: the mcu_debug package adds some nice shortcuts for this.
To flash the firmware:
To start debugging with gdb:
For more info, check the README.