|
|
View previous topic :: View next topic |
Author |
Message |
seidleroni
Joined: 08 Sep 2008 Posts: 21
|
CAN Filters - what am I missing? |
Posted: Wed Nov 26, 2008 10:48 am |
|
|
I am trying to enable CAN filters in my code, but using the "can_enable_filter()" function gives me a "Undefined identifier" error in my compiler. What gives?
Also, I tried without that function call and using a filter code below and it never filters:
Code: |
canMask = 0x100FF000;
canFilter = 0x10011000;
can_set_id(RX0MASK, canMask, CAN_USE_EXTENDED_ID); //set mask 0
can_set_id(RX0FILTER0, canFilter, CAN_USE_EXTENDED_ID); //set filter 0 of mask 0
can_set_id(RX0FILTER1, canFilter, CAN_USE_EXTENDED_ID); //set filter 1 of mask 0
can_set_id(RX1MASK, canMask, CAN_USE_EXTENDED_ID); //set mask 1
can_set_id(RX1FILTER2, canFilter, CAN_USE_EXTENDED_ID); //set filter 0 of mask 1
can_set_id(RX1FILTER3, canFilter, CAN_USE_EXTENDED_ID); //set filter 1 of mask 1
can_set_id(RX1FILTER4, canFilter, CAN_USE_EXTENDED_ID); //set filter 2 of mask 1
can_set_id(RX1FILTER5, canFilter, CAN_USE_EXTENDED_ID); //set filter 3 of mask 1 |
Any help? |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
|
Posted: Wed Nov 26, 2008 11:26 am |
|
|
I have a multiprocessor product and the different processors all communicate via CAN. I based my code on the CCS example code, but I use the CAN RX interrupts. This is the way I set things up:
Code: | // for locations (# shifts)
#define FROM 25
#define DEST 21
#define PURP 18
#define MULTI_STEP 14
#define SENDER_MASK 0x1e000000
#define RECIP_MASK 0x01e00000
#define PURP_MASK 0x001c0000
#define STEP_MASK 0x0003c000
#define MESSAGE_MASK 0x00003fff
// for the from/to addressing
#define TOUCH 1
#define MEMORY 2
#define PROC 3
#define MASTER 4
#define DIS_PROC 5
#define AUX1 6
#define AUX2 7
#define DEBUG 8
#define ALL 9
// for the message purpose
#define STORE 0
#define FETCH 1
#define CONT 2
#define INFO 3
#define ACK 4
#define NACK 5
#define F_MULTI 6
#define MULTI_END 7
// "master" init:
void can_init(void) {
can_set_mode(CAN_OP_CONFIG); //must be in config mode before params can be set
can_set_baud();
RXB0CON=0;
RXB0CON.rxm=CAN_RX_VALID;
RXB0CON.rxb0dben=CAN_USE_RX_DOUBLE_BUFFER;
RXB1CON=RXB0CON;
CIOCON.endrhi=CAN_ENABLE_DRIVE_HIGH;
CIOCON.cancap=CAN_ENABLE_CAN_CAPTURE;
can_set_id(RX0MASK, RECIP_MASK, CAN_USE_EXTENDED_ID); // set mask 0 to only examine recipient field
can_set_id(RX0FILTER0, MASTER << DEST, CAN_USE_EXTENDED_ID); // set filter 0 of mask 0 to respond to messages addressed to the master
can_set_id(RX0FILTER1, 0, CAN_USE_EXTENDED_ID); // don't use filter 1
// use rx 1 for everything else
can_set_id(RX1MASK, RECIP_MASK, CAN_USE_EXTENDED_ID); // set mask 1 to only examine recipient field
can_set_id(RX1FILTER2, ALL << DEST, CAN_USE_EXTENDED_ID); // set filter 2 of mask 1 to respond to broadcast messages only
can_set_id(RX1FILTER3, 0, CAN_USE_EXTENDED_ID); // don't use filter 3
can_set_id(RX1FILTER4, 0, CAN_USE_EXTENDED_ID); // don't use filter 4
can_set_id(RX1FILTER5, 0, CAN_USE_EXTENDED_ID); // don't use filter 5
set_tris_b((*0xF93 & 0xFB ) | 0x08); //b3 is out, b2 is in
can_set_mode(CAN_OP_NORMAL);
}
// example of CAN rx code - triggered by CAN RX0 interrupt elsewhere
void process_message0(void) { // for messages addressed directly to master
int8 sender;
int8 purpose;
int8 step_number;
int16 message_id;
int8 i, j, k;
get_0_data(rx_id, &in_data0[0], rx_len, rxstat0);
sender = (rx_id & SENDER_MASK) >> FROM;
purpose = (rx_id & PURP_MASK) >> PURP;
step_number = (rx_id & STEP_MASK) >> MULTI_STEP;
message_id = (rx_id & MESSAGE_MASK);
// put code here to sort through what was actually sent, based on the message_id
} |
By setting things up in this manner, sending & receiving messages is very easy. Just put a list of CAN messages in a header file. To transmit something, just "build" the transmit id like this:
Code: | transmit_id = (MASTER << FROM) | (MEMORY << DEST) | (FETCH << PURP) | RETRIEVE_SERNUM;
can_putd(transmit_id, out_data, 1, 3, tx_ext, 0); |
The filters take care of retrieving only messages addressed to that processor or broadcast messages and they work very well. I should point out that message_id is an int16 and transmit_id is an int32. |
|
|
seidleroni
Joined: 08 Sep 2008 Posts: 21
|
|
Posted: Wed Nov 26, 2008 11:54 am |
|
|
In my code, the filter is letting everything through, regardless of the ID that it is getting. My mask is 0x100FF000 and my filter is 0x10011000. If my ID is 0x10044000, the message is getting through even though it shouldnt. Why is that happening? Am I missing something? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 26, 2008 12:11 pm |
|
|
In your previous thread on CAN bus problems, you said:
http://www.ccsinfo.com/forum/viewtopic.php?t=36814
Quote: | RXB0CON.rxm = CAN_RX_ALL; //added line to make receive interrupts work |
The can-18f4580.h file says that constant is equal to 3 (0b11):
Code: | enum CAN_RX_MODE {CAN_RX_ALL=3, CAN_RX_EXT=2, CAN_RX_STD=1, CAN_RX_VALID=0}; |
The 18F4685 data sheet says:
Quote: |
REGISTER 23-13: RXB0CON: RECEIVE BUFFER 0 CONTROL REGISTER
11 = Receive all messages (including those with errors); filter criteria is ignored |
This is why it's a good idea to keep all posts on a particular topic in
the same thread. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|