STM32: Difference between revisions

From base48
imported>Rmarko
(dbg hw, bmp usage)
imported>B42
 
(7 intermediate revisions by one other user not shown)
Line 193: Line 193:
* http://embdev.net/articles/STM_Discovery_as_Black_Magic_Probe
* http://embdev.net/articles/STM_Discovery_as_Black_Magic_Probe
* https://esden.net/2014/12/29/black-magic-discovery/
* https://esden.net/2014/12/29/black-magic-discovery/
* http://nuft.github.io/arm/2015/08/24/blackmagic-stlink.html
* https://mjoldfield.com/atelier/2018/08/nucleo-bmp.html


===== F3 Discovery =====
===== F3 Discovery =====
Line 207: Line 209:
===== Flashing =====
===== Flashing =====


To see device states during the process it is useful to open a second terminal and run
{{cmd|code=
dmesg -Hw
}}
Next build firmware for stlink target and upload it to target
{{cmd|code=
{{cmd|code=
# make PROBE_HOST=stlink
# make PROBE_HOST=stlink
# flashing blackmagic_dfu via black magic probe
# flashing blackmagic_dfu via black magic probe
arm-none-eabi-gdb --ex 'target extended-remote /dev/ttyACM0' blackmagic_dfu
git clone https://github.com/blacksphere/blackmagic/
cd blackmagic
make PROBE_HOST=stlink
arm-none-eabi-gdb --ex 'target extended-remote /dev/ttyACM0' src/blackmagic_dfu
monitor swdp_scan
monitor swdp_scan
attach 1
attach 1
Line 219: Line 230:
# [  +0.000002] usb 1-1.2: Product: Black Magic (Upgrade) for STLink/Discovery, (Firmware v1.6-rc0-257-gd6e2977)
# [  +0.000002] usb 1-1.2: Product: Black Magic (Upgrade) for STLink/Discovery, (Firmware v1.6-rc0-257-gd6e2977)
# upload black magic via dfu
# upload black magic via dfu
dfu-util -s 0x08002000:leave -D blackmagic.bin
#
# disconnect your programmer so only target board is connected
#
sudo dfu-util -s 0x08002000:leave -D src/blackmagic.bin
# if dfu-util can't find your device you can specify -S <serial> from dmesg output
sudo dfu-util -S 7EBA7BA4 -s 0x08002000:leave -D src/blackmagic.bin
}}
 
Sample session:
{{cmd|code=
$ arm-none-eabi-gdb --ex 'target extended-remote /dev/ttyACM0' src/blackmagic_dfu
Reading symbols from src/blackmagic_dfu...done.
Remote debugging using /dev/ttyACM0
(gdb) monitor swdp_scan
Target voltage: unknown
Available Targets:
No. Att Driver
1      STM32F1 medium density
(gdb) attach 1
Attaching to program: /home/rmarko/embedded/blackmagic/src/blackmagic_dfu, Remote target
0x08010064 in ?? ()
(gdb) monitor option erase
0x1FFFF800: 0x0000
0x1FFFF802: 0x0000
0x1FFFF804: 0x0000
0x1FFFF806: 0x0000
0x1FFFF808: 0x0000
0x1FFFF80A: 0x0000
0x1FFFF80C: 0x0000
0x1FFFF80E: 0x0000
(gdb) load
Error erasing flash with vFlashErase packet
# ^^ target needs rebooting to unlock flash
(gdb) quit
 
<TARGET REBOOTED>
 
$ arm-none-eabi-gdb --ex 'target extended-remote /dev/ttyACM0' src/blackmagic_dfu
Reading symbols from src/blackmagic_dfu...done.
Remote debugging using /dev/ttyACM0
(gdb) monitor swdp_scan
Target voltage: unknown
Available Targets:
No. Att Driver
1      STM32F1 medium density
(gdb) attach 1
Attaching to program: /home/rmarko/embedded/blackmagic/src/blackmagic_dfu, Remote target
0xfffffffe in ?? ()
(gdb) load
Loading section .text, size 0x1c48 lma 0x8000000
Loading section .data, size 0x90 lma 0x8001c48
Start address 0x8001498, load size 7384
Transfer rate: 11 KB/sec, 820 bytes/write.
(gdb) quit
 
<TARGET REBOOTED>
 
$ dmesg
[691195.851258] usb 1-1.6: new full-speed USB device number 112 using ehci-pci
[691195.943994] usb 1-1.6: New USB device found, idVendor=1d50, idProduct=6017
[691195.943997] usb 1-1.6: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[691195.943999] usb 1-1.6: Product: Black Magic (Upgrade) for STLink/Discovery, (Firmware v1.6.1-98-g9a5b31c)
[691195.944001] usb 1-1.6: Manufacturer: Black Sphere Technologies
[691195.944002] usb 1-1.6: SerialNumber: 7EBA7BA4
 
$ sudo dfu-util -S 7EBA7BA4 -s 0x08002000:leave -D src/blackmagic.bin
dfu-util 0.9
 
Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2016 Tormod Volden and Stefan Schmidt
This program is Free Software and has ABSOLUTELY NO WARRANTY
Please report bugs to http://sourceforge.net/p/dfu-util/tickets/
 
dfu-util: Invalid DFU suffix signature
dfu-util: A valid DFU suffix will be required in a future dfu-util release!!!
Opening DFU capable USB device...
ID 1d50:6017
Run-time device DFU version 011a
Claiming USB DFU Interface...
Setting Alternate Setting #0 ...
Determining device status: state = dfuIDLE, status = 0
dfuIDLE, continuing
DFU mode device DFU version 011a
Device returned transfer size 1024
DfuSe interface name: "Internal Flash  "
Downloading to address = 0x08002000, size = 60660
Download [=========================] 100%        60660 bytes
Download done.
File downloaded successfully
Transitioning to dfuMANIFEST state
 
<TARGET REBOOTED>
 
$ dmesg
[691227.339977] usb 1-1.6: new full-speed USB device number 113 using ehci-pci
[691227.432954] usb 1-1.6: New USB device found, idVendor=1d50, idProduct=6018
[691227.432956] usb 1-1.6: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[691227.432957] usb 1-1.6: Product: Black Magic Probe (STLINK), (Firmware v1.6.1-98-g9a5b31c)
[691227.432958] usb 1-1.6: Manufacturer: Black Sphere Technologies
[691227.432959] usb 1-1.6: SerialNumber: 7EBA7BA4
[691227.433654] cdc_acm 1-1.6:1.0: ttyACM0: USB ACM device
[691227.434290] cdc_acm 1-1.6:1.2: ttyACM1: USB ACM device
}}
}}


Line 255: Line 367:
}}
}}


===== Tested boards =====
* STLink programmers
* STM32 F4 Discovery
* STM32 IoT node (STM32L475)


== Links ==
== Links ==
Line 265: Line 381:
=== Docs ===
=== Docs ===
* [http://www.st.com/content/ccc/resource/technical/document/user_manual/8a/56/97/63/8d/56/41/73/DM00063382.pdf/files/DM00063382.pdf/jcr:content/translations/en.DM00063382.pdf STM32 F3 Discovery (STM32F303VC) Manual]
* [http://www.st.com/content/ccc/resource/technical/document/user_manual/8a/56/97/63/8d/56/41/73/DM00063382.pdf/files/DM00063382.pdf/jcr:content/translations/en.DM00063382.pdf STM32 F3 Discovery (STM32F303VC) Manual]
* [http://www.st.com/content/ccc/resource/technical/document/user_manual/70/fe/4a/3f/e7/e1/4f/7d/DM00039084.pdf/files/DM00039084.pdf/jcr:content/translations/en.DM00039084.pdf STM32 F4 Discovery (STM32F407VG) Manual]
* [http://www.st.com/content/ccc/resource/technical/document/datasheet/f2/1f/e1/41/ef/59/4d/50/DM00058181.pdf/files/DM00058181.pdf/jcr:content/translations/en.DM00058181.pdf STM32F303VC]
* [http://www.st.com/content/ccc/resource/technical/document/datasheet/f2/1f/e1/41/ef/59/4d/50/DM00058181.pdf/files/DM00058181.pdf/jcr:content/translations/en.DM00058181.pdf STM32F303VC]
* [http://www.st.com/content/ccc/resource/technical/document/reference_manual/3d/6d/5a/66/b4/99/40/d4/DM00031020.pdf/files/DM00031020.pdf/jcr:content/translations/en.DM00031020.pdf STM32F407]
* [http://www.st.com/content/ccc/resource/technical/document/reference_manual/3d/6d/5a/66/b4/99/40/d4/DM00031020.pdf/files/DM00031020.pdf/jcr:content/translations/en.DM00031020.pdf STM32F407]

Latest revision as of 14:57, 9 May 2019

STM32 programming

Boot mode configuration

<syntaxhighlight lang="bash">BOOT1 BOOT0 Boot mode Address

 x     0   Main Flash memory                      0x8000000
 0     1   System memory (built-in bootloader)    0x1FFFB000
 1     1   Embedded SRAM                          0x20000000
 in Main flash memory and System memory boot modes address is aliased to 0x0</syntaxhighlight>


STM32F103C86T dev board boot pins

Legend

 o o o BOOT0
 o o o BOOT1
 ---
 |R|   Reset button
 ---

Boot to bootloader (built-in bootloader)

  • Allows flashing over USART
 o [=]
 [=] o
 ---
 |R|
 ---

Boot to main flash (user code)

 [=] o
 o o o
 ---
 |R|
 ---

Flashing

  • Flashing is done via USART1 (A9 TX A10 RX)

Converting Elf to Bin

Required if you only have elf files produced by your toolchain - stm32flash accepts only *.bin files.

<syntaxhighlight lang="bash"> arm-none-eabi-objcopy -O binary miniblink.elf miniblink.bin </syntaxhighlight>

Using stm32flash

Test connection with

<syntaxhighlight lang="bash"> stm32flash /dev/ttyUSB0 </syntaxhighlight>

Flash and run from 0x0

<syntaxhighlight lang="bash"> stm32flash /dev/ttyUSB0 -w miniblink.bin -g 0x0 </syntaxhighlight>

Pinout

Libraries

libopencm3

Minimalistic firmware library/framework.

Bootstrapping on Fedora

Library

<syntaxhighlight lang="bash">dnf install arm-none-eabi* mkdir embedded git clone https://github.com/libopencm3/libopencm3 cd libopencm3

  1. Edit TARGETS in Makefile and keep only targets that you need (or just stm32/f1 for example).

make -j8 cd ..</syntaxhighlight>

DFU bootloader

This is a bootloader for Device Firmware Update via USB (stm32/f1).

<syntaxhighlight lang="bash">git clone https://git.48.io/libopencm3_dfu cd libopencm3_dfu

  1. connect JTAG

make flash

  1. or flash via USART & stm32flash, see flashing section above

make stm32flash</syntaxhighlight>

Sample application

For use with DFU bootloader (stm32/f1).

<syntaxhighlight lang="bash">git clone https://git.48.io/libopencm3_template cd libopencm3_template

  1. flash via dfu

make dfu

  1. or with JTAG

make flash</syntaxhighlight>

Library examples

<syntaxhighlight lang="bash">git clone https://github.com/libopencm3/libopencm3-examples

  1. Either fetch git submodule pointing to libopencm3 and repeat steps in Library section
  2. or symlink libopecm3 dir to previously built libopencm3

cd libopencm3-examples rmdir libopencm3 ln -s <PATH_TO-->/libopencm3 .

  1. try building miniblink example

cd examples/stm32/f1/stm32-h103/miniblink make</syntaxhighlight>

Troubleshooting

  • use make V=1 when make fails
  • make sure openocd is not running when doing 'make flash'
Locked device

If you can't flash and 'make V=1 flash' reports something similar

<syntaxhighlight lang="bash">Info : Device Security Bit Set stm32x unlocked</syntaxhighlight>

You need to unlock your device first (with JTAG):

<syntaxhighlight lang="bash">openocd -f jlink_swd_f103.cfg telnet 127.0.0.1 4444

  1. in telnet issue:

stm32f1x unlock 0</syntaxhighlight>

Debugging

Debugging hardware

ST-Link clones or Discovery boards are flashable with Black Magic Probe firmware.

JLink

<syntaxhighlight lang="bash"># add to Makefile to generate debuginfo CXXFLAGS = -g

  1. run openocd

openocd -f jlink_swd_f103.cfg

  1. connect to openocd via gdb

arm-none-eabi-gdb --ex 'target remote localhost:3333' <EXECUTABLE>.elf

  1. Use following commands when in gdb (more at http://www.yolinux.com/TUTORIALS/GDB-Commands.html)
  2. c (continue), stop execution with Ctrl-C
  3. bt (stacktrace)
  4. b (break), e.g. 'b main' or 'b template.c:360'</syntaxhighlight>

Black Magic probe

Running BMP on Discovery boards

F3 Discovery

Shorting solder bridges SB13 and SB15 connects UART1 port from STM32F303 to STM32F103 making it possible to use Black Magic Probe as UART bridge via /dev/ttyACM1

F4 Discovery

To enable UART bridge via BMP on older F4 discovery (STM32F407) you need to solder wires between UART2 (PA2/PA3) on STM32F407 to pins PA2/PA3 on STM32F103. These are located in the corners of the chips. Take care when soldering PA3 on F407 as it's positioned near VSS, if you manage to short these pins try lifting PA3 from the pad completely soldering wire directly to it.

Flashing

To see device states during the process it is useful to open a second terminal and run

<syntaxhighlight lang="bash">dmesg -Hw</syntaxhighlight>

Next build firmware for stlink target and upload it to target

<syntaxhighlight lang="bash"># make PROBE_HOST=stlink

  1. flashing blackmagic_dfu via black magic probe

git clone https://github.com/blacksphere/blackmagic/ cd blackmagic make PROBE_HOST=stlink arm-none-eabi-gdb --ex 'target extended-remote /dev/ttyACM0' src/blackmagic_dfu monitor swdp_scan attach 1 monitor option erase

  1. restart target so it unlocks, repeat up to 'monitor option erase' then issue

load

  1. restart target, it should boot to black magic dfu
  2. [ +0.000002] usb 1-1.2: Product: Black Magic (Upgrade) for STLink/Discovery, (Firmware v1.6-rc0-257-gd6e2977)
  3. upload black magic via dfu
  4. disconnect your programmer so only target board is connected

sudo dfu-util -s 0x08002000:leave -D src/blackmagic.bin

  1. if dfu-util can't find your device you can specify -S <serial> from dmesg output

sudo dfu-util -S 7EBA7BA4 -s 0x08002000:leave -D src/blackmagic.bin</syntaxhighlight>

Sample session:

<syntaxhighlight lang="bash">$ arm-none-eabi-gdb --ex 'target extended-remote /dev/ttyACM0' src/blackmagic_dfu Reading symbols from src/blackmagic_dfu...done. Remote debugging using /dev/ttyACM0 (gdb) monitor swdp_scan Target voltage: unknown Available Targets: No. Att Driver

1      STM32F1 medium density

(gdb) attach 1 Attaching to program: /home/rmarko/embedded/blackmagic/src/blackmagic_dfu, Remote target 0x08010064 in ?? () (gdb) monitor option erase 0x1FFFF800: 0x0000 0x1FFFF802: 0x0000 0x1FFFF804: 0x0000 0x1FFFF806: 0x0000 0x1FFFF808: 0x0000 0x1FFFF80A: 0x0000 0x1FFFF80C: 0x0000 0x1FFFF80E: 0x0000 (gdb) load Error erasing flash with vFlashErase packet

  1. ^^ target needs rebooting to unlock flash

(gdb) quit

<TARGET REBOOTED>

$ arm-none-eabi-gdb --ex 'target extended-remote /dev/ttyACM0' src/blackmagic_dfu Reading symbols from src/blackmagic_dfu...done. Remote debugging using /dev/ttyACM0 (gdb) monitor swdp_scan Target voltage: unknown Available Targets: No. Att Driver

1      STM32F1 medium density

(gdb) attach 1 Attaching to program: /home/rmarko/embedded/blackmagic/src/blackmagic_dfu, Remote target 0xfffffffe in ?? () (gdb) load Loading section .text, size 0x1c48 lma 0x8000000 Loading section .data, size 0x90 lma 0x8001c48 Start address 0x8001498, load size 7384 Transfer rate: 11 KB/sec, 820 bytes/write. (gdb) quit

<TARGET REBOOTED>

$ dmesg [691195.851258] usb 1-1.6: new full-speed USB device number 112 using ehci-pci [691195.943994] usb 1-1.6: New USB device found, idVendor=1d50, idProduct=6017 [691195.943997] usb 1-1.6: New USB device strings: Mfr=1, Product=2, SerialNumber=3 [691195.943999] usb 1-1.6: Product: Black Magic (Upgrade) for STLink/Discovery, (Firmware v1.6.1-98-g9a5b31c) [691195.944001] usb 1-1.6: Manufacturer: Black Sphere Technologies [691195.944002] usb 1-1.6: SerialNumber: 7EBA7BA4

$ sudo dfu-util -S 7EBA7BA4 -s 0x08002000:leave -D src/blackmagic.bin dfu-util 0.9

Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc. Copyright 2010-2016 Tormod Volden and Stefan Schmidt This program is Free Software and has ABSOLUTELY NO WARRANTY Please report bugs to http://sourceforge.net/p/dfu-util/tickets/

dfu-util: Invalid DFU suffix signature dfu-util: A valid DFU suffix will be required in a future dfu-util release!!! Opening DFU capable USB device... ID 1d50:6017 Run-time device DFU version 011a Claiming USB DFU Interface... Setting Alternate Setting #0 ... Determining device status: state = dfuIDLE, status = 0 dfuIDLE, continuing DFU mode device DFU version 011a Device returned transfer size 1024 DfuSe interface name: "Internal Flash " Downloading to address = 0x08002000, size = 60660 Download [=========================] 100% 60660 bytes Download done. File downloaded successfully Transitioning to dfuMANIFEST state

<TARGET REBOOTED>

$ dmesg [691227.339977] usb 1-1.6: new full-speed USB device number 113 using ehci-pci [691227.432954] usb 1-1.6: New USB device found, idVendor=1d50, idProduct=6018 [691227.432956] usb 1-1.6: New USB device strings: Mfr=1, Product=2, SerialNumber=3 [691227.432957] usb 1-1.6: Product: Black Magic Probe (STLINK), (Firmware v1.6.1-98-g9a5b31c) [691227.432958] usb 1-1.6: Manufacturer: Black Sphere Technologies [691227.432959] usb 1-1.6: SerialNumber: 7EBA7BA4 [691227.433654] cdc_acm 1-1.6:1.0: ttyACM0: USB ACM device [691227.434290] cdc_acm 1-1.6:1.2: ttyACM1: USB ACM device</syntaxhighlight>

Usage

<syntaxhighlight lang="bash">arm-none-eabi-gdb --ex 'target extended-remote /dev/ttyACM0'

  1. Use following commands when in gdb

monitor swdp_scan attach 1</syntaxhighlight>

Sample session:

<syntaxhighlight lang="bash">(gdb) monitor swdp_scan Target voltage: unknown Available Targets: No. Att Driver 1 STM32F4xx (gdb) attach 1 Attaching to Remote target Error while running hook_stop: Invalid type combination in equality test. 0x080035c2 in ?? () (gdb) bt

  1. 0 0x080035c2 in ?? ()
  2. 1 0x08001032 in ?? ()
  3. 2 0x08001032 in ?? ()

Backtrace stopped: previous frame identical to this frame (corrupt stack?)</syntaxhighlight>

To access serial bridge:

<syntaxhighlight lang="bash">screen /dev/ttyACM1 115200</syntaxhighlight>

Tested boards
  • STLink programmers
  • STM32 F4 Discovery
  • STM32 IoT node (STM32L475)

Links

DMA

DAC

Docs

Calculating Timer Frequency

MUCH WIP

period_target_ms = p1 period_hz = p2

1000ms / p1 = p2

E.g. for 20ms (servo) period

1000ms / 20 ms = 50 hz

cpu_freq = 72Mhz prescale = 72

72MHz / 72 = 1Mhz

This increases out count register CNT by 1 000 000 each second. To calculate what is the correct value for the ARR register (which defines the period), we divide the result of the prescaled frequency by the intended frequency for our period:

1 000 000 Hz / 50 Hz = 20 000

Now every time the CNT register is increased by one, this is the equivalent to 1us. The duty cycle now could by set easily. E.g. for a 1.5ms duty cycle we set the CCRx register to 1500.


Stepper

driver_max_duty = 250khz

From http://www.kaltpost.de/?page_id=412