Skip to content
Mikael Wikstrom edited this page Jan 2, 2017 · 9 revisions

Having fun with OpenOCD, ST-Link V2 and a STM32F103C8T6 dev board (STM32_Smart v2.0)

This project was based on notes from rogerclarkmelbourne/Arduino_STM32

Originally I got myself a ST-Link v2 programmer because I needed to fix one of my gadgets that had failed during firmware upgrade over USB so I thought I would fix it using SWD or JTAG. In the end it was never needed as the upgrade had failed due to a bad USB cable and once I changed it I was able to upgrade the firmware successfully.

So... Now I had a ST-Link v2 and a cheep dev board named STM32_Smart v2.0 with a stm32f103c8t6, both from Ali. So lets see if I can get them to work with OpenOCD?

ST-Link V2

STM32_Smart V2.0

First step was to get the wiring right. Four wires: Vcc, GND, CLK and IO. No rocket-surgery there!

ST-Link v2 pinout STM32_Smart v2 SWD pinout ST-Link V2 and STM32_Smart v2.0 hooked up to my laptop ST-Link V2

stm32duino was a helpful resource, this is where I found the SWD pinout.

Ok, skipped one step here. I first tried out the STM_Smart board USB to my linux laptop for reference and got this from dmesg.

[  896.776235] usb 2-1: new full-speed USB device number 16 using xhci_hcd
[  896.888250] usb 2-1: device descriptor read/64, error -71
[  897.104251] usb 2-1: device descriptor read/64, error -71
[  897.320231] usb 2-1: new full-speed USB device number 17 using xhci_hcd
[  897.432270] usb 2-1: device descriptor read/64, error -71
[  897.648283] usb 2-1: device descriptor read/64, error -71
[  897.864272] usb 2-1: new full-speed USB device number 18 using xhci_hcd
[  897.864500] usb 2-1: Device not responding to setup address.
[  898.068437] usb 2-1: Device not responding to setup address.
[  898.272260] usb 2-1: device not accepting address 18, error -71
[  898.384300] usb 2-1: new full-speed USB device number 19 using xhci_hcd
[  898.384530] usb 2-1: Device not responding to setup address.
[  898.588528] usb 2-1: Device not responding to setup address.
[  898.792321] usb 2-1: device not accepting address 19, error -71
[  898.792394] usb usb2-port1: unable to enumerate USB device

This was with what was pre-loaded.

Next up was to try and see it was willing to talk to OpenOCD.

Wired in the dev board four wire to the ST-Link v2 USB dongle connected to my laptop and started OpenOCD from default ubuntu repository. Please see my Makefile for what commands I used.

make ocd
Open On-Chip Debugger 0.10.0-dev-00463-g0c2de8b-dirty (2016-12-23-00:03)
Licensed under GNU GPL v2
For bug reports, read
       `http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
none separate
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : clock speed 950 kHz
Info : STLINK v2 JTAG v17 API v2 SWIM v4 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 3.536842
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : accepting 'telnet' connection on tcp/4444
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08001b68 msp: 0x20000918
dumped 131071 bytes in 2.844688s (44.996 KiB/s)
Info : dropped 'telnet' connection

OpenOCD will start to listen on port 4444 and accept cli commands from telnet on that port.

telnet 127.0.0.1 4444
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Open On-Chip Debugger
> reset halt
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08001b68 msp: 0x20000918
> dump_image dump.bin 0x8000000 0x1ffff
dumped 131071 bytes in 2.844688s (44.996 KiB/s)
> exit
Connection closed by foreign host.

Awesome!

Now lets see if we can load or even compile and load something.

...

Skipping ahead a bit. See Makefile

make git
...
make maple-mini
...
make osd
Open On-Chip Debugger 0.10.0-dev-00463-g0c2de8b-dirty (2016-12-23-00:03)
Licensed under GNU GPL v2
For bug reports, read
       `http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
none separate
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : clock speed 950 kHz
Info : STLINK v2 JTAG v17 API v2 SWIM v4 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 3.486645
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : accepting 'telnet' connection on tcp/4444
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x080000f0 msp: 0x20005000
auto erase enabled
Info : device id = 0x20036410
Info : flash size = 64kbytes
target halted due to breakpoint, current mode: Thread 
xPSR: 0x61000000 pc: 0x2000003a msp: 0x20005000
wrote 7168 bytes from file maple_mini_boot20.bin in 0.447116s (15.656 KiB/s)
Info : dropped 'telnet' connection

and in another terminal my telnet session

make telnet
telnet 127.0.0.1 4444
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Open On-Chip Debugger
> reset halt
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08001b68 msp: 0x20000918
> flash write_image erase maple_mini_boot20.bin 0x08000000
auto erase enabled
device id = 0x20036410
flash size = 64kbytes
target halted due to breakpoint, current mode: Thread 
xPSR: 0x61000000 pc: 0x2000003a msp: 0x20000918
wrote 7168 bytes from file maple_mini_boot20.bin in 0.448962s (15.592 KiB/s)
> reset run
> exit
Connection closed by foreign host.
Makefile:6: recipe for target 'telnet' failed
make: *** [telnet] Error 1

Real Awesome!

So what parts did I "skip". I first downloaded maple_mini_boot20.bin and loaded it successfully. I then tried to compile my own and tested but when connecting the dev board usb to my laptop I got the same output as when it had been pre-loaded with. Apparently gcc-arm-none-eabi 4.9 does not play well with this code. So had to switch from gcc 4.9 to gcc 4.8. (Perhaps I should try gcc 5)

rmadison gcc-arm-none-eabi
gcc-arm-none-eabi | 4.8.2-14ubuntu1+6    | trusty/universe  | amd64, arm64, armhf, i386, powerpc
gcc-arm-none-eabi | 4.8.3-18ubuntu2+12   | vivid/universe   | amd64, arm64, armhf, i386, powerpc, ppc64el
gcc-arm-none-eabi | 6                    | trusty/universe  | source
gcc-arm-none-eabi | 12                   | vivid/universe   | source
gcc-arm-none-eabi | 15:4.9.3+svn231177-1 | xenial/universe  | source, amd64, arm64, armhf, i386, powerpc, ppc64el, s390x
gcc-arm-none-eabi | 15:4.9.3+svn231177-1 | yakkety/universe | source, amd64, arm64, armhf, i386, powerpc, ppc64el, s390x
gcc-arm-none-eabi | 15:5.4.1+svn241155-1 | zesty/universe   | source, amd64, arm64, armhf, i386, powerpc, ppc64el, s390x
echo "deb http://se.archive.ubuntu.com/ubuntu/ vivid universe main" | sudo tee /etc/apt/sources.list.d/stm32.list
sudo apt-get update
sudo apt-get install gcc-arm-none-eabi=4.8.3-18ubuntu2+12
make maple-mini
...

This produced a maple-mini bootloader that was "not-broken" when the dev-board was connected to my laptop. Nice!

And this was the output of dmesg and lsusb -vvvv

dmesg
[ 1765.466893] usb 2-1: new full-speed USB device number 29 using xhci_hcd
[ 1765.596077] usb 2-1: New USB device found, idVendor=1eaf, idProduct=0003
[ 1765.596081] usb 2-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 1765.596084] usb 2-1: Product: Maple 003
[ 1765.596085] usb 2-1: Manufacturer: LeafLabs
[ 1765.596087] usb 2-1: SerialNumber: LLM 003
lsusb -vvvv
...
Bus 002 Device 029: ID 1eaf:0003  
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               1.00
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0x1eaf 
  idProduct          0x0003 
  bcdDevice            2.01
  iManufacturer           1 LeafLabs
  iProduct                2 Maple 003
  iSerial                 3 LLM 003
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           45
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0 
    bmAttributes         0x80
      (Bus Powered)
    MaxPower              100mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           0
      bInterfaceClass       254 Application Specific Interface
      bInterfaceSubClass      1 Device Firmware Update
      bInterfaceProtocol      2 
      iInterface              4 STM32duino bootloader v1.0  ERROR. Upload to RAM not supported.
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       1
      bNumEndpoints           0
      bInterfaceClass       254 Application Specific Interface
      bInterfaceSubClass      1 Device Firmware Update
      bInterfaceProtocol      2 
      iInterface              5 STM32duino bootloader v1.0  Upload to Flash 0x8005000
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       2
      bNumEndpoints           0
      bInterfaceClass       254 Application Specific Interface
      bInterfaceSubClass      1 Device Firmware Update
      bInterfaceProtocol      2 
      iInterface              6 STM32duino bootloader v1.0  Upload to Flash 0x8002000
      Device Firmware Upgrade Interface Descriptor:
        bLength                             9
        bDescriptorType                    33
        bmAttributes                        3
          Will Not Detach
          Manifestation Intolerant
          Upload Supported
          Download Supported
        wDetachTimeout                    255 milliseconds
        wTransferSize                    1024 bytes
        bcdDFUVersion                   1.10
Device Status:     0x0001
  Self Powered
...

I think that is enough for today. On to something else.

Clone this wiki locally