Skip to content

Commit

Permalink
STM32F2 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Rix committed Jul 12, 2015
1 parent 185c80f commit bfa90fe
Show file tree
Hide file tree
Showing 12 changed files with 1,057 additions and 119 deletions.
135 changes: 69 additions & 66 deletions README.md
@@ -1,89 +1,92 @@
# About
This project is used to develop applications for stm32 - ST's ARM Cortex-M3(4) MCUs, using cmake, GCC, newlib (libc), STM32CubeMX package or ChibiOS.

Requirements:
This project is used to develop applications for the STM32 - ST's ARM Cortex-M3(4) MCUs. It uses cmake and GCC, along with newlib (libc), STM32CubeMX or ChibiOS.

## Requirements

* cmake >= 2.8
* GCC toolchain with newlib (optionally).
* STM32CubeMX package for STM32F1 or STM32F4 families.
Project contains:
* CMake common toolchain file, that configures cmake to use arm toolchain.
* CMake family-specific toolchain file, that configures family-specific parameters.
* CMake modules to find and configure CMSIS ans STM32HAL components.
* GCC toolchain with newlib (optional).
* STM32CubeMX package for STM32F1, STM32F2 or STM32F4 families.

## Project contains

* CMake common toolchain file, that configures cmake to use the arm toolchain.
* CMake toolchain file that can generate a tunable linker script
* CMake STM32 family-specific toolchain file, that configures family-specific parameters.
* CMake modules to find and configure CMSIS and STM32HAL components.
* CMake modules to find and configure ChibiOS components.
* CMake project template.
* Example projects:
* stm32-blinky - blink LED using timers and PWM.
* stm32-newlib - show date using uart and libc functions from newlib
* stm32-chibios - blink led using ChibiOS/NIL

## Examples

* `stm32-blinky` - blink LED using timers and PWM.
* `stm32-newlib` - show date using uart and libc functions from newlib.
* `stm32-chibios` - blink led using ChibiOS/NIL.

# Usage
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_CHIP - STM32 device code, e.g. STM32F407VG or STM32F103VG
* STM32_FAMILY - STM32 family (F0, F1, F4, etc.) currently, F1 and F4 family are supported. **Note:** If STM32_CHIP variable is set, STM32_FAMILY is optional.
* STM32Cube_DIR - path to STM32CubeMX directory **default**: /opt/STM32Cube_FW_F1_V1.1.0 /opt/STM32Cube_FW_F4_V1.6.0

For using scripts you'll need to copy contents of cmake folder into cmake's modules path, or use CMAKE_MODULE_PATH variable.
Template project can be found in stm32-template folder.
First of all you need to configure toolchain and libraries, you can do this by editing `gcc_stm32.cmake` or, preferably, by passing it through the command line.

## Configuration

## Configure
Common usage:
* `TOOLCHAIN_PREFIX` - where toolchain is located, **default**: `/usr`
* `TARGET_TRIPLET` - toolchain target triplet, **default**: `arm-none-eabi`
* `STM32_CHIP` - STM32 device code, e.g. `STM32F407VG` or `STM32F103VG`
* `STM32_FAMILY` - STM32 family (F0, F1, F4, etc.) currently, F1, F2 and F4 family are supported. **Note:** If `STM32_CHIP` variable is set, `STM32_FAMILY` is optional.
* `STM32Cube_DIR` - path to STM32CubeMX directory **default**: `/opt/STM32Cube_FW_F1_V1.1.0 /opt/STM32Cube_FW_F2_V1.1.0 /opt/STM32Cube_FW_F4_V1.6.0`

``` cmake -DSTM32_CHIP=<chip> -DCMAKE_TOOLCHAIN_FILE=<path_to_gcc_stm32.cmake> -DCMAKE_BUILD_TYPE=Debug <path_to_source_dir> ```
To use the toolchain, you'll need to copy contents of the `cmake` folder into cmake's modules path, or use the `CMAKE_MODULE_PATH` variable.

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
* STM32_FLASH_SIZE - chip flash size (e.g. 64K)
* STM32_RAM_SIZE - chip RAM size (e.g. 4K)
## Common usage

For using with Eclipse CDT:
cmake -DSTM32_CHIP=<chip> -DCMAKE_TOOLCHAIN_FILE=<path_to_gcc_stm32.cmake> -DCMAKE_BUILD_TYPE=Debug <path_to_source_dir>

``` cmake -DSTM32_CHIP=<chip> -DCMAKE_TOOLCHAIN_FILE=<path_to_gcc_stm32.cmake> -DCMAKE_BUILD_TYPE=Debug -G "Eclipse CDT4 - Unix Makefiles" <path_to_source_dir> ```
Where `<chip>` is the STM32 chip name (e.g. `STM32F100C8`, `STM32F407IG`).

For release build:
This command will generate Makefile for project. For a `Release` build, change `CMAKE_BUILD_TYPE`.

``` cmake -DSTM32_CHIP=<chip> -DCMAKE_TOOLCHAIN_FILE=<path_to_gcc_stm32.cmake> -DCMAKE_BUILD_TYPE=Release <path_to_source_dir> ```
The script will try to detect chip parameters automatically from the chip name (type, flash/ram size), or, you can set these directly with these variables:

## Build
* `STM32_CHIP_TYPE` - family-dependent chip type. Global variable `STM32_CHIP_TYPES` contains list of valid types for current family (e.g `207xG`)
* `STM32_FLASH_SIZE` - chip flash size (e.g. 64K)
* `STM32_RAM_SIZE` - chip RAM size (e.g. 4K)

To build elf file:
### Usage with Eclipse CDT:

``` make ```

To build .hex:
cmake -DSTM32_CHIP=<chip> -DCMAKE_TOOLCHAIN_FILE=<path_to_gcc_stm32.cmake> -DCMAKE_BUILD_TYPE=Debug -G "Eclipse CDT4 - Unix Makefiles" <path_to_source_dir>

``` make <project name>.hex ```

or .bin:
## Building

``` make <project name>.bin ```
* To build elf file: `make`
* To build .hex: `make <project name>.hex`
* To build .bin: `make <project name>.bin`

## Linker script variables
Next cmake variables are useful for linker tuning:
* STM32_LINKER_SCRIPT - Path to custom linker script. You can use cmake variables (listed below) in itd.
* STM32_FLASH_ORIGIN - Start address of flash (**default**: 0x08000000)
* STM32_RAM_ORIGIN - Start address of RAM (**default**: 0x20000000)
* STM32_CCRAM_ORIGIN - Start address of Core-Coupled RAM (only for F4 family) (**default**: 0x10000000)
* STM32_FLASH_SIZE - Flash size (**default**: from chip name)
* STM32_RAM_SIZE - RAM size (**default**: from chip name)
* STM32_CCRAM_SIZE - Core-Coupled RAM size (only for F4 family) (**default**: 64 KiB)
* 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)
## Linker script & variables

You can use cmake variables below to tune the generated linker. To specify a custom linker script, set `STM32_LINKER_SCRIPT` (you can still use these variables in your custom script).

* `STM32_FLASH_ORIGIN` - Start address of flash (**default**: 0x08000000)
* `STM32_RAM_ORIGIN` - Start address of RAM (**default**: 0x20000000)
* `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)
* `STM32_CCRAM_ORIGIN` - Start address of Core-Coupled RAM (only for F4 family) (**default**: 0x10000000)
* `STM32_CCRAM_SIZE` - Core-Coupled RAM size (only for F4 family) (**default**: 64 KiB)

## Useful cmake macros
* STM32_GET_CHIP_TYPE(CHIP CHIP_TYPE) - gets chip type 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.

## ChibiOS Support
This projects also supports ChibiOS v3.x.x (both nil and rt kernels).
CMake modules for ChibiOS can find specified ChibiOS components using COMPONENTS directive.
See project stm32-chibios for example usage.

* `STM32_GET_CHIP_TYPE(CHIP CHIP_TYPE)` - gets chip type from chip name.
* `STM32_GET_CHIP_PARAMETERS(CHIP FLASH_SIZE RAM_SIZE)` - gets chip ram/flash size from chip name.
* `STM32_SET_FLASH_PARAMS(TARGET ...)` - sets chip flash/ram parameters for target.
* `STM32_SET_CHIP_DEFINITIONS(TARGET CHIP_TYPE)` - sets chip family and type-specific compiler flags for target.
* `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.

# ChibiOS Support

This project also supports ChibiOS v3.x.x (both nil and rt kernels).

CMake modules for ChibiOS can find specified ChibiOS components using the COMPONENTS directive.

See project `stm32-chibios` for example usage.
42 changes: 28 additions & 14 deletions cmake/FindCMSIS.cmake
Expand Up @@ -9,12 +9,12 @@ IF(STM32_CHIP_TYPE OR STM32_CHIP)
STRING(TOLOWER ${STM32_CHIP_TYPE} STM32_CHIP_TYPE_LOWER)
ENDIF()

SET(CMSIS_COMMON_HEADERS
arm_common_tables.h
arm_const_structs.h
arm_math.h
core_cmFunc.h
core_cmInstr.h
SET(CMSIS_COMMON_HEADERS
arm_common_tables.h
arm_const_structs.h
arm_math.h
core_cmFunc.h
core_cmInstr.h
core_cmSimd.h
)

Expand All @@ -23,22 +23,36 @@ IF(STM32_FAMILY STREQUAL "F1")
SET(STM32Cube_DIR "/opt/STM32Cube_FW_F1_V1.1.0")
MESSAGE(STATUS "No STM32Cube_DIR specified, using default: " ${STM32Cube_DIR})
ENDIF()
LIST(APPEND CMSIS_COMMON_HEADERS core_cm3.h)

LIST(APPEND CMSIS_COMMON_HEADERS core_cm3.h)
SET(CMSIS_DEVICE_HEADERS stm32f1xx.h system_stm32f1xx.h)
SET(CMSIS_DEVICE_SOURCES system_stm32f1xx.c)
ELSEIF(STM32_FAMILY STREQUAL "F2")
IF(NOT STM32Cube_DIR)
SET(STM32Cube_DIR "/opt/STM32Cube_FW_F2_V1.1.0")
MESSAGE(STATUS "No STM32Cube_DIR specified, using default: " ${STM32Cube_DIR})
ENDIF()

STRING(REGEX REPLACE "^(2[01]7).[BCDEFG]" "\\1" STM32_DEVICE_NUM ${STM32_CHIP_TYPE})
SET(CMSIS_STARTUP_SOURCE startup_stm32f${STM32_DEVICE_NUM}xx.s)

LIST(APPEND CMSIS_COMMON_HEADERS core_cm4.h)
SET(CMSIS_DEVICE_HEADERS stm32f2xx.h system_stm32f2xx.h)
SET(CMSIS_DEVICE_SOURCES system_stm32f2xx.c)
ELSEIF(STM32_FAMILY STREQUAL "F4")
IF(NOT STM32Cube_DIR)
SET(STM32Cube_DIR "/opt/STM32Cube_FW_F4_V1.6.0")
MESSAGE(STATUS "No STM32Cube_DIR specified, using default: " ${STM32Cube_DIR})
ENDIF()
LIST(APPEND CMSIS_COMMON_HEADERS core_cm4.h)

LIST(APPEND CMSIS_COMMON_HEADERS core_cm4.h)
SET(CMSIS_DEVICE_HEADERS stm32f4xx.h system_stm32f4xx.h)
SET(CMSIS_DEVICE_SOURCES system_stm32f4xx.c)
ENDIF()

SET(CMSIS_STARTUP_SOURCE startup_stm32f${STM32_CHIP_TYPE_LOWER}.s)
IF(NOT CMSIS_STARTUP_SOURCE)
SET(CMSIS_STARTUP_SOURCE startup_stm32f${STM32_CHIP_TYPE_LOWER}.s)
ENDIF()

FIND_PATH(CMSIS_COMMON_INCLUDE_DIR ${CMSIS_COMMON_HEADERS}
PATH_SUFFIXES include stm32${STM32_FAMILY_LOWER} cmsis
Expand All @@ -59,17 +73,17 @@ SET(CMSIS_INCLUDE_DIRS

FOREACH(SRC ${CMSIS_DEVICE_SOURCES})
SET(SRC_FILE SRC_FILE-NOTFOUND)
FIND_FILE(SRC_FILE ${SRC}
FIND_FILE(SRC_FILE ${SRC}
PATH_SUFFIXES src stm32${STM32_FAMILY_LOWER} cmsis
HINTS ${STM32Cube_DIR}/Drivers/CMSIS/Device/ST/STM32${STM32_FAMILY}xx/Source/Templates/
HINTS ${STM32Cube_DIR}/Drivers/CMSIS/Device/ST/STM32${STM32_FAMILY}xx/Source/Templates/
CMAKE_FIND_ROOT_PATH_BOTH
)
LIST(APPEND CMSIS_SOURCES ${SRC_FILE})
ENDFOREACH()

IF(STM32_CHIP_TYPE)
SET(SRC_FILE SRC_FILE-NOTFOUND)
FIND_FILE(SRC_FILE ${CMSIS_STARTUP_SOURCE}
FIND_FILE(SRC_FILE ${CMSIS_STARTUP_SOURCE}
PATH_SUFFIXES src stm32${STM32_FAMILY_LOWER} cmsis
HINTS ${STM32Cube_DIR}/Drivers/CMSIS/Device/ST/STM32${STM32_FAMILY}xx/Source/Templates/gcc/
CMAKE_FIND_ROOT_PATH_BOTH
Expand Down
58 changes: 41 additions & 17 deletions cmake/FindSTM32HAL.cmake
@@ -1,48 +1,72 @@
IF(STM32_FAMILY STREQUAL "F1")
SET(HAL_COMPONENTS adc can cec cortex crc dac dma eth flash gpio hcd i2c
SET(HAL_COMPONENTS adc can cec cortex crc dac dma eth flash gpio hcd i2c
i2s irda iwdg nand nor pccard pcd pwr rcc rtc sd smartcard
spi sram tim uart usart wwdg fsmc sdmmc usb)

SET(HAL_REQUIRED_COMPONENTS cortex pwr rcc)

# Components that have _ex sources
SET(HAL_EX_COMPONENTS adc dac flash gpio pcd rcc rtc tim)

# Components that have ll_ in names instead of hal_
SET(HAL_LL_COMPONENTS fsmc sdmmc usb)

SET(HAL_PREFIX stm32f1xx_)

SET(HAL_HEADERS
stm32f1xx_hal.h
stm32f1xx_hal_def.h
)
SET(HAL_SRCS
stm32f1xx_hal.c
)
ELSEIF(STM32_FAMILY STREQUAL "F2")
SET(HAL_COMPONENTS adc can cortex crc cryp dac dcmi dma eth flash
gpio hash hcd i2c i2s irda iwdg nand nor pccard
pcd pwr rcc rng rtc sd smartcard spi sram tim
uart sart wwdg fsmc sdmmc usbb)

SET(HAL_REQUIRED_COMPONENTS cortex pwr rcc)

# Components that have _ex sources
SET(HAL_EX_COMPONENTS adc dac dma flash pwr rcc rtc tim)

# Components that have ll_ in names instead of hal_
SET(HAL_LL_COMPONENTS fsmc sdmmc usb)

SET(HAL_PREFIX stm32f2xx_)

SET(HAL_HEADERS
stm32f2xx_hal.h
stm32f2xx_hal_def.h
)

SET(HAL_SRCS
stm32f2xx_hal.c
)
ELSEIF(STM32_FAMILY STREQUAL "F4")
SET(HAL_COMPONENTS adc can cec cortex crc cryp dac dcmi dma dma2d eth flash
flash_ramfunc fmpi2c gpio hash hcd i2c i2s irda iwdg ltdc
nand nor pccard pcd pwr qspi rcc rng rtc sai sd sdram
smartcard spdifrx spi sram tim uart usart wwdg fmc fsmc
SET(HAL_COMPONENTS adc can cec cortex crc cryp dac dcmi dma dma2d eth flash
flash_ramfunc fmpi2c gpio hash hcd i2c i2s irda iwdg ltdc
nand nor pccard pcd pwr qspi rcc rng rtc sai sd sdram
smartcard spdifrx spi sram tim uart usart wwdg fmc fsmc
sdmmc usb)

SET(HAL_REQUIRED_COMPONENTS cortex pwr rcc)

# Components that have _ex sources
SET(HAL_EX_COMPONENTS adc cryp dac dcmi dma flash fmpi2c hash i2c i2s pcd
SET(HAL_EX_COMPONENTS adc cryp dac dcmi dma flash fmpi2c hash i2c i2s pcd
pwr rcc rtc sai tim)

# Components that have ll_ in names instead of hal_
SET(HAL_LL_COMPONENTS fmc fsmc sdmmc usb)

SET(HAL_PREFIX stm32f4xx_)

SET(HAL_HEADERS
stm32f4xx_hal.h
stm32f4xx_hal_def.h
)

SET(HAL_SRCS
stm32f4xx_hal.c
)
Expand Down Expand Up @@ -76,7 +100,7 @@ FOREACH(cmp ${STM32HAL_FIND_COMPONENTS})
LIST(FIND HAL_EX_COMPONENTS ${cmp} STM32HAL_FOUND_INDEX)
IF(NOT (${STM32HAL_FOUND_INDEX} LESS 0))
LIST(APPEND HAL_HEADERS ${HAL_PREFIX}hal_${cmp}_ex.h)
LIST(APPEND HAL_SRCS ${HAL_PREFIX}hal_${cmp}_ex.c)
LIST(APPEND HAL_SRCS ${HAL_PREFIX}hal_${cmp}_ex.c)
ENDIF()
ENDFOREACH()

Expand All @@ -93,7 +117,7 @@ FIND_PATH(STM32HAL_INCLUDE_DIR ${HAL_HEADERS}

FOREACH(HAL_SRC ${HAL_SRCS})
SET(HAL_${HAL_SRC}_FILE HAL_SRC_FILE-NOTFOUND)
FIND_FILE(HAL_${HAL_SRC}_FILE ${HAL_SRC}
FIND_FILE(HAL_${HAL_SRC}_FILE ${HAL_SRC}
PATH_SUFFIXES src stm32${STM32_FAMILY_LOWER}
HINTS ${STM32Cube_DIR}/Drivers/STM32${STM32_FAMILY}xx_HAL_Driver/Src
CMAKE_FIND_ROOT_PATH_BOTH
Expand Down

0 comments on commit bfa90fe

Please sign in to comment.