#申请原创# @21小跑堂
【前言】
AT32L021目前的开发有很多种可以选择,使用Keil 、IAR是传统的选择,官方提供了示例工程,大多也是基于Keil创建的。当然也可以使用AT32 IDE进行开发,高度的订制,使用起来也是非常的方便。这些都是可以在win、linux下可行的,但是基于mac os下面就不行了。雅特力官网也提供了vscode来搭建环境的教程,但是没有给出at32l021系列的源文件下载。所以我下大力气,搭建好了vscode + cmake的环境。现在分享如下:
【工具】
1、vscode
2、openocd
3、arm-none-eabi
4、cmake
5、AT32 IDE
6、AT32_Work_Bench_V1.1.03
具体的以上工具创建,网上例程非常多,这里不做详细讲解,但是在win下面需要在环境配置中进行配置。我这里的路径配置如下:
在这里需要注意一下,如果是自己安装好的openocd,他的目录里没有at32l02xx.cfg这个配置文件,会报错:Error: flash driver 'at32l0xx' not found
我的解决办法是后面安装了一下雅特力的AT32 IDE,这里他的安装目录里就有openocd,我直接把环境配置给了这个目录后,就解决问题了。
【移植步聚】
1、打开AT32_Work_Bench,创建一个基于at32l021的基础工程,在生成代码时选择生成eclipse_gcc。
2、使用vscode打开工程文件夹。创建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
)
# c文件
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 [ DISCUZ_CODE_278 ]lt;TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
COMMAND ${CMAKE_OBJCOPY} -Obinary [ DISCUZ_CODE_278 ]lt;TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")
这个文件主要是要把所需要包含的头文件、.c源文件、ld、.s添加进来,以后工程需要增加文件,在这两个目录添加路径就行了。
配置好CMakeLists.txt后,保存,系统就可以自动生成build文件夹。
【配置文件】
在文件夹内增加lunch.json来添加调试的配置文件:
<div style="color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div>{</div><div> <span style="color: #6a9955;">//"version": "0.2.0",</span></div><div> <span style="color: #9cdcfe;">"configurations"</span>: [</div><div> {</div><div> <span style="color: #9cdcfe;">"cwd"</span>: <span style="color: #ce9178;">"${workspaceRoot}"</span>,</div><div> <span style="color: #9cdcfe;">"type"</span>: <span style="color: #ce9178;">"cortex-debug"</span>,</div><div> <span style="color: #9cdcfe;">"request"</span>: <span style="color: #ce9178;">"launch"</span>,</div><div> <span style="color: #9cdcfe;">"name"</span>: <span style="color: #ce9178;">"openocd-launch"</span>,</div><div> <span style="color: #9cdcfe;">"armToolchainPath"</span>: <span style="color: #ce9178;">"C:/gcc-arm-none-eabi/bin"</span>,</div><div> <span style="color: #9cdcfe;">"gdbPath"</span>: <span style="color: #ce9178;">"C:/gcc-arm-none-eabi/bin/arm-none-eabi-gdb.exe"</span>,</div><div> <span style="color: #9cdcfe;">"interface"</span>: <span style="color: #ce9178;">"swd"</span>,</div><div> <span style="color: #9cdcfe;">"servertype"</span>: <span style="color: #ce9178;">"openocd"</span>,</div><div> <span style="color: #9cdcfe;">"configFiles"</span>: [</div><div> <span style="color: #ce9178;">"D:/AT32IDE/OpenOCD/scripts/interface/atlink.cfg"</span>,</div><div> <span style="color: #ce9178;">"D:/AT32IDE/OpenOCD/scripts/target/at32l021xx.cfg"</span>,</div><div> ],</div><div> <span style="color: #9cdcfe;">"executable"</span>: <span style="color: #ce9178;">"./build/Project.elf"</span>,</div><div> <span style="color: #9cdcfe;">"svdFile"</span>: <span style="color: #ce9178;">"./project/misc/AT32L021xx_v2.svd"</span>,</div><div> <span style="color: #9cdcfe;">"runToEntryPoint"</span>: <span style="color: #ce9178;">"main"</span>,</div><div> },</div><div> ]</div><div>}</div></div>
这里的AT32L021xx_v2.svd需要从AT32IDE的工具里面复制出来。他的目录是在D:\AT32IDE\repo\SVD下面
编写下载任务:
{
// 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"
}
]
}
"label": "download code任务,主要是指定at32l021xx.cfg、atlink.cfg、Project.hex的路径。
c_cpp_properities.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
}
这样所有的工程就解了,点击“生成”就可以编译固件:
运行任务openocd-lauch就可以进入调试:
【总结】
在雅特力的官网有如何使用vscode创建开发环境的教程,使用的是makfile来编写,我这次使用的是Cmake来创建,相比makeflile,Cmake相对比较简单一些。
经过差不多一个星期的学习,多次试验,终于把AT32L021在vscode 下的环境创建好了。这里也给雅特力的技术支持提个建议,把他的.cfg、svd.s也打包出来,方便vscode + Cmake(makefile)的用户来创建环境。
这次环境创建好后,可以方便的在linux、macOS跨平台进行开发,是非常有意义的!
附工程源码:
AT32L021C8T7_LED1.zip
(1.57 MB)
|