STM STM8 STM8L151C4 Learning Centre MCU Application Notes STMicroelectronics - Application - Notes - 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

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 has an impact on RC accuracy. At 25 C the HSI has an
accuracy of 1% typically but in the temperature range of -40 to 105 C, the accuracy of the
HSI frequency decreases to -4.5% / +3%.
To compensate for the influence of temperature 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.

September 2010

Doc ID 16612 Rev 2

1/21
www.st.com

Contents

AN3101

Contents
1

STM8L15x sytem clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

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


2.1

Calibration principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

2.2

Hardware implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

2.3

2.2.1

Case where LSE is used as the reference frequency . . . . . . . . . . . . . . . 6

2.2.2

Case where another source is used as the reference frequency . . . . . . . 6

Description of the HSI clock calibration firmware . . . . . . . . . . . . . . . . . . . . 7


2.3.1

HSI calibration with minimum error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

2.3.2

HSI calibration with fixed error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2.3.3

HSI frequency measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

2.3.4

HSI calibration demo description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

2.4

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

2.5

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


2.5.1

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

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


3.1

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

3.2

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

3.3

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

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

2/21

Doc ID 16612 Rev 2

AN3101

List of figures

List of figures
Figure 1.
Figure 2.
Figure 3.
Figure 4.
Figure 5.
Figure 6.
Figure 7.
Figure 8.
Figure 9.
Figure 10.
Figure 11.

Timing diagram of HSI calibration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5


Hardware connection using LSE as the reference frequency. . . . . . . . . . . . . . . . . . . . . . . . 6
Hardware connection using external reference frequency . . . . . . . . . . . . . . . . . . . . . . . . . . 7
HSI calibration flowchart: finding the minimum frequency error . . . . . . . . . . . . . . . . . . . . . . 8
Spring loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
HSI calibration flowchart: maximum allowed frequency error . . . . . . . . . . . . . . . . . . . . . . . 11
HSI frequency measurement flowchart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Internal connection between LSI and Timer 2 channel 1 . . . . . . . . . . . . . . . . . . . . . . . . . . 16
LSI measurement configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Timing diagram of LSI measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
LSI frequency measurement flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

Doc ID 16612 Rev 2

3/21

STM8L15x sytem clock

AN3101

STM8L15x sytem 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:

4/21

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

Doc ID 16612 Rev 2

AN3101

High speed internal oscillator calibration

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. Depending on your device, the HSI oscillator accuracy may vary from -4.5% to
+3% and the typical HSI trimming resolution is about 0.4% (64 kHz). Refer to the
datasheet.
The maximum HSI value is 16 MHz + 3% = 16 MHz + 480 kHz. Consequently, 8 trimming
steps (480 kHz / 64 kHz) are required to compensate for this variation and the upper
trimming threshold is HSICALR + 8.
The minimum HSI value is 16 MHz - 4.5% = 16 MHz - 720 kHz. Consequently, 12 trimming
steps (720 kHz / 64 kHz) are required to compensate for this variation and the lower
trimming threshold is HSICALR - 12.

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.
Figure 1.

Timing diagram of HSI calibration


2EFERENCE
SIGNAL
TIME
4)-
COUNTER
X&&&&

X

TIME

#OUNTER &IRST
ENABLED CAPTUREEVENT

3ECOND
CAPTUREEVENT
AI

Doc ID 16612 Rev 2

5/21

High speed internal oscillator calibration

AN3101

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).
Frequency HSI = IC1ReadValue2 IC1ReadValue1 frequency REF

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 = frequency HSI 16000000

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

2.2

Hardware implementation

2.2.1

Case 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 2 shows the hardware connections needed for HSI calibration using LSE as an
accurate frequency source for calibration.
Figure 2.

Hardware connection using LSE as the reference frequency

#

/3#?).
-32

-4&04$
L);
/3#?/54
#

TO 4IMERCHANNEL

,3)
2#

#,+"%%03%,;=

34-,X
AI

2.2.2

Case where another source is used as the reference frequency


Any signal with accurate frequency can be used for HSI calibration and the mains frequency
is one of the possibilities.

6/21

Doc ID 16612 Rev 2

AN3101
Note:

High speed internal oscillator calibration


Refer to the AN2868 application note (STM32F10xx internal RC oscillator (HSI)
calibration) for more details about using mains frequency for calibration.
As shown in the following figure, the reference frequency must be connected to Timer 2
channel 1 (PB0 pin).
Figure 3.

Hardware connection using external reference frequency


2EFERENCE
SIGNAL
4IMERCHANNEL0"

34-,X

AI

2.3

Description of the HSI clock calibration firmware


The HSI clock calibration firmware provided with this application note includes two major
functions:

2.3.1

uint32_t HSI_CalibrateMinError(void)

ErrorStatus HSI_CalibrateFixedError(uint32_t MaxAllowedError,


uint32_t* Freq)

HSI calibration with minimum error


The HSI_CalibrateMinError() function calibrates the HSI clock to have the nearest frequency
to 16 MHz. It measures 21 frequencies for different calibration values and provides the
HSITRIM value that corresponds to the frequency with the minimum error. The so-obtained
HSITRIM value is the calibration value that is written in the HSITRIMR register.
The frequency measurement starts from HSITRIMR = HSICALR - 12 and ends with
HSITRIMR = HSICALR + 8.
After calibration, the HSI_CalibrateMinError() function returns the HSI frequency
value as an unsigned 32-bit integer (uint32_t). This value can be helpful to reconfigure
prescalers, like the ones used for communication peripherals.
The flowchart in Figure 4 gives the algorithm for this function.

Example
uint32_t HSIFrequencyAfterCalib = HSI_VALUE;
{
......
/* Calibrate HSI clock and return its value after calibration
HSIFrequencyAfterCalib = HSI_CalibrateMinError();
.........
}

Doc ID 16612 Rev 2

*/

7/21

High speed internal oscillator calibration


Figure 4.

AN3101

HSI calibration flowchart: finding the minimum frequency error


3TARTOFCALIBRATION

3AVEUSERCLOCKCONFIGURATION
3ETSYSTEMCONFIGURATION
FOR (3)

CALIBRATION

PROCESS

#ALIBRATIONVALUE
(3)#!,2 

3ET(3)42)-2REGISTER
WITH
CALIBRATION
VALUE

FREQUENCY
7AITFORTHECURRENT

MEASUREMENT



#OMPUTETHECURRENTFREQUENCY

ERROR #URRENT%RROR


-INIMUM%RROR

 #URRENT%RROR

.O

9ES
5PDATEMINIMUMFREQUENCYERROR
ANDOPTIMUMCALIBRATIONVALUE

)NCREMENTTRIMMINGINDEX

9ES

4RIMMING INDEX
.O

3ET(3)42)-2
REGISTERWITH
OPTIMUMCALIBRATION

VALUE
2ESTOREUSERCLOCK
CONFIGURATION
2ETURN&REQUENCY!FTER#ALIB

%ND OF CALIBRATION


AIB

1. Frequency measurement is described in Section 2.3.3: HSI frequency measurement on page 12.

2.3.2

HSI calibration with fixed error


The HSI_CalibrateFixedError() function is provided to calibrate the HSI clock with a
maximum allowed frequency error. It is configured by the user as an absolute value given in
Hertz (the first parameter: MaxAllowedError). This function is the same as

8/21

Doc ID 16612 Rev 2

AN3101

High speed internal oscillator calibration


HSI_CalibrateMinError() (refer to Section 2.3.1.), but it searches for the frequency
that has an error (in absolute value) less than or equal to MaxAllowedError.

If it finds this frequency, it stops searching and configures the HSITRIMR register
according to this frequency and returns SUCCESS, meaning that the calibration
operation has succeeded.

Otherwise, it continues searching for it until the HSITRIM bits = HSICALR +8 (21st
frequency). It then sets the HSITRIMR register to the default calibration value and
returns ERROR, meaning that the calibration has failed and did not find any frequency
with an error less than or equal to MaxAllowedError.
The frequency measurements start with HSTRIM = HSICALR -2 (unlike in the
HSI_CalibrateMinError() function where frequency measurements start from
HSICALR - 12 to end with HSICALR + 8). The HSITRIM value is computed in loops to
find the next value. That is, the HSITRIM value starts from HSICALR -2, then goes to
the next value to the left: HSICALR - 3, then to the next to the right: HSICALR - 1, then
to the second to the left and so on until it reaches HSICALR +8, forming a spring loop
(as shown in Figure 5).
This algorithm is based on the fact that the probability of finding the frequency that has
the minimum error increases when the HSITRIM bit value tends to HSICALR. This
algorithm is implemented so as to minimize the time consumed by the calibration
process.

Figure 5.

Spring loop

(3)42)-VALUE
EACHLOOP

(3)#!,2 

(3)#!,2 

(3)#!,2 

(3)#!,2 

(3)#!,2 

AIB

The second parameter is used to get the frequency (in Hertz) after calibration in the form of
an unsigned 32-bit integer (unit32_t).
The flowchart in Figure 6 gives the algorithm for this function.

Doc ID 16612 Rev 2

9/21

High speed internal oscillator calibration

AN3101

Example
uint32_t CurrentHSIFrequency = 0;
ErrorStatus CalibStatus = ERROR;
......
{
......
/* Calibrate HSI with the maximum allowed error in Hz */
/* Set the maximum value of the error frequency at
+/- 60 000 Hz -> 0.375 % */
CalibStatus = HSI_CalibrateFixedError(60000, &CurrentHSIFrequency);
if(CalibStatus != ERROR)
{
STM_EVAL_LEDOn(LED1);
LCD_SetCursorPos(LCD_LINE2, 0);
LCD_Print("
SUCCESS
");
}
else
{
STM_EVAL_LEDOn(LED3);
LCD_SetCursorPos(LCD_LINE2, 0);
LCD_Print("
FAIL
");
}
......
}

10/21

Doc ID 16612 Rev 2

AN3101

High speed internal oscillator calibration


Figure 6.

HSI calibration flowchart: maximum allowed frequency error


3TARTOFCALIBRATION
3AVEUSERCLOCK CONFIGURATION
3ETSYSTEMCONFIGURATION
FOR(3)CALIBRATIONPROCESS
#ALIBRATIONVALUE(3)#!,2 

3ET(3)42)-2REGISTER

WITHCALIBRATIONVALUE

3TART4)-COUNTER
%NABLE4)-INTERRUPTS
7AITFORTHECURRENTFREQUENCY

MEASUREMENT




#OMPUTETHECURRENTFREQUENCY

%RROR

#URRENT%RROR

.O

#URRENT%RROR
-AX!LLOWED%RROR

#OMPUTETHENEXTCALIBRATION
VALUE

9ES

9ES
4RIMMING INDEX

.O
#ALIBRATIONSUCCESSFUL

#ALIBRATIONFAILED

3ET(3)42)-2REGISTER
WITH
DEFAULTCALIBRATION VALUE

2ESTOREUSERCLOCKS
CONFIGURATION

2ETURNTHECALIBRATIONSTATUS
%NDOFCALIBRATION
AIB

1. Frequency measurement is described in Section 2.3.3: HSI frequency measurement on page 12.

Doc ID 16612 Rev 2

11/21

High speed internal oscillator calibration

2.3.3

AN3101

HSI frequency measurement


HSI frequency measurement is performed by Timer2 capture interrupt. In the timer capture
event interrupt, an entire period of HSI frequency is computed. The number of periods to be
measured for each trimming value is configurable by the user in the hsi_calibration.h file as
follows:
#define HSI_PERIOD_NUMBERS 10 /* Number of periods to be measured =
10 */
The averaging method is used to minimize frequency error measurements. So, if the number
of periods reaches HSI_PERIOD_NUMBERS, the average of all measured frequencies is
computed.
You can easily configure the frequency of the reference source. It is defined in the
hsi_calibration.h file as follows:
If the LSE clock is used as the reference frequency, uncomment the line below to make sure
the LSE is configured and internally connected to Timer 2 channel 1:
#define USE_REFERENCE_LSE
If the reference frequency is a mains source frequency equal to 50 Hz, then comment the
line above and define the reference frequency as shown below:
#define REFERENCE_FREQUENCY 50 /* The reference frequency value in
hertz */
The computation of the frequency measurements does not depend on the duty cycle of the
source reference signal. It depends on its frequency since the capture 1 interrupt is
configured to occur on every rising edge of the reference signal (refer to Figure 1).

Note:

12/21

Figure 7 provides the frequency measurement algorithm.

Doc ID 16612 Rev 2

AN3101

High speed internal oscillator calibration


Figure 7.

HSI frequency measurement flowchart


3TARTOF(3)FREQUENCYMEASUREMENT

3ETCAPTURESTATE
TOSTART

%NABLECAPTUREINTERRUPT
%NABLE4)-COUNTER
)NCREMENTTHENUMBEROF
COUNTEDPERIODS
7AITFORCAPTURESTATE
TOBECOMPLETED

TIA

#OMPUTETHECURRENT(3)
FREQUENCYVALUE

#OUNTEDPERIODS


&4

(3)?0%2)/$?.5-"%23

EN

#OMPUTETHEAVERAGEOF(3)
FREQUENCYVALUES

%NTER4)-CAPTUREINTERRUPT

ON

2!

FID

%NDOF(3)FREQUENCYMEASUREMENT

#APTURESTARTED

#APTUREINPROGRESS

#APTURESTATE

3AVECAPTUREDVALUE
IN)#2EAD6ALUE

3AVECAPTUREDVALUE
IN)#2EAD6ALUE

$ISABLECAPTUREINTERRUPT
3TOPTHE4)-COUNTER

3ETCAPTURESTATE
TOINPROGRESS

#OMPUTETHEENTIREPERIOD
CAPTUREVALUE

3ETCAPTURESTATE
TOCOMPLETED

%XITINTERRUPT

AI

2.3.4

HSI calibration demo description


The demo provided with this application note shows the ability of the firmware to calibrate
the HSI oscillator of the STM8L15x microcontroller.
By default, the demo is configured to run the calibration of the HSI oscillator routine using
the LSE source frequency.

Doc ID 16612 Rev 2

13/21

High speed internal oscillator calibration

AN3101

To run the calibration process that provides the frequency with minimum error, you have to
comment the following define line in the main.c file. Conversely, to run the calibration
process that finds the frequency with a maximum allowed error, you have to uncomment the
same line:
/* #define USE_HSI_FIXED_ERROR */
To display the HSI signal, connect an oscilloscope probe to the PC4 pin and uncomment the
following line in the main.c file:
/* #define OUTPUT_HSI_ON_CCO_FOR_DEBUG */

2.4

Note:

Recommendations on the use of the HSI calibration library

1.

If the reference frequency used for HSI calibration is lower than 250 Hz, the TIM2
counter prescaler can be used to support low reference frequencies.

2.

The TIM2 input capture divider (1, 2, 4 and 8) can be used to support high reference
frequencies.

3.

Frequency measurement accuracy is not guaranteed when using a reference


frequency that exceeds 240 kHz.

4.

It is not recommended to call calibration functions in an interrupt routine since, in this


case, the calibration process may be long (refer to the Section 2.5.1).

5.

It is recommended to stop all application activities before the calibration process, and to
restart them after calling the calibration functions. The application therefore has to stop
communications, ADC measurements (see Note: 1) etc. since these processes are
supposed to use clock configurations that are different from those used in the
calibration process. Otherwise, errors might be introduced in the application: errors
while reading/sending frames, ADC reading errors since the sampling time has
changed, etc.

Except when using the ADC for the calibration process (refer to 7.).
6.

The HSI calibration firmware uses the following peripherals: Clock (for trimming HSI),
Timer 2 (for measuring HSI), Beeper (for connecting LSE to Timer 2 if LSE method is
used). So, it is recommanded to reconfigure these peripherals (if used in the
application) after running HSI calibration routine

7.

Real-time calibration vs. temperature can be used when the ambient temperature
changes noticeably while the application is running. The internal temperature sensor
can be used with the ADC watchdog with two thresholds. Each time an ADC watchdog
interrupt occurs, a new HSI calibration process has to be performed and the two
thresholds are updated according to the current temperature (this feature is not
implemented in this application note):
Threshold_High = CurrentTemperatureValue + TemperatureOffset
Threshold_Low = CurrentTemperatureValue TemperatureOffset

2.5

Calibration process performance

2.5.1

Duration of the calibration process


The duration of the calibration process depends on:

14/21

Doc ID 16612 Rev 2

AN3101

High speed internal oscillator calibration


1.

The used reference frequency

2.

The number of measured periods per frequency

3.

The number of measured frequencies during the calibration process

Once peripherals are configured and ready, the duration of the calibration process is
approximated by:
NPeriod + 1 NFreq
CalibDuration = 2x ------------------------------------------------------------------f REF

Where:

NPeriod is the number of times the frequency is measured for each HSITRIM
configuration (the same frequency)

NFreq is the number of measured frequencies (number of HSITRIM values used


for the frequency measurement)

fREF is the frequency value (in Hz) of the reference signal (after the input capture
divider)

In the case of the calibration process with a minimum frequency error


(HSI_CalibrateMinError()) the number of NFreq is equal to 21. If the LSE oscillator is
used as the reference frequency (fREF = LSE value / input capture divider = 32768/8 =
4096 Hz) and the selected number of measured periods configured by the user is 10, the
calibration lasts around 112 ms.
11 21
CalibDuration = 2 ------------------- = 112ms
4096

In the case of the calibration process with a maximum allowed error


(HSI_CalibrateFixedError()), NFreq changes from chip to chip. NFreq also depends
on the maximum allowed error that is selected. The higher the selected allowed error, the
more NFreq will tend to 1. The lower the selected allowed error, the more NFreq will tend to
21.
So, the duration of the calibration process with a maximum allowed error is lower than or
equal to the duration of calibration when using the minimum frequency error process.
Note:

The factor 2 in the CalibDuration formula above is due to the fact that there is no
synchronization between reference signal and timer start counting.

Doc ID 16612 Rev 2

15/21

Low speed internal oscillator measurement

AN3101

Low speed internal oscillator measurement


The low-speed internal oscillator (LSI) is a low power clock source that can be kept running
in Active-halt and Run modes for the independent watchdog (IWDG). The LSI oscillator can
be switched on and off using the LSION bit in the internal clock register (CLK_ICKCR). The
LSI clock is an RC oscillator that can vary, due to environment temperature and supply voltage, from 26 kHz to 56 kHz. This is the reason why a measurement of its real value is
required to avoid an unexpected behavior due to LSI frequency variation. In the STM8L15x
microcontroller family, an internal connection between Timer 2 channel 1 and the LSI clock
is provided to simplify the LSI measurement procedure.
Figure 8.

Internal connection between LSI and Timer 2 channel 1


-32
,3)2#
K (Z

TO4IMERCHANNEL
,3%/3#
 K(Z

#,+"%%03%,;=
AI

3.1

LSI measurement principle


The low-speed internal oscillator (LSI) measurement procedure consists in running the
TIM2 counter using the HSI clock, configuring the Timer 2 channel 1 in input capture mode
and then connecting the LSI clock to Timer 2 channel 1. The following figure shows the
configuration used to perform the LSI measurement.
Figure 9.

LSI measurement configuration

(3)2#-(Z

-32
,3)2#
K (Z
,3%/3#
 K(Z

4)-CHANNEL

)NPUTFILTER

ANDPRESCALER

#,+"%%03%,;=

4)-COUNTER

4)-
CAPTURECOMPARE CAPTUREEVENT

AI

After enabling the timer counter, when the first rising edge occurs, the timer counter value is
registered in IC1ReadValue1. On the second rising edge, the timer counter is captured in
IC1ReadValue2. The elapsed time between two consecutive rising edges of the LSI clock
represents an entire period. The following figure shows the timing diagram of LSI
measurement.

16/21

Doc ID 16612 Rev 2

AN3101

Low speed internal oscillator measurement


Figure 10. Timing diagram of LSI measurement
,3)
SIGNAL
TIME
4)-
COUNTER
X&&&&

X

TIME

#OUNTER &IRST
ENABLED CAPTUREEVENT

3ECOND
CAPTUREEVENT
AI

The LSI frequency value is computed as shown by the following formula:


LSI_Frequency = HSI_Value / Capture
where:

HSI_Value is the HSI frequency value: typical value is 16 MHz

Capture represents an entire LSI period: IC1ReadValue2 - IC1ReadValue1

As you can conclude from the formula above, the LSI measurement accuracy depends on
the HSI frequency accuracy. Consequently, if an external reference signal is available, the
user can run the HSI calibration routine described in Section 2 before performing the LSI
measurement procedure in order to get a more accurate HSI_Value.
The Input capture divider (or prescaler) can be used for better measurement accuracy. In
this case, the formula above becomes: LSI_Frequency = InputCaptureDivider * HSI_Value /
Capture_Value.

3.2

Description of the LSI clock measurement firmware


The LSI clock measurement firmware provided with this application note includes one major
function: LSI_FreqMeasure(void).
The LSI_FreqMeasure() function measures the LSI frequency value. It measures a
predefined number of LSI periods. Then it returns the average value to minimize the error of
measured frequency. The user can change this parameter (number of LSI periods) in the
lsi_measurement.h file.
#define LSI_PERIOD_NUMBERS 10
By default, the number of measured periods per frequency is set to 10.
To start the LSI measurement, save the user clock configuration and set the system
configuration for LSI measurement (select the HSI clock as system clock, configure Timer 2
channel 1 in input capture mode, connect LSI to Timer 2 channel 1, ..).
After system configuration, Timer 2 counter and capture 1 interrupt are enabled. This makes
it possible to save the captured values for the predefined number of LSI periods. Once the
measurement is performed, the average value is computed and returned after restoring the
system clock configuration. Figure 11. gives the flowchart of the LSI measurement.

Doc ID 16612 Rev 2

17/21

Low speed internal oscillator measurement

AN3101

Figure 11. LSI frequency measurement flowchart


3TART
OF,3)

FREQUENCYMEASUREMENT


3ETCAPTURESTATETOSTART




%NABLECAPTUREINTERRUPT
%NABLE4)-COUNTER
)NCREMENTTHENUMBEROF
COUNTEDPERIODS

7AITFORCAPTURESTATE


TOBECOMPLETED

#OMPUTETHE,3)


FREQUENCYVALUE

#OUNTEDPERIOD


,3)?0%2)/$?.5-"%23

#OMPUTETHEAVERAGEOF,3)
FREQUENCYVALUES





%NDOF,3)FREQUENCY

MEASUREMENT
AI

3.3

LSI measurement demo description


The demo provided with this application note shows the importance of measuring the LSI
frequency value before starting to use peripherals clocked by this type of clock. In this
example the independent watchdog (IWDG) is used to generate a reset after 12 ms.
Since the LSI clock is not accurate, the reset will occur slightly after or before the required
time-out. Hence, measuring the LSI frequency enhances the time-out accuracy.

18/21

Doc ID 16612 Rev 2

AN3101

Conclusion

Conclusion
Even if internal RC oscillators are factory calibrated, the user should calibrate them in the
operating environment if a high clock accuracy is required in the application.
This application note gives two routines:

one for the high speed internal clock calibration: How to tune the HSI clock to 16 MHz?

one for the low speed internal clock measurement: How to get the exact LSI frequency
value?

Several frequency sources can be used to calibrate the HSI oscillator: the LSE crystal, the
AC line, etc. Whatever the reference frequency source, the HSI calibration principle is the
same: a reference signal must be provided to be measured by a timer. The higher the
accuracy of the reference signal frequency, the better the accuracy of the HSI frequency
measurement. The error is computed as the absolute value of the typical HSI frequency
value and the measured one for each HSITRIMR register configuration. From this, the
calibration value is calculated and then programmed in the HSITRIMR register.
The second section of this application note focuses on the LSI clock measurement. The
internal connection between the LSI clock and channel 1 of Timer 2 provided in STM8L15x
microcontroller family is used to get the LSI frequency value. The Timer 2 is clocked using
the HSI clock and Timer 2 channel 1 is configured in input capture mode. So the time
between two consecutive rising edges of LSI clock represents an entire period.

Doc ID 16612 Rev 2

19/21

Revision history

AN3101

Revision history
Table 1.

20/21

Document revision history

Date

Revision

Changes

22-Jan-2010

Initial release.

08-Sep-2010

Updated Section 2.3.2: HSI calibration with fixed error.


Updated Section 2.3.4: HSI calibration demo description.

Doc ID 16612 Rev 2

AN3101

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 STs 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 STS 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 USERS 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.

2010 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 - Philippines - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States of America
www.st.com

Doc ID 16612 Rev 2

21/21

You might also like