Skip to content

Commit 9bd97eb

Browse files
henrikbrixandersencarlescufi
authored andcommitted
drivers: can: add CAN_MODE_FD
Add support for enabling/disabling CAN-FD frame transmission/reception at run-time. Fixes: #45303 Signed-off-by: Henrik Brix Andersen <[email protected]>
1 parent 6a0f3ff commit 9bd97eb

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

drivers/can/can_mcan.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,17 @@ int can_mcan_set_mode(const struct device *dev, can_mode_t mode)
246246
struct can_mcan_reg *can = cfg->can;
247247
int ret;
248248

249+
#ifdef CONFIG_CAN_FD_MODE
250+
if ((mode & ~(CAN_MODE_LOOPBACK | CAN_MODE_LISTENONLY | CAN_MODE_FD)) != 0) {
251+
LOG_ERR("unsupported mode: 0x%08x", mode);
252+
return -ENOTSUP;
253+
}
254+
#else
249255
if ((mode & ~(CAN_MODE_LOOPBACK | CAN_MODE_LISTENONLY)) != 0) {
250256
LOG_ERR("unsupported mode: 0x%08x", mode);
251257
return -ENOTSUP;
252258
}
259+
#endif /* CONFIG_CAN_FD_MODE */
253260

254261
if (cfg->phy != NULL) {
255262
ret = can_transceiver_enable(cfg->phy);
@@ -289,6 +296,14 @@ int can_mcan_set_mode(const struct device *dev, can_mode_t mode)
289296
can->cccr &= ~CAN_MCAN_CCCR_MON;
290297
}
291298

299+
#ifdef CONFIG_CAN_FD_MODE
300+
if ((mode & CAN_MODE_FD) != 0) {
301+
can->cccr |= CAN_MCAN_CCCR_FDOE | CAN_MCAN_CCCR_BRSE;
302+
} else {
303+
can->cccr &= ~(CAN_MCAN_CCCR_FDOE | CAN_MCAN_CCCR_BRSE);
304+
}
305+
#endif /* CONFIG_CAN_FD_MODE */
306+
292307
ret = can_leave_init_mode(can, K_MSEC(CAN_INIT_TIMEOUT));
293308
if (ret) {
294309
LOG_ERR("Failed to leave init mode");
@@ -407,13 +422,8 @@ int can_mcan_init(const struct device *dev)
407422
/ 16 + 5) << CAN_MCAN_RXESC_RBDS_POS);
408423
}
409424
#endif
410-
411-
#ifdef CONFIG_CAN_FD_MODE
412-
can->cccr |= CAN_MCAN_CCCR_FDOE | CAN_MCAN_CCCR_BRSE;
413-
#else
414-
can->cccr &= ~(CAN_MCAN_CCCR_FDOE | CAN_MCAN_CCCR_BRSE);
415-
#endif
416-
can->cccr &= ~(CAN_MCAN_CCCR_TEST | CAN_MCAN_CCCR_MON |
425+
can->cccr &= ~(CAN_MCAN_CCCR_FDOE | CAN_MCAN_CCCR_BRSE |
426+
CAN_MCAN_CCCR_TEST | CAN_MCAN_CCCR_MON |
417427
CAN_MCAN_CCCR_ASM);
418428
can->test &= ~(CAN_MCAN_TEST_LBCK);
419429

include/zephyr/drivers/can.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ extern "C" {
9494
/** Controller is not allowed to send dominant bits. */
9595
#define CAN_MODE_LISTENONLY BIT(1)
9696

97+
/** Controller allows transmitting/receiving CAN-FD frames. */
98+
#define CAN_MODE_FD BIT(2)
99+
97100
/** @} */
98101

99102
/**

tests/drivers/can/canfd/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ static void test_set_loopback(void)
349349
{
350350
int err;
351351

352-
err = can_set_mode(can_dev, CAN_MODE_LOOPBACK);
352+
err = can_set_mode(can_dev, CAN_MODE_LOOPBACK | CAN_MODE_FD);
353353
zassert_equal(err, 0, "failed to set loopback-mode (err %d)", err);
354354
}
355355

0 commit comments

Comments
 (0)