How to control LIN communication 16-BIT MICROCONTROLLER F MC-16FX Family

AN704-00007-2v0-E
16-BIT MICROCONTROLLER
F2MC-16FX Family
MB96600 series
How to control LIN communication
AN704-00007-2v0-E
All Rights Reserved.
The contents of this document are subject to change without notice. Customers are advised to consult with
FUJITSU sales representatives before ordering.
The information, such as descriptions of function and application circuit examples, in this document are
presented solely for the purpose of reference to show examples of operations and uses of Fujitsu
semiconductor device; Fujitsu does not warrant proper operation of the device with respect to use based on
such information. When you develop equipment incorporating the device based on such information, you
must assume any responsibility arising out of such use of the information. Fujitsu assumes no liability for
any damages whatsoever arising out of the use of the information.
Any information in this document, including descriptions of function and schematic diagrams, shall not be
construed as license of the use or exercise of any intellectual property right, such as patent right or
copyright, or any other right of Fujitsu or any third party or does Fujitsu warrant non-infringement of any
third-party’s intellectual property right or other right by using such information. Fujitsu assumes no liability
for any infringement of the intellectual property rights or other rights of third parties which would result from
the use of information contained herein.
The products described in this document are designed, developed and manufactured as contemplated for
general use, including without limitation, ordinary industrial use, general office use, personal use, and
household use, but are not designed, developed and manufactured as contemplated (1) for use
accompanying fatal risks or dangers that, unless extremely high safety is secured, could have a serious
effect to the public, and could lead directly to death, personal injury, severe physical damage or other loss
(i.e., nuclear reaction control in nuclear facility, aircraft flight control, air traffic control, mass transport
control, medical life support system, missile launch control in weapon system), or (2) for use requiring
extremely high reliability (i.e., submersible repeater and artificial satellite).
Please note that Fujitsu will not be liable against you and/or any third party for any claims or damages
arising in connection with above-mentioned uses of the products.
Any semiconductor devices have an inherent chance of failure. You must protect against injury, damage or
loss from such failures by incorporating safety design measures into your facility and equipment such as
redundancy, fire protection, and prevention of over-current levels and other abnormal operating conditions.
If any products described in this document represent goods or technologies subject to certain restrictions on
export under the Foreign Exchange and Foreign Trade Law of Japan, the prior authorization by Japanese
government will be required for export of those products from Japan.
The company names and brand names herein are the trademarks or registered trademarks of their
respective owners.
Copyright© 2011-2012 FUJITSU SEMICONDUCTOR LIMITED all rights reserved
1
AN704-00007-2v0-E
Revision History
Rev
Date
Remark
1.0
2011.10.28
First Edition
2.0
2012.4.10
Target Products are added.
2
AN704-00007-2v0-E
Subject
Revision History...................................................................................................................... 2
Subject .................................................................................................................................... 3
Target Products ...................................................................................................................... 4
1
Introduction...................................................................................................................... 5
2
Outline of LIN .................................................................................................................. 6
2.1
What is LIN?............................................................................................................. 6
2.2
LIN specifications ..................................................................................................... 8
2.2.1
3
2.3
LIN communication flow..........................................................................................11
2.4
Communication between master and slave if an error occurs .............................. 13
Hardware Description.................................................................................................... 14
3.1
Detail of hardware environment ............................................................................. 14
3.1.1
4
Lin frame configuration ..................................................................................... 8
LIN/IF .............................................................................................................. 15
Software Description ..................................................................................................... 18
4.1
LIN Driver software overview ................................................................................. 18
4.1.1
LIN communication configuration ................................................................... 19
4.2
LIN Driver program setting ..................................................................................... 21
4.3
LIN Master Sample Program ................................................................................. 22
4.4
LIN Slave Sample program.................................................................................... 35
3
AN704-00007-2v0-E
Target Products
This application note is described about below products;
Series
MB96610
MB96620
MB96630
MB96640
MB96650
MB96670
MB96680
MB96690
MB966A0
MB966B0
MB966C0
Product Number (not included Package suffix)
MB96F612R, MB96F612A
MB96F613R, MB96F613A
MB96F615R, MB96F615A
MB96F622R, MB96F622A
MB96F623R, MB96F623A
MB96F625R, MB96F625A
MB96F633R, MB96F633A
MB96F635R, MB96F635A
MB96F636R
MB96F637R
MB96F643R, MB96F643A
MB96F645R, MB96F645A
MB96F646R
MB96F647R
MB96F653R, MB96F653A
MB96F655R, MB96F655A
MB96F656R
MB96F657R
MB96F673R, MB96F673A
MB96F675R, MB96F675A
MB96F683R, MB96F683A
MB96F685R, MB96F685A
MB96F693R, MB96F693A
MB96F695R, MB96F695A
MB96F696R
MB96F6A5R, MB96F6A5A
MB96F6A6R
MB96F6B5R, MB96F6B5A
MB96F6B6R
MB96F6C5R, MB96F6C5A
MB96F6C6R
4
AN704-00007-2v0-E
1
Introduction
This application note explains the method of control LIN Master and LIN Slave with
F2MC-16FX(MB96F615). You can apply to various applications to perform LIN control.
5
AN704-00007-2v0-E
2
Outline of LIN
2.1
What is LIN?
LIN is an acronym for Local Interconnect Network, and is a type of communications
protocol for vehicle-mounted LAN. The LIN consortium was proposed in 1999 with the
objective of enabling a less expensive configuration than CAN, which is the most
widespread control system vehicle-mounted LAN. Thereafter, after several version
upgrades, LIN2.0, which has added diagnostic and other functions, was launched in 2003.
Further, in 2006, the version was upgraded to LIN2.1.
This section explains LIN applications. Concomitant with multi-function vehicles, the
existence of a network in vehicles also became indispensable. Currently, vehicle-mounted
LANs are broadly divided into two classifications: control systems, which are concerned
with motoring and the vehicle body, and information systems, which connect devices such
as the satellite navigation system and audio, and so different LANs are used depending
on the application. In particular, vehicle body devices such as electric mirrors and power
windows, which are classified as body systems, do not require such fast or detailed
control. Consequently, they are also inexpensive. This is where LIN is used.
Accessories

Wipers

Back mirror

Switch control
Seats

Power seat and motor control

Passenger detection

Heater

Switch control
AC

Motor control

Rear panel
Rear

Rear window heater

Rear wiper

Rear blinkers
Doors

Switch control

Power windows

Mirror control
Figure 1 Example of vehicle LIN applications
6
AN704-00007-2v0-E
The characteristics of LIN used in the way described above, are collated and introduced in
the following five points.
1.
Single master communication
LIN has two types of communication nodes. One is the “master” (sender).This controls
the start of all communications. The other is the slave (recipient).The slave responds to
commands sent by the master. LIN communication must start from the master, and
cannot be started by a slave. Further, the LIN communication mode designated as the
master is pre-determined. This format is called a “single master format”.
2.
A maximum of 15 slave nodes can be connected using bus wiring.
The LIN network configuration (topology) is a bus. With single master LINs, the slaves
communicate only when they receive commands from the master, so there is no conflict
of signals in the bus. A maximum of 15 slave nodes can be connected to one master.
Master
(Door)
Communications cannot be
started from the slave!!
Communications can be
started from the master
Slave1
(Mirror)
Slave2
(Door Lock)
Slave3
(Windows)
Slave15
Up to 15 slaves max. can be connected
Transceiver IC (Electronic component for sending and
receiving data)
Figure 2 Main LIN network configuration
3.
Wiring is completed using a single wire
The on-board ECUs are connected to the LIN network via transceiver ICs (electronic
7
AN704-00007-2v0-E
components that send and receive data), and each ECU is connected on the bus from
the master to a slave. An ordinary single metal wire is used as the bus cable. CAN
combines two opposing metal wires to make one twisted pair cable. FlexRay uses two
twisted pair cables. Consequently, LIN has the advantage of using a single cable for
numerous network wires, unlike CAN and FlexRay, which use twisted pair cables.
The communications distance is 40m max. LIN can be used in combination with CAN,
and in such cases, CAN is most frequently used as the core network, and LIN is used
as the branch network.
4.
The baud rate is 20kbps max.
L The baud rate according to LIN specifications is within the range 1 to 20kbps.
Practically, the baud rate of LINs used as LANs depends on the individual vehicle
manufacturer’s system specifications, but generally one of the following is used:
2,400bps, 9,600bps, or 19,200bps.
5.
Communications errors are detected only, and subsequent processing depends on the
application
With LIN, communications errors are detected based on information as to whether
transmitting and receiving has been performed successfully. Processing after an error
has been detected, however, is not specified. Here, LIN error processing can be
customized according to the application. CAN and FlexRay management of the
communications status depends on the counter value, which is called the error counter,
is featured by the specifications, but in LIN, if an error occurs, simple error processing is
possible, in which LIN merely waits for the next command.
2.2 LIN specifications
This section explains briefly the LIN specifications.
For detailed specifications, access the LIN consortium website
(http://www.lin-subbus.org/), and register your name and e-mail address to get a
specifications.
2.2.1
Lin frame configuration
This section explains frames, which is the basic unit of LIN communication.
LIN frames are configured using “headers” and “responses”. As shown in Figure 3
8
LIN
AN704-00007-2v0-E
communication flow, the basic communications flow is a procedure in which the master
sends headers to the slaves, and the slaves implement processing according to the
contents of the headers received, and then send a response to the master.
Slave
Master
Header
Identifier
Sync byte
Break
Response
Data
Figure 3
Check sum
LIN communication flow
Further, header is configured using three fields: Sync break (Break), Sync field (Sync
byte), and ID field (Identifier), and responses are configured using two fields: Data field and
Checksum field.
Response
Header
DATA1
ID
Break
Sync Byte
ID Field
DATAN
Data Field
:Start bit(“L”)
:Stop bit(“H”)
:Interval
:Response space
:Break delimiter
:Inter-frame space
Figure 4 LIN frame configuration
9
Check Sum
Checksum Field
AN704-00007-2v0-E
1.
Break
Break, which are in the header fields, are variable-length fields that indicate the
start of a new frame. They comprise 13 to 16 “0” bits (fixed value zero) min.
2.
Sync field
Sync filed, which follow on from breaks, are 10-bit fixed-length fields that
synchronize the master and the slaves. Sync byte configurations comprise 1 starter
bit (“0”), 8 data bits, and 1 stop bit (“1”).The 8-bit data bit has the fixed value “0x55”
(which is expressed as “0x01010101” in binary).If the slave receives the 0x55 in
the sync filed send by the master normally, the master and slave are synchronized.
3.
ID field
The “ID field”, which is the final header field and comes after the synchronous byte,
is a 10-bit fixed-length field that specifies the frame type and objective. ID fields
have values from “0” to “63” (6 bits).This ID field is also used by the master to
specify individual slaves. Slaves judge what type of frame has been sent and if it
was intended for them according to the ID field sent by the master, and send
responses to the master accordingly. Further, the ID field has a 2-bit parity bit
following the “0” to “63” (6 bits).This is bracketed by a 1-bit starter bit and 1-bit stop
bit in the same way as the synchronous byte, so overall the field is 10 bits in length.
4.
Data field
The “data”, which is in the response header, is a variable-length field that literally
transfers data. The data in the number of bytes that has been predetermined (1 to
8 bytes) is sent. As there is a 1-bit start bit and 1-bit stop bit bracketing the 1-byte
data in the same way as the header synchronous byte, 1 byte of data is configured
from 10 bits. Consequently, the total data field length is “number of bytes x 10
bites”.
5.
Checksum field
The “checksum”, which follows the data, is a 10-bit fixed-length field for checking
data. The data recipient checks whether there is an error in the data by comparing
the data received with the checksum. The checksum field length is also 10 bits: a
start bit and a stop bit added to the 8-bit checksum in the same way as the
synchronous byte.
10
AN704-00007-2v0-E
2.3 LIN communication flow
In general LIN communication, one master communicates with numerous slaves. LINs,
which adopt a bus topology, connect the master and all the slaves using a single wire, so
header electrical signals sent by the master are transmitted by the wire to all the slaves.
The slaves check the frame ID, and if the header is addressed to them, sent a response to
the master according to the content received. If the header received is addressed to
another slave, it is ignored. In this way, 1-to-1 communication between the master and
each slave is achieved.
This section explains the actual trading of communications. Currently, functions are
allocated to each of the slaves from 1 to 15.The master first communicates with slave 1
and turns the motor ((1) in Figure 5 Main LIN network configuration and Figure 6 Example
of
communication
sequence
between
the master and
slaves
during normal
communication), and next acquires sensor information by communicating with slave 3.
((2) in Figure 5 Main LIN network configuration and Figure 6 Example of communication
sequence between the master and slaves during normal communication.) Thereafter, the
motor is turned by communications with slave 2 ((3) in Figure 5 Main LIN network
configuration and Figure 6 Communications sequence between master and slave during
normal communications).The master acquires sensor information from slave 3 again ((4)
in Figure 5 Main LIN network configuration and Figure 6 Communications sequence
between master and slave during normal communications), and finally turns ON the lamp
by communicating with slave 15 ((5) in Figure 5 Main LIN network configuration and
Figure 6 Communications sequence between master and slave during normal
communications).In this chain of communications, communications between the master
and slaves 2 and 3 are contiguous, and the master processes the motor turning by
communicating with slave 2 using sensor information acquired by communicating with
slave 3 first. In this way, during actual communications the master and multiple slaves
repeatedly communicate on a 1-to-1 basis.
11
AN704-00007-2v0-E
Master
Slave 1
Slave 2
Motor
Slave 3
Slave 15
Motor
Sensor
: Transceiver IC (electronic component
for sending and receiving data)
Figure 5 Main LIN network configuration
Master
Slave 1
Slave 2
Slave 3
Slave 15
Motor
operation
All communications
start from the master
Acquiring
sensor
information
Motor
operation
Acquiring
sensor
information
Lamp
ON
Figure 6 Example of communication sequence between the master and slaves during
normal communication
12
AN704-00007-2v0-E
2.4 Communication between master and slave if an error occurs
LIN error processing is not determined by the protocols, and so depends on the
application. Consequently, during design, it is necessary to consider the error detection
methods and the process after the error has been processed. As this is not determined by
the protocols in the LIN specifications either, however, examples of system design if an
error occurs are introduced in the chapter “Status Management”. In the examples
introduced, errors are managed by slaves reporting their own status to the master. This
mechanism is described below.
The basic master operation is merely to send the header to the next slave when
communications with the current slave have ended. On the other hand, the slave
operation is to perform error checking when a header is received and when a response is
sent. Checksums and other checks are implemented during reception. When sending,
checks are performed by comparing the sent data and the bus data that performs the
monitoring. In this way, the slave identifies its own status, and inserts the results into the
response that is sent to the master. The master identifies the slave status from the
response, and if there is a nonconformance, initializes the slave. In this way, the error
status is completely cleared.
13
AN704-00007-2v0-E
3
Hardware Description
The hardware function overview for LIN communication is shown Figure 7 Hardware
function overview for LIN communication. Master Unit and Slave Unit have LED for output
and Push SW and Volume SW for input each together. LIN sample software is capable of
transmitting input information of these SW to a facing unit via LIN bus and performs LED
control.
Figure 7 Hardware function overview for LIN communication
3.1
Detail of hardware environment
Evaluation environment for LIN communication control are as follows. These are provided
from Fujitsu semiconductor.
-Fujitsu Semiconductor MCU: MB96F615
-Fujitsu Semiconductor MCU Evaluation board: MB2198-760-E
-MB96610 Evaluation board: MB2198-761-01-E
Figure 8 Hardware environment for LIN communication
14
AN704-00007-2v0-E
3.1.1
LIN/IF
The circuit configuration of LIN is shown below. The LIN interface is mounted on Fujitsu
Semiconductor MCU Evaluation board by two channels. LIN power supply setting of LIN
transceiver (MAX13020) can be selected by LIN_PWR and can be chosen from internal
power (default) or external power. It is necessary to short JP46 to use for LIN Master.
Figure 9 LIN I/F circuit configuration
Figure 10 LIN I/F pin assigns (D-SUB9)
15
AN704-00007-2v0-E
Figure 11 LIN I/F connector
LIN communication environment image is shown below. LIN USART channel 7 is used for
LIN communication in LIN sample software. In the evaluation environment of the MB96610
series, LIN-USART channel 7 is connected the LIN ch.0 of MB2198-760-E.
Figure 12 Top View of LIN communication environment
16
AN704-00007-2v0-E
Figure 13 Connection of LIN
17
AN704-00007-2v0-E
4
Software Description
The project software can be separated into two main parts. The first part comprises the
LIN driver control, the second part is application program.
Figure 14 Project Folder
4.1
LIN Driver software overview
The functions of LIN driver software provided are as follows.
- Enable to change LIN Master and LIN Slave function
- Selecting Baud rate (2400/9600/19200bps)
- Enable to add new LIN message ID and DATA easily
- Synchronization control in sync field (LIN slave only)
- Wake up output (LIN slave only)
- Application software including SW and Analog input and LED control
The peripherals used in LIN sample software is shown below.
Figure 15 The peripherals used in LIN sample software
18
AN704-00007-2v0-E
4.1.1
LIN communication configuration
The LIN communication parameters used by the sample program are summarized in Table 1
LIN communication conditions of the sample program.
Table 1 LIN communication conditions of the sample program
Condition
Value
Baud rate
2400/9600/19200bps
Input frequency
4MHz
Peripheral
16MHz(PLL on)
Clock2(CLKP2)
for LIN Clock
13 bit (Reception is fixed to
Sync Break length
detect 11 bits)
Data length
8bit
Data bit format
LSB first
Data byte count
8Byte
The LIN message ID used in LIN communication are described in “Table 2
LIN message
ID and data in the sample program”. Even number of LIN ID is used for data transmit from
master to slave, and odd number of LIN ID is used for data transmit from slave to master.
19
AN704-00007-2v0-E
Table 2
LIN message ID and data in the sample program
※H: Header R: Response
Contents of Data for each LIN ID are below.
○LIN ID:0x00
byte 0:Get command of Slave AD value (If byte0 is 0x55, command is active)
byte 1:Analog value of LIN Master volume SW
○LIN ID:0x01
byte 2:Analog value of LIN Slave volume SW
○LIN ID:0x02
byte 0:Control command of LED01(If byte0 is 0x55, command is active)
byte 1:LIN Master SW4 count value
○LIN ID:0x03
byte 2:LIN Slave SW4 count value
○LIN ID:0x04
byte 0:Control command of LED23(If byte0 is 0x55, command is active)
byte 3:LIN Master SW7 count value
○LIN ID:0x05
byte 4:LIN Slave SW7 count value
20
AN704-00007-2v0-E
4.2
LIN Driver program setting
This sample program can operate as both LIN Master and LIN slave. Please select the
function definition in lindmsg.h.
○In case of using LIN Master: #define
LIN_MASTER 1
○In case of using LIN Slave: #define LIN_MASTER 0
/*===============================================================*/
/*Function definition
*/
/*
BASE_TIME:
5[ms]
*/
/*
LIN_MASTER:
0:SLABE NODE, 1:MASTER NODE
*/
/*
LINUART_CH:
7
*/
/*
EXTERNAL_INT:
10
*/
/*
FREE_RUN_TIMER_CH
1
*/
/*
OUTPUT_COMPARE_CH
6
*/
/*===============================================================*/
#define
BASE_TIME
5
#define
LIN_MASTER
1
#define
LINUART_CH
7
#define
EXTERNAL_INT
10
#define
FREERUN_TIMER_CH
1
#define
OUTPUT_COMPARE_CH
6
Figure 16 Selection of LIN Node(lindbmsg.h)
As for baud rate setting, you can select three type of baud rate setting by changing baud
rate definition in lindmsg.h.
○In case of 2400 bps: #define
BAUD_RATE
2400
○In case of 9600 bps: #define
BAUD_RATE
9600
○In case of 19200 bps: #define BAUD_RATE 19200
/*========================================================*/
/*
BAUDRATE Definition (2400/9600/19200)
/*========================================================*/
#define
BAUD_RATE
9600
#define
BAUD_RATE_ADJUST
1
Figure 17 Changing of baud rate
21
*/
AN704-00007-2v0-E
4.3
LIN Master Sample Program
The flowcharts for the LIN communication in the sample program are shown in Figure 18
LIN communication flowchart (main routine) and Figure 19 LIN communication flowchart
(LIN USART receive interrupt routine). First, the microcontroller is initialized, the LIN-USART
is initialized, and the timer is initialized. Next, the bus connection processing is performed as
the LIN master, and the schedule is set. After this, the program enters a loop. Within the loop,
headers are sent and responses are sent and received at fixed intervals. Sending of the
sync break, sync field, and ID field headers and sending and receiving of responses is
processed by the LIN-USART receive interrupt. Processing is performed in response to the
master ID (identifier).
START
Microcontroller
initialization
LIN-USART
initialization
Timer
initialization
LIN bus connection
processing
Set schedule
Infinite loop
Figure 18 LIN communication flowchart (main routine)
22
AN704-00007-2v0-E
USART receive
interrupt occurred
Start USART receive
interrupt processing
Send ID field
Receive ID?
No
Yes
Data receive
processing
Data send
processing
Checksum receive
processing
Checksum send
processing
Send sync break
and sync field
End USART receive
interrupt processing
Figure 19 LIN communication flowchart (LIN USART receive interrupt routine)
23
AN704-00007-2v0-E
ID field receive
START Application program
for LIN ID
ID=0x00 ?
Yes
AD conversion
Transmit 0x55 and
AD value
No
ID=0x01 ?
Yes
No
ID=0x02 ?
Get Slave AD
value and change
LED4-7 Output
Yes
No
Get Master SW4
count
Transmit 0x55 and
Master SW4 count
Yes
ID=0x03 ?
No
ID=0x04 ?
Get Slave SW4 count
and change LED01
output
Yes
No
Get Master SW7
count
Transmit 0x55 and
Master SW7 count
Yes
ID=0x05 ?
Get Slave SW7 count
and change LED23
output
No
Yes
ID=0x06 ?
No
Transmit 0x66
(Sample Data)
Yes
ID=0x07 ?
No
Transmit 0x77
(Sample Data)
End Application program
for LIN ID
Figure 20 LIN communication flowchart (data processing by ID)
24
AN704-00007-2v0-E
The points where this sample program operates in the LIN protocol during LIN
communication are shown below. Because the sample program is the LIN master, the LIN
bus connection processing and schedule registration are performed first.
void
main(void)
{
(omitted)
l_ifc_connect(hLIN_NORMAL_WAKEUP);
← LIN bus connection
l_sch_set(hSchedule1, Schedule1_DATA00);
←Set schedule
Figure 21 LIN bus initial settings
Parts of the section that sets the LIN ID and sets the schedule is shown below.
In the sample program, one schedule table and eight IDs are used. The IDs that actually use
a response are ID 0x00 to ID 0x05.
typedef
enum {
←Register 8 IDs
Schedule1_DATA00 = 0,
Schedule1_DATA01,
Schedule1_DATA02,
Schedule1_DATA03,
Schedule1_DATA04,
Schedule1_DATA05,
Schedule1_DATA06,
Schedule1_DATA07,
(omitted)
#define
Schedule1Count
←Register 8 IDs
8
(omitted)
__far const
←Register 8 IDs
l_u8
Schedule1_IdList[Schedule1Count] =
{
ID_00, ID_01, ID_02, ID_03, ID_04, ID_05, ID_06, ID_07
Figure 22 LIN ID registration – Lindbmaster.h
25
};
AN704-00007-2v0-E
← Register send response
l_u8*
__far const
/*
0
/* 0*/
ucDATA00, 0, ucDATA02, 0, ucDATA04, 0, ucDATA06, 0, 0, 0,
/*10*/
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1
2
LinTxDataPtr[64] = {
3
4
5
6
7 8
9
*/
(omitted)
← Register receive response
l_u8*
__far const
/*
0
/* 0*/
0, ucDATA01, 0, ucDATA03, 0, ucDATA05, 0, ucDATA07, 0, 0,
1
2
Figure 23
LinRxDataPtr[64] = {
3
4
5
6
7 8
9
*/
Transmit and receive response registration – Lindbmsg.h
This sample program operates by processing multiple interrupts, as shown in Figure 24
Points where the processing of each interrupt is performed. We will now look at the
processing performed by the sample program for each field of the LIN protocol.
HEADER
Sync Break
RESPONSE
Sync Field
ID Field
Data Field
ID
Sync break detect
interrupt
Figure 24
LIN receive
interrupt
(receive sync field)
DT
LIN receive
LIN receive
interrupt Freerun Timer
interrupt
(receive ID)
(receive Data)
interrupt
DT
LIN receive
interrupt
(receive Data)
Check Sum
LIN receive interrupt
(receive Checksum)
Points where the processing of each interrupt is performed
1. Sync break
On sync break, a sync break signal (a low signal for 13 to 16 bits) is sent. In the
sample program, 13 bit are sent. Receive processing is also performed at the same
time, and if the bus is “0” for 11 bit times or more, a sync break interrupt (LBD) occurs.
After a sync break interrupt is detected, the sync break interrupt is set to disabled.
26
AN704-00007-2v0-E
Serial clock cycle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Transmit DATA
Sync break
LBR LIN LIN
Synch
break生成ビット
synch
break
generate bit
0
1
影響なし
No
effect
LIN SynchLIN
break
生成break
Generate
synch
Figure 25
__interrupt void
LBL0
LBL1
LIN
Synch
break長選択ビット
LIN
synch
break
length selection bits
0
0
13ビット分
13
bits long
1
0
14ビット分
14
bits long
0
1
15ビット分
15
bits long
1
1
16ビット分
16
bits long
Sync break data setting
_LinUartRx(void)
{
← Error check
if ((ssr & 0xE0) != 0) {
(omitted)
} else if (IO_UART7_ESCR7_bit_LBD == SET) {
#if
← Sync break detection
(LIN_MASTER==1)
IO_UART7_ESCR7_bit_LBD = CLEAR;
← Clear sync break detection flag
(omitted)
}else{
← Receive processing
l_ifc_rx(data);
Figure 26
Sync break interrupt control
2. Sync field
After a sync break is detected, processing is performed to send and receive the
synch field. 0x55 is sent in the synch field. If this data is successfully received by the
slave, the slave becomes synchronized.
27
AN704-00007-2v0-E
Sync field
START 0 1 2 3 4 5 6 7 STOP
BIT
BIT
DATA = 0x55
8Tbit
2Tbit
2Tbit
2Tbit
2Tbit
First
Time
Fifth
Time
Figure 27
Sync field interrupt control
Sync field reception is performed in l_ifc_rx(l_ifc_handle rx_data). The processing for sync
break reception, sync field reception, ID reception, DATA transmission and reception, and
WAKEUP transmission are split up according to the status,
void
l_ifc_rx(l_ifc_handle rx_data)
{
switch(ucLinStatus){
case
← Transmit DATA FIELD
LIN_TRANSMIT:
(omitted)
case
← Receive DATA FIELD
LIN_DATA_RECEPTION:
(omitted)
case
← Wait to receive ID FIELD
LIN_ID_RECEPTION:
(omitted)
case
← Wait to receive Synch field
LIN_WAIT_SYNCH_FIELD:
(omitted)
case
←Sync break 受信待ち
LIN_MS_WAIT_SYNCH_BREAK:
IO_UART7_ESCR7_byte = 0x00;
← Clear ESCR register
(omitted)
ucLinStatus = LIN_WAIT_SYNCH_FIELD;
case
← Switch to wait to receive
(omitted)
Sync field state
LIN_WAKEUP_TRANSMIT:
← Transmit WAKEUP state
(omitted)
}
Figure 28
Processing to determine whether synch break was received
28
AN704-00007-2v0-E
In Figure 29 sync field receive processing, once it has been confirmed that the data
is 0x55, the program enters a state of waiting to receive the ID field and performs ID
send processing.
void
l_ifc_rx(l_ifc_handle rx_data)
{
(omitted)
case
LIN_WAIT_SYNCH_FIELD:
← Wait to receive sync field
if (rx_data == SYNCH_FIELD_CHAR) {
← Check if Sync field is 0x55
ucLinStatus = LIN_ID_RECEPTION;
← Switch to wait to receive ID
l_ifc_tx(ucLinMsScheduleCurrentId);
← Transmit ID processing
(omitted)
(omitted)
Figure 29 sync field receive processing
The transmit data is stored in a register within the send start processing function
l_ifc_tx(l_ifc_handle tx_data) as shown in Figure 30 USART transmit start processing.
void
l_ifc_tx(l_ifc_handle tx_data)
{
#if
(LINUART_CH == 3)
← Set transmit data in register
IO_UART3_RDR3 = tx_data;
(omitted)
Figure 30 USART transmit start processing
3. ID field
In order to transition to the state of waiting to receive the ID field, the ID receive
determination processing is performed in the normal sequence in the ID receive
processing, as shown in Figure 31
ID receive determination processing. In the ID
receive determination processing, a judgment is made as to whether the acquired ID
is for sending or for receiving and a parity check is performed. If the ID is for sending,
the status is changed to the send preparation state and the send data is copied to a
29
AN704-00007-2v0-E
buffer. If the ID is for receiving, the status is changed to the DATA receive wait state
in preparation for receiving a response (data) from the slave.
void
l_ifc_rx(l_ifc_handle rx_data){
(omitted)
case
← Wait to receive ID FIELD state
LIN_ID_RECEPTION:
ucCurrentId.byte
← Store received ID
= rx_data;
if( ucCurrentId.fields.parity != ucRightParity[ucCurrentId.fields.id] ) {
← Parity check
← Error processing
l_flg_tst(hBIT_ERR);
l_flg_clr(hBIT_ERR);
(omitted)
} else if( LinRxDataPtr[ucCurrentId.fields.id] != 0 ) { ← If receive ID
ucLinStatus = LIN_DATA_RECEPTION;
← DATA receive wait state
(omitted)
vSetLinFreerunTimersCompare(ucRxCount);
← Set Freerun timer
} else if ( LinTxDataPtr[ucCurrentId.fields.id] != 0 ) { ← If transmit ID
(omitted)
↓ Copy send data to buffer
vLinWordCopy(ucUartTxBuffer, LinTxDataPtr[ucCurrentId.fields.id], ucTxCount);
vSetLinFreerunTimersCompare(hTINFRAME_SPACE_IND);← Set Freerun timer
}
(omitted)
}
}
Figure 31
ID receive determination processing
30
AN704-00007-2v0-E
4. DATA field
This section explains data transmission and reception in the DATA field.
As for Data transmission, if the receive ID is for transmit, vTimeoutCheckTask
function for timeout process is called by detecting freerun timer interrupt at first. This
function is called when the timeout value configured in the free-running timer is
detected, and in this case, the function is called when the timeout value from
receiving the header to sending the response (response space) is detected.
The vTimeoutCheckTask function is divided into send pre-processing, initialization
processing, etc. depending on the status information. When the status is the send
pre-processing state, the first byte of the data is sent.
void
vTimeoutCheckTask(void){
(omitted)
if ( uiIntDemandCounter == 0 ) {
switch ( ucLinStatus ) {
case
← Pre-transmit state
LIN_PRETRANSMIT:
ucLinStatus = LIN_TRANSMIT;
← State transition: Transmit DATA FIELD state
ucSaveData = ucUartTxBuffer[0];
← Transmit data: Acquire 1 byte
l_ifc_tx(ucUartTxBuffer[0]);
← Data transmit processing
(omitted)
case
LIN_UART_INITIAL:
(omitted)
case
LIN_ID_RECEPTION:
(omitted)
case
LIN_DATA_RECEPTION:
(omitted)
case
LIN_TRANSMIT:
(omitted)
case
LIN_WAIT_SYNCH_FIELD_START:
(omitted)
}
}
Figure 32
Timeout detection processing
31
AN704-00007-2v0-E
When the first byte of data is sent, a receive interrupt occurs due to receiving the data
that the program itself sent. Therefore, in the same way as the operation for the ID
field, the receive determination processing function _ifc_rx(l_ifc_handle rx_data) is
called, and processing to send the second and subsequent data is performed based
on the send state of the DATA FIELD as shown in Figure 33 DATA transmit
processing, with this same process repeated over and over. Because the data byte
count is set to 8 in the current LIN communication, after the 8th bytes of DATA has
been sent the Checksum is finally sent and the send processing finishes.
void
l_ifc_rx(l_ifc_handle rx_data){
switch(ucLinStatus){
case
LIN_TRANSMIT:
← Transmit DATA FIELD
if ( ucTxCurrentIndex < ucTxCount ){
← If transmit DATA remains
(omitted)
l_ifc_tx(ucUartTxBuffer[ucTxCurrentIndex]);
← Transmit processing
(omitted)
} else if ( ucTxCurrentIndex == ucTxCount ){
← If all transmit data has been sent
(omitted)
l_ifc_tx(((unsigned char)~uiTxCheckSum));
← Transmit checksum
(omitted)
}
case
LIN_DATA_RECEPTION:
(omitted)
case
LIN_ID_RECEPTION:
(omitted)
case
LIN_WAKEUP_TRANSMIT:
(omitted)
}
}
Figure 33 DATA transmit processing
32
AN704-00007-2v0-E
If the ID acquired in the ID receive processing is for receiving, the status is changed
to the DATA receive state and the program waits to receive data from the bits pot
yellow. If an interrupt occurs due to receiving data from the bits pot yellow, the receive
processing is performed within the receive processing function l_ifc_rx(data) as
shown in Figure 34
DATA receive processing. For the case of receiving data, the
receive processing is performed by l_ifc_rx(l_ifc_handle rx_data) for each single byte
of data received, and once all 8 bytes of data have been received, the receive
success flag is set if there are no checksum errors and the receive processing
finishes.
void
l_ifc_rx(l_ifc_handle rx_data){
switch(ucLinStatus){
case
LIN_TRANSMIT:
(omitted)
case
← Receive DATA FIELD
LIN_DATA_RECEPTION:
if ( ucRxCurrentIndex >= ucRxCount ) {
← If all data has been received
if ( (uiRxCheckSum + rx_data) == 0xFF ) {
← If checksum calculation is correct
(omitted)
flagsLinTxRx.bit.SucceedReception = SET;
← Set receive success flag
memcpy( &ucUartRxFixedBuffer[0], &ucUartRxBuffer[0], ucRxCount );
↑ Copy received data
(omitted)
← If there is a checksum error
} else {
l_flg_tst(hCHECKSUM_ERR);
← Error processing
← If there is receive data remaining
} else {
ucUartRxBuffer[ucRxCurrentIndex] = rx_data;
(omitted)
case
LIN_ID_RECEPTION:
(omitted)
case
LIN_WAKEUP_TRANSMIT:
(omitted)
}
}
Figure 34
DATA receive processing
33
← Store receive data in buffer
AN704-00007-2v0-E
Finally, the vBaseTimeTask function in main.c checks whether sending and receiving has
finished primarily. If this function is called when all of the data reception has finished
(flagsLinTxRx.bit.SucceedReception is set), the sub_control function in submain.c is called
and perform application program shown in Figure 20 LIN communication flowchart (data
processing by ID).
34
AN704-00007-2v0-E
4.4
LIN Slave Sample program
The LIN communication flowcharts for the sample programs are shown in Figure 35 LIN
communication flowchart (main routine) and Figure 36 LIN communication flowchart (LIN
USART receive interrupt routine). First, initialize the microcontroller, LIN-USART, and timer.
Next, implement LIN bus connection processing as a LIN slave. Thereafter, the program
enters a loop. Within the loop, monitor whether the data being sent and received can be
completed in a fixed cycle, and when the data has finished being received, implement
processing according to the ID. Synch break detection, ID reception, and data sending and
receiving to operate as a LIN slave is processed using LIN-UART reception interrupts.
Further, the baud rate is adjusted within the input capture interrupts as described in Figure
37 LIN communication flowchart (Input Capture interrupt).
START
Microcontroller
initialization
LIN-USART
initialization
Timer
initialization
LIN bus connection
processing
Set schedule
Infinite loop
Figure 35 LIN communication flowchart (main routine)
35
AN704-00007-2v0-E
USART receive
interrupt occurred
Start USART receive
interrupt processing
Send ID field
Receive ID?
No
Yes
Data receive
processing
Data send
processing
Checksum receive
processing
Checksum send
processing
Send sync break
and sync field
End USART receive
interrupt processing
Figure 36 LIN communication flowchart (LIN USART receive interrupt routine)
36
AN704-00007-2v0-E
Occur input capture
interruption
Start input capture
interrupt process
Get value of ICU Timer1
Get value of ICU Timer2
Baud rate adjust
End input capture
interrupt process
Figure 37 LIN communication flowchart (Input Capture interrupt)
37
AN704-00007-2v0-E
ID field reception
Start Application program for
LIN ID
Yes
ID=0x00 ?
ucDATA00[0]
== 0x55 ?
No
No
ID=0x01 ?
Get Master AD
value and change
LED4-7 Output
Yes
No
ID=0x02 ?
Yes
Transmit Slave
AD value
Yes
No
ucDATA02[0]
== 0x55 ?
Yes
No
Get Master SW4 count
And change LED01
output
Yes
ID=0x03 ?
No
Get Slave SW4
count
Transmit Slave
SW4 count
No
ID=0x04 ?
Yes
No
ucDATA04[0]
== 0x55 ?
Get Master SW7
count And change
LED23 output
No
ID=0x05 ?
Yes
No
Get Slave SW7
count
Transmit Slave
SW7 count
Yes
ID=0x06 ?
No
Transmit 0x66
(Sample Data)
Yes
ID=0x07 ?
No
Transmit 0x77
(Sample Data)
End Application program
for LIN ID
Figure 38 LIN communication flowchart (data processing by ID)
38
AN704-00007-2v0-E
This sample program operates by processing multiple interrupts, as shown in Figure 39
Points where the processing of each interrupt is performed Points where the processing of
each interrupt is performed. We will now look at the processing performed by the sample
program for each field of the LIN protocol.
RESPONSE
HEADER
Sync Break
Sync Field
ID Field
Data Field
ID
Sync break detect Input capture Input capture
interrupt
interrupt
interrupt
DT
DT
LIN receive
LIN receive
interrupt
interrupt Freerun Timer
(receive Data)
(receive ID)
interrupt
LIN receive
interrupt
(receive Data)
Check Sum
LIN receive interrupt
(receive Checksum)
Figure 39 Points where the processing of each interrupt is performed
1.
Sync break
In sync break field, LIN slave receive sync break signal (13 to16-bit Low signals) from
Master and when the bus is “0” in the 11-bit time or greater, a sync break interrupt is
created. When a sync break interrupt is detected, the sync break interrupt prohibition
settings and input capture interrupts are authorized, and the system migrates to waiting
for the synch field to start.
__interrupt void
_LinUartRx(void)
{
←Error check
if ((ssr & 0xE0) != 0) {
(Omitted)
} else if (IO_UART7.ESCR7.bit.LBD==SET {
←Sync break detection
IO_UART7.ESCR7.bit.LBD = CLEAR;
←Clear sync break detection flag
(Omitted)
vSetLinFreerunTimersCompare(hTHEADER_MAX_IND);
ucLinStatus = LIN_WAIT_SYNCH_FIELD_START;
(Omitted)
←State transition:
Wait sync field start
}
Figure 40 Sync break interrupt control
39
←FreeRun Timer setting
AN704-00007-2v0-E
2.
Sync field
After sync break has detected, LIN slave measure the baud rate using input capture
in the sync field and perform compensation. In the sample software, input capture is
set to detect both edges and input capture source is set to LIN USART channel 7.
When the input capture interrupts are set to enable and both edge detection, when
an edge is detected, an input capture interrupt is created. The timer value at both
edges and the number of overflows are measured, and the baud rate calculated and
adjusted using interrupts at 8.
Sync field
START 0 1 2 3 4 5 6 7 STOP
BIT
BIT
DATA = 0x55
8Tbit
2Tbit
2Tbit
2Tbit
2Tbit
First
Time
Fifth
Time
Internal
Signal
FFH
uiICUTime2
・・・・
uiICUTime1
Figure 41 Input capture operation in the synch field
40
AN704-00007-2v0-E
__interrupt void
_LinICU (void)
{
if (ucLinStatus == LIN_WAIT_SYNCH_FIELD_START){
←Synch field start wait
←Acquire timer value
uiICUTime1 = IO_IPCP9;
(Omitted)
ucLinStatus =
LIN_WAIT_SYNCH_FIELD_END;
else if(ucLinStatus == LIN_WAIT_SYNCH_FIELD_END){
←State transition:
sync field end wait
(Omitted)
←Acquire timer value
uiICUTime2= IO_IPCP9;
/* adjust Boud Rate
*/
(Omitted)
←Baud rate adjustment
vEnableLinUartReception();
←LIN-UART interrupt authorized
←State transition: ID
ucLinStatus = LIN_ID_RECEPTION;
}
}
Figure 42 Input capture interrupt controls
3.
ID field
ID reception processing is performed in the LIN-USART interrupt function
_LinUART(void).When an interrupt is created, if no error is created and the cause of the
interrupt isn’t a sync break interrupt, reception processing is performed.
__interrupt void
_LinUART (void)
{
(Omitted)
if ((ssr & 0xE0) != 0) {
←Error check
(Omitted)
} else if (ESCR_LBD == SET) {
←Sync break detection
(Omitted)
}else{
l_ifc_rx(data);
←Receive processing
}
}
Figure 43 LIN-USART receive interrupt control
41
AN704-00007-2v0-E
Processing is divided into ID reception, data sending, data reception, and wakeup
sending
according
the
status
in
the
reception
judgment
processing
function
l_ifc_rx(l_ifc_handle rx_data) as shown in Figure 44 ID receive determination processing. In
normal sequences, to migrate the status during the second input capture interrupt process
to ID FIELD reception waiting, ID reception processing is performed. In ID reception
processing, the ID that has been acquired is judged to be either a send ID or reception ID
and parity check performed, and if it is a send ID, the status is migrated to send preparation
status, and the data to be sent is copied to the buffer. If the ID is a reception ID, the status is
migrated to data reception wait status, and a response (data) is received from the master.
42
AN704-00007-2v0-E
void
l_ifc_rx(l_ifc_handle rx_data){
switch(ucLinStatus){
case
←DATA FIELD transmit status
LIN_TRANSMIT:
(Omitted)
case
LIN_DATA_RECEPTION:
← DATA FIELD reception status
(Omitted)
case
← ID FIELD reception wait status
LIN_ID_RECEPTION:
ucCurrentId.byte
←Store received ID
= rx_data;
←Parity check
if( ucCurrentId.fields.parity != ucRightParity[ucCurrentId.fields.id] ) {
←Error processing
(Omitted)
else if( LinRxDataPtr[ucCurrentId.fields.id] != 0 ) {
ucLinStatus = LIN_DATA_RECEPTION;
←If ID is for reception
←State transition: DATA reception
wait status
(Omitted)
vSetLinFreerunTimersCompare(ucRxCount);
←Freerun Timer set
} else if ( LinTxDataPtr[ucCurrentId.fields.id] != 0 ) {
ucLinStatus = LIN_PRETRANSMIT;
(Omitted)
↓Copy send data to buffer
vLinWordCopy(ucUartTxBuffer, LinTxDataPtr[ucCurrentId.fields.id], ucTxCount);
vSetLinFreerunTimersCompare(hTINFRAME_SPACE_IND);
set
}
(Omitted)
case
←Freerun Timer
LIN_WAKEUP_TRANSMIT:
←WAKEUP send status
(Omitted)
}
}
Figure 44
ID receive determination processing
43
AN704-00007-2v0-E
4.
DATA field
This section explains data transmission and reception in the DATA field.
As for Data transmission, if the receive ID is for transmit, vTimeoutCheckTask
function for timeout process is called by detecting freerun timer interrupt at first. This
function is called when the timeout value configured in the freerun timer is detected.
In this case, the function is called when the timeout value from receiving the header
to sending the response (response space) is detected.
The vTimeoutCheckTask function is divided into send pre-processing, initialization
processing, etc. depending on the status information. When the status is the send
pre-processing state, the first byte of the data is sent.
void
vTimeoutCheckTask(void){
(Omitted)
if ( uiIntDemandCounter == 0 ) {
switch ( ucLinStatus ) {
case
← Pre-transmit state
LIN_PRETRANSMIT:
ucLinStatus = LIN_TRANSMIT;
←State transition: Transmit DATA FIELD
ucSaveData = ucUartTxBuffer[0];
←Transmit data: Acquire 1 byte
l_ifc_tx(ucUartTxBuffer[0]);
←Data transmit processing
(Omitted)
case
LIN_UART_INITIAL:
(Omitted)
case
LIN_ID_RECEPTION:
(Omitted)
case
LIN_DATA_RECEPTION:
(Omitted)
case
LIN_TRANSMIT:
(Omitted)
case
LIN_WAIT_SYNCH_FIELD_START:
(Omitted)
}
}
Figure 45 Timeout detection processing
44
AN704-00007-2v0-E
When sending the first data byte, a reception interrupt is created by receiving the
self-sent data. Whereupon, the reception judgment processing function
_ifc_rx(l_ifc_handle rx_data) is called in the same way as for ID field operations, and
the data is sent from the second byte onwards according to the data field send status
as shown in Figure 46 DATA transmit processing, and the same process is repeated.
In this LIN communication, the number of data bytes is set to 8, so when the eighth
data byte has finished being sent, finally a checksum is sent, and the send
processing ends.
void
l_ifc_rx(l_ifc_handle rx_data){
switch(ucLinStatus){
case
LIN_TRANSMIT:
←DATA FIELD transmit status
if ( ucTxCurrentIndex < ucTxCount ){
←If any transmit data is remaining
(Omitted)
l_ifc_tx(ucUartTxBuffer[ucTxCurrentIndex]);
←Data transmit processing
(Omitted)
} else if ( ucTxCurrentIndex == ucTxCount ){
←If transmit data has all been sent
(Omitted)
l_ifc_tx(((unsigned char)~uiTxCheckSum));
←Checksum transmit processing
(Omitted)
}
case
LIN_DATA_RECEPTION:
(Omitted)
case
LIN_ID_RECEPTION:
(Omitted)
case
LIN_WAKEUP_TRANSMIT:
(Omitted)
}
}
Figure 46 DATA transmit processing
45
AN704-00007-2v0-E
If the ID acquired in the ID receive processing is for receiving, the status is changed
to the DATA receive state and the program waits to receive data from the bits pot
yellow. If an interrupt occurs due to receiving data from the bits pot yellow, the receive
processing is performed within the receive processing function l_ifc_rx(data) as
shown in Figure 47 DATA receive processing. For the case of receiving data, the
receive processing is performed by l_ifc_rx(l_ifc_handle rx_data) for each single byte
of data received, and once all 8 bytes of data have been received, the receive
success flag is set if there are no checksum errors and the receive processing
finishes.
void
l_ifc_rx(l_ifc_handle rx_data){
switch(ucLinStatus){
case
LIN_TRANSMIT:
(Omitted)
case
← Receive DATA FIELD
LIN_DATA_RECEPTION:
if ( ucRxCurrentIndex >= ucRxCount ) {
← If all data has been received
if ( (uiRxCheckSum + rx_data) == 0xFF ) {
← If checksum calculation is correct
(Omitted)
flagsLinTxRx.bit.SucceedReception = SET;
← Set receive success flag
memcpy( &ucUartRxFixedBuffer[0], &ucUartRxBuffer[0], ucRxCount );
↑Copy received data
(Omitted)
← If there is a checksum error
} else {
l_flg_tst(hCHECKSUM_ERR);
← Error processing
← If there is receive data remaining
} else {
ucUartRxBuffer[ucRxCurrentIndex] = rx_data;
(Omitted)
case
LIN_ID_RECEPTION:
(Omitted)
case
LIN_WAKEUP_TRANSMIT:
(Omitted)
}
}
Figure 47 DATA receive processing
46
← Store receive data in buffer
AN704-00007-2v0-E
Finally, the vBaseTimeTask function in main.c checks whether sending and receiving has
finished primarily. If this function is called when all of the data reception has finished
(flagsLinTxRx.bit.SucceedReception is set), the sub_control function in submain.c is called
and perform application program shown in Figure 38 LIN communication flowchart (data
processing by ID).
47