本帖最后由 xhackerustc 于 2024-4-28 00:55 编辑
笔者是linux爱好者,平时都在linux下开发,通常工作style是vim编辑源码,gcc/clang做toolchain,make、cmake、ninja等构造编译工程,gdb/openocd/pyocd调试烧录。虽然stm32cubemx,Keil等都有linux下的版本,但笔者很少用这些工具,甚至笔记本电脑上都没安装它们。然后对于STM32系列MCU来说,以前用标准库甚至玩libopencm3等开源库,现在喜欢用LL库。怎么说呢,有特定的代码/工具口味,所以第一篇测评贴先写一写开发环境搭建,分享给有同样喜好的人们,用其它开发环境的也可以做个参考。
恰巧刚写了一篇给AT32F405搭建开发环境的贴(https://bbs.21ic.com/icview-3374312-1-1.html),为避免水文这里就不重复gcc-arm-none-eabi和pyocd的安装了。下面说说STM32U083特别的地方。
板载STLINK
和所有ST家的开发板一样,NUCLEO-U083RC板载一个STLINK,具体版本是V2EC,pyocd以前只支持daplink,大概在2019年时加入了对stlink的支持。事实上,usb连接后"pyocd list -p"可以识别出来stlink
安装STM32U083 pack包
$ pyocd pack find stm32u083
上述命令可以在Keil官方找到stm32u083的pack包,说明ST官方已经上传pack包至keil,所以直接安装之:
$ pyocd pack install STM32U083RCTx
注:pack包比其它厂家的大不少,笔者也不知道为什么,可能同一个pack支持的MCU种类更多?下载时间长一些需要有点耐心。
下载解压STM32U083的BSP固件开发包
笔者搜索以后从这里下载的:https://www.st.com/en/evaluation-tools/nucleo-u083rc.html,解压后得到STM32Cube_FW_U0_V1.0.0顶级目录,里面有下面这几个目录
Documentations/是U0 bsp的一些文档;Drivers目录比较重要,HAL、LL库都在这里;Middlewares/暂时不用关心,ThreadX、MCUBOOT之类的;Projects目录有三块开发板(NUCLEO-U083RC,NUCLEO-U031R8和STM32U083C-DK)的HAL使用、LL使用、HAL和LL混合使用例子,笔者只关心NUCLEO-U083RC目录中的Examples_LL/
手搓一个CMakeLists.txt
仔细观察STM32Cube_FW_U0_V1.0.0目录,我们发现有如下四个文件注释中明确说明让拷贝到application用,并且要做对应修改(估计用CubeMx的同学这一步直接有CubeMx代劳做掉了),手搓CMakeLists.txt时想用通配符而不是显式列出源文件,所以笔者把它们后缀都从".c"改成了".t"
stm32u0xx_hal_msp_template.c stm32u0xx_hal_timebase_rtc_wakeup_template.c
stm32u0xx_hal_timebase_rtc_alarm_template.c stm32u0xx_hal_timebase_tim_template.c
接下来贴出全部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-m0plus)
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(stm32u083 C ASM)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_STANDARD 99)
include_directories(Drivers/CMSIS/Include/
Drivers/CMSIS/Device/ST/STM32U0xx/Include/
Drivers/STM32U0xx_HAL_Driver/Inc/
Projects/NUCLEO-U083RC/Examples_LL/GPIO/GPIO_InfiniteLedToggling_Init/Inc/)
add_definitions(-DSTM32U083xx -DUSE_FULL_LL_DRIVER)
file(GLOB_RECURSE SOURCES
"Drivers/STM32U0xx_HAL_Driver/Src/*.c"
"Projects/NUCLEO-U083RC/Examples_LL/GPIO/GPIO_InfiniteLedToggling_Init/Src/*.c"
"Projects/NUCLEO-U083RC/Examples_LL/GPIO/GPIO_InfiniteLedToggling_Init/STM32CubeIDE/Application/User/*.c"
"Projects/NUCLEO-U083RC/Examples_LL/GPIO/GPIO_InfiniteLedToggling_Init/STM32CubeIDE/Application/Startup/startup_stm32u083rctx.s"
)
set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/Projects/NUCLEO-U083RC/Examples_LL/GPIO/GPIO_Infinit
eLedToggling_Init/STM32CubeIDE/STM32U083RCTX_FLASH.ld)
add_link_options(
-mthumb -mcpu=cortex-m0plus
-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_48 ]lt;TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
COMMAND ${CMAKE_OBJCOPY} -Obinary [ DISCUZ_CODE_48 ]lt;TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
COMMAND ${CMAKE_OBJDUMP} --all-headers --demangle --disassemble [ DISCUZ_CODE_48 ]lt;TARGET_FILE:${PROJECT_NAME}.elf> > ${LST_FILE}
COMMAND ${SIZE} --format=berkeley [ DISCUZ_CODE_48 ]lt;TARGET_FILE:${PROJECT_NAME}.elf>
)
这里需注意高亮部分的宏定义,因整个STM32Cube_FW_U0_V1.0.0目录目前支持三个不同MCU,通过定义STM32U083xx来区分的;而用HAL库还是LL库是通过USE_FULL_LL_DRIVER宏区分。
后面编译烧录就丝般顺滑了
$ mkdir build
$ cd build
$ cmake ..
$ make
$ pyocd load -e sector -t stm32u083rctx stm32u083.bin
|