Skip to content

dananjayavr/stm32_cmake_minimal_template

Repository files navigation

Prerequisites

  • CMake
  • ARM GNU GCC Tools
  • MinGW

Initialize Project

  • Generate a Makefile project using CubeMX. Use a development board and initialize all peripherals to their default mode (to generate all HAL files)

  • Outside the generated directory (ex. BSP), create a CMakeLists.txt and arm-none-eabi-gcc.cmake files.

  • Populate the CMakeLists.txt file using the generated Makefile (basically copying all necessary files).

  • If cloning this project, adapt the above two files accordingly to your MCU. Pay attention to :

    • Project name
    • Src and Inc paths
    • Name of startup file
    • Name of linker file
    • Compiler definition for the chosen board / MCU

Build the project (Manual)

  • Create a build folder (ex. cmake-build-debug) and cd to that folder

  • Run cmake -G "MinGW Makefiles" -DCMAKE_TOOLCHAIN_FILE=../arm-none-eabi-gcc.cmake -DCMAKE_BUILD_TYPE=Debug .. (or MSYS Makefiles)

  • Then, run cmake --build . -- -j 4

  • You should obtain .bin and .hex files for your target

Run project

  • Use STM32CubeProgrammer to program the device.

Build the project (with CLion)

  • Open the folder containing CMakeLists.txt and *.cmake file with CLion.
  • In File -> Settings -> Build, Execution, Deployment section, add the following to CMake options field: -DCMAKE_TOOLCHAIN_FILE=arm-none-eabi-gcc.cmake
  • CLion should reload the CMake project without any errors.
  • Build the project

Debug using OpenOCD and CLion

  • Create a new run configuration (Run -> Edit Configurations)
  • Add a new OpenOCD Download & Run configuration
  • In the board config file field, supply the appropriate configuration file. Ex. https://github.com/ntfreak/openocd/blob/master/tcl/board/st_nucleo_f4.cfg
  • Also make sure to fill the Executable field ex. nucleo-f401re.out file in the build folder
  • Pressing the Run button should now flash the target
  • To debug, place a breakpoint and click on the green bug (debug) icon. The debugger should stop at the breakpoint

Find CMSIS-SVD Parsers

Troubleshooting

Sources

About

Minimal CMake template for STM32F4xx MCU

Topics

Resources

Stars

Watchers

Forks

Languages