From ccaddf05a4d462144257183d06fc93614d2585b6 Mon Sep 17 00:00:00 2001 From: lunarifish Date: Fri, 13 Sep 2024 13:46:26 +0800 Subject: [PATCH] feat: makefile for openocd op --- CMakeLists.txt | 11 +---------- Makefile | 18 ++++++++++++++++++ README.markdown | 14 -------------- README.md | 21 +++++++++++++++++++++ openocd/flash-target.cmake | 37 ------------------------------------- 5 files changed, 40 insertions(+), 61 deletions(-) create mode 100644 Makefile delete mode 100644 README.markdown create mode 100644 README.md delete mode 100644 openocd/flash-target.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index f244b21..861dd3a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,16 +71,7 @@ target_link_libraries(${CMAKE_PROJECT_NAME} # Convert ELF to BIN for flashing add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD - COMMAND ${CMAKE_OBJCOPY} -O binary ${CMAKE_PROJECT_NAME}.elf ${CMAKE_PROJECT_NAME}.bin + COMMAND ${CMAKE_OBJCOPY} -O binary ${CMAKE_PROJECT_NAME}.elf program.bin DEPENDS ${CMAKE_PROJECT_NAME} COMMENT "Converting ELF to BIN" ) - -# Add openocd functions -include(${CMAKE_SOURCE_DIR}/openocd/flash-target.cmake) - -# 调用 add_erase_and_reset 函数 -add_erase_and_reset() - -# 调用 flash_target 函数 -flash_target() diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..52e8060 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +# Makefile for STM32 operations using OpenOCD + +# OpenOCD configuration file +OPENOCD_CFG = ./openocd/openocd.cfg + +# Target to flash the STM32 +flash: + openocd -f $(OPENOCD_CFG) -c "program program.bin 0x08000000 verify reset exit" + +# Target to reset the STM32 +reset: + openocd -f $(OPENOCD_CFG) -c "init" -c "reset" -c "exit" + +# Target to erase the STM32 +erase: + openocd -f $(OPENOCD_CFG) -c "init" -c "halt" -c "stm32f4x mass_erase 0" -c "exit" + +.PHONY: flash reset erase \ No newline at end of file diff --git a/README.markdown b/README.markdown deleted file mode 100644 index 8d627de..0000000 --- a/README.markdown +++ /dev/null @@ -1,14 +0,0 @@ -## This is a template project - -### How to flash and debug via vscode cortex-debug extension - -**相关文件都在 ./openocd/ 下面** - -- 首先要有openocd工具,可以从官网下载安装。还要有cortex debug插件。 -- 主要有三个target可以使用:`cmake ..` `make`后可以使用 - -1. `make download` flash to stm32 -2. `make reset` reset stm32 -3. `make erase` erase stm32 - -- 自动生成了 launsh.json 文件,可以直接在vscode的调试中使用。 \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..edade1d --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# STM32F4项目模板 + +## Prerequisites + +- cmake `>=3.22` + +- openocd + +- vscode + cortex-debug extension + +- arm-none-eabi-gcc + +## Usage + +把项目clone到本地,然后在VSCode里打开进行开发即可。 + +### makefile shortcuts + +- `make download` +- `make reset` +- `make erase` diff --git a/openocd/flash-target.cmake b/openocd/flash-target.cmake deleted file mode 100644 index 10fc78d..0000000 --- a/openocd/flash-target.cmake +++ /dev/null @@ -1,37 +0,0 @@ -##### -# author: https://github.com/patrislav1/cubemx.cmake -##### -set(OPENOCD_CFG "${CMAKE_SOURCE_DIR}/openocd/openocd.cfg") - -if(DEFINED OPENOCD_CFG) - set(OPENOCD_CFG_OPT -f ${OPENOCD_CFG}) -endif() - -function(add_erase_and_reset) - ##################################### - # Reset chip # - ##################################### - add_custom_target(reset - openocd ${OPENOCD_CFG_OPT} -c "init" -c "reset" -c "exit" - COMMENT "Resetting chip" - ) - - ##################################### - # Mass erase chip # - ##################################### - add_custom_target(erase - openocd ${OPENOCD_CFG_OPT} -c "init" -c "halt" -c "stm32f4x mass_erase 0" -c "exit" - COMMENT "Mass erasing chip" - ) -endfunction() - -##################################### -# Flash application to target # -##################################### -function(flash_target) - add_custom_target(download - openocd ${OPENOCD_CFG_OPT} -c "program ${CMAKE_PROJECT_NAME}.bin 0x08000000 verify reset exit" - DEPENDS ${CMAKE_PROJECT_NAME}.bin - COMMENT "Flashing ${PROJ_NAME}.bin to target" - ) -endfunction() \ No newline at end of file