diff --git a/drivers/mcp2515/candev_mcp2515.c b/drivers/mcp2515/candev_mcp2515.c index 7189297369..beb0bd05d3 100644 --- a/drivers/mcp2515/candev_mcp2515.c +++ b/drivers/mcp2515/candev_mcp2515.c @@ -421,7 +421,6 @@ 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) { DEBUG("inside _set_filter of MCP2515\n"); - bool filter_added = true; struct can_filter f = *filter; int res = -1; enum mcp2515_mode mode; @@ -453,7 +452,7 @@ static int _set_filter(candev_t *dev, const struct can_filter *filter) /* Browse on each mailbox to find an empty space */ int mailbox_index = 0; - while (mailbox_index < MCP2515_RX_MAILBOXES && !filter_added) { + while (mailbox_index < MCP2515_RX_MAILBOXES) { /* mask unused */ if (dev_mcp->masks[mailbox_index] == 0) { /* set mask */ @@ -467,7 +466,7 @@ static int _set_filter(candev_t *dev, const struct can_filter *filter) dev_mcp->filter_ids[mailbox_index][0] = f.can_id; /* function succeeded */ - filter_added = true; + break; } /* mask existed and same mask */ @@ -498,7 +497,7 @@ static int _set_filter(candev_t *dev, const struct can_filter *filter) dev_mcp->filter_ids[mailbox_index][filter_pos] = f.can_id; /* function succeeded */ - filter_added = true; + break; } } mailbox_index++; @@ -513,7 +512,8 @@ static int _set_filter(candev_t *dev, const struct can_filter *filter) return -1; } - return filter_added; + /* Filter added */ + return 0; } static int _remove_filter(candev_t *dev, const struct can_filter *filter) diff --git a/drivers/mcp2515/mcp2515.c b/drivers/mcp2515/mcp2515.c index ae7cec0a8d..158537fa99 100644 --- a/drivers/mcp2515/mcp2515.c +++ b/drivers/mcp2515/mcp2515.c @@ -108,7 +108,15 @@ int mcp2515_init(candev_mcp2515_t *dev, void (*irq_handler_cb)(void *)) return -1; } - uint8_t cmd = MCP2515_RXB0CTRL_MODE_RECV_ALL; + uint8_t cmd; + if (IS_ACTIVE(MCP2515_RECV_FILTER_EN)) { + DEBUG_PUTS("filtering enabled"); + cmd = MCP2515_RXB0CTRL_MODE_RECV_FILTER; + } + else { + DEBUG_PUTS("filtering disabled"); + cmd = MCP2515_RXB0CTRL_MODE_RECV_ALL; + } res = mcp2515_spi_write(dev, MCP2515_RXB0CTRL, &cmd, 1); if (res < 0) { diff --git a/drivers/mcp2515/mcp2515.h b/drivers/mcp2515/mcp2515.h index 62cc31137d..65f1126396 100644 --- a/drivers/mcp2515/mcp2515.h +++ b/drivers/mcp2515/mcp2515.h @@ -71,6 +71,11 @@ enum mcp2515_error { /** Wake up source */ #define MCP2515_WKUP_SRC_INTERNAL 1 +/** Acceptance mode (enable/disable filtering) */ +#ifndef MCP2515_RECV_FILTER_EN +#define MCP2515_RECV_FILTER_EN 0 +#endif + /** * @brief Initialize pins and SPI interface * diff --git a/drivers/mcp2515/mcp2515_defines.h b/drivers/mcp2515/mcp2515_defines.h index bf9cb641c8..bcc8d8baea 100644 --- a/drivers/mcp2515/mcp2515_defines.h +++ b/drivers/mcp2515/mcp2515_defines.h @@ -300,6 +300,7 @@ extern "C" { #define MCP2515_RXB0CTRL_BUKT1 0x02 #define MCP2515_RXB0CTRL_BUKT 0x04 #define MCP2515_RXB0CTRL_RXRTR 0x08 +#define MCP2515_RXB0CTRL_MODE_RECV_FILTER 0x00 #define MCP2515_RXB0CTRL_RXM0 0x20 #define MCP2515_RXB0CTRL_RXM1 0x40 #define MCP2515_RXB0CTRL_MODE_RECV_STD_OR_EXT 0x00 @@ -312,6 +313,7 @@ extern "C" { #define MCP2515_RXB1CTRL_FILHIT1 0x02 #define MCP2515_RXB1CTRL_FILHIT2 0x04 #define MCP2515_RXB1CTRL_RXRTR 0x08 +#define MCP2515_RXB1CTRL_MODE_RECV_FILTER 0x00 #define MCP2515_RXB1CTRL_RXM0 0x20 #define MCP2515_RXB1CTRL_RXM1 0x40 #define MCP2515_RXB1CTRL_MODE_RECV_STD_OR_EXT 0x00