Commit graph

92 commits

Author SHA1 Message Date
Henrik Brix Andersen 71fe0f413b drivers: can: remove unnecessary asserts
Remove unnecessary asserts from various CAN controller drivers. These
asserts are all covered by the common CAN subsystem API implementations.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-04-24 09:53:06 +02:00
Henrik Brix Andersen 6d35ec718f drivers: can: add support for specifying minimum supported CAN bitrate
Add support for specifying the minimum bitrate supported by a CAN
controller in CAN_DT_DRIVER_CONFIG_GET() and
CAN_DT_DRIVER_CONFIG_INST_GET().

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-03-12 10:43:36 +01:00
Henrik Brix Andersen a57db0ddcb drivers: can: rework support for manual bus-off recovery
Since all CAN controllers drivers seem to support automatic recovery (for
any future drivers for hardware without this hardware capability this can
easily be implemented in the driver), change the Zephyr CAN controller API
policy to:

- Always enable automatic bus recovery upon driver initialization,
  regardless of Kconfig options. Since CAN controllers are initialized in
  "stopped" state, no unwanted bus-off recovery will be started at this
  point.

- Invert and rename the Kconfig CONFIG_CAN_AUTO_BUS_OFF_RECOVERY, which is
  enabled by default, to CONFIG_CAN_MANUAL_RECOVERY_MODE, which is disabled
  by default. Enabling CONFIG_CAN_MANUAL_RECOVERY_MODE=y enables support
  for the can_recover() API function and a new manual recovery mode (see
  next bullet). Keeping this guarded by Kconfig allows keeping the flash
  footprint down for applications not using manual bus-off recovery.

- Introduce a new CAN controller operational mode
  CAN_MODE_MANUAL_RECOVERY. Support for this is only enabled if
  CONFIG_CAN_MANUAL_RECOVERY_MODE=y. Having this as a mode allows
  applications to inquire whether the CAN controller supports manual
  recovery mode via the can_get_capabilities() API function and either fail
  or rely on automatic recovery - and it allows CAN controller drivers not
  supporting manual recovery mode to fail early in can_set_mode() during
  application startup instead of failing when can_recover() is called at a
  later point in time.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-03-02 18:26:48 +01:00
Henrik Brix Andersen 0c13f3888d dts: bindings: can: remove deprecated properties for initial timing
Remove the deprecated properties for setting the initial timing parameters
of a CAN controller.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-03-01 09:00:49 +01:00
Henrik Brix Andersen 5e9da13200 drivers: can: propagate CAN controller operation mode to CAN transceiver
Propagate the current CAN controller operation mode to the CAN transceiver
when enabling it.

Some more advanced CAN transceivers, especially those supporting Partial
Networking (CAN PN), require knowledge of the intended CAN operation mode
(e.g. normal mode vs. listen-only mode).

This commit simply prepares the CAN transceiver API for supporting such CAN
transceivers, although no in-tree drivers require this information yet.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-01-26 15:55:18 +00:00
Henrik Brix Andersen 4340724fd0 drivers: can: use common accessor for getting maximum supported bitrate
Use a common accessor for getting the maximum supported bitrate of a CAN
controller.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-01-22 13:09:09 +01:00
Henrik Brix Andersen 3436c93387 drivers: can: remove run-time RTR filtering, add build-time RTR filter
A growing number of CAN controllers do not have support for individual RX
hardware filters based on the Remote Transmission Request (RTR) bit. This
leads to various work-arounds on the driver level mixing hardware and
software filtering.

As the use of RTR frames is discouraged by CAN in Automation (CiA) - and
not even supported by newer standards, e.g. CAN FD - this often leads to
unnecessary overhead, added complexity, and worst-case to non-portable
behavior between various CAN controller drivers.

Instead, move to a simpler approach where the ability to accept/reject RTR
frames is globally configured via Kconfig. By default, all incoming RTR
frames are rejected at the driver level, a setting which can be supported
in hardware by most in-tree CAN controllers drivers.

Legacy applications or protocol implementations, where RTR reception is
required, can now select CONFIG_CAN_ACCEPT_RTR to accept incoming RTR
frames matching added CAN filters. These applications or protocols will
need to distinguish between RTR and data frames in their respective CAN RX
frame handling routines.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-01-21 11:00:31 +01:00
Henrik Brix Andersen 639085b614 drivers: can: mcp2515: flexcan: use common config and data structures
Use the common CAN controller driver configuration and data structures.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-01-19 09:45:29 +01:00
Benedikt Schmidt c533924370 drivers: can: fix thread function signatures
Fix thread function signatures to avoid a stack corruption on thread exit.

Signed-off-by: Benedikt Schmidt <benedikt.schmidt@embedded-solutions.at>
2023-10-30 12:24:34 +01:00
Henrik Brix Andersen 6c5400d2e1 drivers: can: be consistent in filter_id checks when removing rx filters
Change the CAN controller driver implementations for the
can_remove_rx_filter() API call to be consistent in their validation of the
supplied filter_id.

Fixes: #64398

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-10-26 09:49:51 +02:00
Henrik Brix Andersen d9b69f9d33 drivers: can: mcp2515: add CAN statistics support
Add support for CAN statistics to the Microchip MCP2515 CAN controller
driver. The hardware only supports reporting RX overruns.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-10-23 10:33:10 +02:00
Henrik Brix Andersen 4d321448ad drivers: can: mcp2515: only copy frame data for non-RTR frames
Only copy frame data for non-RTR frames as RTR frames do not carry any
data. Limit the amount of data copied to the actual DLC.

Fixes: #57002

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-10-13 10:08:45 +03:00
Henrik Brix Andersen 9bf17f9276 drivers: can: remove unnecessary asserts in timing callbacks
Remove unnecessary calls to __ASSERT_NO_MSG() in CAN controller driver
timing setter callbacks. The CAN API functions can_set_timing and
can_set_timing_data() already provide run-time timing parameter validation.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-10-02 16:59:42 +01:00
Henrik Brix Andersen a9d3935fa0 drivers: can: solely use sjw from devicetree for initial timing
Update the CAN controller drivers to solely use the sjw and sjw-data
devicetree properties for setting the initial timing when devicetree timing
parameters are specified in Time Quanta (TQ).

Any timing set via the CAN timing APIs will contain either user-provided or
automatically calculated SJW values. This includes any timing parameters
calculated from bus-speed and bus-speed-data devicetree properties.

Update the CAN controller driver tests accordingly and remove the
CAN_SJW_NO_CHANGE definition as it has lost its meaning.

Fixes: #63033

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-09-28 16:28:56 +02:00
Henrik Brix Andersen 49ab0b22f0 drivers: can: switch to CAN_DEVICE_DT_INST_DEFINE for remaining drivers
Switch from using DEVICE_DT_DEFINE()/DEVICE_DT_INST_DEFINE() to using
CAN_DEVICE_DT_DEFINE()/CAN_DEVICE_DT_INST_DEFINE() for remaining drivers.

This unifies CAN controller device driver initialization regardless of the
driver implementing CAN statistics support or not.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-09-22 08:37:05 -05:00
Nick Ward 2d65acca3a drivers: gpio: use gpio_is_ready_dt helper function
Update `struct gpio_dt_spec` use with gpio_is_ready_dt()

Signed-off-by: Nick Ward <nix.ward@gmail.com>
2023-08-28 08:48:35 -05:00
Henrik Brix Andersen bab0c0879d drivers: can: mcp2515: set thread name
Set the thread name for the MCP2515 CAN controller driver to aid in
debugging.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-06-02 09:43:16 +02:00
Henrik Brix Andersen 36bffb20ae drivers: can: move CAN utility function to public header
Rename the can_utils_filter_match() utility function to
can_frame_matches_filter(), move it to the public
include/zephyr/drivers/can.h header file, and add a missing mismatch for
standard (11-bit) ID frames passing extended (29-bit) filters.

This allows using the utility function in out-of-tree CAN drivers.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-02-23 09:00:12 +01:00
Bartosz Bilas e077fb73ec drivers: tests: replace usage of spi_is_ready with spi_is_ready_dt
`spi_is_ready` function is being deprecated in favor of
`spi_is_ready_dt` so let's replace the old usage in the tree.

Signed-off-by: Bartosz Bilas <bartosz.bilas@hotmail.com>
2022-12-07 09:40:23 -06:00
Henrik Brix Andersen 58afbc8f26 drivers: can: mcp2515: add support for multiple device instances
Add support for multiple MCP2515 CAN controller device instances.

Fixes: #51458

Signed-off-by: Henrik Brix Andersen <henrik@brixandersen.dk>
2022-10-27 11:03:05 +02:00
Henrik Brix Andersen f8a88cdb27 drivers: can: use flags fields for can_frame and can_filter structs
The can_frame and can_filter structs support a number of different flags
(standard/extended CAN ID type, Remote Transmission Request, CAN-FD format,
Bit Rate Switch, ...). Each of these flags is represented as a discrete bit
in the given structure.

This design pattern requires every user of these structs to initialize all
of these flags to either 0 or 1, which does not scale well for future flag
additions.

Some of these flags have associated enumerations to be used for assignment,
some do not. CAN drivers and protocols tend to rely on the logical value of
the flag instead of using the enumeration, leading to a very fragile
API. The enumerations are used inconsistently between the can_frame and
can_filter structures, which further complicates the API.

Instead, convert these flags to bitfields with separate flag definitions
for the can_frame and can_filter structures. This API allows for future
extensions without having to revisit existing users of the two
structures. Furthermore, this allows driver to easily check for unsupported
flags in the respective API calls.

As this change leads to the "id_mask" field of the can_filter to be the
only mask present in that structure, rename it to "mask" for simplicity.

Fixes: #50776

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-10-25 16:32:10 +02:00
Henrik Brix Andersen 6d08944ac1 drivers: can: mcp2515: abort transfers before entering configuration mode
Abort any pending transmissions before entering configuration mode and
notify the senders.

Poll for mode change completion instead of assuming mode changes take
immediate effect. This is needed for successfully entering configuration
mode with recently aborted transmissions.

Fixes: #50545

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-09-23 17:25:01 +00:00
Henrik Brix Andersen 78d6ba5ce3 drivers: can: mcp2515: assume can_send() always provides callback
Given the recent change to the CAN controller can_send() API wrapper,
assume that a callback is always provided at the driver level.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-09-23 17:25:01 +00:00
Henrik Brix Andersen 180cdc105e 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>
2022-09-13 16:06:50 +00:00
Henrik Brix Andersen 590cd5cb72 drivers: can: rename can_state enumerations
Rename the can_state enumerations to contain the word STATE to make their
meaning more clear:
- CAN_ERROR_ACTIVE  => CAN_STATE_ERROR_ACTIVE
- CAN_ERROR_WARNING => CAN_STATE_ERROR_WARNING
- CAN_ERROR_PASSIVE => CAN_STATE_ERROR_PASSIVE
- CAN_BUS_OFF       => CAN_STATE_BUS_OFF

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-08-18 21:41:11 +00:00
Henrik Brix Andersen bcd936b3b1 drivers: can: mcp2515: rename internal funtions to not use zcan
Rename the mcp2515 driver internal functions to not use "zcan" to avoid
confusion after "struct can_frame" renaming.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-08-18 10:19:29 +02:00
Henrik Brix Andersen 13c75417ba drivers: can: remove z prefix from public CAN API types
Remove the "z" prefix from the public CAN controller API types as this
makes them appear as internal APIs.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-08-18 10:19:29 +02:00
Henrik Brix Andersen bb08e4251a drivers: can: add API for getting supported capabilities
Add API function for getting the supported capabilities of a CAN
controller. This allows for writing portable CAN applications.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-07-13 10:13:56 +02:00
Henrik Brix Andersen 3f97d11afd drivers: can: convert enum can_mode to a bit field
Convert the can_mode enum to a bit field to prepare for future extensions
(CAN-FD mode, transmitter delay compensation, one-shot mode, 3-samples
mode, ...).

Rename the existing modes:
- CAN_NORMAL_MODE   -> CAN_MODE_NORMAL
- CAN_SILENT_MODE   -> CAN_MODE_LISTENONLY
- CAN_LOOPBACK_MODE -> CAN_MODE_LOOPBACK

These mode names align with the Linux naming for CAN control modes.

The old CAN_SILENT_LOOPBACK_MODE can be set with the bitmask
(CAN_MODE_LISTENONLY | CAN_MODE_LOOPBACK).

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-05-11 10:47:54 +02:00
Henrik Brix Andersen 18890828b8 drivers: can: split CAN classic and CAN-FD syscalls
Split CAN classic and CAN-FD syscalls into two:
- can_set_timing() -> can_set_timing() + can_set_timing_data()
- can_set_bitrate() -> can_set_bitrate() + can_set_bitrate_data()

Fixes: #45303

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-05-11 10:47:54 +02:00
Gerard Marull-Paretas fb60aab245 drivers: migrate includes to <zephyr/...>
In order to bring consistency in-tree, migrate all drivers to the new
prefix <zephyr/...>. Note that the conversion has been scripted, refer
to #45388 for more details.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-05-06 19:58:21 +02:00
Henrik Brix Andersen e4c93c3a60 drivers: can: mcp2515: do not soft-reset device when changing timing
Do not soft-reset device when changing timing parameters as the
soft-reset will discard the configured CAN controller mode.

Fixes: #44837

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-04-21 13:00:46 +02:00
Henrik Brix Andersen 17dcc73944 drivers: can: mcp2515: fail when trying to set unsupported mode
Do not silently ignore attempts to set an unsupported mode. Return
-ENOTSUP instead.

Fixes: #44706

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-04-19 11:47:49 +02:00
Alexander Wachter e24b087918 drivers: can: socketCAN: Move socket code to own file
Move the code for socket instanciation from each driver
to a generic driver, that makes an instance of a socketCAN
net device for the chosen node.

Signed-off-by: Alexander Wachter <alexander@wachter.cloud>
2022-04-08 09:31:51 +02:00
Henrik Brix Andersen c389594e12 drivers: can: mcp2515: correct min/max timing values
The MCP2515 requires phase segment 2 to be at least 2 time quanta.

The prescaler has a 6 bit register, allowing for real-world prescaler
values between 1 and 64.

Fixes: #44484

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-04-05 10:33:08 -07:00
Henrik Brix Andersen 99964fab45 drivers: can: add missing static keywords
Add missing static keywords for functions internal to the drivers.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-03-31 10:31:37 +02:00
Henrik Brix Andersen 6f910ae41e drivers: can: mcp2515: do not force CONFIG_CAN_AUTO_BUS_OFF_RECOVERY=y
Do not force CONFIG_CAN_AUTO_BUS_OFF_RECOVERY=y. Instead fix the return
type of the mcp2515_recover() function and return -ENOTSUP.

Fixes: #44344

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-03-31 10:31:15 +02:00
Henrik Brix Andersen 67ba9900f0 drivers: can: add struct device argument to callback functions
Include a pointer to the CAN controller device for the CAN
transmit, receive, and state change callback functions.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-03-19 14:31:22 -04:00
Henrik Brix Andersen 000661db21 drivers: can: mcp2515: add transceiver support
Add support for CAN transceivers to the Microchip MCP2515 CAN driver.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-03-15 14:32:29 -05:00
Fabio Baltieri eb7b3b66a2 drivers: can: mcp2515: fix build break
Fix build break introduced in 03ab730347:

/zephyr/drivers/can/can_mcp2515.c:871:55: error: 'const struct
mcp2515_config' has no member named 'int_pin'; did you mean 'int_gpio'?
  871 |   LOG_ERR("Unable to configure GPIO pin %u", dev_cfg->int_pin);

/zephyr/drivers/can/can_mcp2515.c:942:14: error: 'GPIO_DT_SPEC_INST_GET'
undeclared here (not in a function)
  942 |  .int_gpio = GPIO_DT_SPEC_INST_GET(0),
      |              ^~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Fabio Baltieri <fabio.baltieri@gmail.com>
2022-02-24 09:09:02 -08:00
Henrik Brix Andersen de45e8ec72 drivers: can: use separate log modules for each driver
Use separate log modules for each CAN driver similar to other
sub-systems/driver classes.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-02-22 10:34:04 -05:00
Gerard Marull-Paretas 03ab730347 drivers: can: mcp2515: use gpio_dt_spec
Use struct gpio_dt_spec to simplify driver implementation.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-02-21 22:16:10 -05:00
Henrik Brix Andersen 7815de13c3 drivers: can: mcp2515: reduce the number of tx buffers to 1
Reduce the number of TX buffers in use on the MCP2515 from 3 to 1 in
order to avoid CAN frame priority inversion.

The MCP2515 is unable to do internal TX frame arbitration based on the
CAN-ID of the frame. Priority must be set per TX buffer and the priority
cannot be rewritten unless the frame transmission is aborted.

Fixes: #26541

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-02-21 19:37:35 -05:00
Henrik Brix Andersen 2aed5a1237 drivers: can: remove CAN_BUS_UNKNOWN CAN controller state
The CAN_BUS_UNKNOWN CAN controller state is only used to indicate that
the current CAN controller state could not be read.

Remove it and change the signature of the can_get_state() API function
to return an integer indicating success or failure.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-01-28 10:05:29 +01:00
Henrik Brix Andersen 7c41d34339 drivers: can: mcp2515: add support for CAN_ERROR_WARNING
Add support for CAN_ERROR_WARNING CAN controller state.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-01-19 18:34:26 +01:00
Gerard Marull-Paretas 6378fcd0be drivers: can: drop DEV_DATA/DEV_CFG usage
Stop using redundant DEV_DATA/DEV_CFG macros and use dev->data and
dev->config instead.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-01-19 18:16:02 +01:00
Henrik Brix Andersen e919db3222 drivers: can: add user_data to state change callback
Add optional user data argument to can_set_state_change_callback() to
comply with Zephyr API design guidelines.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-01-11 11:54:04 +01:00
Henrik Brix Andersen 8af4bb722d 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>
2022-01-10 10:44:37 +01:00
Henrik Brix Andersen f499559434 drivers: can: deprecate the use of CAN-specific error return values
Deprecate the use of CAN-specific error return values and replace them
with standard errno values.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2021-12-09 12:37:40 -05:00
Henrik Brix Andersen b21a91e468 drivers: can: catch up on API naming changes
Catch up on the CAN driver API argument naming changes:
- Unify naming of callback function pointers as "callback".
- Unify naming of user-specified callback function arguments as
  "user_data".
- Instances and pointers to struct zcan_frame are named "frame",
  not "msg", to avoid confusion with the CAN message queue support.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2021-12-07 15:39:06 -05:00