# OpenOCD and STLINK for H10 based devices

<abstract>
This article illustrates how to program H10 based devices with OpenOCD and a STLINK-V2 device on a Linux PC.
</abstract>

<img src="./ogimage.jpg">

## STLINK Devices
In this article two devices have been tested :

* STM STLINK - V2
* STLINK - V2 Dongle

## STM STLINK - V2
<img src="./Stlink-V2.jpg" class="img-responsive" width="150px">
This is the original device developed by STMicrolectronics. It uses a 20 pin connector for both JTAG and SWD.

## STLINK - V2 Dongle
<img src="./Stlink-Stick_4.jpg" class="img-responsive" width="200px">
This is a scaled down, non official version, of the original STM device, it has a 10 pin connector and handles SWD only.

## JTAG or SWD ?
We are here only interested in SWD, JTAG is not considered.

## Wirings - For BertaH10
For sake of simplicity, here are shown connections with a BertaH10 board.

| STM STLINK - V2 | STLINK - V2 |  Berta J12 pin # | H10 Signals   |
|-----------------|-------------|------------------|---------------|
|       9         |     6       |      7           | SWCLK         |
|       7         |     2       |      9           | SWDIO         |
|       2         |     3       |      8           | GND           |


## Install Open OCD
Install on your Linux PC the OpenOCD package 
<div class="terminal">
	sudo apt update
	sudo apt install openocd
</div>
</br>
Check correct installation and get current version.
<div class="terminal">
	openocd -v
	Open On-Chip Debugger 0.12.0
</div>
## Preparing config file for Openocd	

Create and save, in your working directory, a file called <code>openocd.cfg</code> with this content:

	source [find interface/stlink.cfg]
	set CPUTAPID 0x0bc11477
	set CHIPNAME SAMR34J18
	source [find target/at91samdXX.cfg]
	adapter speed 4000
</br>

### Note
Should the following error araise when using for the first time your uC :

<div class="terminal">
Warn : UNEXPECTED idcode: 0xXYZXYZXY
Error: expected 1 of 1: 0x0bc11477
in procedure 'program'
** OpenOCD init failed **
shutdown command invoked
</div>
Replace in your openocd.cfg file the parameter CPUTAPID from 0x0bc11477 to 0xXYZXYZXY

## Program the H10 device
Launch the following command:
<div class="terminal">
	openocd -f openocd.cfg -c "program firmware.bin preverify verify reset exit"
</div>
</br>
A few words about the previous comand : 

* "preverify" option prevents chip programming if the firmware on board of the H10 device matches the one in the firmware.bin file.
* "verify" option checks that the newly flashed firmware on the H10 device matches the one in the source firmware.bin file.
* "reset" restarts the chip.
* "exit" closes openocd.

### Preverify / verify examples
Flash matches binary file, flash programming skipped, uC is reset, openocd is closed.
<div class="terminal">
pre-verifying
Verified OK - No flashing
Resetting Target
shutdown command invoked
</div>
</br>

Flash does not match binary file, flash is programmed, verified, uC is reset, openocd is closed.
<div class="terminal">
**pre-verifying**
Error: checksum mismatch - attempting binary compare
** Programming Started **
Info : SAMD MCU: SAMR34J18 (256KB Flash, 32KB RAM)
** Programming Finished **
** Verify Started **
** Verified OK **
** Resetting Target **
shutdown command invoked
</div>

## Working with STM STLINK - V2
<img src="./prg_stm.jpg" class="img-responsive" width="250px">
</br>

## Working with STLINK - V2 Dongle
<img src="./prg_stick.jpg" class="img-responsive" width="250px">
</br>
## Links

* <https://openocd.org/>

## Useful commands
<div class="terminal">
	openocd -c "adapter driver list"
</div>
</br>
@include='bio_rinalduzzi'