MDK-ARM (Windows)

Windows7x64 virtual machine on Mac

The pack installer seems useless. Just close it, and the install link is somehow disabled.

Download stm32f103ze DFP and install

MDK4.7

MDK 4.72 + keygen (Menu/License manage/CID), generated key only valid to 2020, but still can use

Project structure(https://github.com/drriguz/stm32-examples/tree/master/stm32-led):

Set c++ options:

Set debuger to use ST-link+SW:

Note:

  • Don't use startup_stm32f10x_hd.s by Mdk, use the library one
  • Don't call SystemInit in main.c, it's already called in startup_stm32f10x_hd.s

[1]

Opensource toolchain

  • STM32CubeMX: to create project using cmake
  • Openocd: to flash to device

STM32CubeMX

Create cmake project

stm32cubemx

Select STM32F103ZET6:

Then config to use cmake, and click "Generate":

Enable debugging

Debug must be enabled, otherwise ST-link could not be accessed once flashed:


Configure GPIO

Build .elf and generate .bin

Make the project:

$ cd stm32-hello
$ mkdir build
$ cd build
$ make
[100%] Linking C executable stm32-hello.elf
Memory region         Used Size  Region Size  %age Used
             RAM:        1584 B        64 KB      2.42%
           FLASH:        3772 B       512 KB      0.72%
[100%] Built target stm32-hello


$ arm-none-eabi-size stm32-hello.elf
   text	   data	    bss	    dec	    hex	filename
   3760	     12	   1572	   5344	   14e0	stm32-hello.elf
$ arm-none-eabi-objcopy -O binary  stm32-hello.elf stm32-hello.bin

OpenOCD

https://openocd.org/pages/getting-openocd.html

[2] [3]

Installation

# don't brew install, lacks of stlink support 
# brew install openocd

brew install jimtcl libusb

# ./doc/openocd.texi:12874: Unmatched `@end'.
# makeinfo: Removing output file `doc/openocd.info' due to errors; use --force to preserve.
brew install texinfo # need newer version

# configure: error: libusb-1.x is required for adapter "ST-Link Programmer".
export PKG_CONFIG_PATH="/usr/local/opt/libusb/lib/pkgconfig:$PKG_CONFIG_PATH"

git clone https://git.code.sf.net/p/openocd/code openocd-code
cd openocd-code

# 12.0 Error: ST-Link version does not support DAP direct transport
git checkout v0.10.0
./bootstrap
./configure --enable-stlink --enable-ftdi --disable-werror
make -j 8
sudo make install

OpenOCD install path: /usr/local/share/openocd/scripts/

Start debug:

$ openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg
Open On-Chip Debugger 0.10.0 (2025-04-04-01:17)
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.316364
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints

Telnet

Install inetutils first to use telnet command:

brew install inetutils
telnet localhost 4444
> reset halt
> flash write_image erase /Users/riguz/Workspace/embedded/stm32/stm32-hello/build/stm32-hello.bin 0x08000000
> reset run

Use flash command to write programe[4]:

Issues

If somehow cannot connect to ST-link after burn:

$ openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg

Open On-Chip Debugger 0.10.0 (2025-04-04-01:17)
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.228515
Error: init mode failed (unable to connect to the target)
in procedure 'init'
in procedure 'ocd_bouncer'

To fix:

  • Switch to BOOT1
  • Restart
  • Then connect again

STM32 for Vscode