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
1.2
1.3
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2
Example 2: Automatic clock switching . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.1
2.2
2.3
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3
Example 3: Manual clock switching . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.1
3.2
3.3
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4
Example 4: Configuring clocks for maximum speed . . . . . . . . . . . . . . . 6
4.1
4.2
4.3
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2/8
AN2669
Example 1: Clock security system (CSS) capability
1
1.1
Example 1: Clock security system (CSS) capability
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
1 MΩ
60
Ω
OSCOUT
PH0
PH1
PH2
PH3
OSCIN
1 kΩ
1 kΩ
1 kΩ
1 kΩ
LD1
STM8A
LD2
LD3
LD4
CCO
Oscilloscope
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).
3/8
Example 2: Automatic clock switching
AN2669
2
2.1
Example 2: Automatic clock switching
Overview
The purpose of this example is to demonstrate the automatic clock switching feature
available in the STM8A. It provides an easy to use, fast and secure way for the application to
switch from one master clock source to another using very few instructions.
This example is provided as CLK example 2 in the STM8 firmware library.
2.2
Hardware description
The STM8/128-EVAL evaluation board is used for this example. The main hardware
connections are shown in
Figure 1.
2.3
Firmware description
The user can change the clock selection at any time, by pressing the joystick in the desired
direction according to
Table 1.
Table 1.
Joystick control
Joystick movement
Up
Down
Right
Clock source to switch to
HSE clock
LSI clock
HSI clock
The current clock selection is displayed on the LCD.
Each clock source is associated with a LED as shown in
Table 2.
Table 2.
Status of LEDs display
LED
LD1
LD2
LD3
Master clock source
HSE clock
LSI clock
HSI clock
The LED blinking frequency depends on the clock speed. Clock switching can also be
monitored by connecting an oscilloscope to the CCO pin (PE0 ).
4/8
AN2669
Example 3: Manual clock switching
3
3.1
Example 3: Manual clock switching
Overview
This example demonstrates how to manually switch the STM8A master clock from one clock
source to another. Manual switching requires more instructions than automatic switching
(described in
Section 2)
but it gives the application precise control of the switch event time.
This example is provided as CLK example 3 in the STM8 firmware library.
3.2
Hardware description
The STM8/128-EVAL evaluation board is used for this example. The main hardware
connections are shown in
Figure 1.
3.3
Firmware description
When this example is running on the evaluation board, the user can select a new clock
source using the joystick (see
Table 1).
Each time a clock source is selected, LEDs 1 to 3 blink for a short period. Clock switching
can also be monitored by connecting an oscilloscope to the CCO pin (PE0 ).
5/8