Skip to content

Commit

Permalink
openocd programming used instead of st-flash, makefile/cmakelists upd…
Browse files Browse the repository at this point in the history
…ated to allow debugging
  • Loading branch information
lukezsmith committed Sep 24, 2022
1 parent d1e8ef6 commit e8bec26
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 15 deletions.
1 change: 1 addition & 0 deletions .vscode/.cortex-debug.registers.state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
4 changes: 1 addition & 3 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": ""
Expand Down
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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
}
],
}
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@
"stm32f4xx_hal.h": "c",
"stm32f4xx.h": "c",
"stm32f4xx_hal_gpio.h": "c"
}
},
"cortex-debug.showRTOS": false,
"cmake.configureOnOpen": false
}
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"command": "",
"args": [
"make",
"BUILD_TYPE=test",
"TEST_MODE=TRUE",
],
"group": {
"kind": "build",
Expand Down
36 changes: 32 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 --------
Expand Down Expand Up @@ -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
Expand All @@ -95,8 +122,9 @@ if(CMAKE_BUILD_TYPE STREQUAL release)
-fdata-sections
-Wno-unused-parameter
--specs=nano.specs
$<$<CONFIG:Debug>:-Og -g3 -ggdb>
$<$<CONFIG:Release>:-Og -g0>)
-Og -g0)

endif()

target_link_options(${EXECUTABLE} PRIVATE
-T${MCU_LINKER_SCRIPT}
Expand Down
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 \
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit e8bec26

Please sign in to comment.