平台:正点原子战舰开发板
根据电路图,设置为LED设推挽输出;按键采用KEY0,KEY1,设置为输入上拉。
配置好引脚后,生成代码:
接着在生成的代码目录中生成hex文件,有两种方式Makefile和CMakeLists.txt方式。
1. Makefile方式直接在目录内进行make
;
2. CMakeLists.txt方式需要编写如下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.14)
# 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(TEMPLATE C CXX ASM)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)
add_compile_options(-mcpu=cortex-m3 -mthumb -mthumb-interwork)
add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0)
# add macro definition
# add_definitions(-DUSE_HAL_DRIVER -DSTM32F103xE -DUSE_STDPERIPH_DRIVER -DSTM32F103_HD -DSTM32F10X_HD)
add_definitions(-DUSE_HAL_DRIVER -DSTM32F103xE)
# get sources
file(GLOB_RECURSE SRC_LIST
"Core/Src/*.c"
)
set(SRC_LIST1
"Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c"
"Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c"
"Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c"
"Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c"
"Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c"
"Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c"
"Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c"
"Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c"
"Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c"
"Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c"
"Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c"
"Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c"
"Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c"
)
list(APPEND SRC_LIST ${SRC_LIST1} "startup_stm32f103xe.s")
# set include directories
set (INCLUDE_DIRS
"Core/Inc"
"Drivers/CMSIS/Device/ST/STM32F1xx/Include"
"Drivers/CMSIS/Include"
"Drivers/STM32F1xx_HAL_Driver/Inc"
"Drivers/STM32F1xx_HAL_Driver/Inc/Legacy"
)
# add executable, .elf or .hex
add_executable(${PROJECT_NAME}.elf ${SRC_LIST} ${LINKER_SCRIPT})
target_include_directories(${PROJECT_NAME}.elf PRIVATE ${INCLUDE_DIRS})
# compile option
set(CPU_FLAGS "-mcpu=cortex-m3" "-mthumb")
set(OPT_FLAGS "-Og")
set(DEBUG_FLAGS "-g -gdwarf-2")
set(C_DEFS "-DUSE_HAL_DRIVER" "-DSTM32F103xE")
target_compile_options(${PROJECT_NAME}.elf PRIVATE ${CPU_FLAGS} ${OPT_FLAGS} ${C_DEFS})
if(DEBUG)
target_compile_options(${PROJECT_NAME}.elf PRIVATE ${DEBUG_FLAGS})
endif()
# set linker script
set(LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/STM32F103ZETx_FLASH.ld")
# 链接选项
target_link_options(${PROJECT_NAME}.elf PRIVATE
${CPU_FLAGS}
-specs=nano.specs
-specs=nosys.specs
-T${LINKER_SCRIPT}
-Wl,-Map=${BUILD_DIR}/${TEMPLATE}.map,--cref
-Wl,--gc-sections
)
# generate hex and bin files
add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${BUILD_DIR}/${TEMPLATE}.hex
COMMAND ${CMAKE_OBJCOPY} -O binary -S $<TARGET_FILE:${PROJECT_NAME}.elf> ${BUILD_DIR}/${TEMPLATE}.bin
COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${PROJECT_NAME}.elf>
)
# clean
add_custom_target(clean_all COMMAND ${CMAKE_COMMAND} -E remove_directory ${BUILD_DIR})
然后即可在cmake