【前言】
在MCU的开发中,目前使用最多的是keil这个王牌工具,目前也有各个厂商给自己开发了自己的传用IDE,知名的是意法半导体的STM32Cube生态系统。象国外的恩智浦、英飞凌,PIC、TI等等都有自己的IDE。使用厂商订制的IDE,开发起来是非常方便,但是我象我这样经常使用不同的厂商的MCU进行开发,电脑装满了两个IDE,特别象STM32的固件包,那是动不动就好几个G。最近准备转到mac OS下面开发,所以只有选择vscode + makefile或Cmake来建搭开发环境。所以搭建vscode的跨平台开发环境就特别的重要,在网上也看到了许多的这方面的文章,通过学习我就国产的雅特力最新出的AT32L021为模版来写了一个开发环境。在此分享如下:
【工具的安装】
1、vscode ,这当我是基础了,安装教程非常多,安装也不是很难。
2、openocd,这款工具是下载、调试的工具,当然还有pyocd可选,但是pyocd的调试速度相比openocd要慢很多。但是openocd支持的芯片比较少,需要厂商提供.cfg来支持,当然高手也可以参照芯片手册自己来写。基于eclipse的IDE也基于上是基于openocd来进行调试下载的。象AT32IDE,STM32CubeIDE也都是使用openocd来下载的。
3、arm-none-eabi
4、cmake (如果使用makefile,就需要安装make)
安装好工具后,需要把以上的软件添加到环境变量中去,这里以win11为例
【vscode工程文件】
我这里使用了AT32_Work_Bench_V1.1.03 生成的工程,使用vscode打开他的工程。
1、编写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.20) # 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 settings project(Project 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-m0 -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") if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release") message(VERBOSE "Maximum optimization for speed") add_compile_options(-Ofast) elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo") message(VERBOSE "Maximum optimization for speed, debug info included") add_compile_options(-Ofast -g) elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel") message(VERBOSE "Maximum optimization for size") add_compile_options(-Os) else () message(VERBOSE "Minimal optimization, debug info included") add_compile_options(-Og -g) endif () add_definitions(-DAT32L021C8T7 -DAT_START_L021_V1) #ͷ ļ include_directories( ./project/inc ./libraries/cmsis/cm0plus/core_support ./libraries/cmsis/cm0plus/device_support ./libraries/drivers/inc ) file(GLOB_RECURSE SOURCES ./project/src/*.c ./libraries/drivers/src/*.c ./libraries/cmsis/cm0plus/device_support/*.c ./project/Eclipse_gcc/AT32L021x8_FLASH.ld ./project/Eclipse_gcc/startup_at32l021.s ) set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/project/Eclipse_gcc/AT32L021x8_FLASH.ld) add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map) add_link_options(-mcpu=cortex-m0 -mthumb -mthumb-interwork) add_link_options(-T ${LINKER_SCRIPT}) add_link_options(-specs=nano.specs -specs=nosys.specs -u _printf_float) add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT}) 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} -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}")
2、编写配置文件:
launch.json:
{ //"version": "0.2.0", "configurations": [ { "cwd": "${workspaceRoot}", "type": "cortex-debug", "request": "launch", "name": "openocd-launch", "armToolchainPath": "C:/gcc-arm-none-eabi/bin", "gdbPath": "C:/gcc-arm-none-eabi/bin/arm-none-eabi-gdb.exe", "interface": "swd", "servertype": "openocd", "configFiles": [ "D:/AT32IDE/OpenOCD/scripts/interface/atlink.cfg", "D:/AT32IDE/OpenOCD/scripts/target/at32l021xx.cfg", ], "executable": "./build/Project.elf", "svdFile": "./project/misc/AT32L021xx_v2.svd", "runToEntryPoint": "main", }, ] }
task.json
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "make", "args": [ "-j12" ], "problemMatcher": [] }, { "label": "clean", "type": "shell", "command": "make", "args": [ "clean" ], "problemMatcher": [] }, { "label": "download code", "type": "process", "command": "openocd", "args": [ "-f", "D:/AT32IDE/OpenOCD/scripts/interface/atlink.cfg", "-f", "D:/AT32IDE/OpenOCD/scripts/target/at32l021xx.cfg", "-c", "program build/Project.hex", "-c", "program build/Project.elf verify reset exit" ], "group": "build" } ] }
c_cpp_properties.json:
{ "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}/**" ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE" ], "compilerPath": "C:\\gcc-arm-none-eabi\\bin\\arm-none-eabi-gcc.exe", "cStandard": "c17", "cppStandard": "gnu++14", "intelliSenseMode": "gcc-arm", "configurationProvider": "ms-vscode.cmake-tools" } ], "version": 4 }
建好这些文件后,保存文件,Cmake就会自动配置工程,这样就可以进行烧录下载了:
调试:
【总结】
在跨平台开发,使用vscode+cmake是最好的选择组合工具。
分享工程源码如下: