|
| 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 | + |
0 commit comments