drivers: can: add start and stop CAN controller API functions

Up until now, the Zephyr CAN controller drivers set a default bitrate (or
timing) specified via devicetree and start the CAN controller in their
respective driver initialization functions.

This is fine for CAN nodes using only one fixed bitrate, but if the bitrate
is set by the user (e.g. via a DIP-switch or other HMI which is very
common), the CAN driver will still initialise with the default
bitrate/timing at boot and use this until the application has determined
the requested bitrate/timing and set it using
can_set_bitrate()/can_set_timing().

During this period, the CAN node will potentially destroy valid CAN frames
on the CAN bus (which is using the soon-to-be-set-by-the-application
bitrate) by sending error frames. This causes interruptions to the ongoing
CAN bus traffic when a Zephyr-based CAN node connected to the bus is
(re-)booted.

Instead, require all configuration (setting bitrate, timing, or mode) to
take place when the CAN controller is stopped. This maps nicely to entering
"reset mode" (called "configuration mode" or "freeze mode" for some CAN
controller implementations) when stopping and exiting this mode when
starting the CAN controller.

Fixes: #45304

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
This commit is contained in:
Henrik Brix Andersen 2022-08-31 14:28:42 +02:00 committed by Fabio Baltieri
commit 180cdc105e
30 changed files with 930 additions and 319 deletions

View file

@ -157,11 +157,25 @@ static void canopen_tx_retry(struct k_work *item)
void CO_CANsetConfigurationMode(void *CANdriverState)
{
/* No operation */
struct canopen_context *ctx = (struct canopen_context *)CANdriverState;
int err;
err = can_stop(ctx->dev);
if (err != 0 && err != -EALREADY) {
LOG_ERR("failed to stop CAN interface (err %d)", err);
}
}
void CO_CANsetNormalMode(CO_CANmodule_t *CANmodule)
{
int err;
err = can_start(CANmodule->dev);
if (err != 0 && err != -EALREADY) {
LOG_ERR("failed to start CAN interface (err %d)", err);
return;
}
CANmodule->CANnormal = true;
}
@ -250,8 +264,8 @@ void CO_CANmodule_disable(CO_CANmodule_t *CANmodule)
canopen_detach_all_rx_filters(CANmodule);
err = can_set_mode(CANmodule->dev, CAN_MODE_LISTENONLY);
if (err) {
err = can_stop(CANmodule->dev);
if (err != 0 && err != -EALREADY) {
LOG_ERR("failed to disable CAN interface (err %d)", err);
}
}