From 88fb5e2edbbe1edf48778fbe1b0e3af51d51227b Mon Sep 17 00:00:00 2001 From: Henrik Brix Andersen Date: Mon, 27 May 2024 10:29:18 +0200 Subject: [PATCH] drivers: can: shell: print device name in RX path Include the device name when printing received CAN frames. This improves the user experience when working with multiple CAN controllers via the CAN shell. Signed-off-by: Henrik Brix Andersen --- doc/hardware/peripherals/can/shell.rst | 18 +++++++----- drivers/can/can_shell.c | 38 ++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/doc/hardware/peripherals/can/shell.rst b/doc/hardware/peripherals/can/shell.rst index d10759ac302..ed36379c55f 100644 --- a/doc/hardware/peripherals/can/shell.rst +++ b/doc/hardware/peripherals/can/shell.rst @@ -186,16 +186,20 @@ Received CAN frames matching the added filter(s) are printed to the shell. A few .. code-block:: console - # Flags ID Size Data bytes - -- 010 [8] 01 02 03 04 05 06 07 08 - B- 010 [08] 01 02 03 04 05 06 07 08 - BP 010 [03] 01 aa bb - -- 00000010 [0] - -- 010 [1] 20 - -- 010 [8] remote transmission request + # Dev Flags ID Size Data bytes + can0 -- 010 [8] 01 02 03 04 05 06 07 08 + can0 B- 010 [08] 01 02 03 04 05 06 07 08 + can0 BP 010 [03] 01 aa bb + can0 -- 00000010 [0] + can0 -- 010 [1] 20 + can0 -- 010 [8] remote transmission request The columns have the following meaning: +* Dev + + * Name of the device receiving the frame. + * Flags * ``B``: The frame has the CAN FD Baud Rate Switch (BRS) flag set. diff --git a/drivers/can/can_shell.c b/drivers/can/can_shell.c index d7c9fd4076d..aaed76b9fa5 100644 --- a/drivers/can/can_shell.c +++ b/drivers/can/can_shell.c @@ -20,6 +20,11 @@ struct can_shell_tx_event { int error; }; +struct can_shell_rx_event { + struct can_frame frame; + const struct device *dev; +}; + struct can_shell_mode_mapping { const char *name; can_mode_t mode; @@ -49,7 +54,8 @@ static struct k_poll_event can_shell_tx_msgq_events[] = { &can_shell_tx_msgq, 0) }; -CAN_MSGQ_DEFINE(can_shell_rx_msgq, CONFIG_CAN_SHELL_RX_QUEUE_SIZE); +K_MSGQ_DEFINE(can_shell_rx_msgq, sizeof(struct can_shell_rx_event), + CONFIG_CAN_SHELL_RX_QUEUE_SIZE, 4); const struct shell *can_shell_rx_msgq_sh; static struct k_work_poll can_shell_rx_msgq_work; static struct k_poll_event can_shell_rx_msgq_events[] = { @@ -62,7 +68,8 @@ static struct k_poll_event can_shell_rx_msgq_events[] = { static void can_shell_tx_msgq_triggered_work_handler(struct k_work *work); static void can_shell_rx_msgq_triggered_work_handler(struct k_work *work); -static void can_shell_print_frame(const struct shell *sh, const struct can_frame *frame) +static void can_shell_print_frame(const struct shell *sh, const struct device *dev, + const struct can_frame *frame) { uint8_t nbytes = can_dlc_to_bytes(frame->dlc); int i; @@ -72,6 +79,8 @@ static void can_shell_print_frame(const struct shell *sh, const struct can_frame shell_fprintf(sh, SHELL_NORMAL, "(%05d) ", frame->timestamp); #endif /* CONFIG_CAN_RX_TIMESTAMP */ + shell_fprintf(sh, SHELL_NORMAL, "%s ", dev->name); + #ifdef CONFIG_CAN_FD_MODE /* Flags */ shell_fprintf(sh, SHELL_NORMAL, "%c%c ", @@ -156,6 +165,23 @@ static void can_shell_tx_callback(const struct device *dev, int error, void *use } } +static void can_shell_rx_callback(const struct device *dev, struct can_frame *frame, + void *user_data) +{ + struct can_shell_rx_event event; + int err; + + ARG_UNUSED(user_data); + + event.frame = *frame; + event.dev = dev; + + err = k_msgq_put(&can_shell_rx_msgq, &event, K_NO_WAIT); + if (err != 0) { + LOG_ERR("CAN shell rx event queue full"); + } +} + static int can_shell_rx_msgq_poll_submit(const struct shell *sh) { int err; @@ -177,10 +203,10 @@ static int can_shell_rx_msgq_poll_submit(const struct shell *sh) static void can_shell_rx_msgq_triggered_work_handler(struct k_work *work) { - struct can_frame frame; + struct can_shell_rx_event event; - while (k_msgq_get(&can_shell_rx_msgq, &frame, K_NO_WAIT) == 0) { - can_shell_print_frame(can_shell_rx_msgq_sh, &frame); + while (k_msgq_get(&can_shell_rx_msgq, &event, K_NO_WAIT) == 0) { + can_shell_print_frame(can_shell_rx_msgq_sh, event.dev, &event.frame); } (void)can_shell_rx_msgq_poll_submit(can_shell_rx_msgq_sh); @@ -879,7 +905,7 @@ static int cmd_can_filter_add(const struct shell *sh, size_t argc, char **argv) (filter.flags & CAN_FILTER_IDE) != 0 ? 8 : 3, filter.id, (filter.flags & CAN_FILTER_IDE) != 0 ? 8 : 3, filter.mask); - err = can_add_rx_filter_msgq(dev, &can_shell_rx_msgq, &filter); + err = can_add_rx_filter(dev, can_shell_rx_callback, NULL, &filter); if (err < 0) { shell_error(sh, "failed to add filter (err %d)", err); return err;