0:09
hello and welcome to controllers Tech
0:12
this is the eighth video in the SDM 32
0:15
uart series and today we will start a
0:17
minseries on the linbus the Lin stands
0:21
for local interconnect Network and just
0:23
like the can protocol it is also majorly
0:26
used in the automation industry compared
0:29
to to the can protocol it offers lower
0:32
performance speed and reliability but it
0:37
drastically I have made a separate
0:39
playlist for the Lin protocol and the
0:41
first video in the playlist explains
0:43
what this protocol is and where and why
0:46
it is used do watch this video if you
0:49
don't know what the Lin protocol is
0:52
today in this video we will program our
0:55
SDM 32 MCU as the Lin
0:58
master I am not going to connect any
1:00
transceiver in today's video we will
1:03
simply see how to send the data in the
1:06
format in the next tutorial we will
1:09
connect an actual Lin transceiver and
1:11
receive the data using another
1:14
slave so today we will just focus on the
1:18
formatting the Lin protocol uses the U
1:21
peripheral to transmit the data so we
1:23
need to arrange the data in a proper
1:26
format so that it is recognized as a
1:30
I am going to focus on the Lin version
1:32
2.1 as it is the most recent one here I
1:37
have the Lin specification package for
1:41
2.1 we will just see some important
1:43
things in this specification
1:46
package here on page number 28 you can
1:49
see a typical Lin frame a linf frame
1:53
consists of a header and the
1:55
response the header further has the
1:57
brake field syn field and the protected
2:02
field the response field consists of the
2:05
data bytes which can be up to 8 bytes
2:08
and the check sum bite we will basically
2:11
program our MCU to send this Frame and
2:14
then read it on the logic
2:16
analyzer let's start the QBE ID and
2:21
project I am using the stm32f 446
2:26
re let's give some name to the project
2:30
finish I will start with the clock setup
2:34
first the MCU will use the Crystal from
2:37
the St link so bypass it here the board
2:41
has 8 MHz Crystal on it and let's run
2:43
the system at maximum 80
2:51
MHz I am going to use the usart one so
2:54
let's enable the usart one in the Lin
2:57
mode the pin PA 9 is the uart TX pin and
3:02
P 10 is the RX pin note that Linn is a
3:06
single wire protocol but we still have
3:10
uart this is because we can't use the
3:12
MCU directly to the linbus instead we
3:15
would need to use some transceiver in
3:17
the middle the Lin buus used in cars
3:20
generally uses the 12vt signals hence
3:24
the use of a transceiver is
3:26
necessary I will connect a Lin
3:28
transceiver in the next tutorial but
3:31
today we will just focus on the TX pin
3:34
the data sent by the MCU will be via
3:37
this pin so we will monitor this pin on
3:40
analyzer the Lin protocol uses the board
3:43
rate of up to 20 kilobits per second but
3:46
I am using the board rate of
3:48
9,600 here the word length is 8 Bits
3:52
with one stop bit and no
3:55
parity the brake detect length is the
3:58
length of the brake field which is set
4:00
to 10 bits this is used in transmitting
4:03
as well as in the reception that is all
4:06
the configuration we need for now click
4:09
save to generate the project let's
4:12
define a TX buffer where we will store
4:14
the data to be sent the brake will be
4:17
sent by the uart peripheral itself so we
4:20
will send the sync field first the syn
4:23
field consists of the fixed value 55 hex
4:27
after sending the SN field we need to
4:30
send the protected ID the protected ID
4:33
consists of the six bits ID followed by
4:36
the two par bits here you can see the
4:39
table containing the protected IDs for
4:42
the equivalent ID fields for example if
4:46
the ID is zero the equivalent protected
4:49
ID will be 80 hex and if the ID is 19
4:54
the protected ID will be D3 hex you can
4:58
directly use this table to find the
5:00
required protected idid for the
5:02
equivalent ID field but I will write a
5:04
code to convert the ID to the protected
5:07
ID as I mentioned the protected ID
5:11
consists of the six bits ID along with
5:15
bits since we have only six bits for the
5:18
ID the maximum value of the ID allowed
5:24
fhx now to find the parity bit p 0 we
5:28
have to perform the exor oper ation
5:30
between the ID bits 0 1 2 and 4 and to
5:35
calculate the parity bit P1 we have to
5:38
first perform the exor operation between
5:40
the ID bits 1 3 4 and five and then
5:46
obtained let's write the code to do this
5:50
the function P ID Cal will take the
5:54
parameter the maximum allowed ID is 3 FX
5:58
so if the ID is greater than this we
6:00
will call the error Handler let's first
6:04
extract each bit from the 6bit ID field
6:07
and store them in the ID
6:10
buffer now to calculate the parity bit p
6:13
0 we will exore the ID bits 0 1 2 and
6:25
4 similarly to obtain the parity bit P1
6:29
first EXO the ID bits 1 3 4 and five and
6:34
then negate the obtained value now let's
6:37
store the parity bits along with the ID
6:40
bits and store them in the ID variable
6:44
the ID is now converted to protected
6:47
ID finally we will return the obtained
6:51
ID now as for the Lin frame after
6:54
sending the sync field we need to send
6:57
the protected ID so let's store the
7:00
protected ID at the next position in the
7:03
TX buffer here I am using the ID 34 hex
7:08
which will be converted to the protected
7:09
ID and will be stored in the TX
7:12
buffer next we will send the data the
7:16
data can be up to 8 bytes so I am going
7:21
bytes here I am storing the data at the
7:24
very next position in the buffer at the
7:27
end we have the check sum the process of
7:30
calculating the check sum is given in
7:33
appendix basically we have to keep
7:37
byes if the result is higher than
7:43
255 from it the check sum calculation
7:47
varies as per the Lin version you are
7:50
using for lin 1.3 we use the normal
7:54
check sum where we only perform the
7:56
addition operation between the data
7:58
bytes but for lin 2.1 we use the
8:02
enhanced check sum where we perform the
8:04
addition between the data bytes as well
8:08
Also let's write a function to calculate
8:11
the check sum the parameters of this
8:14
function will be the protected ID the
8:17
pointer to the data array and the size
8:20
data here we will first combine the data
8:23
bytes and the protected ID in a single
8:26
buffer so let's copy the protected ID to
8:29
the first position in the buffer and the
8:33
position now we have all the required
8:35
data in the buffer array itself so let's
8:38
add the data we will keep performing the
8:41
addition and if the sum is greater than
8:44
255 we will subtract 255 from the sum
8:50
the output of this Loop will be a value
8:52
less than or equal to
8:55
255 in the end we need to invert the
8:57
result so subtract the the sum from
9:01
255 this is also as per the instruction
9:04
given in the specification
9:08
manual let's go back to our TX data
9:11
buffer we have the sync field the PID
9:15
field and the eight bytes of the
9:17
data now we will add the check sum to
9:20
the last bite of this TX buffer the P ID
9:24
is already stored in the TX data 1 so
9:27
let's pass it here the actual data
9:30
starts from the offset of two in the TX
9:32
data buffer and the data size is 8
9:35
bytes as I mentioned the Lin version 1.3
9:39
does not use the PID for the check sum
9:42
calculations so if you are using version
9:44
1.3 you can pass zero to the P ID
9:48
parameter now we have the complete data
9:51
to be transmitted via the linbus so
9:53
let's start the sending
9:55
process we will first send the brake
9:58
field so so call the hall Lin send brake
10:01
function then transmit the TX data
10:04
buffer let's give a delay of one second
10:08
so that we can observe it in the
10:10
analyzer all right let's build and Flash
10:14
board here you can see the pin pa9 the
10:18
TX pin of the uart 1 is connected to the
10:22
analyzer we just need to check the data
10:25
sent by the MCU so this much connection
10:29
let's open the analyzer
10:31
software let me change the board rate to
10:36
9,600 I am also adding the Lin analyzer
10:39
to the same channel and set the board
10:47
9,600 all right here you can see we are
10:51
receiving frames every 1
10:53
second let's analyze one of these here
10:57
the frame starts with the brake Fe field
10:59
which was sent by the function hall in
11:02
break then we have the sync field with
11:07
hex then we have the protected ID B4 hex
11:11
but this is recognized by the Lin
11:12
protocol as the ID 34
11:15
hex this is the actual ID we sent and if
11:19
you check the appendix the P ID for 34
11:23
hex this is the P transmitted by the
11:27
MCU then we have the eight bytes of the
11:31
field and in the end we have the check
11:34
sum the check sum is 2 fhx and this is
11:39
correct I am saying this because if the
11:42
check sum is wrong the Lin protocol will
11:44
identify it as the wrong check sum let
11:48
me demonstrate this I am commenting out
11:51
the part where we calculate the check
11:53
sum and instead I will send another
11:55
random value here let's see how the
12:06
this you can see we have the entire
12:08
frame as it is and the check sum is 3F
12:12
the Lin protocol recognizes this as the
12:14
wrong check sum value for the data sent
12:17
previously this means that the check sum
12:20
we calculated was correct so we are able
12:23
to transmit the data in the Lin protocol
12:26
format the analyzer can decod CED this
12:29
data prettyy well and hence verifies
12:31
this as a valid Lin frame in the next
12:34
video we will connect an actual transer
12:38
MCU the transceiver will convert the
12:41
received data to the Lin voltage and
12:43
transmit it over the Lin bus we will
12:46
also use another transceiver to convert
12:48
the data back to the TTL format and then
12:51
read the data on another MCU the slave
12:55
this is it for today I hope you
12:57
understood how to get get started with
13:00
protocol you can download the code from
13:03
the link in the description leave
13:06
comments in case of any doubt keep
13:09
watching and have a nice day