Day2:-ESP-IDF messing around with CMake

Adding a new component - ble peripheral

Now that the setup is complete, let’s talk about components. Code modularity is crucial, and in ESP-IDF, we achieve that by creating components. However, creating a component requires some knowledge of CMake and C. A component is essentially a standalone set of code that can be used in the main file or other components. Here’s a quick look at my code structure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
├── CMakeLists.txt
├── components
│   ├── common
│   │   ├── nimble_central_utils
│   │   └── nimble_peripheral_utils
│   └── nimble-ble-prph
│   ├── CMakeLists.txt
│   ├── gatt_svr.c
│   ├── include
│   ├── Kconfig.projbuild
│   └── nimble-ble-prph.c
├── dependencies.lock
├── LICENSE
├── main
│   ├── CMakeLists.txt
│   ├── idf_component.yml
│   └── main.c
├── README.md
├── sdkconfig
├── sdkconfig.defaults
└── sdkc

The common directory holds essential files frequently utilized alongside a module. Meanwhile, the nimble-ble-prph component is centered around the nimble-ble-prph.c file, housing the bulk of the code.

CMake structure for the project

Starting with the high-level CMakeLists.txt:

1
2
3
4
cmake_minimum_required(VERSION 3.16)
set(EXTRA_COMPONENT_DIRS components/common/nimble_peripheral_utils)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(assistive-shoulder-firmware)
  • cmake_minimum_required: Specifying the CMake version.
  • set: Defining the location for the essential common component.
  • include: Incorporating essential configurations from project.cmake.

Now, the core CMakeLists.txt in the main folder:

1
2
3
idf_component_register(SRCS "main.c"
INCLUDE_DIRS "."
REQUIRES nimble-ble-prph)
  • REQUIRES: Integrating the nimble-ble-prph component as a dependency.

Lastly, the pivotal CMakeLists.txt for the nimble-ble-prph component:

1
2
3
4
5
set(srcs "main.c")
idf_component_register(SRCS "nimble-ble-prph.c" "gatt_svr.c"
INCLUDE_DIRS "include"
REQUIRES nvs_flash nimble_peripheral_utils bt
)
  • SRCS: Enumerating source files for the compilation process.
  • INCLUDE_DIRS: Specifying directories for header files.
  • REQUIRES: Listing dependencies, including components like nvs_flash, nimble_peripheral_utils, and bt. CMake kindly reminds us if we miss one.Awww!🥺
    1
    2
    3
    Compilation failed because nimble-ble-prph.c (in "nimble-ble-prph" component) includes nvs_flash.h, provided by nvs_flash component(s).
    However, nvs_flash component(s) is not in the requirements list of "nimble-ble-prph".
    To fix this, add nvs_flash to PRIV_REQUIRES list of idf_component_register call in /home/thedevmanek/Documents/Group Projects/assistive-shoulder-firmware/components/nimble-ble-prph/CMakeLists.txt.
    I want to talk more about what I am trying to do with bluetooth but I will do that in the next post.I know it is sad but I am hungry. BYE!