打印
[活动专区]

【AT-START-L021 测评】Linux下开发环境搭建(cmake+gcc+openocd)与点灯

[复制链接]
181|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
xhackerustc|  楼主 | 2024-12-1 20:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
概述
开发板很漂亮,AT系的经典黑白色搭配。笔者一直是linux下开发,这次也不例外,但相对于以前用cmake+gcc+pyocd组合https://bbs.21ic.com/icview-3374312-1-1.html,这次换个花样,改用cmake+gcc+openocd。pyocd最大的优点是能利用Keil的现成pack,但弱点也很明显不支持板级扩展的nor flash烧写,而openocd在编写适当算法支持后,MCU内置flash和板级扩展的flash都能支持。另外openocd的速度天然比pycod快。一般而言做mcu的openocd flash烧录算法还是需要一番功夫的,好在AT官方其实做了openocd的支持,其代码仓库在
https://github.com/ArteryTek/openocd
clone后编译即可,并不复杂,这里不再赘述

PS:其实笔者有个梦想--openocd一统浆糊。事实上笔者也是这么做的,自己维护了一个openocd仓库,把各家MCU的支持都合在一起了,这样编译出来的openocd可以用于笔者所用到的所有MCU。


Firmware Library
AT32L021的Firmware Library请从雅特力官方下载。

CMakeLists.txt
接下来就是书写CMakeLists.txt了,基于AT32F405改一改就可以用了,只是要注意CPU从cortex-m4变成
了cortex-m0+,除此以外就是一些宏定义,比如AT32F405RCT7_7改成AT32L021C8T7,AT_START_F405_V1改成AT_START_L021_V1,最后做好目录文件的一些变动,最后笔者的点灯用例的CMakeLists如下
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
cmake_minimum_required(VERSION 3.20)

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_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

add_compile_options(-mthumb -mcpu=cortex-m0plus -mfloat-abi=soft)
add_compile_options(-ffunction-sections -fdata-sections -fno-builtin -fno-common -Wall -Wshadow -Wdouble-promotion -Werror -Wno-unused-parameter)

add_compile_options(-O3)

project(at32l021 C ASM)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_STANDARD 99)

include_directories(libraries/cmsis/cm0plus/core_support
                    libraries/cmsis/cm0plus/device_support/
                    libraries/drivers/inc/
                    project/at32l021_board/
                    project/at_start_l021/examples/gpio/led_toggle/inc)

add_definitions(-DAT32L021C8T7 -DAT_START_L021_V1)

file(GLOB_RECURSE SOURCES
                "project/at32l021_board/at32l021_board.c"
                "project/at_start_l021/examples/gpio/led_toggle/src/*.c"
                "libraries/drivers/src/*.c"
                "libraries/cmsis/cm0plus/device_support/system_at32l021.c"
                "libraries/cmsis/cm0plus/device_support/startup/gcc/startup_at32l021.s"
                )

set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/libraries/cmsis/cm0plus/device_support/startup/gcc/linker/AT32L021x8_FLASH.ld)
add_link_options(
                -mthumb -mcpu=cortex-m0plus -mfloat-abi=soft
                -Wl,--gc-sections,--print-memory-usage,-Map,${PROJECT_NAME}.map
                --specs=nano.specs
                --specs=nosys.specs)
add_link_options(-T ${LINKER_SCRIPT})

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)
set(LST_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.lst)
add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -Oihex [        DISCUZ_CODE_51        ]lt;TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
        COMMAND ${CMAKE_OBJCOPY} -Obinary [        DISCUZ_CODE_51        ]lt;TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
        COMMAND ${CMAKE_OBJDUMP} --all-headers --demangle --disassemble [        DISCUZ_CODE_51        ]lt;TARGET_FILE:${PROJECT_NAME}.elf> > ${LST_FILE}
        COMMAND ${SIZE} --format=berkeley [        DISCUZ_CODE_51        ]lt;TARGET_FILE:${PROJECT_NAME}.elf>
)

测试代码
把官方的点灯改一改以测试openocd烧录正常:
int main(void)
{
  system_clock_config();

  at32_board_init();

  uart_print_init(115200);
  printf("Hello AT32L021\r\n");
  while(1)
  {
    at32_led_toggle(LED2);
    delay_ms(200);
    at32_led_toggle(LED3);
    delay_ms(200);
    at32_led_toggle(LED4);
    delay_ms(200);
  }
}



openocd烧录
openocd -f interface/cmsis-dap.cfg -f target/at32l021xx.cfg -c "program build/at32l021.bin verify reset exit 0x8000000"


运行结果
串口有Hello AT32L021打印,三个LED交互闪烁,说明固件成功烧录了。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

22

主题

70

帖子

0

粉丝