Skip to content

Commit

Permalink
CAN: Consider bus/bridge number for address claiming
Browse files Browse the repository at this point in the history
Narrow down the received address claim message filter used by the
ThingSet CAN stack to the local bus for that interface.
  • Loading branch information
martinjaeger committed May 14, 2024
1 parent c1040ed commit 282c892
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions include/thingset/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ extern "C" {
(((uint32_t)id & THINGSET_CAN_MSG_NO_MASK) >> THINGSET_CAN_MSG_NO_POS)

/* bus numbers for request/response messages and multi-frame reports */
#ifdef CONFIG_THINGSET_CAN_ROUTING_BUSES
#define THINGSET_CAN_SOURCE_BUS_POS (16U)
#define THINGSET_CAN_SOURCE_BUS_MASK (0xF << THINGSET_CAN_SOURCE_BUS_POS)
#define THINGSET_CAN_SOURCE_BUS_SET(id) \
Expand All @@ -143,15 +144,18 @@ extern "C" {
#define THINGSET_CAN_TARGET_BUS_GET(id) \
(((uint32_t)id & THINGSET_CAN_TARGET_BUS_MASK) >> THINGSET_CAN_TARGET_BUS_POS)
#define THINGSET_CAN_TARGET_BUS_DEFAULT (0x0)
#endif /* CONFIG_THINGSET_CAN_ROUTING_BUSES */

/* bridge numbers for request/response messages and multi-frame reports */
#ifdef CONFIG_THINGSET_CAN_ROUTING_BRIDGES
#define THINGSET_CAN_BRIDGE_POS (16U)
#define THINGSET_CAN_BRIDGE_MASK (0xFF << THINGSET_CAN_BRIDGE_POS)
#define THINGSET_CAN_BRIDGE_SET(id) \
(((uint32_t)id << THINGSET_CAN_BRIDGE_POS) & THINGSET_CAN_BRIDGE_MASK)
#define THINGSET_CAN_BRIDGE_GET(id) \
(((uint32_t)id & THINGSET_CAN_BRIDGE_MASK) >> THINGSET_CAN_BRIDGE_POS)
#define THINGSET_CAN_BRIDGE_LOCAL (0x00)
#endif /* CONFIG_THINGSET_CAN_ROUTING_BRIDGES */

/* random number for address discovery messages */
#define THINGSET_CAN_RAND_POS (16U)
Expand Down
11 changes: 10 additions & 1 deletion src/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static const struct can_filter mf_report_filter = {
};
#endif /* CONFIG_THINGSET_CAN_REPORT_RX */

static const struct can_filter addr_claim_filter = {
static struct can_filter addr_claim_filter = {
.id = THINGSET_CAN_TYPE_NETWORK | THINGSET_CAN_TARGET_SET(THINGSET_CAN_ADDR_BROADCAST),
.mask = THINGSET_CAN_TYPE_MASK | THINGSET_CAN_TARGET_MASK,
.flags = CAN_FILTER_IDE,
Expand Down Expand Up @@ -585,6 +585,15 @@ int thingset_can_init_inst(struct thingset_can *ts_can, const struct device *can
ts_can->dev = can_dev;
ts_can->route = bus_number;

#ifdef CONFIG_THINGSET_CAN_ROUTING_BUSES
addr_claim_filter.id |=
THINGSET_CAN_TARGET_BUS_SET(bus_number) | THINGSET_CAN_SOURCE_BUS_SET(bus_number);
addr_claim_filter.mask |= THINGSET_CAN_TARGET_BUS_MASK | THINGSET_CAN_SOURCE_BUS_MASK;
#elif defined(CONFIG_THINGSET_CAN_ROUTING_BRIDGES)
addr_claim_filter.id |= THINGSET_CAN_BRIDGE_SET(bus_number);
addr_claim_filter.mask |= THINGSET_CAN_BRIDGE_MASK;
#endif

/* set initial address (will be changed if already used on the bus) */
if (ts_can->node_addr < THINGSET_CAN_ADDR_MIN || ts_can->node_addr > THINGSET_CAN_ADDR_MAX) {
ts_can->node_addr = THINGSET_CAN_ADDR_MIN;
Expand Down

0 comments on commit 282c892

Please sign in to comment.