From 043e6ecbf6b4c80e9eef4a7265e47e588fa7e44f Mon Sep 17 00:00:00 2001 From: Henrik Brix Andersen Date: Fri, 27 Oct 2023 13:11:09 +0200 Subject: [PATCH] drivers: can: add accessor for the CAN bit error counter Add accessor function for the CAN bit error counter. Signed-off-by: Henrik Brix Andersen --- drivers/can/can_handlers.c | 8 ++++++++ drivers/can/can_shell.c | 5 +++-- include/zephyr/drivers/can.h | 25 +++++++++++++++++++++++++ tests/drivers/can/api/src/stats.c | 1 + 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/drivers/can/can_handlers.c b/drivers/can/can_handlers.c index 842c3786d5d..7401a7f9fd3 100644 --- a/drivers/can/can_handlers.c +++ b/drivers/can/can_handlers.c @@ -246,6 +246,14 @@ static inline int z_vrfy_can_recover(const struct device *dev, k_timeout_t timeo #ifdef CONFIG_CAN_STATS +static inline uint32_t z_vrfy_can_stats_get_bit_errors(const struct device *dev) +{ + Z_OOPS(Z_SYSCALL_OBJ(dev, K_OBJ_DRIVER_CAN)); + + return z_impl_can_stats_get_bit_errors(dev); +} +#include + static inline uint32_t z_vrfy_can_stats_get_bit0_errors(const struct device *dev) { Z_OOPS(Z_SYSCALL_OBJ(dev, K_OBJ_DRIVER_CAN)); diff --git a/drivers/can/can_shell.c b/drivers/can/can_shell.c index 66900f18e8f..4faf11aae4e 100644 --- a/drivers/can/can_shell.c +++ b/drivers/can/can_shell.c @@ -364,8 +364,9 @@ static int cmd_can_show(const struct shell *sh, size_t argc, char **argv) #ifdef CONFIG_CAN_STATS shell_print(sh, "statistics:"); - shell_print(sh, " bit0 errors: %u", can_stats_get_bit0_errors(dev)); - shell_print(sh, " bit1 errors: %u", can_stats_get_bit1_errors(dev)); + shell_print(sh, " bit errors: %u", can_stats_get_bit_errors(dev)); + shell_print(sh, " bit0 errors: %u", can_stats_get_bit0_errors(dev)); + shell_print(sh, " bit1 errors: %u", can_stats_get_bit1_errors(dev)); shell_print(sh, " stuff errors: %u", can_stats_get_stuff_errors(dev)); shell_print(sh, " crc errors: %u", can_stats_get_crc_errors(dev)); shell_print(sh, " form errors: %u", can_stats_get_form_errors(dev)); diff --git a/include/zephyr/drivers/can.h b/include/zephyr/drivers/can.h index e53a39471d7..0028abb330d 100644 --- a/include/zephyr/drivers/can.h +++ b/include/zephyr/drivers/can.h @@ -1384,6 +1384,27 @@ static inline void can_set_state_change_callback(const struct device *dev, * @{ */ +/** + * @brief Get the bit error counter for a CAN device + * + * The bit error counter is incremented when the CAN controller is unable to + * transmit either a dominant or a recessive bit. + * + * @note @kconfig{CONFIG_CAN_STATS} must be selected for this function to be + * available. + * + * @param dev Pointer to the device structure for the driver instance. + * @return bit error counter + */ +__syscall uint32_t can_stats_get_bit_errors(const struct device *dev); + +#ifdef CONFIG_CAN_STATS +static inline uint32_t z_impl_can_stats_get_bit_errors(const struct device *dev) +{ + return Z_CAN_GET_STATS(dev).bit_error; +} +#endif /* CONFIG_CAN_STATS */ + /** * @brief Get the bit0 error counter for a CAN device * @@ -1393,6 +1414,8 @@ static inline void can_set_state_change_callback(const struct device *dev, * @note @kconfig{CONFIG_CAN_STATS} must be selected for this function to be * available. * + * @see can_stats_get_bit_errors() + * * @param dev Pointer to the device structure for the driver instance. * @return bit0 error counter */ @@ -1414,6 +1437,8 @@ static inline uint32_t z_impl_can_stats_get_bit0_errors(const struct device *dev * @note @kconfig{CONFIG_CAN_STATS} must be selected for this function to be * available. * + * @see can_stats_get_bit_errors() + * * @param dev Pointer to the device structure for the driver instance. * @return bit1 error counter */ diff --git a/tests/drivers/can/api/src/stats.c b/tests/drivers/can/api/src/stats.c index 85320dc64ca..c272cc89b9f 100644 --- a/tests/drivers/can/api/src/stats.c +++ b/tests/drivers/can/api/src/stats.c @@ -23,6 +23,7 @@ ZTEST_USER(can_stats, test_can_stats_accessors) { uint32_t val; + val = can_stats_get_bit_errors(can_dev); val = can_stats_get_bit0_errors(can_dev); val = can_stats_get_bit1_errors(can_dev); val = can_stats_get_stuff_errors(can_dev);