6 years, 8 months ago.

Help with CAN Filter/Freq

Hi everyone I need your help with a CAN filter and how to set it up. I have the LPC1768 to a MCP2551 using P9 and P10. Everything seems to be working as I can read and write to the bus. Even though I have not able to set the frequency for the bus. I'm assuming this is because I am connected directly to the MCP2551 the baud rate is detected?? For the filter I am using the example at the bottom. filter1=can1.filter(0x410, 0x7FF, CANStandard,filter1); Filter1 always equals zero.

In the Handbook I found this:

CAN filter exple 1

int filter	(	unsigned int 	id, unsigned int  mask, CANFormat 	format = CANAny, int  handle = 0 )		
Filter out incomming messages.

and I see that can.read also uses the handle:

CAN filter exple 2

int read	(	CANMessage & msg, int 	handle = 0 )

In the forum I also found an example someone posted using the same format as above:

CAN filter

can->reset(); // reset and clear all filters (accept all messages)
h1 = can->filter(0x100, 0x700, CANStandard); // Accept standard messages with id 0x100-0x1FF
h2 = can->filter(0x200, 0x7FF, CANStandard); // Accept standard message 0x200 as well
[...]
h2 = can->filter(0x300, 0x7FF, CANStandard, h2); // Accept standard message 0x300, overwrite 0x200

CANMessage msg = CANMessage();
int r = can.read(msg, h1); // Should only return messages with id 0x100-0x1FF
int r = can.read(msg, h2); // Should only return messages with id 0x300
int r = can.read(msg);     // Returns 0x100-0x1FF if pending, otherwise 0x300, otherwise any message

Unfortunately all I get is a return of 0 and I'm stuck.

I have been pointed to this bit of code to enable the filtering: http://mbed.org/users/ym1784/code/CAN_MONITOR/file/fe1f886dff76/main.cpp

posted by Rob Crain 05 Apr 2014

FYI,
My code is simplified version from the original source found on the net.

https://mbed.org/forum/mbed/topic/2033/?page=1#comment-20838

http://www.dragonwake.com/download/LPC1768/Example.zip

The current mbed source is...

can_api.c

int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) {
    return 0; // not implemented
}

https://github.com/mbedmicro/mbed/blob/master/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/can_api.c

posted by Yoshi Mimura 05 Apr 2014

I'm having the same problem with the mbed library filter and have been unable to find a working example to help me out. My work-around solution is to use the dragonwake.com code (by Yoshi Mimura) mentioned above and in other posts but I'd really like to understand how to make the mbed library filter work. I'm using an LPC1768 and all the can functions are working as expected (read, write, set frequency, reading via an interrupt using 'attach'). It would be great is someone could respond with a working code snippet / explanation. Thanks!

posted by Chris Mabey 17 Oct 2017

2 Answers

Rob Crain
poster
6 years, 8 months ago.

Thanks Peter for your reply. But as I mentioned in my previous message everything is working correctly for the Read/Write to the bus and messages are be Rx correctly. It's when I set the bus speed using CAN.frequency (int hz) or filters it is not working.

Setting the frequency always returns 0

    if(!can1.frequency(500000))
    printf("baud rate faild");

6 years, 8 months ago.

You have to specify the baud rate with the frequency command. The MCP2551 is only a level translator. First start without filter. The usage of filter is to listen only to the messages which are addressed to my node. To get the system running it is more useful to receive all messages on the bus.