OpenOCD init failed to upload code to STM32 on Ubuntu

Now its happening more often that everytime i try to upload code to my stm32, it fails with the following output.

$ pio run --target upload -v
Processing nucleo_f303k8 (platform: ststm32; board: nucleo_f303k8; framework: stm32cube; upload_protocol: stlink; platform_packages: platformio/tool-openocd@2.1100.211028; build_flags: -Wl,-u,_printf_float)

CONFIGURATION: Redirecting...
PLATFORM: ST STM32 (19.3.0) > ST Nucleo F303K8
HARDWARE: STM32F303K8T6 72MHz, 12KB RAM, 64KB Flash
DEBUG: Current (stlink) On-board (stlink) External (blackmagic, cmsis-dap, jlink)
PACKAGES:

  • framework-stm32cubef3 @ 1.11.5
  • tool-dfuutil @ 1.11.0
  • tool-dfuutil-arduino @ 1.11.0
  • tool-ldscripts-ststm32 @ 0.2.0
  • tool-openocd @ 2.1100.211028 (11.0)
  • tool-stm32duino @ 1.0.1
  • tool-stm32flash @ 0.7.0
  • toolchain-gccarmnoneeabi @ 1.70201.0 (7.2.1)
    LDF: Library Dependency Finder → Library Dependency Finder (LDF) — PlatformIO latest documentation
    LDF Modes: Finder ~ chain, Compatibility ~ soft
    Found 28 compatible libraries
    Scanning dependencies…
    No dependencies
    Building in release mode
    MethodWrapper([“checkprogsize”], [“.pio/build/nucleo_f303k8/firmware.elf”])
    Advanced Memory Usage is available via “PlatformIO Home > Project Inspect”
    RAM: [= ] 6.9% (used 844 bytes from 12288 bytes)
    Flash: [== ] 24.1% (used 15820 bytes from 65536 bytes)
    .pio/build/nucleo_f303k8/firmware.elf :
    section size addr
    .isr_vector 392 134217728
    .text 14728 134218120
    .rodata 616 134232848
    .ARM.extab 0 134233464
    .ARM 0 134233464
    .preinit_array 0 134233464
    .init_array 4 134233464
    .fini_array 4 134233468
    .data 476 536870912
    .ccmram 0 268435456
    .bss 368 536871388
    ._user_heap_stack 1540 536871756
    .ARM.attributes 42 0
    .comment 126 0
    .debug_frame 2216 0
    Total 20512
    ([“upload”], [“.pio/build/nucleo_f303k8/firmware.elf”])
    AVAILABLE: blackmagic, cmsis-dap, jlink, mbed, stlink
    CURRENT: upload_protocol = stlink
    openocd -d2 -s /home/bob/.platformio/packages/tool-openocd@2.1100.211028/openocd/scripts -f board/st_nucleo_f3.cfg -c “program {.pio/build/nucleo_f303k8/firmware.elf} verify reset; shutdown;”
    xPack OpenOCD x86_64 Open On-Chip Debugger 0.11.0+dev (2021-10-16-21:15)
    Licensed under GNU GPL v2
    For bug reports, read
    OpenOCD: Bug Reporting
    debug_level: 2

Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : DEPRECATED target event trace-config; use TPIU events {pre,post}-{enable,disable}
srst_only separate srst_nogate srst_open_drain connect_deassert_srst

Info : clock speed 1000 kHz
Info : STLINK V2J46M31 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.233136
Error: init mode failed (unable to connect to the target)
in procedure ‘program’
** OpenOCD init failed **
shutdown command invoked

*** [upload] Error 1

Am running this on an ubuntu 22.04, and i thought it was ModemManager that was grabing the SWD pin so that when OpenOCD wants to grab them and to reset, they are not available, i could be wrong. But this is how i did in on my box

sudo systemctl stop ModemManager brltty 2>/dev/null || true

and sometimes i just tried to disable it

sudo systemctl disable --now ModemManager

All these trials didnt seem to solve the issues for me. This is the current configurations in my platformio.ini file.

[env:nucleo_f303k8]
platform = ststm32
board = nucleo_f303k8
framework = stm32cube
upload_protocol = stlink
platform_packages =
platformio/tool-openocd@2.1100.211028
build_flags = -Wl,-u,_printf_float

I would also like to point out that i programmed arduino nanos, and i had no problem uploading the code to them. Only the STM32 is the one thats problematic. still on the same ubuntu platform.