HOWTO: Flashing with ST-Link v2

User avatar
dima
Posts: 157
Joined: Sun Dec 09, 2018 9:35 pm
Location: Canada

HOWTO: Flashing with ST-Link v2

Post by dima »

stlinkv2.png
stlinkv2.png (7.27 KiB) Viewed 15067 times

Option 1: Command-Line (Windows/Mac/Linux)
https://github.com/texane/stlink/releases/tag/1.3.0

USB Drivers: (Windows)
https://zadig.akeo.ie

Code: Select all

st-flash.exe write stm32_loader.bin 0x08000000 --reset
or

Code: Select all

st-flash.exe write stm32_loader.hex --reset
Note: BIN require to specify address, HEX do not (built-in inside)
Bootloader: 0x08000000
Firmware: 0x08001000

Option 2: GUI (Windows Only)
https://www.st.com/en/development-tools ... nk004.html (registration required)

Image

How does it work?

By default STM32 operates with JTAG. ST-Link is a SWD (Serial over Wire) only needs 2 pins - clock and data in/out

To start SWD we need to send "magic" sequence.

First it sends 50+ ones

Code: Select all

wireWrite(0xFFFFFFFF, 32);
wireWrite(0xFFFFFFFF, 32);
then it sends the magic

Code: Select all

wireWrite(0xE79E, 16); // 0b0111100111100111
and then 50+ more ones

Code: Select all

wireWrite(0xFFFFFFFF, 32);
wireWrite(0xFFFFFFFF, 32);
and completes with 50+ zeros

Code: Select all

wireWrite(0, 32);
wireWrite(0, 32);
Here is the official link page 1088

There is more after (like getting ID code) but this is the basics.
arber333
Posts: 3241
Joined: Mon Dec 24, 2018 1:37 pm
Location: Slovenia
Has thanked: 74 times
Been thanked: 223 times
Contact:

Re: HOWTO: Flashing with ST-Link v2

Post by arber333 »

If you want to only load compiled files i recommend you read my quick instructions below.
I just remove Olimex from board and programm it through JTAG connector using only 4 pins with ST link dongle. Its great because it works in Windows and because it is more portable. Granted i need a PC for this too. Here is procedure that works with .hex files.
https://www.st.com/en/development-tools/st-link-v2.html
https://www.st.com/en/development-tools ... nk009.html Also observe Android phone loading :).
First get the app:
https://play.google.com/store/apps/deta ... 2&hl=en_US

I have sucessfuly updated Olimex board using ZFlasher STM32, my Android phone and ST link V2 dongle with USB OTG cable.
1. I erased flash - OK
2. I downloaded firmware to the phone - OK
3. I used bootloader code and flashed Olimex - OK
4. I flashed again with Firmware code - OK
I got flashing green light as serial began to work.

For flashing with Android i use .hex file.
Attachments
IMG_20181019_195352_HDR.jpg
arber333
Posts: 3241
Joined: Mon Dec 24, 2018 1:37 pm
Location: Slovenia
Has thanked: 74 times
Been thanked: 223 times
Contact:

Re: HOWTO: Flashing with ST-Link v2

Post by arber333 »

I have found some info on how to programm the STM32 chip on the blue pill.
I will use ST-Link like before. Some instructions here:
https://idyl.io/arduino/how-to/program- ... 2f103c8t6/

Or here:
https://stm32programming.blogspot.com/2 ... -uart.html

Connecting the Blue Pill to the STM-32
__ST-LINK V2................ Blue Pill
PIN 2.... SWCLK............. CLK
PIN 4.... SWDIO............. IO
PIN 6.... GND................ GND
PIN 8.... 3.3V................ 3.3V
When programming with ST link, it’s not necessary to move the jumpers on the Blue Pill board.

Of course the file upload procedure is the same as for the Olimex H103.
It worked the first time for me. I just have to use .hex files. Bin files cause chip lockout for some reason.
Attachments
33811986-beae634a-ddcd-11e7-9861-c7706aeb3a4d.png
1_NwPYrVoPUbciDWzvGsTavQ.png
1_NwPYrVoPUbciDWzvGsTavQ.png (11.47 KiB) Viewed 14922 times
doobedoobedo
Posts: 260
Joined: Sat Jan 12, 2019 12:39 am
Location: UK

Re: HOWTO: Flashing with ST-Link v2

Post by doobedoobedo »

Quick word of warning about the cheap chinese st-link clones; The markings for SWDIO and SWDCLK may be reversed on the casing (I've experienced this myself) If you get weird errors then swap the cables over and try again. It can't do any harm so long as you just swap those two.
damian.lo
Posts: 123
Joined: Sat Dec 22, 2018 12:46 pm
Location: Poland

Re: HOWTO: Flashing with ST-Link v2

Post by damian.lo »

Thanks Arber for instruction. Do You know if the same procedure is for programming STM32 on v3 board from Johannes - means ST-Link connect to board with brand new uc and put bootloader and software?
arber333
Posts: 3241
Joined: Mon Dec 24, 2018 1:37 pm
Location: Slovenia
Has thanked: 74 times
Been thanked: 223 times
Contact:

Re: HOWTO: Flashing with ST-Link v2

Post by arber333 »

OK i will show you how to connect to Rev. 3 board

First you have to get ST-Link USB gizmo.
Next you need SWD to JTAG or some such adapter. You may work out your own from SWD cable, but i use Olimex adapter here
https://www.olimex.com/Products/ARM/JTA ... TAG-20-10/

Then you connect SWD cable to Rev 3 board and to JTAG adapter. Observe the red stripe.
From JTAG then is the mirror connection as to Olimex STM32H103.
Programming procedure is exactly the same as with H103.
IMG_20200102_150600.jpg
IMG_20200102_150609.jpg
IMG_20200102_150622.jpg
IMG_20200102_150719.jpg
IMG_20200102_150734.jpg
User avatar
dima
Posts: 157
Joined: Sun Dec 09, 2018 9:35 pm
Location: Canada

Re: HOWTO: Flashing with ST-Link v2

Post by dima »

v3_pinout.png
arber333
Posts: 3241
Joined: Mon Dec 24, 2018 1:37 pm
Location: Slovenia
Has thanked: 74 times
Been thanked: 223 times
Contact:

Re: HOWTO: Flashing with ST-Link v2

Post by arber333 »

Yeah... that too :D.
damian.lo
Posts: 123
Joined: Sat Dec 22, 2018 12:46 pm
Location: Poland

Re: HOWTO: Flashing with ST-Link v2

Post by damian.lo »

Thanks all, once I will finish soldering I will try to program board.
Another question about parts in BOM for v3 board, but I will ask in correct forum.

Thanks for quick reply.
damian.lo
Posts: 123
Joined: Sat Dec 22, 2018 12:46 pm
Location: Poland

Re: HOWTO: Flashing with ST-Link v2

Post by damian.lo »

Hi,

Today I finally finished soldering v3 board - now 5V and 3V3 are present :) so my next step is programming.
Of course it can't be so easy, because when I want to connect via ST-Link utility I have error with communication like can't connect to target.
I saw somewhere, that can be problem with usb so I tried to put to another USB port - without success. So - next days I will spend with searching of connection problem - nice.
In those milesteps it looks, that I will try inverter in next year - unfortunately now is January.
Have someone any idea what to do?
arber333
Posts: 3241
Joined: Mon Dec 24, 2018 1:37 pm
Location: Slovenia
Has thanked: 74 times
Been thanked: 223 times
Contact:

Re: HOWTO: Flashing with ST-Link v2

Post by arber333 »

You want to inspect the onboard chip traces if you somehow made a cold solder joint.
I would recommend you solder 4 short cables to programming port and put a JST connector on the other end, so you have firm connection like DImas drawing.
You can translate SWD connector directly to 4 pin connector on STLink. It is even drawn on the underside of the STLink.

SWD ___________STLink
Vcc ____________pin8
GND ___________pin6
SWDIO _________pin4
SWCLK ________ pin2

STLink has to be updated and you have to install drivers of course.
damian.lo
Posts: 123
Joined: Sat Dec 22, 2018 12:46 pm
Location: Poland

Re: HOWTO: Flashing with ST-Link v2

Post by damian.lo »

Thanks,

I will look today at joints, but instead of this maybe I will decrease length of programming cables (now is about 40cm). I've already soldered cables directly to pcb, because I don't have small connector.
Yesterday also I found on YT, that sometimes on cheap ST-Link (like mine) description on top is different than on pcb, so I will check also this today and give report here.
Look at this video:

Regards.
doobedoobedo
Posts: 260
Joined: Sat Jan 12, 2019 12:39 am
Location: UK

Re: HOWTO: Flashing with ST-Link v2

Post by doobedoobedo »

Don't like having to quote myself, but...
doobedoobedo wrote: Wed Jan 01, 2020 9:53 pm Quick word of warning about the cheap chinese st-link clones; The markings for SWDIO and SWDCLK may be reversed on the casing (I've experienced this myself) If you get weird errors then swap the cables over and try again. It can't do any harm so long as you just swap those two.
damian.lo
Posts: 123
Joined: Sat Dec 22, 2018 12:46 pm
Location: Poland

Re: HOWTO: Flashing with ST-Link v2

Post by damian.lo »

Yes doobedoobedo, You have right - I saw somewhere Your post in past and had it in back of head :) unfortunately it was something with pc/drivers, because today it starts work at half throttle :(
Half because ST-Link see STM32, but was unable to erase chip. Ok so I tried to program bootloader without erasing a few times and now I have info"
Can not read memory! Disable Read Out Protection and retry"
I tried to take of protection by change state in Target/Option bytes but without success. Nice.
Arber - thanks also for Your helping hand - I checked cold solders, was ok luckily.
arber333
Posts: 3241
Joined: Mon Dec 24, 2018 1:37 pm
Location: Slovenia
Has thanked: 74 times
Been thanked: 223 times
Contact:

Re: HOWTO: Flashing with ST-Link v2

Post by arber333 »

damian.lo wrote: Wed Jan 15, 2020 7:21 pm Yes doobedoobedo, You have right - I saw somewhere Your post in past and had it in back of head :) unfortunately it was something with pc/drivers, because today it starts work at half throttle :(
Half because ST-Link see STM32, but was unable to erase chip. Ok so I tried to program bootloader without erasing a few times and now I have info"
Can not read memory! Disable Read Out Protection and retry"
I tried to take of protection by change state in Target/Option bytes but without success. Nice.
Arber - thanks also for Your helping hand - I checked cold solders, was ok luckily.
If you have problems with your STLink, just order another from different supplier. You can play with other circuits while you wait for it.
If the problem is STLink it will save you much trouble, if it is not, then it will confirm the truth.

Oh, try updating firmware on STLink sometimes it is old unit...
Also try reinstalling drivers for it.
damian.lo
Posts: 123
Joined: Sat Dec 22, 2018 12:46 pm
Location: Poland

Re: HOWTO: Flashing with ST-Link v2

Post by damian.lo »

STLink, just order another - probably I will try this also
updating firmware on STLink - done, nothing changed
reinstalling drivers - done, nothing changed

I found somewhere, that my error about memory protection means, that memory is really blocked and is needed to unblock via boot0 boot1 jumpers to 0 or 1, don't remember exactly, otherwise will be blocked forever...
So - resold STM32? Damn, it's to many problems with this. Next one and I will need order board from Johannes and burn into fire this one.
doobedoobedo
Posts: 260
Joined: Sat Jan 12, 2019 12:39 am
Location: UK

Re: HOWTO: Flashing with ST-Link v2

Post by doobedoobedo »

arber333 wrote: Wed Jan 15, 2020 7:31 pm If you have problems with your STLink, just order another from different supplier. You can play with other circuits while you wait for it.
If the problem is STLink it will save you much trouble, if it is not, then it will confirm the truth.

Oh, try updating firmware on STLink sometimes it is old unit...
Also try reinstalling drivers for it.
+1 for this. For really cheap stuff I'll get one from a couple of different vendors. Whichever arrives first gets used and I know that I already have a spare on the way.
damian.lo
Posts: 123
Joined: Sat Dec 22, 2018 12:46 pm
Location: Poland

Re: HOWTO: Flashing with ST-Link v2

Post by damian.lo »

I agree, yesterday I ordered another one ST-Link to check if this is problem. Unfortunately I think, that STM32 is somehow blocked and I will have to replace him with new one. Maybe someone else who works more with them can explain what to do in this case. Tomorrow I hope parts will arrive so next result I will write.
arber333
Posts: 3241
Joined: Mon Dec 24, 2018 1:37 pm
Location: Slovenia
Has thanked: 74 times
Been thanked: 223 times
Contact:

Re: HOWTO: Flashing with ST-Link v2

Post by arber333 »

damian.lo wrote: Thu Jan 16, 2020 10:48 am I agree, yesterday I ordered another one ST-Link to check if this is problem. Unfortunately I think, that STM32 is somehow blocked and I will have to replace him with new one. Maybe someone else who works more with them can explain what to do in this case. Tomorrow I hope parts will arrive so next result I will write.
Well i suggest you try to re/install ST Link V2 software and board firmware. Maybe windows 10 is inhibiting its performance somehow. Try starting utility with admin rights.

https://www.st.com/content/st_com/en/pr ... s-software
damian.lo
Posts: 123
Joined: Sat Dec 22, 2018 12:46 pm
Location: Poland

Re: HOWTO: Flashing with ST-Link v2

Post by damian.lo »

Thx, but yesteray evening I did this also. Windows is not a problem, because I prefer to work on Win7. Maybe try to admin rights today.
Strange, because was good on first connection and then somehow blocked memory reading. What I saw on web, I'm not only one with this problem.
It's very annoying to stop on small problem like this, sice BMW is waiting for this heart and is possible to make some trial ride, even on supports in workshop, ehh.
Anyway - thanks guys for good feedback. I hope somehow I will be able to start this asap. Today I will try out to program this.
damian.lo
Posts: 123
Joined: Sat Dec 22, 2018 12:46 pm
Location: Poland

Re: HOWTO: Flashing with ST-Link v2

Post by damian.lo »

Hello,

Ok, finally board was possible to program, but was necessary to replace STM32. Somehow was blocked memory and I was unable to disable this feature. What I found on ST comunity, sometimes when user break writing process and switch off power, memory goes to memory read protect and then on Level 2 it is unpossible to undo. So, I replaced this (in meantime tried with another ST-Link, the same error, tried with UART bootloader demo programmer, the same) and then was possible to program STM32.
YES - after programming bootloader + firmware I saw that green LED starts blinking - yeah.
Next - I bought clone of ESP8266 called ESP-01S but unfortunately with 1MB flash and... of course firmware via USB-TTL goes perfectly and I had out of memory for DATA :)
Good, that doobedoobedo put idea to buy more cheap stuff and together I bought WEMOS D1 mini with 4MB flash - here I had totally no problem with programming - it was even faster 4x than ESP. Everything was good, wifi was visible and was possible to login - second yeah :)
Today I tried to connect v3 board and WEMOS and everything is working good, parameters are visible and is possible to download-upload param.json to board.
One question: why was necessary to upload gaugues.html to see page with gaugues - I need also to upload something more what I don't know?

Second: I didn't try to solder test board+ upload test firmware to v3 board - is it possible to upload only stm32-test via wifi?

Thanks guys for a big help - probably without exact procedure for programming will be more difficult to do this. Thanks.
Attachments
v3 board+wemosD1.JPG
MattsAwesomeStuff
Posts: 871
Joined: Fri Apr 26, 2019 5:40 pm
Has thanked: 278 times
Been thanked: 165 times

Re: HOWTO: Flashing with ST-Link v2

Post by MattsAwesomeStuff »

So, just to summarize the last two pages, Damian.Io's problem was:

An error that said: "can't connect to target" in ST-Link software.
Which later said: "Can not read memory! Disable Read Out Protection and retry" in ST-Link Software

And after being suspicious about the ST-LINK dongle, was in the end just a bad STM32 chip that had a wrecked bootloader, which is not an uncommon problem that can happen if power is removed during programming, that he thinks was done before he even got the chip to begin with.

It was fixed by replacing the STM32 chip. Which is a bit like "My car won't start" and fixing it by "I bought a new car".
arber333
Posts: 3241
Joined: Mon Dec 24, 2018 1:37 pm
Location: Slovenia
Has thanked: 74 times
Been thanked: 223 times
Contact:

Re: HOWTO: Flashing with ST-Link v2

Post by arber333 »

MattsAwesomeStuff wrote: Sun Jan 19, 2020 10:49 pm It was fixed by replacing the STM32 chip. Which is a bit like "My car won't start" and fixing it by "I bought a new car".
This here analogy is a bit exaggerated. Maybe more like you would replace an engine... :D.

Good job Damian!
MattsAwesomeStuff
Posts: 871
Joined: Fri Apr 26, 2019 5:40 pm
Has thanked: 278 times
Been thanked: 165 times

Re: HOWTO: Flashing with ST-Link v2

Post by MattsAwesomeStuff »

arber333 wrote: Mon Jan 20, 2020 5:50 amThis here analogy is a bit exaggerated. Maybe more like you would replace an engine... :D.
I guess.

My point was that, for people who'd come across the issue and were hoping to find a solution or to fix that part, that the solution ended up being "It means it's broken. Throw it away and buy a new one."
damian.lo
Posts: 123
Joined: Sat Dec 22, 2018 12:46 pm
Location: Poland

Re: HOWTO: Flashing with ST-Link v2

Post by damian.lo »

But exactly because of this summary mine solution was - replace engine. If on my knowledge level I tried everyhing, sped some hours of searching solution, trials and I found nothing - the cheapest (according to working hour) and fastest way was to replace engine. Previously I wrote some post about this issue because I hoped, that someone else had the same problem - like for example with ST-Link missed connectors. My skill with STM is poor, so the best was in my case was replace because it is visible result - IS working well. Maybe is some other solution, like I wrote with BOOT0 and BOOT1 pins but result was uncertain. Of course any new idea how to repair blocked cpu is strongly appreciated - my old STM32 will wait in storage.
Post Reply