How to fix PlatformIO Error: libusb_open() failed with LIBUSB_ERROR_ACCESS

Problem

When you try to flash your firmware using PlatformIO, you see an error message such as

Building in release mode
Checking size .pio/build/nucleo_h743zi/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   7.4% (used 38876 bytes from 524288 bytes)
Flash: [          ]   3.2% (used 67004 bytes from 2097152 bytes)
Configuring upload protocol...
AVAILABLE: blackmagic, cmsis-dap, jlink, mbed, stlink
CURRENT: upload_protocol = stlink
Uploading .pio/build/nucleo_h743zi/firmware.elf
xPack Open On-Chip Debugger 0.12.0-01004-g9ea7f3d64-dirty (2023-01-30-15:03)
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 1.45 seconds =======================================================================

Solution

Your current system user does not have access to USB devices. In order to fix that, use

sudo usermod -a -g plugdev $USER

Note that the change will only take effect after you log out and log back in again, or after a reboot.

Additionally, you need to install the current PlatformIO udev rules:

curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/system/99-platformio-udev.rules | sudo tee /etc/udev/rules.d/99-platformio-udev.rules

and then restart udev:

sudo systemctl restart udev

After that, unplug and re-plug the USB cable leading to the microcontroller, or reboot your system.

After doing all that, your upload should work:

Checking size .pio/build/nucleo_h743zi/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   7.4% (used 38876 bytes from 524288 bytes)
Flash: [          ]   3.2% (used 67004 bytes from 2097152 bytes)
Configuring upload protocol...
AVAILABLE: blackmagic, cmsis-dap, jlink, mbed, stlink
CURRENT: upload_protocol = stlink
Uploading .pio/build/nucleo_h743zi/firmware.elf
xPack Open On-Chip Debugger 0.12.0-01004-g9ea7f3d64-dirty (2023-01-30-15:03)
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

[stm32h7x.cpu0] halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x0800a0d0 msp: 0x24080000
** Programming Started **
Warn : Adding extra erase range, 0x08010880 .. 0x0801ffff
** Programming Finished **
** Verify Started **
** Verified OK **
** Resetting Target **
shutdown command invoked
=========================