{"id":29049396,"url":"https://github.com/gembcior/stm32-cmake-blinking-led","last_synced_at":"2025-06-26T19:09:38.937Z","repository":{"id":216010327,"uuid":"301181129","full_name":"gembcior/stm32-cmake-blinking-led","owner":"gembcior","description":"Simple example how to use CMake with C++ for STM32 development.","archived":false,"fork":false,"pushed_at":"2020-10-17T16:37:58.000Z","size":38,"stargazers_count":2,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-01-07T23:40:01.756Z","etag":null,"topics":["cmake","cpp","embedded","stm32"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gembcior.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-10-04T17:03:00.000Z","updated_at":"2024-01-07T23:40:05.475Z","dependencies_parsed_at":"2024-01-07T23:50:11.281Z","dependency_job_id":null,"html_url":"https://github.com/gembcior/stm32-cmake-blinking-led","commit_stats":null,"previous_names":["gembcior/stm32-cmake-blinking-led"],"tags_count":1,"template":null,"template_full_name":null,"purl":"pkg:github/gembcior/stm32-cmake-blinking-led","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gembcior%2Fstm32-cmake-blinking-led","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gembcior%2Fstm32-cmake-blinking-led/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gembcior%2Fstm32-cmake-blinking-led/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gembcior%2Fstm32-cmake-blinking-led/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gembcior","download_url":"https://codeload.github.com/gembcior/stm32-cmake-blinking-led/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gembcior%2Fstm32-cmake-blinking-led/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262128910,"owners_count":23263352,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cmake","cpp","embedded","stm32"],"created_at":"2025-06-26T19:09:38.309Z","updated_at":"2025-06-26T19:09:38.928Z","avatar_url":"https://github.com/gembcior.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"## STM32 CubeMX + Cmake\n\n### Introduction\nOn this repository you can find simple example how you can use CMake with C++ for STM32 development.\nThe code itself doesn't do anything special, just the led blinks.\nThe main purpose of this example is to show how `STMCube` and `STMCubeMX` can be integrated with CMake.\n\n### Project structure\n```\n📦stm32-cmake-blinking-led\n ┣━📂cmake              ─ contains all additonal CMake files neccessary to build project\n ┃ ┣━📂defaults         ─ contains file with default cache variables\n ┃ ┣━📂modules          ─ contains all CMake modules files\n ┃ ┗━📂toolchain        ─ contains toolchain file\n ┣━📂cubemx             ─ CubeMX project and all generated files\n ┣━📂source             ─ application sources divided into libraries\n ┃ ┗━📜CMakeLists.txt   ─ application CMake file\n ┣━📂utils              ─ contains some useful files for stm32 development\n ┗━📜CMakeLists.txt     ─ top level CMake file\n```\n\n### Requirements\nThe project is designed base on STM32 Nucleo-32 board with `STM32L432KC` microcontroller. It was developed on Linux based machine. For properly build the project you will need:\n - Linux based machine,\n - CMake \u003e= 3.16,\n - [STMCube repository for STM32L4 series](https://github.com/STMicroelectronics/STM32CubeL4),\n - [GNU Arm Embedded Toolchain](https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads),\n - [STM32CubeProgrammer](https://www.st.com/en/development-tools/stm32cubeprog.html) (not required for project compilation).\n\nIn the project there is one crucial file:\n\n```\n📦stm32-cmake-blinking-led\n ┗━📂cmake\n   ┗━📂defaults\n     ┗━📜stm32_cache.cmake\n```\n\nThere are variables that point to required tools to build the project.\nIt is important to set these variables correctly in order to build this project.\n\n```cmake\nset(STM32_TOOLCHAIN $ENV{HOME}/tools/toolchains/gcc-arm-none-eabi-9-2020-q2-update CACHE PATH \"\")\nset(STM32_CUBE $ENV{HOME}/tools/STM32/STM32Cube/STM32CubeL4 CACHE PATH \"\")\nset(STM32_CHIP \"STM32L432xx\" CACHE STRING \"\")\nset(STM32_CHIP_FAMILY \"STM32L4\" CACHE STRING \"\")\nset(STM32_CUBE_PROGRAMMER $ENV{HOME}/tools/STM32/STM32CubeProgrammer/bin/STM32_Programmer_CLI CACHE PATH \"\")\n```\n\nAfter some tweaks, there is a chance that this project can also be compiled on Windows based machine.\n\n### STMCube integration\n\nIn the project there are a few major files responsible for `STM32Cube` and `STM32CubeMX` integration.\n\n##### STM32CubeMX project\n```\n📦stm32-cmake-blinking-led\n ┗━📂cubemx\n   ┣━📂Inc\n   ┣━📂Src\n   ┗━📜cubemx.ioc\n```\nThere is `STM32CubeMX` project file and all generated files.\nThese files are copied to cubemx library in the source directory.\nThe main reason why these files are copied is to separate pure generated file from actual application code.\n\n```\n📦stm32-cmake-blinking-led\n ┗━📂source\n   ┗━📂cubemx\n```\n\nGenerated files can be use as is. The only change is in `main.h` and `main.c` file. The `main()` function was deleted and declaration of `SystemClock_Config` function was added to make it visible from other libraries.\nOtherwise, the compiler will show an error that it cannot see this function.\n\n```cpp\n/* USER CODE BEGIN EFP */\nvoid SystemClock_Config(void);\n/* USER CODE END EFP */\n```\n\n##### STM32Cube HAL wrappers\n```\n📦stm32-cmake-blinking-led\n ┗━📂source\n   ┗━📂hal\n```\n\nThere is a hal library which contain C++ wrappers for STM32Cube HAL drivers.\nIts purpose is to simplify usage of STM32Cube HAL with C++ language.\nThe idea is to make wrappers for all used STM32Cube HAL drivers and after that use these wrappers in the application code.\n\n##### CMake find modules\n```\n📦stm32-cmake-blinking-led\n ┗━📂cmake\n   ┗━📂modules\n     ┣━📜FindCMSIS.cmake\n     ┗━📜FindSTM32HAL.cmake\n```\n\nOne of the most important files are CMake find modules.\nThese modules are responsible for finding source and header files of the CMSIS library and STM32Cube HAL drivers.\n\n### How to build\n\nClone the repository and cd to project directory:\n\n```shell\ngit clone https://github.com/gembcior/stm32-cmake-blinking-led.git\ncd stm32-cmake-blinking-led\n```\n\nCreate workspace directory for build and cd into it:\n\n```shell\nmkdir build\ncd build\n```\n\nGenerate CMake project:\n\n```shell\ncmake ../\n```\n\nRun build command:\n\n```shell\ncmake --build .\n```\n\n### Programming device\nYou can use CMake target **stm32-flash** to program application to the device over SWD interface.\n\u003cbr\u003e\nSimply run:\n\n```shell\ncmake --build . --target stm32-flash\n```\n\nThis requires STM32CubeProgrammer installed and properly set path to it in **stm32_cache.cmake** file.\nSee [Requirements](#requirements).\n\n### Project in action\n\n![in_action](https://mywayof.dev/assets/img/stm32_cubemx_with_cmake/in_action.webp)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgembcior%2Fstm32-cmake-blinking-led","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgembcior%2Fstm32-cmake-blinking-led","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgembcior%2Fstm32-cmake-blinking-led/lists"}