Close

Flashing multiple images with two stlinkv2 at once

A project log for stm8-easymon

Use 7 segment displays to monitor numbers of any kind

andrew-clappAndrew Clapp 04/27/2020 at 19:470 Comments

Multiple Programmers!  I have a project where I need to have two STM8's with different code on them in the same circuit.  So, I wanted to build a programmer that could simultaneously put code on two STM8's at once using two USB plugged stlinkv2 programmers.  The programmer is based on a raspberry pi 3 B+ and I used raspbian buster full as the OS.  It has the compliler and almost all the bits needed right out of the box.  I had to install libusb dev to build the stm8flash binary.  

Then I ran into a snag.

The stlink's showed up as /dev/stlinkv2_2 and /dev/stlinkv2_3 so I figured that I'd simply point the stm8flash at them thusly:

stm8flash -c stlinkv2 -d /dev/stlinkv2_2 -p stm8s103f3 -s flash -w IMG1.hex 

and

stm8flash -c stlinkv2 -d /dev/stlinkv2_3 -p stm8s103f3 -s flash -w IMG2.hex 

No dice.  The stm8flash simply flashes to the first one it sees both times.  You need to pass stm8flash the -S flag with the stlinkv2's internal serial number.  Then I had to dig in and learn about serial numbers.  I tried the usual things, lsusb -v, and dmesg, and digging around in /proc, and usb-devices and so on.  No matter where I pulled the serial number from, it was a string of unintelligible and some non-printable characters.  I could not 

It then dawned on me that I had the source for stm8flash.  *lightbulb*  

At line 233 of main.c (at least in my copy), I found this:

                                // print programmer data if no serial number specified
                                if(!pgm_serialno_specified) {
                                        fprintf(stderr, "Programmer %d: %s %s, Serial Number:%s\n", numOfProgrammers, vendor, device, serialno_hex);
                                }

I'm not sure how the logic is intended to be triggered to print the serial number from command line use of stm8flash.  It seems like if there is more than one programmer, and the serial is not specified, it should print them, but it did not work that way for me.  However, if you copy the fprintf() statement to outisde of the if() { }, then it prints them regardless, and you get a nice happy hexidicimal string that works with stm8flash.  Since I had my system copy installed in /usr/local/bin, I just used the local source copy to print out serial numbers and I'm sure one could probably not kludge it up like me, but I was being impatient.

pi@raspberrypi:~/stm8flash-master $ ./stm8flash -d /dev/stlinkv2_3 -c
stlinkv2 -p stm8s103f3 -u
Determine OPT area
WARNING: More than one programmer found but no serial number given.
Programmer 1 will be used:
Programmer 1: STMicroelectronics STM32 STLink, Serial
Number:553F6606483F57541227073F
Programmer 1: STMicroelectronics STM32 STLink, Serial
Number:553F6606483F57541227073F
Programmer 2: STMicroelectronics STM32 STLink, Serial
Number:503F6F06483F57562156203F
Programmer 2: STMicroelectronics STM32 STLink, Serial
Number:503F6F06483F57562156203F
Due to its file extension (or lack thereof), "Workaround" is
considered as RAW BINARY format!
Tries exceeded

Now to flash the multiple images to their respective STM8's, I just run this:

stm8flash -c stlinkv2 -d /dev/stlinkv2_2 -p stm8s103f3 -S 553F6606483F57541227073F -s flash -w IMG1.hex 

and

stm8flash -c stlinkv2 -d /dev/stlinkv2_3 -p stm8s103f3 -S 503F6F06483F57562156203F -s flash -w IMG2.hex 

Discussions