Installing Gnuk to STM32 part of STM8S Discovery Kit

Prerequisite

Note that once you install Gnuk to its STM32F103,there is no way to recover to original state. That is, you cannot use the feature of USB dongle for STM8S any more.

You have JTAG debugger which uses FTDI 2232D.

You installed OpenOCD.

You have source code of Gnuk. You build and have gnuk.elf, and you are at src directory of Gnuk.

Hardware setup

  • Connect STM32 part of STM8S Discovery Kit to JTAG debugger
  • Connect USB plug of STM32 part of STM8S Discovery Kit to PC
  • Connect JTAG debugger to PC

Check flash ROM protection of STM32F103

On STM8S Discovery Kit, flash ROM of STM32F103 is read/write protected. We check the status of read protection by OpenOCD: Readout Protection On. Here is exact log:

$ openocd -f interface/openocd-usb.cfg -f target/stm32.cfg -f ../tool/openocd-script/options_read.tcl
Open On-Chip Debugger 0.3.1 (2009-11-25-12:22)
$URL$
For bug reports, read
      http://openocd.berlios.de/doc/doxygen/bugs.html
OLD SYNTAX: DEPRECATED - use jtag_khz, not jtag_speed
jtag_speed: 1
1000 kHz
jtag_nsrst_delay: 100
jtag_ntrst_delay: 100
trst_and_srst separate srst_gates_jtag trst_push_pull srst_open_drain
Info : clock speed 1000 kHz
Info : JTAG tap: stm32.cpu tap/device found: 0x3ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x3)
Info : JTAG tap: stm32.bs tap/device found: 0x16410041 (mfg: 0x020, part: 0x6410, ver: 0x1)
Info : JTAG tap: stm32.cpu tap/device found: 0x3ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x3)
Info : JTAG tap: stm32.bs tap/device found: 0x16410041 (mfg: 0x020, part: 0x6410, ver: 0x1)
target state: halted
target halted due to debug-request, current mode: Handler HardFault
xPSR: 0x20000003 pc: 0x080000ec msp: 0x20004fd8
Info : device id = 0x20036410
Error: AHBAP Cached values: dp_select 0x0, ap_csw 0xa2000011, ap_tar 0xffffffff
Error: SWJ-DP STICKY ERROR
Error: Read MEM_AP_CSW 0x23000051, MEM_AP_TAR 0x1ffff7e2
Warn : STM32 flash size failed, probe inaccurate - assuming 128k flash
Info : flash size = 128kbytes
Option Byte: 0x3fffffe
Readout Protection On
Software Watchdog
Stop: No reset generated
Standby: No reset generated
$

Unlock flash ROM of STM32F103

At first, we need to unlock flash ROM of STM32F103.

The output of OpenOCD says: stm32x unlocked:

$ openocd -f interface/openocd-usb.cfg -f target/stm32.cfg -f ../tool/openocd-script/unlock.tcl
Open On-Chip Debugger 0.3.1 (2009-11-25-12:22)
$URL$
For bug reports, read
      http://openocd.berlios.de/doc/doxygen/bugs.html
OLD SYNTAX: DEPRECATED - use jtag_khz, not jtag_speed
jtag_speed: 1
1000 kHz
jtag_nsrst_delay: 100
jtag_ntrst_delay: 100
trst_and_srst separate srst_gates_jtag trst_push_pull srst_open_drain
Info : clock speed 1000 kHz
Info : JTAG tap: stm32.cpu tap/device found: 0x3ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x3)
Info : JTAG tap: stm32.bs tap/device found: 0x16410041 (mfg: 0x020, part: 0x6410, ver: 0x1)
Info : JTAG tap: stm32.cpu tap/device found: 0x3ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x3)
Info : JTAG tap: stm32.bs tap/device found: 0x16410041 (mfg: 0x020, part: 0x6410, ver: 0x1)
target state: halted
target halted due to debug-request, current mode: Handler HardFault
xPSR: 0x20000003 pc: 0x080000ec msp: 0x20004fd8
Info : device id = 0x20036410
Error: AHBAP Cached values: dp_select 0x0, ap_csw 0xa2000011, ap_tar 0xffffffff
Error: SWJ-DP STICKY ERROR
Error: Read MEM_AP_CSW 0x23000051, MEM_AP_TAR 0x1ffff7e2
Warn : STM32 flash size failed, probe inaccurate - assuming 128k flash
Info : flash size = 128kbytes
Info : Device Security Bit Set
stm32x unlocked
Info : JTAG tap: stm32.cpu tap/device found: 0x3ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x3)
Info : JTAG tap: stm32.bs tap/device found: 0x16410041 (mfg: 0x020, part: 0x6410, ver: 0x1)

Power Off and Power On, two times

At the first "Power Off and Power On" cycle, STM32F103 erases its flash ROM and makes it read enable. Next "Power Off and Power On" cycle it becomes initialized state.

Write gnuk.elf image to STM32F103

Then, we write Gnuk image to STM32F103:

$ openocd -f interface/openocd-usb.cfg -f target/stm32.cfg -f ../tool/openocd-script/write.tcl
Open On-Chip Debugger 0.3.1 (2009-11-25-12:22)
$URL$
For bug reports, read
      http://openocd.berlios.de/doc/doxygen/bugs.html
OLD SYNTAX: DEPRECATED - use jtag_khz, not jtag_speed
jtag_speed: 1
1000 kHz
jtag_nsrst_delay: 100
jtag_ntrst_delay: 100
trst_and_srst separate srst_gates_jtag trst_push_pull srst_open_drain
Info : clock speed 1000 kHz
Info : JTAG tap: stm32.cpu tap/device found: 0x3ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x3)
Info : JTAG tap: stm32.bs tap/device found: 0x16410041 (mfg: 0x020, part: 0x6410, ver: 0x1)
Info : JTAG tap: stm32.cpu tap/device found: 0x3ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x3)
Info : JTAG tap: stm32.bs tap/device found: 0x16410041 (mfg: 0x020, part: 0x6410, ver: 0x1)
target state: halted
target halted due to debug-request, current mode: Handler HardFault
xPSR: 0x01000003 pc: 0xfffffffe msp: 0xffffffdc
auto erase enabled
Info : device id = 0x20036410
Info : flash size = 64kbytes
Info : Padding image section 0 with 3196 bytes
Warn : not enough working area available(requested 16384, free 16336)
wrote 61440 byte from file gnuk.elf in 5.530510s (10.848909 kb/s)

It just takes five seconds or so.

Lock flash ROM of STM32F103

We need to enable read protection of the flash ROM.

The output of OpenOCD says: stm32x locked:

$ openocd -f interface/openocd-usb.cfg -f target/stm32.cfg -f ../tool/openocd-script/lock.tcl
Open On-Chip Debugger 0.3.1 (2009-11-25-12:22)
$URL$
For bug reports, read
      http://openocd.berlios.de/doc/doxygen/bugs.html
OLD SYNTAX: DEPRECATED - use jtag_khz, not jtag_speed
jtag_speed: 1
1000 kHz
jtag_nsrst_delay: 100
jtag_ntrst_delay: 100
trst_and_srst separate srst_gates_jtag trst_push_pull srst_open_drain
Info : clock speed 1000 kHz
Info : JTAG tap: stm32.cpu tap/device found: 0x3ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x3)
Info : JTAG tap: stm32.bs tap/device found: 0x16410041 (mfg: 0x020, part: 0x6410, ver: 0x1)
Info : JTAG tap: stm32.cpu tap/device found: 0x3ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x3)
Info : JTAG tap: stm32.bs tap/device found: 0x16410041 (mfg: 0x020, part: 0x6410, ver: 0x1)
target state: halted
target halted due to debug-request, current mode: Handler HardFault
xPSR: 0x61000003 pc: 0x20000026 msp: 0xffffffdc
Info : device id = 0x20036410
Info : flash size = 64kbytes
stm32x locked
Info : JTAG tap: stm32.cpu tap/device found: 0x3ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x3)
Info : JTAG tap: stm32.bs tap/device found: 0x16410041 (mfg: 0x020, part: 0x6410, ver: 0x1)

Note that another "Power Off and Power On" cycle is needed to really enable the read protection.