AN2868
Application note
STM32F10xxx internal RC oscillator (HSI) calibration
Introduction
The STM32F10xxx microcontrollers offer the possibility of running from an internal RC oscillator (HSI: high-speed internal oscillator of 8 MHz, typically). At 25 °C, the HSI has an accuracy of ±1% typically. In the range of –40 to 105 °C, the accuracy value of the RC frequency increases to the maximum value of ±3%. Temperature therefore has an impact on RC accuracy.
To compensate for the influence of temperature in the application, the output frequency of the STM32F10xxx HSI oscillator can be further trimmed by the user runtime calibration routine to improve the HSI frequency accuracy. This may prove crucial for communication peripherals.
This application note gives two methods of calibrating the internal RC oscillator: finding the frequency with the minimum error or finding the maximum allowed frequency error. Both are implemented by providing an accurate reference source such as an RTC/64 signal or a mains source signal.
Both methods are based on the same technique: computing of the RC frequency vs. the reference frequency, computing of the HSI frequency error and setting of the HSITRIM bits in the RCC_CR register.
February 2009 |
Rev 1 |
1/22 |
www.st.com
Contents |
AN2868 |
|
|
Contents
1 |
STM32F10xxx’s internal clock: HSI clock . . . . . . . . . . . . . . . . . . . . . . . . |
5 |
|
|
1.1 |
Calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
5 |
2 |
RC calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
6 |
|
|
2.1 |
Principle of calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
6 |
|
2.2 |
Hardware implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
7 |
2.2.1 Case where RTC/64 is used as the reference frequency: 512 Hz . . . . . . 7
2.2.2Case where the mains frequency is used as the reference frequency:
|
|
50 Hz/60 Hz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
. 7 |
3 |
Description of the RC calibration library . . . . . . . . . . . . . . . . . . . . . . . . |
9 |
|
|
3.1 |
HSI_FreqMeasure() function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
9 |
|
3.2 |
HSI_CalibrateMinError() function . . . . . . . . . . . . . . . . . . . . . . . . . |
11 |
|
3.3 |
HSI_CalibrateFixedError() function . . . . . . . . . . . . . . . . . . . . . . . |
13 |
|
3.4 |
Calibration demo description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
16 |
|
3.5 |
Recommendations on the use of the HSI calibration library . . . . . . . . . . |
17 |
4 |
Calibration process performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
18 |
4.1 Accuracy of frequency measurements . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 4.2 Duration of the calibration process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5 |
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
20 |
6 |
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
21 |
2/22
AN2868 |
List of tables |
|
|
List of tables
Table 1. Component values when using the mains frequency as the reference . . . . . . . . . . . . . . . . 8 Table 2. RC frequency accuracy vs. reference frequency accuracy . . . . . . . . . . . . . . . . . . . . . . . . 18 Table 3. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3/22
List of figures |
AN2868 |
|
|
List of figures
Figure 1. Quantification of the reference signal period (RTC signal) . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Figure 2. Hardware connection using RTC/64 as a source for calibration. . . . . . . . . . . . . . . . . . . . . . 7 Figure 3. Hardware connection in the AC mains calibration method . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Figure 4. RC frequency measurement flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Figure 5. RC calibration flowchart: finding the minimum frequency error . . . . . . . . . . . . . . . . . . . . . 12 Figure 6. “Spring loop” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Figure 7. RC calibration flowchart: calibration with the maximum allowed frequency error . . . . . . . . 15
4/22
AN2868 |
STM32F10xxx’s internal clock: HSI clock |
|
|
The HSI clock signal is generated from an internal 8 MHz RC oscillator and can be used directly as a system clock or divided by 2 to be used as a PLL input. The HSI RC oscillator has the advantage of providing a clock source at low cost (no external components). It also has a faster startup time than the HSE crystal oscillator. However, even with calibration the frequency is less accurate than an external crystal oscillator or ceramic resonator. The HSI signal can also be used as a backup source (auxiliary clock) if the HSE crystal oscillator fails.
RC oscillator frequencies may vary from one chip to another due to manufacturing process variations. For this reason, each device is factory-calibrated by ST for 1% accuracy at TA = 25 °C.
After reset, the factory calibration value is loaded into the HSICAL[7:0] bits in the clock control register RCC_CR.
User calibration is performed by setting the HSITRIM[4:0] bits in the RCC_CR register. These bits can be programmed to take into account voltage and temperature variations that affect the frequency of the internal HSI RC oscillator. The default value is 16, which, when
added to the HSICAL value, should trim the HSI to 8 MHz ±1%. The trimming step (Fhsitrim) is around 40 kHz between two consecutive HSICAL steps.
5/22
RC calibration |
AN2868 |
|
|
The principle of calibration consists in first measuring the HSI frequency, then computing the frequency error, and finally setting the HSITRIM bits in the RCC_CR register.
The HSI frequency is not measured directly, but it is estimated from the number of HSI clock pulses counted using a timer, and compared to an ideal value: 8 000 000 Hz. To do so, a very accurate reference frequency must be available such as the RTC/64 signal provided by the external 32 kHz crystal or the 50 Hz/60 Hz of the mains (refer to Section 2.2.2). In the case of an RTC clock source, the reference frequency is equal to 512 Hz (32768 Hz/64).
Figure 1 shows how the reference signal period is measured in number of timer counts.
Reference |
1/FrequencyRef |
signal |
TIMx |
Only Update event interrupt |
Capture 1 value |
|
counter |
|||
|
|
||
0xFFFF |
|
|
|
0x0 |
(N –1) × 65535 |
|
|
|
|
Capture compare 1 interrupt on rising edge + update event interrupt
Capture compare 1 interrupt on rising edge + update event interrupt
N: number of counter overflows |
ai15840 |
On each rising edge, two interrupts occur: capture compare 1 interrupt and update event interrupt. The latter is used to count the number of counter overflows over a reference signal period. Since both interrupts occur at the same time at the beginning of every new period, an extra overflow occurs. This is the reason why we have to subtract 1 from the number of counter overflows: N – 1.
Thus the number of counted HSI clock pulses is given as follows:
TimerPeriodCount = ( N – 1) × 65535 + Capture1 , where:
–N is the number of timer overflows during one period of the reference frequency
–Capture1 is the value read from the timer CCR1 register.
Since the timer is clocked by the internal RC, the microcontroller can compute the real frequency generated by the HSI versus the reference frequency.
FrequencyRC = TimerPeriodCount × FrequencyRef
The error (in Hz) is computed as the absolute value of the difference between the RC frequency (FrequencyRC) and 8 000 000 Hz.
Hence the RC frequency error is expressed as:
Error( Hz) = FrequencyRC – 8000000 .
After calculating the error, the algorithm determines the calibration value that has to be written to the HSITRIM bits in the RCC_CR register (refer to Section 3 for more details).
6/22
AN2868 |
RC calibration |
|
|
|
The STM32F10xxx can output the RTC signal divided by 64 onto GPIO PC13 (TAMPER- |
|||||||||||||||
|
RTC pin). The TAMPER-RTC signal can be used as the reference frequency for RC |
|||||||||||||||
|
calibration. For that purpose, this pin must be configured as alternate function push-pull and |
|||||||||||||||
|
be connected to a timer channel input. |
|||||||||||||||
Note: |
In the rest of this application note, the channel used will be timer 3 channel 1 (TIM3_CH1). |
|||||||||||||||
|
Figure 2 shows the hardware connections needed for RC calibration using RTC/64 as an |
|||||||||||||||
|
accurate frequency source for calibration. |
|||||||||||||||
|
Figure 2. Hardware connection using RTC/64 as a source for calibration |
|||||||||||||||
|
|
|
|
|
|
|
C1 |
|||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OSC32_IN |
|
|
|
|
|
|
|
|
|
|
|
|
|
32.768 kHz |
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OSC32_OUT |
|
|
|
|
|
|
|
|
|
||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||||
|
|
|
|
|
|
|
|
C2 |
||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
STM32F10xxx |
|
Reference signal |
|||||||||||||||
|
|
|
|
|
|
|
|
|
|
32768 Hz/64 |
||||||
|
|
|
|
|
|
|
|
|
||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TAMPER-RTC/PC13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 /512 Hz |
|
|
|
|
|||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TIM3_CH1 (PA6) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ai15842 |
This section describes the hardware requirements of the method that uses the AC mains frequency as a reference. Figure 3 illustrates the circuit implemented to provide the DC power supply (approximately 3.3 V) to the microcontroller:
The only component required to protect the timer input is a resistor. So if no power supply is needed, then only a resistor is needed on the TIM3_CH1 input, to protect the timer input from overcurrents.
This circuit includes a few passive components to convert the 220 V/50 Hz mains of the EU, or the 110 V/60 Hz mains of the US, to a 3.3 V DC power supply. For applications that have higher current consumptions, a power converter could be used (refer to the application note AN1357: VIPower: low cost power supplies using VIPer12A in non isolated applications).
Caution: The reference signal conditioning and power supply circuits cannot be used if there are big current variations.
In case of noisy mains, it is recommended to use an input power line filter (refer to the application note AN2326: Calibrating the RC oscillator of the ST7ULTRALITE MCU using the mains).
7/22