drivers: can: rename API functions for better consistency

Rename a few CAN API functions for clarity and consistency with other
Zephyr RTOS APIs.

CAN_DEFINE_MSGQ() becomes CAN_MSGQ_DEFINE() to match K_MSGQ_DEFINE().

can_attach_isr() becomes can_add_rx_filter() since a filter callback
function is not an interrupt service routine (although it is called in
isr context). The word "attach" is replaced with "add" since filters are
added, not attached. This matches the terminology used is other Zephyr
APIs better.

can_detach() becomes can_remove_rx_filter() to pair with
can_add_rx_filter().

can_attach_msgq() becomes can_add_rx_filter_msgq() and documentation is
updated to mention its relationship with can_add_rx_filter().

can_register_state_change_isr() becomes can_set_state_change_callback()
since a state change callback function is not an interrupt service
routine (although it is called in isr context). The word "register" is
replaced with "set" since only one state change callback can be in
place.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
This commit is contained in:
Henrik Brix Andersen
2021-12-28 20:00:34 +01:00
committed by Carles Cufí
parent 479215100d
commit 8af4bb722d
26 changed files with 579 additions and 533 deletions

View File

@@ -205,24 +205,24 @@ Receiving
*********
Frames are only received when they match a filter.
The following code snippets show how to receive frames by attaching filters.
The following code snippets show how to receive frames by adding filters.
Here we have an example for a receiving callback as used for
:c:func:`can_attach_isr`. The argument arg is passed when the filter is
attached.
:c:func:`can_add_rx_filter`. The user data argument is passed when the filter is
added.
.. code-block:: C
void rx_callback_function(struct zcan_frame *frame, void *arg)
void rx_callback_function(struct zcan_frame *frame, void *user_data)
{
... do something with the frame ...
}
The following snippet shows how to attach a filter with an interrupt callback.
The following snippet shows how to add a filter with a callback function.
It is the most efficient but also the most critical way to receive messages.
The callback function is called from an interrupt context, which means that the
callback function should be as short as possible and must not block.
Attaching ISRs is not allowed from userspace context.
Adding callback functions is not allowed from userspace context.
The filter for this example is configured to match the identifier 0x123 exactly.
@@ -240,15 +240,15 @@ The filter for this example is configured to match the identifier 0x123 exactly.
can_dev = device_get_binding("CAN_0");
filter_id = can_attach_isr(can_dev, rx_callback_function, callback_arg, &my_filter);
filter_id = can_add_rx_filter(can_dev, rx_callback_function, callback_arg, &my_filter);
if (filter_id < 0) {
LOG_ERR("Unable to attach isr [%d]", filter_id);
LOG_ERR("Unable to add rx filter [%d]", filter_id);
}
Here an example for :c:func:`can_attach_msgq` is shown. With this function, it
is possible to receive frames synchronously. This function can be called from
userspace context.
The size of the message queue should be as big as the expected backlog.
Here an example for :c:func:`can_add_rx_filter_msgq` is shown. With this
function, it is possible to receive frames synchronously. This function can be
called from userspace context. The size of the message queue should be as big
as the expected backlog.
The filter for this example is configured to match the extended identifier
0x1234567 exactly.
@@ -262,16 +262,16 @@ The filter for this example is configured to match the extended identifier
.rtr_mask = 1,
.id_mask = CAN_EXT_ID_MASK
};
CAN_DEFINE_MSGQ(my_can_msgq, 2);
CAN_MSGQ_DEFINE(my_can_msgq, 2);
struct zcan_frame rx_frame;
int filter_id;
const struct device *can_dev;
can_dev = device_get_binding("CAN_0");
filter_id = can_attach_msgq(can_dev, &my_can_msgq, &my_filter);
filter_id = can_add_rx_filter_msgq(can_dev, &my_can_msgq, &my_filter);
if (filter_id < 0) {
LOG_ERR("Unable to attach isr [%d]", filter_id);
LOG_ERR("Unable to add rx msgq [%d]", filter_id);
return;
}
@@ -280,11 +280,11 @@ The filter for this example is configured to match the extended identifier
... do something with the frame ...
}
:c:func:`can_detach` removes the given filter.
:c:func:`can_remove_rx_filter` removes the given filter.
.. code-block:: C
can_detach(can_dev, filter_id);
can_remove_rx_filter(can_dev, filter_id);
Setting the bitrate
*******************

View File

@@ -40,12 +40,12 @@ static void can_msgq_put(struct zcan_frame *frame, void *arg)
}
}
int z_impl_can_attach_msgq(const struct device *dev, struct k_msgq *msg_q,
const struct zcan_filter *filter)
int z_impl_can_add_rx_filter_msgq(const struct device *dev, struct k_msgq *msgq,
const struct zcan_filter *filter)
{
const struct can_driver_api *api = dev->api;
return api->attach_isr(dev, can_msgq_put, msg_q, filter);
return api->add_rx_filter(dev, can_msgq_put, msgq, filter);
}
static inline void can_work_buffer_init(struct can_frame_buffer *buffer)
@@ -140,7 +140,7 @@ int can_attach_workq(const struct device *dev, struct k_work_q *work_q,
work->cb_arg = user_data;
can_work_buffer_init(&work->buf);
return api->attach_isr(dev, can_work_isr_put, work, filter);
return api->add_rx_filter(dev, can_work_isr_put, work, filter);
}

View File

@@ -55,9 +55,9 @@ static inline int z_vrfy_can_send(const struct device *dev,
}
#include <syscalls/can_send_mrsh.c>
static inline int z_vrfy_can_attach_msgq(const struct device *dev,
struct k_msgq *msgq,
const struct zcan_filter *filter)
static inline int z_vrfy_can_add_rx_filter_msgq(const struct device *dev,
struct k_msgq *msgq,
const struct zcan_filter *filter)
{
Z_OOPS(Z_SYSCALL_OBJ(dev, K_OBJ_DRIVER_CAN));
@@ -65,20 +65,20 @@ static inline int z_vrfy_can_attach_msgq(const struct device *dev,
sizeof(struct zcan_filter)));
Z_OOPS(Z_SYSCALL_OBJ(msgq, K_OBJ_MSGQ));
return z_impl_can_attach_msgq((const struct device *)dev,
(struct k_msgq *)msgq,
(const struct zcan_filter *) filter);
return z_impl_can_add_rx_filter_msgq((const struct device *)dev,
(struct k_msgq *)msgq,
(const struct zcan_filter *)filter);
}
#include <syscalls/can_attach_msgq_mrsh.c>
#include <syscalls/can_add_rx_filter_msgq_mrsh.c>
static inline void z_vrfy_can_detach(const struct device *dev, int filter_id)
static inline void z_vrfy_can_remove_rx_filter(const struct device *dev, int filter_id)
{
Z_OOPS(Z_SYSCALL_DRIVER_CAN(dev, detach));
Z_OOPS(Z_SYSCALL_DRIVER_CAN(dev, remove_rx_filter));
z_impl_can_detach((const struct device *)dev, (int)filter_id);
z_impl_can_remove_rx_filter((const struct device *)dev, (int)filter_id);
}
#include <syscalls/can_detach_mrsh.c>
#include <syscalls/can_remove_rx_filter_mrsh.c>
static inline
enum can_state z_vrfy_can_get_state(const struct device *dev,

View File

@@ -147,9 +147,8 @@ static inline int get_free_filter(struct can_loopback_filter *filters)
return -ENOSPC;
}
int can_loopback_attach_isr(const struct device *dev, can_rx_callback_t isr,
void *cb_arg,
const struct zcan_filter *filter)
int can_loopback_add_rx_filter(const struct device *dev, can_rx_callback_t cb,
void *cb_arg, const struct zcan_filter *filter)
{
struct can_loopback_data *data = dev->data;
struct can_loopback_filter *loopback_filter;
@@ -175,21 +174,21 @@ int can_loopback_attach_isr(const struct device *dev, can_rx_callback_t isr,
loopback_filter = &data->filters[filter_id];
loopback_filter->rx_cb = isr;
loopback_filter->rx_cb = cb;
loopback_filter->cb_arg = cb_arg;
loopback_filter->filter = *filter;
k_mutex_unlock(&data->mtx);
LOG_DBG("Filter attached. ID: %d", filter_id);
LOG_DBG("Filter added. ID: %d", filter_id);
return filter_id;
}
void can_loopback_detach(const struct device *dev, int filter_id)
void can_loopback_remove_rx_filter(const struct device *dev, int filter_id)
{
struct can_loopback_data *data = dev->data;
LOG_DBG("Detach filter ID: %d", filter_id);
LOG_DBG("Remove filter ID: %d", filter_id);
k_mutex_lock(&data->mtx, K_FOREVER);
data->filters[filter_id].rx_cb = NULL;
k_mutex_unlock(&data->mtx);
@@ -236,11 +235,11 @@ int can_loopback_recover(const struct device *dev, k_timeout_t timeout)
}
#endif /* CONFIG_CAN_AUTO_BUS_OFF_RECOVERY */
static void can_loopback_register_state_change_isr(const struct device *dev,
can_state_change_isr_t isr)
static void can_loopback_set_state_change_callback(const struct device *dev,
can_state_change_callback_t cb)
{
ARG_UNUSED(dev);
ARG_UNUSED(isr);
ARG_UNUSED(cb);
}
int can_loopback_get_core_clock(const struct device *dev, uint32_t *rate)
@@ -261,13 +260,13 @@ static const struct can_driver_api can_loopback_driver_api = {
.set_mode = can_loopback_set_mode,
.set_timing = can_loopback_set_timing,
.send = can_loopback_send,
.attach_isr = can_loopback_attach_isr,
.detach = can_loopback_detach,
.add_rx_filter = can_loopback_add_rx_filter,
.remove_rx_filter = can_loopback_remove_rx_filter,
.get_state = can_loopback_get_state,
#ifndef CONFIG_CAN_AUTO_BUS_OFF_RECOVERY
.recover = can_loopback_recover,
#endif
.register_state_change_isr = can_loopback_register_state_change_isr,
.set_state_change_callback = can_loopback_set_state_change_callback,
.get_core_clock = can_loopback_get_core_clock,
.get_max_filters = can_loopback_get_max_filters,
.timing_min = {

View File

@@ -440,11 +440,12 @@ static void can_mcan_state_change_handler(const struct can_mcan_config *cfg,
{
enum can_state state;
struct can_bus_err_cnt err_cnt;
const can_state_change_callback_t cb = data->state_change_cb;
state = can_mcan_get_state(cfg, &err_cnt);
if (data->state_change_isr) {
data->state_change_isr(state, err_cnt);
if (cb != NULL) {
cb(state, err_cnt);
}
}
@@ -769,55 +770,55 @@ static int can_mcan_get_free_std(volatile struct can_mcan_std_filter *filters)
* Dual mode gets tricky, because we can only activate both filters.
* If one of the IDs is not used anymore, we would need to mark it as unused.
*/
int can_mcan_attach_std(struct can_mcan_data *data,
struct can_mcan_msg_sram *msg_ram,
can_rx_callback_t isr, void *cb_arg,
const struct zcan_filter *filter)
int can_mcan_add_rx_filter_std(struct can_mcan_data *data,
struct can_mcan_msg_sram *msg_ram,
can_rx_callback_t callback, void *user_data,
const struct zcan_filter *filter)
{
struct can_mcan_std_filter filter_element = {
.id1 = filter->id,
.id2 = filter->id_mask,
.sft = CAN_MCAN_SFT_MASKED
};
int filter_nr;
int filter_id;
k_mutex_lock(&data->inst_mutex, K_FOREVER);
filter_nr = can_mcan_get_free_std(msg_ram->std_filt);
filter_id = can_mcan_get_free_std(msg_ram->std_filt);
if (filter_nr == -ENOSPC) {
if (filter_id == -ENOSPC) {
LOG_INF("No free standard id filter left");
return -ENOSPC;
}
/* TODO propper fifo balancing */
filter_element.sfce = filter_nr & 0x01 ? CAN_MCAN_FCE_FIFO1 :
filter_element.sfce = filter_id & 0x01 ? CAN_MCAN_FCE_FIFO1 :
CAN_MCAN_FCE_FIFO0;
memcpy32_volatile(&msg_ram->std_filt[filter_nr], &filter_element,
memcpy32_volatile(&msg_ram->std_filt[filter_id], &filter_element,
sizeof(struct can_mcan_std_filter));
CACHE_CLEAN(&msg_ram->std_filt[filter_nr],
sizeof(struct can_mcan_std_filter));
k_mutex_unlock(&data->inst_mutex);
LOG_DBG("Attached std filter at %d", filter_nr);
LOG_DBG("Attached std filter at %d", filter_id);
if (filter->rtr) {
data->std_filt_rtr |= (1U << filter_nr);
data->std_filt_rtr |= (1U << filter_id);
} else {
data->std_filt_rtr &= ~(1U << filter_nr);
data->std_filt_rtr &= ~(1U << filter_id);
}
if (filter->rtr_mask) {
data->std_filt_rtr_mask |= (1U << filter_nr);
data->std_filt_rtr_mask |= (1U << filter_id);
} else {
data->std_filt_rtr_mask &= ~(1U << filter_nr);
data->std_filt_rtr_mask &= ~(1U << filter_id);
}
data->rx_cb_std[filter_nr] = isr;
data->cb_arg_std[filter_nr] = cb_arg;
data->rx_cb_std[filter_id] = callback;
data->cb_arg_std[filter_id] = user_data;
return filter_nr;
return filter_id;
}
static int can_mcan_get_free_ext(volatile struct can_mcan_ext_filter *filters)
@@ -831,106 +832,104 @@ static int can_mcan_get_free_ext(volatile struct can_mcan_ext_filter *filters)
return -ENOSPC;
}
static int can_mcan_attach_ext(struct can_mcan_data *data,
struct can_mcan_msg_sram *msg_ram,
can_rx_callback_t isr, void *cb_arg,
const struct zcan_filter *filter)
static int can_mcan_add_rx_filter_ext(struct can_mcan_data *data,
struct can_mcan_msg_sram *msg_ram,
can_rx_callback_t callback, void *user_data,
const struct zcan_filter *filter)
{
struct can_mcan_ext_filter filter_element = {
.id2 = filter->id_mask,
.id1 = filter->id,
.eft = CAN_MCAN_EFT_MASKED
};
int filter_nr;
int filter_id;
k_mutex_lock(&data->inst_mutex, K_FOREVER);
filter_nr = can_mcan_get_free_ext(msg_ram->ext_filt);
filter_id = can_mcan_get_free_ext(msg_ram->ext_filt);
if (filter_nr == -ENOSPC) {
LOG_INF("No free extender id filter left");
if (filter_id == -ENOSPC) {
LOG_INF("No free extended id filter left");
return -ENOSPC;
}
/* TODO propper fifo balancing */
filter_element.efce = filter_nr & 0x01 ? CAN_MCAN_FCE_FIFO1 :
filter_element.efce = filter_id & 0x01 ? CAN_MCAN_FCE_FIFO1 :
CAN_MCAN_FCE_FIFO0;
memcpy32_volatile(&msg_ram->ext_filt[filter_nr], &filter_element,
memcpy32_volatile(&msg_ram->ext_filt[filter_id], &filter_element,
sizeof(struct can_mcan_ext_filter));
CACHE_CLEAN(&msg_ram->ext_filt[filter_nr],
sizeof(struct can_mcan_ext_filter));
k_mutex_unlock(&data->inst_mutex);
LOG_DBG("Attached ext filter at %d", filter_nr);
LOG_DBG("Attached ext filter at %d", filter_id);
if (filter->rtr) {
data->ext_filt_rtr |= (1U << filter_nr);
data->ext_filt_rtr |= (1U << filter_id);
} else {
data->ext_filt_rtr &= ~(1U << filter_nr);
data->ext_filt_rtr &= ~(1U << filter_id);
}
if (filter->rtr_mask) {
data->ext_filt_rtr_mask |= (1U << filter_nr);
data->ext_filt_rtr_mask |= (1U << filter_id);
} else {
data->ext_filt_rtr_mask &= ~(1U << filter_nr);
data->ext_filt_rtr_mask &= ~(1U << filter_id);
}
data->rx_cb_ext[filter_nr] = isr;
data->cb_arg_ext[filter_nr] = cb_arg;
data->rx_cb_ext[filter_id] = callback;
data->cb_arg_ext[filter_id] = user_data;
return filter_nr;
return filter_id;
}
int can_mcan_attach_isr(struct can_mcan_data *data,
struct can_mcan_msg_sram *msg_ram,
can_rx_callback_t isr, void *cb_arg,
const struct zcan_filter *filter)
int can_mcan_add_rx_filter(struct can_mcan_data *data,
struct can_mcan_msg_sram *msg_ram,
can_rx_callback_t callback, void *user_data,
const struct zcan_filter *filter)
{
int filter_nr;
int filter_id;
if (!isr) {
if (callback == NULL) {
return -EINVAL;
}
if (filter->id_type == CAN_STANDARD_IDENTIFIER) {
filter_nr = can_mcan_attach_std(data, msg_ram, isr, cb_arg,
filter);
filter_id = can_mcan_add_rx_filter_std(data, msg_ram, callback,
user_data, filter);
} else {
filter_nr = can_mcan_attach_ext(data, msg_ram, isr, cb_arg,
filter);
filter_nr += NUM_STD_FILTER_DATA;
filter_id = can_mcan_add_rx_filter_ext(data, msg_ram, callback,
user_data, filter);
filter_id += NUM_STD_FILTER_DATA;
}
if (filter_nr == -ENOSPC) {
if (filter_id == -ENOSPC) {
LOG_INF("No free filter left");
}
return filter_nr;
return filter_id;
}
void can_mcan_detach(struct can_mcan_data *data,
struct can_mcan_msg_sram *msg_ram, int filter_nr)
void can_mcan_remove_rx_filter(struct can_mcan_data *data,
struct can_mcan_msg_sram *msg_ram, int filter_id)
{
k_mutex_lock(&data->inst_mutex, K_FOREVER);
if (filter_nr >= NUM_STD_FILTER_DATA) {
filter_nr -= NUM_STD_FILTER_DATA;
if (filter_nr >= NUM_STD_FILTER_DATA) {
if (filter_id >= NUM_STD_FILTER_DATA) {
filter_id -= NUM_STD_FILTER_DATA;
if (filter_id >= NUM_STD_FILTER_DATA) {
LOG_ERR("Wrong filter id");
return;
}
memset32_volatile(&msg_ram->ext_filt[filter_nr], 0,
memset32_volatile(&msg_ram->ext_filt[filter_id], 0,
sizeof(struct can_mcan_ext_filter));
CACHE_CLEAN(&msg_ram->ext_filt[filter_nr],
CACHE_CLEAN(&msg_ram->ext_filt[filter_id],
sizeof(struct can_mcan_ext_filter));
data->rx_cb_ext[filter_nr] = NULL;
} else {
memset32_volatile(&msg_ram->std_filt[filter_nr], 0,
memset32_volatile(&msg_ram->std_filt[filter_id], 0,
sizeof(struct can_mcan_std_filter));
CACHE_CLEAN(&msg_ram->std_filt[filter_nr],
CACHE_CLEAN(&msg_ram->std_filt[filter_id],
sizeof(struct can_mcan_std_filter));
data->rx_cb_std[filter_nr] = NULL;
}
k_mutex_unlock(&data->inst_mutex);

View File

@@ -166,7 +166,7 @@ struct can_mcan_data {
can_rx_callback_t rx_cb_ext[NUM_EXT_FILTER_DATA];
void *cb_arg_std[NUM_STD_FILTER_DATA];
void *cb_arg_ext[NUM_EXT_FILTER_DATA];
can_state_change_isr_t state_change_isr;
can_state_change_callback_t state_change_cb;
uint32_t std_filt_rtr;
uint32_t std_filt_rtr_mask;
uint8_t ext_filt_rtr;
@@ -219,13 +219,13 @@ int can_mcan_send(const struct can_mcan_config *cfg, struct can_mcan_data *data,
k_timeout_t timeout, can_tx_callback_t callback,
void *user_data);
int can_mcan_attach_isr(struct can_mcan_data *data,
struct can_mcan_msg_sram *msg_ram,
can_rx_callback_t isr, void *cb_arg,
const struct zcan_filter *filter);
int can_mcan_add_rx_filter(struct can_mcan_data *data,
struct can_mcan_msg_sram *msg_ram,
can_rx_callback_t callback, void *user_data,
const struct zcan_filter *filter);
void can_mcan_detach(struct can_mcan_data *data,
struct can_mcan_msg_sram *msg_ram, int filter_nr);
void can_mcan_remove_rx_filter(struct can_mcan_data *data,
struct can_mcan_msg_sram *msg_ram, int filter_id);
enum can_state can_mcan_get_state(const struct can_mcan_config *cfg,
struct can_bus_err_cnt *err_cnt);

View File

@@ -518,83 +518,83 @@ static int mcp2515_send(const struct device *dev,
return 0;
}
static int mcp2515_attach_isr(const struct device *dev,
can_rx_callback_t rx_cb,
void *cb_arg,
const struct zcan_filter *filter)
static int mcp2515_add_rx_filter(const struct device *dev,
can_rx_callback_t rx_cb,
void *cb_arg,
const struct zcan_filter *filter)
{
struct mcp2515_data *dev_data = DEV_DATA(dev);
int filter_idx = 0;
int filter_id = 0;
__ASSERT(rx_cb != NULL, "response_ptr can not be null");
k_mutex_lock(&dev_data->mutex, K_FOREVER);
/* find free filter */
while ((BIT(filter_idx) & dev_data->filter_usage)
&& (filter_idx < CONFIG_CAN_MAX_FILTER)) {
filter_idx++;
while ((BIT(filter_id) & dev_data->filter_usage)
&& (filter_id < CONFIG_CAN_MAX_FILTER)) {
filter_id++;
}
/* setup filter */
if (filter_idx < CONFIG_CAN_MAX_FILTER) {
dev_data->filter_usage |= BIT(filter_idx);
if (filter_id < CONFIG_CAN_MAX_FILTER) {
dev_data->filter_usage |= BIT(filter_id);
dev_data->filter[filter_idx] = *filter;
dev_data->rx_cb[filter_idx] = rx_cb;
dev_data->cb_arg[filter_idx] = cb_arg;
dev_data->filter[filter_id] = *filter;
dev_data->rx_cb[filter_id] = rx_cb;
dev_data->cb_arg[filter_id] = cb_arg;
} else {
filter_idx = -ENOSPC;
filter_id = -ENOSPC;
}
k_mutex_unlock(&dev_data->mutex);
return filter_idx;
return filter_id;
}
static void mcp2515_detach(const struct device *dev, int filter_nr)
static void mcp2515_remove_rx_filter(const struct device *dev, int filter_id)
{
struct mcp2515_data *dev_data = DEV_DATA(dev);
k_mutex_lock(&dev_data->mutex, K_FOREVER);
dev_data->filter_usage &= ~BIT(filter_nr);
dev_data->filter_usage &= ~BIT(filter_id);
k_mutex_unlock(&dev_data->mutex);
}
static void mcp2515_register_state_change_isr(const struct device *dev,
can_state_change_isr_t isr)
static void mcp2515_set_state_change_callback(const struct device *dev,
can_state_change_callback_t cb)
{
struct mcp2515_data *dev_data = DEV_DATA(dev);
dev_data->state_change_isr = isr;
dev_data->state_change_cb = cb;
}
static void mcp2515_rx_filter(const struct device *dev,
struct zcan_frame *frame)
{
struct mcp2515_data *dev_data = DEV_DATA(dev);
uint8_t filter_idx = 0U;
uint8_t filter_id = 0U;
can_rx_callback_t callback;
struct zcan_frame tmp_frame;
k_mutex_lock(&dev_data->mutex, K_FOREVER);
for (; filter_idx < CONFIG_CAN_MAX_FILTER; filter_idx++) {
if (!(BIT(filter_idx) & dev_data->filter_usage)) {
for (; filter_id < CONFIG_CAN_MAX_FILTER; filter_id++) {
if (!(BIT(filter_id) & dev_data->filter_usage)) {
continue; /* filter slot empty */
}
if (!can_utils_filter_match(frame,
&dev_data->filter[filter_idx])) {
&dev_data->filter[filter_id])) {
continue; /* filter did not match */
}
callback = dev_data->rx_cb[filter_idx];
callback = dev_data->rx_cb[filter_id];
/*Make a temporary copy in case the user modifies the message*/
tmp_frame = *frame;
callback(&tmp_frame, dev_data->cb_arg[filter_idx]);
callback(&tmp_frame, dev_data->cb_arg[filter_id]);
}
k_mutex_unlock(&dev_data->mutex);
@@ -672,15 +672,15 @@ static enum can_state mcp2515_get_state(const struct device *dev,
static void mcp2515_handle_errors(const struct device *dev)
{
struct mcp2515_data *dev_data = DEV_DATA(dev);
can_state_change_isr_t state_change_isr = dev_data->state_change_isr;
can_state_change_callback_t state_change_cb = dev_data->state_change_cb;
enum can_state state;
struct can_bus_err_cnt err_cnt;
state = mcp2515_get_state(dev, state_change_isr ? &err_cnt : NULL);
state = mcp2515_get_state(dev, state_change_cb ? &err_cnt : NULL);
if (state_change_isr && dev_data->old_state != state) {
if (state_change_cb && dev_data->old_state != state) {
dev_data->old_state = state;
state_change_isr(state, err_cnt);
state_change_cb(state, err_cnt);
}
}
@@ -783,13 +783,13 @@ static const struct can_driver_api can_api_funcs = {
.set_timing = mcp2515_set_timing,
.set_mode = mcp2515_set_mode,
.send = mcp2515_send,
.attach_isr = mcp2515_attach_isr,
.detach = mcp2515_detach,
.add_rx_filter = mcp2515_add_rx_filter,
.remove_rx_filter = mcp2515_remove_rx_filter,
.get_state = mcp2515_get_state,
#ifndef CONFIG_CAN_AUTO_BUS_OFF_RECOVERY
.recover = mcp2515_recover,
#endif
.register_state_change_isr = mcp2515_register_state_change_isr,
.set_state_change_callback = mcp2515_set_state_change_callback,
.get_core_clock = mcp2515_get_core_clock,
.get_max_filters = mcp2515_get_max_filters,
.timing_min = {

View File

@@ -42,7 +42,7 @@ struct mcp2515_data {
can_rx_callback_t rx_cb[CONFIG_CAN_MAX_FILTER];
void *cb_arg[CONFIG_CAN_MAX_FILTER];
struct zcan_filter filter[CONFIG_CAN_MAX_FILTER];
can_state_change_isr_t state_change_isr;
can_state_change_callback_t state_change_cb;
/* general data */
struct k_mutex mutex;

View File

@@ -116,7 +116,7 @@ struct mcux_flexcan_data {
struct k_sem tx_allocs_sem;
struct mcux_flexcan_tx_callback tx_cbs[MCUX_FLEXCAN_MAX_TX];
enum can_state state;
can_state_change_isr_t state_change_isr;
can_state_change_callback_t state_change_cb;
struct can_timing timing;
};
@@ -366,10 +366,10 @@ static int mcux_flexcan_send(const struct device *dev,
return 0;
}
static int mcux_flexcan_attach_isr(const struct device *dev,
can_rx_callback_t isr,
void *user_data,
const struct zcan_filter *filter)
static int mcux_flexcan_add_rx_filter(const struct device *dev,
can_rx_callback_t callback,
void *user_data,
const struct zcan_filter *filter)
{
const struct mcux_flexcan_config *config = dev->config;
struct mcux_flexcan_data *data = dev->data;
@@ -379,7 +379,7 @@ static int mcux_flexcan_attach_isr(const struct device *dev,
int alloc = -ENOSPC;
int i;
__ASSERT_NO_MSG(isr);
__ASSERT_NO_MSG(callback);
k_mutex_lock(&data->rx_mutex, K_FOREVER);
@@ -400,7 +400,7 @@ static int mcux_flexcan_attach_isr(const struct device *dev,
&mask);
data->rx_cbs[alloc].arg = user_data;
data->rx_cbs[alloc].function = isr;
data->rx_cbs[alloc].function = callback;
FLEXCAN_SetRxIndividualMask(config->base, ALLOC_IDX_TO_RXMB_IDX(alloc),
mask);
@@ -422,12 +422,12 @@ static int mcux_flexcan_attach_isr(const struct device *dev,
return alloc;
}
static void mcux_flexcan_register_state_change_isr(const struct device *dev,
can_state_change_isr_t isr)
static void mcux_flexcan_set_state_change_callback(const struct device *dev,
can_state_change_callback_t callback)
{
struct mcux_flexcan_data *data = dev->data;
data->state_change_isr = isr;
data->state_change_cb = callback;
}
static enum can_state mcux_flexcan_get_state(const struct device *dev,
@@ -484,7 +484,7 @@ int mcux_flexcan_recover(const struct device *dev, k_timeout_t timeout)
}
#endif /* CONFIG_CAN_AUTO_BUS_OFF_RECOVERY */
static void mcux_flexcan_detach(const struct device *dev, int filter_id)
static void mcux_flexcan_remove_rx_filter(const struct device *dev, int filter_id)
{
const struct mcux_flexcan_config *config = dev->config;
struct mcux_flexcan_data *data = dev->data;
@@ -517,6 +517,8 @@ static inline void mcux_flexcan_transfer_error_status(const struct device *dev,
{
const struct mcux_flexcan_config *config = dev->config;
struct mcux_flexcan_data *data = dev->data;
const can_state_change_callback_t cb = data->state_change_cb;
can_tx_callback_t function;
int status = 0;
void *arg;
@@ -547,8 +549,9 @@ static inline void mcux_flexcan_transfer_error_status(const struct device *dev,
state = mcux_flexcan_get_state(dev, &err_cnt);
if (data->state != state) {
data->state = state;
if (data->state_change_isr) {
data->state_change_isr(state, err_cnt);
if (cb) {
cb(state, err_cnt);
}
}
@@ -746,13 +749,13 @@ static const struct can_driver_api mcux_flexcan_driver_api = {
.set_mode = mcux_flexcan_set_mode,
.set_timing = mcux_flexcan_set_timing,
.send = mcux_flexcan_send,
.attach_isr = mcux_flexcan_attach_isr,
.detach = mcux_flexcan_detach,
.add_rx_filter = mcux_flexcan_add_rx_filter,
.remove_rx_filter = mcux_flexcan_remove_rx_filter,
.get_state = mcux_flexcan_get_state,
#ifndef CONFIG_CAN_AUTO_BUS_OFF_RECOVERY
.recover = mcux_flexcan_recover,
#endif
.register_state_change_isr = mcux_flexcan_register_state_change_isr,
.set_state_change_callback = mcux_flexcan_set_state_change_callback,
.get_core_clock = mcux_flexcan_get_core_clock,
.get_max_filters = mcux_flexcan_get_max_filters,
/*

View File

@@ -140,8 +140,8 @@ drop:
}
}
static inline int attach_mcast_filter(struct net_can_context *ctx,
const struct in6_addr *addr)
static inline int add_mcast_filter(struct net_can_context *ctx,
const struct in6_addr *addr)
{
static struct zcan_filter filter = {
.id_type = CAN_EXTENDED_IDENTIFIER,
@@ -158,13 +158,13 @@ static inline int attach_mcast_filter(struct net_can_context *ctx,
((group & CAN_NET_IF_ADDR_MASK) <<
CAN_NET_IF_ADDR_DEST_POS);
filter_id = can_attach_isr(ctx->can_dev, net_can_recv,
ctx, &filter);
filter_id = can_add_rx_filter(ctx->can_dev, net_can_recv,
ctx, &filter);
if (filter_id == CAN_NET_FILTER_NOT_SET) {
return CAN_NET_FILTER_NOT_SET;
}
NET_DBG("Attached mcast filter. Group 0x%04x. Filter:%d",
NET_DBG("Added mcast filter. Group 0x%04x. Filter:%d",
group, filter_id);
return filter_id;
@@ -188,9 +188,9 @@ static void mcast_cb(struct net_if *iface, const struct net_addr *addr,
NET_ERR("Can't get a free filter_mapping");
}
filter_id = attach_mcast_filter(ctx, &addr->in6_addr);
filter_id = add_mcast_filter(ctx, &addr->in6_addr);
if (filter_id < 0) {
NET_ERR("Can't attach mcast filter");
NET_ERR("Can't add mcast filter");
return;
}
@@ -203,7 +203,7 @@ static void mcast_cb(struct net_if *iface, const struct net_addr *addr,
return;
}
can_detach(ctx->can_dev, filter_mapping->filter_id);
can_remove_rx_filter(ctx->can_dev, filter_mapping->filter_id);
filter_mapping->addr = NULL;
}
}
@@ -222,25 +222,24 @@ static void net_can_iface_init(struct net_if *iface)
net_if_mcast_mon_register(&mcast_monitor, iface, mcast_cb);
}
static int can_attach_filter(const struct device *dev, can_rx_callback_t cb,
void *cb_arg,
const struct zcan_filter *filter)
static int net_can_add_rx_filter(const struct device *dev, can_rx_callback_t cb,
void *cb_arg, const struct zcan_filter *filter)
{
struct net_can_context *ctx = dev->data;
return can_attach_isr(ctx->can_dev, cb, cb_arg, filter);
return can_add_rx_filter(ctx->can_dev, cb, cb_arg, filter);
}
static void can_detach_filter(const struct device *dev, int filter_id)
static void net_can_remove_rx_filter(const struct device *dev, int filter_id)
{
struct net_can_context *ctx = dev->data;
if (filter_id >= 0) {
can_detach(ctx->can_dev, filter_id);
can_remove_rx_filter(ctx->can_dev, filter_id);
}
}
static inline int can_attach_unicast_filter(struct net_can_context *ctx)
static inline int can_add_unicast_filter(struct net_can_context *ctx)
{
struct zcan_filter filter = {
.id_type = CAN_EXTENDED_IDENTIFIER,
@@ -254,20 +253,20 @@ static inline int can_attach_unicast_filter(struct net_can_context *ctx)
filter.id = (dest << CAN_NET_IF_ADDR_DEST_POS);
filter_id = can_attach_isr(ctx->can_dev, net_can_recv,
ctx, &filter);
filter_id = can_add_rx_filter(ctx->can_dev, net_can_recv,
ctx, &filter);
if (filter_id == CAN_NET_FILTER_NOT_SET) {
NET_ERR("Can't attach FF filter");
NET_ERR("Can't add FF filter");
return CAN_NET_FILTER_NOT_SET;
}
NET_DBG("Attached FF filter %d", filter_id);
NET_DBG("Added FF filter %d", filter_id);
return filter_id;
}
#ifdef CONFIG_NET_L2_CANBUS_ETH_TRANSLATOR
static inline int can_attach_eth_bridge_filter(struct net_can_context *ctx)
static inline int can_add_eth_bridge_filter(struct net_can_context *ctx)
{
const struct zcan_filter filter = {
.id_type = CAN_EXTENDED_IDENTIFIER,
@@ -280,19 +279,19 @@ static inline int can_attach_eth_bridge_filter(struct net_can_context *ctx)
int filter_id;
filter_id = can_attach_isr(ctx->can_dev, net_can_recv,
ctx, &filter);
filter_id = can_add_rx_filter(ctx->can_dev, net_can_recv,
ctx, &filter);
if (filter_id == CAN_NET_FILTER_NOT_SET) {
NET_ERR("Can't attach ETH bridge filter");
NET_ERR("Can't add ETH bridge filter");
return CAN_NET_FILTER_NOT_SET;
}
NET_DBG("Attached ETH bridge filter %d", filter_id);
NET_DBG("Added ETH bridge filter %d", filter_id);
return filter_id;
}
static inline int can_attach_all_mcast_filter(struct net_can_context *ctx)
static inline int can_add_all_mcast_filter(struct net_can_context *ctx)
{
const struct zcan_filter filter = {
.id_type = CAN_EXTENDED_IDENTIFIER,
@@ -304,14 +303,14 @@ static inline int can_attach_all_mcast_filter(struct net_can_context *ctx)
int filter_id;
filter_id = can_attach_isr(ctx->can_dev, net_can_recv,
ctx, &filter);
filter_id = can_add_rx_filter(ctx->can_dev, net_can_recv,
ctx, &filter);
if (filter_id == CAN_NET_FILTER_NOT_SET) {
NET_ERR("Can't attach all mcast filter");
NET_ERR("Can't add all mcast filter");
return CAN_NET_FILTER_NOT_SET;
}
NET_DBG("Attached all mcast filter %d", filter_id);
NET_DBG("Added all mcast filter %d", filter_id);
return filter_id;
}
@@ -323,7 +322,7 @@ static int can_enable(const struct device *dev, bool enable)
if (enable) {
if (ctx->recv_filter_id == CAN_NET_FILTER_NOT_SET) {
ctx->recv_filter_id = can_attach_unicast_filter(ctx);
ctx->recv_filter_id = can_add_unicast_filter(ctx);
if (ctx->recv_filter_id < 0) {
return -EIO;
}
@@ -331,34 +330,34 @@ static int can_enable(const struct device *dev, bool enable)
#ifdef CONFIG_NET_L2_CANBUS_ETH_TRANSLATOR
if (ctx->eth_bridge_filter_id == CAN_NET_FILTER_NOT_SET) {
ctx->eth_bridge_filter_id = can_attach_eth_bridge_filter(ctx);
ctx->eth_bridge_filter_id = can_add_eth_bridge_filter(ctx);
if (ctx->eth_bridge_filter_id < 0) {
can_detach(ctx->can_dev, ctx->recv_filter_id);
can_remove_rx_filter(ctx->can_dev, ctx->recv_filter_id);
return -EIO;
}
}
if (ctx->all_mcast_filter_id == CAN_NET_FILTER_NOT_SET) {
ctx->all_mcast_filter_id = can_attach_all_mcast_filter(ctx);
ctx->all_mcast_filter_id = can_add_all_mcast_filter(ctx);
if (ctx->all_mcast_filter_id < 0) {
can_detach(ctx->can_dev, ctx->recv_filter_id);
can_detach(ctx->can_dev, ctx->eth_bridge_filter_id);
can_remove_rx_filter(ctx->can_dev, ctx->recv_filter_id);
can_remove_rx_filter(ctx->can_dev, ctx->eth_bridge_filter_id);
return -EIO;
}
}
#endif /*CONFIG_NET_L2_CANBUS_ETH_TRANSLATOR*/
} else {
if (ctx->recv_filter_id != CAN_NET_FILTER_NOT_SET) {
can_detach(ctx->can_dev, ctx->recv_filter_id);
can_remove_rx_filter(ctx->can_dev, ctx->recv_filter_id);
}
#ifdef CONFIG_NET_L2_CANBUS_ETH_TRANSLATOR
if (ctx->eth_bridge_filter_id != CAN_NET_FILTER_NOT_SET) {
can_detach(ctx->can_dev, ctx->eth_bridge_filter_id);
can_remove_rx_filter(ctx->can_dev, ctx->eth_bridge_filter_id);
}
if (ctx->all_mcast_filter_id != CAN_NET_FILTER_NOT_SET) {
can_detach(ctx->can_dev, ctx->all_mcast_filter_id);
can_remove_rx_filter(ctx->can_dev, ctx->all_mcast_filter_id);
}
#endif /*CONFIG_NET_L2_CANBUS_ETH_TRANSLATOR*/
}
@@ -370,8 +369,8 @@ static struct net_can_api net_can_api_inst = {
.iface_api.init = net_can_iface_init,
.send = net_can_send,
.attach_filter = can_attach_filter,
.detach_filter = can_detach_filter,
.add_rx_filter = net_can_add_rx_filter,
.remove_rx_filter = net_can_remove_rx_filter,
.enable = can_enable,
};

View File

@@ -198,7 +198,7 @@ struct can_rcar_data {
can_rx_callback_t rx_callback[CONFIG_CAN_RCAR_MAX_FILTER];
void *rx_callback_arg[CONFIG_CAN_RCAR_MAX_FILTER];
struct zcan_filter filter[CONFIG_CAN_RCAR_MAX_FILTER];
can_state_change_isr_t state_change_isr;
can_state_change_callback_t state_change_cb;
enum can_state state;
};
@@ -250,6 +250,7 @@ static void can_rcar_state_change(const struct device *dev, uint32_t newstate)
{
const struct can_rcar_cfg *config = DEV_CAN_CFG(dev);
struct can_rcar_data *data = DEV_CAN_DATA(dev);
const can_state_change_callback_t cb = data->state_change_cb;
struct can_bus_err_cnt err_cnt;
if (data->state == newstate) {
@@ -260,11 +261,11 @@ static void can_rcar_state_change(const struct device *dev, uint32_t newstate)
data->state = newstate;
if (data->state_change_isr == NULL) {
if (cb == NULL) {
return;
}
can_rcar_get_error_count(config, &err_cnt);
data->state_change_isr(newstate, err_cnt);
cb(newstate, err_cnt);
}
static void can_rcar_error(const struct device *dev)
@@ -655,12 +656,12 @@ unlock:
return ret;
}
static void can_rcar_register_state_change_isr(const struct device *dev,
can_state_change_isr_t isr)
static void can_rcar_set_state_change_callback(const struct device *dev,
can_state_change_callback_t cb)
{
struct can_rcar_data *data = DEV_CAN_DATA(dev);
data->state_change_isr = isr;
data->state_change_cb = cb;
}
static enum can_state can_rcar_get_state(const struct device *dev,
@@ -791,10 +792,10 @@ int can_rcar_send(const struct device *dev, const struct zcan_frame *frame,
return 0;
}
static inline int can_rcar_attach(const struct device *dev,
can_rx_callback_t cb,
void *cb_arg,
const struct zcan_filter *filter)
static inline int can_rcar_add_rx_filter_unlocked(const struct device *dev,
can_rx_callback_t cb,
void *cb_arg,
const struct zcan_filter *filter)
{
struct can_rcar_data *data = DEV_CAN_DATA(dev);
int i;
@@ -812,30 +813,29 @@ static inline int can_rcar_attach(const struct device *dev,
return -ENOSPC;
}
int can_rcar_attach_isr(const struct device *dev, can_rx_callback_t isr,
void *cb_arg,
const struct zcan_filter *filter)
int can_rcar_add_rx_filter(const struct device *dev, can_rx_callback_t cb,
void *cb_arg, const struct zcan_filter *filter)
{
struct can_rcar_data *data = DEV_CAN_DATA(dev);
int filter_nr;
int filter_id;
k_mutex_lock(&data->rx_mutex, K_FOREVER);
filter_nr = can_rcar_attach(dev, isr, cb_arg, filter);
filter_id = can_rcar_add_rx_filter_unlocked(dev, cb, cb_arg, filter);
k_mutex_unlock(&data->rx_mutex);
return filter_nr;
return filter_id;
}
void can_rcar_detach(const struct device *dev, int filter_nr)
void can_rcar_remove_rx_filter(const struct device *dev, int filter_id)
{
struct can_rcar_data *data = DEV_CAN_DATA(dev);
if (filter_nr >= CONFIG_CAN_RCAR_MAX_FILTER) {
if (filter_id >= CONFIG_CAN_RCAR_MAX_FILTER) {
return;
}
k_mutex_lock(&data->rx_mutex, K_FOREVER);
compiler_barrier();
data->rx_callback[filter_nr] = NULL;
data->rx_callback[filter_id] = NULL;
k_mutex_unlock(&data->rx_mutex);
}
@@ -861,7 +861,7 @@ static int can_rcar_init(const struct device *dev)
memset(data->rx_callback, 0, sizeof(data->rx_callback));
data->state = CAN_ERROR_ACTIVE;
data->state_change_isr = NULL;
data->state_change_cb = NULL;
/* reset the registers */
ret = clock_control_off(config->clock_dev,
@@ -992,13 +992,13 @@ static const struct can_driver_api can_rcar_driver_api = {
.set_mode = can_rcar_set_mode,
.set_timing = can_rcar_set_timing,
.send = can_rcar_send,
.attach_isr = can_rcar_attach_isr,
.detach = can_rcar_detach,
.add_rx_filter = can_rcar_add_rx_filter,
.remove_rx_filter = can_rcar_remove_rx_filter,
.get_state = can_rcar_get_state,
#ifndef CONFIG_CAN_AUTO_BUS_OFF_RECOVERY
.recover = can_rcar_recover,
#endif
.register_state_change_isr = can_rcar_register_state_change_isr,
.set_state_change_callback = can_rcar_set_state_change_callback,
.get_core_clock = can_rcar_get_core_clock,
.get_max_filters = can_rcar_get_max_filters,
.timing_min = {

View File

@@ -10,7 +10,7 @@
#include <zephyr/types.h>
#include <stdlib.h>
CAN_DEFINE_MSGQ(msgq, 4);
CAN_MSGQ_DEFINE(msgq, 4);
const struct shell *msgq_shell;
static struct k_work_poll msgq_work;
static struct k_poll_event msgq_events[1] = {
@@ -389,7 +389,7 @@ static int cmd_attach(const struct shell *shell, size_t argc, char **argv)
filter.id, ext ? "extended" : "standard", filter.id_mask,
filter.rtr_mask);
ret = can_attach_msgq(can_dev, &msgq, &filter);
ret = can_add_rx_filter_msgq(can_dev, &msgq, &filter);
if (ret < 0) {
if (ret == -ENOSPC) {
shell_error(shell, "Can't attach, no free filter left");
@@ -439,7 +439,7 @@ static int cmd_detach(const struct shell *shell, size_t argc, char **argv)
shell_error(shell, "filter_id must not be negative");
}
can_detach(can_dev, (int)id);
can_remove_rx_filter(can_dev, (int)id);
return 0;
}

View File

@@ -122,6 +122,7 @@ static inline void can_stm32_bus_state_change_isr(CAN_TypeDef *can,
{
struct can_bus_err_cnt err_cnt;
enum can_state state;
const can_state_change_callback_t cb = data->state_change_cb;
if (!(can->ESR & CAN_ESR_EPVF) && !(can->ESR & CAN_ESR_BOFF)) {
return;
@@ -138,8 +139,8 @@ static inline void can_stm32_bus_state_change_isr(CAN_TypeDef *can,
state = CAN_ERROR_ACTIVE;
}
if (data->state_change_isr) {
data->state_change_isr(state, err_cnt);
if (cb != NULL) {
cb(state, err_cnt);
}
}
@@ -439,7 +440,7 @@ static int can_stm32_init(const struct device *dev)
data->mb0.tx_callback = NULL;
data->mb1.tx_callback = NULL;
data->mb2.tx_callback = NULL;
data->state_change_isr = NULL;
data->state_change_cb = NULL;
data->filter_usage = (1ULL << CAN_MAX_NUMBER_OF_FILTERS) - 1ULL;
(void)memset(data->rx_cb, 0, sizeof(data->rx_cb));
@@ -523,16 +524,16 @@ static int can_stm32_init(const struct device *dev)
return 0;
}
static void can_stm32_register_state_change_isr(const struct device *dev,
can_state_change_isr_t isr)
static void can_stm32_set_state_change_callback(const struct device *dev,
can_state_change_callback_t cb)
{
struct can_stm32_data *data = DEV_DATA(dev);
const struct can_stm32_config *cfg = DEV_CFG(dev);
CAN_TypeDef *can = cfg->can;
data->state_change_isr = isr;
data->state_change_cb = cb;
if (isr == NULL) {
if (cb == NULL) {
can->IER &= ~CAN_IER_EPVIE;
} else {
can->IER |= CAN_IER_EPVIE;
@@ -764,9 +765,9 @@ enum can_filter_type can_stm32_get_filter_type(int bank_nr, uint32_t mode_reg,
return type;
}
static int can_calc_filter_index(int filter_nr, uint32_t mode_reg, uint32_t scale_reg)
static int can_calc_filter_index(int filter_id, uint32_t mode_reg, uint32_t scale_reg)
{
int filter_bank = filter_nr / 4;
int filter_bank = filter_id / 4;
int cnt = 0;
uint32_t mode_masked, scale_masked;
enum can_filter_type filter_type;
@@ -779,19 +780,19 @@ static int can_calc_filter_index(int filter_nr, uint32_t mode_reg, uint32_t scal
/* plus the filters in the same bank */
mode_masked = mode_reg & (1U << filter_bank);
scale_masked = scale_reg & (1U << filter_bank);
cnt += (!scale_masked && mode_masked) ? filter_nr & 0x03 :
(filter_nr & 0x03) >> 1;
cnt += (!scale_masked && mode_masked) ? filter_id & 0x03 :
(filter_id & 0x03) >> 1;
return cnt;
}
static void can_stm32_set_filter_bank(int filter_nr,
static void can_stm32_set_filter_bank(int filter_id,
CAN_FilterRegister_TypeDef *filter_reg,
enum can_filter_type filter_type,
uint32_t id, uint32_t mask)
{
switch (filter_type) {
case CAN_FILTER_STANDARD:
switch (filter_nr & 0x03) {
switch (filter_id & 0x03) {
case 0:
filter_reg->FR1 = (filter_reg->FR1 & 0xFFFF0000) | id;
break;
@@ -810,7 +811,7 @@ static void can_stm32_set_filter_bank(int filter_nr,
break;
case CAN_FILTER_STANDARD_MASKED:
switch (filter_nr & 0x02) {
switch (filter_id & 0x02) {
case 0:
filter_reg->FR1 = id | (mask << 16);
break;
@@ -821,7 +822,7 @@ static void can_stm32_set_filter_bank(int filter_nr,
break;
case CAN_FILTER_EXTENDED:
switch (filter_nr & 0x02) {
switch (filter_id & 0x02) {
case 0:
filter_reg->FR1 = id;
break;
@@ -888,7 +889,7 @@ static inline int can_stm32_set_filter(const struct zcan_filter *filter,
{
uint32_t mask = 0U;
uint32_t id = 0U;
int filter_nr = 0;
int filter_id = 0;
int filter_index_new = -ENOSPC;
int bank_nr;
uint32_t bank_bit;
@@ -928,11 +929,11 @@ static inline int can_stm32_set_filter(const struct zcan_filter *filter,
filter_type);
do {
uint64_t usage_shifted = (device_data->filter_usage >> filter_nr);
uint64_t usage_shifted = (device_data->filter_usage >> filter_id);
uint64_t usage_demand_mask = (1ULL << register_demand) - 1;
bool bank_is_empty;
bank_nr = filter_nr / 4;
bank_nr = filter_id / 4;
bank_bit = (1U << bank_nr);
bank_mode = can_stm32_get_filter_type(bank_nr, can->FM1R,
can->FS1R);
@@ -941,20 +942,20 @@ static inline int can_stm32_set_filter(const struct zcan_filter *filter,
bank_nr);
if (!bank_is_empty && bank_mode != filter_type) {
filter_nr = (bank_nr + 1) * 4;
filter_id = (bank_nr + 1) * 4;
} else if (usage_shifted & usage_demand_mask) {
device_data->filter_usage &=
~(usage_demand_mask << filter_nr);
~(usage_demand_mask << filter_id);
break;
} else {
filter_nr += register_demand;
filter_id += register_demand;
}
if (!usage_shifted) {
LOG_INF("No free filter bank found");
return -ENOSPC;
}
} while (filter_nr < CAN_MAX_NUMBER_OF_FILTERS);
} while (filter_id < CAN_MAX_NUMBER_OF_FILTERS);
/* set the filter init mode */
can->FMR |= CAN_FMR_FINIT;
@@ -972,7 +973,7 @@ static inline int can_stm32_set_filter(const struct zcan_filter *filter,
shift_width = filter_in_bank[filter_type] - filter_in_bank[bank_mode];
filter_index_new = can_calc_filter_index(filter_nr, mode_reg,
filter_index_new = can_calc_filter_index(filter_id, mode_reg,
scale_reg);
start_index = filter_index_new + filter_in_bank[bank_mode];
@@ -988,7 +989,7 @@ static inline int can_stm32_set_filter(const struct zcan_filter *filter,
if (filter_index_new >= CONFIG_CAN_MAX_FILTER || res) {
LOG_INF("No space for a new filter!");
filter_nr = -ENOSPC;
filter_id = -ENOSPC;
goto done;
}
}
@@ -996,59 +997,60 @@ static inline int can_stm32_set_filter(const struct zcan_filter *filter,
can->FM1R = mode_reg;
can->FS1R = scale_reg;
} else {
filter_index_new = can_calc_filter_index(filter_nr, can->FM1R,
filter_index_new = can_calc_filter_index(filter_id, can->FM1R,
can->FS1R);
if (filter_index_new >= CAN_MAX_NUMBER_OF_FILTERS) {
filter_nr = -ENOSPC;
filter_id = -ENOSPC;
goto done;
}
}
can_stm32_set_filter_bank(filter_nr, &can->sFilterRegister[bank_nr],
can_stm32_set_filter_bank(filter_id, &can->sFilterRegister[bank_nr],
filter_type, id, mask);
done:
can->FA1R |= bank_bit;
can->FMR &= ~(CAN_FMR_FINIT);
LOG_DBG("Filter set! Filter number: %d (index %d)",
filter_nr, filter_index_new);
filter_id, filter_index_new);
*filter_index = filter_index_new;
return filter_nr;
return filter_id;
}
static inline int can_stm32_attach(const struct device *dev,
can_rx_callback_t cb,
void *cb_arg,
const struct zcan_filter *filter)
static inline int can_stm32_add_rx_filter_unlocked(const struct device *dev,
can_rx_callback_t cb,
void *cb_arg,
const struct zcan_filter *filter)
{
const struct can_stm32_config *cfg = DEV_CFG(dev);
struct can_stm32_data *data = DEV_DATA(dev);
CAN_TypeDef *can = cfg->master_can;
int filter_index = 0;
int filter_nr;
int filter_id;
filter_nr = can_stm32_set_filter(filter, data, can, &filter_index);
if (filter_nr != -ENOSPC) {
filter_id = can_stm32_set_filter(filter, data, can, &filter_index);
if (filter_id != -ENOSPC) {
data->rx_cb[filter_index] = cb;
data->cb_arg[filter_index] = cb_arg;
}
return filter_nr;
return filter_id;
}
int can_stm32_attach_isr(const struct device *dev, can_rx_callback_t isr,
void *cb_arg,
const struct zcan_filter *filter)
int can_stm32_add_rx_filter(const struct device *dev, can_rx_callback_t cb,
void *cb_arg,
const struct zcan_filter *filter)
{
struct can_stm32_data *data = DEV_DATA(dev);
int filter_nr;
int filter_id;
k_mutex_lock(&data->inst_mutex, K_FOREVER);
filter_nr = can_stm32_attach(dev, isr, cb_arg, filter);
filter_id = can_stm32_add_rx_filter_unlocked(dev, cb, cb_arg, filter);
k_mutex_unlock(&data->inst_mutex);
return filter_nr;
return filter_id;
}
void can_stm32_detach(const struct device *dev, int filter_nr)
void can_stm32_remove_rx_filter(const struct device *dev, int filter_id)
{
const struct can_stm32_config *cfg = DEV_CFG(dev);
struct can_stm32_data *data = DEV_DATA(dev);
@@ -1061,28 +1063,28 @@ void can_stm32_detach(const struct device *dev, int filter_nr)
enum can_filter_type type;
uint32_t reset_mask;
__ASSERT_NO_MSG(filter_nr >= 0 && filter_nr < CAN_MAX_NUMBER_OF_FILTERS);
__ASSERT_NO_MSG(filter_id >= 0 && filter_id < CAN_MAX_NUMBER_OF_FILTERS);
k_mutex_lock(&data->inst_mutex, K_FOREVER);
bank_nr = filter_nr / 4;
bank_nr = filter_id / 4;
bank_bit = (1U << bank_nr);
mode_reg = can->FM1R;
scale_reg = can->FS1R;
filter_index = can_calc_filter_index(filter_nr, mode_reg, scale_reg);
filter_index = can_calc_filter_index(filter_id, mode_reg, scale_reg);
type = can_stm32_get_filter_type(bank_nr, mode_reg, scale_reg);
LOG_DBG("Detatch filter number %d (index %d), type %d", filter_nr,
LOG_DBG("Detatch filter number %d (index %d), type %d", filter_id,
filter_index,
type);
reset_mask = ((1 << (reg_demand[type])) - 1) << filter_nr;
reset_mask = ((1 << (reg_demand[type])) - 1) << filter_id;
data->filter_usage |= reset_mask;
can->FMR |= CAN_FMR_FINIT;
can->FA1R &= ~bank_bit;
can_stm32_set_filter_bank(filter_nr, &can->sFilterRegister[bank_nr],
can_stm32_set_filter_bank(filter_id, &can->sFilterRegister[bank_nr],
type, 0, 0xFFFFFFFF);
if (!CAN_BANK_IS_EMPTY(data->filter_usage, bank_nr)) {
@@ -1102,13 +1104,13 @@ static const struct can_driver_api can_api_funcs = {
.set_mode = can_stm32_set_mode,
.set_timing = can_stm32_set_timing,
.send = can_stm32_send,
.attach_isr = can_stm32_attach_isr,
.detach = can_stm32_detach,
.add_rx_filter = can_stm32_add_rx_filter,
.remove_rx_filter = can_stm32_remove_rx_filter,
.get_state = can_stm32_get_state,
#ifndef CONFIG_CAN_AUTO_BUS_OFF_RECOVERY
.recover = can_stm32_recover,
#endif
.register_state_change_isr = can_stm32_register_state_change_isr,
.set_state_change_callback = can_stm32_set_state_change_callback,
.get_core_clock = can_stm32_get_core_clock,
.get_max_filters = can_stm32_get_max_filters,
.timing_min = {

View File

@@ -64,7 +64,7 @@ struct can_stm32_data {
uint64_t filter_usage;
can_rx_callback_t rx_cb[CONFIG_CAN_MAX_FILTER];
void *cb_arg[CONFIG_CAN_MAX_FILTER];
can_state_change_isr_t state_change_isr;
can_state_change_callback_t state_change_cb;
};
struct can_stm32_config {

View File

@@ -55,12 +55,12 @@ void can_stm32fd_clock_enable(void)
FDCAN_CONFIG->CKDIV = CONFIG_CAN_STM32_CLOCK_DIVISOR >> 1;
}
void can_stm32fd_register_state_change_isr(const struct device *dev,
can_state_change_isr_t isr)
void can_stm32fd_set_state_change_callback(const struct device *dev,
can_state_change_callback_t cb)
{
struct can_stm32fd_data *data = DEV_DATA(dev);
data->mcan_data.state_change_isr = isr;
data->mcan_data.state_change_cb = cb;
}
static int can_stm32fd_init(const struct device *dev)
@@ -111,23 +111,23 @@ int can_stm32fd_send(const struct device *dev, const struct zcan_frame *frame,
callback, user_data);
}
int can_stm32fd_attach_isr(const struct device *dev, can_rx_callback_t isr,
void *cb_arg, const struct zcan_filter *filter)
int can_stm32fd_add_rx_filter(const struct device *dev, can_rx_callback_t callback,
void *user_data, const struct zcan_filter *filter)
{
const struct can_stm32fd_config *cfg = DEV_CFG(dev);
struct can_mcan_data *mcan_data = &DEV_DATA(dev)->mcan_data;
struct can_mcan_msg_sram *msg_ram = cfg->msg_sram;
return can_mcan_attach_isr(mcan_data, msg_ram, isr, cb_arg, filter);
return can_mcan_add_rx_filter(mcan_data, msg_ram, callback, user_data, filter);
}
void can_stm32fd_detach(const struct device *dev, int filter_nr)
void can_stm32fd_remove_rx_filter(const struct device *dev, int filter_id)
{
const struct can_stm32fd_config *cfg = DEV_CFG(dev);
struct can_mcan_data *mcan_data = &DEV_DATA(dev)->mcan_data;
struct can_mcan_msg_sram *msg_ram = cfg->msg_sram;
can_mcan_detach(mcan_data, msg_ram, filter_nr);
can_mcan_remove_rx_filter(mcan_data, msg_ram, filter_id);
}
int can_stm32fd_set_mode(const struct device *dev, enum can_mode mode)
@@ -175,15 +175,15 @@ static const struct can_driver_api can_api_funcs = {
.set_mode = can_stm32fd_set_mode,
.set_timing = can_stm32fd_set_timing,
.send = can_stm32fd_send,
.attach_isr = can_stm32fd_attach_isr,
.detach = can_stm32fd_detach,
.add_rx_filter = can_stm32fd_add_rx_filter,
.remove_rx_filter = can_stm32fd_remove_rx_filter,
.get_state = can_stm32fd_get_state,
#ifndef CONFIG_CAN_AUTO_BUS_OFF_RECOVERY
.recover = can_mcan_recover,
#endif
.get_core_clock = can_stm32fd_get_core_clock,
.get_max_filters = can_stm32fd_get_max_filters,
.register_state_change_isr = can_stm32fd_register_state_change_isr,
.set_state_change_callback = can_stm32fd_set_state_change_callback,
.timing_min = {
.sjw = 0x7f,
.prop_seg = 0x00,

View File

@@ -22,7 +22,7 @@
#define BUF_ALLOC_TIMEOUT K_MSEC(50)
/* TODO: make msgq size configurable */
CAN_DEFINE_MSGQ(socket_can_msgq, 5);
CAN_MSGQ_DEFINE(socket_can_msgq, 5);
K_KERNEL_STACK_DEFINE(rx_thread_stack, RX_THREAD_STACK_SIZE);
struct socket_can_context {
@@ -93,8 +93,8 @@ static inline int socket_can_setsockopt(const struct device *dev, void *obj,
__ASSERT_NO_MSG(optlen == sizeof(struct zcan_filter));
ret = can_attach_msgq(socket_context->can_dev, socket_context->msgq,
optval);
ret = can_add_rx_filter_msgq(socket_context->can_dev, socket_context->msgq,
optval);
if (ret == -ENOSPC) {
errno = ENOSPC;
return -1;
@@ -109,7 +109,7 @@ static inline void socket_can_close(const struct device *dev, int filter_id)
{
struct socket_can_context *socket_context = dev->data;
can_detach(socket_context->can_dev, filter_id);
can_remove_rx_filter(socket_context->can_dev, filter_id);
}
static struct canbus_api socket_can_api = {

View File

@@ -273,13 +273,13 @@ typedef void (*can_tx_callback_t)(int error, void *user_data);
typedef void (*can_rx_callback_t)(struct zcan_frame *frame, void *user_data);
/**
* @typedef can_state_change_isr_t
* @brief Defines the state change Interrupt Service Routine (ISR) handler function signature
* @typedef can_state_change_callback_t
* @brief Defines the state change callback handler function signature
*
* @param state State of the CAN controller.
* @param err_cnt CAN controller error counter values.
*/
typedef void (*can_state_change_isr_t)(enum can_state state, struct can_bus_err_cnt err_cnt);
typedef void (*can_state_change_callback_t)(enum can_state state, struct can_bus_err_cnt err_cnt);
/**
* @cond INTERNAL_HIDDEN
@@ -310,25 +310,25 @@ typedef int (*can_set_mode_t)(const struct device *dev, enum can_mode mode);
*/
typedef int (*can_send_t)(const struct device *dev,
const struct zcan_frame *frame,
k_timeout_t timeout, can_tx_callback_t callback_isr,
k_timeout_t timeout, can_tx_callback_t callback,
void *user_data);
/**
* @typedef can_attach_isr_t
* @brief Callback API upon attaching an RX filter ISR
* See @a can_attach_isr() for argument description
* @typedef can_add_rx_filter_t
* @brief Callback API upon adding an RX filter
* See @a can_add_rx_callback() for argument description
*/
typedef int (*can_attach_isr_t)(const struct device *dev,
can_rx_callback_t isr,
void *user_data,
const struct zcan_filter *filter);
typedef int (*can_add_rx_filter_t)(const struct device *dev,
can_rx_callback_t callback,
void *user_data,
const struct zcan_filter *filter);
/**
* @typedef can_detach_t
* @brief Callback API upon detaching an RX filter
* See @a can_detach() for argument description
* @typedef can_remove_rx_filter_t
* @brief Callback API upon removing an RX filter
* See @a can_remove_rx_filter() for argument description
*/
typedef void (*can_detach_t)(const struct device *dev, int filter_id);
typedef void (*can_remove_rx_filter_t)(const struct device *dev, int filter_id);
/**
* @typedef can_recover_t
@@ -339,19 +339,19 @@ typedef int (*can_recover_t)(const struct device *dev, k_timeout_t timeout);
/**
* @typedef can_get_state_t
* @brief Callback API upon get the CAN controller state
* @brief Callback API upon getting the CAN controller state
* See @a can_get_state() for argument description
*/
typedef enum can_state (*can_get_state_t)(const struct device *dev,
struct can_bus_err_cnt *err_cnt);
/**
* @typedef can_register_state_change_isr_t
* @brief Callback API upon registering a state change ISR
* See @a can_register_state_change_isr() for argument description
* @typedef can_set_state_change_callback_t
* @brief Callback API upon setting a state change callback
* See @a can_set_state_change_callback() for argument description
*/
typedef void(*can_register_state_change_isr_t)(const struct device *dev,
can_state_change_isr_t isr);
typedef void(*can_set_state_change_callback_t)(const struct device *dev,
can_state_change_callback_t callback);
/**
* @typedef can_get_core_clock_t
@@ -371,13 +371,13 @@ __subsystem struct can_driver_api {
can_set_mode_t set_mode;
can_set_timing_t set_timing;
can_send_t send;
can_attach_isr_t attach_isr;
can_detach_t detach;
can_add_rx_filter_t add_rx_filter;
can_remove_rx_filter_t remove_rx_filter;
#if !defined(CONFIG_CAN_AUTO_BUS_OFF_RECOVERY) || defined(__DOXYGEN__)
can_recover_t recover;
#endif /* CONFIG_CAN_AUTO_BUS_OFF_RECOVERY */
can_get_state_t get_state;
can_register_state_change_isr_t register_state_change_isr;
can_set_state_change_callback_t set_state_change_callback;
can_get_core_clock_t get_core_clock;
can_get_max_filters_t get_max_filters;
/* Min values for the timing registers */
@@ -683,16 +683,16 @@ static inline int can_write(const struct device *dev, const uint8_t *data, uint8
*/
/**
* @brief Attach a callback function with a given CAN filter
* @brief Add a callback function for a given CAN filter
*
* Attach a callback to CAN identifiers specified by a filter. Whenever a frame
* matching the filter is received by the CAN controller, the callback function
* is called in interrupt context.
* Add a callback to CAN identifiers specified by a filter. When a recevied CAN
* frame matching the filter is received by the CAN controller, the callback
* function is called in interrupt context.
*
* If a frame matches more than one attached filter, the priority of the match
* is hardware dependent.
*
* The same callback function can be attached to more than one filter.
* The same callback function can be used for multiple filters.
*
* @param dev Pointer to the device structure for the driver instance.
* @param callback This function is called by the CAN controller driver whenever
@@ -703,66 +703,67 @@ static inline int can_write(const struct device *dev, const uint8_t *data, uint8
* @retval filter_id on success.
* @retval -ENOSPC if there are no free filters.
*/
static inline int can_attach_isr(const struct device *dev, can_rx_callback_t callback,
void *user_data, const struct zcan_filter *filter)
static inline int can_add_rx_filter(const struct device *dev, can_rx_callback_t callback,
void *user_data, const struct zcan_filter *filter)
{
const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
return api->attach_isr(dev, callback, user_data, filter);
return api->add_rx_filter(dev, callback, user_data, filter);
}
/**
* @brief Statically define and initialize a CAN RX message queue.
*
* The message queue's ring buffer contains space for @a size CAN frames.
* The message queue's ring buffer contains space for @a max_frames CAN frames.
*
* @param name Name of the message queue.
* @param size Number of CAN frames.
* @see can_add_rx_filter_msgq()
*
* @param name Name of the message queue.
* @param max_frames Maximum number of CAN frames that can be queued.
*/
#define CAN_DEFINE_MSGQ(name, size) \
K_MSGQ_DEFINE(name, sizeof(struct zcan_frame), size, 4)
#define CAN_MSGQ_DEFINE(name, max_frames) \
K_MSGQ_DEFINE(name, sizeof(struct zcan_frame), max_frames, 4)
/**
* @brief Attach a message queue with a given filter
* @brief Wrapper function for adding a message queue for a given filter
*
* Attach a message queue to CAN identifiers specified by a filter. Whenever a
* frame matching the filter is received by the CAN controller, the frame is
* pushed to the message queue.
* Wrapper function for @a can_add_rx_filter() which puts received CAN frames
* matching the filter in a message queue instead of calling a callback.
*
* If a frame matches more than one attached filter, the priority of the match
* is hardware dependent.
*
* A message queue can be attached to more than one filter.
* The same message queue can be used for multiple filters.
*
* @note The message queue must me initialized before, and the caller must have
* appropriate permissions on it.
* @note The message queue must be initialized before calling this function and
* the caller must have appropriate permissions on it.
*
* @param dev Pointer to the device structure for the driver instance.
* @param msg_q Pointer to the already initialized @a k_msgq struct.
* @param msgq Pointer to the already initialized @a k_msgq struct.
* @param filter Pointer to a @a zcan_filter structure defining the filter.
*
* @retval filter_id on success.
* @retval -ENOSPC if there are no free filters.
*/
__syscall int can_attach_msgq(const struct device *dev, struct k_msgq *msg_q,
const struct zcan_filter *filter);
__syscall int can_add_rx_filter_msgq(const struct device *dev, struct k_msgq *msgq,
const struct zcan_filter *filter);
/**
* @brief Detach an ISR or CAN message queue RX filter
* @brief Remove a CAN RX filter
*
* This routine detaches an CAN RX filter based on the filter ID returned by @a
* can_attach_isr() or @a can_attach_msgq().
* This routine removes a CAN RX filter based on the filter ID returned by @a
* can_add_rx_filter() or @a can_add_rx_filter_msgq().
*
* @param dev Pointer to the device structure for the driver instance.
* @param filter_id Filter ID
*/
__syscall void can_detach(const struct device *dev, int filter_id);
__syscall void can_remove_rx_filter(const struct device *dev, int filter_id);
static inline void z_impl_can_detach(const struct device *dev, int filter_id)
static inline void z_impl_can_remove_rx_filter(const struct device *dev, int filter_id)
{
const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
return api->detach(dev, filter_id);
return api->remove_rx_filter(dev, filter_id);
}
/**
@@ -852,20 +853,23 @@ static inline int z_impl_can_recover(const struct device *dev, k_timeout_t timeo
#endif /* !CONFIG_CAN_AUTO_BUS_OFF_RECOVERY */
/**
* @brief Register an ISR callback for the CAN controller state change interrupt
* @brief Set a callback for CAN controller state change events
*
* Set the callback for CAN controller state change events. The callback
* function will be called in interrupt context.
*
* Only one callback can be registered per controller. Calling this function
* again overrides any previously registered callback.
*
* @param dev Pointer to the device structure for the driver instance.
* @param isr ISR callback function.
* @param dev Pointer to the device structure for the driver instance.
* @param callback Callback function.
*/
static inline void can_register_state_change_isr(const struct device *dev,
can_state_change_isr_t isr)
static inline void can_set_state_change_callback(const struct device *dev,
can_state_change_callback_t callback)
{
const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
return api->register_state_change_isr(dev, isr);
return api->set_state_change_callback(dev, callback);
}
/** @} */
@@ -1050,7 +1054,7 @@ static inline void can_copy_zfilter_to_filter(const struct zcan_filter *zfilter,
* The `CAN_TX_*` error codes are used for CAN specific error return codes from
* @a can_send() and for `error_flags` values in @a can_tx_callback_t().
*
* `CAN_NO_FREE_FILTER` is returned by `can_attach_*()` if no free filters are
* `CAN_NO_FREE_FILTER` is returned by `can_add_rx_*()` if no free filters are
* available. `CAN_TIMEOUT` indicates that @a can_recover() timed out.
*
* @warning These definitions are deprecated. Use the corresponding errno
@@ -1150,13 +1154,13 @@ struct zcan_work {
*
* The same CAN work queue can be attached to more than one filter.
*
* @see @a can_detach()
* @see @a can_remove_rx_filter()
*
* @note The work queue must be initialized before and the caller must have
* appropriate permissions on it.
*
* @warning This function is deprecated. Use @a can_attach_msgq() along with @a
* k_work_poll_submit() instead.
* @warning This function is deprecated. Use @a can_add_rx_filter_msgq() along
* with @a k_work_poll_submit() instead.
*
* @param dev Pointer to the device structure for the driver instance.
* @param work_q Pointer to the already initialized @a zcan_work queue.
@@ -1173,6 +1177,46 @@ __deprecated int can_attach_workq(const struct device *dev, struct k_work_q *wo
struct zcan_work *work, can_rx_callback_t callback,
void *user_data, const struct zcan_filter *filter);
/**
* @see can_add_rx_filter()
*/
__deprecated static inline int can_attach_isr(const struct device *dev, can_rx_callback_t isr,
void *user_data, const struct zcan_filter *filter)
{
return can_add_rx_filter(dev, isr, user_data, filter);
}
/**
* @see CAN_MSGQ_DEFINE()
*/
#define CAN_DEFINE_MSGQ(name, size) CAN_MSGQ_DEFINE(name, size) __DEPRECATED_MACRO
/**
* @see can_add_rx_filter_msgq()
*/
__deprecated static inline int can_attach_msgq(const struct device *dev, struct k_msgq *msg_q,
const struct zcan_filter *filter)
{
return can_add_rx_filter_msgq(dev, msg_q, filter);
}
/**
* @see can_remove_rx_filter()
*/
__deprecated static inline void can_detach(const struct device *dev, int filter_id)
{
can_remove_rx_filter(dev, filter_id);
}
/**
* @see can_set_state_change_callback()
*/
__deprecated static inline void can_register_state_change_isr(const struct device *dev,
can_state_change_callback_t isr)
{
can_set_state_change_callback(dev, isr);
}
/** @endcond */
/**

View File

@@ -101,11 +101,11 @@ struct net_can_api {
/** Send a single CAN frame */
int (*send)(const struct device *dev, const struct zcan_frame *frame,
can_tx_callback_t cb, void *cb_arg, k_timeout_t timeout);
/** Attach a filter with it's callback */
int (*attach_filter)(const struct device *dev, can_rx_callback_t cb,
/** Add a filter with its callback */
int (*add_rx_filter)(const struct device *dev, can_rx_callback_t cb,
void *cb_arg, const struct zcan_filter *filter);
/** Detach a filter */
void (*detach_filter)(const struct device *dev, int filter_id);
/** Remove a filter */
void (*remove_rx_filter)(const struct device *dev, int filter_id);
/** Enable or disable the reception of frames for net CAN */
int (*enable)(const struct device *dev, bool enable);
};

View File

@@ -71,20 +71,20 @@ static void canopen_detach_all_rx_filters(CO_CANmodule_t *CANmodule)
for (i = 0U; i < CANmodule->rx_size; i++) {
if (CANmodule->rx_array[i].filter_id != -ENOSPC) {
can_detach(CANmodule->dev,
CANmodule->rx_array[i].filter_id);
can_remove_rx_filter(CANmodule->dev,
CANmodule->rx_array[i].filter_id);
CANmodule->rx_array[i].filter_id = -ENOSPC;
}
}
}
static void canopen_rx_isr_callback(struct zcan_frame *msg, void *arg)
static void canopen_rx_callback(struct zcan_frame *msg, void *arg)
{
CO_CANrx_t *buffer = (CO_CANrx_t *)arg;
CO_CANrxMsg_t rxMsg;
if (!buffer || !buffer->pFunct) {
LOG_ERR("failed to process CAN rx isr callback");
LOG_ERR("failed to process CAN rx callback");
return;
}
@@ -94,12 +94,12 @@ static void canopen_rx_isr_callback(struct zcan_frame *msg, void *arg)
buffer->pFunct(buffer->object, &rxMsg);
}
static void canopen_tx_isr_callback(int error, void *arg)
static void canopen_tx_callback(int error, void *arg)
{
CO_CANmodule_t *CANmodule = arg;
if (!CANmodule) {
LOG_ERR("failed to process CAN tx isr callback");
LOG_ERR("failed to process CAN tx callback");
return;
}
@@ -132,7 +132,7 @@ static void canopen_tx_retry(struct k_work *item)
memcpy(msg.data, buffer->data, buffer->DLC);
err = can_send(CANmodule->dev, &msg, K_NO_WAIT,
canopen_tx_isr_callback, CANmodule);
canopen_tx_callback, CANmodule);
if (err == -EAGAIN) {
break;
} else if (err != 0) {
@@ -285,14 +285,14 @@ CO_ReturnError_t CO_CANrxBufferInit(CO_CANmodule_t *CANmodule, uint16_t index,
filter.rtr_mask = 1;
if (buffer->filter_id != -ENOSPC) {
can_detach(CANmodule->dev, buffer->filter_id);
can_remove_rx_filter(CANmodule->dev, buffer->filter_id);
}
buffer->filter_id = can_attach_isr(CANmodule->dev,
canopen_rx_isr_callback,
buffer, &filter);
buffer->filter_id = can_add_rx_filter(CANmodule->dev,
canopen_rx_callback,
buffer, &filter);
if (buffer->filter_id == -ENOSPC) {
LOG_ERR("failed to attach CAN rx isr, no free filter");
LOG_ERR("failed to add CAN rx callback, no free filter");
CO_errorReport(CANmodule->em, CO_EM_MEMORY_ALLOCATION_ERROR,
CO_EMC_SOFTWARE_INTERNAL, 0);
return CO_ERROR_OUT_OF_MEMORY;
@@ -355,7 +355,7 @@ CO_ReturnError_t CO_CANsend(CO_CANmodule_t *CANmodule, CO_CANtx_t *buffer)
msg.rtr = (buffer->rtr ? 1 : 0);
memcpy(msg.data, buffer->data, buffer->DLC);
err = can_send(CANmodule->dev, &msg, K_NO_WAIT, canopen_tx_isr_callback,
err = can_send(CANmodule->dev, &msg, K_NO_WAIT, canopen_tx_callback,
CANmodule);
if (err == -EAGAIN) {
buffer->bufferFull = true;

View File

@@ -35,8 +35,8 @@ struct k_work state_change_work;
enum can_state current_state;
struct can_bus_err_cnt current_err_cnt;
CAN_DEFINE_MSGQ(change_led_msgq, 2);
CAN_DEFINE_MSGQ(counter_msgq, 2);
CAN_MSGQ_DEFINE(change_led_msgq, 2);
CAN_MSGQ_DEFINE(counter_msgq, 2);
static struct k_poll_event change_led_events[1] = {
K_POLL_EVENT_STATIC_INITIALIZER(K_POLL_TYPE_MSGQ_DATA_AVAILABLE,
@@ -69,7 +69,7 @@ void rx_thread(void *arg1, void *arg2, void *arg3)
struct zcan_frame msg;
int filter_id;
filter_id = can_attach_msgq(can_dev, &counter_msgq, &filter);
filter_id = can_add_rx_filter_msgq(can_dev, &counter_msgq, &filter);
printk("Counter filter id: %d\n", filter_id);
while (1) {
@@ -165,7 +165,7 @@ void state_change_work_handler(struct k_work *work)
#endif /* CONFIG_CAN_AUTO_BUS_OFF_RECOVERY */
}
void state_change_isr(enum can_state state, struct can_bus_err_cnt err_cnt)
void state_change_callback(enum can_state state, struct can_bus_err_cnt err_cnt)
{
current_state = state;
current_err_cnt = err_cnt;
@@ -224,7 +224,7 @@ void main(void)
k_work_init(&state_change_work, state_change_work_handler);
k_work_poll_init(&change_led_work, change_led_work_handler);
ret = can_attach_msgq(can_dev, &change_led_msgq, &change_led_filter);
ret = can_add_rx_filter_msgq(can_dev, &change_led_msgq, &change_led_filter);
if (ret == -ENOSPC) {
printk("Error, no filter available!\n");
return;
@@ -257,7 +257,7 @@ void main(void)
printk("ERROR spawning poll_state_thread\n");
}
can_register_state_change_isr(can_dev, state_change_isr);
can_set_state_change_callback(can_dev, state_change_callback);
printk("Finished init.\n");

Some files were not shown because too many files have changed in this diff Show More