Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions doc/hardware/peripherals/canbus/controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ a mailbox. When a transmitting mailbox is assigned, sending cannot be canceled.
.. code-block:: C

struct can_frame frame = {
.id_type = CAN_STANDARD_IDENTIFIER,
.rtr = CAN_DATAFRAME,
.flags = 0,
.id = 0x123,
.dlc = 8,
.data = {1,2,3,4,5,6,7,8}
Expand Down Expand Up @@ -187,8 +186,7 @@ occurred. It does not block until the message is sent like the example above.
int send_function(const struct device *can_dev)
{
struct can_frame frame = {
.id_type = CAN_EXTENDED_IDENTIFIER,
.rtr = CAN_DATAFRAME,
.flags = CAN_FRAME_IDE,
.id = 0x1234567,
.dlc = 2
};
Expand Down Expand Up @@ -227,10 +225,8 @@ The filter for this example is configured to match the identifier 0x123 exactly.
.. code-block:: C

const struct can_filter my_filter = {
.id_type = CAN_STANDARD_IDENTIFIER,
.rtr = CAN_DATAFRAME,
.flags = CAN_FILTER_DATA,
.id = 0x123,
.rtr_mask = 1,
.id_mask = CAN_STD_ID_MASK
};
int filter_id;
Expand All @@ -252,10 +248,8 @@ The filter for this example is configured to match the extended identifier
.. code-block:: C

const struct can_filter my_filter = {
.id_type = CAN_EXTENDED_IDENTIFIER,
.rtr = CAN_DATAFRAME,
.flags = CAN_FILTER_DATA | CAN_FILTER_IDE,
.id = 0x1234567,
.rtr_mask = 1,
.id_mask = CAN_EXT_ID_MASK
};
CAN_MSGQ_DEFINE(my_can_msgq, 2);
Expand Down
4 changes: 2 additions & 2 deletions drivers/can/can_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ static inline int z_vrfy_can_set_bitrate_data(const struct device *dev,

#endif /* CONFIG_CAN_FD_MODE */

static inline int z_vrfy_can_get_max_filters(const struct device *dev, enum can_ide id_type)
static inline int z_vrfy_can_get_max_filters(const struct device *dev, bool ide)
{
/* Optional API function */
Z_OOPS(Z_SYSCALL_OBJ(dev, K_OBJ_DRIVER_CAN));

return z_impl_can_get_max_filters(dev, id_type);
return z_impl_can_get_max_filters(dev, ide);
}
#include <syscalls/can_get_max_filters_mrsh.c>

Expand Down
57 changes: 37 additions & 20 deletions drivers/can/can_loopback.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ struct can_loopback_filter {
struct can_loopback_data {
struct can_loopback_filter filters[CONFIG_CAN_MAX_FILTER];
struct k_mutex mtx;
bool loopback;
struct k_msgq tx_msgq;
char msgq_buffer[CONFIG_CAN_LOOPBACK_TX_MSGQ_SIZE * sizeof(struct can_loopback_frame)];
struct k_thread tx_thread_data;
bool started;
bool loopback;
#ifdef CONFIG_CAN_FD_MODE
bool fd;
#endif /* CONFIG_CAN_FD_MODE */

K_KERNEL_STACK_MEMBER(tx_thread_stack,
CONFIG_CAN_LOOPBACK_TX_THREAD_STACK_SIZE);
Expand All @@ -51,9 +54,8 @@ static void dispatch_frame(const struct device *dev,

LOG_DBG("Receiving %d bytes. Id: 0x%x, ID type: %s %s",
frame->dlc, frame->id,
frame->id_type == CAN_STANDARD_IDENTIFIER ?
"standard" : "extended",
frame->rtr == CAN_DATAFRAME ? "" : ", RTR frame");
(frame->flags & CAN_FRAME_IDE) != 0 ? "extended" : "standard",
(frame->flags & CAN_FRAME_RTR) != 0 ? ", RTR frame" : "");

filter->rx_cb(dev, &frame_tmp, filter->cb_arg);
}
Expand Down Expand Up @@ -99,15 +101,29 @@ static int can_loopback_send(const struct device *dev,

LOG_DBG("Sending %d bytes on %s. Id: 0x%x, ID type: %s %s",
frame->dlc, dev->name, frame->id,
frame->id_type == CAN_STANDARD_IDENTIFIER ?
"standard" : "extended",
frame->rtr == CAN_DATAFRAME ? "" : ", RTR frame");
(frame->flags & CAN_FRAME_IDE) != 0 ? "extended" : "standard",
(frame->flags & CAN_FRAME_RTR) != 0 ? ", RTR frame" : "");

#ifdef CONFIG_CAN_FD_MODE
if (frame->fd != 0) {
if ((frame->flags & ~(CAN_FRAME_IDE | CAN_FRAME_RTR |
CAN_FRAME_FDF | CAN_FRAME_BRS)) != 0) {
LOG_ERR("unsupported CAN frame flags 0x%02x", frame->flags);
return -ENOTSUP;
}

if ((frame->flags & CAN_FRAME_FDF) != 0) {
if (!data->fd) {
return -ENOTSUP;
}

max_dlc = CANFD_MAX_DLC;
}
#endif /* CONFIG_CAN_FD_MODE */
#else /* CONFIG_CAN_FD_MODE */
if ((frame->flags & ~(CAN_FRAME_IDE | CAN_FRAME_RTR)) != 0) {
LOG_ERR("unsupported CAN frame flags 0x%02x", frame->flags);
return -ENOTSUP;
}
#endif /* !CONFIG_CAN_FD_MODE */

if (frame->dlc > max_dlc) {
LOG_ERR("DLC of %d exceeds maximum (%d)", frame->dlc, max_dlc);
Expand Down Expand Up @@ -150,14 +166,12 @@ static int can_loopback_add_rx_filter(const struct device *dev, can_rx_callback_
struct can_loopback_filter *loopback_filter;
int filter_id;

LOG_DBG("Setting filter ID: 0x%x, mask: 0x%x", filter->id,
filter->id_mask);
LOG_DBG("Filter type: %s ID %s mask",
filter->id_type == CAN_STANDARD_IDENTIFIER ?
"standard" : "extended",
((filter->id_type && (filter->id_mask == CAN_STD_ID_MASK)) ||
(!filter->id_type && (filter->id_mask == CAN_EXT_ID_MASK))) ?
"with" : "without");
LOG_DBG("Setting filter ID: 0x%x, mask: 0x%x", filter->id, filter->mask);

if ((filter->flags & ~(CAN_FILTER_IDE | CAN_FILTER_DATA | CAN_FILTER_RTR)) != 0) {
LOG_ERR("unsupported CAN filter flags 0x%02x", filter->flags);
return -ENOTSUP;
}

k_mutex_lock(&data->mtx, K_FOREVER);
filter_id = get_free_filter(data->filters);
Expand Down Expand Up @@ -242,14 +256,17 @@ static int can_loopback_set_mode(const struct device *dev, can_mode_t mode)
LOG_ERR("unsupported mode: 0x%08x", mode);
return -ENOTSUP;
}

data->fd = (mode & CAN_MODE_FD) != 0;
#else
if ((mode & ~(CAN_MODE_LOOPBACK)) != 0) {
LOG_ERR("unsupported mode: 0x%08x", mode);
return -ENOTSUP;
}
#endif /* CONFIG_CAN_FD_MODE */

data->loopback = (mode & CAN_MODE_LOOPBACK) != 0 ? 1 : 0;
data->loopback = (mode & CAN_MODE_LOOPBACK) != 0;

return 0;
}

Expand Down Expand Up @@ -335,9 +352,9 @@ static int can_loopback_get_core_clock(const struct device *dev, uint32_t *rate)
return 0;
}

static int can_loopback_get_max_filters(const struct device *dev, enum can_ide id_type)
static int can_loopback_get_max_filters(const struct device *dev, bool ide)
{
ARG_UNUSED(id_type);
ARG_UNUSED(ide);

return CONFIG_CAN_MAX_FILTER;
}
Expand Down
Loading