AN2669
Application note
How to configure the STM8A microcontroller clocks
Introduction
The STM8A microcontroller has a very flexible clock controller (CLK) that allows the clocks
to be reconfigured at any time to suit different requirements or functional modes in the
application.
There are four clock sources that can be used.
■ 1-24 MHz high speed external crystal (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)
This application note provides practical examples of how to program the STM8A CLK
registers. The source code of the examples is included in the STM8 firmware library.
For further information and a hardware description of the STM8A CLK, please refer to the
section on ‘Clock control (CLK)’ in the STM8A microcontroller family reference manual
(RM0009). Please contact STMicroelectronics for information on the STM8 firmware library.
September 2008 Rev 2 1/8
www.st.com
Contents AN2669
Contents
1 Example 1: Clock security system (CSS) capability . . . . . . . . . . . . . . . 3
1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Example 2: Automatic clock switching . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2 Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3 Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Example 3: Manual clock switching . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.2 Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.3 Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4 Example 4: Configuring clocks for maximum speed . . . . . . . . . . . . . . . 6
4.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.2 Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.3 Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2/8
AN2669 Example 1: Clock security system (CSS) capability
1 Example 1: Clock security system (CSS) capability
1.1 Overview
This example shows how to use the CSS capability. Enabling the CSS, is a way of making
the application more reliable, by ensuring the microcontroller keeps running even if the main
clock, (HSE) fails.
The example is provided as CLK example 1 in the STM8 firmware library. It uses most of the
features of the CLK driver provided in the library.
1.2 Hardware description
Figure 1 shows the main hardware connections needed to run the CSS.
Figure 1. STM8A CLK interface
24 MHz
20 pF
20 pF
Oscilloscope
1 MΩ
60 Ω
OSCIN
OSCOUT
CCO
STM8A
1.3 Firmware description
The STM8 firmware library includes the CLK driver which supports all CLK features through
a set of functions.
The current example shows how to configure the CLK to detect a failure on the HSE.
When the 24 MHz HSE clock is used as the system clock, the four LEDs (LD1, LD2, LD3,
and LD4) connected to pins PH3, PH2, PH1 and PH0 respectively are toggled with a fast
frequency derived from the system clock.
The CSS is enabled, so that when the HSE clock disappears (when it is broken or the
external crystal is disconnected), the following clock recovery sequence is performed
automatically:
● The HSE clock is disabled and the system clock switches to the HSI backup clock.
● The CPU continues processing operations but the frequency is changed to HSI/8 (CSS
default clock).
● LED1, 2, 3, 4 toggle at a slower frequency.
● The clock switchover can be monitored by connecting an oscilloscope to the CCO pin
(PE0).
PH0
PH1
PH2
PH3
1 kΩ
1 kΩ
1 kΩ
1 kΩ
LD1 LD2 LD3 LD4
3/8