From e8bec2698b6063fc98ecb515fc89126457a77fc5 Mon Sep 17 00:00:00 2001 From: = Date: Sat, 24 Sep 2022 19:58:17 +0100 Subject: [PATCH] openocd programming used instead of st-flash, makefile/cmakelists updated to allow debugging --- .vscode/.cortex-debug.registers.state.json | 1 + .vscode/c_cpp_properties.json | 4 +-- .vscode/launch.json | 22 +++++++++++++ .vscode/settings.json | 4 ++- .vscode/tasks.json | 2 +- CMakeLists.txt | 36 +++++++++++++++++++--- Makefile | 10 +++--- README.md | 7 +++-- 8 files changed, 71 insertions(+), 15 deletions(-) create mode 100644 .vscode/.cortex-debug.registers.state.json create mode 100644 .vscode/launch.json diff --git a/.vscode/.cortex-debug.registers.state.json b/.vscode/.cortex-debug.registers.state.json new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/.vscode/.cortex-debug.registers.state.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 397b0ff..99eed0c 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -21,9 +21,7 @@ "intelliSenseMode": "clang-x64", "browse": { "path": [ - "${workspaceRoot}/include", - "${workspaceRoot}/libs/CMSIS_5/CMSIS/Core/Include", - "${workspaceRoot}/libs/CMSIS_5/CMSIS/DSP/Include" + "${workspaceRoot}/include" ], "limitSymbolsToIncludedHeaders": true, "databaseFilename": "" diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..c4a9191 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,22 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Cortex Debug", + "cwd": "${workspaceRoot}", + "executable": "./build/release/main.elf", + "request": "launch", + "type": "cortex-debug", + "servertype": "openocd", + "device": "STM32F407VG", + // "interface": "swd", + "runToEntryPoint": "main", + "configFiles": [ + "interface/stlink.cfg", + "target/stm32f4x.cfg" + ], + "showDevDebugOutput": "raw" + // "showDevDebugOutput": true + } + ], + } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 87d375b..17c4f9a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -31,5 +31,7 @@ "stm32f4xx_hal.h": "c", "stm32f4xx.h": "c", "stm32f4xx_hal_gpio.h": "c" - } + }, + "cortex-debug.showRTOS": false, + "cmake.configureOnOpen": false } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 04ad936..635a9f2 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -7,7 +7,7 @@ "command": "", "args": [ "make", - "BUILD_TYPE=test", + "TEST_MODE=TRUE", ], "group": { "kind": "build", diff --git a/CMakeLists.txt b/CMakeLists.txt index 58cb4f6..c030ac9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,7 @@ set(HAL_PATH ${CMAKE_CURRENT_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver) add_subdirectory(src/modules) # Production build -if(CMAKE_BUILD_TYPE STREQUAL release) +if(NOT TEST_MODE) # -------- Collate Code Sources -------- file(GLOB_RECURSE PROJECT_SOURCES "${PROJECT_DIR}/*.c") # -------- Include HAL Sources -------- @@ -72,7 +72,34 @@ if(CMAKE_BUILD_TYPE STREQUAL release) ${CMSIS_PATH}/Include) # -------- Compilation configuration -------- - target_compile_options(${EXECUTABLE} PRIVATE + if (CMAKE_BUILD_TYPE STREQUAL Debug) + target_compile_options(${EXECUTABLE} PRIVATE + ${CPU_PARAMETERS} + -DUSE_HAL_DRIVER + -DSTM32F407xx + -std=gnu11 + -Wall + -Wextra + -Wpedantic + -Wshadow + -Wdouble-promotion + -Wformat=2 -Wformat-truncation + -Wmissing-include-dirs + -Wsign-compare + -Wundef + # -Wcast-align + # -Wconversion + -fno-common + -fsingle-precision-constant + -fomit-frame-pointer + -ffunction-sections + -fdata-sections + -Wno-unused-parameter + --specs=nano.specs + -Og -g3 -ggdb) + + else() + target_compile_options(${EXECUTABLE} PRIVATE ${CPU_PARAMETERS} -DUSE_HAL_DRIVER -DSTM32F407xx @@ -95,8 +122,9 @@ if(CMAKE_BUILD_TYPE STREQUAL release) -fdata-sections -Wno-unused-parameter --specs=nano.specs - $<$:-Og -g3 -ggdb> - $<$:-Og -g0>) + -Og -g0) + + endif() target_link_options(${EXECUTABLE} PRIVATE -T${MCU_LINKER_SCRIPT} diff --git a/Makefile b/Makefile index d3a1be1..03a8ee0 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,13 @@ .PHONY: all build cmake clean TARGET := main -BUILD_TYPE ?= test +TEST_MODE ?= FALSE +BUILD_TYPE ?= Debug CMAKE_PATH := /opt/local/bin/cmake CTEST_PATH := /opt/local/bin/ctest STFLASH_PATH := /opt/homebrew/bin/st-flash -ifeq ($(BUILD_TYPE), test) +ifeq ($(TEST_MODE), TRUE) BUILD_DIR := build/test TOOLCHAIN_FILE := "" else @@ -15,13 +16,14 @@ else endif all: build - @if [ "$(BUILD_TYPE)" = "test" ]; then \ + @if [ "$(TEST_MODE)" = TRUE ]; then \ $(CTEST_PATH) --test-dir $(BUILD_DIR);\ fi ${BUILD_DIR}/Makefile: @${CMAKE_PATH} \ -B${BUILD_DIR} \ + -DTEST_MODE=${TEST_MODE} \ -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \ -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE} \ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ @@ -33,7 +35,7 @@ build: cmake @$(MAKE) -C ${BUILD_DIR} --no-print-directory flash: build/release/Makefile - @st-flash write build/release/$(TARGET).bin 0x8000000 + openocd -f interface/stlink.cfg -f target/stm32f4x.cfg -c "program build/release/$(TARGET).elf verify reset exit" # test: build # ctest --test-dir ./build/test diff --git a/README.md b/README.md index 7c734cd..6a91cb3 100644 --- a/README.md +++ b/README.md @@ -38,13 +38,16 @@ In `CMakeLists.txt` only a subset of HAL drivers have been added to the executab The template uses CMake to build the code. To automate the process there is a Makefile in the root directory which can be run to automate the build. -This template has cross-compilation set up. There is a test build which compiles on the host machine. I use this to unit test the code. To run this simply run `make` or `make BUILD_TYPE=test` in the terminal from the root directory of the repository. The build will be saved in `/build/test`. +This template has cross-compilation set up. There is a test build which compiles on the host machine. I use this to unit test the code. To run this simply run `make` or `make TEST_MODE=TRUE` in the terminal from the root directory of the repository. The build will be saved in `/build/test`. -To build for the STM32 microcontroller run `make BUILD_TYPE=release`. The build will be saved in `/build/release`. +To build for the STM32 microcontroller run `make` to build a version for debugging. To create a release build run `make BUILD_TYPE=Release`. These build will be saved in `/build/release`. ### Testing This template includes GoogleTest. The process for adding test modules is explained above. Testing runs automatically when the test target is built with `make BUILD_TYPE=test`. There is also a Visual Studio Code Task included with this template that automatically runs the test build (and associated tests) when the task is run. For me the keyboard shortcut to run this task is `CMD+SHIFT+B`. This task can be found in `/.vscode/tasks.json`. +### Debugging +The project is ready for debugging via gdb through the `Debug` build. I usually use the [cortex-debug](https://marketplace.visualstudio.com/items?itemName=marus25.cortex-debug) Visual Studio Code extension. A configuration file for debugging with cortex-debug (with openocd, stlink) can be found at `.vscode/launch.json`. You will need to modify this file based on your debugging server/microcontroller setup. + ### Flashing Included in the Makefile is a flash command to flash the program to the STM32.