ST AN3101 Application note

AN3101

Application note

STM8L15x internal RC oscillator calibration

Introduction

The STM8L15x microcontrollers offer the possibility of using internal RC oscillators HSI (High-speed internal factory trimmed oscillator of 16 MHz, typically) or LSI (Low-speed internal low-consumption oscillator of 38 kHz, typically) as clock sources.

The operating temperature and voltage have an impact on RC accuracy. At 3 V and 25 °C, the HSI has an accuracy of ±1% typically, but in the full temperature and voltage ranges , the accuracy of the HSI frequency decreases to -4.5% / +3%.

To compensate for the influence of temperature and voltage on internal RC oscillator accuracy, the STM8L15x microcontrollers provide the capability of calibrating the HSI clock and of measuring the LSI clock.

This application note gives two methods of calibrating the HSI internal RC oscillator. The first method is based on finding the frequency with the minimum error and the second one consists in finding the maximum allowed frequency error. Both are implemented by providing an accurate reference source.

The measurement of the LSI clock is performed by connecting the LSI oscillator to a timer input capture. Depending on the measured value, peripheral registers are updated to meet user requirements.

This application note applies to STM8L15x High-density and Medium-density devices.

February 2011

Doc ID 16612 Rev 3

1/23

www.st.com

Contents

AN3101

 

 

Contents

1

STM8L15x system clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4

2

High speed internal oscillator calibration . . . . . . . . . . . . . . . . . . . . . . . .

5

 

2.1

Calibration principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

 

2.2

Hardware implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7

2.2.1 Case where LSE is used as the reference frequency . . . . . . . . . . . . . . . 7 2.2.2 Case where another source is used as the reference frequency . . . . . . . 8

2.3 Description of the HSI clock calibration firmware . . . . . . . . . . . . . . . . . . . . 8

2.3.1 HSI calibration with minimum error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.3.2 HSI calibration with fixed error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.3.3 HSI frequency measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.3.4 HSI calibration demo description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

 

2.4

Recommendations on the use of the HSI calibration library . . . . . . . . . .

16

 

2.5

Calibration process performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

16

 

 

2.5.1

Duration of the calibration process . . . . . . . . . . . . . . . . . . . . . . . . . . . .

16

3

Low speed internal oscillator measurement . . . . . . . . . . . . . . . . . . . .

18

 

3.1

LSI measurement principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

18

 

3.2

Description of the LSI clock measurement firmware . . . . . . . . . . . . . . . .

19

 

3.3

LSI measurement demo description . . . . . . . . . . . . . . . . . . . . . . . . . . . .

20

4

Conclusion .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

21

5

Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

22

2/23

Doc ID 16612 Rev 3

AN3101

List of figures

 

 

List of figures

Figure 1. Example of HSI oscillator trimming characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Figure 2. Example of HSI oscillator trimming steps size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Figure 3. Timing diagram of HSI calibration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Figure 4. Hardware connection using LSE as the reference frequency. . . . . . . . . . . . . . . . . . . . . . . . 8 Figure 5. Hardware connection using external reference frequency . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Figure 6. HSI calibration flowchart: finding the minimum frequency error . . . . . . . . . . . . . . . . . . . . . 10 Figure 7. “Spring loop” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Figure 8. HSI calibration flowchart: maximum allowed frequency error . . . . . . . . . . . . . . . . . . . . . . . 13 Figure 9. HSI frequency measurement flowchart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Figure 10. Internal connection between LSI and Timer 2 channel 1 . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Figure 11. LSI measurement configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Figure 12. Timing diagram of LSI measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Figure 13. LSI frequency measurement flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Doc ID 16612 Rev 3

3/23

STM8L15x system clock

AN3101

 

 

 

1

STM8L15x system clock

 

 

The STM8L15x microcontroller family offers different clock sources that can be used to drive

 

the system clock:

 

 

16 MHz high-speed internal (HSI) factory-trimmed RC clock

 

 

1 to 16 MHz high-speed external (HSE) oscillator clock

 

 

32.768 kHz low-speed external (LSE) oscillator clock

 

 

38 kHz low-speed internal (LSI) low-consumption clock

 

 

The internal RC oscillators (HSI and LSI) have the advantage of providing a low-cost clock

 

source (no external components required). It also has a faster startup time than the external

 

oscillator. However, even with calibration, the internal RC oscillator frequency is less

 

accurate than the frequency of an external crystal oscillator or ceramic resonator.

 

Note:

The internal oscillator can also be used as backup source (auxiliary clock) if the external

 

oscillator fails.

 

4/23

Doc ID 16612 Rev 3

AN3101

High speed internal oscillator calibration

 

 

2 High speed internal oscillator calibration

HSI oscillator frequencies may vary from one chip to another due to manufacturing process variations. For this reason, each device is factory-calibrated by ST to have a 1% accuracy at TA = 25 °C. After reset, the factory calibration value is automatically loaded in an internal calibration HSICALR register.

The frequency of the HSI RC oscillator can be trimmed to achieve a better accuracy with wider temperature and supply voltage ranges. The HSITRIMR register is used for this purpose.

To modify the content of the HSITRMR register the access has to be granted by successfully unlocking the write protection mechanism which consists of two consecutive write accesses to the HSIUNLCKR register, the first one with the value 0xAC and the second one with the value 0x35. This must be immediately followed by a write access to the HSITRIM register.

Once this register is configured, its value is used instead of the HSICALR register values.

The HSI oscillator frequency increases monotonically with the increasing values in the HSITRIMR register as long as the trimming value changes in the low nibble (4-bits). Any change to the HSITRIMR value in the high nibble can cause a non-monotonicity in the graph of the trimming function resulting in a sawtooth shape. Refer to the following figure.

Figure 1. Example of HSI oscillator trimming characteristics

 

Calibration range

frequency

16 MHz

 

 

 

 

 

HSI

 

 

0xXF

 

 

 

 

0xX0

 

 

HSICALR-13

HSICALR

HSICALR+16

HSITRIMR value

The maximum HSI frequency variation due to the temperature and/or voltage drift is 16 MHz + 3% = 16 MHz + 480 kHz. Please, refer to the datasheet.

13 trimming steps (each of about 64 kHz) are required to compensate for this variation and the lower trimming threshold is therefore required to be HSICALR - 13.

The minimum HSI frequency variation due to the temperature and/or voltage drift is 16 MHz - 4.5% = 16 MHz - 720 kHz. To be able to compensate for this variation, 16 trimming steps (each of about 64 kHz) are required to compensate for this variation and the upper trimming threshold must be HSICALR + 16.

Doc ID 16612 Rev 3

5/23

ST AN3101 Application note

High speed internal oscillator calibration

AN3101

 

 

The frequency step size for two consecutive HSITRIMR values is not constant. It is typically 0.4% (max. 0.7%) for the monotonous range (change in the low nibble only). The frequency step size for two consecutive HSITRIMR values which differ in the high nibble (i.e. from 0x3F to 0x40) can be up to +/- 1.5%. See the Figure 2 and the datasheet for details.

Figure 2. Example of HSI oscillator trimming steps size

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(3)42)-2 VALUE

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Note:

This graph is given for guidance only. It is based on a device whose calibration value is

 

equal to 90.

2.1 Calibration principle

The calibration principle consists in

first measuring the HSI frequency for many calibration values,

computing the frequency error for each calibration value,

and finally setting the HSITRIMR register with the optimum value.

The HSI frequency is not measured directly but it is estimated from the number of counted HSI clock pulses using a timer compared with the typical value (16 MHz). To do so, a very accurate reference frequency must be available such as the LSE frequency provided by the external 32.768 kHz crystal or the 50 Hz/60 Hz of the mains (refer to Section 2.2.2: Case where another source is used as the reference frequency).

The following figure shows how the reference signal period is measured in number of timer counts.

6/23

Doc ID 16612 Rev 3

AN3101

 

 

High speed internal oscillator calibration

 

 

 

 

 

 

 

 

 

 

 

Figure 3. Timing diagram of HSI calibration

 

 

 

 

 

 

 

 

 

 

2EFERENCE

 

 

 

 

 

 

 

 

SIGNAL

 

 

 

 

 

 

 

 

4)-

 

 

 

 

 

 

 

 

TIME

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

COUNTER

 

 

 

 

 

 

 

 

 

 

X&&&&

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

X

 

 

 

 

 

 

 

 

TIME

 

#OUNTERN

 

 

 

 

 

 

 

&IRSTR

3ECOND

 

ENABLED

CAPTURETEVENT

CAPTURETEVENT

 

 

 

 

 

 

 

 

 

 

AI

 

 

 

 

 

 

 

 

 

 

 

After enabling the timer counter, when the first rising edge occurs, the timer counter value is registered in IC1ReadValue1. At the second rising edge, the timer counter is captured in IC1ReadValue2. The elapsed time between two consecutive rising edges (IC1ReadValue2 and IC1ReadValue1) represents an entire period of the reference signal.

Since the timer is clocked by the internal HSI oscillator, the microcontroller can compute the real frequency generated by the HSI versus the reference signal

(frequencyRef).

FrequencyHSI = ( IC1ReadValue2 IC1ReadValue1) × frequencyREF

The error (in Hz) is computed as the absolute value of the difference between the measured HSI frequency (FrequencyHSI) and 16 000 000 Hz.

Hence the HSI frequency error is expressed as:

Error(Hz) = frequencyHSI 16000000

After calculating the error for each calibration value, the algorithm determines the optimum calibration value (the nearest value to 16 MHz) to be programmed in the HSITRIMR register (refer to Section 2.3 for more details).

2.2Hardware implementation

2.2.1Case where LSE is used as the reference frequency

The STM8L15x offers a very useful feature: the ability to connect low speed oscillators (LSI or LSE) to Timer 2 channel 1. Thus, the LSE clock can be used as the reference signal for HSI calibration and no additional hardware connections are required. Only the LSE oscillator should be connected to OSC32_IN and OSC32_OUT.

Figure 4 shows the hardware connections needed for HSI calibration using LSE as an accurate frequency source for calibration.

Doc ID 16612 Rev 3

7/23

Loading...
+ 16 hidden pages