本帖最后由 xhackerustc 于 2024-4-28 00:57 编辑
#申请原创# @21小跑堂
谢谢二姨家的活动,谢谢雅特力,这是俺第一次在二姨家测评开发板,现在进入测评正题。
本人是linux爱好者,另有些开源项目是必须linux下才能编译的,个人习惯linux环境下做开发。开发环境其实主要解决三件事:toolchain、项目工程编译和调试烧录。
toolchain
建议使用发行版自带的gcc-arm-none-eabi工具链,好处显而易见:安装方便且能随着发行版升级而升级。
# apt-get install gcc-arm-none-eabi
调试烧录
openocd和pyocd都可以使用,openocd用c语言实现,pyocd用python语言写成。openocd支持体系架构更多,比如arm、riscv、openrisc、mips、xtensa,支持各种各样的debug probe协议,比如标准JTAG、cJTAG和SWD等等;pyocd是arm专为arm M level的MCU而做,仅支持SWD。但openocd烧录需要源码适配芯片flash烧录算法,而pyocd利用所谓的DFP(Device Familiy Pack)来支持各种SoC, pack格式遵循Open-CMSIS-Pack Spec,所以arm MCU层次的芯片建议使用pyocd。因pyocd开发比较快,更新频繁,建议通过pip安装pyocd:
$ python3 -m pip install -U pyocd
通过这种pip方式安装的pyocd,其位置在$HOME/.local/bin,记得将此目录设置进PATH环境变量。另为解决linux下非根用户操作daplink设备的权限问题,需要到pyocd官方github仓库下载50-cmsis-dap.rules文件,放入/etc/udev/rules.d/目录,并执行
# udevadm control --reload
下载AT32F405的pack文件和bsp包
通过如下命令发现keil官网是找不到AT32F405的pack的:$ pyocd pack find at32f405
最后可以发现AT32系列的pack都没上传到keil官网,这里可以给雅特力提一个小建议:如果可能的话把MCU的pack文件上传keil吧并经常同步更新。现在我们需要到雅特力官网下载AT32F405相关的Keil pack包,解压后找到一个名为ArteryTek.AT32F402_405_DFP.2.0.6.pack文件,放入~/.local/share/cmsis-pack-manager/at32/目录下。
bsp开发包下载解压。雅特力官网把这个称为Firmware Library,下载解压后有如下几个目录/文件
其中document有bsp有关的文档,libraries目录是bsp和cmsis有关的,middlewares目录里是中间件比如freertos阿、fatfs阿之类的,project放例子工程,utilities目录放一些特别demo用的源码和工具
因为pack不在pyocd官方index中,所以需要手动告诉pyocd到哪找额外的pack,在上述解压后的目录中创建一个pyocd.yaml文件,内容为:
pack:
- ~/.local/share/cmsis-pack-manager/at32/ArteryTek.AT32F402_405_DFP.2.0.6.pack
项目工程编译
linux下常用make或cmake,前者要写Makefile,后者要写相应的CMakeLists.txt然后通过cmake生成Makefile。因接下来的测试需求,我会利用cmake这个工具。早听说过AT32 Work Bench的大名,所以在手动写CMakeLists.txt文件之前尝试一下Work Bench,不过最后发现现在这个版本不能生成Makefile或CMakeLists.txt,所以这里给雅特力提个小需求:支持下Makefile和CMakeLists.txt的生成。现在我们只能手搓一个CMakeLists.txt,project就选project/at_start_f405/examples/gpio/led_toggle/为例,用于其它project可以照着此例改动。这里先贴出完整的CMakeLists.txt文件:
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-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16)
add_compile_options(-ffunction-sections -fdata-sections -fno-strict-aliasing -fno-builtin -fno-common -Wall -Wshadow -Wdouble-promotion -Werror -Wno-unused-parameter)
add_compile_options(-O2)
project(at32f405 C ASM)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_STANDARD 99)
include_directories(libraries/cmsis/cm4/core_support
libraries/cmsis/cm4/device_support/
libraries/cmsis/dsp/include/
libraries/drivers/inc/
project/at32f402_405_board/
project/at_start_f405/examples/gpio/led_toggle/inc)
add_definitions(-DAT32F405RCT7_7 -DAT_START_F405_V1)
file(GLOB_RECURSE SOURCES
"project/at32f402_405_board/at32f402_405_board.c"
"project/at_start_f405/examples/gpio/led_toggle/src/*.c"
"libraries/drivers/src/*.c"
"libraries/cmsis/cm4/device_support/system_at32f402_405.c"
"libraries/cmsis/cm4/device_support/startup/gcc/startup_at32f402_405.s"
)
set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/libraries/cmsis/cm4/device_support/startup/gcc/linker/AT32F405xC_FLASH.ld)
add_link_options(
-mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16
-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})
# target_link_libraries(${PROJECT_NAME}.elf printfloat)
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_55 ]lt;TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
COMMAND ${CMAKE_OBJCOPY} -Obinary [ DISCUZ_CODE_55 ]lt;TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
COMMAND ${CMAKE_OBJDUMP} --all-headers --demangle --disassemble [ DISCUZ_CODE_55 ]lt;TARGET_FILE:${PROJECT_NAME}.elf> > ${LST_FILE}
COMMAND ${SIZE} --format=berkeley [ DISCUZ_CODE_55 ]lt;TARGET_FILE:${PROJECT_NAME}.elf>
)
这里需要注意的是高亮的这行的宏定义:
这两项是AT32F405硬件相关的,因为手里这块板子叫AT_START_F405_V1, 芯片是AT32F405RCT7_7。
在project/at_start_f405/examples/gpio/led_toggle/src/main.c加两行printf打印顺便测试下uart printf功能:
编译烧录运行
~$ mkdir build
~$ cd build
~$ cmake ..
~$ make
编译结果截屏:
固件烧录之前先打开minicom串口终端:
固件烧录:
$ pyocd load -e sector -t at32f405rct7_7 at32f405.bin
这时在minicom终端可见期望的打印
板子上可见LED轮流闪烁
pyocd调试界面展示
$ pyocd cmd -t at32f405rct7_7
可以看到pc、xpsr、lr、sp、r0、r1等寄存器,看起来pyocd调试挺好使!
|