UM0470 User Manual: STM8 SWIM Communication Protocol and Debug Module
UM0470 User Manual: STM8 SWIM Communication Protocol and Debug Module
User manual
STM8 SWIM communication protocol and debug module
Introduction
This manual is addressed to developers who build programming, testing or debugging tools
for the STM8 8-bit MCUs family. This document explains the debug architecture of the
STM8 core.
The STM8 8-bit MCUs debug system includes two modules:
• DM: debug module
• SWIM: single wire interface module
Related documentation:
How to program STM8S and STM8A Flash program memory and data EEPROM (PM0051)
How to program STM8L and STM8AL Flash program memory and data EEPROM
(PM0054).
Contents
2 Communication layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
List of tables
List of figures
The STM8 MCUs debug system interface allows a debugging or programming tool to be
connected to the MCU through a single wire. This connection results in a bidirectional
communication based on an open-drain line and provides a non-intrusive read/write access
to RAM and peripherals during the program execution.
The block diagram is shown in Figure 1.
6700&8
/6,RVFLOODWRU
6:,0
6:,0HQWU\ 'HEXJPRGXOH &38EXV 670
60,0SLQ &RPP &RPPDQG '0 FRUH
OD\HU GHFRGH
3HULSKHUDOEXV
5DPEXV
+6,RVFLOODWRU
)ODVK
3HULSKHUDOV 5$0 'DWD((3520
06Y9
The debug module uses the two internal clock sources present in the device:
• the low speed internal clock (LSI clock): usually in the range of 30 kHz to 200 kHz
depending on the product
• the high speed internal clock (HSI clock): usually in the range of 10 MHz to 25 MHz
depending on the device.
The clocks are automatically started when necessary.
2 Communication layer
The SWIM is a single wire interface based on asynchronous, high sink (8 mA), open-drain,
bidirectional communication. While the CPU is running, the SWIM allows a non-intrusive
read/write accesses to be performed on-the-fly to the RAM and peripheral registers, for
debug purposes.
In addition, while the CPU is stalled, the SWIM allows read/write accesses to be performed
to any other part of the MCU’s memory space (data EEPROM and program memory).
The CPU registers (A, X, Y, CC, SP) can also be accessed. These registers are mapped in
the memory and can be accessed in the same way as any other memory addresses. It is
important to note that:
• Register, peripherals and memory can be accessed only when the SWIM_DM bit is set.
• When the system is in HALT, WFI or readout protection mode, the NO_ACCESS flag in
the SWIM_CSR register is set. In this case, it is forbidden to perform any accesses
because parts of the device may not be clocked and a read access could return
garbage or a write access might not succeed.
The SWIM can perform a MCU device software reset. The SWIM pin can also be used by
the MCU target application as a standard I/O port with some restrictions if the user also want
to use it for debug. The safest way is to provide a strap option on the application PCB.
6700&8
$SSOLFDWLRQ,2
6:,0SLQ
6:,0LQWHUIDFHIRUWRROV
-XPSHUVHOHFWLRQIRU
GHEXJSXUSRVHV
06Y9
325
UHVHW
2))
1
<
6:'ELWVHO ,2 <
&65567
ELWVHW
< 6567
6:,0HQWU\
$&7,9(
VHTXHQFH
1
527) :27)
06Y9
2ESET 2ESET RISING
/PTION BYTE LOADING
(3) !$# /.
,3) /3# /. AI
The SWIM activation is shown in Figure 4 and each segment of the diagram is described
below:
1. To make the SWIM active, the SWIM pin must be forced low during a period of 16 µs.
2. After this first pulse at 0, the SWIM detects a specific sequence to guarantee the
robustness in the SWIM active state entry. The SWIM entry sequence is: four pulses at
1 kHz followed by four pulses at 2 kHz. The frequency ratio is detected and allows the
SWIM entry. The ratio can be easily detected regardless of the internal LSI frequency
value. The waveform of the entry sequence is shown in Figure 5. Note that the
sequence starts and ends with the SWIM pin at 1.
3. After the entry sequence, the SWIM enters in SWIM active state, and the HSI oscillator
is automatically turned ON.
4. After this delay, the SWIM sends a synchronization frame to the host.
Synchronization frame description: a synchronization frame of 128 x SWIM clocks
periods with the SWIM line at 0 is sent out by the MCU device to allow the
measurement of the HSI by the debug host. An advanced debug host can re-calibrate
its clock to adapt to the frequency of the internal HSI RC oscillator.
5. Before starting a SWIM communication, the SWIM line must be released at 1 to
guarantee that the SWIM is ready for communication (at least 300 ns).
6. Write 0A0h in the SWIM_CSR:
- setting the bit 5 allows the whole memory range and the SRST command to be
accessed.
- setting the bit 7 masks the internal reset sources
7. Release the reset which starts the option byte loading sequence. Wait 1 ms for
stabilization.
8. Once the option byte loading has occurred and that the stabilization time is reached,
the CPU is in phase 8:
- STM8 is stalled and HSI = 16 Mhz (see STM8 datasheets for HSI clock accuracy)
- SWIM clock is at HSI/2 = 8 Mhz
- SWIM is active in low speed bit format (see Section 3.3.2)
9. After the HSI is calibrated internally, a copy of the factory calibration value is uploaded
from the option bytes and stored into the HSI calibration register at the RAM, then a
SWIM communication reset command can be generated to get the synchronization
frame again but with greater reliability than in step 4. Depending on the target context
since power on, the HSI clock could be not well calibrated in step 4, because at that
moment the HSI calibration register in the RAM is not yet initialized with the proper
value.
6:,0DFWLYH
6:,0SLQ
PV V
6:,0HQWU\VHTXHQFH
06Y9
!
!
06Y9
!
!
06Y9
&RPPDQG 'DWD
E E E SE DFN E E E E E E E E SE DFN
,WDOLFXQGHUOLQHG %LWVHQWE\WKHKRVW
%ROG %LWVHQWE\WKHGHYLFH
06Y9
,WDOLFXQGHUOLQHG %LWVHQWE\WKHKRVW
%ROG %LWVHQWE\WKHGHYLFH
06Y9
SRST 000
ROTF 001
WOTF 010
011
Reserved for future use
1xx
Parameters:
None.
The SRST command generates a system reset only if the SWIM_CSR/SWIM_DM bit is set.
Parameters:
N The 8 bits are the number of bytes to read (from 1 to 255)
@E/H/L: This is the 24-bit address to be accessed.
D[...]: These are the data bytes read from the memory space
If the host sends a NACK to a data byte, the device will send the same byte again.
If the SWIM_DM bit is cleared, the ROTF can only be done on the SWIM internal registers.
Parameters:
N The 8 bits are the number of bytes to write (from 1 to 255)
@E/H/L: This is the 24-bit address to be accessed.
D[...]: These are the data bytes to write in the memory space
If a byte D [i] has not been written when the following byte D [i+1] arrives, D [i+1] will be
followed by a NACK. In this case the host must send D [i+1] again until it is acknowledged.
For the last byte, if it is not yet written when a new command occurs, the new command will
receive a NACK and will not be taken into account.
If the SWIM_DM bit is cleared, the WOTF can only be done on the SWIM internal registers.
A 7F00h
PCE 7F01h
PCH 7F02h
PCL 7F03h
XH 7F04h
XL 7F05h
YH 7F06h
YL 7F07h
SPH 7F08h
SPL 7F09h
CC 7F0Ah
WI WLE
6:,0SLQ WEWE
WI
06Y9
Low time for a bit at 1 High speed: Tb1 TBD 192 ns 208 ns
(high speed) Low speed: Tb1 TBD 150 ns 250 ns
Injected current on SWIM
- TBD - 8 mA
pin
rw r rw rw rw rw r rw
Reserved SWIMCLK
- rw
4.1 Introduction
The debug module (DM) allows the developer to perform certain debugging tasks without
using an emulator. For example, the DM can interrupt the MCU to break infinite loops or to
output the core context (stack) at a given point. The DM is mainly used for in-circuit
debugging.
'HEXJPRGXOH'0
:DWFKGRJ
W
:DWFKGRJFRQWURO
ORJLF
'0UHJLVWHUV
UHDGZULWHORJLF
'0B%. '0B%.
6:,0 6:,0
,16758&7,21
%&
6:%.( %&
6:%.( '0EUHDNSRLQW
HQDEOHELW %&
ORJLF
)(7&+
(08'0$ IODJ
LQWHUIDFH %,5
6RIWZDUH %,:
EUHDN
5(6(7
'(&2'( (;(&87(
'0B&65
'0B&5
670
23&2'(RSHUDQG 67( 67) 567 %5: %.)%.)
&38
'0VWDOO
'067$//75$3 IURPVODYH
'0ZKHQ
67$//ELW DYDLODEOH
6:75$3ELW
WRE\WHV (;767$//
PDQDJHPHQW ([LWVWDOO
6:75)IODJ )9B520
WRW&38 WRW&38
)9B5$0
WRW&38 '0B&5
3HULSKHUDOV
(1)&7[
WLPHUV86%«
'0B(1)&7 06Y9
06Y9
4.3 Debug
The DM registers can be read and written only through the SWIM interface. The STM8 core
has no access to these registers.
4.3.1 Reset
Once the SWIM is active and that the SWIM_DM bit is set in the SWIM_CSR register, a
‘data read’ breakpoint at the reset vector address is automatically set, due to the reset
values of the debug module registers. This breakpoint can be used to initialize the debug
session.
4.3.2 Breakpoints
The DM generates a stall to the core when a breakpoint is reached. When the processor is
stalled, the host can read or modify any address in memory. Access to the processor
registers is explained in Table 3.7: CPU register access on page 16.
To restart the program execution, the STALL bit in the DM_CSR2 must be cleared using the
WOTF command of the SWIM protocol.
4.3.3 Abort
To use the abort function, the host must write the STALL bit in the DM_CSR2 using the
SWIM WOTF command.
No interrupt is generated. The core is stalled in the current state. Using the SWIM
commands, the host can read and modify the status of the MCU. Use the procedure
described inSection 3.7: CPU register access if the CPU registers must be modified.
The host can restart the program execution by resetting the STALL bit using the SWIM
commands.
,16758&7,21
)(7&+
'(&2'( (;(&87(
23&2'(RSHUDQG
WRE\WHV
WRW&38 WRW&38
WRW&38
06Y9
4.7 Abort
The stall is generated immediately when writing the STALL bit in the DM_CSR2 register.
W
« ,16758&7,21
,16758&7,21
,16758&7,21
'0VWDOOJHQHUDWHG
67$//UHTXHVW
06Y9
6WDOOJHQHUDWHG
'$7$%5($.5(48(67
06Y9
6WDOOJHQHUDWHG
'$7$%5($.5(48(67
06Y9
06Y9
Note: When the Step mode and the instruction break on the next instruction mode are both
enabled, both the STF and the BKxF flags are set. When the user clears the STALL bit, the
step function continues its normal operation.
4.11.3 DM break
After a DM break, the CPU is stalled (through the EMU_Stall signal). While the CPU is
stalled, the SWIM can read/write any memory location or any memory mapped register. The
program can be continued from the breakpoint, by resetting the stall bit.
If a change of PC is needed, the SWIM must write the new PC value using the method
described in Section 3.7: CPU register access on page 16. In order to fetch the code from
the new PC address, the SWIM must set the FLUSH bit in the DM control/status register 2
(DM_CSR2) (refer to Section 4.12.10 on page 33) before resetting the STALL bit.
4.12 DM registers
These registers are read/write only through the SWIM interface.
In this section, the following abbreviations are used:
read/write (rw) The SWIM can read and write to these bits via the ROTF/WOTF commands.
read-only (r) The SWIM can only read these bits via the ROTF command.
BK1[23:16]
rw rw rw rw rw rw rw rw
BK1[15:8]
rw rw rw rw rw rw rw rw
BK1[7:0]
rw rw rw rw rw rw rw rw
BK2[23:16]
rw rw rw rw rw rw rw rw
BK2[15:8]
rw rw rw rw rw rw rw rw
BK2[7:0]
rw rw rw rw rw rw rw rw
rw - rw rw rw rw rw -
rw rw
rw rw r r r r
Bit 7 Reserved.
STE Step mode enable (read / write)
This bit is set and cleared by software. It enables the Step mode.
Bit 6
0: Step mode disabled
1: Step mode enabled
STF Step flag (read only)
This bit indicates that the stall was generated by the Step mode. It is set and cleared
Bit 5 by hardware. Writing to this bit does not change the bit value.
0: Step mode stall did not occur
1: Step mode stall occurred
RST Reset flag (read only)
This bit is set by hardware when the CPU was stalled by the debug module (DM), just
after reset. It is cleared by hardware when the STALL bit is cleared. Writing to this bit
Bit 4
does not change the bit value.
0: No reset occurred
1: A reset occurred
BRW Break on read/write flag (read only).
This bit gives the value of the read/write signal when a break occurs. Its value is not
significant for the instruction fetch breaks. It is set by hardware depending on the
breakpoint conditions (see Table 4: Decoding table for breakpoint interrupt generation
Bit 3
on page 23) and is cleared by hardware depending on the next breakpoint conditions.
Writing to this bit does not change the bit value.
0: Breakpoint on write
1: Breakpoint on read
BK2F Breakpoint 2 flag (read only).
This bit indicates that the DM stall was generated by breakpoint 2. It is set by
hardware depending on the control conditions (see Table 4: Decoding table for
Bit 2 breakpoint interrupt generation on page 23) and it is cleared by hardware when the
STALL bit is cleared. Writing to this bit does not change the bit value.
0: Breakpoint 2 did not occur
1: Breakpoint 2 occurred
BK1F Breakpoint 1 flag (read only).
This bit indicates that the DM interrupt was generated by breakpoint 1. It is set by
hardware depending on the control conditions (see Table 4: Decoding table for
Bit 1 breakpoint interrupt generation on page 23) and it is cleared by hardware when the
STALL bit is cleared. Writing to this bit does not change the bit value.
0: Breakpoint 1 did not occur
1: Breakpoint 1 occurred
Bit 0 Reserved
rw r r rw
rw rw rw rw rw rw rw rw
A A7 A6 A5 A4 A3 A2 A1 A0
7F00h
Reset value 0 0 0 0 0 0 0 0
7F01h PCE(1) PC23 PC22 PC21 PC20 PC19 PC18 PC17 PC16
7F02h PCH(1) PC15 PC14 PC13 PC12 PC11 PC10 PC9 PC8
7F03h PCL(1) PC7 PC6 PC5 PC4 PC3 PC2 PC1 PC0
XL X7 X6 X5 X4 X3 X2 X1 X0
7F05h
Reset value 0 0 0 0 0 0 0 0
YL Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
7F07h
Reset value 0 0 0 0 0 0 0 0
7F08h SPH(1) SP15 SP14 SP13 SP12 SP11 SP10 SP9 SP8
(1)
7F09h SPL SP7 SP6 SP5 SP4 SP3 SP2 SP1 SP0
CC V - I1 H I0 N Z C
7F0Ah
Reset value 0 0 1 0 1 0 0 0
NO_ACCES SWIM_D
SWIM_CSR SAFE_MASK HS OSCOFF RST HSIT PRI
7F80h S M
Reset value 0 0 0 0 0 0
0 0
Reserve
DM_CR1 WDGOFF Reserved BC2 BC1 BC0 BIR BIW
7F96h d
Reset value 0 0 0 0 0 0 0
0
DM_CR2 FV_ROM Reserved FV_RAM
7F97h Reserved
Reset value 0 0 0
Reserve
DM_CSR1 Reserved STE STF RST BRW BK2F BK1F
7F98h d
Reset value 0 0 0 0 0 0 0
0
1. The reset value for the SP and PC registers is product dependent. Refer to the device datasheet for more details
Some peripherals can be frozen through the debug module during the debug and while
using the DM_ENFCTR register (address: 7F9Ah). Table 6 shows the peripherals which are
frozen by the bits (ENFCT0 to ENFCT7) of the DM_ENFCTR register.
Table 6. Peripherals which are frozen by the bits of the DM_ENFCTR register
for each STM8 product
STM8AF51A STM8AF616
DM_ENFCTR STM8AF51B STM8S103/903 STM8L101 STM8L15x
STM8S207/208 STM8S105
register (256 Kbyte die) (8 Kbyte die) (8 Kbyte die) (32 Kbyte die)
(128 Kbyte die) (32 Kbyte die)
Bit Peripheral
Revision history
STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and
improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on
ST products before placing orders. ST products are sold pursuant to ST’s terms and conditions of sale in place at the time of order
acknowledgement.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or
the design of Purchasers’ products.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. All other product or service names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.