The question is which type of ID and size of filter.
1) For example, standard ID and 4x16bit filter the setup is:
// 4x16bit filter for CAN4
// 4x16bit filter ==> 4x ID10~ID0, RTR,IDE,x,x,x)
// ==> 4xIDAR XXXX XXXX XXXRTR IDE??? - filter
// ==> 4xIDMR XXXX XXXX XXXRTR IDE111 - mask of the filter
// you required ID is 0x01 => IDAR = ID(10~0),RTR,IDE,x,x,x
// let's s suppose you do not care about frame type (data/remote) and you want to accept only standard
// frames IDE=0
// So IDAR = 00000000001,x,0,x,x,x (x = does not care) = 0B0000000000100000 = 0x0020
// IDMR = = ID(10~0),RTR,IDE,x,x,x and defines acceptance, 0= accept, 1= ignore
// we want accept exact ID, don't care RTR, exact IDE, don’t care of last tree bits of IDAR
// So IDMR = 00000000000,1,0,1,1,1 = 0B0000000000010111 = 0x0017
// all 4 filters are set to accept standard identifier only frames with odd ID
// Because of this all ID mask bits in IDMR are sent to 1(ignore) except lowest ID bit
// The lowest bit of IDMR is set to 0(match) and this bit is set in IDAR to 1 which is required value to
// receive only messages with odd number IDs.
// ! last three bits in IDMR must be set to 1(ignore).
// In this example I do not care of RTR so I set corresponding mask bits in IDMR to 1(ignore)
// In this example I check IDE so I set corresponding mask bits in IDMR to 0(match)
CAN4IDAC = 0x10; // 4x16bit filter
CAN4IDAR01 = 0x0020; // or CAN4IDAR0=0x00, CAN4IDAR1=0x20
CAN4IDMR01 = 0x0017; // or CANI4DMR4=0x00, CAN4IDMR1=0x17
because we want to use only one filter then all other must be set in the same way
CAN4IDAR23 = 0x0020;
CAN4IDMR23 = 0x0017;
CAN4IDAR45 = 0x0020;
CAN4IDMR45 = 0x0017;
CAN4IDAR67 = 0x0020;
CAN4IDMR67 = 0x0017;
init_CAN(…….);
2)
How filters work.
A)
If filter feature is used then only messages which meet filter condition are accepted and are received.
In the simplest case, filters work by applying a logical comparison to individual bits in the CAN message identifier. For each filtered bit, the comparison can be either
Test if bit is logic ‘1’ or
Test if bit is logic ‘0’ or
Ignore value of bit
Consider the following example. Suppose that the filter examines the eleven bits of a standard CAN message identifier. For each bit, the filter will check if the Id contains a logic ‘1’ (represented by ‘1’), a logic ‘0’ (represented by ‘0’), or either logic ‘1’ or logic ‘0’ (represented by ‘x’). In this simple example there are only three possible CAN messages.
The msCAN provides the features described above and adds further flexibility by allowing the user to configure the filter in a number of ways.
For the M68HC08 family the filter may act as a single 32-bit value that operates across the full 29-bits of an extended CAN message identifier.
In this case the filter identifies two sets of messages: one set that it receives and one set that it rejects. Alternatively, the filter may be split into two. This allows the msCAN to examine only the first 16 bits of a message identifier, but allows two separate filters to perform the checking. In our simple example, this would allow Message 2 to be accepted by the extra filter now available:
Filter value A: 0001x1001x0
Filter value B: 00x101x01x0
Message 1: 00011100110
Message 2: 00110100110
Message 3: 00010100100
Now the msCAN will accept all three messages. Filter A will accept Messages 1 and 3 as before and filter B will accept Message 2. For the configuration that uses two filters, there are four sets of messages: accepted by Filter A, rejected by Filter A, accepted by Filter B, rejected by Filter B. In practice, it is unimportant which filter accepts words, for the M68HC12 family the msCAN has the following combinations:
2 x 32-bit filters
1 x 32-bit filter, 2 x 16-bit filters
1 x 32-bit filter, 4 x 8-bit filters
2 x 16-bit filter, 4 x 8-bit filters
4 x 16-bit filters
8 x 8-bit filters
In practice the msCAN filter uses three sets of registers to provide the filter configuration. Firstly, the CANIDAC register determines the configuration of the banks into filter sizes and number of filters.
Secondly, registers CANIDMR0/1/2/3 determine those bits on which the filter will operate by placing a ‘0’ at the appropriate bit position in the filter register. Finally, registers CANIDAR0/1/2/3 determine the value of those bits determined by CANIDMR0/1/2/3.
For instance in the case of our simple example with the filter value of:
0001x1001x0
The CANIDMR0/1/2/3 register would be configured as:
00001000010
and so all message identifier bits except bit 1 and bit 6 would be 0.
The issue could be that identifier is not continual number within CANIDAR register and mask registers CANIDMR are able to put all bits from CANIDAR register into filtering.
So,
B)
Standard Identifier (11 bit) IDE = 0 looks like:
CANIDAR0: ID10 ID9 ID8 ID7 ID6 ID5 ID4 ID3
CANIDAR1: ID2 ID1 ID0 RTR IDE(=0) x x x
CANIDAR2: - - - - - - - -
CANIDAR3: - - - - - - - -
Extended Identifier (29 bit) IDE = 1 & SRR = 1 looks like:
CANIDAR0: ID28 ID27 ID26 ID25 ID24 ID23 ID22 ID21
CANIDAR1: ID20 ID19 ID18 SRR(=1) IDE(=1) ID17 ID16 ID15
CANIDAR2: ID14 ID13 ID12 ID11 ID10 ID9 ID8 ID7
CANIDAR3: ID6 ID5 ID4 ID3 ID2 ID1 ID0 RTR
To mask which bits from CANIDAR register will be put the CANIDMR registers must be set. (0=put given bit in CANIDAR into comparison, 1=bit value in CANIDMR is not relevant and is not put into filtering)
If we assume standard identifier we see the lowest significant three bits are not used. The acceptance filters can be 8, 16, or 32 bit long. So in order to correctly apply identifier mask in 16 bit acceptance filter mode register to a standard identifier these three bits AM[2,1,0] must be removed from process in IDMR1, IDMR3, IDMR5 and IDMR7 to correctly apply filter (set to 1-don’t care). Also in order to correctly apply identifier mask in 32 bit acceptance filter mode register to a standard identifier these three bits must be removed from process in IDMR1 and IDMR5 to correctly apply filter. The problematic bit contains values from control field and will cause issue in filtering when they are included into filtering process. Because of this they must be set to “don’t care”.
Let’s assume different filtering modes 2x32bits, 4x16bits and 8x8bits. The question is: Which bits from standard and extended identifier does go into filtering process?
AM – ID mask (0/1 = put/remove coresponding CANIDAR bit into/from filtering)
1 – remove from filtering
x – setup of the bit has no meaning
1) Acceptance Registers: 2x32 bit filter mode
1a) Standard Identifier (11 bit) IDE = 0
Following bits from CANIDAR are compared in dependence on filter setup CANIDMR
CANIDAR0: ID10 ID9 ID8 ID7 ID6 ID5 ID4 ID3 // ID1
CANIDAR1: ID2 ID1 ID0 RTR IDE(=0) x x x
CANIDAR2: - - - - - - - -
CANIDAR3: - - - - - - - -
CANIDAR4: ID10 ID9 ID8 ID7 ID6 ID5 ID4 ID3 // ID2
CANIDAR5: ID2 ID1 ID0 RTR IDE(=0) x x x
CANIDAR6: - - - - - - - -
CANIDAR7: - - - - - - - -
Following bits from CANIDMR masks CANIDAR filtering process
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 0 higher byte mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 1 1 1 // filter 0 lower byte mask
CANIDMR2 x x x x x x x x // filter 1 higher byte mask
CANIDMR3 x x x x x x x x // filter 1 lower byte mask
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 2 higher byte mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 1 1 1 // filter 2 lower byte mask
CANIDMR2 x x x x x x x x // filter 3 higher byte mask
CANIDMR3 x x x x x x x x // filter 3 lower byte mask
1b) Extended Identifier (29 bit) IDE = 1 & SRR = 1
Following bits from CANIDAR are compared in dependence on filter setup CANIDMR
CANIDAR0: ID28 ID27 ID26 ID25 ID24 ID23 ID22 ID21 //ID1
CANIDAR1: ID20 ID19 ID18 SRR(=1) IDE(=1) ID17 ID16 ID15
CANIDAR2: ID14 ID13 ID12 ID11 ID10 ID9 ID8 ID7
CANIDAR3: ID6 ID5 ID4 ID3 ID2 ID1 ID0 RTR
CANIDAR4: ID28 ID27 ID26 ID25 ID24 ID23 ID22 ID21 //ID2
CANIDAR5: ID20 ID19 ID18 SRR(=1) IDE(=1) ID17 ID16 ID15
CANIDAR6: ID14 ID13 ID12 ID11 ID10 ID9 ID8 ID7
CANIDAR7: ID6 ID5 ID4 ID3 ID2 ID1 ID0 RTR
Following bits from CANIDMR masks CANIDAR filtering process
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 0 mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0
CANIDMR2 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0
CANIDMR3 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0
CANIDMR4 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 1 mask
CANIDMR5 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 //
CANIDMR6 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 //
CANIDMR7 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 //
2) Acceptance Registers: 4x16 bit filter
2a) Standard Identifier (11 bit) IDE = 0
Following bits from CANIDAR are compared in dependence on filter setup CANIDMR
CANIDAR0: ID10 ID9 ID8 ID7 ID6 ID5 ID4 ID3 //ID1
CANIDAR1: ID2 ID1 ID0 RTR IDE(=0) x x x
CANIDAR2: ID10 ID9 ID8 ID7 ID6 ID5 ID4 ID3 //ID2
CANIDAR3: ID2 ID1 ID0 RTR IDE(=0) x x x
CANIDAR4: ID10 ID9 ID8 ID7 ID6 ID5 ID4 ID3 //ID3
CANIDAR5: ID2 ID1 ID0 RTR IDE(=0) x x x
CANIDAR6: ID10 ID9 ID8 ID7 ID6 ID5 ID4 ID3 //ID4
CANIDAR7: ID2 ID1 ID0 RTR IDE(=0) x x x
Following bits from CANIDAR are compared in dependence on filter setup CANIDMR
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 0 higher byte mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 1 1 1 // filter 0 lower byte mask
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 1 higher byte mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 1 1 1 // filter 1 lower byte mask
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 2 higher byte mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 1 1 1 // filter 2 lower byte mask
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 3 higher byte mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 1 1 1 // filter 3 lower byte mask
2b) Extended Identifier (29 bit) IDE = 1 & SRR = 1
Following bits from CANIDAR are compared in dependence on filter setup CANIDMR
CANIDAR0: ID28 ID27 ID26 ID25 ID24 ID23 ID22 ID21 //ID1
CANIDAR1: ID20 ID19 ID18 SRR(=1) IDE(=1) ID17 ID16 ID15
CANIDAR2: ID28 ID27 ID26 ID25 ID24 ID23 ID22 ID21 //ID2
CANIDAR3: ID20 ID19 ID18 SRR(=1) IDE(=1) ID17 ID16 ID15
CANIDAR4: ID28 ID27 ID26 ID25 ID24 ID23 ID22 ID21 //ID3
CANIDAR5: ID20 ID19 ID18 SRR(=1) IDE(=1) ID17 ID16 ID15
CANIDAR6: ID28 ID27 ID26 ID25 ID24 ID23 ID22 ID21 //ID4
CANIDAR7: ID20 ID19 ID18 SRR(=1) IDE(=1) ID17 ID16 ID15
Following bits from CANIDAR are compared in dependence on filter setup CANIDMR
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 0 higher byte mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 0 lower byte mask
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 1 higher byte mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 1 lower byte mask
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 2 higher byte mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 2 lower byte mask
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 3 higher byte mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 3 lower byte mask
3) Acceptance Registers: 8x8 bit filter
3a) Standard Identifier (11 bit) IDE = 0
Following bits from CANIDAR are compared in dependence on filter setup CANIDMR
CANIDAR0: ID10 ID9 ID8 ID7 ID6 ID5 ID4 ID3 //ID1
CANIDAR1: ID10 ID9 ID8 ID7 ID6 ID5 ID4 ID3 //ID2
CANIDAR2: ID10 ID9 ID8 ID7 ID6 ID5 ID4 ID3 //ID3
CANIDAR3: ID10 ID9 ID8 ID7 ID6 ID5 ID4 ID3 //ID4
CANIDAR4: ID10 ID9 ID8 ID7 ID6 ID5 ID4 ID3 //ID5
CANIDAR5: ID10 ID9 ID8 ID7 ID6 ID5 ID4 ID3 //ID6
CANIDAR6: ID10 ID9 ID8 ID7 ID6 ID5 ID4 ID3 //ID7
CANIDAR7: ID10 ID9 ID8 ID7 ID6 ID5 ID4 ID3 //ID8
Following bits from CANIDAR are compared in dependence on filter setup CANIDMR
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 0 mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 1 mask
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 2 mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 3 mask
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 4 mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 5 mask
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 6 mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 7 mask
3b) Extended Identifier (29 bit) IDE = 1 & SRR = 1
Following bits from CANIDAR are compared in dependence on filter setup CANIDMR
CANIDAR0: ID28 ID27 ID26 ID25 ID24 ID23 ID22 ID21 //ID1
CANIDAR1: ID28 ID27 ID26 ID25 ID24 ID23 ID22 ID21 //ID2
CANIDAR2: ID28 ID27 ID26 ID25 ID24 ID23 ID22 ID21 //ID3
CANIDAR3: ID28 ID27 ID26 ID25 ID24 ID23 ID22 ID21 //ID4
CANIDAR4: ID28 ID27 ID26 ID25 ID24 ID23 ID22 ID21 //ID5
CANIDAR5: ID28 ID27 ID26 ID25 ID24 ID23 ID22 ID21 //ID6
CANIDAR6: ID28 ID27 ID26 ID25 ID24 ID23 ID22 ID21 //ID7
CANIDAR7: ID28 ID27 ID26 ID25 ID24 ID23 ID22 ID21 //ID8
Following bits from CANIDAR are compared in dependence on filter setup CANIDMR
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 0 mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 1 mask
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 2 mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 3 mask
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 4 mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 5 mask
CANIDMR0 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 6 mask
CANIDMR1 AM7 AM6 AM5 AM4 AM3 AM2 AM1 AM0 // filter 7 mask