cd00282612

AN3258
Application note
STM8AF and STM8S series HSI oscillator calibration
using LIN automatic resynchronization
Introduction
Local interconnect network (LIN) is a widely used standard for communication between
various nodes present inside the electronic control unit (ECU) of a vehicle. LIN sync frame,
which is defined by LIN standard, is used as a reference by the LIN slave nodes for clock
synchronization. Using this technique, LIN slave nodes can calibrate the variable internal
RC oscillator and use it as the system clock source. Therefore, the LIN slave node
application can save the cost of using crystal or resonator oscillators.
This application note describes a method to calibrate the STM8AF and STM8S series highspeed internal (HSI) oscillator using the LIN automatic resynchronization feature of the
LINUART peripheral. The calibration method is also provided with a software routine. It can
be downloaded from www.st.com.
The user must be familiar with the LIN bus standard, STM8AF and STM8S series
architecture, and the basics of C language. Detailed information about the STM8AF and
STM8S series microcontroller peripheral features, hardware registers, and electrical
characteristics are available in STM8S series and STM8AF series 8-bit microcontrollers
reference manual (RM0016) and the product datasheets.
September 2015
DocID17825 Rev 2
1/16
www.st.com
1
Contents
AN3258
Contents
1
HSI calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1
HSI trimming bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2
LIN automatic resynchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3
HSI calibration method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.4
HSI calibration routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.5
LIN header error handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
1.6
LIN divider update method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
1.7
LIN clock deviation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2/16
DocID17825 Rev 2
AN3258
List of tables
List of tables
Table 1.
Table 2.
CLK_HSITRIMR values and binary two’s complement representation. . . . . . . . . . . . . . . . . 6
Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
DocID17825 Rev 2
3/16
3
List of figures
AN3258
List of figures
Figure 1.
Figure 2.
Figure 3.
Figure 4.
Figure 5.
Figure 6.
4/16
STM8AF and STM8S series HSI trimming principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
LIN header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
LIN synch field measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
HSI calibration routine flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
LDIV read/write operations when LDUM = 0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
LDIV read/write operations when LDUM = 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
DocID17825 Rev 2
AN3258
HSI calibration
1
HSI calibration
1.1
HSI trimming bits
STM8AF and STM8S series microcontrollers have an HSI oscillator with a nominal
frequency (fHSI) of 16 MHz which is factory calibrated at an ambient temperature (TA) of
25°C and a supply voltage (VCC) of 5 V. The accuracy for overall temperature and voltage
range is ±5% (please refer to the device specific datasheet) which is sufficient for many
applications. For a given voltage and temperature condition, the HSI oscillator frequency
can be calibrated to ±1% or ± 0.5% by using calibration bits as described below and in
Figure 1.
After a device reset, the factory calibration value at TA = 25 °C and VCC = 5 V is
automatically loaded into the internal calibration register. The HSI frequency can be finetuned by writing the calibration bits present in the HSI clock calibration trimming register
(CLK_HSITRIMR). The maximum number of calibration bits present is either three or four
depending on the device. The calibration bits provide an additional trimming value which is
added to the factory calibration value to fine-tune the HSI output frequency. The additional
trimming value, written in the trimming bits, is interpreted as a signed value with two’s
complement representation (see Table 1). In the case of three trimming bits, this additional
trimming value can vary from -4 (100b) to 3 (011b) and in the case of four trimming bits, this
additional trimming value varies from -7 (1001b) to 7 (0111b). Thus, the additional trimming
value can be added or subtracted to the factory calibration value. An increase in the
trimming value causes a decrease in the HSI frequency. The frequency change per step is
±1% or ±0.5% depending on the device and number of trimming bits used (three or four).
Some devices can use either three or four trimming bits which are programmable via the
HSITRIM option byte.
Figure 1. STM8AF and STM8S series HSI trimming principle
CLK_HSITRIM
+
DECODER
RC OSCILLATOR
HSI Clock Output
INIT_TRIM
DocID17825 Rev 2
5/16
15
HSI calibration
AN3258
Table 1. CLK_HSITRIMR values and binary two’s complement representation
3-bit trimming value
1.2
4-bit trimming value
Decimal
value
Binary two’s
complement value
Decimal
value
Binary two’s complement
value
3
011b
7
0111b
2
010b
6
0110b
1
001b
5
0101b
0
000b
4
0100b
-1
111b
3
0011b
-2
110b
2
0010b
-3
101b
1
0001b
-4
100b
0
0000b
-
-
-1
1111b
-
-
-2
1110b
-
-
-3
1101b
-
-
-4
1100b
-
-
-5
1011b
-
-
-6
1010b
-
-
-7
1001b
LIN automatic resynchronization
In the LIN standard, the master node initiates the communication by sending a LIN message
header to the slave node. The LIN message header sent by the master node comprises
three fields (see Figure 2):

Break field

Sync field

Identifier field
–
frame identifier
–
parity
The break field is at least 13 nominal bit times the dominant value followed by a break
delimiter. The sync field is a byte with a data value of 0x55. The identifier field consists of
two subfields: the frame identifier and the parity. Bits 0 to 5 are frame identifier and bits 6 to
7 are the parity.
6/16
DocID17825 Rev 2
AN3258
HSI calibration
Figure 2. LIN header
Parity bits
LIN break
LIN synch
field
Identifier
field
Usually, the master node uses a crystal or resonator oscillator to generate the correct baud
rate. The sync field byte is used by the slave node to synchronize with the master clock.
The STM8AF and STM8S series LINUART peripheral has an automatic resynchronization
feature which makes use of the sync byte field to synchronize with the master baud rate.
The automatic resynchronization feature measures the sync byte field and automatically
adjusts the slave baud rate generator after each LIN sync field reception from the master
node (see Figure 3).
Figure 3. LIN synch field measurement
TBR
LIN break
LIN synch field
Break Start
Bit0
delim. bit
Bit1
Bit2
Bit3
Bit4
Bit5
Bit6
Bit7
Stop
bit
Next
start
bit
Measurement = 8.TBR = SM.TMASTER
LDIV(n+1)
LDIV(n)
After each LIN break reception, the time duration between five falling edges on the LIN Rx
pin is sampled on the fMASTER clock. The result of this measurement is stored in an internal
19-bit register called SM which is not user accessible. Then, the LIN baud rate divider
(LDIV) is automatically updated at the end of the fifth falling edge.
Note:
fMASTER and TMASTER are the respective system clock frequency and time period of the
STM8AF and STM8S series slave node (please refer to the clock tree figure in STM8S
series and STM8AF series 8-bit microcontrollers reference manual (RM0016)) and should
not be considered as the frequency of the master node.
DocID17825 Rev 2
7/16
15
HSI calibration
1.3
AN3258
HSI calibration method
This section explains how to calibrate the HSI oscillator using the LIN automatic
resynchronization feature described above. The automatic resynchronization feature
updates the LIN baud rate divider (LDIV) after each LIN sync field reception. The calibration
method uses the updated LDIV factor to calculate the HSI frequency variation, under current
conditions, using the equations below.
Equation 1
f HSI
f HSIDIV"" = -------------------HSIDIV
Where, fHSI = nominal HSI frequency of 16 MHz and HSIDIV is the HSI prescaler factor
programmed in the CLK_CKDIVR register.
The LIN baud rate is programmed by software after writing the LDIV_NOM value into the
BRR1 and BRR2 registers.
Equation 2
f MASTER
Baudrate = -----------------------------LDIV_NOM
As slave node uses the HSI clock as fMASTER, fMASTER = fHSIDIV.
Equation 3
f MASTER
LDIV_NOM = -------------------------Baudrate
Equation 4
f MESASURED = LDIV_MEAS  Baudrate
After the LIN sync field reception, the LIN baud rate divider is measured by the automatic
resynchronization (LDIV_MEAS) and it is automatically loaded into the BRR1 and BRR2
registers. These actions keep the same baud rate under varying HSI oscillator frequency
conditions. The LDIV_MEAS factor multiplied by the baud rate gives the current HSI
frequency (fMEASURED).
Where, LDIV_MEAS = LDIV measured after automatic resynchronization.
The difference between the fREFERENCE and fMEASURED gives the variation of HSI oscillator
frequency.
Equation 5
 FREQUENCY = f MASTER – f MEASURED
Where, ∆FREQUENCY is the HSI oscillator frequency variation.
8/16
DocID17825 Rev 2
AN3258
1.4
HSI calibration
HSI calibration routine
The calibration routines are written in C language. They include STM8S/AF standard
peripheral library (STSW-STM8069) functions.
The calibration routine starts with the configuration of the HSI clock prescaler value bits,
HSIDIV[1:0], in the CLK_CKDIVR register. The value programmed in the software example
is HSIDIV[1:0] = 00 which means, fHSIDIV = fHSI/1 = 16 MHz. The HSI trimming bits,
HSITRIM[3:0] or HSITRIM[2:0], in the CLK_HSITRIMR register are kept at reset value.
The LINUART peripheral is initialized at baud rate 19200 bps, 8 data bits, 1 stop bit and no
parity. LINUART is configured in slave mode (LSLV bit =1) with the automatic
resynchronization feature enabled (LASE bit = 1) and the LIN divider update method
(LDUM) bit = 0. Both LIN header detection (LHDIEN) and LIN receiver (RIEN) interrupts are
enabled. The LINEN bit is set to enable the LIN mode.
When a valid LIN header is received by the LIN slave, the LIN receive interrupt is generated.
Inside the interrupt service routine, if the LIN header detection flag (LHDF) is set,
LDIV_MEAS is read from the BRR1 and BRR2 registers (see Figure 4). Using Equation 4,
fMEASURED is calculated. The variation of the HSI oscillator frequency (∆FREQUENCY) is
calculated by using Equation 5.
Depending on whether the ∆FREQUENCY is positive or negative, the trimming value (as
described in Section 1.1: HSI trimming bits) is written to increase or decrease the HSI
oscillator frequency by one trimming step. When the next sync frame is received, the
procedure is repeated to check and calibrate the HSI oscillator until the trimming value
reaches the upper/lower limit or until the measured ∆FREQUENCY is greater than the value
measured with the previous trimming value. The maximum number of valid LIN headers
required to calibrate the HSI oscillator is four if three trimming bits are used and eight if four
trimming bits are used.
DocID17825 Rev 2
9/16
15
HSI calibration
AN3258
Figure 4. HSI calibration routine flowchart
,).RECEIVEINTERRUPT
SERVICEROUTINE
-AINROUTINE
.O
#ONFIGURETHE(3)CLOCKPRESCALER
)NITIALIZETHE,).5!24
%NABLEINTERRUPTS
)NFINITELOOP
WAITINGFOR,).RECEIVEINTERRUPT
9ES
#HECKFOR,(%FLAG
ANDCLEARIF
%ND
,($&FLAG
,$)6?-%!3VALUEREADFROM
"22AND"22F-%!352%$
,$)6?-%!3
"AUDRATE
6ARIATION
F-!34%2F-%!352%$
!
.O
6ARIATIONAND
(3)42)-VALUE
42)-?-!8?6!,5%
9ES
0OSITIVEVARIATION
(3)42)-(3)42)-
6ARIATIONAND
(3)42)-VALUE
42)-?-).?6!,5%
.O
42)--).'$)3!",%
.EGATIVEVARIATION
(3)42)-(3)42)-
0OSITIVEVARIATION
NEGATIVEVARIATION
9ES
42)--).'$)3!",%
(3)42)-(3)42)-
.EGATIVEVARIATION
POSITIVEVARIATION
9ES
42)--).'$)3!",%
(3)42)-(3)42)-
!
2ELOADTHE,$)6./-VALUE
"22)NITVALUE
"22)NITVALUE
%NDOF,).5!24
INTERRUPTSERVICEROUTINE
AI
10/16
DocID17825 Rev 2
AN3258
1.5
HSI calibration
LIN header error handling
It is possible that slave node receives an invalid LIN header or a LIN header with an error. In
this case, the LIN header detection flag (LHDF) is not set and the corresponding LIN header
detection interrupt does not occur but, the LIN header error (LHE) flag is set.
A LIN header error may occur under the following conditions:
1.
The break delimiter is too short.
2.
The sync field is not equal to 0x55 or the deviation error in the sync field is outside the
LIN specification which allows a clock deviation of up to 14% between the slave and
master oscillators.
3.
The framing error occurs in the sync field or the identifier field.
4.
A LIN header reception timeout occurs.
In the case of a LIN header error detection (LHE bit set), the LHE bit is cleared inside the
interrupt service routine and the software waits for the new valid LIN header.
1.6
LIN divider update method
Three registers are managed internally by the LINUART peripheral for the automatic update
of the LIN divider (LDIV). They are:
1.
LDIV_NOM which stores the value written by software in the LINUART_BRR1 and
LINUART_BRR2 registers.
2.
LDIV_MEAS which stores the value of the LIN sync field measurement.
3.
LDIV which stores the value which is used to generate the baud rate.
The LDIV can be updated from the LDIV_MEAS register after the LIN sync field
measurement or from the LDIV_NOM register after a software write in LINUART_BRR1.
If LASE =1, the LDIV_MEAS is loaded automatically into LDIV after each LIN sync field
measurement. The loading from LDIV_NOM into LDIV depends on the LDUM bit setting.
If LDUM = 1, the LDIV_NOM value is loaded automatically into LDIV register at the end of
character reception (RXNE =1).
If LDUM =0, the LDIV_NOM value is loaded into LDIV after a software write into
LINUART_BRR1.
If the loading from LDIV_MEAS and LDIV_NOM into LDIV occurs at the same time,
LDIV_NOM has the priority.
DocID17825 Rev 2
11/16
15
HSI calibration
AN3258
In Figure 5, the software example has been implemented with (LDUM = 0). After the LIN
reception of the LIN header interrupt and the execution of the calibration routine, the LDIV is
loaded with LDIV_NOM by writing the LINUART_BRR1.
Figure 5. LDIV read/write operations when LDUM = 0
Write LINUART_BRR1
Write LINUART_BRR2
LDIV[11:4] LDIV[15:2]
LDIV[3:0]
LDIV_NOM
Write
LINUART_BRR1
LIN sync field
measurement
LDIV[11:4] LDIV[15:12]
LDIV[3:0]
LDIV_MEAS
Update
at end of
synch field
LDIV[7:0]
LDIV[15:12]
LDIV[3:0]
Read LINUART_BRR1
Baud rate
generation
LDIV
Read LINUART_BRR2
In the case of LDUM = 1 (see Figure 6), the LDUM bit has to set by software before the LIN
checksum reception. In this way, the LDIV_NOM is loaded into LDIV at the end of character
reception.
Figure 6. LDIV read/write operations when LDUM = 1
Write LINUART_BRR1
Write LINUART_BRR2
LDIV[11:4] LDIV[15:12]
LDIV[3:0]
LDIV_NOM
LIN sync field
measurement
RXNE=1
LDIV[11:4] LDIV[15:12] LDIV_MEAS
LDIV[3:0]
Update
at end of
synch field
LDIV[11:4] LDIV[15:12] LDIV
LDIV[3:0]
Read LINUART_BRR1
12/16
Read LINUART_BRR2
DocID17825 Rev 2
LDUM is reset
Baud rate
generation
AN3258
1.7
HSI calibration
LIN clock deviation
LIN clock deviations may be caused by the following sources:
1.
Deviation due to transmitter error (DTRA): The transmitter can be either a master or a
slave (in the case of a slave listening to the response of another slave).
2.
Error due to the LIN synch measurement performed by the receiver (DMEAS).
3.
Error due to the baud rate quantization of the receiver (DQUANT).
4.
Deviation of the local oscillator of the receiver (DREC): This deviation can occur during
the reception of one complete LIN message assuming that the deviation was
compensated at the beginning of the message.
5.
Deviation due to the transmission line (DTCL) which is generally due to the
transceivers.
Total clock deviation = DTRA + DMEAS + DQUANT + DREC + DTCL
If the LINUART is to receive a character correctly, the total deviation should be <3.75%. For
more details, please refer to STM8S series and STM8AF series 8-bit microcontrollers
reference manual (RM0016).
DocID17825 Rev 2
13/16
15
Conclusion
2
AN3258
Conclusion
The calibration method described in this application note allows the LIN slave nodes to
calibrate and operate with the HSI internal oscillator (16 MHz) under variable temperature
and voltage conditions.
14/16
DocID17825 Rev 2
AN3258
3
Revision history
Revision history
Table 2. Document revision history
Date
Revision
Changes
05-Oct-2010
1
Initial release
11-Sep-2015
2
Extended the document applicability to STM8AF and STM8S
series and updated the document references accordingly.
Replaced LINUART/UART1 occurrences with LINUART.
DocID17825 Rev 2
15/16
15
AN3258
IMPORTANT NOTICE – PLEASE READ CAREFULLY
STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and
improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on
ST products before placing orders. ST products are sold pursuant to ST’s terms and conditions of sale in place at the time of order
acknowledgement.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or
the design of Purchasers’ products.
No license, express or implied, to any intellectual property right is granted by ST herein.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. All other product or service names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.
© 2015 STMicroelectronics – All rights reserved
16/16
DocID17825 Rev 2