Installing ST-Link v2 to flash STM32 targets on Linux
Posted in Embedded By Grig On October 23, 2020This guide is targeting Linux users and ST-Link v2 hardware.
First, we are going to install the necessary libraries and build tools:
sudo apt-get install git make cmake libusb-1.0-0-dev sudo apt-get install gcc build-essential
Now, we will download and build the ST-Link utilities:
cd ~myusername mkdir stm32 cd stm32 git clone https://github.com/stlink-org/stlink cd stlink cmake . make
Now we copy the built binaries to their place:
cd bin sudo cp st-* /usr/local/bin cd ../lib sudo cp *.so* /lib32
then udev rules:
sudo cp stlink/config/udev/rules.d/49-stlinkv* /etc/udev/rules.d/
Now, plug the ST-Link device into the USB port, but DON’T CONNECT the target board, then type:
lsusb
If everything went OK, you should see something like this:
then:
Now, disconnect the ST-LINK from USB, connect the four wires to the target board as in the picture
then plug back the ST-Link to the USB and type:
st-flash read dummy.bin 0 0xFFFF
You should see something like:
If the target board is not connected, the ST-Link device will be identified, but you (obviously) will get an error during memory read.
We can decide it is correctly installed.
Please add comments if you encounter any issue using this guide. I hope it will help you.
Thanks for the effort to all contributors to this repository https://github.com/stlink-org/stlink .
Check these articles related to STM32.
The older version of this post is here.
I found the st-* files under /bin directory , not under /stlink .
Must the other files ” test-* ” also being copied to /usr/local/bin or is that not important ?
Thanks for letting me know about the issue, I have corrected. If you think you’ll never need those test binaries, don’t copy them.
You are the best! Working well
😉 Thanks. Keep an eye on the page.
Hi there,
evething goes well,
marco@marco:~/stm32/stlink/bin$ lsusb
Bus 002 Device 005: ID 046d:c526 Logitech, Inc. Nano Receiver
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 004: ID 0483:3748 STMicroelectronics ST-LINK/V2
Bus 001 Device 003: ID 0781:556b SanDisk Corp. Cruzer Edge
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
accept …
marco@marco~/stm32/stlink/bin$ st-info –probe
2020-06-04T03:44:35 ERROR usb.c: Could not open USB device 0x0483:0x3748, access error.
Found 0 stlink programmers
the device shows up, but probing it returns error.
what do i need to do now?
please, i would love to finally compile the blinking led example.
Hi,
Perhaps a nice addition, also with Linux build.
https://github.com/EmBitz/EBlink
I started this project 4 years ago because I was a bit frustrated about texane in those days. For every new device a lot of changes and rebuild were necessary.
I consider adding a post about using GDB with stlink. Thanks for the idea.
after a reboot ….
marco@marco:/home/marco# st-info –probe
[!] send_recv read reply failed: LIBUSB_ERROR_TIMEOUT
[!] send_recv STLINK_DEBUG_ENTER
[!] send_recv send request failed: LIBUSB_ERROR_TIMEOUT
[!] send_recv STLINK_JTAG_DRIVE_NRST
[!] send_recv send request failed: LIBUSB_ERROR_TIMEOUT
[!] send_recv STLINK_DEBUG_RESETSYS
[!] send_recv send request failed: LIBUSB_ERROR_TIMEOUT
[!] send_recv STLINK_DEBUG_READCOREID
[!] send_recv send request failed: LIBUSB_ERROR_TIMEOUT
[!] send_recv STLINK_JTAG_READDEBUG_32BIT
Found 1 stlink programmers
serial: 493f6e066772565630332267
hla-serial: “\x49\x3f\x6e\x06\x67\x72\x56\x56\x30\x33\x22\x67”
flash: 0 (pagesize: 0)
sram: 0
chipid: 0x0000
descr: unknown device
marco@marco:/home/marco# st-flash –debug read dummy.file 0 256
st-flash 1.6.0-368-gade735d
2020-06-04T03:57:01 DEBUG common.c: *** looking up stlink version
[!] send_recv send request failed: LIBUSB_ERROR_TIMEOUT
[!] send_recv STLINK_GET_VERSION
2020-06-04T03:57:04 DEBUG common.c: *** looking up stlink version
[!] send_recv send request failed: LIBUSB_ERROR_TIMEOUT
[!] send_recv STLINK_GET_VERSION
[!] send_recv send request failed: LIBUSB_ERROR_TIMEOUT
[!] send_recv STLINK_GET_CURRENT_MODE
2020-06-04T03:57:10 DEBUG common.c: stlink mode: unknown!
2020-06-04T03:57:10 DEBUG usb.c: JTAG/SWD freq set to 0
2020-06-04T03:57:10 DEBUG common.c: *** set_swdclk ***
[!] send_recv send request failed: LIBUSB_ERROR_TIMEOUT
[!] send_recv STLINK_GET_CURRENT_MODE
2020-06-04T03:57:13 DEBUG common.c: stlink mode: unknown!
2020-06-04T03:57:13 DEBUG common.c: *** stlink_enter_swd_mode ***
[!] send_recv send request failed: LIBUSB_ERROR_TIMEOUT
[!] send_recv STLINK_DEBUG_ENTER
2020-06-04T03:57:16 DEBUG common.c: *** stlink_jtag_reset ***
[!] send_recv send request failed: LIBUSB_ERROR_TIMEOUT
[!] send_recv STLINK_JTAG_DRIVE_NRST
2020-06-04T03:57:19 DEBUG common.c: *** stlink_reset ***
[!] send_recv send request failed: LIBUSB_ERROR_TIMEOUT
[!] send_recv STLINK_DEBUG_RESETSYS
2020-06-04T03:57:22 DEBUG common.c: Loading device parameters….
2020-06-04T03:57:22 DEBUG common.c: *** stlink_core_id ***
[!] send_recv send request failed: LIBUSB_ERROR_TIMEOUT
[!] send_recv STLINK_DEBUG_READCOREID
2020-06-04T03:57:25 ERROR common.c: Failed to read core_id
[!] send_recv send request failed: LIBUSB_ERROR_TIMEOUT
[!] send_recv STLINK_JTAG_READDEBUG_32BIT
2020-06-04T03:57:28 WARN common.c: unknown chip id! 0x7f90
Failed to connect to target
but fails on:
st-flash –debug read dummy.file 0 256
what do i need to do now?
now, i have a different serial number and a chipid show up.
found 1 stlink programmers
serial: 303030303030303030303031
hla-serial: “\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x31”
flash: 0 (pagesize: 0)
sram: 0
chipid: 0x0f79
Hello ‘By Natural’
I had exactly the same problem as you.
The fix for me was to use stlink v1.6.0 instead of the latest (v1.6.1). In the author’s instructions above, simply replace the line:
“git clone https://github.com/texane/stlink” with:
“git clone https://github.com/stlink-org/stlink –branch v1.6.0″
Lining up the date of this article with the stlink release dates here:
https://github.com/stlink-org/stlink/releases
It appears the author must have been using v1.5.1, as v1.6.0 was only released in Feb 2020. The build instructions for v1.6.1 here https://github.com/stlink-org/stlink/blob/develop/doc/compiling.md don’t seem to line up with the instructions given by the author.
Perhaps between the releases the manual build process changed, and this is what caused our issue?
Thanks for letting us know. I might need to update the article.
For me it looks like you are not having admin priviledges. Did you try with sudo?
My USB stlink device is listed when I type lsusb. But when I type st-info –probe I get the reply: st-info: command not found. Do you have to be in a subdirectory to run the program?
Update. All is now working fine.
Found 1 stlink programmers
serial: 3f79080110134753384c4e00
hla-serial: “\x3f\x79\x08\x01\x10\x13\x47\x53\x38\x4c\x4e\x00”
flash: 131072 (pagesize: 1024)
sram: 20480
chipid: 0x0410
descr: F1xx Medium-density
Hi Enjay. Could you share how did you manage to resolve the issue?
udev rules are under /stlink/config/udev/rules.d/
Thanks for finding this! Updated!
Hello,
I have exactly the same problem with Enjay. I think there is a dependency problem. when I run
sudo apt install libusb-1.0 I see this message:
libusb-1.0-0-dbg : Depends: libusb-1.0-0 (= 2:1.0.20-1) but 2:1.0.21-2 is to be installed
Does anybody have any idea?
Please see my comment below, on 1 sept
Hi, thanks for the post, but it seems there is an error with the new libusb 1.0.21-2.
Each time I run st-info –probe I get this error :
st-info: symbol lookup error: /usr/lib/libstlink.so.1: undefined symbol: libusb_set_option
I’m I correct in assuming it may be because libusb made a braking change and stlink taxne is not up to date ?
If you know how to correct that it would be greatly appreciated !
Thanks ! 🙂
I guess your libusb version is wrong, even it is newer.
Try using this one: libusb-1.0-0-dev
Hi DarkstartXt have you find a solution for this, I’m facing the same problem here
hi,
when i try flashing i am getting response like
st-flash 1.6.1-96-gbf41f14
2020-09-23T20:41:30 WARN common.c: unknown chip id! 0x5fa0004
Failed to connect to target
any one faced this before?!
It looks either like a wiring error between st-link debugger and target board, or the target has no power.
The article is up to date. It should work now.
Followed the instructions and got the same error as Natural @ June 4.
Tried RB’s solution (downgrade to v1.6) and now I get this output
corv-root: st-info –probe
Found 1 stlink programmers
serial: 563f6906713f555109421267
openocd: “\x56\x3f\x69\x06\x71\x3f\x55\x51\x09\x42\x12\x67”
flash: 0 (pagesize: 0)
sram: 0
chipid: 0x0000
descr: unknown device
BUT when I run the same command a second time, it goes back to the original message
corv-root: st-info –probe
Found 1 stlink programmers
serial: 303030303030303030303031
openocd: “\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x31”
flash: 0 (pagesize: 0)
sram: 0
chipid: 0x0000
descr: unknown device
Running the second command does this:
corv-root: st-flash read dummy.bin 0 0xFFFF
st-flash 1.6.0
2020-10-27T11:08:20 INFO common.c: Loading device parameters….
2020-10-27T11:08:20 WARN common.c: Invalid flash type, please check device declaration
Update: I get the expected results when an STM32 board is plugged in to the St-link
updated
How to get a stlink st st-link/v2 driver?
driver is installed automatically when running this command (presented in the article):
sudo cp stlink/config/udev/rules.d/49-stlinkv* /etc/udev/rules.d/
when send this command ->> st-flash read dummy.bin 0 0xFFFF
i get this :
st-flash 1.7.0-195-g951859c
/usr/local/stlink/chips: No such file or directory
2022-06-25T13:00:30 WARN common.c: unknown chip id! 0x410
Failed to connect to target
what should i do ?
When running lsusb, do you see the STlink device listed? Also, what is return of the st-probe command?
There is the option of using “sudo make install”; Also forgot to mention one needs to run ‘sudo ldconfig’ to let the system know about the new libraries.
When I wrote this guide, in my case was no need of ldconfig. Something could be changed since then.
intererested in stlink st st-link/v2 driver
It should be automatically installed with the software from github, so no other driver is needed.
Hi Grig,
I got the same issue as Ahmed does:
st-flash 1.7.0-201-g254a525
/usr/local/stlink/chips: No such file or directory
2022-08-13T18:01:37 WARN common.c: unknown chip id! 0x413
Failed to connect to target
And when running lsusb I do see:
Bus 001 Device 003: ID 0483:374b STMicroelectronics ST-LINK/V2.1
And the output of st-info –probe is:
/usr/local/stlink/chips: No such file or directory
Chao
Update: After upgrading my virtualbox I got the following output:
st-info –probe
/usr/local/stlink/chips: No such file or directory
Found 1 stlink programmers
version: V2J37S26
serial: 066DFF565588494867235353
flash: 0 (pagesize: 0)
sram: 0
chipid: 0x413
st-flash read dummy.bin 0 0xFFFF
st-flash 1.7.0-201-g254a525
/usr/local/stlink/chips: No such file or directory
2022-08-19T13:30:38 WARN common.c: unknown chip id! 0x413
Failed to connect to target
root@sane64:~/stm32/stlink# cd bin
root@sane64:~/stm32/stlink/bin# sudo cp st-* /usr/local/bin
root@sane64:~/stm32/stlink/bin# cd ../lib
root@sane64:~/stm32/stlink/lib# sudo cp *.so* /lib32
cp: target ‘/lib32’ is not a directory
Do I need to create a dir named /lib32 or ???
instaed of coping manually, just type
$ sudo make install
Thanks for update, Pavel
Thank you, I have been looking for this solution for over two months
thanks for this. at ubuntu 22.04 is getting error: /usr/local/share//stlink/chips: No such file or directory
so we need to copy: /home/{USER}/stm32/stlink/config/chips/ to /usr/local/share/stlink/chips/
thank you!
It is possible that some paths are updated. I see in the path reported in your error, a double slash // -> you could check that, it looks like something minor.
That helped. Thank you.
Hi Grig! I have tried the above steps, but just like Alexander Letetski I am also getting the same error along with the double slash //. I don’t know what it means. Please check the screenshot I have attached in the following link:
https://photos.app.goo.gl/dastUUXmA184vdMh8
Kindly help to resolve the
Hi,
From your picture I get the conclusion that something is wrong with the rights. Try to use sudo before your commands.
Grig
Completely I share your opinion. Idea excellent, I support.
st-flash read dummy.bin 0 0xFFFF
st-flash 1.7.0-296-gf7fb0a6
/usr/local/share//stlink/chips: No such file or directory
2023-09-01T12:33:44 WARN common.c: unknown chip id! 0x497
Failed to connect to target
Failed to parse flash type or unrecognized flash type
Hi,
I am stuck at the command : “sudo cp *.so* /lib32” since there is no directory called /lib32.
Doing “sudo make install” also comes up with the error “no rule to make target install”
The article is pretty old. In the mean time, the paths might changed. When I’ll have a chance, I’ll do a verification.