Yeah that code shouldn’t be able to cause such failure. The SWD pins are PA13 and P14 and you’re not using those.
Assuming you have NRST connected between the adapter and the target board, it’s weird.
Try different reset configuration commands for OpenOCD. Add to the platformio.ini
upload_command = $PROJECT_PACKAGES_DIR/tool-openocd/bin/openocd $UPLOAD_FLAGS -c "program {$SOURCE} verify reset; shutdown;"
upload_flags =
-d2
-s
$PROJECT_PACKAGES_DIR/tool-openocd/scripts
-f
interface/stlink.cfg
-c
transport select hla_swd
-f
target/stm32f4x.cfg
-c
reset_config none separate
And for the last line, try different values from STM32 + VS Code + mbed upload issue: Error: libusb_open() failed with LIBUSB_ERROR_ACCESS Error: open failed - #12 by maxgerhardt.