Skip to content

Add CAN api filter support and LPC11CXX CAN implementation #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 25, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion libraries/mbed/api/CAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,13 @@ class CAN {
/** Read a CANMessage from the bus.
*
* @param msg A CANMessage to read to.
* @param handle message filter handle (0 for any message)
*
* @returns
* 0 if no message arrived,
* 1 if message arrived
*/
int read(CANMessage &msg);
int read(CANMessage &msg, int handle = 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please explain a bit the usage of "handle" in this context (both in read and filter functions) ? Do you think that this "handle" mechanism is generic enough to implement CAN message filtering on all platforms with a CAN transceiver?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the LPC11CXX the handle is the message object index (1-32). 32 is used for transmitting message and 1 is used by default as the accept-all mailbox, so by overriding it you disable acceptance of any message.
If there are no more mailboxes available the function will return 0.

Now that I think of it it might be better to make 31 the defailt object for accept-all, as messages are accepted and prioritized by the message boxes in order. This way you can make sure you don't loose the message you're interested in while still receiving any other message as well assuming you can read fast enough.

I think it should be relatively easy to implement the same scheme using the Acceptance Filter Mode on the LPC17XX. Everytime you call can->filter you add it to the identifier table, where handle returns the index in that table.

Here's a typical usage scenario:

int h1,h2;
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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed comment. I'm not at all that familiar with CAN, so I'd like to think a bit more if this method is really universal enough. The problem is not really in the HAL, but can_api.h (below) also uses an "int handle" and that has to be good enough for all platforms/CAN transceivers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "can_api.h also uses an int handle"?

Here's someone who implemented filtering for the LPC1768:
http://mbed.org/users/ym1784/code/CAN_MONITOR/file/fe1f886dff76/main.cpp

Here cnt1 would be the handle. If I understand the datasheet correctly the can peripheral can also automatically move the received message from the buffer to RAM. Calling can->read can then return the requested message from RAM

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that the type of the handle in api/CAN.h (read and filter) is an int and I'm not sure if this is generic enough. Thanks for the link, I'll take a closer look at the LPC1768 implementation.


/** Reset CAN interface.
*
Expand Down Expand Up @@ -169,6 +170,19 @@ class CAN {
*/
int mode(Mode mode);

/** Filter out incomming messages
*
* @param id the id to filter on
* @param mask the mask applied to the id
* @param format format to filter on (Default CANAny)
* @param handle message filter handle (Optional)
*
* @returns
* 0 if filter change failed or unsupported,
* new filter handle if successful
*/
int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);

/** Returns number of read errors to detect read overflow errors.
*/
unsigned char rderror();
Expand Down
3 changes: 2 additions & 1 deletion libraries/mbed/api/can_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ extern "C" {

enum CANFormat {
CANStandard = 0,
CANExtended = 1
CANExtended = 1,
CANAny = 2
};
typedef enum CANFormat CANFormat;

Expand Down
30 changes: 17 additions & 13 deletions libraries/mbed/common/CAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ int CAN::write(CANMessage msg) {
return can_write(&_can, msg, 0);
}

int CAN::read(CANMessage &msg) {
return can_read(&_can, &msg);
int CAN::read(CANMessage &msg, int handle) {
return can_read(&_can, &msg, handle);
}

void CAN::reset() {
Expand All @@ -63,19 +63,23 @@ int CAN::mode(Mode mode) {
return can_mode(&_can, (CanMode)mode);
}

void CAN::attach(void (*fptr)(void), IrqType type) {
if (fptr) {
_irq[(CanIrqType)type].attach(fptr);
can_irq_set(&_can, (CanIrqType)type, 1);
} else {
can_irq_set(&_can, (CanIrqType)type, 0);
}
}
int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle) {
return can_filter(&_can, id, mask, format, handle);
}

void CAN::_irq_handler(uint32_t id, CanIrqType type) {
CAN *handler = (CAN*)id;
handler->_irq[type].call();
void CAN::attach(void (*fptr)(void), IrqType type) {
if (fptr) {
_irq[(CanIrqType)type].attach(fptr);
can_irq_set(&_can, (CanIrqType)type, 1);
} else {
can_irq_set(&_can, (CanIrqType)type, 0);
}
}

void CAN::_irq_handler(uint32_t id, CanIrqType type) {
CAN *handler = (CAN*)id;
handler->_irq[type].call();
}

} // namespace mbed

Expand Down
3 changes: 2 additions & 1 deletion libraries/mbed/hal/can_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ void can_irq_free (can_t *obj);
void can_irq_set (can_t *obj, CanIrqType irq, uint32_t enable);

int can_write (can_t *obj, CAN_Message, int cc);
int can_read (can_t *obj, CAN_Message *msg);
int can_read (can_t *obj, CAN_Message *msg, int handle);
int can_mode (can_t *obj, CanMode mode);
int can_filter (can_t *obj, unsigned int id, unsigned int mask, CANFormat format, int handle);
void can_reset (can_t *obj);
unsigned char can_rderror (can_t *obj);
unsigned char can_tderror (can_t *obj);
Expand Down
1,768 changes: 1,768 additions & 0 deletions libraries/mbed/targets/cmsis/TARGET_NXP/TARGET_LPC11XX_11CXX/bitfields.h

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@

#include "LPC11xx.h"
#include "cmsis_nvic.h"
#include "bitfields.h"

#endif
Loading