STM32 + VS Code + mbed upload issue: Error: libusb_open() failed with LIBUSB_ERROR_ACCESS Error: open failed

Hello,

I’m experimenting with STM32F401CCU6 based board.
As mentioned here it should be close to DISCO_F401VC target.

The problem is an attempt to program it from VS Code + Platformio (both newest versions) using St-Link.

The code is compiling and building, but not uploading to target.

The error log is:

> Executing task: C:\Users\UserName\.platformio\penv\Scripts\platformio.exe run --target upload <

Processing disco_f401vc (platform: ststm32; board: disco_f401vc; framework: mbed)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/disco_f401vc.html
PLATFORM: ST STM32 5.7.0 > ST 32F401CDISCOVERY
HARDWARE: STM32F401VCT6 84MHz, 64KB RAM, 256KB Flash
DEBUG: Current (stlink) On-board (stlink) External (blackmagic, jlink)
PACKAGES: toolchain-gccarmnoneeabi 1.70201.0 (7.2.1), framework-mbed 5.51401.191023 (5.14.1), tool-stm32duino 1.0.2, tool-openocd 2.1000.190707 (10.0), tool-dfuutil 1.9.190708
Collecting mbed sources...
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 4 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Checking size .pio\build\disco_f401vc\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
DATA:    [=         ]   5.7% (used 3768 bytes from 65536 bytes)
PROGRAM: [==        ]  18.1% (used 47412 bytes from 262144 bytes)
Configuring upload protocol...
AVAILABLE: blackmagic, jlink, stlink
CURRENT: upload_protocol = stlink
Uploading .pio\build\disco_f401vc\firmware.elf
xPack OpenOCD, 64-bit Open On-Chip Debugger 0.10.0+dev (2019-07-17-11:28)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 1

srst_only separate srst_nogate srst_open_drain connect_deassert_srst

Error: libusb_open() failed with LIBUSB_ERROR_ACCESS
Error: open failed
in procedure 'program'
** OpenOCD init failed **
shutdown command invoked

*** [upload] Error 1
============================================================================== [FAILED] Took 6.63 seconds ==============================================================================
The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

But if I’l take compiled file from .pio\build\disco_401vc\firmware.bin and use STM32 ST-Link Utility to program board: it works.

Is there a way to fix this problem?

1 Like

Hm interesting I’ve only seen this on linux before when you don’t have the rights to access the device.

have you installed the ST-Link drivers from STSW-LINK009 - ST-LINK, ST-LINK/V2, ST-LINK/V2-1, STLINK-V3 USB driver signed for Windows7, Windows8, Windows10 - STMicroelectronics? Does it show up properly in the windows device manager?

1 Like

Yes, I use fresh drivers and STLink is shown in Device manager as STM32 STLink
I also updated the STLink firmware.

P.S. I doesn’t look like a problem of rights to access device. I just tested it: replaced the board with BluePill and selected the board as bluepill_f103c8, and programming was successful.

  • what’s the output of pio run -v (Verbose Upload)?
  • do you have the STLink utility open at the same time?
1 Like
PS C:\Users\UserName\Documents\PlatformIO\Projects\blink> pio run -v
Processing disco_f401vc (platform: ststm32; board: disco_f401vc; framework: mbed; upload_protocol: stlink)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/disco_f401vc.html
PLATFORM: ST STM32 5.7.0 > ST 32F401CDISCOVERY
HARDWARE: STM32F401VCT6 84MHz, 64KB RAM, 256KB Flash
DEBUG: Current (stlink) On-board (stlink) External (blackmagic, jlink)
PACKAGES: toolchain-gccarmnoneeabi 1.70201.0 (7.2.1), framework-mbed 5.51401.191023 (5.14.1)
Collecting mbed sources...
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 4 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
MethodWrapper(["checkprogsize"], [".pio\build\disco_f401vc\firmware.elf"])
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
DATA:    [=         ]   5.7% (used 3768 bytes from 65536 bytes)
PROGRAM: [==        ]  18.1% (used 47412 bytes from 262144 bytes)
.pio\build\disco_f401vc\firmware.elf  :

section             size        addr

.text              44628   134217728

.ARM.exidx             8   134262356

.data               2776   536871320

.bss                 992   536874096

.heap              57264   536875088

.ARM.attributes       46           0

.comment             126           0

.debug_info       348082           0

.debug_abbrev      51867           0

.debug_loc         64539           0

.debug_aranges      7056           0

.debug_ranges       8856           0

.debug_line        84714           0

.debug_str         64362           0

.debug_frame       23444           0

Total             758760
============================================================================= [SUCCESS] Took 6.89 seconds ============================================================================= 

No, StLink Utility is closed.

Ah sorry pio run -t upload -v

PS C:\Users\UserName\Documents\PlatformIO\Projects\blink> pio run -t upload -v
Processing disco_f401vc (platform: ststm32; board: disco_f401vc; framework: mbed; upload_protocol: stlink)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/disco_f401vc.html
PLATFORM: ST STM32 5.7.0 > ST 32F401CDISCOVERY
HARDWARE: STM32F401VCT6 84MHz, 64KB RAM, 256KB Flash
DEBUG: Current (stlink) On-board (stlink) External (blackmagic, jlink)
PACKAGES: toolchain-gccarmnoneeabi 1.70201.0 (7.2.1), framework-mbed 5.51401.191023 (5.14.1), tool-stm32duino 1.0.2, tool-openocd 2.1000.190707 (10.0), tool-dfuutil 1.9.190708
Collecting mbed sources...
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 4 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
MethodWrapper(["checkprogsize"], [".pio\build\disco_f401vc\firmware.elf"])                                                                                                                
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
DATA:    [=         ]   5.7% (used 3768 bytes from 65536 bytes)
PROGRAM: [==        ]  18.1% (used 47412 bytes from 262144 bytes)
.pio\build\disco_f401vc\firmware.elf  :

section             size        addr

.text              44628   134217728

.ARM.exidx             8   134262356

.data               2776   536871320

.bss                 992   536874096

.heap              57264   536875088

.ARM.attributes       46           0

.comment             126           0

.debug_info       348082           0

.debug_abbrev      51867           0

.debug_loc         64539           0

.debug_aranges      7056           0

.debug_ranges       8856           0

.debug_line        84714           0

.debug_str         64362           0

.debug_frame       23444           0

Total             758760
<lambda>(["upload"], [".pio\build\disco_f401vc\firmware.elf"])
AVAILABLE: blackmagic, jlink, stlink
CURRENT: upload_protocol = stlink
openocd -d2 -s "C:\Users\UserName\.platformio\packages\tool-openocd/scripts" -f board/stm32f4discovery.cfg -c "program {.pio\build\disco_f401vc\firmware.elf}  verify reset; shutdown;"
xPack OpenOCD, 64-bit Open On-Chip Debugger 0.10.0+dev (2019-07-17-11:28)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 2

Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
srst_only separate srst_nogate srst_open_drain connect_deassert_srst

Info : clock speed 2000 kHz
Info : STLINK V2J34S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 2.963428
Info : stm32f4x.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : Listening on port 3333 for gdb connections
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Error: timed out while waiting for target halted
embedded:startup.tcl:449: Error: ** Unable to reset target **
in procedure 'program' 
in procedure 'program_error' called at file "embedded:startup.tcl", line 474
at file "embedded:startup.tcl", line 449
*** [upload] Error 1
============================================================================== [FAILED] Took 7.83 seconds ==============================================================================

Aha so the error suddenly changed and now it does find the STLink v2 probe. Can you try to connect the NRST signal of the STLink to the R pin (reset) of the target board and retry?

Short:

> Executing task: C:\Users\UserName\.platformio\penv\Scripts\platformio.exe run --target upload <

Processing disco_f401vc (platform: ststm32; board: disco_f401vc; framework: mbed)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/disco_f401vc.html
PLATFORM: ST STM32 5.7.0 > ST 32F401CDISCOVERY
HARDWARE: STM32F401VCT6 84MHz, 64KB RAM, 256KB Flash
DEBUG: Current (stlink) On-board (stlink) External (blackmagic, jlink)
PACKAGES: toolchain-gccarmnoneeabi 1.70201.0 (7.2.1), framework-mbed 5.51401.191023 (5.14.1), tool-stm32duino 1.0.2, tool-openocd 2.1000.190707 (10.0), tool-dfuutil 1.9.190708
Collecting mbed sources...
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 4 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Checking size .pio\build\disco_f401vc\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
DATA:    [=         ]   5.7% (used 3768 bytes from 65536 bytes)
PROGRAM: [==        ]  18.1% (used 47412 bytes from 262144 bytes)
Configuring upload protocol...
AVAILABLE: blackmagic, jlink, stlink
CURRENT: upload_protocol = stlink
Uploading .pio\build\disco_f401vc\firmware.elf
xPack OpenOCD, 64-bit Open On-Chip Debugger 0.10.0+dev (2019-07-17-11:28)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 1

srst_only separate srst_nogate srst_open_drain connect_deassert_srst

Error: timed out while waiting for target halted
embedded:startup.tcl:449: Error: ** Unable to reset target **
in procedure 'program' 
in procedure 'program_error' called at file "embedded:startup.tcl", line 474
at file "embedded:startup.tcl", line 449
*** [upload] Error 1
============================================================================== [FAILED] Took 7.45 seconds ==============================================================================
The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

Long:

PS C:\Users\UserName\Documents\PlatformIO\Projects\blink> pio run -t upload -v
Processing disco_f401vc (platform: ststm32; board: disco_f401vc; framework: mbed; upload_protocol: stlink)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/disco_f401vc.html
PLATFORM: ST STM32 5.7.0 > ST 32F401CDISCOVERY
HARDWARE: STM32F401VCT6 84MHz, 64KB RAM, 256KB Flash
DEBUG: Current (stlink) On-board (stlink) External (blackmagic, jlink)
PACKAGES: toolchain-gccarmnoneeabi 1.70201.0 (7.2.1), framework-mbed 5.51401.191023 (5.14.1), tool-stm32duino 1.0.2, tool-openocd 2.1000.190707 (10.0), tool-dfuutil 1.9.190708
Collecting mbed sources...
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 4 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
MethodWrapper(["checkprogsize"], [".pio\build\disco_f401vc\firmware.elf"])
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
DATA:    [=         ]   5.7% (used 3768 bytes from 65536 bytes)
PROGRAM: [==        ]  18.1% (used 47412 bytes from 262144 bytes)
.pio\build\disco_f401vc\firmware.elf  :

section             size        addr

.text              44628   134217728

.ARM.exidx             8   134262356

.data               2776   536871320

.bss                 992   536874096

.heap              57264   536875088

.ARM.attributes       46           0

.comment             126           0

.debug_info       348082           0

.debug_abbrev      51867           0

.debug_loc         64539           0

.debug_aranges      7056           0

.debug_ranges       8856           0

.debug_line        84716           0

.debug_str         64362           0

.debug_frame       23444           0

Total             758762
<lambda>(["upload"], [".pio\build\disco_f401vc\firmware.elf"])
AVAILABLE: blackmagic, jlink, stlink
xPack OpenOCD, 64-bit Open On-Chip Debugger 0.10.0+dev (2019-07-17-11:28)
CURRENT: upload_protocol = stlink
Licensed under GNU GPL v2
openocd -d2 -s "C:\Users\UserName\.platformio\packages\tool-openocd/scripts" -f board/stm32f4discovery.cfg -c "program {.pio\build\disco_f401vc\firmware.elf}  verify reset; shutdown;"
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 2

Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
srst_only separate srst_nogate srst_open_drain connect_deassert_srst

Info : clock speed 2000 kHz
Info : STLINK V2J33S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 6.392157
Info : stm32f4x.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : Listening on port 3333 for gdb connections
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Error: timed out while waiting for target halted
embedded:startup.tcl:449: Error: ** Unable to reset target **
in procedure 'program' 
in procedure 'program_error' called at file "embedded:startup.tcl", line 474
at file "embedded:startup.tcl", line 449
*** [upload] Error 1
============================================================================== [FAILED] Took 7.49 seconds ==============================================================================

Can you hold down the “BOOT0” button on the board, then press the NRST button once, then release the BOOT0 button, and retry programming?

Same result.

P.S. Interesting observation. I also have original Nucleo-F411RE. If I’m wiring its ST-Link part to target board : uploads from VS Code works (only if reset pin is connected). External ST-Link - no.

Okay let’s try a few reset configurations. Can you go in C:\Users\UserName\.platformio\packages\tool-openocd\scripts\board\ in the file stm32f4discovery.cfg, instead of the line reset_config srst_only, test these options in sequence:

  • reset_config none
  • reset_config none separate
  • reset_config srst_only
  • reset_config srst_only srst_nogate
  • reset_config srst_only srst_nogate connect_assert_srst
  • reset_config trst_and_srst srst_nogate connect_assert_srst

Ideally test the same config twice, once with NRST connected to the probe and one go without it connected.

Only those are interesting for which the openOCD error is different.

1 Like

reset_config value:

  • none : uploaded both ways ( either reset was connected and disconnected)
  • none separate : uploaded both ways
  • srst_only srst_nogate : both ways Error: ** Unable to reset target **
  • srst_only srst_nogate connect_assert_srst : both ways Error: ** Unable to reset target **
  • trst_and_srst srst_nogate connect_assert_srst : both ways Error: ** Unable to reset target **

But it is still weird that STLink from Nucleo board worked fine without modification of original files.

Thats what bit me, my stlink utility was open.
Thx

Hi,

I have similiar issue like OP just the circumstances are different. I have a windows machine with freshly installed VSCode and PlatformIO running the remote agent. A Nucleo L432KC board is attached to this machine.

I am trying to remote upload a firmware to the Nucleo board from a different machine (which has fresh VSCode and PlatformIO installed, also I am able to program the nucleo board directly from this machine).

When I try to upload remotely the firmware I am getting the libusb_open()… error message in the terminal of the host machine.

On the remote machine, there is no error message and the last message is: “Remote command received: run”

Could someone help me out, how to resolve this problem? There is not much information in the wild or I just failed to find them…

Does the remote machine have ST-Link drivers installed? https://www.st.com/en/development-tools/stsw-link009.html

Many thanks, that was the problem.

One question remained from my side: sometimes when I try to program the chip the “init mode failed” error message appears in the terminal during uploading. I can resolve this by clearing the chip with CubeProgrammer, but it need physical intervention for this (holding the reset button on the board while connecting with the programmer).

Do you know what can be the root cause so I could avoid this in the future?

In my case, this issue was because

“openocd_board”: “st_nucleo_f4”

in the board definition, deleting this fixed the problem.

Please use the following command to install the driver for the ST-Link: sudo apt -y install stlink-tools . By doing so, it should start working properly.