mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-17 05:12:57 +01:00
drivers/mcp2515: apply the filter to a target mailbox
sys/include/can: add the mailbox target as a member to the filter structure drivers/mcp2515: cleanup defines
This commit is contained in:
parent
d9fc082686
commit
bc0d3f9de3
@ -421,6 +421,8 @@ static int _get(candev_t *candev, canopt_t opt, void *value, size_t max_len)
|
|||||||
static int _set_filter(candev_t *dev, const struct can_filter *filter)
|
static int _set_filter(candev_t *dev, const struct can_filter *filter)
|
||||||
{
|
{
|
||||||
DEBUG("inside _set_filter of MCP2515\n");
|
DEBUG("inside _set_filter of MCP2515\n");
|
||||||
|
assert(filter->target_mailbox < MCP2515_RX_MAILBOXES);
|
||||||
|
|
||||||
struct can_filter f = *filter;
|
struct can_filter f = *filter;
|
||||||
int res = -1;
|
int res = -1;
|
||||||
enum mcp2515_mode mode;
|
enum mcp2515_mode mode;
|
||||||
@ -449,58 +451,66 @@ static int _set_filter(candev_t *dev, const struct can_filter *filter)
|
|||||||
f.can_mask &= CAN_SFF_MASK;
|
f.can_mask &= CAN_SFF_MASK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Browse on each mailbox to find an empty space */
|
/* mask unused */
|
||||||
int mailbox_index = 0;
|
if (dev_mcp->masks[f.target_mailbox] == 0) {
|
||||||
|
if (mutex_trylock(&_mcp_mutex)) {
|
||||||
while (mailbox_index < MCP2515_RX_MAILBOXES) {
|
|
||||||
/* mask unused */
|
|
||||||
if (dev_mcp->masks[mailbox_index] == 0) {
|
|
||||||
/* set mask */
|
/* set mask */
|
||||||
mcp2515_set_mask(dev_mcp, mailbox_index, f.can_mask);
|
mcp2515_set_mask(dev_mcp, f.target_mailbox, f.can_mask);
|
||||||
/* set filter */
|
/* set filter */
|
||||||
mcp2515_set_filter(dev_mcp, MCP2515_FILTERS_MB0 * mailbox_index,
|
mcp2515_set_filter(dev_mcp, MCP2515_FILTERS_MB0 * f.target_mailbox,
|
||||||
f.can_id);
|
f.can_id);
|
||||||
|
mutex_unlock(&_mcp_mutex);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
DEBUG("setfilt2_Failed to lock mutex\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* save filter */
|
||||||
|
dev_mcp->masks[f.target_mailbox] = f.can_mask;
|
||||||
|
dev_mcp->filter_ids[f.target_mailbox][0] = f.can_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* mask existed and same mask */
|
||||||
|
else if (dev_mcp->masks[f.target_mailbox] == f.can_mask) {
|
||||||
|
/* find an empty space if it exists */
|
||||||
|
int filter_pos = 1; /* first one is already filled */
|
||||||
|
/* stop at the end of mailbox or an empty space found */
|
||||||
|
while (filter_pos < _max_filters(f.target_mailbox) &&
|
||||||
|
dev_mcp->filter_ids[f.target_mailbox][filter_pos] != 0) {
|
||||||
|
filter_pos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* an empty space is found */
|
||||||
|
if (filter_pos < _max_filters(f.target_mailbox)) {
|
||||||
|
/* set filter on this memory space */
|
||||||
|
if (mutex_trylock(&_mcp_mutex)) {
|
||||||
|
mcp2515_set_filter(dev_mcp,
|
||||||
|
MCP2515_FILTERS_MB0 * f.target_mailbox + filter_pos,
|
||||||
|
f.can_id);
|
||||||
|
mutex_unlock(&_mcp_mutex);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
DEBUG("setfilt3_Failed to lock mutex");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/* save filter */
|
/* save filter */
|
||||||
dev_mcp->masks[mailbox_index] = f.can_mask;
|
dev_mcp->filter_ids[f.target_mailbox][filter_pos] = f.can_id;
|
||||||
dev_mcp->filter_ids[mailbox_index][0] = f.can_id;
|
|
||||||
|
|
||||||
/* function succeeded */
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
/* No empty space is found */
|
||||||
/* mask existed and same mask */
|
else {
|
||||||
else if (dev_mcp->masks[mailbox_index] == f.can_mask) {
|
DEBUG_PUTS("no empty space found");
|
||||||
/* find en empty space if it exist */
|
if (mutex_trylock(&_mcp_mutex)) {
|
||||||
int filter_pos = 1; /* first one is already filled */
|
mcp2515_set_mode(dev_mcp, mode);
|
||||||
/* stop at the end of mailbox or an empty space found */
|
mutex_unlock(&_mcp_mutex);
|
||||||
while (filter_pos < _max_filters(mailbox_index) &&
|
|
||||||
dev_mcp->filter_ids[mailbox_index][filter_pos] != 0) {
|
|
||||||
filter_pos++;
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
/* an empty space is found */
|
DEBUG_PUTS("setfilt4_Failed to lock mutex");
|
||||||
if (filter_pos < _max_filters(mailbox_index)) {
|
return -1;
|
||||||
/* set filter on this memory space */
|
|
||||||
if (mutex_trylock(&_mcp_mutex)) {
|
|
||||||
mcp2515_set_filter(dev_mcp,
|
|
||||||
MCP2515_FILTERS_MB0 * mailbox_index + filter_pos,
|
|
||||||
f.can_id);
|
|
||||||
mutex_unlock(&_mcp_mutex);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
DEBUG("setfilt2_Failed to lock mutex");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* save filter */
|
|
||||||
dev_mcp->filter_ids[mailbox_index][filter_pos] = f.can_id;
|
|
||||||
|
|
||||||
/* function succeeded */
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
mailbox_index++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mutex_trylock(&_mcp_mutex)) {
|
if (mutex_trylock(&_mcp_mutex)) {
|
||||||
@ -508,7 +518,7 @@ static int _set_filter(candev_t *dev, const struct can_filter *filter)
|
|||||||
mutex_unlock(&_mcp_mutex);
|
mutex_unlock(&_mcp_mutex);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
DEBUG("setfilt3_Failed to lock mutex");
|
DEBUG("setfilt5_Failed to lock mutex");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -303,9 +303,6 @@ extern "C" {
|
|||||||
#define MCP2515_RXB0CTRL_MODE_RECV_FILTER 0x00
|
#define MCP2515_RXB0CTRL_MODE_RECV_FILTER 0x00
|
||||||
#define MCP2515_RXB0CTRL_RXM0 0x20
|
#define MCP2515_RXB0CTRL_RXM0 0x20
|
||||||
#define MCP2515_RXB0CTRL_RXM1 0x40
|
#define MCP2515_RXB0CTRL_RXM1 0x40
|
||||||
#define MCP2515_RXB0CTRL_MODE_RECV_STD_OR_EXT 0x00
|
|
||||||
#define MCP2515_RXB0CTRL_MODE_RECV_STD MCP2515_RXB0CTRL_RXM0
|
|
||||||
#define MCP2515_RXB0CTRL_MODE_RECV_EXT MCP2515_RXB0CTRL_RXM1
|
|
||||||
#define MCP2515_RXB0CTRL_MODE_RECV_ALL (MCP2515_RXB0CTRL_RXM1 | \
|
#define MCP2515_RXB0CTRL_MODE_RECV_ALL (MCP2515_RXB0CTRL_RXM1 | \
|
||||||
MCP2515_RXB0CTRL_RXM0)
|
MCP2515_RXB0CTRL_RXM0)
|
||||||
|
|
||||||
@ -316,9 +313,6 @@ extern "C" {
|
|||||||
#define MCP2515_RXB1CTRL_MODE_RECV_FILTER 0x00
|
#define MCP2515_RXB1CTRL_MODE_RECV_FILTER 0x00
|
||||||
#define MCP2515_RXB1CTRL_RXM0 0x20
|
#define MCP2515_RXB1CTRL_RXM0 0x20
|
||||||
#define MCP2515_RXB1CTRL_RXM1 0x40
|
#define MCP2515_RXB1CTRL_RXM1 0x40
|
||||||
#define MCP2515_RXB1CTRL_MODE_RECV_STD_OR_EXT 0x00
|
|
||||||
#define MCP2515_RXB1CTRL_MODE_RECV_STD MCP2515_RXB1CTRL_RXM0
|
|
||||||
#define MCP2515_RXB1CTRL_MODE_RECV_EXT MCP2515_RXB1CTRL_RXM1
|
|
||||||
#define MCP2515_RXB1CTRL_MODE_RECV_ALL (MCP2515_RXB1CTRL_RXM1 | \
|
#define MCP2515_RXB1CTRL_MODE_RECV_ALL (MCP2515_RXB1CTRL_RXM1 | \
|
||||||
MCP2515_RXB1CTRL_RXM0)
|
MCP2515_RXB1CTRL_RXM0)
|
||||||
/** @} */
|
/** @} */
|
||||||
|
@ -101,6 +101,9 @@ struct can_frame {
|
|||||||
struct can_filter {
|
struct can_filter {
|
||||||
canid_t can_id; /**< CAN ID */
|
canid_t can_id; /**< CAN ID */
|
||||||
canid_t can_mask; /**< Mask */
|
canid_t can_mask; /**< Mask */
|
||||||
|
#if defined(MODULE_MCP2515)
|
||||||
|
uint8_t target_mailbox; /**< The mailbox to apply the filter to */
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user