Step1 准备软件,安装环境:
1. cubemx v6.5.0(可以兼容以前版本的project)
https://www.st.com.cn/zh/development-tools/stm32cubemx.html
STM32CubeMX 默认安装目录, 6.5版本可以兼容老版本
C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeMX
打开后选择help => 更新Firmware
里面的updater settings
reflash data
manage imbeded software packages 保留最新的一个
2. openocd
openocd-20231002.7z
https://gnutoolchains.com/arm-eabi/openocd/
解压缩到 D:\service\stm32\OpenOCD-20231002-0.12.0
3. arm-none-eabi
gcc-arm-none-eabi-5_4-2016q3-20160926-win32.zip
https://launchpad.net/gcc-arm-embedded/+download
解压缩到 D:\service\stm32\gcc-arm-none-eabi-5_4-2016q3-20160926-win32
4. Mingx64
ref: https://blog.csdn.net/m0_52559040/article/details/131603782
x86_64-13.2.0-release-posix-seh-ucrt-rt_v11-rev0.7z
https://github.com/niXman/mingw-builds-binaries/releases
- posix:这些压缩包适用于符合POSIX标准的操作系统,如Linux、UNIX等。
(我由于Linux和Windows经常切着用,所以选择posix,如果你只用Windows开发,那么选win32即可)
解压缩到 D:\service\stm32\mingw64
----------------------------------------------------------------------
Step2 环境变量:
搜索 view advanced system settings
打开 高级系统设置 =>环境变量
ARM_NONE_EABI_HOME D:\service\stm32\arm-gnu-toolchain-13.2.Rel1-mingw-w64-i686-arm-none-eabi\arm-none-eabi
OpenOCD_HOME D:\service\stm32\OpenOCD-20231002-0.12.0
MINGW64_HOME D:\service\stm32\mingw64
path add the following:
%ARM_NONE_EABI_HOME%\bin
%OpenOCD_HOME%\bin
%MINGW64_HOME%\bin
----------------------------------------------------------------------
Step3: 验证
gcc -v
arm-none-eabi-gcc -v
----------------------------------------------------------------------
Step4 使用CubeMX创建工程
File 配置完成后
Project => Generate Code => Application Structure 选择Basic 不要选择Advanced
Toolchain/IDE =>SW4STM32(6.5以后的版本可能改成了Cmake)取消 generate under root
注意底下的Firmware Package Name and Version会是刚刚manage imbeded software packages保留的那个
Generate Code
新版本可能会要求登录后才能下载
---------------------------------------------------
Step5 用Clion打开项目,设定Clion的环境
在项目档案里右键 Open Folder as Clion Project
File =>Settings=>Build,Execution,Deployment=> Toolchains
Toolset: D:\service\stm32\mingw64
Build Tool: D:\service\stm32\mingw64\bin\mingw32-make.exe
C Compiler: D:\service\stm32\mingw64\bin\gcc.exe
C++ Compiler: D:\service\stm32\mingw64\bin\g++.exe
Debugger: D:\service\stm32\gcc-arm-none-eabi-5_4-2016q3-20160926-win32\bin\arm-none-eabi-gdb.exe
File =>Settings=>Build,Execution,Deployment=> Embedded Development
OpenOCD Location:D:\service\stm32\OpenOCD-20231002-0.12.0\bin\openocd.exe
Stm32CubeMax Location:
C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeMX\STM32CubeMX.exe
--------------------------------------------------------------------------------------------
Step6 在Clion里建立不会被覆盖的文件夹Core(之前的 Application Structure选择Basic)
在项目根目录建立Core文件夹,里面建立子文件夹CMakeFile,Config,Inc,Src
将根目录Src和Inc的档案移动到Core里
在根目录建立Middlewares,将FreeRTOS文件夹放入
将下面的CMakeLists.txt放入CMakeFile里,注意修改,其中.ld档案的路径会随着CubeMax的版本不同而改变
CMakelists.txt 范例
#THIS FILE IS AUTO GENERATED FROM THE TEMPLATE! DO NOT CHANGE!
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
cmake_minimum_required(VERSION 3.23)# specify cross-compilers and tools
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
set(CMAKE_AR arm-none-eabi-ar)
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_OBJDUMP arm-none-eabi-objdump)
set(SIZE arm-none-eabi-size)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)# 这里不要重新定义PROJECT_SOURCE_DIR,要另外立一个新的名字,PROJECT_ROOT_DIR
set(PROJECT_ROOT_DIR "D:/wks_freeRTOS/mininal_h743_stm32BK0510/mininal_h743_stm32")
set(PROJECT_BINARY_DIR "${PROJECT_ROOT_DIR}/cmake-build-debug")# project settings
project(mininal_h743_stm32 C CXX ASM)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)#Uncomment for hardware floating point
add_compile_definitions(ARM_MATH_CM4;ARM_MATH_MATRIX_CHECK;ARM_MATH_ROUNDING)
add_compile_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16)
add_link_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16)#Uncomment for software floating point
#add_compile_options(-mfloat-abi=soft)add_compile_options(-mcpu=cortex-m7 -mthumb -mthumb-interwork)
add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0)# uncomment to mitigate c++17 absolute addresses warnings
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-register")# Enable assembler files preprocessing
add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-x$<SEMICOLON>assembler-with-cpp>)if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")message(STATUS "Maximum optimization for speed")add_compile_options(-Ofast)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")message(STATUS "Maximum optimization for speed, debug info included")add_compile_options(-Ofast -g)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")message(STATUS "Maximum optimization for size")add_compile_options(-Os)
else ()message(STATUS "Minimal optimization, debug info included")add_compile_options(-Og -g)
endif ()include_directories(# Driver${PROJECT_ROOT_DIR}/Drivers/STM32H7xx_HAL_Driver/Inc${PROJECT_ROOT_DIR}/Drivers/STM32H7xx_HAL_Driver/Inc/Legacy${PROJECT_ROOT_DIR}/Drivers/CMSIS/Device/ST/STM32H7xx/Include${PROJECT_ROOT_DIR}/Drivers/CMSIS/Include# Middlewares${PROJECT_ROOT_DIR}/Middlewares${PROJECT_ROOT_DIR}/Middlewares/FreeRTOS/Source/include${PROJECT_ROOT_DIR}/Middlewares/FreeRTOS/Source/portable/GCC/ARM_CM4F${PROJECT_ROOT_DIR}/Middlewares/canfestival/include${PROJECT_ROOT_DIR}/Middlewares/canfestival/src${PROJECT_ROOT_DIR}/Middlewares/canfestival/include/cm4${PROJECT_ROOT_DIR}/Middlewares/canfestival/include/win32${PROJECT_ROOT_DIR}/Middlewares/canfestival/include/AVR${PROJECT_ROOT_DIR}/Middlewares/canfestival/include/none${PROJECT_ROOT_DIR}/Middlewares/can-slave${PROJECT_ROOT_DIR}/Middlewares/delay# Config${PROJECT_ROOT_DIR}/Core/Config/${PROJECT_ROOT_DIR}/Core/Config/bsp${PROJECT_ROOT_DIR}/Core/Config/bsp/tim${PROJECT_ROOT_DIR}/Core/Config/bsp/usart${PROJECT_ROOT_DIR}/Core/Config/bsp/can${PROJECT_ROOT_DIR}/Core/Config/system# My Inc${PROJECT_ROOT_DIR}/Core/Inc${PROJECT_ROOT_DIR}/Core/Inc/bsp${PROJECT_ROOT_DIR}/Core/Inc/bsp/tim/common)add_definitions(-DUSE_HAL_DRIVER -DSTM32H743xx)file(GLOB_RECURSE SOURCES# driver"${PROJECT_ROOT_DIR}/startup/*.*""${PROJECT_ROOT_DIR}/Drivers/*.*"# Middlewares"${PROJECT_ROOT_DIR}/Middlewares/canfestival/*.*"
# "${PROJECT_ROOT_DIR}/Middlewares/canfestival_original/*.*""${PROJECT_ROOT_DIR}/Middlewares/can-slave/*.*""${PROJECT_ROOT_DIR}/Middlewares/delay/*.*"# My Config"${PROJECT_ROOT_DIR}/Core/Config/*.*"# My Src"${PROJECT_ROOT_DIR}/Core/Inc/*.*""${PROJECT_ROOT_DIR}/Core/Src/*.*")#这里修改过
set(LINKER_SCRIPT ${PROJECT_ROOT_DIR}/STM32H743IITx_FLASH.ld)add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map)
add_link_options(-mcpu=cortex-m7 -mthumb -mthumb-interwork)
add_link_options(-T ${LINKER_SCRIPT})add_executable(${PROJECT_NAME}.elf${SOURCES}${LINKER_SCRIPT})#这里重定义过PROJECT_BINARY_DIR
set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILDCOMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")
--------------------------------------------------------------------------------------------
Step 7 Run
右上角 Edit Configuration
新建 OpenOCD Download & Run
Target: 项目名称
Executable binary: 项目名称
Debugger: arm-none-eabi-gdb.exe
Board config file: 点击Assist 选择最接近的,以h743为例,选择st_nucleo_h743zi.cfg
选择Copy to Project & Use 将文件拷贝到项目根目录,点击OK
连接板子,打开电源,Run