Skip to content

ted-xp/stm32-cmake

 
 

Repository files navigation

Table of Contents

About

This project is used to develop applications for stm32 - ST's ARM Cortex-M3 MCUs, using cmake, GCC, newlib (libc) and STM32Fxxx Standard Peripherals Library.

Requirements:

  • cmake >= 2.8
  • GCC toolchain with newlib.
  • STM32F0xx Standard Peripherals Library for STM32F0 family
  • STM32F10x Standard Peripherals Library for STM32F1 family
  • STM32F2xx Standard Peripherals Library for STM32F2 family
  • STM32F4 DSP and Standard Peripherals Library for STM32F4 family
Project contains:
  • CMake common toolchain file, that configures cmake to use arm toolchain.
  • CMake family-specific toolchain file, that configures family-specific parameters.
  • CMake projects that builds CMSIS and Standard Peripherals Library into static libraries.
  • CMake modules to find and configure CMSIS ans StdPeriphLib libraries.
  • CMake project template.
  • Example projects
    • blinky - blink LED using timers and PWM.
    • newlib - show date from RTC using uart and libc functions from newlib

Building & Installing

First of all you need to configure toolchain and libraries, you can do this by editing gcc_stm32.cmake or (better way) pass it throught command line. Variables for toolchain:

  • TOOLCHAIN_PREFIX - where toolchain is located, default: /usr
  • TARGET_TRIPLET - toolchain target triplet, default: arm-none-eabi
  • STM32_FAMILY - STM32 family (F0, F1, F4, etc.) currently, F0, F1, F2 and F4 family are supported.
Note: If STM32_CHIP variable is set, STM32_FAMILY is optional.

Variables for CMSIS and StdPeriphLib:

  • STM32F0_StdPeriphLib_DIR - path to STM32F0xx Standard Peripherals Library default: /opt/STM32F0xx_StdPeriph_Lib_V1.3.1
  • STM32F1_StdPeriphLib_DIR - path to STM32F10x Standard Peripherals Library default: /opt/STM32F10x_StdPeriph_Lib_V3.5.0
  • STM32F2_StdPeriphLib_DIR - path to STM32F2xx Standard Peripherals Library default: /opt/STM32F2xx_StdPeriph_Lib_V1.1.0
  • STM32F4_StdPeriphLib_DIR - path to STM32F4 DSP and Standard Peripherals Library default: /opt/STM32F4xx_DSP_StdPeriph_Lib_V1.3.0
  • USE_ASSERT - Use internal asserts in Standard Peripherals Library.

Build CMSIS and Standard Peripherals Library

In cmsis folder:

 cmake -DCMAKE_TOOLCHAIN_FILE=../gcc_stm32.cmake -DSTM32_FAMILY=F1 -DCMAKE_INSTALL_PREFIX=<path_to_toolchain>/arm-none-eabi/ -DCMAKE_BUILD_TYPE=Release
 make && make install

In stdperiph folder

 cmake -DCMAKE_TOOLCHAIN_FILE=../gcc_stm32.cmake -DCMAKE_MODULE_PATH=<path_to_cmake_folder_of_this_project>/Modules -DSTM32_FAMILY=F1 -DCMAKE_INSTALL_PREFIX=<path_to_toolchain>/arm-none-eabi/ -DCMAKE_BUILD_TYPE=Release
 make && make install

Note: For building for STM32F0 family, change -DSTM32_FAMILY=F1 to -DSTM32_FAMILY=F0 Note: For building for STM32F2 family, change -DSTM32_FAMILY=F1 to -DSTM32_FAMILY=F2 Note: For building for STM32F4 family, change -DSTM32_FAMILY=F1 to -DSTM32_FAMILY=F4

Note: You can use different CMAKE_INSTALL_PREFIX, but than you'll have to configure cmake search paths when using cmake modules. You may specify an additional search path with -DEXTRA_FIND_PATH=<path-to-search></path-to-search>

Note: To prevent header collision file "misc.h" from Standard Peripherals Library will be installed as stm32f0xx_misc.h / stm32f10x_misc.h / stm32f2xx_misc.h / stm32f4xx_misc.h

Usage

After building you need to copy cmake modules in cmake's modules path, or just set CMAKE_MODULE_PATH in project. Template project can be found in stm32-template folder.

Configure

Common usage:

 cmake -DSTM32_CHIP=&lt;chip&gt;&lt;/chip&gt; -DCMAKE_TOOLCHAIN_FILE=&lt;path_to_gcc_stm32.cmake&gt;&lt;/path_to_gcc_stm32.cmake&gt; -DCMAKE_BUILD_TYPE=Debug &lt;path_to_source_dir&gt;&lt;/path_to_source_dir&gt;

Where <chip> - stm32 chip name (e.g. STM32F100C8, STM32F407IG). This command will generate Makefile for project. Scripts will try to detected chip parameters (type, flash/ram size) from chip name. You can set this parameters directly using following cmake variables:

  • STM32_CHIP_TYPE - family-depended chip type. Global variable STM32_CHIP_TYPES contains list of valid types for current family (e.g. for F1 family: HD, HD_VL, MD, MD_VL, LD, LD_VL, CL, XL)
  • STM32_FLASH_SIZE - chip flash size (e.g. 64K)
  • STM32_RAM_SIZE - chip RAM size (e.g. 4K)
For using with Eclipse CDT:

 cmake -DSTM32_CHIP=&lt;chip&gt;&lt;/chip&gt; -DCMAKE_TOOLCHAIN_FILE=&lt;path_to_gcc_stm32.cmake&gt;&lt;/path_to_gcc_stm32.cmake&gt; -DCMAKE_BUILD_TYPE=Debug -G "Eclipse CDT4 - Unix Makefiles" &lt;path_to_source_dir&gt;&lt;/path_to_source_dir&gt;

For release build:

 cmake -DSTM32_CHIP=&lt;chip&gt;&lt;/chip&gt; -DCMAKE_TOOLCHAIN_FILE=&lt;path_to_gcc_stm32.cmake&gt;&lt;/path_to_gcc_stm32.cmake&gt; -DCMAKE_BUILD_TYPE=Release &lt;path_to_source_dir&gt;&lt;/path_to_source_dir&gt;

Build

 make

To build .hex:

 make &lt;project&gt;&lt;/project&gt;.hex

or .bin:

 make &lt;project&gt;&lt;/project&gt;.bin

Linker script variables

Next cmake variables are useful for linker tuning:

  • STM32_FLASH_ORIGIN - Start address of flash (default: 0x08000000)
  • STM32_RAM_ORIGIN - Start address of RAM (default: 0x20000000)
  • STM32_STACK_ADDRESS - Address of stack bottom (default: RAM_ORIGIN + RAM_SIZE)
  • STM32_FLASH_SIZE - Flash size (default: from chip name)
  • STM32_RAM_SIZE - RAM size (default: from chip name)
  • STM32_MIN_STACK_SIZE - Minimum stack size for error detection at link-time (default: 512 bytes)
  • STM32_MIN_HEAP_SIZE - Minimum heap size for error detection at link-time (default: 0 bytes)

Useful cmake macros

  • STM32_GET_CHIP_TYPE(CHIP CHIP_TYPE) - gets chip type (HD, MD, etc.) from chip name.
  • STM32_GET_CHIP_PARAMETERS(CHIP FLASH_SIZE RAM_SIZE) - gets chip ram/flash size from chip name.
  • STM32_SET_CHIP_DEFINITIONS(TARGET CHIP_TYPE) - sets chip family and type-specific compiler flags for target.
  • STM32_SET_FLASH_PARAMS(TARGET ...) - sets chip flash/ram parameters for targer.
  • STM32_SET_TARGET_PROPERTIES(TARGET) - sets all needed parameters and compiler flags for target.
  • STM32_GENERATE_LIBRARIES(NAME SOURCES LIBRARIES) - generates libraries for all chip types in family. Resulting libraries stored in LIBRARIES and have names in ${NAME}_${FAMILY}_${CHIP_TYPE} format.

Notes about using this package under Windows

This package is successfully used to build STM32 applications under Windows using freely available Sourcery_CodeBench_Lite_for_ARM_EABI toolchain. The recommended setup is:

  • Install Sourcery Codebench Lite
  • Install minimal MinGW to have properly functioning 'make.exe'
  • Prepare initial CMake cache contents build_settings setting paths proper for your installation:
 SET(CMAKE_MAKE_PROGRAM c:/MinGW/bin/mingw32-make.exe CACHE FILEPATH "Make program")
 SET(CMAKE_TOOLCHAIN_FILE d:/Projects/lib/stm32-cmake/gcc_stm32.cmake CACHE FILEPATH "Toolchain file")
 SET(TOOLCHAIN_PREFIX "c:/MentorGraphics/Sourcery_CodeBench_Lite_for_ARM_EABI" CACHE PATH "Toolchain prefix")
 SET(STM32_CHIP "STM32F030R8" CACHE STRING "stm32 chip")
 SET(STM32F2_StdPeriphLib_DIR "d:/Projects/lib/STM32F2xx_StdPeriph_Lib_V1.1.0" CACHE PATH "StdPeriph path")
 SET(STM32F0_StdPeriphLib_DIR "d:/Projects/lib/STM32F0xx_StdPeriph_Lib_V1.3.1" CACHE PATH "StdPeriph path")
 SET(CMAKE_INSTALL_PREFIX "d:/Projects/lib/bin" CACHE PATH "Install prefix")
 SET(CMAKE_MODULE_PATH "d:/Projects/lib/stm32-cmake/cmake/Modules" CACHE PATH "CMake modules path")
 SET(EXTRA_FIND_PATH "${CMAKE_INSTALL_PREFIX}" CACHE PATH "Build root path")
 SET(CMAKE_ECLIPSE_VERSION "4.3" CACHE STRING "Eclipse version")

  • And now you go (again, use proper paths):
 "C:\Program Files (x86)\CMake 2.8\bin\cmake.exe" -G "Eclipse CDT4 - MinGW Makefiles" -C ../../build/build_settings -DCMAKE_BUILD_TYPE=Debug ../source/

About

CMake for stm32 developing.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 95.1%
  • Objective-C 4.9%