UM0470 User Manual: STM8 SWIM Communication Protocol and Debug Module
UM0470 User Manual: STM8 SWIM Communication Protocol and Debug Module
User manual
Introduction
This manual has been written for developers who need to build programming, testing or
debugging tools for the STM8 microcontroller family. It explains the debug architecture of the
STM8 core.
The STM8 debug system consists of 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)
(STM8A version is not published yet)
● How to program STM8L Flash program memory and data EEPROM (PM0054)
Contents
2 Communication layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
List of tables
List of figures
The STM8 debug system interface allows a debugging or programming tool to be connected
to the MCU through a single wire bidirectional communication based on open-drain line.
It provides non-intrusive read/write access to RAM and peripherals during program
execution
The block diagram is shown in Figure 1.
STM8
LSI oscillator
SWIM
SWIM Entry
STM8
Debug module (DM) CPU Bus
SWIM pin Comm Command Core
Layer Decode
Peripheral Bus
HSI oscillator
RAM Bus
Flash/
Peripherals RAM Data EEPROM
The debug module uses the two internal clock sources present in the device, the LSI Low
Speed Internal clock (usually in the range 30 kHz-200 kHz, depending on the product) one
and the HSI High Speed Internal clock (usually in the range 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 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).
CPU registers (A, X, Y, CC, SP) can also be accessed. These registers are mapped in
memory and can be accessed in the same way as other memory addresses.
● 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 you also want to use it for debug. The safest way is to provide a strap
option on the application PCB.
STM8
Application I/O
SWIM pin
SWIM interface for tools
POR
OFF reset
N
Y
SWD bit set I/O CSR Y
RST bit set
N
N
ROTF WOTF
2ESET 2ESET RISING
/PTION BYTE LOADING
(3) !$# /.
,3) /3# /. AI
SWIM activation is shown in Figure 4 and each segment on the diagram is described below.
1. To make the SWIM active, the SWIM pin must be forced low during a period of 16 µs
(which is 64 pulses minimum at the frequency of HSI).
2. After this first pulse at 0, the SWIM detects a specific sequence to guarantee
robustness in the SWIM active state entry. The SWIM entry sequence is: 4 pulses at 1
kHz followed by 4 pulses at 2 kHz. The frequency ratio is detected and allows SWIM
entry. The ratio can be easily detected whatever the internal RC frequency. 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 HSI clock
periods with the SWIM line at 0 is sent out by the MCU device to allow for the
measurement of the RC by the debug host. An advanced debug host can re-calibrate
its clock to adapt to the frequency of Internal RC.
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 bit 5 allows the whole memory range and SRST command to be accessed.
- setting bit 7 masks the internal reset sources
7. Release reset which starts the option byte loading sequence. Wait 1 ms for
stabilization.
8. Once option byte loading has occurred and stabilization time is reached, the CPU is in
phase 8:
- STM8S is stalled and HSI = 16 Mhz (see STM8S datasheets for % accuracy)
- SWIM clock is at HSI/2 = 8 Mhz
- SWIM is active in low speed bit format (see Section 3.3.2)
SWIM Active
SWIM pin
1 ms 500 µs
SWIM entry sequence
<=> ‘1’
<=> ‘0’
<=> ‘1’
<=> ‘0’
...
0 b2 b1 b0 pb ack 0 b7 b6 b5 b4 b3 b2 b1 b0 pb ack
1 b7 b6 b5 b4 b3 b2 b1 b0 pb ack
Italic: Bit sent by the Host
Bold: Bit sent by the device
SRST 000
ROTF 001
WOTF 010
011
Reserved for future use
1xx
Parameters:
None.
SRST command generates a system reset only if 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 SWIM_DM bit is cleared, ROTF can only be done on 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 SWIM_DM bit is cleared, WOTF can only be done on 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
tr tib
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
output the core context (stack) at a given point. The DM is mainly used for in-circuit
debugging.
WATCHDOG
CPU
DM STALL
DM STALL/TRAP ( Fr o m
Slave DM
when
STALL bit available)
SWTRAP bit
EXT STALL EXT
MANAGEMENT STALL
SWTRF FV_ROM
flag
FV_RAM
DM_CR2
PERIPHERALS ENFCTx
(Timers, USB,...)
DM_ENFCT
4.3 Debug
The DM registers can be read and written only through the SWIM interface. STM8 core has
no access to these registers.
4.3.1 Reset
Once the SWIM is active and SWIM_DM bit is set in 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 Section 3.7: CPU register access.
To restart the program execution, the STALL bit in 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. If the CPU registers must
be modified, the procedure described in Section 3.7: CPU register access has to be used.
The host can restart the program execution by resetting the STALL bit using the SWIM
commands.
0 1 1 X X Disabled 0 0 x
1 1 0 X X Disabled 0 0 x
INSTRUCTION
FETCH
DECODE EXECUTE
OP-CODE/operand
(1 to 5 tCPU) (1 to 10 tCPU)
1-5 bytes
(1-2 tCPU)
4.7 Abort
The stall is generated immediately on writing the STALL bit in the DM_CSR2 register.
t
INSTRUCTION 1
INSTRUCTION 2
INSTRUCTION 3
DM Stall generated
STALL REQUEST
t
Fetch2 Dec2.n Exe2.1 ...Exe3(stalled)...
D_rd1 D_wr1
Stall generated
DATA BREAK REQUEST
EXE1 t
DEC1 ... EXEC2 (stalled)
DEC2
Note: When Step mode and Instruction Break on the next instruction mode are both enabled, both
the STF and the BKxF flags are set. When you clear the STALL bit, the step function
continues its normal operation.
4.11.3 DM break
After an 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 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. 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)
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) SWIM can read and write to these bits via the ROTF/WOTF commands.
read-only (r) 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 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 Step mode. It is set and cleared by
Bit 5 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
Bit 4
this bit 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 instruction fetch breaks. It is set by hardware depending on the
breakpoint conditions (see Table 4: Decoding table for breakpoint interrupt
Bit 3
generation 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 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 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
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
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 debug 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
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the
right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any
time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no
liability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this
document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products
or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such
third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED
WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS
OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS ARE NOT
RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING
APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY,
DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE
GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void
any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any
liability of ST.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.