From 09f45840f113c8aba9a61313bdfed9f0202b64df Mon Sep 17 00:00:00 2001 From: Thomas Stranger Date: Fri, 12 Aug 2022 23:42:16 +0200 Subject: [PATCH] drivers/can: stm32fd fix not applied clock divider The configurable CAN clock divider CAN_STM32FD_CLOCK_DIVISOR was not applied during initialization, because write protection was not disabled. While the clock divider was not applied, it was still used in clock rate calculation, therefore resulting in incorrect bus speed setup. Signed-off-by: Thomas Stranger --- drivers/can/can_mcan.c | 12 ++++++++++++ drivers/can/can_mcan.h | 2 ++ drivers/can/can_stm32fd.c | 1 + 3 files changed, 15 insertions(+) diff --git a/drivers/can/can_mcan.c b/drivers/can/can_mcan.c index cd75bc59f92..8f32525da40 100644 --- a/drivers/can/can_mcan.c +++ b/drivers/can/can_mcan.c @@ -1086,3 +1086,15 @@ int can_mcan_get_max_bitrate(const struct device *dev, uint32_t *max_bitrate) return 0; } + +/* helper function allowing mcan drivers without access to private mcan + * definitions to set CCCR_CCE, which might be needed to disable write + * protection for some registers. + */ +void can_mcan_enable_configuration_change(const struct device *dev) +{ + const struct can_mcan_config *cfg = dev->config; + struct can_mcan_reg *can = cfg->can; + + can->cccr |= CAN_MCAN_CCCR_CCE; +} diff --git a/drivers/can/can_mcan.h b/drivers/can/can_mcan.h index 3d28ffcae0c..ccf94e903bb 100644 --- a/drivers/can/can_mcan.h +++ b/drivers/can/can_mcan.h @@ -296,4 +296,6 @@ void can_mcan_set_state_change_callback(const struct device *dev, int can_mcan_get_max_bitrate(const struct device *dev, uint32_t *max_bitrate); +void can_mcan_enable_configuration_change(const struct device *dev); + #endif /* ZEPHYR_DRIVERS_CAN_MCAN_H_ */ diff --git a/drivers/can/can_stm32fd.c b/drivers/can/can_stm32fd.c index ab4199d6b3e..14928f1775c 100644 --- a/drivers/can/can_stm32fd.c +++ b/drivers/can/can_stm32fd.c @@ -87,6 +87,7 @@ static int can_stm32fd_clock_enable(const struct device *dev) return ret; } + can_mcan_enable_configuration_change(dev); FDCAN_CONFIG->CKDIV = CAN_STM32FD_CLOCK_DIVISOR >> 1; return 0;