AN1278 Application Note: Lin (Local Interconnect Network) Solutions
AN1278 Application Note: Lin (Local Interconnect Network) Solutions
AN1278 Application Note: Lin (Local Interconnect Network) Solutions
APPLICATION NOTE
LIN (LOCAL INTERCONNECT NETWORK) SOLUTIONS
by Microcontroller Division Applications
INTRODUCTION
Many mechanical components in the automotive sector have been replaced or are now being
replaced by intelligent mechatronical systems. A lot of wires are needed to connect these
components. To reduce the amount of wires and to handle communications between these
systems, many car manufacturers have created different bus systems that are incompatible
with each other.
In order to have a standard sub-bus, car manufacturers in Europe have formed a consortium
to define a new communications standard for the automotive sector. The new bus, called LIN
bus, was invented to be used in simple switching applications like car seats, door locks, sun
roofs, rain sensors, mirrors and so on.
The LIN bus is a sub-bus system based on a serial communications protocol. The bus is a
single master / multiple slave bus that uses a single wire to transmit data.
To reduce costs, components can be driven without crystal or ceramic resonators. Time synchronization permits the correct transmission and reception of data. The system is based on a
UART / SCI hardware interface that is common to most microcontrollers.
The bus detects defective nodes in the network. Data checksum and parity check guarantee
safety and error detection.
As a long-standing partner to the automotive industry, STMicroelectronics offers a complete
range of LIN silicon products: slave and master LIN microcontrollers covering the protocol
handler part and LIN transceivers for the physical line interface. For a quick start with LIN,
STMicroelectronics supports you with LIN software enabling you to rapidly set up your first LIN
communication and focus on your specific application requirements.
Figure 1. LIN Network Overview
CAN Bus
MASTER
LIN Bus
SLAVE
AN1278/0502
SLAVE
SLAVE
1/44
Table of Contents
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1 LIN PROTOCOL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 LIN PRODUCTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1 LIN MICROCONTROLLERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.1 LIN Slave MCUs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.2 LIN Master MCUs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2 LIN TRANSCEIVERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2.1 L9637 K-Line Transceiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2.2 L9638 LIN Transceiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3 LIN SOFTWARE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.1 TYPES AND MACRO DEFINITIONS: LIB.H . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1.1 Debug settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1.2 Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1.3 Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2 PROTOCOL HANDLER: LIN.P/H . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2.1 Type definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.2.2 User interface functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.2.3 Timeout handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.2.3.1 Initializing the timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.3 LIN CONFIGURATION FILE: LIN_CONFIG.H . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.4 APPLICATION INTERFACE: LIN_AI.C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4 EXAMPLES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.1 IMPLEMENTATION ON THE ST72254G2 - SOFTWARE EMULATED SCI . . . 25
4.1.1 Step by Step Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.2 IMPLEMENTATION ON THE ST72334N4 - HARDWARE SCI . . . . . . . . . . . . . 28
4.2.1 Step by Step Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.2.1.1 lin_config.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.2.1.2 lin_ai.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.2.1.3 Master data request (DataRequest_Notification) . . . . . . . . . . . . . 30
4.2.1.4 Data reception (DataReceived_Notification) . . . . . . . . . . . . . . . . 30
4.3 STMICROELECTRONICS LIN PACKAGE - EXAMPLE INSTALLATION . . . . 33
4.3.1 LIN package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
4.3.2 Quick start with STVD7 and Cosmic C Compiler . . . . . . . . . . . . . . . . . . . . 33
4.4 PERFORMANCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
4.4.1 Timing considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
4.4.2 Using the Emulated SCI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
4.4.2.1 Reception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44. . 39
.
4.4.2.2 Transmission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2/44
Table of Contents
4.4.3 Using the on-chip SCI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.4.3.1 Reception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.4.3.2 Transmission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5 SUMMARY OF CHANGES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3/44
1 LIN PROTOCOL
The aim of this chapter is to give an overview of the LIN protocol and concept. For detailed and
up-to-date information please refer to the official LIN homepage: www.lin-subbus.org where
you can register for the LIN specification package.
The LIN specification package consists of three parts:
s
The first part describes the LIN physical and data link layers. The second part describes the
LIN configuration language. The LIN configuration language enables the user LIN network to
be described in a file (how many nodes, how many frames, frame description, baudrate etc.).
The goal of this specification is to ease communications between the parties involved in the
development of a LIN network like car manufacturers and their suppliers. The third and last
part is about the software implementation of the LIN protocol and specifies some points on
how the software implementation has to be done.
The LIN concept uses a single master / multiple slave model. Only the master is able to initiate
a communication. A LIN frame consists of a header and a response part. To initiate a communication with a slave the master sends the header part. If the master wants to send data to the
slave it goes on sending the response part. If the master requests data from the slave the
slave sends the response part.
Figure 2. Basics of LIN communication
Master to slave 1
Slave 1
Slave 2
Master
Slave 2 to master
sent by:
LIN
Bus
Master
Slave 1
1 LIN frame
Header
Response
Slave 2
Direct communication between slaves is not possible. But as all nodes always listen to the
bus, a master request can be used to handle slave-to-slave communications.
4/44
The LIN protocol is object-oriented and not address-oriented. The header contains the identifier which identifies the LIN frame and the data it contains. Different nodes may receive the
same frame data.
The response part consists mainly of data of selectable length (1 to 8 bytes). The data are secured by an 8 bit checksum.
The LIN protocol is time-trigger oriented. The master periodically sends the same sequence of
LIN frames. Each sequence, the master and the slaves update the data they send and receive. The sequence sent by the master may change depending on application events.
Example: The slave is a sensor measuring a analog value which is communicated to the
master via LIN. The slave continuously measures its analog input independently from the LIN
communication. In response to a master request (periodical) the slave sends the up-to-date/
last measured value of the analog input.
Figure 3. Time-triggered protocol
Master
Slave 1
LIN
Bus
25
Slave 2
25
Analog value:25
Analog
value
change
38
1 sequence
38
Analog value:38
In order to achieve a good level of security, different mechanisms exist like parity bits on the
identifier or checksum on data bytes.
One important feature of the protocol is to enable the slave MCUs to run with low cost oscillators such as an integrated RC oscillator provided that the accuracy is better than +/-15%. For
this purpose the header contains a sync field byte consisting of the constant 0x55. This byte
enables each slave to measure the master bit time and to synchronize its clock accordingly.
In order to obtain very low power consumption, the master is able to send a sleep frame. Any
node can go into low power mode. To wake up the network, any node can send a so-called
wake-up signal.
5/44
Wake-up
signal
Sleep mode
command
normal mode
6/44
Sleep mode
Network
start-up
normal
mode
2 LIN PRODUCTS
A typical LIN node consists of a microcontroller for handling the LIN protocol and a LIN transceiver for interfacing the digital part and the physical line (see Figure 5 . LIN bus topology).
STMicroelectronics offers both kind of products.
Figure 5. LIN bus topology
LIN Bus
TRANSCEIVER
TX
MASTER
RX
LIN Bus
MICROCONTROLLER
SLAVE
SLAVE
SLAVE
Peripherals
Operating Supply
CPU Frequency
Operating Temperature
Packages
8k
4k
8k
4k
8k
256 (128)
Watchdog
Watchdog
timer,
Watchdog timer, Two 16Watchdog timer, One 16- timer, One
Two16-bit
bit timers, SPI, I2C, ADC
16-bit timer,
bit timer, SPI
timers, SPI,
SPI, ADC
ADC
3.2V to 5.5V
Up to 8MHz (with oscillator up to 16 MHz)
-40C to +85C (-40C to +105/125C optional)
SO28 / SDIP32
7/44
Peripherals
Operating Supply
CPU Frequency
Operating Temperature
Packages
ST72334J2
16k
8k
16k
8k
384
(256)
384
(256)
ST72334N4
ST72314N4
8k
ST72334N2
ST72314N2
8k
ST72334J4
ST72314J4
Program memory
- bytes
RAM (stack) bytes
ST72314J2
Features
ST72124J2
Table 2. Low cost LIN slave MCUs with hardware SCI - Flash/ROM MCUs
16k
8k
16k
384
(256)
512
(256)
512
384
512
384
512
(256)
(256)
(256)
(256)
(256)
Watchdog timer, Two 16-bit timers, SPI, SCI
ADC
3.0V to 5.5V
Up to 8MHz (with up to 16MHz oscillator)
TQF64/SDIP56
TQF44/SDIP42
TQF64/SDIP56
ST72511R9
ST72511R7
ST72511R6
60k
48k
32k
2048 (256)
1536 (256)
1024 (256)
Watchdog timer, Two 16-bit timers, 8-bit PWM ART, SPI, ADC
3.0V to 5.5V
Up to 8MHz (with oscillator up to 16 MHz)
-40C to +85C (-40C to +105/125C optional)
TQFP64
8/44
48k
ST72521R5
ST72521R4
24k
16k
32k
2048 (256)
1536 (256)
1024 (256)
768 (256)
512 (256)
Watchdog timer, 16-bit timers, SPI, SCI, 10-bit ADC, CAN
8-bit PWM
8-bit PWM ART, I2C
ART
2.7V to 5.5V
16 to 50kHz (with 32 to 100kHz oscillator), 500 to 8 MHz (with 1 to 16 MHz oscillator), 2 to 8 MHz (with 2 to 4 MHz oscillator and PLL)
0C to 70C/-40C to +85C/-40C to +105C/-40C to +125C/
TQFP80(M), TQFP64 (R)
ST92F150JD
128K
6K
1K
2MFT, 2 EFT, STIM,
WD
2 SCI, SPI, IC
16 x 10 bits
2 CAN, J1850
P/TQFP100
ST92F150JC
60/128K
2/4K
1K
ST92F124J
60/128K
2/4K
1K
ST92F124
60/128K
2/4K
1K
Note: The master MCUs listed above have all an on-chip CAN peripheral. This corresponds to
the initial LIN concept: the LIN network as sub-network of CAN. However any other MCUs
(listed above as slave for example) can be used to implement a master node.
2.2 LIN TRANSCEIVERS
To ensure the physical behaviour of the LIN bus STMicroelectronics also offers K-Line drivers
and a dedicated LIN Bus Transceiver.
2.2.1 L9637 K-Line Transceiver
The L9637 K-Line transceiver is a monolithic integrated circuit containing standard ISO 9141
compatible interface functions. Its features are listed below.
s
s
s
s
Defined OFF output status in under voltage condition and VS or GND interruption
V K VS
9/44
VS
Int.5V
Internal
Voltage
Regulator
Glitch
Filter
RX D
LIN
TXD
Control
Logic
EN
WUP
Glitch
Filter
INH
Protection
GND
10/44
The following figure (Figure 7) shows a typical application of the L9638 LIN transceiver together with the ST72124J microcontroller and a voltage regulator.
Figure 7. Application of L9638 with ST72124J Microcontroller
LIN Node
GND
Vout
Vbatt
Vs
voltage
regulator
EN
Vdd
RES
reset
GND
ST72124J
MCU
Vs
INH
EN
LIN
transceiver
LIN
L9638 TxD
LIN
RxD
WUP
I/O pin
TDI
RDO
GND
Vdd
The voltage regulator supplies the application and generates the MCU reset signal. The LIN
transceiver is the physical line interface between the SCI (Serial Communication Interface)
TDI and RDO pins of the microcontroller and the LIN bus line. The microcontroller handles the
LIN protocol and the application functions.
In order to lower power consumption the microcontroller is able to switch off the LIN transceiver via the L9638 EN input. The transceiver is then able to switch off the voltage regulator
by connecting its INH output to the EN input of the voltage regulator. In this state any activity on the LIN bus will cause the L9638 to wake the voltage regulator up via the INH pin.
Another wake-up source is the WUP pin of the L9638 that can be used for contact sensing.
Any edge on this pin will also wake up the regulator.
11/44
3 LIN SOFTWARE
Table 6. Software Overview
Version
Supported nodes
Supported MCUs
LIN protocol specification revision
2.0
slave
all ST7 MCUs
rev 1.2
The LIN standard includes the specification of the communication protocol but also the use of
associated tools.
STMicroelectronics supports the development of your LIN application by providing ready-touse LIN software. This software only handles the communication protocol part. For a complete
software development tool solution you can contact LIN specialist third party tools manufacturers like VCT (http://www.vct.se) or Vector (http://www.vector-informatik.de).
The software supports LIN slave nodes. It consists of 4 files:
lin.c/h: protocol handler code
lin_config.c: LIN parameter configuration file
lin_ai.c: application interface
A fifth additional file is delivered:
lib.h: library file (macros, types definition)
The software supports the COSMIC C compiler.
Figure 8. Software Architecture
application
APPLICATION
specifics
lin-ai.c
LIN
lin_config.h
MESSAGE
lin.c
HARDWARE
12/44
interrupt
The software is interrupt driven. As soon as a message begins on the bus, an interrupt is generated and automatically handles the protocol. This means that the protocol handler is totally
autonomous. It runs in the background. When the received frame is decoded by the software
this is notified to the application in the lin_ai.c. In this file the user is able to customize the behaviour of the application upon reception of a frame for example.
3.1 TYPES AND MACRO DEFINITIONS: LIB.H
3.1.1 Debug settings
see 4.4.1 Timing considerations
3.1.2 Types
The software uses predefined types for 1-byte and 2-byte variables.
The name used for the one-byte type is uByte.
The name used the two-byte type is uWord.
Beside these, a third type is used to define two-byte variables that can also be accessed high
byte or low byte only.
typedef union {
unsigned int w_form;
struct {
unsigned char high, low;
} b_form;
} TwoBytes;
13/44
defines the different error code values that the software functions are able to return.
t_message_direction
typedef enum {ID_DATAREQUEST,ID_DATASENT}t_message_direction;
defines a type of function return value consisting of an error code made up of error type and a
data byte.
t_header
typedef struct
{
uByte identifier;
uByte length;} t_header;
A LIN frame consists of a header and a response. This typedef defines the header part type.
t_response
typedef struct
{
uByte data[8];
uByte checksum;} t_response;
A LIN frame consists of a header and a response. This typedef defines the response part type.
3.2.2 User interface functions
To integrate this software into your application software you have to link 3 or 4 functions defined in lin.c to your project.
14/44
Function
Parameters
Return value
Description
Function
Parameters
Return value
Description
Function
Parameters
Return value
Description
void LIN_Init(void)
none
none
LIN communication initialization
Has to be called after reset
void LIN_SendWakeUpSignal(void)
none
none
When the LIN network is in sleep mode and the application
wants to wake it up, this function has to be called to send a
wakeup frame.
void LIN_Interrupt(void)
none
none
LIN interrupt service routine. You should link this function to the
corresponding interrupt vector: timer or SCI interrupt according
to the SCI hardware used (see 3.3 LIN Configuration File:
lin_config.h).
void LINTimeOut_Interrupt(void)
none
none
LIN timeout interrupt service routine (exists only in some cases).
It only exists if SCI is defined or if the timer defined for timeouts
is not the one used for the SCI emulation You should link this
function to the corresponding timer interrupt vector (see 3.2.3
Timeout handling and 3.3 LIN Configuration File: lin_config.h).
15/44
#define UART_TIMERA
#define UART_TIMERB
16/44
FCPU
Speed in MHz of the MCU internal frequency
#define SCI
#define
Description
#define LIN_PORT_ADD
#define LIN_RX_PINNB
#define
LIN_PORT_ADD
Needs to be defined only if UART_TIMERA or
UART_TIMERB is defined.
Description
#define
LIN_RX_PINNB
Needs to be defined only if UART_TIMERA or
UART_TIMERB is defined.
Description
LIN RX is pin:
LIN_PORT_ADD
LIN_RXC_PINNB
17/44
#define UART_IC1
#define UART_IC2
#define
Description
UART_IC1, UART_IC2
Needs to be defined only if UART_TIMERA or
UART_TIMERB is defined.
Define only one of these options. Define UART_IC1
if the LIN RX pin is connected to the Input Capture
1 pin. Define UART_IC2 if the LIN RX pin is connected to the Input Capture 2 pin.
#define UART_OC1
#define UART_OC2
#define
Description
UART_OC1, UART_OC2
Needs to be defined only if UART_TIMERA or
UART_TIMERB is defined.
Define only one of these options. Define
UART_OC1 if the LIN RX pin is connected to the
Output Compare 1 pin. Define UART_OC2 if the
LIN RX pin is connected to the Output Compare 2
pin.
#define TIMEOUT_TIMERA
#define TIMEOUT_TIMERB
#define
Description
TIMEOUT_TIMERA, TIMEOUT_TIMERB
Define only one of these options. Define which timer is used for the timeout handling.
If you already defined a timer for the SCI emulation
set the same timer for the timeout handling. This will
optimize the use of resources: A timer is fully used
for the LIN software and the second is free for the
application.
#define TIMEOUT_OC1
#define TIMEOUT_OC2
#define
Description
18/44
TIMEOUT_OC1, TIMEOUT_OC2
Define only one of these options. Define which output compare of the previously defined timer is used
for the timeout handling.
Warning: If the same timer is used for both the SCI
emulation and the timeouts handling, do not define
the same output compare for both features.
#define BAUDRATE
#define
Description
BAUDRATE
LIN communication speed in bit/s
#define BRR
#define ExPR
#define
BRR, ExPR
Needs to be defined only if SCI is defined.
Set both registers to obtain the baudrate previously
defined. BRR is the baudrate register and ExPR
will set the same value for both the extended receive prescaler division register and the extended
transmit prescaler division register. Refer to the
datasheet of the MCU you are using (Serial Communication Interface chapter).
Description
Note: Here is a list of baudrates and corresponding settings of (BRR,ExPR) depending on the
CPU frequency. Note that there are some small differences in the SCI prescaler between for
example the ST72324/321/521 and ST72314/334 derivatives. As a result the value of BRR
and ExPR may also depend on the MCU derivative.
Table 8. Example of (BRR,ExPR) values versus baudrate and fCPU
fCPU(*)
ST72x314/334/124
8MHz
4MHz
2MHz
8MHz
ST72x324/321/521
4MHz
2MHz
bit rate(bit/s)
4,8k
(0xD2,0x00)
9,6k
(0xC9,0x00)
19,2k
(0xC0,0x00)
(0x00,0x0D)
20k
(0x00,0x19)
ID_TABLE_SIZE
number of LIN frames to be handled by the application. See Section 3.4
19/44
#define RESYNCH
#define
description
RESYNCH
Can only be defined if UART_TIMERA or
UART_TIMERB has been defined.
If the application is working with an inaccurate clock
the LIN software is able to resynchronize to the
master clock (refer to the LIN protocol). Define this
symbol to activate this feature.
direction represents the data flow direction, is of type t_message_direction and should therefore be set to ID_DATAREQUEST for data being requested by the master and to be sent by
the application/slave or to ID_DATASENT for data being sent by the master to the application/
slave.
data length represents the number of data bytes of the corresponding frame. It can be set between 0 to 8.
Note: The LIN protocol specification gives some advice concerning the coding of the data
length through the ID5 and ID4 bits in the identifier byte. But this coding is no longer mandatory since revision 1.2 of the specification.
The number of members has to be entered in the lin_config.h file (See 3.3 LIN Configuration
File: lin_config.h).
3 Notification functions
The LIN software is interrupt driven which means you do not have to poll any variables to
handle LIN communication. When activity appears on the LIN bus, the LIN interrupt service
routine is entered and starts decoding the LIN frame. Once the LIN software is able to notify an
20/44
event to the application, one of the 3 notification functions is called. These functions are delivered empty and by expanding them the user is able to fully define his application LIN communication. The 3 functions correspond to the 3 kinds of events:
Function
Parameters
Return value
Description
Function
Parameters
Return value
Description
Function
Parameters
Return value
Description
21/44
4 EXAMPLES
The purpose of this chapter is to give you an example, describing step by step how to use the
LIN software. This example was defined to demonstrate the LIN software and not to show a
typical LIN application.
The network consists of the master node and 2 slave nodes.
Figure 9. LIN network example
MASTER
LIN Bus
SLAVE 1
SLAVE 2
Identifier ID[7..0]
0x03
0x20
0x76
Message Length
2
4
2
Data Direction
master to slave1
slave1 to master
slave2 to master
0x80(*)
(*): The example is given for the LIN specification package 1.1 as many tools still dont support
the 1.2 package.
22/44
master
0x03 data to
slave 1
0x20
slave 1
TX
0x20
0x03
0x20
slave1
data
slave 2
TX
bus
line
0x03
0x76
slave2
data
0x03
0x20
DataToSlave1
RequestToSlave1
0x76
RequestToSlave2
The figure above represents the LIN communication in running mode. The SleepModeCOmmand frame is sent by the master to set the LIN network in sleep mode and is therefore not part of the normal communication.
The baud rate is 9600 baud.
This communication corresponds to the following LIN description file:
23/44
The rest of the example is divided into 2 main parts. The described example is first implemented on the ST72254G2 MCU which has no SCI peripheral. This part demonstrates the capability of the LIN software to emulate the whole LIN protocol using the embedded 16-bit
timer. The second part describes the implementation on the ST72334N4 which has an SCI peripheral.
24/44
Communication peripheral
Comments
An external resonator of 16 Mhz
is used. As a result the internal
CPU frequency is 8MHz. So
FCPU is set to 8000000.
The ST72254 has no SCI peripheral on chip. As a result the
UART communication has to be
emulated by one of the 16bit timer TimerA or TimerB. TimerA is
chosen in this example. As a result UART_TIMERA is defined
and UART_TIMERB and SCI
are commented out.
Depending on the constraints of
the board layout and according to
the ST72254 pinout we select the
Input Capture 1 pin and the Output Compare 1 pin for respectively the LIN RX and LIN TX signals.
The Input Capture 1 pin of TimeLIN RX and LIN TX pin definition rA is linked to the Port B pin 0
(see pin description in the MCU
datasheet). As a result
LIN_RX_PINNB is set to 0. Port
B data register has the address
0x04. As a result
LIN_PORT_ADD is defined to
0x04
The UART communication is already using one input capture
and one output compare of TimerA. Setting TimerA for the timeout
handling will complement the use
of the timer. So we define
Timeouts
TIMEOUT_TIMERA. Output
Compare 1 is already used by the
UART communication (see
UART_OC1) so we define the
output compare 2 for the timeouts handling.
The example LIN baudrate is
LIN baudrate
9600 Baud.
8000000
#define UART_TIMERA
//#define UART_TIMERB
//#define SCI
#define UART_IC1
//#define UART_IC2
#define UART_OC1
//#define UART_OC2
#define LIN_PORT_ADD
0x04
#define LIN_RX_PINNB
#define TIMEOUT_TIMERA
//#define TIMEOUT_TIMERB
//#define TIMEOUT_OC1
#define TIMEOUT_OC2
#define BAUDRATE
9600
25/44
Setting Group
SCI peripheral registers
Comments
lin_ai.c
The first part of the lin_ai.c code is the definition of the LIN frames the application has to
handle. Out of the 4 defined for the whole network the application handles the 3 following
frames:
Frame name
DataToSlave1
RequestToSlave1
SleepModeCommand
Identifier ID[7..0]
0x03
0x20
0x80
Message Length
2
4
2
Data Direction
master to slave1
slave1 to master
master to all slaves
The second part of the lin_ai.c code consists of 3 notification functions and is the kernel of the
LIN communication. Filling the notification functions enables you to define the behaviour of the
application upon a master data request (DataRequest_Notification function) on the reception
of data from the master ( DataReceived_Notification function) and when errors occur
(Error_Notification function).
Master data request (DataRequest_Notification)
The application handles one data request from the master corresponding to the identifier 0x20
(ID_Table [1]). The application has to return a pointer to an array containing the data to be
sent. In this example we declare an array slave_data[]. This array will be shared between the
26/44
application updating it with the latest data and the LIN communication sending its content on
request of the master. The corresponding code for the DataRequest_Notification function is:
extern uByte slave_data[];
uByte * DataRequest_Notification(@tiny t_header *header)
{
if(header->identifier == ID_Table[1].id){
return(slave_data);
}
}
Any activity on the bus will wake up the ST7 out of Halt mode. As soon as the ST7 is ready to
execute the next instruction any incoming frame can be received.
27/44
The application can also be woken up by a sensor connected to one pin of the ST7 and then
should also wake up the whole LIN network. For this a wake-up frame has to be sent, which is
done by the LIN_SendWakeUpSignal() function. This function is therefore inserted in the application wake-up interrupt:
@interrupt void PORTS_0_Interrupt(void)
{
unsigned char i;
i=250;
while(i--);
if(!(PADR&0xFE))
{
PAOR&=0xFE;
LIN_SendWakeUpSignal();
}
}
Communication peripheral
Comments
An external 16 MHz resonator is
used. As a result the internal
CPU frequency is 8 MHz. So
FCPU is set to 8000000.
The ST72334N4 has an on chip
SCI peripheral. As a result SCI
is defined, UART_TIMERA and
UART_TIMERB are commented out.
8000000
//#define UART_TIMERA
//#define UART_TIMERB
#define SCI
//#define UART_IC1
//#define UART_IC2
//#define UART_OC1
These are not needed when us//#define UART_OC2
LIN RX and LIN TX pin definitions ing an on-chip SCI peripheral. All
symbols are commented out.
//#define LIN_PORT_ADD
0x04
//#define LIN_RX_PINNB
28/44
Timeouts
#define TIMEOUT_TIMERA
One output compare of one timer
is needed for the timeouts han- //#define TIMEOUT_TIMERB
dling. We choose the output compare 1 of timer A. So
#define TIMEOUT_OC1
TIMEOUT_TIMERA and
//#define TIMEOUT_OC2
TIMEOUT_OC1 are defined.
LIN baudrate
#define BAUDRATE
9600
//#define BRR
0xC9
//#define ExPR
0x00
4.2.1.2 lin_ai.c
The first part of the lin_ai.c code is the definition of the LIN frames the application has to
handle. Out of the 4 defined for the whole network the application handles the 3 following
frames:
Frame name
DataToSlave1
RequestToSlave1
SleepModeCommand
Identifier ID[7..0]
0x03
0x20
0x80
Message Length
2
4
2
Data Direction
master to slave1
slave1 to master
master to all slaves
The second part of the lin_ai.c code consists of 3 notification functions and is the kernel of the
LIN communication. Filling the notification functions enables you to define the behaviour of the
application upon a master data request (DataRequest_Notification function) upon the reception of data from the master ( DataReceived_Notification function) and upon errors
(Error_Notification function).
29/44
30/44
scribed in 3.2.2 User interface functions the application is responsible for the initialization and
the interrupt routine.
Initialization:
Firstly, the output compare must be configured: The OCIE flag of the TACR1 register must be
set and the defined OC disabled by writing the high byte of the OC1 register. As the application is not using OC2 it is disabled also in the same way.
Secondly, the timer prescaler must be calculated using the equations given in Table 7:
on-chip SCI used
31/44
DESCRIPTION
COMMENTS
------------------------------------------------------------------------------*/
void TIMA_Init(void)
{
TACR1=0x40;
TACR2=0x00;
/* prescaler = 4 */
TASR;
TAOC2_L;
TAOC2_H=0x55;
TASR;
TAOC1_L;
TAOC1_H=0x55;
Interrupt:
The timer is only used for the timeout handling so the LINTimeOut_Interrupt function just
needs to be called in the Timer A interrupt service routine defined by the application which corresponds to the following code:
void LINTimeOut_Interrupt(void);
/*-----------------------------------------------------------------------------ROUTINE NAME : TIMA_Interrupt
INPUT/OUTPUT : None
DESCRIPTION
COMMENTS
------------------------------------------------------------------------------*/
@interrupt void TIMA_Interrupt(void)
{
LINTimeOut_Interrupt();
}
32/44
application note
examples
254
334
lin_v2.0
You will receive a zip file called st_lin.zip that will generate the above directory tree. Create
a new folder we will call the working directory and extract the files into it.
The directory tree and architecture are the same for both example.
Figure 13. Example directory tree
254 (resp.334)
config
configuration files
(makefile, linker file,
LIN description file)
object
sources
main file
lin
LIN software
st7
33/44
Cosmic C Compiler: Cosmic C compiler for the ST7 target. For further information or contacts go to: http://www.cosmic-software.com/
In order to be able to compile and start a debug session and even flash an ST7 MCU with the
example code you need to create a new STVD7 workspace and configure your COSMIC tools
installation directory.
Create a new STVD7 project:
start the STVD7 software.
First check that the Cosmic compiler installation directory is configured: Project>Toolchains
Path... Under Cosmic Builder Path enter (if not already configured) the compiler path (where
cxst7.exe is located).
We are ready to create a new project. Select File>New Workspace. Under Workspace
filename enter 334env (or 254env). Under Workspace location enter the examples directories: <your working directory>\st_lin\examples\334 (or <your working directory>\st_lin\examples\254). Click on Next. Fill in the next dialog box as follows:
Figure 14. Project configuration
or
object\254env.elf
Click OK.
To have easy access to your source files, configure the source file directories in the workspace window. The example has 3 folders containing the source files:
34/44
35/44
need to update them when you move the example to another directory/PC. One file contains
absolute paths: The linker file located in <your working directory>\st_lin\examples\334\config
(or -\254\config) and named 334env.lkf (resp. 254env.lkf). You need to enter 3 paths. Edit the
file. See Figure 16:
Figure 16. COSMIC linker file configuration
...
+seg .share -a UZPAGE -is -sRAM
<ENTER COSMIC INSTALL PATH>\lib\crtsx.st7 # startup routine
..\object\main.o # application program
..\object\adc.o
..\object\crs.o
..\object\eep.o
..\object\mcc.o
..\object\misc.o
..\object\ports.o
..\object\sci.o
..\object\spi.o
..\object\tima.o
..\object\timb.o
..\object\trap.o
..\object\lin.o
..\object\lin_ai.o
<ENTER COSMIC INSTALL PATH>\lib\libm.st7
<ENTER COSMIC INSTALL PATH>\lib\libims.st7
+seg .const -b 0xFFE0 -k # vectors start address
..\object\vector.o # interrupt vectors
...
In place of <ENTER COSMIC INSTALL PATH> (3 times) enter your Cosmic compiler installation path (path of cxst7.exe). For example replace <ENTER COSMIC INSTALL
PATH>\lib\crtsx.st7 by c:\cosmic\\lib\crtsx.st7. Save the file.
The example is ready. You can build the example (F7). Build will generate a 334env.elf (or
254env.elf) for debugging and a 334env.s19 (254env.s19) for flashing an MCU. Refer to the
STVD7 documentation for details using of the STVD7 editor and debugger.
4.4 PERFORMANCE
The above examples correspond to the 2 main kinds of software configuration, the SCI communication emulated by software or supported by the hardware SCI peripheral. The corre-
36/44
sponding software performance is different. Table 9 and Table 10 give performance summaries for both examples.
Table 9. ST72254 Example performance summary
Compiler version
Memory model
Compiler options
Code size
LIN interrupt - CPU load
Max LIN baudrate @16MHz
v4.3a
+modm - memory short
+debug
whole project
lin.o
lin_ai.o
17%
16kbaud
2.1 kbyte
1.8 kbyte
44 byte
v4.3a
+modms - memory small
+debug
whole project
lin.o
lin_ai.o
2%
no limit-20kbaud
1.4 kbyte
1.0 kbyte
44 byte
37/44
terruptable for a long time so that the LIN interrupt is called too late and an event is lost (bit/
byte). As too many parameters are involved its impossible to give generally-applicable performance data. Therefore we implemented a timing analysis feature (DEBUG_MODE) you
can activate when debugging your software.
This feature can be activated in the lib.h file using 5 symbols:
#define
DEBUG_MODE
When defined, this activates the timing debug feature. This feature configure an pin as output and
uses it to indicate when the LIN interrupt is entered
(set) and when it is left (reset).
Description
DEBUG_PxDRADD
#define
DEBUG_PxDDRADD
DEBUG_PxORADD
To configure the used I/O pin, first define these
symbols to the address of the corresponding data
register, data direction register and option register.
Description
#define
DEBUG_LIN_IT_PIN
Set this symbol to the pin number used (ex: 5 when
using PC5)
Description
The final piece of information you need to do the timing analysis is how much can the application delay the occurrence of the LIN interrupt? For this we need to go into more detail to analyze the way the software is handling each bit/byte. Once again, depending on whether the
SCI communication is emulated or not the software is works in very different ways:
4.4.2 Using the Emulated SCI
Figure 17. SCI emulation: LIN Reception
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
LIN Bus
IC OC OC OC OC OC OC OC OC OC OC
38/44
4.4.2.1 Reception
The SCI is emulated by software using the input capture and output compare of the on-chip
16-bit timer. When the bus is idle, the software waits for a negative edge: The input capture interrupt is activated and calls the LIN interrupt when a negative edge occurs. The input capture
time is used to generate an output compare in the middle of this first bit. The LIN interrupt routine returns to the calling program. When the output compare event occurs the LIN interrupt is
called again. The bus level is checked and a new output compare is set to occur in the middle
of the next bit. This last process is repeated until the stop bit.
As a result the application software should not:
delay the occurrence of the IC interrupt too much. Specifically: A problem occurs if the first
output compare is set after the expected occurrence of the output compare event, which is
the middle of the bit. So as long as the IC interrupt ends before the middle of the bit, the delay
is acceptable. See Figure 18.
Figure 18. IC Interrupt Handing in Reception Mode
start bit
1/2
1/2
application task
deadline for
end of IC interrupt
IC request IC interrupt served
Delay the occurrence of each OC interrupt too much. Specifically: A problem occurs if the
sample time defined at the beginning of the interrupt is delayed so that it occurs after the end
of the bit. See Figure 19.
39/44
application task
deadline for
start of OC interrupt
OC request
OC interrupt served
4.4.2.2 Transmission
For transmission the software only uses the Output Compare feature of the 16-bit timer. For
each bit, two OC interrupts are generated. The first one is generated to output the value of the
new bit. The second is used to read back the bus and check whether the output value is actually being sent, in other words to check for a bit error. See Figure 20.
Figure 20. SCI emulation: LIN Transmission
1 bit
1st OC:
output new bit
2nd OC:
read back
The bit transition time is precise because the timer output compare itself outputs the new level
when an output compare event occurs. Software delays do not influence the bit transitions.
The first OC sets a new OC for the middle of the bit. The second reads the LIN bus level and
prepares the next OC to output the next bit.
As a result the application software should not:
40/44
Delay the occurrence of the first OC interrupt too much. Specifically: A problem occurs if the
second output compare is set after the expected time, which is the middle of the bit. So as
long as the first OC interrupt ends before the middle of the bit, the delay is acceptable. See
Figure 21.
Figure 21. Handling the first OC Interrupt in transmission mode
1 bit
application task
deadline for
end of 1st
OC interrupt
1rst OC request
1rst OC serviced
Delay the occurrence of the second OC interrupt too much. Specifically: A problem occurs if
the sample time defined at the beginning of the interrupt is delayed outside the bit. See Figure 22
Figure 22. Handling the second OC Interrupt in transmission mode
1 bit
application task
deadline for
start of 2nd
OC interrupt
2nd OC request
2nd OC serviced
41/44
LIN Bus
4.4.3.1 Reception
The on-chip peripheral is handling the SCI communication and notifies a reception at the end
of each byte. A LIN interrupt is then generated and handles the received data according to the
LIN protocol.
Figure 24. Reception Notification Interrupt
1 byte
stop
bit
42/44
As a result the application software should not delay the occurrence of the SCI reception notification interrupt so much that an overrun condition occurs. Specifically: The end the LIN interrupt should end before the stop bit of the next byte.
Figure 25. Reception Interrupt Handling
1 byte
application task
SCI reception
interrupt request
interrupt served
4.4.3.2 Transmission
In transmission delays coming from the application cannot disturb the proper working of the
software. This will delay the transmission and the issue is more a timeout issue on the current
transmitted frame: If the interrupt time is very long the transmitted frame may exceed the maximum allowed frame time. The SCI interrupt in transmission occurs also at the stop bit. If the
occurrence of the interrupt is delayed by the application the interbyte time will increase.
5 SUMMARY OF CHANGES
Revision
Main changes
1.0
First version
1.1
Remove section HOW TO GET THE LIN SOFTWARE?
Date
August 2001
April 2002
43/44
THE PRESENT NOTE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH INFORMATION
REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, STMICROELECTRONICS
SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO
ANY CLAIMS ARISING FROM THE CONTENT OF SUCH A NOTE AND/OR THE USE MADE BY CUSTOMERS OF
THE INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
Information furnished is believed to be accurate and reliable. However, STMicroelectronics assumes no responsibility for the consequences
of use of such information nor for any infringement of patents or other rights of third parties which may result from its use. No license is granted
by implication or otherwise under any patent or patent rights of STMicroelectronics. Specifications mentioned in this publication are subject
to change without notice. This publication supersedes and replaces all information previously supplied. STMicroelectronics products are not
authorized for use as critical components in life support devices or systems without the express written approval of STMicroelectronics.
The ST logo is a registered trademark of STMicroelectronics
2002 STMicroelectronics - All Rights Reserved.
Purchase of I2C Components by STMicroelectronics conveys a license under the Philips I2C Patent. Rights to use these components in an
I2C system is granted provided that the system conforms to the I2C Standard Specification as defined by Philips.
STMicroelectronics Group of Companies
Australia - Brazil - Canada - China - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan
Malaysia - Malta - Morocco - Singapore - Spain - Sweden - Switzerland - United Kingdom - U.S.A.
http://www.st.com
44/44