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í
commit 8af4bb722d
26 changed files with 579 additions and 533 deletions

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 = {