ST AN3300 Application note

AN3300

Application note

How to calibrate an STM32L1xx internal RC oscillator

Introduction

The STM32L1xx microcontrollers have two internal RC oscillators that can be selected as the system clock source. These are known as the HSI (High-Speed Internal) and MSI (MultiSpeed Internal) oscillators. The HSI oscillator has a typical frequency of 16 MHz. The MSI oscillator is a lower speed, low-power clock source.

The operating temperature has an impact on the accuracy of the RC oscillators. At 25 °C, the HSI and MSI oscillators have an accuracy of ±1% typically, but in the temperature range of -40 to 105 °C, the accuracy decreases.

To compensate for the influence of temperature on internal RC oscillators accuracy, the STM32L1xx microcontrollers have built-in features to allow you to calibrate the HSI and MSI oscillators and measure the MSI and LSI (Low-Speed Internal) oscillator frequencies.

This application note focuses on how to calibrate internal RC oscillators: HSI and MSI. Two methods are presented: 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 signal.

The measurement of the MSI and LSI oscillators is performed by connecting the oscillator to a timer input capture.

Note: 1 STM32L1xx refers to Ultra Low Power High-density, Medium-density Plus and Mediumdensity devices in this document.

2Ultra Low Power Medium-density devices are STM32L151xx and STM32L152xx microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.

3Ultra Low Power Medium-density Plus devices are STM32L151xx, STM32L152xx and STM32L162xx microcontrollers where the Flash memory density is 256 Kbytes.

4Ultra Low Power High-density devices are STM32L151xx, STM32L152xx and STM32L162xx microcontrollers where the Flash memory density is 384 Kbytes.

January 2012

Doc ID 18148 Rev 2

1/28

www.st.com

Contents

AN3300

 

 

Contents

1

STM32L1xx system clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4

2

Internal RC oscillator calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

 

2.1

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

8

 

2.2

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

9

2.2.1 Case where LSE is used as the reference frequency . . . . . . . . . . . . . . . 9 2.2.2 Case where another source is used as the reference frequency . . . . . . 10

2.3 Description of the internal oscillator calibration firmware . . . . . . . . . . . . . 11

2.3.1 Internal oscillator calibration with minimum error . . . . . . . . . . . . . . . . . . 11 2.3.2 HSI calibration with fixed error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.3.3 MSI calibration with fixed error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.3.4 Internal oscillator frequency measurement . . . . . . . . . . . . . . . . . . . . . . 17

2.4 Recommendations on the use of the calibration library . . . . . . . . . . . . . . 19 2.5 Calibration process performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

2.5.1 Duration of the calibration process . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

3

Internal oscillator measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

21

 

3.1

Measurement principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

21

 

3.2

Description of the internal oscillator measurement firmware . . . . . . . . . .

23

 

3.3

Internal oscillator calibration/measurement demo description . . . . . . . . .

23

4

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

26

5

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

27

2/28

Doc ID 18148 Rev 2

AN3300

List of figures

 

 

List of figures

Figure 1. Simplified clock tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Figure 2. HSI oscillator trimming characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Figure 3. MSI trimming behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Figure 4. Timing diagram of internal oscillator calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Figure 5. Hardware connection using LSE as the reference frequency. . . . . . . . . . . . . . . . . . . . . . . 10 Figure 6. Hardware connection using external reference frequency . . . . . . . . . . . . . . . . . . . . . . . . . 10 Figure 7. Internal oscillator calibration: finding the minimum frequency error . . . . . . . . . . . . . . . . . . 12 Figure 8. “Spring loop” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Figure 9. HSI calibration flowchart: maximum allowed frequency error . . . . . . . . . . . . . . . . . . . . . . . 14 Figure 10. MSI calibration flowchart: maximum allowed frequency error. . . . . . . . . . . . . . . . . . . . . . . 16 Figure 11. Internal oscillator frequency measurement flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Figure 12. MSI measurement configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Figure 13. LSI measurement configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Figure 14. Timing diagram of an internal RC oscillator measurement . . . . . . . . . . . . . . . . . . . . . . . . . 22 Figure 15. HSI calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Figure 16. MSI calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

Doc ID 18148 Rev 2

3/28

STM32L1xx system clock

AN3300

 

 

1 STM32L1xx system clock

The STM32L1xx microcontroller family has various clock sources that can be used to drive the system clock:

16 MHz High-Speed Internal (HSI) RC oscillator clock

1 to 24 MHz High-Speed External (HSE) oscillator clock

65.5 kHz to 4.2 MHz Multi-Speed Internal (MSI) RC oscillator clock

2 to 24 MHz Phase-Locked Loop (PLL) that is clocked by HSI or HSE oscillators

 

The High-Speed Internal (HSI) RC oscillator typically has a frequency of 16 MHz and

 

consumes 100 µA.

 

 

The Multi-Speed Internal (MSI) RC oscillator provides seven frequency ranges: 65.5 kHz,

 

131 kHz, 262 kHz, 524 kHz, 1.05 MHz, 2.1 MHz (default value) and 4.2 MHz. It is designed

 

to operate with a current proportional to the frequency (refer to the product datasheet for

 

more details about MSI power consumption versus selected range), so as to minimize the

 

internal oscillator consumption when the CPU runs at low frequencies. The MSI clock is

 

used as a system clock after restart from Reset, Wake-up from stop, and Standby low power

 

modes.

 

 

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

 

source (no external components required). It also has a faster startup time and lower power

 

consumption than the external oscillator. HSI and MSI oscillators can be calibrated to

 

improve their accuracy. But even with calibration, the internal RC oscillator frequency is less

 

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

 

ppm).

 

Note:

The internal RC oscillators (HSI and MSI) can also be used as a backup clock source

 

(auxiliary clock) if the external oscillator fails.

 

 

Figure 1. Simplified clock tree

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

SW[1:0]

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MSI

 

 

 

 

 

 

 

 

65.5 kHz-4.2 MHz

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

HSI

 

 

 

 

 

 

 

 

16 MHz

 

 

 

 

 

System clock

 

 

 

 

 

 

 

 

 

 

HSE

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1 - 24 MHz

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

PLL

 

 

 

 

 

 

 

 

2 - 24 MHz

 

 

 

 

 

MS18637V1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4/28

Doc ID 18148 Rev 2

AN3300

STM32L1xx system clock

 

 

The STM32L1x devices also have two secondary clock sources (that cannot be used as system clock sources):

37 kHz Low-Speed Internal (LSI) RC which is designed to drive the independent watchdog and optionally the Real Time Clock (RTC). The LSI oscillator cannot be calibrated, but can be measured to evaluate frequency deviations (due to temperature and voltage changes)

32.768 kHz Low-Speed External crystal (LSE crystal) which optionally drives the Real Time Clock (RTC)

Doc ID 18148 Rev 2

5/28

Internal RC oscillator calibration

AN3300

 

 

2 Internal RC oscillator calibration

The frequency of the internal RC oscillators may vary from one chip to another due to manufacturing process variations. For this reason, MSI and HSI RC oscillators are factorycalibrated by ST to have a 1% accuracy at TA = 25 °C. After reset, the factory calibration value is automatically loaded in the internal calibration bits.

The frequency of the internal RC oscillators can be fine-tuned to achieve better accuracy with wider temperature and supply voltage ranges. The trimming bits are used for this purpose.

For the HSI oscillator, the calibration value is loaded in HSICAL[7:0] bits after reset. Five trimming bits HSITRIM[4:0] are used for fine-tuning. The default trimming value is 16. An increase/decrease in this trimming value causes an increase/decrease in HSI frequency. The HSI oscillator is fine-tuned in steps of 0.5% (around 80 kHz).

Writing a trimming value in the range of 17 to 31 increases the HSI frequency.

Writing a trimming value in the range of 0 to 15 decreases the HSI frequency.

Writing a trimming value equal to 16 causes the HSI frequency to keep its default value.

The graph below shows HSI oscillator behavior versus calibration value. The HSI oscillator frequency increases with calibration value (calibration value = default HSICAL[7:0] + HSITRIM[4:0]), except at modulo 16. At these calibration values, the negative steps can reach three times the positive steps.

Figure 2. HSI oscillator trimming characteristics

 

Calibration range

16MHz

 

 

HSI frequency

 

0xXF

 

 

 

0xX 0

 

 

 

Calibration values

HSICALR

HSICALR+16

HSICALR +31

 

 

MS18653V1

6/28

Doc ID 18148 Rev 2

AN3300

Internal RC oscillator calibration

 

 

For the MSI oscillator, the calibration value is loaded in the MSICAL[7:0] bits after reset. Eight trimming bits MSITRIM[7:0] are used giving a wide tuning range. The calibration is based on adding the default MSICAL[7:0] bits (reset value) to the MSITRIM[7:0] bits.

The result is stored in MSICAL[7:0]:

MSICAL[7:0] = default MSICAL[7:0] + MSITRIM[7:0]

Example:

Assuming the default MSI calibration value MSICAL[7:0] is 0x80.

1.Writing a value between 0x01 and 0x7F in MSITRIM[7:0] leads to calibration value MSICAL[7:0] in the range of:

MSICAL[7:0] = 0x80 + 0x01 = 0x81

and MSICAL[7:0] = 0x80 + 0x7F = 0xFF

These results are greater than 0x80 (default MSI[7:0] value) and consequently the MSI frequency is increased by 1 step (0x81 - 0x80) to 127 steps (0xFF - 0x80).

2.Writing a value between 0x81 and 0xFF in MSITRIM[7:0] leads to calibration value MSICAL[7:0] in the range of:

MSICAL[7:0] = 0x80 + 0x81 = 0x01

and MSICAL[7:0] = 0x80 + 0xFF = 0x7F

These results are lower than 0x80 (default MSI[7:0] value) and consequently the MSI frequency is decreased by 1 step (0x01) to 127 steps (0x7F).

3.Writing the default calibration value (0x80) in MSITRIM[7:0] leads to calibration value MSICAL[7:0] equal to MSICAL[7:0] = 0x80 + 0x80 = 0x00 and consequently the MSI frequency is decreased by 128 steps (minimum frequency).

Doc ID 18148 Rev 2

7/28

ST AN3300 Application note

Internal RC oscillator calibration

AN3300

 

 

Figure 3 below shows MSI behavior at range 6 (4194304 Hz) versus MSICAL[7:0].

Figure 3. MSI trimming behavior

 

8

 

 

 

 

7

 

 

0xFF

 

 

 

 

 

6

 

0xBF

 

(MHz)

 

 

 

5

0x7F

 

MSI

 

0xC0

value

4

 

value

 

 

 

 

 

0x3F

0x80

 

 

MSI

 

 

 

3

 

 

 

 

0x40

 

 

 

 

0x00

 

 

 

 

2

 

 

 

 

1

 

 

 

 

0

MSICAL[7:0]

 

 

 

 

Note the negative steps when the two MSB bits change from “00” to “01”, from “01” to “10” and from “10” to “11”. The change of the six LSBs leads to fine-tuning of around 0.5% per step.

2.1Calibration principle

The calibration principle consists in:

1.Setting the internal RC oscillator (that needs to be calibrated) as system clock,

2.Measuring the internal RC oscillator (HSI or MSI) frequency for each trimming value,

3.Computing the frequency error for each trimming value,

4.Finally, setting the trimming bits with the optimum value (corresponding to the lowest frequency error).

The internal oscillator frequency is not measured directly but is computed from the number of clock pulses counted using a timer compared with the typical value. To do this, 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.

8/28

Doc ID 18148 Rev 2

AN3300

Internal RC oscillator calibration

 

 

Figure 4. Timing diagram of internal oscillator calibration

Reference signal

TIM10 counter

65535

0

Counter

Capture

Capture

enabled

interrupt

interrupt

MS18638V1

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

Since the timer counter is clocked by the system clock (internal RC oscillator HSI or MSI), the real frequency generated by the internal RC oscillator versus the reference signal is given by:

Measuredfrequency = ( IC1ReadValue2 IC1ReadValue1) × referencefrequency

The error (in Hz) is computed as the absolute value of the difference between the measured frequency and the typical value.

Hence the internal oscillator frequency error is expressed as:

Error(Hz) = Measuredfrequency typicalvalue

After calculating the error for each trimming value, the algorithm determines the optimum trimming value (that corresponds to the nearest frequency to typical value) to be programmed in the trimming bits (refer to Section 2.3: Description of the internal oscillator calibration firmware for more details).

2.2Hardware implementation

2.2.1Case where LSE is used as the reference frequency

The STM32L1xx offers a useful feature, that is the ability to connect internally the LowSpeed External (LSE) oscillator to Timer 10 channel 1. Thus, the LSE clock can be used as the reference signal for internal oscillator calibration and no additional hardware connections are required. Only the LSE oscillator should be connected to OSC32_IN and OSC32_OUT.

Figure 5 shows the hardware connections needed for internal oscillators calibration using LSE as an accurate frequency source for calibration.

Doc ID 18148 Rev 2

9/28

Loading...
+ 19 hidden pages