0:09
hello and welcome to controllers Tech
0:12
this is the ninth video in the SDM 32
0:15
uart series and part two of the Lin
0:18
protocol in the previous video we saw
0:20
how to configure and program the master
0:23
to send the data we also analyzed the
0:26
data using the logic analyzer and saw
0:29
the Lin Pro call frame in the analyzer
0:32
today in this video we will connect an
0:34
actual Lin transceiver to the MCU and
0:37
analyze the data sent by the master over
0:39
the Lin bus we will establish connection
0:42
between two transceivers and connect the
0:45
slave to the second transceiver we will
0:48
also program the slave MCU and extract
0:51
the data from the receive frame I am
0:54
going to start with connecting a Lin
0:56
transceiver module to the master MCU I
0:59
am going to use the mCP 20004 Lin
1:02
transceiver for it here is the data
1:06
sheet of the transceiver it supports a
1:08
wide range of voltages from 6 volts to
1:13
DC here you can see the pinout for the
1:16
transceiver I see the rxd pin sends the
1:19
U data out to the MCU so it connects to
1:23
the U RX pin of the MCU the txt pin
1:27
receives the uart data from the MCU so
1:30
it connects to the uart TX pin of the
1:32
MCU the Cs pin controls the transmitter
1:35
part of the IC and it connects to the
1:39
MCU the txe or fault pin is
1:43
bidirectional and allows disabling of
1:45
the transmitter as well as fault
1:47
reporting related to disabling the
1:50
transmitter we will leave this pin
1:53
disconnected the vbb and vsss pins will
1:56
be connected to the 12vs power
1:58
source the vren is the voltage regulator
2:01
enable pin and we will leave this pin
2:05
disconnected and finally we have the lbs
2:08
the Lin bus pin this is where the data
2:12
will be transmitted in the Lin protocol
2:15
format here you can see the operational
2:20
20004 the IC is turned on when the
2:23
voltage higher than 5.5 Vols is provided
2:27
the receiver is on by default and the
2:30
transmitter is off to turn on the
2:33
transmitter we need to set the Cs pin
2:36
High here you can see the connection
2:39
diagram between the mCP
2:43
MCU the TX pin from the IC should be
2:47
connected to the TX pin of the
2:49
MCU the RX pin connects to the RX pin we
2:54
will connect the Cs pin to the io pin of
2:56
the MCU so that we can control the
3:00
IC here you can see how the transceiver
3:03
IC is connected to the
3:05
MCU on the right side we have the pin
3:09
IC the pin one is the RX pin and it is
3:13
connected to the uart1 RX pin on the
3:16
MCU pin 4 is the TX pin and it is
3:19
connected to the uart1 TX pin of the
3:23
MCU pin 2 is the Cs pin and it is
3:26
connected to the pin pc7 on the MC you
3:30
we will set this pin's output to control
3:34
transmitter the pins 5 and seven are the
3:37
power supply pins and they are connected
3:41
supply the pin number six is the linbus
3:45
I have connected it to the logic
3:47
analyzer the ground pin from the
3:49
analyzer is connected to the ground of
3:51
the power supply here I have the
3:54
previous project where we sent the data
3:57
in the Lin format but without using any
4:00
transceiver we need to add the Cs pin to
4:03
this project so let's open the cube MX
4:06
the pin pc7 will be used as the Cs pin
4:10
so let's set it as the output the uart
4:13
configuration is the same as what we
4:15
discussed in the previous video I am
4:18
enabling the interrupt just in case the
4:21
master wants to receive the data also
4:24
the brake length should be 11 bits as
4:27
per the Lin 2.1 standard that is all
4:30
click save to generate the project I
4:34
want the master to continuously send the
4:36
data so the transmitter should be always
4:39
enabled here we will set the Cs pin High
4:42
to ensure that the data is still the
4:45
same that we sent in the previous
4:48
tutorial let's build and Flash the code
4:51
to the board the Lin bus is connected to
4:54
the logic analyzer so let's see if it
4:56
recognizes the Lin frame here you can
4:59
see the analyzer do read the frames
5:02
every 1 second let's analyze it in
5:05
detail we have the brake filed followed
5:08
by the sync field then we have the
5:11
protected ID 34 hex after that we have
5:15
eight bytes of data which is followed by
5:18
the check sum bite in the end so the
5:21
transceiver is working fine and the
5:23
analyzer is able to detect the Lin frame
5:25
on the Lin bus now I want to program the
5:28
master so that it sends different data
5:31
each time let's define an index variable
5:35
whose value will be used as the data the
5:38
index variable will increment after the
5:40
previous value has been stored and if
5:43
the variable is more than
5:44
255 we will reset it back to zero all
5:49
right let's build and Flash it to the
5:54
board let's analyze a random
6:03
here you can see the data with different
6:08
variable this particular frame has the
6:10
data values from 24 to
6:13
31 the next frame has the data from 32
6:17
39 and the next one has it from 40 to
6:21
47 the check sum B changes for each
6:24
frame because the data values are
6:27
different so our is working fine and the
6:31
transceiver is able to transmit the data
6:34
bus now we will add one slave to this
6:38
project let's create a new
6:41
project I am going to use the f103 C8
6:46
device let's give some name to the
6:51
finish here you can see how the master
6:53
and slave are connected via the
6:56
transceivers the linbus pin of the
6:58
master transceiver is connected to the
7:02
slave we have exactly the similar
7:04
connection between the MCU and the
7:07
transceiver I am using the usat 3 in the
7:10
slave MCU and the TX and RX pins are
7:14
connected to the respective pins on the
7:16
transceiver the Cs pin is the PIN
7:24
MCU we will start with the clock setup
7:27
first I am selecting an external Crystal
7:30
to provide the clock the board has 8 MHz
7:34
Crystal on it and we will run the MCU at
7:38
MHz go to CIS debug and enable serial
7:43
wire now enable the usart 3 in the Lin
7:46
mode the pins pb10 and pb11 are enabled
7:50
by default and we are using these pins
7:53
for the connection let's set the pin pb1
7:56
as the output so that it can be used as
7:59
as the Cs pin the board rate should be
8:02
the same as what the master has so set
8:06
9,600 go to the interrupt Tab and enable
8:11
interrupt now go to the gpio settings
8:15
and enable the pull-up for the RX pin
8:18
this is very important otherwise the
8:21
slave will not receive the data all
8:24
right that is all the configuration we
8:26
need click save to generate the project
8:29
we will copy the same code from the
8:38
project we don't need the PID
8:40
calculation for now as the slave will
8:43
not transmit the data instead of the TX
8:46
data let's rename it to RX data the
8:50
check sum calculation will be required
8:53
as we need to verify the received data
8:56
Note here that I am not setting the Cs
8:58
pin instead it is low by default I just
9:02
enabled it to demonstrate that the Cs
9:04
pin is not needed to receive the data it
9:07
is only required for transmitting let's
9:10
call the function receed to idle in the
9:12
interrupt mode to receive 20 bytes of
9:14
data now when all the 20 bytes are
9:17
received Boron idle line is detected
9:19
before that an interrupt will trigger
9:21
and the RX event callback will be
9:24
called inside the Callback we will call
9:27
the same function so that to make
9:31
reception let's build and debug the
9:34
project I have connected the master MCU
9:37
to another computer so that the ID does
9:40
not detect multiple SD
9:42
links let me add the RX data to the live
9:47
expression all right let's run the
9:49
debugger you can see the data is being
9:52
received in the RX data buffer let me
9:58
heximal we will analyze this data in
10:01
detail so let's see the
10:06
analyzer first we have the brake field
10:10
low this brake field is detected as the
10:14
beginning then we have the sync field
10:17
with the bite 55 hex followed by the
10:22
hex the actual ID is 34
10:26
hex here we have the sync filed 5 5 hex
10:30
and then the protected ID before
10:32
hex we can extract the actual ID from it
10:36
so we will write the code for the same
10:39
then we have the eight data byes and the
10:41
check sum bite at the end note here that
10:45
the actual data starts from the third
10:47
position and the data length may vary
10:49
depending on how many bytes the master
10:52
sending we will write the slave program
10:55
keeping that in mind let me take a
10:58
screenshot of it so that we can write
11:00
the program according to this
11:04
format all right let's Define a variable
11:07
num data bytes which will keep track of
11:10
how many data bytes has been received
11:13
the variable ID will store the actual ID
11:16
sent by the master no matter how many
11:19
data bytes are sent by the master we
11:22
will definitely have the first three
11:23
bytes and the last cheex some bite so we
11:27
have four extra bytes in total the size
11:30
parameter of the call back stores the
11:33
number of bytes received so the data
11:36
bytes will be equal to the size minus 4
11:39
this variable will confirm the validity
11:42
of the received data the actual data
11:45
starts from the third position in the
11:47
buffer so we will once again calculate
11:50
the check sum here the protected ID is
11:53
at the second position and the data
11:56
start from the offset of three the
11:58
number number of data bytes are already
12:01
calculated we know the check sum is the
12:04
last bite in the buffer so we will match
12:06
the calculated check sum with the
12:08
received check sum if the check sum does
12:11
not match this means that the data is
12:14
invalid otherwise we have the valid data
12:18
the data buffer will store the actual
12:27
received inside the while loop we will
12:30
check if the data valid variable is set
12:33
to one if it is we will start copying
12:36
the data from the third position in the
12:38
RX buffer to our data buffer reset the
12:41
variable so that this Loop does not run
12:44
again all right let's build and debug
12:47
the project now let's add the data
12:50
buffer to the live expression or right
12:53
here you can see the data buffer
12:55
contains the actual data extracted from
12:59
let me change the display to decimal
13:02
numbers the data is being received every
13:05
second and you can clearly see the
13:07
received data it is according to how we
13:09
programmed the master we can also
13:12
extract the actual ID from the protected
13:14
ID field to do so we just need to
13:18
extract the first six bits of the
13:25
ID let's check it in the
13:30
you can see the ID is 34 hex and it is
13:33
the same ID that the master sent so
13:36
everything is working well so far we
13:39
were able to program the slave device to
13:42
receive the data from the linbus and
13:44
then extract the actual data bytes from
13:46
the received data we also made a
13:50
successful connection between the two
13:52
transceivers and between the transceiver
13:55
MCU in the next tutorial we will see
13:58
more more practical uses of the Lin
14:00
communication where the master will send
14:02
the command to the slave device to
14:06
operations this is it for this
14:09
video you can download the code from the
14:13
description leave comments in case of
14:15
any doubt keep watching and have a nice