完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
一. 开发工具安装
工具安装和使用参考: Linux下使用VSCode,GCC,OpenOCD实现STM32一键编译烧录调试(STM32CubeMX篇) 二. 测试工程简介 此文使用STM32的标准外设库(STM32F4xx_StdPeriph_Driver)编写点灯测试代码,其中有3个文件是从STM32CubeMX篇复制过来的:
三. CMake工具 CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,使用方法参考:CMake tutorial 1. CMakeLists.txt 这里贴出CMake最重要的文件CMakeLists.txt cmake_minimum_required(VERSION 3.0) project(test02) #以下用于屏蔽 error: unrecognized command line option ‘-rdynamic’ set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") #指定编译工具 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(CMAKE_SIZE "arm-none-eabi-size") #编译相关选项 set(MCU_FLAGS "-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16") set(CMAKE_C_FLAGS "${MCU_FLAGS} -Wall -Wno-unknown-pragmas") #-w -Wall set(CMAKE_C_FLAGS_DEBUG "-O0 -g2 -ggdb") set(CMAKE_C_FLAGS_RELEASE "-O3") set(CMAKE_BUILD_TYPE "Debug") #set(CMAKE_BUILD_TYPE "Release") #设置宏定义,对应MDK里Target Options里的选项 add_definitions(-DUSE_STDPERIPH_DRIVER -DSTM32F427X) #设置工程包含当前目录 #set(CMAKE_INCLUDE_CURRENT_DIR ON) #设置头文件包含目录 include_directories( . CMSIS CMSIS/Include STM32F4xx_StdPeriph_Driver/inc ) #startup文件是STM32CubeMX生成的 ENABLE_LANGUAGE(ASM) set(SRC_STARTUP "CMSIS/startup_stm32f427xx.s") aux_source_directory(. SRC_MAIN) aux_source_directory(CMSIS/ SRC_CMSIS) aux_source_directory(STM32F4xx_StdPeriph_Driver/src/ SRC_STD_LIB) #连接生成,ld文件是STM32CubeMX生成的 set(LINKER_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/STM32F427VGTx_FLASH.ld") set(CMAKE_EXE_LINKER_FLAGS "-specs=nosys.specs -T${LINKER_SCRIPT} -lc -lm -lnosys -Wl,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map,--cref -Wl,--gc-sections" ) #生成可执行文件 add_executable(${PROJECT_NAME}.elf ${SRC_STARTUP} ${SRC_MAIN} ${SRC_CMSIS} ${SRC_STD_LIB}) set(ELF_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.elf) 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_BUILD COMMAND ${CMAKE_OBJCOPY} -Obinary ${ELF_FILE} ${BIN_FILE} COMMAND ${CMAKE_OBJCOPY} -Oihex ${ELF_FILE} ${HEX_FILE} COMMENT "Building ${PROJECT_NAME}.bin and ${PROJECT_NAME}.hex" COMMAND ${CMAKE_COMMAND} -E copy ${HEX_FILE} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.hex" COMMAND ${CMAKE_COMMAND} -E copy ${BIN_FILE} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.bin" COMMAND ${CMAKE_SIZE} --format=berkeley ${PROJECT_NAME}.elf ${PROJECT_NAME}.hex COMMENT "Invoking: Cross ARM GNU Print Size" ) 有几点需要注意:
进入build目录cd build然后输入cmake ..,成功生成Makefile 3. make编译 在build目录直接执行make命令,开始编译并生成可执行文件 然后再利用openocd烧录进STM32板子验证即可,具体操作参考STM32CubeMX篇 四. json脚本实现一键编译烧录调试 1. launch.json 内容与STM32CubeMX篇一样 { // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Cortex Debug", "cwd": "${workspaceRoot}", "executable": "./build/test01.elf", "request": "launch", "type": "cortex-debug", "servertype": "openocd", "configFiles": [ "interface/stlink-v2.cfg", "target/stm32f4x.cfg" ], "armToolchainPath": "/opt/gcc-arm-none-eabi-10-2020-q4-major/bin", "svdFile": "STM32F427x.svd", "preLaunchTask": "Build" } ] } 2. task.json 这里与STM32CubeMX篇不同,用到了两条指令 { // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "options": { "cwd": "${workspaceFolder}/build" }, "tasks": [ { "label": "cmake", "type": "shell", "command": "cmake", "args": [".."] }, { "label": "make", "type": "shell", "command": "make", }, { "label": "Build", "dependsOrder": "sequence", "dependsOn":[ "cmake", "make" ], } ] } 3. 一键编译调试 按F5直接自动编译调试 |
|
|
|
只有小组成员才能发言,加入小组>>
3280 浏览 9 评论
2958 浏览 16 评论
3460 浏览 1 评论
9004 浏览 16 评论
4052 浏览 18 评论
1115浏览 3评论
573浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
571浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2303浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1859浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-25 12:19 , Processed in 1.394253 second(s), Total 79, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号