0:12
hello everyone welcome to controller's tech today in this video we will discuss
0:19
about the can peripheral there is a lot to cover in the can peripheral so i will do multiple parts
0:25
for it this video will cover the basic can protocol how to use it in the loopback mode and
0:32
also how to configure the filters we will not use the extended version of this can in the beginning
0:39
but focus on the basic can let's start with the can protocol first
0:45
as you can see the can data frame is quite big but we will not discuss everything here we will only focus on
0:52
the region i have highlighted with r1 other than this region we will write the data obviously but the
0:59
rest is handled by the whole library region r1 contains the arbitration field
1:06
and the control field here we have the 11-bit identifier which is more like the id of the
1:12
transmitting node then we have rtr remote transmission request
1:17
it defines either we are sending remote frame or data frame then we have id which decides whether we
1:25
are using standard id or extended id for the can r is the reserved bit dlc is the data
1:33
length in bytes and finally we have the data field we will see them all today let's start
1:41
by creating the project in cube id
1:51
i am using stm32f1 controller
2:01
give some name to the project and click [Music]
2:06
finish let's also take a look at the reference manual
2:18
here is the can peripheral pill can support basic and extended can protocols the bit rate
2:27
here can be up to 1 mbps in the reception part it have 2 fifo to
2:32
receive the data for the devices with two can peripherals there are 28 filter banks
2:39
and for the devices with one can peripheral there are only 14 filter banks we will discuss filter
2:46
banks later in this tutorial blue pill only have one can peripheral
2:51
so we only have 14 filter banks here
3:00
here is how the can transceiver is connected it is connected to the can controller
3:06
via the tx and the rx pins and the can high and can low from the
3:12
transceiver are connected to the can bus all other can transceivers are also
3:17
connected to this bus and that's where the data transfer takes place
3:22
this is the can transceiver i have it is mcp2551 can transceiver
3:29
and as you can see it have 4 data pins tx rx can high and
3:36
can low [Music] for the transmit purpose it have 3
3:42
transmit mailbox and we can transmit data using these three mailbox
3:48
we will check this in a while
3:54
now comes the loopback mode this is basically a test mode where we can test the transceiver
4:04
whatever data we send it should be able to loop back [Music]
4:11
okay that's all for now let's set up the things in cube mx i am enabling the external crystal for
4:18
the clock [Music] i have eight megahertz crystal and i
4:24
want the system to run at 72 megahertz
4:30
let's also enable the serial wire debug
4:36
enable the can peripheral you see here p11 is the can rx pin
4:43
and pier 12 is the can tx this is how the connection is p11 is
4:50
connected to rx p12 to tx and i have shorted the can high and can low since we are using loopback
4:58
mode there should be a 120 ohms resistor here between these two
5:06
let's set up the board rate
5:14
i am trying different setups to get the board rate of 500 000 bits per second
5:19
[Music] here i got it let's change the operating
5:26
mode to loopback mode leave everything as default [Music]
5:34
now go to the nvic tab and select the fifo interrupt
5:40
you can use either of these fifo but i will show the working for both so i am enabling both interrupts
6:08
here is the can initialization function i will explain all these but let's
6:14
quickly test the loopback mode first
6:25
we need to configure the filters
6:37
let's put it here after the
6:56
it have a lot of elements in it
7:03
for now i am going with this configuration but don't worry i will explain this in a while
7:13
now we will call the filter configuration function [Music]
7:26
we need to set the tx header before transmitting the data
7:35
also we will check the rx header after the data has been received
7:40
let's create a mailbox to send the data also create buffer for transmit and
7:54
now inside the main function start the can
8:00
let's activate the notification of receiving data here we need to write the interrupt type
8:14
the type are defined in the can header
8:29
here we will look for the message pending in the fifo zero
8:39
now once this interrupt gets triggered the interrupt callback function will be
9:04
we will receive data in this message pending callback but for now let's just increment the
9:11
count now before sending the data to the can we have to modify the header
9:17
basically set the arbitration and the control fields
9:24
we will do this in the tx header that we defined earlier the first member is the dlc
9:32
this specifies the length of data that we are going to send i am planning on transferring only
9:39
one byte of data next member is the extended id
9:44
since we are using basic can protocol today we will keep it zero next is the ide
9:52
this specifies the type of identifier
10:09
basically whether it's a standard identifier or extended id
10:20
we are using standard one today so we will keep it standard id
10:33
this specifies whether we are transferring data or remote frame
10:38
as we are sending data we will set it to
10:44
data next is the standard identifier here we
10:50
can give any identifier for this can peripheral if there are other devices connected on
10:55
the can bus they all will receive this header and based on identifier we can filter
11:01
the messages from a particular device this identifier can be 11 bit wide and
11:07
this serves as the id for the can device next is the transmit global time
11:14
let's not go into this and we will keep it disabled
11:23
we will send this data to the can to send the data use whole can add tx
11:30
message here we will send the tx header and data using this tx mailbox
11:36
[Music] the data is received back in the rx-500
11:45
and as we have activated the notification for the fifo zero message pending interrupt
11:50
the callback will be called let's test this much part first
12:00
build and debug the code
12:11
i am going to use the analyzer to visualize the data
12:35
let's run the code now [Music]
12:48
here we got something on the transmit line so we have the can identifier just like
12:54
we have assigned the control field is 0.01
13:06
this include all those rtr dlc ide bits and then we have the data field
13:13
which contains the data for some reason the receiver is not able to receive
13:20
i guess some issue with the wire anyway let's not worry about that we
13:26
will check the receive directly in our program itself
13:40
inside the callback function we will get the rx message
13:49
the data will be received from fifo zero
13:56
the header will be stored in the rx header and the data will be saved in the rx
14:02
data looks like some issue here
14:14
the header is a pointer so let's give the address here
14:38
let's run it now we got the hit here as you can see the rx header have the
14:45
same parameters that we set in the tx header the standard id is zero cross one
14:51
zero three the dlc is one
15:00
also the rx data is same that we sent to the can so the things are working all right
15:08
the loopback mode works pretty well
15:15
now let's take a look at the can filters here the first parameter is filter
15:20
activation this basically specifies whether you want to enable
15:25
or disable the filters we need it enabled since we are using the filters next
15:32
parameter is the filter bank this parameter specifies which filter
15:38
bank you want to use this parameter is associated with slave
15:44
start filter bank to understand this let's take a look at the reference manual
15:50
as it's mentioned here if there are two can peripherals there are 28 filter banks out of these
15:57
two can peripherals one is termed as master and another is slave
16:03
slave start filter bank is basically the filter bank after which the rest of the banks will
16:08
be allocated to the slave can peripheral for example if i write 13 here the banks
16:16
from 13 to 27 will be allocated for the can too
16:25
or we can also say that banks from 0 to 12 are assigned to the can one the master
16:43
it's mentioned here that if there is only one can peripheral then this parameter is useless
17:00
now filter bank is the filter bank number that you want to use for the controllers with single can
17:07
peripheral this can be between 0 to 13. but for the controllers with two can
17:13
peripherals you can use any filter bank that you have assigned for the respective can
17:18
i am using filter bank 10 and the comment here is for the devices with token peripherals
17:26
next we have filter fifo assignment this specifies which fifo must be
17:32
assigned to this particular filter the incoming data will be saved to the
17:37
fifo that we assign here we have two fifo and i am using fifo zero we will cover
17:44
this part but before that let's check the filter
17:58
mode here we have to choose the type of filter that we are going to use
18:05
we have two types of filters the id mask mode and the id list mode
18:19
in mask mode the identifier registers and the mask registers can be used to
18:24
look for the particular bits in the identifier i will explain this part in detail
18:34
also we will be using 32-bit scale which means the id and the mask
18:39
registers will be bit wide
18:51
now let's go back to the identifier and the mask registers these registers are used to filter the
18:58
incoming data basically we can configure these registers to filter the data
19:03
and let it pass through only when some particular conditions are met these are 32-bit wide registers
19:11
the red block is the higher 16 bits and the green block is the lower 16 bits
19:17
since we are not using extended id we will keep our focus on the yellow color boxes
19:24
now if you remember the standard identifier that we used during the transmit was zero cross one
19:31
zero three as you can see here the standard identifier is the part of the higher 16
19:37
bits of these registers when some device send the data over the can bus
19:43
every can device receives this id let's say that this is the incoming id also let's assume
19:50
that we have same value set in the id register and the mask register
20:04
now pay attention to what will happen here since these bits are set in the mask register only these bits of the id
20:11
register will be compared with the incoming id and if the bits in the incoming id
20:17
matches to that of the id register the message will pass through
20:24
let's see the application now
20:29
remember that the id of the transmitter was zero cross 1 0 3.
20:36
so i am setting the same id here in the id high register also i am using the same value in the
20:43
mask i register so why am i shifting it by 5 places
20:48
well that's because the extended id is also the part of id high register
20:54
and if you notice here the extended if takes first 5 bits of this register
21:00
so the standard id starts from the fifth position
21:11
let's test this part first
21:20
i am adding a break point here and we hit the break point
21:27
the i d is zero cross one zero three so this was as expected
21:36
now let's change this transmit i d to zero cross one zero two and see if it is passing
21:49
this time the filter didn't let it pass this is because the incoming id is 1 0
21:55
2 and the value in the id register is 1 0 3. also since this mask bit is set to 1
22:03
so these bits will be compared if these bits are not same the message will not pass
22:10
and that's what happened in this case
22:17
but what will happen if i don't write a 1 here in the mask register
22:23
now these 2 bits will not be compared and even if we have the id 1 0 3 in the
22:31
id register the data should pass through
22:50
i am going to change the mask register value so that the fifth bit is zero
22:59
let's see the result now
23:08
as expected the message got through the filter here the id is 102 and it was passed
23:20
through even if the id register was set 103
23:28
so this entire process depends on the mask register which decides which bits should be
23:34
compared now let's say we want to pass multiple
23:40
ids in that case i have decided to only check this bit
23:49
now only this 13th bit in the id register will be compared to the incoming id
23:55
and if they are same the message will pass through any other bits in the incoming id don't
24:07
let's test this i am writing a 1 in the 13th position in the mask register
24:28
let's change the id to 1 2 3.
24:38
so this is passed through because the 13th position in the incoming id was 1.
24:52
let's try with 5 2 3.
25:01
this was also passed through the filter
25:14
5 2 3 will be like this and this have a 1 in the 13th position
25:19
and since all that matter right now the message was passed through
25:27
also if i write a 1 in this position in the mask register then this particular bit will be
25:32
compared in the id register and the incoming id if these are same
25:38
the data will be ok this is how the filter works i hope you understood the logic here
26:00
i am writing this so that it's easier to set the break point
26:10
the another filter type we have is the id list filter
26:19
here instead of having one mask register we have two id registers thus instead of defining an
26:26
identifier and a mask two identifiers are specified doubling the number of single identifiers
26:33
all bits of the incoming identifier must match the bits specified in the filter registers we will not be looking into
26:40
this one today now the filters are done let's take a look at the transmit mailbox
26:50
as mentioned here it supports up to three transmit mailbox
26:57
the detailed description for this is given in the reference manual basically the mailbox was empty at first
27:05
then we write some data into it once we do the transmit it goes into the
27:10
pending state from there it gets scheduled and finally it transmits
27:16
while the mailbox is in the pending state it can be aborted let's see how can we use these mailbox
27:24
i am going to create an array of four mailbox [Music] and now i will send three mailbox
27:38
let's modify this so that we know if we have some
28:13
i am going to send different data field with each mailbox
28:47
okay so everything was fine and note here that we received the data from first mailbox
28:56
again from the second mailbox and now from the third mailbox
29:04
things are going well till now now i will send the fourth mailbox
29:09
and see what happens
29:33
you see we are not hitting the break point anymore and here we are in the error handler
29:39
so what happens is when it tried sending that fourth mailbox it went into the error handler this is
29:46
because this can peripheral can support up to three mailbox and sending any more
29:51
will result in failure when all three mailbox were in pending states
29:56
you can change their priorities also check this section of the data sheet as
30:02
i am not going into that depth for now so this is it for the loopback mode and
30:07
the filter configuration i hope things were clear in the next
30:12
video i will cover the normal mode where we can communicate between two different can devices
30:19
you can download the code from the link in the description keep watching and have a nice day ahead