本帖最后由 sujingliang 于 2024-11-24 21:08 编辑
AT-START-L021 SDK中好像没有提供gcc+vscode+cmake开发环境相关配置。所以...
用过gcc+vscode+cmake组合的开发环境,一般就不太爱用keil、IAR,因为实在是太香了。
最早可能为了编译AI-M61或是PICO安装了各个软件和扩展,所以已经配置好了环境。
要安装哪些软件记得可能不太清楚:VSCODE、CMAKE、mingw、nijia,gcc多个版本,还有VSCODE扩展C/C++、CMake、CMakeTools。
这方面网上相关资料比较多,安装相对复杂,要花一些时间,但安装一次一劳永逸。
如果在环境就绪的情况下,面对一个新的SDK,其实就是写CMakeLists.txt的工作啦。
CMakeLists就是一个生成makefile的描述文件,比直接写makefile方便一些。
1、在AT32L021_Firmware_Library_V2.0.5下建一个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(AT32L021_DEMO C CXX ASM)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)
# add_compile_options(-mcpu=cortex-m33 -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(-DUSE_STDPERIPH_DRIVER -DCUSTOM_HSE_VAL)
add_subdirectory(project/at_start_l021/examples/gpio/led_toggle)
前面的内容各个SDK基本都一样,
最后一行
add_subdirectory(project/at_start_l021/examples/gpio/led_toggle)
将led_toggle加入cmake管理,如果想编译其他demo,也需要做add_subdirectory这个操作。
2、在project/at_start_l021/examples/gpio/led_toggle下建一个CMakeLists.txt,内容如下
cmake_minimum_required(VERSION 3.20)
# 获取当前源目录的绝对路径
set(CURRENT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
# 提取当前源目录的名称(即最后一个斜杠/或反斜杠\之后的部分)
get_filename_component(CURRENT_DIR_NAME ${CURRENT_SOURCE_DIR} NAME)
#project(gpio_basic)
project(${CURRENT_DIR_NAME})
add_compile_options(-mcpu=cortex-m0 -mthumb -mthumb-interwork)
add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0)
#添加宏定义
add_definitions(-DUSE_STDPERIPH_DRIVER -DCUSTOM_HSE_VAL)
set(PrjDirPath ${CMAKE_CURRENT_SOURCE_DIR})
#添加头文件路径,即.h文件
include_directories(
${PrjDirPath}/../../../../../libraries/drivers/inc
${PrjDirPath}/../../../../../libraries/cmsis/cm0plus/core_support
${PrjDirPath}/../../../../../libraries/cmsis/cm0plus/device_support
${PrjDirPath}/../../../../../project/at32l021_board
${PrjDirPath}/.
${PrjDirPath}/inc
)
#添加源文件路径,即.c或者.s文件
file(GLOB SOURCES
${PrjDirPath}/../../../../../libraries/cmsis/cm0plus/device_support/*.c
${PrjDirPath}/../../../../../libraries/cmsis/cm0plus/device_support/startup/gcc/*.s
${PrjDirPath}/../../../../../libraries/drivers/src/*.c
${PrjDirPath}/../../../../../project/at32l021_board/*.c
${PrjDirPath}/src/*.c
)
#AT32L021x8_FLASH.ld的连接脚本路径
set(LINKER_SCRIPT ${PrjDirPath}/../../../../../libraries/cmsis/cm0plus/device_support/startup/gcc/linker/AT32L021x8_FLASH.ld)
add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map)
#选择cortex-m3内核
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})
target_compile_definitions(${PROJECT_NAME}.elf
PRIVATE AT32L021C8T7
PRIVATE USE_STDPERIPH_DRIVER
PRIVATE AT_START_L021_V1
)
target_link_libraries(${PROJECT_NAME}.elf
-Wl,--start-group
debug m
debug c
debug gcc
debug nosys
optimized m
optimized c
optimized gcc
optimized nosys
-Wl,--end-group
)
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_221 ]lt;TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
COMMAND ${CMAKE_OBJCOPY} -Obinary [ DISCUZ_CODE_221 ]lt;TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")
主要是配置好头文件和源文件的路径,和KEIL相比有一个好处是用通配符就可以包括一个目录下的所有*.c文件。
还要配置好ld的连接脚本路径,gcc下的ld相当于keil下用sct。
3、在AT32L021_Firmware_Library_V2.0.5下右键“通过 code打开”
或者在命令行下键入code . 进入VSCODE
因为目录中CMakeLists.txt的存在,导致nijia会自动配置环境,一般自动配置的过程都会失败。
可以打开扩展CMake Tools,进行定制化配置。配置编译工具,生成的目标等
配置编译工具,
选择编译目标
按下面这个按钮编译
如果正常,会出现如下信息。
4、关于烧录
可以选择的烧录手段:
1)雅特力官方提供的图形化烧录工具。
2)第三方烧录工具,没试过,可能power writer可以。
3)openocd
4)pyocd
|