Skip to content

Commit 080ed3b

Browse files
committed
tools: Integrate Corex-debugger.
Signed-off-by: wxyyy0117 <wxyyy2002@outlook.com>
1 parent 3f96e17 commit 080ed3b

File tree

3 files changed

+284
-0
lines changed

3 files changed

+284
-0
lines changed

docs/debugging-instructions.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Debugging (VS Code)
2+
===================
3+
4+
Debugging support described here is for Visual Studio Code. The Arduino IDE already provides a built-in debugger for supported boards.
5+
6+
#. Install the `Cortex-Debug` extension in VS Code.
7+
#. Copy the `task_psoc6.json` file from `tools/vscode-profile` to the `.vscode` directory and rename it to `tasks.json` in your project root.
8+
#. In VS Code, run the task: **Generate launch.json for debug (PSoC6)**.
9+
#. Required parameters for this task:
10+
* **fqbn**: Fully Qualified Board Name (e.g., `infineon:psoc6:cy8ckit_062s2_ai`)
11+
* **build path**: Directory where the `.elf` file will be placed
12+
* **example path**: Path to the sketch (`.ino` file) to debug (ensure it has been built at least once to generate the required build files)
13+
#. Optional parameters:
14+
* **boards.txt path**: Path to a custom `boards.txt` file
15+
* **gdb path**: Path to a custom GDB executable
16+
17+
Refer to the documentation of your chosen debugger and scripts in the `tools/` folder for more details.
18+
19+
.. note::
20+
If you encounter an error indicating that ``libncurses.so.5`` or a similar library cannot be found, please search online and install the appropriate package for your environment.

tools/gen_launch.sh

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#!/bin/bash
2+
# gen_launch.sh: Compile and generate launch.json for XMC or PSoC6 boards
3+
# Usage: ./gen_launch.sh <fqbn> <build_path> <sketch_path> [boards.txt] [gdb_path]
4+
# <fqbn> : Fully Qualified Board Name (e.g. infineon:psoc6:CY8CKIT_062S2_AI or arduino-git:xmc:kit_xmc47_relax)
5+
# <build_path> : Directory where the .elf file will be placed
6+
# <sketch_path> : Path to the sketch (.ino) file
7+
# [boards.txt] : (Optional) Path to boards.txt (default: inferred based on device)
8+
# [gdb_path] : (Optional) Path to GDB executable (default: inferred based on device)
9+
10+
set -e
11+
12+
FQBN_FULL="$1"
13+
BUILD_PATH="$2"
14+
SKETCH_PATH="$3"
15+
16+
if [[ -z "$FQBN_FULL" || -z "$BUILD_PATH" || -z "$SKETCH_PATH" ]]; then
17+
echo "Usage: $0 <fqbn> <build_path> <sketch_path> [boards.txt] [gdb_path]"
18+
exit 1
19+
fi
20+
21+
# Get the script directory and package root
22+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
23+
PACKAGE_DIR="$(dirname "$SCRIPT_DIR")"
24+
25+
# Detect device type based on FQBN
26+
if [[ "$FQBN_FULL" == infineon:psoc6:* ]]; then
27+
DEVICE_TYPE="psoc6"
28+
BOARDS_TXT="${4:-$PACKAGE_DIR/boards.txt}"
29+
GDB_PATH="${5:-$HOME/.arduino15/packages/infineon/tools/mtb-gcc-arm-none-eabi/11.3.1.67/bin/arm-none-eabi-gdb}"
30+
elif [[ "$FQBN_FULL" == arduino-git:xmc:* ]]; then
31+
DEVICE_TYPE="xmc"
32+
XMC_DIR="$(dirname "$SCRIPT_DIR")"
33+
BOARDS_TXT="${4:-$XMC_DIR/boards.txt}"
34+
GDB_PATH="${5:-$HOME/.arduino15/packages/infineon/tools/arm-none-eabi-gcc/10.3-2021.10/bin/arm-none-eabi-gdb}"
35+
else
36+
echo "Unsupported device type in FQBN: $FQBN_FULL"
37+
exit 1
38+
fi
39+
40+
# Extract board name from FQBN
41+
BOARD_NAME=$(echo "$FQBN_FULL" | awk -F: '{print $NF}')
42+
43+
# Compile the sketch
44+
arduino-cli compile -b "${FQBN_FULL}" --build-path "${BUILD_PATH}" "${SKETCH_PATH}" || exit 1
45+
46+
# Parse boards.txt for variant and other parameters
47+
VARIANT=$(grep "^${BOARD_NAME}\.build\.variant=" "$BOARDS_TXT" | cut -d= -f2)
48+
if [[ -z "$VARIANT" ]]; then
49+
echo "Could not find variant for $BOARD_NAME in $BOARDS_TXT"
50+
exit 2
51+
fi
52+
53+
if [[ "$DEVICE_TYPE" == "xmc" ]]; then
54+
BOARD_V=$(grep "^${BOARD_NAME}\.build\.board\.v=" "$BOARDS_TXT" | cut -d= -f2)
55+
if [[ -z "$BOARD_V" ]]; then
56+
echo "Could not find board.v for $BOARD_NAME in $BOARDS_TXT"
57+
exit 2
58+
fi
59+
DEVICE="${VARIANT}-${BOARD_V}"
60+
else
61+
DEVICE="${VARIANT}"
62+
fi
63+
64+
# Find the .elf executable
65+
EXECUTABLE=$(find "${BUILD_PATH}" -maxdepth 1 -type f -name "*.elf" | head -n 1)
66+
if [[ -z "$EXECUTABLE" ]]; then
67+
echo "No .elf executable found in $BUILD_PATH."
68+
exit 3
69+
fi
70+
71+
# Create the .vscode directory and generate launch.json
72+
LAUNCH_DIR="$PACKAGE_DIR/.vscode"
73+
if [ ! -d "$LAUNCH_DIR" ]; then
74+
mkdir -p "$LAUNCH_DIR"
75+
fi
76+
if [ -f "$LAUNCH_DIR/launch.json" ]; then
77+
rm "$LAUNCH_DIR/launch.json"
78+
fi
79+
80+
if [[ "$DEVICE_TYPE" == "psoc6" ]]; then
81+
# Generate launch.json for PSoC6
82+
cat > "$LAUNCH_DIR/launch.json" <<EOF
83+
{
84+
"version": "0.2.0",
85+
"configurations": [
86+
{
87+
88+
"name": "Cortex-Debug: Debug ${DEVICE} CM4",
89+
"type": "cortex-debug",
90+
"request": "launch",
91+
"servertype": "openocd",
92+
"device": "${DEVICE}",
93+
"executable": "${EXECUTABLE}",
94+
"cwd": "\${workspaceFolder}",
95+
"interface": "swd",
96+
"gdbPath": "${GDB_PATH}",
97+
"showDevDebugOutput": "vscode",
98+
"configFiles": [
99+
"$HOME/.arduino15/packages/infineon/tools/openocd/5.2.1.3248/scripts/interface/kitprog3.cfg",
100+
"$HOME/.arduino15/packages/infineon/tools/openocd/5.2.1.3248/scripts/target/psoc6_2m.cfg"
101+
],
102+
"overrideLaunchCommands": [
103+
"set mem inaccessible-by-default off",
104+
"-enable-pretty-printing",
105+
"set remotetimeout 15",
106+
"monitor reset run",
107+
"monitor psoc6 reset_halt sysresetreq"
108+
],
109+
"numberOfProcessors": 2,
110+
"targetProcessor": 1,// Set to 0 for the CM0+, set to 1 for the CM4
111+
"postStartSessionCommands": [
112+
"monitor gdb_sync",
113+
"stepi"
114+
],
115+
"overrideRestartCommands": [
116+
"starti"
117+
],
118+
"postRestartSessionCommands": [],
119+
"breakAfterReset": true
120+
}
121+
]
122+
}
123+
EOF
124+
else
125+
# Generate launch.json for XMC
126+
cat > "$LAUNCH_DIR/launch.json" <<EOF
127+
{
128+
"version": "0.2.0",
129+
"configurations": [
130+
{
131+
"name": "Cortex-Debug: Debug ${DEVICE}",
132+
"type": "cortex-debug",
133+
"request": "launch",
134+
"servertype": "jlink",
135+
"device": "${DEVICE}",
136+
"executable": "${EXECUTABLE}",
137+
"cwd": "\${workspaceFolder}",
138+
"interface": "swd",
139+
"gdbPath": "${GDB_PATH}",
140+
"showDevDebugOutput": "vscode"
141+
}
142+
]
143+
}
144+
EOF
145+
fi
146+
147+
echo "launch.json generated for ${DEVICE_TYPE} device ${DEVICE} at $LAUNCH_DIR."
148+
echo "(Using boards.txt at $BOARDS_TXT)"
149+
150+
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "Arduino Build",
6+
"type": "shell",
7+
"command": "arduino-cli",
8+
"args": [
9+
"compile",
10+
"--fqbn", "${input:boardFqbn}",
11+
"${input:examplePath}"
12+
],
13+
"group": {
14+
"kind": "build",
15+
"isDefault": false
16+
},
17+
"problemMatcher": []
18+
},
19+
{
20+
"label": "Arduino Upload",
21+
"type": "shell",
22+
"command": "arduino-cli",
23+
"args": [
24+
"upload",
25+
// "--verbose",
26+
"-p", "${input:port}",
27+
"--fqbn", "${input:boardFqbn}",
28+
"${input:examplePath}"
29+
],
30+
"dependsOn": "Arduino Build",
31+
"group": {
32+
"kind": "build",
33+
"isDefault": true
34+
},
35+
"problemMatcher": []
36+
},
37+
{
38+
"label": "Generate lauch.json for debug (PSOC)",
39+
"type": "shell",
40+
"command": "${workspaceFolder}/tools/gen_launch.sh",
41+
"args": [
42+
"${input:boardFqbn}",
43+
"${input:debugBuildPath}",
44+
"${input:examplePath}",
45+
"${input:boardsTxtPath}",
46+
"${input:gdbPath}"
47+
],
48+
"group": {
49+
"kind": "build",
50+
"isDefault": false
51+
},
52+
"problemMatcher": [],
53+
"presentation": {
54+
"echo": true,
55+
"reveal": "always",
56+
"focus": false,
57+
"panel": "shared"
58+
}
59+
},
60+
{
61+
"label": "Arduino Monitor",
62+
"type": "shell",
63+
"command": "arduino-cli",
64+
"args": [
65+
"monitor",
66+
"-p", "${input:port}",
67+
"-c", "baudrate=115200"
68+
],
69+
"group": {
70+
"kind": "build",
71+
"isDefault": false
72+
},
73+
"problemMatcher": []
74+
}
75+
],
76+
"inputs": [
77+
{
78+
"id": "boardFqbn",
79+
"type": "promptString",
80+
"description": "Enter the FQBN (Fully Qualified Board Name) for the Arduino board",
81+
"default": "infineon:psoc6:cy8ckit_062s2_ai"
82+
},
83+
{
84+
"id": "debugBuildPath",
85+
"type": "promptString",
86+
"description": "Enter the build path where the .elf file would be placed",
87+
"default": "${workspaceFolder}/extras/arduino-core-tests/build/output"
88+
},
89+
{
90+
"id": "examplePath",
91+
"type": "promptString",
92+
"description": "Enter the path to the Arduino example sketch",
93+
"default": "${workspaceFolder}/examples/bug/bug.ino"
94+
},
95+
{
96+
"id": "port",
97+
"type": "promptString",
98+
"description": "Enter the port for the Arduino board",
99+
"default": "/dev/ttyACM0"
100+
},
101+
{
102+
"id": "boardsTxtPath",
103+
"type": "promptString",
104+
"description": "(Optional) Enter the path to boards.txt, or leave blank for default",
105+
"default": ""
106+
},
107+
{
108+
"id": "gdbPath",
109+
"type": "promptString",
110+
"description": "(Optional) Enter the path to arm-none-eabi-gdb, or leave blank for default",
111+
"default": ""
112+
},
113+
]
114+
}

0 commit comments

Comments
 (0)