0 预备环境
1. 正常使用 gcc 的环境编译可以运行
2.STM32CubeMX 可以生成 SW4STM32
注意:一定要是 SW4STM32 ,不然后期在 Clion 中无法进行编译
SW4STM32 在新版本中已经取消,这里提供一个老的版本
链接:https://pan.baidu.com/s/1NPl9mFzq8jIvHnfeq4cPZg?pwd=vpck
提取码:vpck
3. 这里使用的是 stlink 烧录的
4. 参考视频:https://www.bilibili.com/video/BV1z44y1t7Lg/?share_source=copy_web&vd_source=08ffec12a130bb99e9aac8670b5b9c2f
5. 上面视频的资料:https://share.weiyun.com/KdkTFRnp
1 配置 ARMCLANG 环境
Keil_v5 的安装路径,进入 ARM 文件夹
打开 ARMCLANG 文件夹
打开里面的 bin 目录,并复制其地址
在系统环境变量中配置
打开【此电脑】→右键【属性】→【高级系统设置】→【环境变量】
在 path 中添加刚才的地址
2 使用 CubeMX 新建工程
此处省略 -----
只需要在最后的 Toolchain / IDE 中选择 SW4STM32 即可
3 使用 Clion 打开,修改 CMakeLists.txt
这里会让你选择 board, 选择下图所示的这个(如果后面烧录出错的话,里面可能需要更改)【如果是 F4 则选择 f4 对应的】
打开 CMakeLists.txt 文件
这一部分是 CMakeLists.txt 默认生成的,这里需要和你自己的保持一致,所以复制粘贴替换的时候,要从这一部分之下开始,把这一部分之下的内容全部替换成下面的程序
3.1 STM32F1 系列替换文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 set (PROJECT_NAME demo)set (CMAKE_SYSTEM_PROCESSOR Cortex-M3)set (CMAKE_C_COMPILER armclang.exe)set (CMAKE_C_COMPILER_WORKS TRUE )set (CMAKE_CXX_COMPILER armclang.exe)set (CMAKE_CXX_COMPILER_WORKS TRUE )set (CMAKE_ASM_COMPILER armclang.exe)set (CMAKE_ASM_COMPILER_WORKS TRUE )set (CMAKE_C_LINK_EXECUTABLE armlink.exe)set (CMAKE_ASM_LINK_EXECUTABLE armlink.exe)set (CMAKE_CXX_LINK_EXECUTABLE armlink.exe)set (SECTIONS_SCRIPT_PATH ${CMAKE_HOME_DIRECTORY} /MDK-ARM/${PROJECT_NAME} /${PROJECT_NAME} .sct)set (C_TARGET_FLAG --target =arm-arm-none-eabi) set (ASM_TARGET_FLAG --target =arm-arm-none-eabi) set (LINKER_TARGET_FLAG --cpu=${CMAKE_SYSTEM_PROCESSOR} ) set (COMPILE_RULE_FLAG "-mcpu=${CMAKE_SYSTEM_PROCESSOR}" )set (CMAKE_C_FLAGS_INIT "${C_TARGET_FLAG} ${COMPILE_RULE_FLAG} \ -xc -std=c99 -fno-rtti -funsigned-char -fshort-enums -fshort-wchar \ -gdwarf-3 -Oz -ffunction-sections -w" )set (CMAKE_ASM_FLAGS_INIT "${ASM_TARGET_FLAG} ${COMPILE_RULE_FLAG} \ -masm=auto -c -gdwarf-3 " )set (CMAKE_EXE_LINKER_FLAGS_INIT " \ ${LINKER_TARGET_FLAG} \ --strict \ --scatter ${SECTIONS_SCRIPT_PATH} \ --info sizes --info totals --info unused --info veneers \ --summary_stderr \ --info summarysizes" ) project (demo C CXX ASM)set (CMAKE_CXX_STANDARD 11 )set (CMAKE_C_STANDARD 99 )include_directories (App Core/Inc RT-Thread Drivers/STM32F1xx_HAL_Driver/Inc/Legacy Drivers/STM32F1xx_HAL_Driver/Inc Drivers/CMSIS/Device/ST/STM32F1xx/Include Drivers/CMSIS/Include Middlewares/Third_Party/RealThread_RTOS/include / Middlewares/Third_Party/RealThread_RTOS/bsp/_template/cubemx_config)add_definitions (-DUSE_HAL_DRIVER -DSTM32F103xB)file (GLOB_RECURSE SOURCES "startup/*.*" "RT-Thread/*.*" "Middlewares/*.*" "Drivers/*.*" "Core/*.*" App)file (GLOB_RECURSE SOURCES ${SOURCES} "MDK-ARM/startup_stm32f103xb.s" )file (GLOB_RECURSE EXCLUDE_SRCS "Middlewares/Third_Party/RealThread_RTOS/libcpu/arm/cortex-m3/context_gcc.S" "startup/*.*" "Core/Src/syscalls.c" "STM32F103C8Tx_FLASH.ld" ) list (REMOVE_ITEM SOURCES ${EXCLUDE_SRCS} )add_executable (${PROJECT_NAME} ${SOURCES} ${LINKER_SCRIPT} )set (HEX_FILE ${PROJECT_BINARY_DIR} /${PROJECT_NAME} .hex)set (BIN_FILE ${PROJECT_BINARY_DIR} /${PROJECT_NAME} .bin)set (ELF_FILE ${PROJECT_BINARY_DIR} /${PROJECT_NAME} .elf)set (ARMCC_fromelf fromelf.exe)add_custom_command (TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${ARMCC_fromelf} --i32combined --output="${HEX_FILE}" "${ELF_FILE}" COMMENT "Building ${HEX_FILE}" )
3.2 STM332F4 系列替换文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 set (CMAKE_SYSTEM_NAME Generic)set (CMAKE_SYSTEM_VERSION 1 )cmake_minimum_required (VERSION 3.24 )set (PROJECT_NAME testF4)set (CMAKE_SYSTEM_PROCESSOR Cortex-M4)set (CMAKE_C_COMPILER armclang.exe)set (CMAKE_C_COMPILER_WORKS TRUE )set (CMAKE_CXX_COMPILER armclang.exe)set (CMAKE_CXX_COMPILER_WORKS TRUE )set (CMAKE_ASM_COMPILER armclang.exe)set (CMAKE_ASM_COMPILER_WORKS TRUE )set (CMAKE_C_LINK_EXECUTABLE armlink.exe)set (CMAKE_ASM_LINK_EXECUTABLE armlink.exe)set (CMAKE_CXX_LINK_EXECUTABLE armlink.exe)set (SECTIONS_SCRIPT_PATH ${CMAKE_HOME_DIRECTORY} /MDK-ARM/${PROJECT_NAME} /${PROJECT_NAME} .sct)set (C_TARGET_FLAG --target =arm-arm-none-eabi) set (ASM_TARGET_FLAG --target =arm-arm-none-eabi) set (LINKER_TARGET_FLAG --cpu=${CMAKE_SYSTEM_PROCESSOR} ) set (COMPILE_RULE_FLAG "-mcpu=${CMAKE_SYSTEM_PROCESSOR} -mfpu=fpv4-sp-d16 -mfloat-abi=hard -c " )set (CMAKE_C_FLAGS_INIT "${C_TARGET_FLAG} ${COMPILE_RULE_FLAG} \ -xc -std=c99 -fno-rtti -funsigned-char -fshort-enums -fshort-wchar \ -gdwarf-4 -O1 -ffunction-sections -w" )set (CMAKE_ASM_FLAGS_INIT "${ASM_TARGET_FLAG} ${COMPILE_RULE_FLAG} \ -masm=auto -c -gdwarf-4 " )set (CMAKE_EXE_LINKER_FLAGS_INIT " \ ${LINKER_TARGET_FLAG} \ --strict \ --scatter ${SECTIONS_SCRIPT_PATH} \ --info sizes --info totals --info unused --info veneers \ --summary_stderr \ --info summarysizes" ) project (testF4 C CXX ASM)set (CMAKE_CXX_STANDARD 11 )set (CMAKE_C_STANDARD 99 )include_directories (Core/Inc Drivers/STM32F4xx_HAL_Driver/Inc Drivers/STM32F4xx_HAL_Driver/Inc/Legacy Drivers/CMSIS/Device/ST/STM32F4xx/Include Drivers/CMSIS/Include )add_definitions (-DUSE_HAL_DRIVER -DSTM32F407xx)file (GLOB_RECURSE SOURCES "startup/*.*" "Drivers/*.*" "Core/*.*" )file (GLOB_RECURSE SOURCES ${SOURCES} "MDK-ARM/startup_stm32f407xx.s" )file (GLOB_RECURSE EXCLUDE_SRCS "Middlewares/Third_Party/RealThread_RTOS/libcpu/arm/cortex-m3/context_gcc.S" "startup/*.*" "Core/Src/syscalls.c" "STM32F103C8Tx_FLASH.ld" ) list (REMOVE_ITEM SOURCES ${EXCLUDE_SRCS} )add_executable (${PROJECT_NAME} ${SOURCES} ${LINKER_SCRIPT} )set (HEX_FILE ${PROJECT_BINARY_DIR} /${PROJECT_NAME} .hex)set (BIN_FILE ${PROJECT_BINARY_DIR} /${PROJECT_NAME} .bin)set (ELF_FILE ${PROJECT_BINARY_DIR} /${PROJECT_NAME} .elf)set (ARMCC_fromelf fromelf.exe)add_custom_command (TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${ARMCC_fromelf} --i32combined --output="${HEX_FILE}" "${ELF_FILE}" COMMENT "Building ${HEX_FILE}" )
3.3 替换文件需要注意的事项
根据自己项目的不同需要更改的地方
4 使用 CubeMX 生成 MDK 文件
打开 CubeMX,如果你之前的关了,那么从下面这里进入
将 SW4STM32 换成 MDK,然后生成
Keil 打开生成的工程,并进行编译(这里编译环境要看一下,是不是 6.** 以上的,如果使用 AC5 是不行的)
编译运行一下,然后就可以关掉 Keil 了
5 Clion 编译运行测试
重新加载一下 CMakeLists
如果这里没有出现锤子,那么就进去更换一下文件
打开后,点一下,它会自动让你选择正确的,默认的是带.elf 的,这里就是选一个不带.elf 的
点击锤子开编译
点击三角,进行烧录运行
如果出现错误,需要去找一下 st_nucleo_f103rb.cfg 的位置,并打开
再次烧录就可以了