Spark Logic

Black Magic Probe on an stm32f103c8

Rick Kimball

11/07/2017 – I attached a binary that can be loaded onto your blue pill in this post: viewtopic.php?f=37&t=2801

12/3/2016 – You can now contribute to developers of the Black Magic Probe project (which isn’t me) using paypal. Find and click the paypal contribute button here: https://github.com/blacksphere/blackmagic

12/8/2016 – Instead of using this port of Black Magic Probe, it is a lot easier to just compile the PROBE_HOST=stlink and load that onto your bluepill. The bluepill hardware is the same as the stlink v1/v2 hardware. Just follow the steps from the Black Magic Probe software for building and installing the BMP stlink platform. The advantage being, you will get the latest BMP version and you will be able to update future versions just using the USB cable on the bluepill with a program called usb-util. The posts in this thread can be thought of as an interesting read on my journey from bluepill newbie to a somewhat proficient users of it.

The Black Magic Probe is a modern, in-application programmer and debugging software for embedded microprocessors. It allows you to see what is going on ‘inside’ an application running on an embedded microprocessor while it executes. It is able to control and examine the state of the target microprocessor using a JTAG or SWD (Serial Wire debugging) port and on-chip debug logic provided by the microprocessor. The probe connects to a host computer using a standard USB interface. The user is able to control exactly what happens using the GNU source level debugging software, GDB. Read more about it here: https://github.com/blacksphere/blackmagic/wiki

However, the stm32duino crowd here will find the open source Black Magic firmware more appealing than the hardware. The Black Magic Probe firmware can be loaded onto the low cost stm32c8t6 boards you find on eBay. You can then use that clone board with BMP firmware to take the place of an ST-Link programmer. Once the BMP firmware is loaded on a clone board, it provides the ability to upload to the flash of another target board. It does this using a gdb server built into the BMP firmware. This allows for hardware debugging using just arm-none-eabi-gdb without any host server code.

I started with the latest github version of the Black Magic Probe software and added platform support for the stm32f103c8t6 boards you find on ebay. I call it the ‘jc66_coreboard’ platform, as that is the label printed on the silk screen. Any board with the same pin outs should work (See Red Pill Blue Pill post for more info about the hardware). The new platform is based mostly on the already existing stlink platform source with changes to support the different hardware pin outs and features.

The ebay “Red Pill” board loaded with the Black Magic Probe firmware acting like a programmer:
Image
by Rick Kimball, on Flickr

Target board (another “Red Pill” board) connections from the “Red Pill” BMP programmer:
Image

I’ve attached a zip file containing the blackmagic.bin file, the blackmagic elf file, and patch file for the source. ( NOTE: I will fork BMP github code and create a branch with the source changes). For now, just download and unpack the zip and then upload the bin or elf file to your jc66_coreboard.

If you want to build the source yourself, do something like the following:
$ git clone https://github.com/blacksphere/blackmagic
Cloning into 'blackmagic'...
remote: Counting objects: 3463, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3463 (delta 0), reused 0 (delta 0), pack-reused 3461
Receiving objects: 100% (3463/3463), 1.70 MiB | 0 bytes/s, done.
Resolving deltas: 100% (2331/2331), done.
Checking connectivity... done.
$ cd blackmagic
$ unzip ../bmp_jc66_coreboard.zip
Archive: ../bmp_jc66_coreboard.zip
inflating: blackmagic
inflating: blackmagic.bin
inflating: bmp_jc66_coreboard.patch
$ patch -p1 <bmp_jc66_coreboard.patch
patching file src/Makefile
patching file src/command.c
patching file src/platforms/common/cdcacm.c
patching file src/platforms/jc66_coreboard/Makefile.inc
patching file src/platforms/jc66_coreboard/Readme
patching file src/platforms/jc66_coreboard/jc66_coreboard.png
patching file src/platforms/jc66_coreboard/platform.c
patching file src/platforms/jc66_coreboard/platform.h
$ make
$ cd src
$ make install # assuming it go OK, you will have to change the path for the st-write utility to match your system

Replies20

jcw Sun Nov 22, 2015 9:53 am

You mean which of the two virtual serial ports of a BMP is for gdb and which is serial pass-through?
On Mac OSX, the gdb port’s name always end in “1”, serial pass-through always ends in “3”.

RogerClark Sun Nov 22, 2015 10:05 am

@jcw

Yes. Thats what I was trying to ask

Unfortunately on Windows its not so clear as they just have 2 different COM numbers and I think it matters which order you install the drivers which around the 2 comm numbers are assigned

Duncan Cragg Sun Jan 17, 2016 5:09 pm

Hi Roger

I’m looking for a super-easy, beginner’s flasher for my Arduino-on-nRF51822 project.

So that means:

  • – very cheap (£10/$15 say)
    – very easily sourced (so Adafruit ideally)
    – no flasher needed to create the flasher! (so already flashed, software-driven or flashable through serial)

I’ve been playing with ST-Link/V2+OpenOCD, which already meets these requirements, and have pre-ordered an IBDAP and some FT232H boards, both of which could also meet them, along with OpenOCD.

But I discovered BMP through your own Arduino-on-nRF51822 article
http://www.rogerclark.net/arduino-on-th … ontroller/
and this gives us two significant bonuses:

  • – the GDB connection
    – the Serial connection

Which brings me back to the above requirements list, and a list of questions about STM32F103-based boards – which I’m hoping you’ll kindly answer.. :D

Option 1 – re-purpose an ST-Link/V2 mini:

  • – can I use something like the ST firmware upgrader to re-flash an ST-Link/V2 mini through USB?
    – or somehow through Serial like you did? (I believe they have SWD pads, but that’s the circular problem)

Option 2 – commodity STM32F103 boards:

  • – what is this ‘red pill’ / ‘blue pill’ thing all about? :? are these essentially Maple Minis?
    – can they all be flashed through serial or is the one you used (which you said was a Maple Mini) special?
    – are they only available from China? I see local UK suppliers, but I’m not sure what I’m getting

I’d also love to see a photo of (a) your board being flashed through serial, and (b) your board all wired up to the target! I find pictures make it easier for me to understand what’s going on!

Hope that’s not too much to ask for a first posting to this forum!

Cheers!
Duncan

RogerClark Sun Jan 17, 2016 8:44 pm

Duncan

It is possible to put the BMP onto a STLink dongle, but there are reasons why this is not ideal…

STLink dongles do not break out the Serial port pins (PA9 and PA10) and solderimg to the STM32 is challenging

You cant use the STLink updated progam by STM to update to BMP, you have to flash using another STLink.

So I would keep your STlink to program other STM32 and STM8 boards

Re:Blue Pill etc
This is the name someone coined for the cheap generic STM32F103C8 boards that are easily available on eBay. They are a bit like an Arduino Nano, just a bit longer.

But a better option is the Maple mini, which is the same size but is well made and has the 128k flash F103CB chip

Im not sure who sells STM32 boards apart from the eBay and Aliexpress vendors, but if you want local stock, just look on ebay for a local supplier of the Maple Mini board

Once you have the Maple mini or the blue pill etc, you can flash the bmp using your stlink

BTW. I added Segger JLink as an upload option, and Jlink compatible programmers are available farily cheaply on eBay, and the home / educational version from Segger is not too expensive, But both options are more expensive than using a Maple mini.

I’m currently using Jlink, but may go back to BMP, as I have some wierd things going on with a WT51822-S2 module that I’m trying to use.

The most reliable modules to start with, seem to be the development motherboard and modules from WaveShare. They all work fine for me ( both the QFAC and QFAA device types ).
BTW. I dont have any QFAB modules as they are only 128k flash and the S130 Softdevice takes 109k, and user code starts at 112k, which does not leave enough room for a user application that actually uses the Softdevice.

Duncan Cragg Sun Jan 17, 2016 11:00 pm

Thanks for that quick and detailed reply, Roger, much appreciated.

Sad that the ST-Link/V2 can’t be flashed by serial – or rather, we haven’t managed to work out how ST do it with their upgrade tool through USB!

So I want the users of my project to be able to easily buy a board and download BMP to it via serial – but I don’t know where I’d point them to find a board – they could either pick up a Maple Mini clone or one of the red/blue boards it seems? Could you provide links to definitive examples of each to get me started – I may have to just buy one of each and try it myself.. :)

Do the red/blue boards support serial flashing on PA9/10 like the Maple? I found this article on your site, which implies that they do:

http://www.rogerclark.net/stm32f103-and … 1-5-x-ide/

Is that a ‘blue pill’, then?

Cheers!
Duncan

RogerClark Sun Jan 17, 2016 11:30 pm

Duncan

Most STM32 board support flashing via serial. The serial bootloader is built into the hardware of the F103 chip

The only reason that its hard to flash the STLink dongles is that the pins for PA9 and PA10 and also for Boot0 are hard to get to, as they are not broken out onto a pad or pin

The STLink dongles generally have the SWD (SWDIO and SWCLK) pins plus power and ground, connected to 4 small pads on the dongle, so that its possible for mere mortals to solder onto the pads, and connect another STLink (or BMP etc) and program them

If you are good at soldering it would be possible to solder directly onto pins PA9 and PA10 and Boot0 on the chip and connect a USB to serial adaptor and also to set Boot0 HIGH (you may also need to set Boot1 low, as this may be floating – I can’t remember if the STLink dongles bother to tie this low as Boot1 only makes a difference when Boot1 is HIGH)

The Blue and Red Pill are basically the forums’ names for the board you found on my blog (http://www.rogerclark.net/stm32f103-and … 1-5-x-ide/)
e.g you can get them on ebay for a few pounds.

This is an example on eBay, but you may find cheaper or want to get from a local supplier
http://www.ebay.co.uk/itm/STM32F103C8T6 … Sw6aVUo1Pq

For the Maple mini, I forget precisely which thread its in, but most people buy them from a company called “Baite” as they actually manufacture the boards, and hence you get ones which have passed their QA process.

Often Maple Mini’s from other suppliers are actually Baite reject boards that have been reflow soldered to fix dry joints.
And I think perhaps there are other manufacturers..

@mrburnette is the Maple mini supply expert

But any STM32F103 board should technically be capable of being flashed with the BMP firmware via Serial

I have versions for the Blue / Red pill as well as the Maple mini (which has different USB reset hardware), and also the GD32 (like the STM32 but faster) (but the GD32 boards normally have a 12Mhz crystal instead of an 8Mhz on the STM32 – though this is not why the GD32 is faster, as the master clock frequencies for them both is still 72Mhz)

zmemw16 Mon Jan 18, 2016 1:50 am

there is a maple mini not from Baite, it’s got a red led, not the Baites bright brilliant blue and the silkscreen is a subtly different style.
price is similar.

i’d suggest you get a Baites 5 off set, go to their store and search for ‘STM32’, pricing is almost the same per unit as a singleton, single postage though.

i don’t think i’ve killed a single 103c8t6 as yet, but definitely a zet6 with 12v and not any of the pills i have, similar comment as to purchasing them.

stephen

mrburnette Mon Jan 18, 2016 2:27 am

Baite store on Aliexpress

5 pack Maple Mini clone

Yes, you may find some $0.50 less. However, my belief is to build a relationship with a vendor who has consistently providedme with excellent product – out of 40+ devices, 100% satisfied. Ss they say, your results msy vary.

Warning: Chinese New Year is next month – almost all of China will be on vacation. Expect delays.

Ray

Duncan Cragg Mon Jan 18, 2016 10:01 pm

Thanks mrburnette, zmemw16 and Roger for all that excellent detailed and useful info – bookmarked!

Could I ask a couple more questions..? On USB flashing – which I have just realised may be an option instead of PA9/10/Boot0 or SWD.

I believe Maple clones can be flashed over USB, but you (Roger) didn’t suggest that approach in your article: http://www.rogerclark.net/arduino-on-th … ontroller/ – was there a reason for that?

I presume there’s no hope of flashing a Red/Blue board over USB?

As we know, ST-Links can be upgraded over USB (http://www.st.com/web/en/catalog/tools/PF258194), so has anyone looked around the ST Java code to see how to do generic flashing? I poked around and saw AES encryption in there, so maybe they’ve got some kind of protection guff.

Finally, if you did flash BMP onto an ST-Link mini, using any approach, do you think you could use the non-SWD pins (marked RST and SWM on my board) as the serial connection, if BMP were re-built accordingly?

Apologies if all this is out there and I shoulda known it! :-)

Cheers!

Duncan

RogerClark Mon Jan 18, 2016 11:47 pm

Duncan

The STM32F103 does not have a built in USB upload feature built into the chip (the STM32F4 series does but not the F1 series)

Normally when we program the Maple mini and the generic STM32F103 boards using the Arduino IDE, we use the STM32duino-bootloader
(This is updated version of the original Maple-bootloder written by Leaflabs)

The bootloader comes pre-installed on Maple mini boards and can be flashed onto generic STM32 boards (e.g. Blue Pill), and then remains resident and provides the upload / reflashing via USB.

i.e when you power on the Maple mini it runs the bootloader code in the base of flash which enumarates on USB as a DFU device for about 1 second.

If the host PC does not start sending data to the DFU device in about the first second after booting, the bootloader looks to see if there is any code at the sketch start address 0x8002000 and if so it jumps to that address and the sketch starts to run.

In order to get the BMP to work in this way, it would need to be compiled so that the base address of the code is 0x8002000 instead of the start of flash (which is 0x800000)
I did briefly look at doing this, but the code that would need to be changed seems to be deep inside libcm3 and it looked like it would take quite some time to modify, test and debug.

So as there was very little gain in doing this apart from allowing Maple mini users to run the BMP as if it was a sketch, I didnt feel it was worth me spending any of my very limited time on doing that as an option.

Re: Repurposing some of the pins on the STlink as Serial

Its a nice idea, but I’m not sure whether it would be possible

Although the STM32F103 has multiple USARTS, I’m not sure if any of them are on the pins use by RESET and SWIM

You’d need to find a schematic for the STLink stick and find which pins are used (I think one of them my be PB0), you’d then need to check if any of them are USART pins.
If they are not primary USART pins, there are remap / alternative pins for the USARTS, so you need to check that as well

If they are remap pins, in theory you could modify the BMP code to remap the pins and use the appropriate USART.

However the pins may not be both on the same USART

I suspect you are making yourself a huge amount of work.

zmemw16 Tue Jan 19, 2016 3:48 am

the v2.0 of the bootloader is a major speed improvement, certainly on the maple mini, not done it with a pill.

flashing a pill for bmp install is easy, it’s puzzled me or i was unsure and then it was so simple i posted a comment to that effect.
istr 2 photo’s from a rick thread, combining them shows all 4 wires that will connected to the target, programming it is
via 4 pins on end of the pill. careful pin counting is reccomended. also there is a link to a binary zip you can install, beginning of the thread and it gets updated.

after that it gets interesting and maybe a tad confusing for my braincell. openocd is suggested. using it is not a gui.

stephen

Rick Kimball Mon Jul 04, 2016 3:00 am

Go back to the start. Delete all those extra things you downloaded. Get rid of the launchpad arm-none-eabi-gcc toolchain. Unpack the stm32duino stuff in the right place. Then start up the IDE and install the boards package for the Arduino Zero. The will get rid of the missing g++. Then report back.

https://github.com/rogerclarkmelbourne/ … stallation

Step 2:

Run the IDE, and on the Tools menu, select the Boards manager, and install the Arduino Due from the list of available boards.

*NOTE: this step installed the arm-none-eabi-g++

Rick Kimball Mon Jul 04, 2016 3:03 am

Are you running the bootloader on your stm32f103c8?

Darius Mon Jul 04, 2016 3:49 am

Rick Kimball wrote:Go back to the start. Delete all those extra things you downloaded. Get rid of the launchpad arm-none-eabi-gcc toolchain. Unpack the stm32duino stuff in the right place. Then start up the IDE and install the boards package for the Arduino Zero. The will get rid of the missing g++. Then report back.

https://github.com/rogerclarkmelbourne/ … stallation

Step 2:

Run the IDE, and on the Tools menu, select the Boards manager, and install the Arduino Due from the list of available boards.

*NOTE: this step installed the arm-none-eabi-g++

alex Mon Aug 29, 2016 5:25 am

BMP and gdb can load(flash) .hex now.
‘STM32 ST-LINK Utility’ is needed for convert .bin to .hex, I tried to use arm-none-eabi-objcopy for convertion but failed.
$ arm-none-eabi-gdb -batch -ex 'tar ext COM3' -ex 'mon ver' -ex 'mon sw' -ex 'at 1' -ex 'mon option erase' -ex 'load x.hex'
Black Magic Probe (Firmware xxx) (Hardware Version 1)
Copyright (C) 2015 Black Sphere Technologies Ltd.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

Target voltage: Not Implemented!
Available Targets:
No. Att Driver
1 STM32F1 medium density
0x0800cc98 in ?? ()
0x1FFFF800: 0x5AA5
0x1FFFF802: 0xFFFF
0x1FFFF804: 0xFFFF
0x1FFFF806: 0xFFFF
0x1FFFF808: 0xFFFF
0x1FFFF80A: 0xFFFF
0x1FFFF80C: 0xFFFF
0x1FFFF80E: 0xFFFF
Loading section .sec1, size 0x2000 lma 0x8000000
Loading section .sec2, size 0x2000 lma 0x8002000
Loading section .sec3, size 0x2000 lma 0x8004000
Loading section .sec4, size 0x2000 lma 0x8006000
Loading section .sec5, size 0x2000 lma 0x8008000
Loading section .sec6, size 0x2000 lma 0x800a000
Loading section .sec7, size 0x2000 lma 0x800c000
Loading section .sec8, size 0x318 lma 0x800e000
Start address 0x0, load size 58136

RogerClark Mon Aug 29, 2016 6:10 am

You should be able to get the IDE to generate HEX

Try using the Export Compiled Binary menu option on the Sketch menu

alex Mon Aug 29, 2016 7:35 am

RogerClark wrote:You should be able to get the IDE to generate HEX

Try using the Export Compiled Binary menu option on the Sketch menu

RogerClark Mon Aug 29, 2016 10:42 am

alex wrote:

This is only for closed source firmware.

edogaldo Wed Oct 26, 2016 2:14 pm

Hi there, I was planning to create my BMP for upload & debug purpose.
I’ve seen suggestions about using the cheapest blue pill anyway I have a concern on this: blue pills (and newer black pills – viewtopic.php?f=28&t=117&start=170#p18316) have a quite low power LDO so:
  • Would the blue pill LDO be enough to power up the target board, upload and debug at least non power demanding sketches?
  • In case the power provided by the blue pill based BMP is not enough, how else should I power the target board?
  • Could it be better to use a board which features a AMS1117?!

Thanks in advance and bye, E.

Side questions for who have both the blue pill and the new black pill: which one would you suggest and why?

Post a reply