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 2 3 4 5 2/8 Example 1: Clock security system (CSS) capability . . . . . . . . . . . . . . . 3 1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.3 Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Example 2: Automatic clock switching . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2.2 Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2.3 Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Example 3: Manual clock switching . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.2 Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.3 Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Example 4: Configuring clocks for maximum speed . . . . . . . . . . . . . . . 6 4.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 4.2 Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 4.3 Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 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 OSCIN 1 MΩ 60 Ω OSCOUT CCO Oscilloscope PH0 PH1 PH2 PH3 1 kΩ 1 kΩ 1 kΩ 1 kΩ LD1 LD2 LD3 LD4 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). 3/8 Example 2: Automatic clock switching AN2669 2 Example 2: Automatic clock switching 2.1 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 Clock source to switch to Up HSE clock Down LSI clock Right 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 Master clock source LD1 HSE clock LD2 LSI clock LD3 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 Example 3: Manual clock switching 3.1 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 Example 4: Configuring clocks for maximum speed AN2669 4 Example 4: Configuring clocks for maximum speed 4.1 Overview This example shows how to configure the STM8A microcontroller to run at its highest or second highest frequency using the external or internal clock sources (HSE or HSI respectively). Such configurations make the CPU run at the frequency provided by the CLK without any prescaler or division factors. This example is provided as CLK example 4 in the STM8 firmware library. 4.2 Hardware description The STM8/128-EVAL evaluation board is used for this example. The main hardware connections are shown in Figure 1. 4.3 Firmware description The CLK registers are configured so that the CPU runs at exactly 24 MHz (HSE clock frequency). Pressing the ‘KEY’ button on the evaluation board makes the CPU run at 16 MHz (HSI clock frequency). This example can be used as a tutorial exercise or as starting point that can be easily modified for use in different applications. 6/8 AN2669 5 Revision history Revision history Table 3. Document revision history Date Revision 07-Apr-2008 1 Initial release 2 Section 2.2: Removed Figure 2: USART and HyperTerminal interface and associated text. Section 2.3: Simplified and removed text. Added Table 1: Joystick control which was previously in Section 3.3. Section 3.3: Removed Table 2: Joystick control and added it to Section 2.3. 25-Sep-2008 Changes 7/8 AN2669 Please Read Carefully: Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any time, without notice. All ST products are sold pursuant to ST’s terms and conditions of sale. Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no liability whatsoever relating to the choice, selection or use of the ST products and services described herein. No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such third party products or services or any intellectual property contained therein. UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT. UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS ARE NOT RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY, DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK. Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any liability of ST. ST and the ST logo are trademarks or registered trademarks of ST in various countries. Information in this document supersedes and replaces all information previously supplied. The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners. © 2008 STMicroelectronics - All rights reserved STMicroelectronics group of companies Australia - Belgium - Brazil - Canada - China - Czech Republic - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan Malaysia - Malta - Morocco - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States of America www.st.com 8/8