January 2012 Doc ID 18148 Rev 2 1/28
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 (Multi-
Speed 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 Medium-
density devices in this document.
2 Ultra Low Power Medium-density devices are STM32L151xx and STM32L152xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
3 Ultra Low Power Medium-density Plus devices are STM32L151xx, STM32L152xx and
STM32L162xx microcontrollers where the Flash memory density is 256 Kbytes.
4 Ultra Low Power High-density devices are STM32L151xx, STM32L152xx and
STM32L162xx microcontrollers where the Flash memory density is 384 Kbytes.
www.st.com
Contents AN3300
2/28 Doc ID 18148 Rev 2
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