This reference manual provides complete information for application developers on how to
use STM8S and STM8A microcontroller memory and peripherals.
The STM8A is a family of microcontrollers designed for automotive applications, with
different memory densities, packages and peripherals:
■ The medium density STM8A devices are the STM8AF622x/4x, STM8AF6266/68,
STM8AF612x/4x, and STM8AF6166/68 microcontrollers with 8 to 32 Kbytes of Flash
memory.
■ The high density STM8A devices are the STM8AF52xx STM8AF6269/8x/Ax,
STM8AF51xx, and STM8AF6169/7x/8x/9x/Ax microcontrollers with 32 to 128 Kbytes of
Flash memory.
The STM8S is a family of microcontrollers designed for general purpose applications, with
different memory densities, packages and peripherals.
■ The value line low density STM8S devices are the STM8S003xx microcontrollers with
8 Kbytes of Flash memory.
■ The value line medium density STM8S devices are the STM8S005xx microcontrollers
with 32 Kbytes of Flash memory.
■ The value line high density STM8S devices are the STM8S007xx microcontrollers with
64 Kbytes of Flash memory.
■ The access line low density STM8S devices are the STM8S103xx and STM8S903xx
microcontrollers with 8 Kbytes of Flash memory.
■ The access line medium density STM8S devices are the STM8S105xx microcontrollers
with 16 to 32-Kbytes of Flash memory.
■ The performance line high density STM8S devices are the STM8S207xx and
STM8S208xx microcontrollers with 32 to 128 Kbytes of Flash memory.
Refer to the product datasheet for ordering information, pin description, mechanical and
electrical device characteristics, and for the complete list of available peripherals.
Reference documents
■ For information on programming, erasing and protection of the internal Flash memory
please refer to the STM8S and STM8A Flash programming manual (PM0051), and to the
STM8 SWIM communication protocol and debug module user manual (UM0470).
■ For information on the STM8 core, please refer to the STM8 CPU programming manual
December 2011Doc ID 14587 Rev 81/449
(PM0044).
■ The bootloader user manual (UM0560) describes the usage of the integrated ROM
The CPU has an 8-bit architecture. Six internal registers allow efficient data manipulations.
The CPU is able to execute 80 basic instructions. It features 20 addressing modes and can
address six internal registers. For the complete description of the instruction set, refer to the
STM8 microcontroller family programming manual (PM0044).
1.2 CPU registers
The six CPU registers are shown in the programming model in Figure 1. Following an
interrupt, the registers are pushed onto the stack in the order shown in Figure 2. They are
popped from stack in the reverse order. The interrupt routine must therefore handle it, if
needed, through the POP and PUSH instructions.
1.2.1 Description of CPU registers
Accumulator (A)
The accumulator is an 8-bit general purpose register used to hold operands and the results
of the arithmetic and logic calculations as well as data manipulations.
Index registers (X and Y)
These are 16-bit registers used to create effective addresses. They may also be used as a
temporary storage area for data manipulations and have an inherent use for some
instructions (multiplication/division). In most cases, the cross assembler generates a
PRECODE instruction (PRE) to indicate that the following instruction refers to the Y register.
Program counter (PC)
The program counter is a 24-bit register used to store the address of the next instruction to
be executed by the CPU. It is automatically refreshed after each processed instruction. As a
result, the STM8 core can access up to 16 Mbytes of memory.
Doc ID 14587 Rev 823/449
Page 24
Central processing unit (CPU)RM0016
07
A ACCUMULATOR
07815
SP STACK POINTER
SHS
X INDEX
Y INDEX
07815
PC PROGRAM COUNTER
PCHPCL
07
CC CODE CONDITION
VI1HI0NZC
1623
PCE
07815
XHXL
07815
XHXL
0
Figure 1.Programming model
Stack pointer (SP)
The stack pointer is a 16-bit register. It contains the address of the next free location of the
stack. Depending on the product, the most significant bits can be forced to a preset value.
The stack is used to save the CPU context on subroutine calls or interrupts. The user can
also directly use it through the POP and PUSH instructions.
The stack pointer can be initialized by the startup function provided with the C compiler. For
applications written in C language, the initialization is then performed according to the
address specified in the linker file for C users. If you use your own linker file or startup file,
make sure the stack pointer is initialized properly (with the address given in the datasheets).
For applications written in assembler, you can use either the startup function provided by ST
or write your own by initializing the stack pointer with the correct address.
The stack pointer is decremented after data has been pushed onto the stack and
incremented after data is popped from the stack. It is up to the application to ensure that the
lower limit is not exceeded.
A subroutine call occupies two or three locations. An interrupt occupies nine locations to
store all the internal registers (except SP). For more details refer to Figure 2.
Note:The WFI/HALT instructions save the context in advance. If an interrupt occurs while the CPU
is in one of these modes, the latency is reduced.
24/449 Doc ID 14587 Rev 8
Page 25
RM0016Central processing unit (CPU)
JUMP TO INTERRUPT ROUTINE GIVEN BY THE INTERRUPT VECTOR
INTERRUPT GENERATION (execute pipeline)
YH
YL
PCE
PCL
CC
STACK
(PUSH)
UNSTACK
INTERRUPT
RETURN
PCH
JUMP TO THE ADDRESS GIVEN BY PROGRAM COUNTER (Reload Pipeline)
IRET INSTRUCTION
(POP)
9 CPU CYCLES
9 CPU CYCLES
POP PCL
POP PCH
POP PCE
POP Y
POP X
POP A
POP CC
A
XH
XL
PUSH PCL
PUSH PCH
PUSH PCE
PUSH Y
PUSH X
PUSH A
PUSH CC
Complete instruction in execute stage (1-6 cycles latency)
Figure 2.Stacking order
Condition code register (CC)
The condition code register is an 8-bit register which indicates the result of the instruction
just executed as well as the state of the processor. The 6th bit (MSB) of this register is
reserved. These bits can be individually tested by a program and specified action taken as a
result of their state. The following paragraphs describe each bit:
●V: Overflow
When set, V indicates that an overflow occurred during the last signed arithmetic operation,
on the MSB result bit. See the INC, INCW, DEC, DECW, NEG, NEGW, ADD, ADDW, ADC,
SUB, SUBW, SBC, CP, and CPW instructions.
●I1: Interrupt mask level 1
The I1 flag works in conjunction with the I0 flag to define the current interruptability level as
shown in Ta bl e 1 . These flags can be set and cleared by software through the RIM, SIM,
HALT, WFI, IRET, TRAP, and POP instructions and are automatically set by hardware when
entering an interrupt service routine.
Doc ID 14587 Rev 825/449
Page 26
Central processing unit (CPU)RM0016
Table 1.Interrupt levels
InterruptabilityPriorityI1I0
Interruptable main
Lowest
10
Interruptable level 101
Interruptable level 200
Non interruptable11
●H: Half carry bit
Highest
The H bit is set to 1 when a carry occurs between the bits 3 and 4 of the ALU during an ADD
or ADC instruction. The H bit is useful in BCD arithmetic subroutines.
●I0: Interrupt mask level 0
See Flag I1.
●N: Negative
When set to 1, this bit indicates that the result of the last arithmetic, logical or data
manipulation is negative (i.e. the most significant bit is a logic 1).
●Z: Zero
When set to 1, this bit indicates that the result of the last arithmetic, logical or data
manipulation is zero.
●C: Carry
When set, C indicates that a carry or borrow out of the ALU occurred during the last
arithmetic operation on the MSB operation result bit. This bit is also affected during bit test,
branch, shift, rotate and load instructions. See the ADD, ADC, SUB, and SBC instructions.
In a division operation, C indicates if trouble occurred during execution (quotient overflow or
zero division). See the DIV instruction.
In bit test operations, C is the copy of the tested bit. See the BTJF and BTJT instructions.
In shift and rotate operations, the carry is updated. See the RRC, RLC, SRL, SLL, and SRA
instructions.
This bit can be set, reset or complemented by software using the SCF, RCF, and CCF
instructions.
26/449 Doc ID 14587 Rev 8
Page 27
RM0016Central processing unit (CPU)
Example: Addition
$B5 + $94 = "C" + $49 = $149
C70
010110101
C70
+010010100
C70
=101001001
1.2.2 STM8 CPU register map
The CPU registers are mapped in the STM8 address space as shown inTa bl e 2 . These
registers can only be accessed by the debug module but not by memory access instructions
executed in the core.
Table 2.CPU register map
Address
offset
Register name765432 1 0
0x00A MSB----- -LSB
0x01PCEMSB-
-
--- -LSB
0x02 PCH MSB----- -LSB
0x03 PCL MSB----- -LSB
0x04 XH MSB----- -LSB
0x05 XL MSB----- -LSB
0x06 YH MSB----- -LSB
0x07 YL MSB----- -LSB
0x08 SPH MSB----- -LSB
0x09 SPL MSB----- -LSB
0x0ACCV0I1HI0NZC
1.3 Global configuration register (CFG_GCR)
1.3.1 Activation level
The MCU activation level is configured by programming the AL bit in the CFG_GCR register.
For information on the use of this bit refer to Section 6.4: Activation level/low power mode
control on page 62.
Doc ID 14587 Rev 827/449
Page 28
Central processing unit (CPU)RM0016
1.3.2 SWIM disable
By default, after an MCU reset, the SWIM pin is configured to allow communication with an
external tool for debugging or Flash/EEPROM programming. This pin can be configured by
the application for use as a general purpose I/O. This is done by setting the SWD bit in the
CFG_GCR register.
1.3.3 Description of global configuration register (CFG_GCR)
Address offset: 0x00
Reset value: 0x00
76543210
Reserved
Bits 7:2 Reserved
Bit 1 AL: Activation level
This bit is set and cleared by software. It configures main or interrupt-only activation.
0: Main activation level. An IRET instruction causes the context to be retrieved from the stack and
the main program continues after the WFI instruction.
1: Interrupt-only activation level. An IRET instruction causes the CPU to go back to WFI/Halt mode
without restoring the context.
Bit 0 SWD: SWIM disable
0: SWIM mode enabled
1: SWIM mode disabled
When SWIM mode is enabled, the SWIM pin cannot be used as general purpose I/O.
ALSWD
rwrw
1.3.4 Global configuration register map and reset values
The CFG_GCR is mapped in the STM8 address space. Refer to the corresponding
datasheets for the base address.
Table 3.CFG_GCR register map
Address
offset
0x00
Register name765432 1 0
CFG_GCR
Reset value
-
0
-
0
-
0
-
0
-
0
-
0
AL
SWD
0
0
28/449 Doc ID 14587 Rev 8
Page 29
RM0016Boot ROM
2 Boot ROM
The internal 2 Kbyte boot ROM (available in some devices) contains the bootloader code. Its
main tasks are to download the application program to the internal Flash/EEPROM through
the SPI, CAN, or UART interface, and to program the code, data, option bytes and interrupt
vectors in internal Flash/EEPROM.
To perform bootlloading in LIN mode, a different bootloader communication protocol is
implemented on UART2/UART3 and UART1.
The boot loader starts executing after reset. Refer to the STM8 bootloader user manual
(UM0560) for more details.
Doc ID 14587 Rev 829/449
Page 30
Memory and register mapRM0016
00 000h
RAM upper limit
Data EEPROM lower limit
Data EEPROM upper limit
00 4800h
00 5000h
00 6000h
00 6800h
00 7F00h
00 8000h
00 8080h
RAM
Stack
Reserved
Data EEPROM
Reserved
Option bytes
Reserved
HW registers
Reserved
Boot ROM (optional)
Reserved
Registers for CPU, SWIM, ITC, DM
Interrupt vectors
Program EEPROM
Program memory upper limit
ai 18468
Option bytes upper limit
HW registers upper limit
3 Memory and register map
For details on the memory map, I/O port hardware register map and CPU/SWIM/debug
module/interrupt controller registers, refer to the product datasheets.
3.1 Memory layout
3.1.1 Memory map
Figure 3.Memory map
30/449 Doc ID 14587 Rev 8
The RAM upper limit, data EEPROM upper and lower limit, Option Byte upper limit,
hardware (HW) registers upper limit, and the program memory upper limit are specific to the
device configuration. Please refer to the datasheets for quantitative information.
Page 31
RM0016Memory and register map
ai15055
Default stack model
End address
Stack roll-over limit
(1)
Stack pointer initialization value
Start address
RAM
3.1.2 Stack handling
Default stack model
The stack of the STM8S and STM8A microcontrollers is implemented in the user RAM area.
The default stack model is shown in Figure 4.
Figure 4.Default stack model
1. The stack roll-over limit is not implemented on all devices. Refer to the datasheets for detailed information.
Stack pointer initialization value
This is the default value of the stack pointer. The user must take care to initialize this pointer.
Correct loading of this pointer is usually performed by the initialization code generated by
the development tools (linker file). In the default stack model this pointer is initialized to the
RAM end address.
Stack roll-over limit
In some devices, a stack roll-over limit is implemented at a fixed address. If the stack pointer
is decreased below the stack roll-over limit, using a push operation or during context saving
for subroutines or interrupt routines, it is reset to the RAM end address. The stack pointer
does not roll over if stack pointer arithmetic is used.
Such behavior of the stack pointer is of particular importance when developing software on
a device with a different memory configuration than the target device.
Doc ID 14587 Rev 831/449
Page 32
Memory and register mapRM0016
ai15056
Customized stack model
End address
Stack roll-over limit
(1)
Stack pointer initialization value
Start address
RAM
Optional guard cells
(2)
Flexible stack size
Customized stack model
STM8S and STM8A stack pointer handling allows a customized stack model to be
implemented. This permits a flexible stack size without restrictions due to the stack roll-over
limit. Implementing the customized stack also benefits portability of the software on products
with different memory configurations. Figure 5 shows the customized stack model.
Figure 5.Customized stack model
1. The stack roll-over limit is not implemented on all devices.
2. The guard cells are RAM locations that have to be continuously polled by the application program to detect
whether a stack overflow has taken place.
In this stack model, the initial stack pointer must be placed beyond the stack roll-over limit.
Consequently, the growing stack never reaches the stack roll-over limit. It is clear that in this
implementation the stack size is not limited by the roll-over mechanism. Nevertheless, the
user has to define the stack position and stack size in the link file, and he has to ensure that
the stack pointer does not exceed the defined stack area (stack overflow or under-run).
The RAM locations above and below the customized stack can be regularly used as RAM to
store variables or other information.
Guard cells can be implemented at the lower end of the stack to detect if the stack pointer
exceeds the defined limit. These cells are standard RAM locations, initialized with fixed
values that the stack overwrites if an overflow occurs. The user software can regularly poll
these cells, detect the overflow condition, and put the application in a fail safe state.
During the software validation phase hardware breakpoints can be set at both limits of the
stack to validate that neither a stack overflow nor an under-run happens.
32/449 Doc ID 14587 Rev 8
Page 33
RM0016Memory and register map
3.2 Register description abbreviations
In the register descriptions of each chapter in this reference manual, the following
abbreviations are used:
Table 4.List of abbreviations
AbbreviationDescription
read/write (rw)Software can read and write to these bits.
read-only (r)Software can only read these bits.
write only (w)
read/write once (rwo)
read/clear (rc_w1)
read/clear (rc_w0)
Software can only write to this bit. Reading the bit returns a meaningless
value.
Software can only write once to this bit but can read it at any time. Only a
reset can return this bit to its reset value.
Software can read and clear this bit by writing 1. Writing ‘0’ has no effect on
the bit value.
Software can read and clear this bit by writing 0. Writing ‘1’ has no effect on
the bit value.
read/set (rs)Software can read and set this bit. Writing ‘0’ has no effect on the bit value.
read/clear by read
(rc_r)
Software can read this bit. Reading this bit automatically clears it to ‘0’.
Writing ‘0’ has no effect on the bit value.
Reserved (Res.)Reserved bit, must be kept at reset value.
Doc ID 14587 Rev 833/449
Page 34
Flash program memory and data EEPROMRM0016
4 Flash program memory and data EEPROM
4.1 Introduction
The embedded Flash program memory and data EEPROM memories are controlled by a
common set of registers. Using these registers, the application can program or erase
memory contents and set write protection, or configure specific low power modes. The
application can also program the device option bytes.
4.2 Glossary
●Block
A block is a set of bytes that can be programmed or erased in one single programming
operation. Operations that are performed at block level are faster than standard
programming and erasing. Refer to Tab le 5 for the details on block size.
●Page
A page is a set of blocks.
A dedicated option byte can be used to configure, by increments of one page, the size
of the user boot code.
34/449 Doc ID 14587 Rev 8
Page 35
RM0016Flash program memory and data EEPROM
4.3 Main Flash memory features
●STM8S and STM8A EEPROM is divided into two memory areas
–Up to 128 Kbytes of Flash program memory. The density differs according to the
device. Refer to Section 4.4: Memory organization for details
–Up to 2 Kbytes of data EEPROM including option bytes. Data EEPROM density
differs according to the device. Refer to Section 4.4: Memory organization for
details.
●Programming modes
–Byte programming and automatic fast byte programming (without erase operation)
–Word programming
–Block programming and fast block programming mode (without erase operation)
–Interrupt generation on end of program/erase operation and on illegal program
operation.
●Read-while-write capability (RWW). This feature is not available on all devices. Refer to
the datasheets for details
●In-application programming (IAP) and in-circuit programming (ICP) capabilities
●Protection features
–Memory readout protection (ROP)
–Program memory write protection with memory access security system (MASS
keys)
–Data memory write protection with memory access security system (MASS keys)
–Programmable write protected user boot code area (UBC).
●Memory state configurable to operating or power-down (I
modes
) in Halt and Active-halt
DDQ
Doc ID 14587 Rev 835/449
Page 36
Flash program memory and data EEPROMRM0016
4.4 Memory organization
4.4.1 STM8S and STM8A memory organization
STM8S and STM8A EEPROM is organized in 32-bit words (4 bytes per word).
The memory organization differs according to the devices:
●Low density STM8S devices
–8 Kbytes of Flash program memory organized in 128 pages or blocks of 64 bytes
each. The Flash program memory is divided into 2 areas, the user boot code area
(UBC), which size can be configured by option byte, and the main program
memory area. The Flash program memory is mapped in the upper part of the
STM8S addressing space and includes the reset and interrupt vectors.
–Up to 640 bytes of data EEPROM (DATA) organized in pages or blocks of 64 bytes
each. One block (64 bytes) contains the option bytes of which 11 are used to
configure the device hardware features. The options bytes can be programmed in
user, IAP and ICP/SWIM modes.
●Medium density STM8S devices
–From 16 to 32 Kbytes of Flash program memory organized in up to 64 pages of 4
blocks of 128 bytes each. The Flash program memory is divided into 2 areas, the
user boot code area (UBC), which size can be configured by option byte, and the
main program memory area. The Flash program memory is mapped in the upper
part of the STM8S addressing space and includes the reset and interrupt vectors.
–Up to 1 Kbyte of data EEPROM (DATA) organized in up to 2 pages of 4 blocks of
128 bytes each. One block (128 bytes) contains the option bytes of which 13 are
used to configure the device hardware features. The options bytes can be
programmed in user, IAP and ICP/SWIM modes.
●Medium density STM8A devices
–From 8 to 32 Kbytes of Flash program memory organized in up to 64 pages of 4
blocks of 128 bytes each. The Flash program memory is divided into 2 areas, the
user boot code area (UBC), which size can be configured by option byte, and the
main program memory area. The Flash program memory is mapped in the upper
part of the STM8A addressing space and includes the reset and interrupt vectors.
–Up to 1 Kbyte of data EEPROM (DATA) organized in up to 2 pages of 4 blocks of
128 bytes each. One block (128 bytes) contains the option bytes of which 13 are
used to configure the device hardware features. The options bytes can be
programmed in user, IAP and ICP/SWIM modes.
●High density STM8S devices
–From 32 to 128 Kbytes of Flash program memory organized in up to 256 pages of
4 blocks of 128 bytes each. The Flash program memory is divided into 2 areas, the
user boot code area (UBC), which size can be configured by option byte, and the
main program memory area. The Flash program memory is mapped in the upper
part of the STM8S addressing space and includes the reset and interrupt vectors.
–Up to 2 Kbytes of data EEPROM (DATA) organized in up to 4 pages of 4 blocks of
128 bytes each. The size of the DATA area is fixed for a given microcontroller. One
block (128 bytes) contains the option bytes of which 15 are used to configure the
36/449 Doc ID 14587 Rev 8
Page 37
RM0016Flash program memory and data EEPROM
device hardware features. The options bytes can be programmed in user, IAP and
ICP/SWIM modes.
●High density STM8A devices
–From 32 to 128 Kbytes of Flash program memory organized in up to 256 pages of
4 blocks of 128 bytes each. The Flash program memory is divided into 2 areas, the
user boot code area (UBC), which size can be configured by option byte, and the
main program memory area. The Flash program memory is mapped in the upper
part of the STM8A addressing space and includes the reset and interrupt vectors.
–Up to 2 Kbytes of data EEPROM (DATA) organized in up to 4 pages of 4 blocks of
128 bytes each. The size of the DATA area is fixed for a given microcontroller. One
block (128 bytes) contains the option bytes of which 15 are used to configure the
device hardware features. The options bytes can be programmed in user, IAP and
ICP/SWIM modes.
The page defines the granularity of the user boot code area as described in Section 4.4.3:
User boot area (UBC).
Figure 6, Figure 7, and Figure 8 show the Flash memory and data EEPROM organization
for STM8S and STM8A devices. Refer to the STM8S and STM8A programming manual
(PM0051) for more information.
Note:The EEPROM access time allows the device to run up to 16 MHz. For clock frequencies
above 16 MHz, Flash/data EEPROM access must be configured for 1 wait state. This is
enabled by the device option byte (refer to the option bytes section of the STM8S and
STM8A datasheets).
Doc ID 14587 Rev 837/449
Page 38
Flash program memory and data EEPROMRM0016
ai15503
USER BOOT CODE (UBC)
(permanently write protected)
0x00 8000
MAIN PROGRAM
(write access possible for IAP
and using MASS mechanism)
0x00 9FFF
Programmable size
from 2 pages (1 Kbytes)
up to 8 Kbytes
(1 page steps)
DATA MEMORY
(up to 640 bytes)
8 Kbytes of
FLASH PROGRAM
MEMORY
Flash program
memory
Interrupt vectors (128 bytes)
OPTION BYTES (1 block)
0x00 483F
0x00 4000
DATA EEPROM
1 page = 1 block = 64 bytes
0x00 4800
0x00 427F
Figure 6.Flash memory and data EEPROM organization on low density STM8S
38/449 Doc ID 14587 Rev 8
Page 39
RM0016Flash program memory and data EEPROM
Figure 7.Flash memory and data EEPROM organization on medium density STM8S and STM8A
1 page = 512 bytes
1 block = 128 bytes
00 4000h
DATA MEMORY
DATA EEPROM
00 43FFh
00 4800h
00 487Fh
(up to 1 Kbyte)
OPTION BYTES (1 block)
00 8000h
Programmable size
from 2 pages (1 Kbyte)
up to 32 Kbytes
Interrupt vectors (128 bytes)
USER BOOT CODE (UBC)
(permanently write protected)
(1 page steps)
Flash program
memory
MAIN PROGRAM
(write access possible for IAP
and using MASS mechanism)
00 FFFFh
ai15502
1. The memory mapping is given for the STM8A devices featuring 32 Kbytes of Flash program memory and 1 Kbytes of
SRAM.
Doc ID 14587 Rev 839/449
Page 40
Flash program memory and data EEPROMRM0016
ai15501b
USER BOOT CODE (UBC)
(permanently write protected)
0x00 8000
MAIN PROGRAM
(write access possible for IAP
and using MASS mechanism)
0x02 7FFF
Programmable size
from 2 pages (1 Kbytes)
up to 64 or 128 Kbytes
(1 page steps)
DATA MEMORY
(up to 2 Kbytes)
32 to 128 Kbytes of
Flash Program
Memory
Flash program
memory
Interrupt vectors (128 bytes)
OPTION BYTES (1 block)
0x00 487F
0x00 4000
DATA EEPROM
1 page = 512 bytes
1 block = 128 bytes
0x00 47FF
Figure 8.Flash memory and data EEPROM organization high density STM8S and STM8A
4.4.2 Memory access/ wait state configuration
4.4.3 User boot area (UBC)
40/449 Doc ID 14587 Rev 8
The Flash/ data EEPROM access time allows the device to run at up to 16 MHz without wait
states.
When using the high-speed external clock (HSE) at higher frequencies up to 24 MHz, one
wait state is necessary. In this case the device option byte should be programmed to insert
this wait state. Refer to the datasheet option byte section.
The user boot area (UBC) contains the reset and the interrupt vectors. It can be used to
store the IAP and communication routines. The UBC area has a second level of protection
to prevent unintentional erasing or modification during IAP programming. This means that it
is always write protected and the write protection cannot be unlocked using the MASS keys.
The size of the UBC area can be obtained by reading the UBC option byte.
The size of the UBC area can be configured in ICP mode (using the SWIM interface)
through the UBC option byte. The UBC option byte specifies the number of pages allocated
for the UBC area starting from address 0x00 8000.
Page 41
RM0016Flash program memory and data EEPROM
0x00 9FFF
0x00 9FC0
0x00 9F80
0x00 9F40
0x00 9F00
0x00 8100
0x00 8080
0x00 8040
Page 127
Page 126
Page 125
Page 124
Page 3
Page 2
Page 1
Page 0
0x00 8000
UBC[7:0] =0x01
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes to 8 Kbytes
64 bytes
user boot code area
UBC[7:0] =0x02
128 bytes
64 bytes
0x00 80C0
64 bytes
64 bytes
Interrupt vectors
UBC[7:0] =0x7F
8 Kbytes
Refer to Figure 9, Figure 10, and Figure 11 for a description of the UBC area memory
mapping and to the option byte section in the datasheets for more details on the UBC option
byte.
Figure 9.UBC area size definition on low density STM8S devices
1. N (number of protected pages) = UBC[7:0].
2. UBC[7:0] = 0x00 means no user boot code area is defined. Refer to the datasheets for the description of
the UBC option byte.
3. The first 2 pages (128 bytes) contain the interrupt vectors.
Doc ID 14587 Rev 841/449
Page 42
Flash program memory and data EEPROMRM0016
0x00 FFFF
0x00 FE00
0x00 FC00
0x00 FA00
0x00 F800
0x00 8800
0x00 8600
0x00 8400
0x00 8200
Page 63
Page 62
Page 61
Page 60
Page 3
Page 2
Page 1
Page 0
0x00 8000
UBC[7:0] =0x01
512 bytes
512 bytes
512 bytes
512 bytes
512 bytes
512 bytes
512 bytes
1K to 32 Kbytes
1 Kbytes
UBC[7:0] =0x3E
32 Kbytes
User boot code area
UBC[7:0] =0x02
2 Kbytes
0x00 807F
Interrupt vector table
Figure 10. UBC area size definition on medium density STM8S
and STM8A with up to 32 Kbytes of Flash program memory
1. N (number of protected pages) = UBC[7:0] + 2 for UBC[7:0] > 1.
2. UBC[7:0] =0x00 means no user boot code area is defined. Refer to the datasheets for the description of the
UBC option byte.
3. The first 2 pages (1 Kbytes) contain the 128 bytes of interrupt vectors (32 IT vectors).
42/449 Doc ID 14587 Rev 8
Page 43
RM0016Flash program memory and data EEPROM
0x02 7FFF
0x02 7E00
0x02 7C00
0x02 7A00
0x02 7800
0x00 8800
0x00 8600
0x00 8400
0x00 8200
Page 255
Page 254
Page 253
Page 252
Page 3
Page 2
Page 1
Page 0
0x00 8000
UBC[7:0] =0x01
512 bytes
512 bytes
512 bytes
512 bytes
512 bytes
512 bytes
512 bytes
1K to 128 Kbytes
1 Kbytes
UBC[7:0] =0xFE
128 Kbytes
User boot code area
UBC[7:0] =0x02
2 Kbytes
0x00 807F
Interrupt vector table
Figure 11. UBC area size definition on high density STM8S and
STM8A with up to 128 Kbytes of Flash program memory
1. UBC[7:0] = 0x00 means no user boot code area is defined. Refer to the datasheets for the description of
the UBC option byte.
2. The first 2 pages (1 Kbytes) contain the interrupt vectors, of which only 128 bytes (32 IT vectors) are used.
4.4.4 Data EEPROM (DATA)
The data EEPROM area can be used to store application data. By default, the DATA area is
write protected to prevent unintentional modification when the main program is updated in
IAP mode. The write protection can be unlocked only by using a specific MASS key
sequence (refer to Enabling write access to the DATA area).
Refer to Section 4.4: Memory organization for the size of the DATA area.
4.4.5 Main program area
4.4.6 Option bytes
The main program is the part of the Flash program memory which is used to store the
application code (see Figure 6, Figure 7 and Figure 8).
The option bytes are used to configure device hardware features and memory protection.
They are located in a dedicated memory array of one block.
The option bytes can be modified both in ICP/SWIM and in IAP mode, with OPT bit of the
FLASH_CR2 register set to 1 and the NOPT bit of the FLASH_NCR2 register set to 0 (see
Section 4.8.2: Flash control register 2 (FLASH_CR2) and Section 4.8.3: Flash
complementary control register 2 (FLASH_NCR2)).
Doc ID 14587 Rev 843/449
Page 44
Flash program memory and data EEPROMRM0016
Refer to the option byte section in the datasheet for more information on option bytes, and to
the STM8 SWIM protocol and debug module user manual (UM0470) for details on how to
program them.
4.5 Memory protection
4.5.1 Readout protection
Readout protection is selected by programming the ROP option byte to 0xAA. When readout
protection is enabled, reading or modifying the Flash program memory and DATA area in
ICP mode (using the SWIM interface) is forbidden, whatever the write protection settings.
Furthermore, on medium and high density STM8S and STM8A, the debug module (DM)
cannot start code execution by the CPU when the readout protection is active, and the CPU
is stalled.
Even if no protection can be considered as totally unbreakable, the readout feature provides
a very high level of protection for a general purpose microcontroller.
Removing the readout protection
The readout protection can be disabled on the program memory, UBC, and DATA areas, by
reprogramming the ROP option byte in ICP mode. In this case, the Flash program memory,
the DATA area and the option bytes are automatically erased and the device can be
reprogrammed.
Refer to Table 6: Memory access versus programming method for details on memory
access when readout protection is enabled or disabled.
4.5.2 Memory access security system (MASS)
After reset, the main program and DATA areas are protected against unintentional write
operations. They must be unlocked before attempting to modify their content. This unlock
mechanism is managed by the memory access security system (MASS).
The UBC area specified in the UBC option byte is always write protected (see Section 4.4.3:
User boot area (UBC)).
Once the memory has been modified, it is recommended to enable the write protection
again to protect the memory content against corruption.
Enabling write access to the main program memory
After a device reset, it is possible to disable the main program memory write protection by
writing consecutively two values called MASS keys to the FLASH_PUKR register (see
Section 4.8.6: Flash program memory unprotecting key register (FLASH_PUKR)). These
programmed keys are then compared to two hardware key values:
●First hardware key: 0b0101 0110 (0x56)
●Second hardware key: 0b1010 1110 (0xAE)
44/449 Doc ID 14587 Rev 8
Page 45
RM0016Flash program memory and data EEPROM
The following steps are required to disable write protection of the main program area:
1.Write a first 8-bit key into the FLASH_PUKR register. When this register is written for
the first time after a reset, the data bus content is not latched into the register, but
compared to the first hardware key value (0x56).
2. If the key available on the data bus is incorrect, the FLASH_PUKR register remains
locked until the next reset. Any new write commands sent to this address are
discarded.
3. If the first hardware key is correct when the FLASH_PUKR register is written for the
second time, the data bus content is still not latched into the register, but compared to
the second hardware key value (0xAE).
4. If the key available on the data bus is incorrect, the write protection on program
memory remains locked until the next reset. Any new write commands sent to this
address is discarded.
5. If the second hardware key is correct, the main program memory is write unprotected
and the PUL bit of the FLASH_IAPSR is set (see Section 4.8.8: Flash status register
(FLASH_IAPSR) register.
Before starting programming, the application must verify that PUL bit is effectively set. The
application can choose, at any time, to disable again write access to the Flash program
memory by clearing the PUL bit.
Enabling write access to the DATA area
After a device reset, it is possible to disable the DATA area write protection by writing
consecutively two values called MASS keys to the FLASH_DUKR register (see
Section 4.8.9: Flash register map and reset values). These programmed keys are then
compared to two hardware key values:
●First hardware key: 0b1010 1110 (0xAE)
●Second hardware key: 0b0101 0110 (0x56)
The following steps are required to disable write protection of the DATA area:
1.Write a first 8-bit key into the FLASH_DUKR register. When this register is written for
the first time after a reset, the data bus content is not latched into the register, but
compared to the first hardware key value (0xAE).
2. If the key available on the data bus is incorrect, the application can re-enter two MASS
keys to try unprotecting the DATA area.
3. If the first hardware key is correct, the FLASH_DUKR register is programmed with the
second key. The data bus content is still not latched into the register, but compared to
the second hardware key value (0x56).
4. If the key available on the data bus is incorrect, the data EEPROM area remains write
protected until the next reset. Any new write command sent to this address is ignored.
5. If the second hardware key is correct, the DATA area is write unprotected and the DUL
bit of the FLASH_IAPSR register is set (see Section 4.8.8: Flash status register
(FLASH_IAPSR)).
Before starting programming, the application must verify that the DATA area is not write
protected by checking that the DUL bit is effectively set. The application can choose, at any
time, to disable again write access to the DATA area by clearing the DUL bit.
Doc ID 14587 Rev 845/449
Page 46
Flash program memory and data EEPROMRM0016
4.5.3 Enabling write access to option bytes
The procedure for enabling write access to the option byte area is the same as the one used
for data EEPROM. However, the OPT bit in the Flash control register 2 (FLASH_CR2) must
be set, and the corresponding NOPT bit in the Flash complementary control register 2
(FLASH_NCR2) must be cleared to enable write access to the option bytes.
4.6 Memory programming
The main program memory, and the DATA area must be unlocked before attempting to
perform any program operation. The unlock mechanism depends on the memory area to be
programmed as described in Section 4.5.2: Memory access security system (MASS).
4.6.1 Read-while-write (RWW)
The RWW feature allows write operations to be performed on data EEPROM while reading
and executing the program memory. Execution time is therefore optimized. The opposite
operation is not allowed: Data memory cannot be read while writing to the program memory.
This RWW feature is always enabled and can be used at any time. Any access to Flash
control registers FLASH_CR1 and FLASH_CR2 while writing to the memory stalls the CPU,
making RWW unavailable.
Note:The RWW feature is not available on all devices. Refer to the datasheets for addition
information.
4.6.2 Byte programming
The main program memory and the DATA area can be programmed at byte level. To
program one byte, the application writes directly to the target address.
●In the main program memory:
The application stops for the duration of the byte program operation.
●In DATA area:
–Devices with RWW capability: Program execution does not stop, and the byte
program operation is performed using the read-while-write (RWW) capability in
IAP mode.
–Devices without RWW capability: The application stops for the duration of the byte
program operation.
To erase a byte, simply write 0x00 at the corresponding address.
The application can read the FLASH_IAPSR register to verify that the programming or
erasing operation has been correctly executed:
●EOP flag is set after a successful programming operation
●WR_PG_DIS is set when the software has tried to write to a protected page. In this
case, the write procedure is not performed.
As soon as one of these flags are set, a Flash interrupt is generated if it has been previously
enabled by setting the IE bit of the FLASH_CR1 register.
46/449 Doc ID 14587 Rev 8
Page 47
RM0016Flash program memory and data EEPROM
Automatic fast byte programming
The programming duration can vary according to the initial content of the target address. If
the word (4 bytes) containing the byte to be programmed is not empty, the whole word is
automatically erased before the program operation. On the contrary if the word is empty, no
erase operation is performed and the programming time is shorter (see t
PROG
in Ta bl e
“Flash program memory” in the datasheet).
However, the programming time can be fixed by setting the FIX bit of the FLASH_CR1
register to force the program operation to systematically erase the byte whatever its content
(see Section 4.8.1: Flash control register 1 (FLASH_CR1)). The programming time is
consequently fixed and equal to the sum of the erase and write time (see t
PROG
in Ta bl e
“Flash program memory” in the datasheet).
Note:To write a byte fast (no erase), the whole word (4 bytes) into which it is written must be
erased beforehand. Consequently, It is not possible to do two fast writes to the same word
(without an erase before the second write): The first write will be fast but the second write to
the other byte will require an erase.
4.6.3 Word programming
A word write operation allows an entire 4-byte word to be programmed in one shot, thus
minimizing the programming time.
As for byte programming, word operation is available both for the main program memory and
data EEPROM. On some devices, the read-while-write (RWW) capability is also available
when a word programming operation is performed on the data EEPROM. Refer to the
datasheets for additional information.
●In the main program memory:
The application stops for the duration of the byte program operation.
●In DATA area
–Devices with RWW capability: Program execution does not stop, and the byte
program operation is performed using the read-while-write (RWW) capability in
IAP mode.
–Devices without RWW capability: The application stops for the duration of the byte
program operation.
To program a word, the WPRG/NWPRG bits in the FLASH_CR2 and FLASH_NCR2
registers must be previously set/cleared to enable word programming mode (see
Section 4.8.2: Flash control register 2 (FLASH_CR2) and Section 4.8.2: Flash control
register 2 (FLASH_CR2)). Then, the 4 bytes of the word to be programmed must be loaded
starting with the first address. The programming cycle starts automatically when the 4 bytes
have been written.
As for byte operation, the EOP and the WR_PG_DIS control flags of FLASH_IAPSR,
together with the Flash interrupt, can be used to determine if the operation has been
correctly completed.
4.6.4 Block programming
Block program operations are much faster than byte or word program operations. In a block
program operation, a whole block is programmed or erased in a single programming cycle.
Refer to Ta bl e 5 for details on the block size according to the devices.
Doc ID 14587 Rev 847/449
Page 48
Flash program memory and data EEPROMRM0016
Block operations can be performed both to the main program memory and DATA area:
●In the main program memory:
Block program operations to the main program memory have to be executed totally
from RAM.
●In the DATA area
–Devices with RWW capability: DATA block operations can be executed from the
main program memory. However, the data loading phase (see below) has to be
executed from RAM.
–Devices without RWW capability: Block program operations must be executed
totally from RAM.
There are three possible block operations:
●Block programming, also called standard block programming: The block is
automatically erased before being programmed.
●Fast block programming: No previous erase operation is performed.
●Block erase
During block programming, interrupts are masked by hardware.
Standard block programming
A standard block program operation allows a whole block to be written in one shot. The
block is automatically erase before being programmed.
To program a whole block in standard mode, the PRG/NPRG bits in the FLASH_CR2 and
FLASH_NCR2 registers must be previously set/cleared to enable standard block
programming (see Section 4.8.2: Flash control register 2 (FLASH_CR2) andSection 4.8.2:
Flash control register 2 (FLASH_CR2)). Then, the block of data to be programmed must be
loaded sequentially to the destination addresses in the main program memory or DATA
area. This causes all the bytes of data to be latched. To start programming the whole block,
all bytes of data must be written. All bytes written in a programming sequence must be in the
same block. This means that they must have the same high address: Only the six least
significant bits of the address can change. When the last byte of the target block is loaded,
the programming starts automatically. It is preceded by an automatic erase operation of the
whole block.
When programming a block in DATA area, the application can check the HVOFF bit in the
Flash status register (FLASH_IAPSR). As soon the HVOFF flag is reset the actual
programming phase starts and the application can return to main program memory.
The EOP and the WR_PG_DIS control flags of the FLASH_IAPSR together with the Flash
interrupt can be used to determine if the operation has been correctly completed.
Fast block programming
Fast block programming allows programming without first erasing the memory contents.
Fast block programming is therefore twice as fast as standard programming.
This mode is intended only for programming parts that have already been erased. It is very
useful for programming blank parts with the complete application code, as the time saving is
significant.
Fast block programming is performed by using the same sequence as standard block
programming. To enable fast block programming mode, the FPRG/NFPRG bits of the
FLASH_CR2 and FLASH_NCR2 registers must be previously set/cleared.
48/449 Doc ID 14587 Rev 8
Page 49
RM0016Flash program memory and data EEPROM
The HVOFF flag can also be polled by the application which can execute other instructions
(RWW) during the actual programming phase of the DATA.
The EOP and WR_PG_DIS bits of the FLASH_IAPSR register can be checked to determine
if the fast block programming operation has been correctly completed.
Caution:The data programmed in the block are not guaranteed when the block is not blank before
the fast block program operation.
Block erasing
A block erase allows a whole block to be erased.
To erase a whole block, the ERASE/NERASE bits in the FLASH_CR2 and FLASH_NCR2
registers must be previously set/cleared to enable block erasing (see Section 4.8.2: Flash
control register 2 (FLASH_CR2) and Section 4.8.3: Flash complementary control register 2
(FLASH_NCR2)). The block is then erased by writing ‘0x00 00 00 00’ to any word inside the
block. The word start address must end with ‘0’, ‘4’, ‘8’, or ‘C’.
The EOP and the WR_PG_DIS control flags of the FLASH_IAPSR together with the Flash
interrupt can be used to determine if the operation has been correctly completed.
Table 5.Block size
STM8 microcontroller familyBlock size
Low density STM8S64 bytes
Medium density STM8S and STM8A (up to 32 Kbytes)128 bytes
High density STM8S and STM8A (up to 128 Kbytes)128 bytes
4.6.5 Option byte programming
Option byte programming is very similar to data EEPROM byte programming.
The application writes directly to the target address. The program does not stop and the
write operation is performed using the RWW capability.
Refer to the datasheet for details of the option byte contents.
4.7 ICP and IAP
The in-circuit programming (ICP) method is used to update the entire content of the memory,
using the SWIM interface to load the user application into the microcontroller. ICP offers
quick and efficient design iterations and eliminates unnecessary package handling or
socketing of devices. The SWIM interface (single wire interface module) uses the SWIM pin
to connect to the programming tool.
In contrast to the ICP method, in-application programming (IAP) can use any communication
interface supported by the microcontroller (I/Os, I
be programmed in the memory. IAP allows the Flash program memory content to be
reprogrammed during application execution. Nevertheless, part of the application must have
been previously programmed in the Flash program memory using ICP.
2
C, SPI, USART...) to download the data to
Refer to the STM8S and STM8A Flash programming manual (PM0051) and STM8 SWIM
protocol and debug manual (UM0470) for more information on programming procedures.
Doc ID 14587 Rev 849/449
Page 50
Flash program memory and data EEPROMRM0016
Table 6.Memory access versus programming method
ModeROPMemory Area
User boot code area (UBC)R/E
Readout
Main programR/W/E
protection
enabled
User, IAP, and bootloader (if
available)
Readout
Data EEPROM area (DATA)R/W
Option bytesR
User boot code area (UBC)R/E
Main programR/W/E
protection
disabled
Data EEPROM area (DATA)R/W
Option bytesR/W
User boot code area (UBC)P
Readout
Main programP
protection
enabled
SWIM active
(ICP mode)
Readout
Data EEPROM area (DATA)P
Option bytesP/W
User boot code area (UBC)R/E
Main programR/W/E
protection
disabled
Data EEPROM area (DATA)R/W
Option bytesR/W
(1)
Access from
core
(2)
(3)
(4)
(2)
(3)
(5)
(6)
ROP
(4)
(2)
(3)
(5)
1. R/W/E = Read, write, and execute
R/E = Read and execute (write operation forbidden)
R = Read (write and execute operations forbidden)
P = The area cannot be accessed (read, execute and write operations forbidden)
P/W
= Protected, write forbidden except for ROP option byte.
ROP
2. The Flash program memory is write protected (locked) until the correct MASS key is written in the FLASH_PUKR. It is
possible to lock the memory again by resetting the PUL bit in the FLASH_IAPSR register. If incorrect keys are provided, the
device must be reset and new keys programmed.
3. The data memory is write protected (locked) until the correct MASS key is written in the FLASH_DUKR. It is possible to lock
the memory again by resetting the DUL bit in the IAPSR register. If incorrect keys are provided, another key program
sequence can be performed without resetting the device.
4. To program the UBC area, the application must first clear the UBC option byte.
5. The option bytes are write protected (locked) until the correct MASS key is written in the FLASH_DUKR (with OPT set to 1).
It is possible to lock the memory again by resetting the DUL bit in the FLASH_IAPSR register. If incorrect keys are
provided, another key program sequence can be performed without resetting the device.
6. When ROP is removed, the whole memory is erased, including the option bytes.
50/449 Doc ID 14587 Rev 8
Page 51
RM0016Flash program memory and data EEPROM
4.8 Flash registers
4.8.1 Flash control register 1 (FLASH_CR1)
Address offset: 0x00
Reset value: 0x00
76543210
Reserved
Bits 7:4 Reserved
Bit 3 HALT: Power-down in Halt mode
This bit is set and cleared by software.
0: Flash in power-down mode when MCU is in Halt mode
1: Flash in operating mode when MCU is in Halt mode
Bit 2 AHALT: Power-down in Active-halt mode
This bit is set and cleared by software.
0: Flash in operating mode when MCU is in Active-halt mode
1: Flash in power-down when MCU is in Active-halt mode
Bit 1 IE: Flash Interrupt enable
This bit is set and cleared by software.
0: Interrupt disabled
1: Interrupt enabled. An interrupt is generated if the EOP or WR_PG_DIS flag in the
FLASH_IAPSR register is set.
HALTAHALTIEFIX
rwrwrwrw
Bit 0 FIX: Fixed Byte programming time
This bit is set and cleared by software.
0: Standard programming time of (1/2 t
) if the memory is already erased and t
prog
prog
otherwise.
1: Programming time fixed at t
prog
.
Doc ID 14587 Rev 851/449
Page 52
Flash program memory and data EEPROMRM0016
4.8.2 Flash control register 2 (FLASH_CR2)
Address offset: 0x01
Reset value: 0x00
76543210
OPTWPRGERASEFPRG
rwrwrwrwrw
Reserved
Bit 7 OPT: Write option bytes
This bit is set and cleared by software.
0: Write access to option bytes disabled
1: Write access to option bytes enabled
Bit 6 WPRG: Word programming
This bit is set by software and cleared by hardware when the operation is completed.
0: Word program operation disabled
1: Word program operation enabled
Bit 5 ERASE
(1)
: Block erasing
This bit is set by software and cleared by hardware when the operation is completed.
0: Block erase operation disabled
1: Block erase operation enabled
Bit 4 FPRG
(1)
: Fast block programming
This bit is set by software and cleared by hardware when the operation is completed.
0: Fast block program operation disabled
1: Fast block program operation enabled
Bits 3:1 Reserved
Bit 0 PRG: Standard block programming
This bit is set by software and cleared by hardware when the operation is completed.
0: Standard block programming operation disabled
1: Standard block programming operation enabled (automatically first erasing)
1. The ERASE and FPRG bits are locked when the memory is busy.
PRG
52/449 Doc ID 14587 Rev 8
Page 53
RM0016Flash program memory and data EEPROM
4.8.3 Flash complementary control register 2 (FLASH_NCR2)
Address offset: 0x02
Reset value: 0xFF
76543210
NOPTNWPRGNERASENFPRG
rwrwrwrwrw
Reserved
Bit 7 NOPT: Write option bytes
This bit is set and cleared by software.
0: Write access to option bytes enabled
1: Write access to option bytes disabled
Bit 6 NWPRG: Word programming
This bit is cleared by software and set by hardware when the operation is completed.
0: Word programming enabled
1: Word programming disabled
Bit 5 NERASE: Block erase
This bit is cleared by software and set by hardware when the operation is completed.
0: Block erase enabled
1: Block erase disabled
Bit 4 NFPRG: Fast block programming
This bit is cleared by software and set by software reading the register.
0: Fast block programming enabled (no erase before programming, the programmed data
values are not guaranteed when the block is not blank (fully erased) before the operation)
1: Fast block programming disabled
Bits 3:1 Reserved.
NPRG
Bit 0 NPRG: Block programming
This bit is cleared by software and set by hardware when the operation is completed.
0: Block programming enabled
1: Block programming disabled
Doc ID 14587 Rev 853/449
Page 54
Flash program memory and data EEPROMRM0016
4.8.4 Flash protection register (FLASH_FPR)
Address offset: 0x03
Reset value: 0x00
76543210
Reserved
WPB5WPB4WPB3WPB2WPB1WPB0
rorororororo
Bits 7:6 Reserved.
Bits 5:0 WPB[5:0]: User boot code area protection bits
These bits show the size of the boot code area. They are loaded at startup with the content of
the UBC option byte. Refer to the datasheets for the protected pages according to the bit
values.
4.8.5 Flash protection register (FLASH_NFPR)
Address offset: 0x04
Reset value: 0xFF
76543210
Reserved
Bits 7:6 Reserved.
NWPB5NWPB4NWPB3NWPB2NWPB1NWPB0
rorororororo
Bits 5:0 WPB[5:0]: User boot code area protection bits
These bits show the size of the boot code area. They reflect the content of the NUBC option
byte. Refer o the datasheet for the protected pages according to the bit values.
4.8.6 Flash program memory unprotecting key register (FLASH_PUKR)
Address offset: 0x08
Reset value: 0x00
76543210
MASS_PRG KEYS
rw
Bits 7:0 PUK [7:0]: Main program memory unlock keys
This byte is written by software (all modes). It returns 0x00 when read.
Refer to Enabling write access to the main program memory on page 44 for the description
of main program area write unprotection mechanism.
54/449 Doc ID 14587 Rev 8
Page 55
RM0016Flash program memory and data EEPROM
4.8.7 Data EEPROM unprotection key register (FLASH_DUKR)
Address offset: 0x0A
Reset value: 0x00
76543210
MASS_DATA KEYS
rw
Bits 7:0 DUK[7:0]: Data EEPROM write unlock keys
This byte is written by software (all modes). It returns 0x00 when read.
Refer to Enabling write access to the DATA area on page 45 for the description of main
program area write unprotection mechanism.
4.8.8 Flash status register (FLASH_IAPSR)
Address offset: 0x05
Reset value: 0x40
76543210
ReservedHVOFF
res.rrc_w0rc_rrc_w0rc_r
Reserved
DULEOPPULWR_PG_DIS
Bit 7 Reserved.
Bit 6 HVOFF: End of high voltage flag
This bit is set and cleared by hardware.
0: HV ON, start of actual programming
1: HV OFF, end of high voltage
Bits 5:4 Reserved, forced by hardware to 0.
Bit 3 DUL: Data EEPROM area unlocked flag
This bit is set by hardware and cleared by software by programming it to 0.
0: Data EEPROM area write protection enabled
1: Data EEPROM area write protection has been disabled by writing the correct MASS
keys
Bit 2 EOP: End of programming (write or erase operation) flag
This bit is set by hardware. It is cleared by software by reading the register, or when a new
write/erase operation starts.
0: No EOP event occurred
1: An EOP operation occurred. An interrupt is generated if the IE bit is set in the
FLASH_CR1 register.
Bit 1 PUL: Flash Program memory unlocked flag
This bit is set by hardware and cleared by software by programming it to 0.
0: Write protection of main Program area enabled
1: Write protection of main Program area has been disabled by writing the correct MASS
keys.
Bit 0 WR_PG_DIS: Write attempted to protected page flag
This bit is set by hardware and cleared by software by reading the register.
0: No WR_PG_DIS event occurred
1: A write attempt to a write protected page occurred. An interrupt is generated if the IE bit
is set in the FLASH_CR1 register.
Doc ID 14587 Rev 855/449
Page 56
Flash program memory and data EEPROMRM0016
4.8.9 Flash register map and reset values
For details on the Flash register boundary addresses, refer to the general hardware register
map in the datasheets.
Table 7.Flash register map and reset values
AddressRegister name7654 3 2 1 0
0x00
0x01
0x02
0x03
0x04
0x05
0x06-0x07Reserved
0x08
0x09Reserved
0x0A
FLASH_CR1
Reset value
FLASH_CR2
Reset value
FLASH_NCR2
Reset value
FLASH_FPR
Reset value
FLASH_NFPR
Reset value
FLASH_IAPSR
Reset value
FLASH_PUKR
Reset value
FLASH_DUKR
Reset value
-
0
OPT
0
NOPT1NWPRG
-
0
-
1
-
0
PUK7
0
DUK7
0
0
WPRG
0
1
0
1
HVOFF
1
PUK6
0
DUNP60DUK5
-
0
ERASE0FPRG
NERASE1NFPRG
WPB5
0
NWPB51NWPB41NWPB31NWPB21NWPB1
--DUL
PUK5
0
0
-
0
0
1
WPB4
0
PUK4
0
DUK4
0
HALT
0
-
0
-
1
WPB3
0
0
PUK3
0
DUK3
0
AHALT
0
0
1
WPB2
0
EOP
0
PUK2
0
DUK2
0
IE
0
-
0
-
1
WPB1
0
1
PUL
0
PUK1
0
DUK1
0
FIX
0
PRG
0
NPRG
1
WPB0
0
NWPB0
1
WR_PG_DIS
0
PUK0
0
DUK0
0
56/449 Doc ID 14587 Rev 8
Page 57
RM0016Single wire interface module (SWIM) and debug module (DM)
MCU
SWIM/PA0
Jumper selection for
debug purposes
I/O for application
SWIM interface for tools
5 Single wire interface module (SWIM) and debug
module (DM)
5.1 Introduction
In-circuit debugging mode or in-circuit programming mode are managed through a single
wire hardware interface featuring ultrafast memory programming. Coupled with an in-circuit
debugging module, it also offers a non-intrusive emulation mode, making the in-circuit
debugger extremely powerful, close in performance to a full-featured emulator.
5.2 Main features
●Based on an asynchronous, high sink (8 mA), open-drain, bidirectional communication.
●Allows reading or writing any part of memory space.
●Access to CPU registers (A, X, Y, CC, SP). They are memory mapped for read or write
access.
●Non intrusive read/write on the fly to the RAM and peripheral registers.
●Device reset capability with status flag in the Reset status register (RST_SR).
●Clock speed selectable in the SWIM clock control register (CLK_SWIMCCR).
SWIM pin can be used as a standard I/O with some restrictions if you also want to use it for
debug. The most secure way is to provide on the PCB a strap option.
Figure 12. SWIM pin connection
5.3 SWIM modes
After a power-on reset, the SWIM is reset and enters OFF mode.
1.OFF: Default state after power-on reset. The SWIM pin cannot be used by the
application as an I/O.
2. I/O: This state is entered by software writing to the SWD bit in the Global configuration
register (CFG_GCR). In this state, the SWIM pin can be used by the application as a
standard I/O pin. In case of a reset, the SWIM goes back to OFF mode.
3. SWIM: This state is entered when a specific sequence is performed on the SWIM pin.
In this state, the SWIM pin is used by the host tool to control the STM8 with 3
Note: Refer to the STM8 SWIM communication Protocol and Debug Module User Manual for a
commands (SRST system reset, ROTF read on the fly, WOTF write on the fly).
description of the SWIM and Debug module (DM) registers.
Doc ID 14587 Rev 857/449
Page 58
Interrupt controller (ITC)RM0016
6 Interrupt controller (ITC)
6.1 ITC introduction
●Management of hardware interrupts
–External interrupt capability on most I/O pins with dedicated interrupt vector and
edge sensitivity setting per port
–Peripheral interrupt capability
●Management of software interrupt (TRAP)
●Nested or concurrent interrupt management with flexible interrupt priority and level
management:
–Up to 4 software programmable nesting levels
–Up to 32 interrupt vectors fixed by hardware
–2 non maskable events: RESET, TRAP
–1 non-maskable top level hardware interrupt (TLI)
This interrupt management is based on:
●Bit I1 and I0 of the CPU Condition Code register (CCR)
●Software priority registers (ITC_SPRx)
●Reset vector address 0x00 8000 at the beginning of program memory. In devices with
boot ROM, the reset initialization routine is programmed in ROM by
STMicroelectronics.
●Fixed interrupt vector addresses located at the high addresses of the memory map
(0x00 8004 to 0x00 807C) sorted by hardware priority order.
6.2 Interrupt masking and processing flow
The interrupt masking is managed by bits I1 and I0 of the CCR register and by the
ITC_SPRx registers which set the software priority level of each interrupt vector (see
Ta bl e 8 ). The processing flow is shown in Figure 13.
When an interrupt request has to be serviced:
1.Normal processing is suspended at the end of the current instruction execution.
2. The PC, X,Y, A and CCR registers are saved onto the stack.
3. Bits I1 and I0 of CCR register are set according to the values in the ITC_SPRx registers
corresponding to the serviced interrupt vector.
4. The PC is then loaded with the interrupt vector of the interrupt to service and the first
instruction of the interrupt service routine is fetched .
The interrupt service routine should end with the IRET instruction which causes the content
of the saved registers to be recovered from the stack. As a consequence of the IRET
instruction, bits I1 and I0 are restored from the stack and the program execution resumes.
58/449 Doc ID 14587 Rev 8
Page 59
RM0016Interrupt controller (ITC)
“IRET”
RESTORE PC, X, Y, A, CCR
STACK PC, X, Y, A, CCR
LOAD I1:0 FROM INTERRUPT SW REG.
FETCH NEXT
RESET
TRAP
PENDING
INSTRUCTION
I1:0
FROM STACK
LOAD PC FROM INTERRUPT VECTOR
Y
N
Y
N
Y
N
Interrupt has the same or a
lower software priority
THE INTERRUPT
STAYS PENDING
than current one
Interrupt has a higher
software priority
than current one
EXECUTE
INSTRUCTION
INTERRUPT
Table 8.Software priority levels
Software priorityLevelI1I0
Level 0 (main)
Level 101
Level 200
Low
High
10
Level 3 (= software priority disabled)11
Figure 13. Interrupt processing flowchart
Caution:If the interrupt mask bits I0 and I1 are set within an interrupt service routine (ISR) with the
instruction SIM, removal of the interrupt mask with RIM causes the software priority to be
set to level 0.
To restore the correct priority when disabling and enabling interrupts inside an ISR, follow
the procedures presented in Ta bl e 8 for disabling and enabling the interrupts.
Table 9.Interrupt enabling/disabling inside an ISR
Disabling the interruptsEnabling the interrupts
#asm
PUSH CC
POP ISR_CC
(1)
SIM
#endasm
1. IRS_CC is a variable which stores the current value of the CC register.
Doc ID 14587 Rev 859/449
#asm
PUSH ISR_CC
POP CC
#endasm
(1)
Page 60
Interrupt controller (ITC)RM0016
PENDING
SOFTWARE
Different
INTERRUPTS
Same
HIGHEST HARDWARE
PRIORITY SERVICED
PRIORITY
HIGHEST SOFTWARE
PRIORITY SERVICED
6.2.1 Servicing pending interrupts
Several interrupts can be pending at the same time. The interrupt to be taken into account is
determined by the following two-step process:
1.The highest software priority interrupt is serviced.
2. If several interrupts have the same software priority then the interrupt with the highest
hardware priority is serviced first.
When an interrupt request is not serviced immediately, it is latched and then processed
when its software priority combined with the hardware priority becomes the highest one.
Note:1The hardware priority is exclusive while the software one is not. This allows the previous
process to succeed with only one interrupt.
2RESET, TLI and TRAP are considered as having the highest software priority in the decision
process.
See Figure 14 for a description of pending interrupt servicing process.
Figure 14. Priority decision process
6.2.2 Interrupt sources
60/449 Doc ID 14587 Rev 8
Two interrupt source types are managed by the STM8 interrupt controller:
●Non-maskable interrupts: RESET, TLI and TRAP
●Maskable interrupts: external interrupts or interrupts issued by internal peripherals
Non-maskable interrupt sources
Non-maskable interrupt sources are processed regardless of the state of bits I1 and I0 of
the CCR register (see Figure 13). PC, X, Y, A and CCR registers are stacked only when a
Page 61
RM0016Interrupt controller (ITC)
TRAP interrupt occurs. The corresponding vector is then loaded in the PC register and bits
I1 and I0 of the CCR register are set to disable interrupts (level 3).
●TRAP (non-maskable software interrupt)
This software interrupt source is serviced when the TRAP instruction is executed. It is
serviced as a TLI according to the flowchart shown in Figure 13.
A TRAP interrupt does not allow the processor to exit from Halt mode.
●RESET
The RESET interrupt source has the highest STM8 software and hardware priorities.
This means that all the interrupts are disabled at the beginning of the reset routine.
They must be re-enabled by the RIM instruction (see Table 11: Dedicated interrupt
instruction set).
A RESET interrupt allows the processor to exit from Halt mode.
See RESET chapter for more details on RESET interrupt management.
●TLI (top level hardware interrupt)
This hardware interrupt occurs when a specific edge is detected on the corresponding
TLI input.
Caution:A TRAP instruction must not be used in a TLI service routine.
Doc ID 14587 Rev 861/449
Page 62
Interrupt controller (ITC)RM0016
Maskable interrupt sources
Maskable interrupt vector sources are serviced if the corresponding interrupt is enabled and
if its own interrupt software priority in ITC_SPRx registers is higher than the one currently
being serviced (I1 and I0 in CCR register). If one of these two conditions is not met, the
interrupt is latched and remains pending.
●External interrupts
External interrupts can be used to wake up the MCU from Halt mode. The device
sensitivity to external interrupts can be selected by software through the External
Interrupt Control registers (EXTI_CRx).
When several input pins connected to the same interrupt line are selected
simultaneously, they are logically ORed.
When external level-triggered interrupts are latched, if the given level is still present at
the end of the interrupt routine, the interrupt remains activated except if it has been
inactivated in the routine.
●Peripheral interrupts
Most peripheral interrupts cause the MCU to wake up from Halt mode. See the interrupt
vector table in the datasheet.
A peripheral interrupt occurs when a specific flag is set in the peripheral status register
and the corresponding enable bit is set in the peripheral control register.
The standard sequence for clearing a peripheral interrupt performs an access to the
status register followed by a read or write to an associated register. The clearing
sequence resets the internal latch. A pending interrupt (that is an interrupt waiting to be
serviced) is therefore lost when the clear sequence is executed.
6.3 Interrupts and low power modes
All interrupts allow the processor to exit from Wait mode.
Only external and other specific interrupts allow the processor to exit from Halt and Activehalt mode (see wakeup from halt and wakeup from Active-halt in the interrupt vector table in
the datasheet).
When several pending interrupts are present while waking up from Halt mode, the first
interrupt serviced can only be an interrupt with exit-from-Halt mode capability. It is selected
through the decision process shown in Figure 14. If the highest priority pending interrupt
cannot wake up the device from Halt mode, it will be serviced next.
If any internal or external interrupt (from a timer for example) occurs while the HALT
instruction is executing, the HALT instruction is completed but the interrupt invokes the
wakeup process immediately after the HALT instruction has finished executing. In this case
the MCU is actually waking up from Halt mode to Run mode, with the corresponding delay of
t
as specified in the datasheet.
WUH
6.4 Activation level/low power mode control
The MCU activation level is configured by programming the AL bit in the CFG_GCR register
(see global configuration register (CFG_GCR)).
This bit is used to control the low power modes of the MCU. In very low power applications,
the MCU spends most of the time in WFI and is woken up (through interrupts) at specific
62/449 Doc ID 14587 Rev 8
Page 63
RM0016Interrupt controller (ITC)
MAIN
IT4
IT2
IT1
TRAP
IT1
MAIN
IT0
I1
HARDWARE PRIORITY
SOFTWARE
3
3
3
3
3
3/0
3
11
11
11
11
11
11 / 10
11
RIM
IT2
IT1
IT4
TRAP
IT3
IT0
IT3
I0
10
PRIORITY
LEVEL
USED STACK = 10 BYTES
moments in order to execute a specific task. Some of these recurring tasks are short
enough to be treated directly in an ISR (interrupt service routine), rather than going back to
the main program. To cover this case, you can set the AL bit before entering Low power
mode (by executing WFI instruction), then the interrupt routine returns directly to Low power
mode. The run time/ISR execution is reduced due to the fact that the register context is
saved only on the first interrupt.
As a consequence, all the operations can be executed in ISR in very simple applications. In
more complex ones, an interrupt routine may relaunch the main program by simply resetting
the AL bit.
For example, an application may need to be woken up by the auto-wakeup unit (AWU) every
50 ms in order to check the status of some pins/sensors/push-buttons. Most of the time, as
these pins are not active, the MCU can return to Low power mode without running the main
program. If one of these pins is active, the ISR decides to launch the main program by
resetting the AL bit.
6.5 Concurrent and nested interrupt management
STM8 devices feature two interrupt management modes:
●Concurrent mode
●Nested mode
6.5.1 Concurrent interrupt management mode
In this mode, all interrupts are interrupt priority level 3 so that none of them can be
interrupted, except by a TLI, RESET, or TRAP.
The hardware priority is given in the following order from the lowest to the highest priority,
that is: MAIN, IT4, IT3, IT2, IT1, IT0, TRAP/TLI (same priority), and RESET.
Figure 15 shows an example of concurrent interrupt management mode.
Figure 15. Concurrent interrupt management
Doc ID 14587 Rev 863/449
Page 64
Interrupt controller (ITC)RM0016
6.5.2 Nested interrupt management mode
In this mode, interrupts are allowed during interrupt routines. This mode is activated as soon
as an interrupt priority level lower than level 3 is set.
The hardware priority is given in the following order from the lowest to the highest priority,
that is: MAIN, IT4, IT3, IT2, IT1, IT0, and TRAP.
The software priority is configured for each interrupt vector by setting the corresponding
I1_x and I0_x bits of the ITC_SPRx register. I1_x and I0_x bits have the same meaning as
I1 and I0 bits of the CCR register (see Tab le 1 0 ).
Level 0 can not be programmed (I1_x=1, I0_x=0). In this case, the previously stored value is
kept. For example: if previous value is 0xCF, and programmed value equals 64h, the result is
44h.
The RESET and TRAP vectors have no software priorities. When one is serviced, bits I1
and I0 of the CCR register are both set.
Caution:If bits I1_x and I0_x are modified while the interrupt x is executed, the device operates as
follows: if the interrupt x is still pending (new interrupt or flag not cleared) and the new
software priority is higher than the previous one, then the interrupt x is re-entered.
Otherwise, the software priority remains unchanged till the next interrupt request (after the
IRET of the interrupt x).
During the execution of an interrupt routine, the HALT, POPCC, RIM, SIM and WFI
instructions change the current software priority till the next IRET instruction or one of the
previously mentioned instructions is issued. See Section 6.7 for the list of dedicated
interrupt instructions.
Figure 16 shows an example of nested interrupt management mode.
Warning:A stack overflow may occur without notifying the software of
the failure.
Table 10.Vector address map versus software priority bits
Vector addressITC_SPRx bits
0x00 8008hI1_0 and I0_0 bits
0x00 800ChI1_1 and I0_1 bits
......
0x00 807ChI1_29 and I0_29 bits
1. ITC_SPRx register bits corresponding to the TLI can be read and written. However they are not significant
in the interrupt process management.
(1)
64/449 Doc ID 14587 Rev 8
Page 65
RM0016Interrupt controller (ITC)
MAIN
IT2
TRAP
MAIN
IT0
IT2
IT1
IT4
TRAP
IT3
IT0
HARDWARE PRIORITY
3
2
1
3
3
3/0
3
11
00
01
11
11
11
RIM
IT1
IT4
IT4
IT1
IT2
IT3
I1I0
11 / 10
10
SOFTWARE
PRIORITY
LEVEL
USED STACK = 20 BYTES
Figure 16. Nested interrupt management
6.6 External interrupts
Five interrupt vectors are dedicated to external Interrupt events:
●5 lines on Port A: PA[6:2]
●8 lines on Port B: PB[7:0]
●8 lines on Port C: PC[7:0]
●7 lines on Port D: PD[6:0]
●8 lines on Port E: PE[7:0]
PD7 is the Top Level Interrupt source (TLI), except for 20-pin packages on which the Top
Level Interrupt source (TLI) can be available on the PC3 pin using an alternate function
remapping option bit. Refer to option bytes section in the product datasheet for more details.
To generate an interrupt, the corresponding GPIO port must be configured in input mode
with interrupts enabled. Refer to the register description in the GPIO chapter for details.
The interrupt sensitivity must be configured in the external interrupt control register 1
(EXTI_CR1) and external interrupt control register 2 (EXTI_CR2) (see Section 6.9.3 and
RIMEnable interrupt (level 0 set)Load 10 in I1:0 of CCR10
SIMDisable interrupt (level 3 set)Load 11 in I1:0 of CCR11
TRAPSoftware trapSoftware NMI11
WFIWait for interrupt10
6.8 Interrupt mapping
Refer to the corresponding device datasheet for the table of interrupt vector addresses.
66/449 Doc ID 14587 Rev 8
Page 67
RM0016Interrupt controller (ITC)
6.9 ITC and EXTI registers
6.9.1 CPU condition code register interrupt bits (CCR)
Address: refer to the general hardware register map table in the datasheet.
Reset value: 0x28
76543210
V–I1HI0NZC
rrrwrrwrrr
Bits 5, 3
(1)
I[1:0]: Software interrupt priority bits
(2)
These two bits indicate the software priority of the current interrupt request. When an
interrupt request occurs, the software priority of the corresponding vector is loaded
automatically from the software priority registers (ITC_SPRx).
The I[1:0] bits can be also set/cleared by software using the RIM, SIM, HALT, WFI, IRET or
PUSH/POP instructions (see Figure 16: Nested interrupt management).
I1I0PriorityLevel
10Level 0 (main)
01Level 1
00Level 2
11Level 3 (= software priority disabled*)
1. Refer to the central processing section for details on the other CCR bits.
2. TLI, TRAP and RESET events can interrupt a level-3 program.
Bits 7:0 VECTxSPR[1:0]: Vector x software priority bits
These eight read/write registers (ITC_SPR1 to ITC_SPR8) are written by software to define
the software priority of each interrupt vector.
The list of vectors is given in Table 10: Vector address map versus software priority bits.
Refer to Section 6.9.1: CPU condition code register interrupt bits (CCR) for the values to be
programmed for each priority.
ITC_SPR1 bits 1:0 are forced to 1 by hardware (TLI)
ITC_SPR8 bits 7:4 are forced to 1 by hardware.
Note: It is forbidden to write 10 (priority level 0). If 10 is written, the previous value is kept
and the interrupt priority remains unchanged.
68/449 Doc ID 14587 Rev 8
Page 69
RM0016Interrupt controller (ITC)
6.9.3 External interrupt control register 1 (EXTI_CR1)
Address offset: 0x00
Reset value: 0x00
76543210
PDIS[1:0]PCIS[1:0]PBIS[1:0]PAIS[1:0]
rwrwrwrw
Bits 7:6 PDIS[1:0]: Port D external interrupt sensitivity bits
These bits can only be written when I1 and I0 in the CCR register are both set to 1 (level 3).
They define the sensitivity of Port D external interrupts.
00: Falling edge and low level
01: Rising edge only
10: Falling edge only
11: Rising and falling edge
Bits 5:4 PCIS[1:0]: Port C external interrupt sensitivity bits
These bits can only be written when I1 and I0 in the CCR register are both set to 1 (level 3).
They define the sensitivity of Port C external interrupts.
00: Falling edge and low level
01: Rising edge only
10: Falling edge only
11: Rising and falling edge
Bits 3:2 PBIS[1:0]: Port B external interrupt sensitivity bits
These bits can only be written when I1 and I0 in the CCR register are both set to 1 (level 3).
They define the sensitivity of Port B external interrupts.
00: Falling edge and low level
01: Rising edge only
10: Falling edge only
11: Rising and falling edge
Bits 1:0 PAIS[1:0]: Port A external interrupt sensitivity bits
These bits can only be written when I1 and I0 in the CCR register are both set to 1 (level 3).
They define the sensitivity of Port A external interrupts.
00: Falling edge and low level
01: Rising edge only
10: Falling edge only
11: Rising and falling edge
Doc ID 14587 Rev 869/449
Page 70
Interrupt controller (ITC)RM0016
6.9.4 External interrupt control register 1 (EXTI_CR2)
Address offset: 0x01
Reset value: 0x00
76543210
Reserved
Bits 7:3 Reserved.
Bit 2 TLIS: Top level interrupt sensitivity
This bit is set and cleared by software. This bit can be written only when external interrupt
is disabled on the corresponding GPIO port (PD7 or PC3, refer to Section 6.6: External
interrupts on page 65).
0: Falling edge
1: Rising edge
Bits 1:0 PEIS[1:0]: Port E external interrupt sensitivity bits
These bits can only be written when I1 and I0 in the CCR register are both set to 1 (level 3).
They define the sensitivity of the Port E external interrupts.
00: Falling edge and low level
01: Rising edge only
10: Falling edge only
11: Rising and falling edge
TLISPEIS[1:0]
rwrw
70/449 Doc ID 14587 Rev 8
Page 71
RM0016Interrupt controller (ITC)
6.9.5 ITC and EXTI register map and reset values
Table 12.Interrupt register map
Add.
offset
ITC-SPR block
0x00
0x01
0x02
0x03
0x04
0x05
0x06
0x07
ITC-EXTI block
0x00EXTI_CR1
0x01EXTI_CR2
1. The address offsets are expressed for the ITC-SPR block base address (see CPU/SWIM/debug module/interrupt controller registers table in the
datasheet).
2. The address offsets are expressed for the ITC-EXTI block base address (see General hardware register map table in the datasheet).
Reserved
1
1
1
1
1
1
1
1
PA IS 0
0
PEIS0
0
Doc ID 14587 Rev 871/449
Page 72
Power supplyRM0016
Low Power Voltage Regulator
V
DD
V
CAP
V
DDIO
Main Voltage Regulator
I/O buffers
MCU core
1.8V
3V-5.5V
RAM
Flash
3V-5.5V
A/D converter
V
DDA
V
SSA
3V-5.5V
V
REF+
V
REF-
CPU
V
18
7 Power supply
The MCU has four distinct power supplies:
●V
DD/VSS
●V
DDIO/VSSIO
●V
DDA/VSSA
●V
REF+/VREF-
The V
DD/VSS
internal Low Power Voltage Regulator (LPVR). The 2 regulator outputs are connected and
provide the 1.8 V supply (V
In low power modes the system automatically switches from the MVR to the LPVR in order
to reduce current consumption.
To stabilize the MVR, a capacitor must be connected to the VCAP pin (for more details refer
to the datasheet electrical characteristics section).
Depending on the package size, there are one or two pairs of dedicated pins for
V
DDIO/VSSIO
V
DDA/VSSA
: Main power supply (3 V to 5.5 V)
: I/O power supply (3 V to 5.5 V)
: Power supply for the analog functions
: Reference supply for Analog Digital Converter
pins are used to supply the internal Main Voltage Regulator (MVR) and the
) to the MCU core (CPU, Flash and RAM)
18
to supply power to the I/Os.
and V
REF+/VREF-
are connected to the Analog to Digital Converter (ADC).
Figure 17. Power supply overview
72/449 Doc ID 14587 Rev 8
Page 73
RM0016Reset (RST)
NRST
R
PU
V
DD_IO
PULSE
GENERATOR
SWIM RESET
EXTERNAL
RESET
(min 20 µs)
SYSTEM NRESET
ILLEGAL OPCODE RESET
EMC RESET
IWDG/WWDG/SOFTWARE RESET
POR/BOR RESET
Filter
(typ 45 kΩ)
8 Reset (RST)
There are 9 reset sources:
●External reset through the NRST pin
●Power-on reset (POR)
●Brown-out Reset (BOR)
●Independent watchdog reset (IWDG)
●Window watchdog reset (WWDG)
●Software reset
●SWIM reset
●Illegal opcode reset
●EMC reset: generated if critical registers are corrupted or badly loaded
These sources act on the RESET
pin and it is always kept low during the delay phase. The
RESET service routine vector is fixed at address 6000h in the memory map.
Figure 18. Reset circuit
8.1 “Reset state” and “under reset” definitions
When a reset occurs, there is a reset phase from the external pin pull-down to the internal
reset signal release. During this phase, the microcontroller sets some hardware
configurations before going to the reset vector.
At the end of this phase, most of the registers are configured with their “reset state” values.
During the reset phase, i.e. “under reset”, some pin configurations may be different from
their “reset state” configuration.
8.2 Reset circuit description
The NRST pin is both an input and an open-drain output with integrated RPU weak pull-up
resistor.
The low pulse of duration t
reset detection is asynchronous and therefore the MCU can enter reset even in Halt mode.
The NRST pin also acts as an open-drain output for resetting external devices.
INFP(NRST)
Doc ID 14587 Rev 873/449
on the NRST pin generates an external reset. The
Page 74
Reset (RST)RM0016
V
DD/VDDIO
NRST
V
IT+
V
IT-
An internal temporization maintains a pulse of duration t
OP(NRST)
source. An additional internal weak pull-up ensures a high level on the reset pin when the
reset is not forced.
8.3 Internal reset sources
Each internal reset source is linked to a specific flag bit in the Reset status register
(RST_SR) except POR/BOR which have no flag. These flags are set respectively at reset
depending on the given reset source. So they are used to identify the last reset source. They
are cleared by software writing the logic value “1”.
8.3.1 Power-on reset (POR) and brown-out reset (BOR)
During power-on, the POR keeps the device under reset until the supply voltages (V
V
) reach the voltage level at which the BOR starts to function. At this point, the BOR
DDIO
reset replaces the POR and the POR is automatically switched off. The BOR reset is
maintained till the supply voltage reaches the operating voltage range.
See Electrical parameters section of the datasheet for more details.
The BOR also generates a reset when the supply voltage drops below the V
When this occurs, the POR is re-armed for the next power-on phase.
An hysteresis is implemented to ensure clean detection of voltage rise and fall.
The BOR always remains active even when the MCU is put into Low Power mode.
whatever the internal reset
DD and
threshold.
IT-
Figure 19. V
DD/VDDIO
voltage detection: POR/BOR threshold
74/449 Doc ID 14587 Rev 8
Page 75
RM0016Reset (RST)
8.3.2 Watchdog reset
Refer to Section 15: Window watchdog (WWDG) and Section 14: Independent watchdog
(IWDG) for details.
8.3.3 Software reset
The application software can trigger reset by clearing bit T6 in the WWDG_CR register.
Refer to Section 15: Window watchdog (WWDG).
8.3.4 SWIM reset
An external device connected to the SWIM interface can request the SWIM block to
generate an MCU reset.
8.3.5 Illegal opcode reset
In order to provide enhanced robustness to the device against unexpected behavior, a
system of illegal opcode detection is implemented. If a code to be executed does not
correspond to any opcode or prebyte value, a reset is generated. This, combined with the
Watchdog, allows recovery from an unexpected fault or interference.
Note:A valid prebyte associated with a valid opcode forming an unauthorized combination does
not generate a reset.
8.3.6 EMC reset
To protect the application against spurious write access or system hang-up, possibly caused
by electromagnetic disturbance, the most critical registers are implemented as two bitfields
that must contain complementary values. Mismatches are automatically detected by this
mechanism, triggering an EMC reset and allowing the application to cleanly recover normal
operations.
Doc ID 14587 Rev 875/449
Page 76
Reset (RST)RM0016
8.4 RST register description
8.4.1 Reset status register (RST_SR)
Address offset: 0x00
Reset value: 0xXX
76543210
Reserved
EMCFSWIMFILLOPFIWDGFWWDGF
rc_w1rc_w1rc_w1rc_w1rc_w1
Bits 7:5 Reserved.
Bit 4 EMCF: EMC reset flag
This bit is set by hardware and cleared by software writing “1”.
0: No EMC reset occurred
1: An EMC reset occurred (possible cause: complementary register or option byte mismatch).
Bit 3 SWIMF: SWIM reset flag
This bit is set by hardware and cleared by software writing “1”.
0: No SWIM reset occurred
1: A SWIM reset occurred
Bit 2 ILLOPF: Illegal opcode reset flag
This bit is set by hardware and cleared by software writing “1”.
0: No ILLOP reset occurred
1: An ILLOP reset occurred
Bit 1 IWDGF: Independent Watchdog reset flag
This bit is set by hardware and cleared by software writing “1”.
0: No IWDG reset occurred
1: An IWDG reset occurred
Bit 0 WWDGF: Window Watchdog reset flag
This bit is set by hardware and cleared by software writing “1”.
0: No WWDG reset occurred
1: An WWDG reset occurred
8.5 RST register map
Refer to the corresponding datasheet for the base address.
Table 13.RST register map
Address
offset
0x00
76/449 Doc ID 14587 Rev 8
Register Name765432 1 0
RST_SR
Reset value
-
x
-
x
-
x
EMCFxSWIMFxILLOPFxIWDGF
x
WWDGF
x
Page 77
RM0016Clock control (CLK)
9 Clock control (CLK)
The clock controller is designed to be powerful, very robust, and at the same time easy to
use. Its purpose is to allow you to obtain the best performance in your application while at
the same time get the full benefit of all the microcontroller’s power saving capabilities.
You can manage all the different clock sources independently and distribute them to the
CPU and to the various peripherals. Prescalers are available for the master and CPU clocks.
A safe and glitch-free switch mechanism allows you to switch the master clock on the fly
from one clock source to another one.
EMC-hardened clock configuration registers
To protect the application against spurious write access or system hang-up, possibly caused
by electromagnetic disturbance, the most critical CLK registers are implemented as two
bitfields that must contain complementary values. Mismatches are automatically detected
by the CLK, triggering an EMC reset and allowing the application to cleanly recover normal
operations. See CLK register description for more details.
4 different clock sources can be used to drive the master clock:
●1-24 MHz high speed external crystal oscillator (HSE)
●Up to 24 MHz high speed user-external clock (HSE user-ext)
●16 MHz high speed internal RC oscillator (HSI)
●128 kHz low speed internal RC (LSI)
Each clock source can be switched on or off independently when it is not used, to optimize
power consumption.
9.1.1 HSE
The high speed external clock signal (HSE) can be generated from two possible clock
sources:
●HSE external crystal/ceramic resonator
●HSE user external clock
Figure 21. HSE clock sources
Hardware configuration
External clockCrystal/ceramic resonators
The resonator and the load capacitors have to be placed as close as possible to the
oscillator pins in order to minimize output distortion and start-up stabilization time. The
loading capacitance values must be adjusted according to the selected oscillator.
Doc ID 14587 Rev 879/449
Page 80
Clock control (CLK)RM0016
External crystal/ceramic resonator (HSE crystal)
The 1 to 24 MHz external oscillator has the advantage of producing a very accurate rate on
the main clock with 50% duty cycle.
The associated hardware configuration is shown in Figure 21. Refer to the electrical
characteristics section for more details.
At start up the clock signal produced by the oscillator is not stable, and by default a delay of
2048 osc cycles is inserted before the clock signal is released. You can program a shorter
stabilization time in the HSECNT option byte, please refer to option bytes section in the
datasheet.
The HSERDY flag in the External clock register (CLK_ECKR) indicates if the high-speed
external oscillator is stable or not. At startup, the clock is not released until this bit is set by
hardware.
The HSE Crystal can be switched on and off using the HSEEN bit in the External clock
register (CLK_ECKR).
External source (HSE user-ext)
In this mode, an external clock source must be provided. It can have a frequency of up to
24 MHz. You select this mode by programming the EXTCLK option bit. Refer to the option
bytes section of the datasheet. The external clock signal (square, sinus or triangle) with
~50% duty cycle has to drive the OSCIN pin while the OSCOUT pin is available as standard
I/O (see Figure 20).
Note:For clock frequencies above 16 MHz, Flash /data EEPROM access must be configured for 1
wait state. This is enabled by the device option byte. Refer to the datasheet option byte
section.
9.1.2 HSI
The HSI clock signal is generated from an internal 16 MHz RC oscillator together with a
programmable divider (factor 1 to 8). This is programmed in the Clock divider register
(CLK_CKDIVR).
Note:At startup the master clock source is automatically selected as HSI RC clock output divided
by 8 (f
The HSI RC oscillator has the advantage of providing a 16 MHz master clock source with
50% duty cycle at low cost (no external components). It also has a faster startup time than
the HSE crystal oscillator however, even with calibration the frequency is less accurate than
an external crystal oscillator or ceramic resonator.
The HSIRDY flag in the Internal clock register (CLK_ICKR) indicates if the HSI RC is stable
or not. At startup, the HSI RC output clock is not released until this bit is set by hardware.
The HSI RC can be switched on and off using the HSIEN bit in the Internal clock register
(CLK_ICKR).
Backup source
The HSI/8 signal can also be used as a backup source (Auxiliary clock) if the HSE crystal
oscillator fails. Refer to Section 9.6: Clock security system (CSS).
HSI
/8).
80/449 Doc ID 14587 Rev 8
Page 81
RM0016Clock control (CLK)
Fast wakeup feature
If the FHWU bit in the Internal clock register (CLK_ICKR) is set, this automatically selects
the HSI clock as master clock after MCU wakeup from Halt or Active-halt (see Low power
chapter).
Calibration
Each device is factory calibrated by ST.
After reset, the factory calibration value is automatically loaded in an internal calibration
register.
If the application is subject to voltage or temperature variations this may affect the RC
oscillator speed. You can trim the HSI frequency in the application using the HSI clock
calibration trimming register (CLK_HSITRIMR). In this register there are 3 or 4 bits providing
an additional trimming value that is added to the internal HSI calibration register value.
The width of the trimming steps with 4 bits is half the trimming step width with 3 bits.
As the trimming step width depends on the absolute frequency of the RC oscillator, a
successive approximation method needs to be applied for the trimming. This method is
described in a separate technical document.
The 128 kHz LSI RC acts as a low power, low cost alternative master clock source as well
as a low power clock source that can be kept running in Halt mode for the independent
watchdog (IWDG) and auto-wakeup unit (AWU).
The LSI RC can be switched on and off using the LSIEN bit in the Internal clock register
(CLK_ICKR).
Doc ID 14587 Rev 881/449
Page 82
Clock control (CLK)RM0016
The LSIRDY flag in the Internal clock register (CLK_ICKR) indicates if the low-speed
internal oscillator is stable or not. At startup, the clock is not released until this bit is set by
hardware.
Calibration
Like the HSI RC, the LSI RC device is factory calibrated by ST. However, it is not possible to
perform further trimming.
Note:When using the independent watchdog with the LSI as clock source, in order to guarantee
that the CPU will never run on the same clock in case of corruption, the LSI clock cannot be
the master clock if LSI_EN option bit is reset. Refer to the option bytes section in the
datasheet.
9.2 Master clock switching
The clock switching feature provides an easy to use, fast and secure way for the application
to switch from one master clock source to another.
9.2.1 System startup
For fast system startup, after a reset the clock controller configures the master clock source
as HSI RC clock output divided by 8 (HSI/8). This is to take advantage of the short
stabilization time of the HSI oscillator. The /8 divider is to ensure safe start-up in case of
poor V
Once the master clock is released, the user program can switch the master clock to another
clock source.
conditions.
DD
9.2.2 Master clock switching procedures
To switch clock sources, you can proceed in one of two ways:
●Automatic switching
●Manual switching
Automatic switching
The automatic switching enables, the user to launch a clock switch with a minimum number
of instructions. The software can continue doing other operations without taking care of the
switch event exact time.
To enable automatic switching, follow the sequence below (refer to the flowchart in
Figure 22):
1.Enable the switching mechanism by setting the SWEN bit in the Switch control register
(CLK_SWCR).
2. Write the 8-bit value used to select the target clock source in the Clock master switch
register (CLK_SWR). The SWBSY bit in the CLK_SWCR register is set by hardware,
and the target source oscillator starts. The old clock source continues to drive the CPU
and peripherals.
As soon as the target clock source is ready (stabilized), the content of the CLK_SWR
register is copied to the Clock master status register (CLK_CMSR).
82/449 Doc ID 14587 Rev 8
Page 83
RM0016Clock control (CLK)
The SWBSY bit is cleared and the new clock source replaces the old one. The SWIF flag in
the CLK_SWCR is set and an interrupt is generated if the SWIEN bit is set.
Manual switching
The manual switching is not as immediate as the automatic switching but it offers to the user
a precise control of the switch event time.
To enable manual switching, follow the sequence below (refer to the flowchart in Figure 23):
1.Write the 8-bit value used to select the target clock source in the Clock master switch
register (CLK_SWR). Then the SWBSY bit is set by hardware, and the target source
oscillator starts. The old clock source continues to drive the CPU and peripherals.
2. The software has to wait until the target clock source is ready (stabilized). This is
indicated by the SWIF flag in the CLK_SWCR register and by an interrupt if the SWIEN
bit is set.
3. The final software action is to set, at the chosen time, the SWEN bit in the CLK_SWCR
register to execute the switch.
In both manual and automatic switching modes, the old master clock source will not be
powered off automatically in case it is required by other blocks (the LSI RC may be used to
drive the independent watchdog for example). The clock source can be powered off using
the bits in the Internal clock register (CLK_ICKR) and External clock register (CLK_ECKR).
If the clock switch does not work for any reason, software can reset the current switch
operation by clearing the SWBSY flag. This will restore the CLK_SWR register to its
previous content (old master clock).
Doc ID 14587 Rev 883/449
Page 84
Clock control (CLK)RM0016
Reset
MCU in Run mode with HSI/8
Write target clock source in CLK_SWR
Target clock source ready after
CLK_SWR
CLK_CMSR
SWBSY
0
Set SWEN bit in CLK_SWCR
Target clock source powered on
SWBSY
1
stabilization time
Switch busy
MCU in Run mode
with new master clock source
SOFTWARE ACTIONHARDWARE ACTION
Reset switch busy flag
Update clock master status
Clear SWIF flag
Set SWIEN bit in CLK_SWCR to enable interrupt if suitable
divided according to the CKAWUSEL option bit. Refer to option bytes section in the
datasheet.
The Low speed clock source for the AWU or the independent watchdog can be LSI or HSE
The division factor for HSE has to be programmed in the HSEPRSC[1:0] option bits Refer to
in the option bytes section of the datasheet. The goal is to get 128 kHz at the output of the
HSE prescaler.
9.4 CPU clock divider
The CPU clock (f
programmed in the CPUDIV[2:0] bits in the Clock divider register (CLK_CKDIVR). Seven
division factors (1 to 128 in steps of power of 2) can be selected (refer to Figure 20).
The f
signal is the clock for both the CPU and the window watchdog.
CPU
) is derived from the master clock (f
CPU
MASTER
), divided by a factor
Doc ID 14587 Rev 885/449
Page 86
Clock control (CLK)RM0016
9.5 Peripheral clock gating (PCG)
Gating the clock to unused peripherals helps reduce power consumption. Peripheral clock
Gating (PCG) mode allows you to selectively enable or disable the f
MASTER
connection to the following peripherals at any time in Run mode:
●ADC
●I2C
●AWU (register clock, not counter clock)
●SPI
●TIM[4:1]
●UART
●CAN (register clock, not CAN clock)
After a device reset, all peripheral clocks are enabled. You can disable the clock to any
peripheral by clearing the corresponding PCKEN bit in the Peripheral clock gating register 1
(CLK_PCKENR1) and in the Peripheral clock gating register 2 (CLK_PCKENR2). But you
have to disable properly the peripheral using the appropriate bit, before stopping the
corresponding clock.
To enable a peripheral, you must first enable the corresponding PCKEN bit in the
CLK_PCKENR registers and then set the peripheral enable bit in the peripheral’s control
registers.
clock
The AWU counter is driven by an internal or external clock (LSI or HSE) independent from
f
MASTER
, so that it continues to run even if the register clock to this peripheral is switched off.
86/449 Doc ID 14587 Rev 8
Page 87
RM0016Clock control (CLK)
9.6 Clock security system (CSS)
The Clock Security System (CSS) monitors HSE crystal clock source failures. When
f
MASTER
broken or disconnected resonator or any other reason, the clock controller activates a stallsafe recovery mechanism by automatically switching f
(HSI/8). Once selected the auxiliary clock source remains enabled until the MCU is reset.
You enable the clock security system by setting the CSSEN bit in the Clock security system
register (CLK_CSSR). For safety reason, once CSS is enabled it cannot be disabled until
the next reset.
The following conditions must be met so that the CSS can detect HSE quartz crystal
failures:
●HSE crystal on: (HSEEN = 1 in the External clock register (CLK_ECKR))
●HSE oscillator in quartz crystal configuration (EXTCLK option bit is reset)
●CSS function enabled: (CSSEN = 1 in the CLK_CSSR register)
If HSE is the current clock master when a failure is detected, the CSS performs the following
actions:
●The CSSD bit is set in the CLK_CSSR register and an interrupt is generated if the
●The Clock master status register (CLK_CMSR), Clock master switch register
●The HSIEN bit in the Internal clock register (CLK_ICKR) register is set (HSI on).
●The HSEEN bit in the External clock register (CLK_ECKR) is cleared (HSE off)
●The AUX bit is set to indicate that the HSI/8 auxiliary clock source is forced.
depends on HSE crystal, i.e. when HSE is selected, if the HSE clock fails due to a
MASTER
to the auxiliary clock source
CSSIEN bit is set.
(CLK_SWR) register and the HSIDIV[1:0] bits in the Clock divider register
(CLK_CKDIVR) are set to their reset values (CKM[7:0]= SWI[7:0]=E1h). HSI/8
becomes the master clock.
You can clear the CSSD bit by software but the AUX bit is cleared only by reset.
To select a faster clock speed, you can modify the HSIDIV[1:0] bits in the CLK_CKDIVR
register after the CSSD bit in the CLK_CSSR register is cleared.
If HSE is not the current clock master when a failure is detected, the master clock is not
switched to the auxiliary clock and none of the above actions are performed except:
●The HSEEN bit is cleared in the CLK_ECKR register, HSE is then switched OFF
●The CSSD bit is set in the CLK_CSSR register and interrupt is generated if CSSDIE is
also set, it can be cleared by software.
If HSE is not the current clock master and the master clock switch to HSE is ongoing, the
SWBSY bit in the CLK_SWCR register must be cleared by software before clearing the
CSSD bit.
If HSE is selected by CCOSEL to be in output mode (see Clock-out capability (CCO)) when
a failure is detected, the selection is automatically changed to force HSI (HSIDIV) instead of
HSE.
Doc ID 14587 Rev 887/449
Page 88
Clock control (CLK)RM0016
9.7 Clock-out capability (CCO)
The configurable Clock Output (CCO) capability allows you to output a clock on the external
CCO pin. You can select one of 6 clock signals as CCO clock:
●f
HSE
●
f
HSI
●
f
HSIDIV
●
f
LSI
●
f
MASTER
●
f
(with current prescaling selection)
CPU
Note:50% duty cycle is not guaranteed on all possible prescaled values
The selection is controlled by the CCOSEL[3:0] bits in the Configurable clock output register
(CLK_CCOR).
The user has to select first the desired clock for the dedicated I/O pin (see Pin Description
chapter). This I/O must be set at 1 in the corresponding Px_CR1 register to be set as input
with pull-up or push-pull output.
The sequence to really output the chosen clock starts with CCOEN=1 in Configurable clock
output register (CLK_CCOR).
The CCOBSY is set to indicate that the configurable clock output system is operating. As
long as the CCOBSY bit is set, the CCOSEL bits are write protected.
The CCO automatically activates the target oscillator if needed. The CCORDY bit is set
when the chosen clock is ready.
To disable the clock output the user has to clear the CCOEN bit. Both CCOBSY and
CCORDY remain at 1 till the shut down is completed. The time between the clear of CCOEN
and the reset of the two flags can be relatively long, for instance in case the selected clock
output is very slow compared to f
9.8 CLK interrupts
The following interrupts can be generated by the clock controller:
●Master clock source switch event
●Clock Security System event
Both interrupts are individually maskable.
Table 16.CLK interrupt requests
Interrupt event
CSS eventCSSDCSSDIEYesNo
Master clock switch eventSWIFSWIENYesNo
CPU
.
Event
flag
Enable
control
bit
Exit
from
wait
Exit
from
Halt
88/449 Doc ID 14587 Rev 8
Page 89
RM0016Clock control (CLK)
9.9 CLK register description
9.9.1 Internal clock register (CLK_ICKR)
Address offset: 0x00
Reset value: 0x01
76543210
Reserved
REGAHLSIRDYLSIENFHWHSIRDYHSIEN
rwrrwrwrrw
Bits 7:6 Reserved, must be kept cleared.
Bit 5 REGAH: Regulator power off in Active-halt mode
This bit is set and cleared by software. When it is set, the main voltage regulator is powered off as
soon as the MCU enters Active-halt mode, so the wakeup time is longer.
0: MVR regulator ON in Active-halt mode
1: MVR regulator OFF in Active-halt mode
Bit 4 LSIRDY: Low speed internal oscillator ready
This bit is set and cleared by hardware.
0: LSI clock not ready
1: LSI clock ready
Bit 3 LSIEN: Low speed internal RC oscillator enable
This bit is set and cleared by software. It is set by hardware whenever the LSI oscillator is required,
for example:
–When switching to the LSI clock (see CLK_SWR register)
–When LSI is selected as the active CCO source (see CLK_CCOR register)
–When BEEP is enabled (BEEPEN bit set in the BEEP_CSR register)
–When LSI measurement is enabled (MSR bit set in the AWU_CSR register)
It cannot be cleared when LSI is selected as master clock source (CLK_CMSR register), as active
CCO source or as clock source for the AWU peripheral or independent Watchdog.
0: Low-speed internal RC off
1: Low-speed internal RC on
Bit 2 FHWU: Fast wakeup from Halt/Active-halt modes
This bit is set and cleared by software.
0: Fast wakeup from Halt/Active-halt modes disabled
1: Fast wakeup from Halt/Active-halt modes enabled
Bit 1 HSIRDY: High speed internal oscillator ready
This bit is set and cleared by hardware.
0: HSI clock not ready
1: HSI clock ready
Doc ID 14587 Rev 889/449
Page 90
Clock control (CLK)RM0016
Bit 0 HSIEN: High speed internal RC oscillator enable
This bit is set and cleared by software. It is set by hardware whenever the HSI oscillator is required,
for example:
–When activated as safe oscillator by the CSS
–When switching to HSI clock (see CLK_SWR register)
–When HSI is selected as the active CCO source (see CLK_CCOR register)
It cannot be cleared when HSI is selected as clock master (CLK_CMSR register), as active CCO
source or if the safe oscillator (AUX) is enabled.
0: High-speed internal RC off
1: High-speed internal RC on
9.9.2 External clock register (CLK_ECKR)
Address offset: 0x01
Reset value: 0x00
76543210
Reserved
Bits 7:2 Reserved, must be kept cleared.
Bit 1 HSERDY: High speed external crystal oscillator ready
This bit is set and cleared by hardware.
0: HSE clock not ready
1: HSE clock ready (HSE clock is stabilized and available)
Bit 0 HSEEN: High speed external crystal oscillator enable
This bit is set and cleared by software. It can be used to switch the external crystal oscillator on or
off. It is set by hardware in the following cases:
–When switching to HSE clock (see CLK_SWR register)
–When HSE is selected as the active CCO source (see CLK_CCOR register)
It cannot be cleared when HSE is selected as clock master (indicated in CLK_CMSR register) or as
the active CCO source.
0: HSE clock off
1: HSE clock on
HSERDYHSEEN
rrw
90/449 Doc ID 14587 Rev 8
Page 91
RM0016Clock control (CLK)
9.9.3 Clock master status register (CLK_CMSR)
Address offset:0x03
Reset value: 0xE1
76543210
CKM[7:0]
rrrrrrrr
Bits 7:0 CKM[7:0]: Clock master status bits
These bits are set and cleared by hardware. They indicate the currently selected master clock
source. An invalid value occurring in this register will automatically generate an MCU reset.
0xE1: HSI selected as master clock source (reset value)
0xD2: LSI selected as master clock source (only if LSI_EN option bit is set)
0xB4: HSE selected as master clock source
9.9.4 Clock master switch register (CLK_SWR)
Address offset: 0x04
Reset value: 0xE1
76543210
SWI[7:0]
rwrwrwrwrwrwrwrw
Bits 7:0 SWI[7:0]: Clock master selection bits
These bits are written by software to select the master clock source. Its contents are write protected
while a clock switch is ongoing (while the SWBSY bit is set). They are set to the reset value (HSI) if
the AUX bit is set in the CLK_CSSR register. If Fast Halt wakeup mode is selected (FHW bit =1 in
CLK_ICKR register) then these bits are set by hardware to E1h (HSI selected) when resuming from
Halt/Active-halt mode.
0xE1: HSI selected as master clock source (reset value)
0xD2: LSI selected as master clock source (only if LSI_EN option bit is set)
0xB4: HSE selected as master clock source
Doc ID 14587 Rev 891/449
Page 92
Clock control (CLK)RM0016
9.9.5 Switch control register (CLK_SWCR)
Address offset: 0x05
Reset value: 0xXX
76543210
Reserved
Bits 7:4 Reserved.
Bit 3 SWIF: Clock switch interrupt flag
This bit is set by hardware and cleared by software writing 0. Its meaning depends on the status of
the SWEN bit. Refer to Figure 22 and Figure 23.
This bit is set and cleared by hardware. It can be cleared by software to reset the clock switch
process.
0: No clock switch ongoing
1: Clock switch ongoing
92/449 Doc ID 14587 Rev 8
Page 93
RM0016Clock control (CLK)
9.9.6 Clock divider register (CLK_CKDIVR)
Address offset: 0x06
Reset value: 0x18
76543210
Reserved
HSIDIV[1:0]CPUDIV[2:0]
rwrwrwrwrw
Bits 7:5 Reserved, must be kept cleared.
Bits 4:3 HSIDIV[1:0]: High speed internal clock prescaler
These bits are written by software to define the HSI prescaling factor.
00: f
01: f
10: f
11: f
HSI
HSI
HSI
HSI
= f
= f
= f
= f
HSI RC output
HSI RC output
HSI RC output
HSI RC output
/2
/4
/8
Bits 2:0 CPUDIV[2:0]: CPU clock prescaler
These bits are written by software to define the CPU clock prescaling factor.
000: f
These bits are written by software to enable or disable the f
peripheral. See Ta b le 1 7
0: f
MASTER
1: f
MASTER
Table 18.Peripheral clock gating bits
to peripheral disabled
to peripheral enabled
Control bitPeripheral
PCKEN27CAN (product dependent, see datasheet)
MASTER
clock to the corresponding
PCKEN26Reserved
PCKEN25Reserved
PCKEN24Reserved
PCKEN23ADC
PCKEN22AWU
PCKEN21Reserved
PCKEN20Reserved
Doc ID 14587 Rev 895/449
Page 96
Clock control (CLK)RM0016
9.9.9 Clock security system register (CLK_CSSR)
Address offset: 0x08
Reset value: 0x00
76543210
Reserved
CSSDCSSDIEAUXCSSEN
rc_w0rwrrwo
Bits 7:4 Reserved, must be kept cleared.
Bit 3 CSSD: Clock security system detection
This bit is set by hardware and cleared by software writing 0.
0: CSS is off or no HSE crystal clock disturbance detected.
1: HSE crystal clock disturbance detected.
Bit 2 CSSDIE: Clock security system detection interrupt enable
This bit is set and cleared by software.
0: Clock security system interrupt disabled
1: Clock security system interrupt enabled
Bit 1 AUX: Auxiliary oscillator connected to master clock
This bit is set and cleared by hardware.
0: Auxiliary oscillator is off.
1: Auxiliary oscillator (HSI/8) is on and selected as current clock master source.
Bit 0 CSSEN: Clock security system enable
This bit can be read many times and be written once-only by software.
0: Clock security system off
1: Clock security system on
This bit is set and cleared by hardware. It indicates that the selected CCO clock source is being
switched-on and stabilized. While CCOBSY is set, the CCOSEL bits are write-protected. CCOBSY
remains set until the CCO clock is enabled.
0: CCO clock not busy
1: CCO clock busy
Bit 5 CCORDY: Configurable clock output ready
This bit is set and cleared by hardware. It indicates that the CCO clock is being output.
0: CCO clock not available
1: CCO clock available
These bits are written by software to fine tune the HSI calibration.
Note: In high density STM8S and STM8A devices, only bits 2:0 are available.
In other devices, bits 3:0 are available to achieve a better HSI resolution. Compatibility with bits
2:0 can be selected through options bytes (refer to datasheet).
HSITRIM[3:0]
rwrwrwrw
98/449 Doc ID 14587 Rev 8
Page 99
RM0016Clock control (CLK)
9.9.12 SWIM clock control register (CLK_SWIMCCR)
Address offset: 0x0D
Reset value: 0bXXXX XXX0
76543210
Bits 7:1 Reserved.
Bit 0 SWIMCLK SWIM clock divider
This bit is set and cleared by software.
0: SWIM clock divided by 2
1: SWIM clock not divided by 2