Commit graph

79 commits

Author SHA1 Message Date
Henrik Brix Andersen 68096cedae drivers: can: rename struct can_driver_config fields
Rename the "bus_speed" and "bus_speed_data" fields of struct
can_driver_config to "bitrate" and "bitrate_data" to match the
corresponding devicetree properties and the terminology used in the rest of
the CAN subsystem API.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-06-05 14:43:00 +01:00
Henrik Brix Andersen 695e704b5d dts: bindings: can: rename bus-speed/bus-speed-data properties
Deprecate the CAN controller bus-speed/bus-speed-data properties and rename
them to bitrate/bitrate-data to match the terminology used in other CAN
devicetree properties and the CAN subsystem API.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-06-05 14:43:00 +01:00
Yong Cong Sin bbe5e1e6eb build: namespace the generated headers with zephyr/
Namespaced the generated headers with `zephyr` to prevent
potential conflict with other headers.

Introduce a temporary Kconfig `LEGACY_GENERATED_INCLUDE_PATH`
that is enabled by default. This allows the developers to
continue the use of the old include paths for the time being
until it is deprecated and eventually removed. The Kconfig will
generate a build-time warning message, similar to the
`CONFIG_TIMER_RANDOM_GENERATOR`.

Updated the includes path of in-tree sources accordingly.

Most of the changes here are scripted, check the PR for more
info.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-05-28 22:03:55 +02:00
Henrik Brix Andersen 0f7cd6128e drivers: can: set default initial bitrates via Kconfig
Set the default initial bitrates globally via Kconfig. The initial bitrates
can still be overridden using the "bus-speed" and "bus-speed-data"
devicetree properties.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-05-16 09:23:59 +02:00
Henrik Brix Andersen cba3a831e2 drivers: can: remove the use of bitfields from struct can_filter
Remove the use of bitfields in the can_filter struct.

Signed-off-by: Henrik Brix Andersen <henrik@brixandersen.dk>
2024-04-24 09:53:06 +02:00
Henrik Brix Andersen 09604d4044 drivers: can: validate arguments in can_add_rx_filter()
Validate the CAN ID and CAN ID mask used in the can_filter struct before
passing it to the driver.

Signed-off-by: Henrik Brix Andersen <henrik@brixandersen.dk>
2024-04-24 09:53:06 +02:00
Henrik Brix Andersen c8af469ce4 drivers: can: remove the use of bitfields from struct can_frame
Remove the use of bitfields in the can_frame struct.

Signed-off-by: Henrik Brix Andersen <henrik@brixandersen.dk>
2024-04-24 09:53:06 +02:00
Henrik Brix Andersen 8eded2f76b drivers: can: add can_get_bitrate_{min,max}(), deprecate existing APIs
Since the minimum/maximum supported bitrates are now stored in the common
CAN controller driver configuration struct, retrieving these can no longer
fail.

Add new CAN controller API functions can_get_bitrate_min() and
can_get_bitrate_max() reflecting this and deprecate the existing
can_get_min_bitrate() and can_get_max_bitrate().

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-04-11 17:08:35 -04:00
Henrik Brix Andersen 85b3e458ce drivers: can: bump API version from 1.0.0 to 1.1.0
Bump the CAN controller API version from 1.0.0 to 1.1.0 to reflect bus
recovery API changes introduced for v3.7.0.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-04-11 16:44:15 -04:00
Henrik Brix Andersen c6e4748deb drivers: can: deprecate CAN_MAX_STD_ID/CAN_MAX_EXT_ID macros
Deprecate CAN_MAX_STD_ID/CAN_MAX_EXT_ID macros in favor of
CAN_STD_ID_MASK/CAN_EXT_ID_MASK.

Signed-off-by: Henrik Brix Andersen <henrik@brixandersen.dk>
2024-04-10 10:00:59 +02:00
Henrik Brix Andersen 2f136f21a4 drivers: can: use is_null_no_warn() check in generic macro
Use is_null_no_warn() for checking the init_fn against NULL in order to
avoid compiler warning when using CONFIG_COMPILER_SAVE_TEMPS=y.

Fixes: #70390

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-04-05 12:20:04 +02:00
Henrik Brix Andersen a3631264d1 drivers: can: add utility macro for calculating TDC offset
Add a utility macro for calculating the Transmitter Delay Compensation
(TDC) Offset using the sample point and CAN core clock prescaler specified
by a set of data phase timing parameters.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-03-28 09:41:53 +00:00
Ederson de Souza 28d08ada28 include/zephyr: Add 'version' and 'since' tag to groups
Based on information from doc/develop/api/overview.rst, add current
version for some groups representing APIs, following the following
table:

  - Experimental:   0.1.0
  - Unstable:       0.8.0
  - Stable:         1.0.0

Also based on doc/develop/api/overview.rst, add 'since' tag to the
groups.

Signed-off-by: Ederson de Souza <ederson.desouza@intel.com>
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2024-03-19 13:13:45 +01:00
Henrik Brix Andersen c769da9e55 drivers: can: add can_get_min_bitrate() API function
Add a new CAN controller API function can_get_min_bitrate() for getting the
minimum supported bitrate of a CAN controller/transceiver combination.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-03-12 10:43:36 +01: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 afc7c20d5a drivers: can: fix can_get_core_clock() API description
The can_get_core_clock() API function returns the core clock rate of the
CAN controller, which provides the minimum time quantum (mtq). This clock
can be further divided by the CAN clock prescaler, providing the time
quantum (tq).

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-03-11 17:24:54 +01:00
Henrik Brix Andersen 49653d39dd drivers: can: extend support for automatic sample point location
Extend support for automatic sample point location to also cover
can_calc_timing() and can_calc_timing_data().

Previously, automatic sample point location was only supported by
can_set_bitrate() and can_set_bitrate_data().

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-03-06 15:23:13 -06:00
Henrik Brix Andersen 5f99e771fe drivers: can: deprecate can_calc_prescaler()
Deprecate the can_calc_prescaler() API function, as it allows for bitrate
errors. Bitrate errors between nodes on the same network leads to them
drifting apart after the start-of-frame (SOF) synchronization has taken
place, leading to bus errors.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-03-05 15:43:29 -06: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 583d44d7d5 drivers: can: add can_get_transceiver() system call
Add system call can_get_transceiver() for getting the CAN transceiver
associated with a CAN controller.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-01-26 14:27:57 +01:00
Henrik Brix Andersen 69d072ad2f drivers: can: add can_get_mode() system call
Add system call can_get_mode() for getting the current operation mode of a
CAN controller.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-01-26 14:27:57 +01: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 75117a0deb drivers: can: remove CAN_FILTER_FDF flag
Remove the CAN_FILTER_FDF flag for filtering on classic CAN/CAN FD frames
as it is not supported natively by any known CAN controller.

Applications can still filter on classic CAN/CAN FD frames in their receive
callback functions as needed.

Fixes: #64554

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-01-19 09:55:43 +01:00
Henrik Brix Andersen e19533382d drivers: can: add structs for common configuration and data fields
Add structs and initializers for common CAN controller driver configuration
and data fields.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2024-01-19 09:45:29 +01:00
Henrik Brix Andersen 043e6ecbf6 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 <hebad@vestas.com>
2023-11-02 09:48:05 +01:00
Henrik Brix Andersen 5d5249d85b drivers: can: unify spelling of CAN Flexible Data-rate abbreviation
Unify spelling of CAN Flexible Data-rate abbreviation to "CAN FD" instead
of "CAN-FD". The former aligns with the CAN in Automation (CiA)
recommendation.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-11-01 11:17:17 +00:00
Henrik Brix Andersen 6ddde16bdb drivers: can: add missing documentation to can.h
Add missing doxygen documentation to the can.h header file.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-10-27 12:28:04 +02:00
Henrik Brix Andersen 40ad8c8711 drivers: can: add bit error counter
The bit error counter is incremented when the CAN controller is unable to
transmit either a dominant or a recessive bit.

This new error counter should only be incremented directly if the CAN
controller is unable to distinquish between failure to transmit a dominant
versus failure to transmit a recessive bit. If the CAN controller supports
distinguishing between the two, either the `bit0` or `bit1` error counter
shall be incremented instead. Incrementing one of these will automatically
increment the bit error count as well.

Typically, a CAN controller driver will either use this new bit error
counter or the discrete bit0 and bit1 error counters depending on the
features supported by the hardware. Mixing the two on driver level should
not be required.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-10-23 18:20:23 +02:00
Henrik Brix Andersen 0cfca8be85 drivers: can: add accessor functions for the CAN statistics
Add accessor functions for the individual CAN statistics.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-10-23 10:33:04 +02: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 e397b85eb8 drivers: can: calculate a default SJW value
Change the CAN timing calculation APIs to automatically calculate a default
(Re-)Synchronization Jump Width (SJW) value. The calculated value can be
overwritten by the caller if desired.

This allows automatically scaling the SJW according to the number of Time
Quanta (TQ) used for phase segment 2 instead of relying on a compile-time
fallback value defined in devicetree.

This reduces the can_set_timing()/can_set_timing_data() API functions to
simple setters (with validation).

Fixes: #63033

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-09-28 16:28:56 +02:00
Fabio Baltieri 4856fd4cb6 can: rework the table lookup code in can_dlc_to_bytes
Rework the can_dlc_to_bytes table lookup code in a way that allow the
compiler to guess the resulting output and somehow fix the build
warning:

zephyr/drivers/can/can_nxp_s32_canxl.c:757:9: warning:
'__builtin___memcpy_chk' forming offset [16, 71] is out of the bounds
[0, 16] of object 'frame' with type 'struct can_frame' [-Warray-bounds]
 757 | memcpy(frame->data, msg_data.data, can_dlc_to_bytes(frame->dlc));

where the compiler detects that frame->data is 8 bytes long but
can_dlc_to_bytes could return more than that.

Can be reproduced with:

west build -p -b s32z270dc2_rtu1_r52 \
	-T samples/net/sockets/can/sample.net.sockets.can.one_socket

Suggested-by: Martin Jäger <martin@libre.solar>
Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2023-09-15 15:05:57 +02:00
Gerard Marull-Paretas 1b73d1e0c6 drivers: can: allow NULL init function
Generic device API allows for NULL init function, do the same for I2C.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-04-19 10:00:25 +02:00
Gerard Marull-Paretas 0ebe14beb4 sys: util: migrate all files to DIV_ROUND_UP
ceiling_fraction is deprecated, use DIV_ROUND_UP.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-04-11 12:00:37 +02:00
Henrik Brix Andersen 4f0cfdda80 drivers: can: document return value of 0 for can_get_max_bitrate()
Explicitly document that a return value of 0 from can_get_max_bitrate()
indicates success.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-03-31 16:22:16 +00:00
Henrik Brix Andersen d5672c9ee3 drivers: can: check timing parameters
Check the requested CAN timing parameters against the min/max values
supported by the driver and return an error if they are out of range.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-03-23 09:40:34 +01:00
Henrik Brix Andersen 3c89bbf876 drivers: can: reset statistics when starting the CAN controller
Reset CAN controller statistics when starting the CAN controller.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-03-20 09:52:50 +01:00
Henrik Brix Andersen b24dca032e drivers: can: add CAN-FD Error State Indicator (ESI) flag
Add definition for the CAN-FD Error State Indicator (ESI) flag. The
presence of this flag indicates that the transmitting CAN-FD node is in
error-passive state.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-03-11 19:45:14 +01:00
Henrik Brix Andersen 9ca4968b21 drivers: can: clarify the documentation on overlapping filters
Attempt to clarify the documentation on the behaviour of overlapping CAN RX
filters.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-03-11 19:45:07 +01: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
Henrik Brix Andersen c5a4469cc3 drivers: can: add RX overrun counter statistic
The RX overrun counter is incremented when the CAN controller receives a
CAN frame matching an installed filter but lacks the capacity to store it
(either due to an already full RX mailbox or a full RX FIFO).

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-01-19 16:51:22 -08:00
Cong Nguyen Huu d167d3a29a driver: can: add new filter to match CAN-FD frames
Add support FD frame filter to configure type frame for
each Rx msg to receive corresponding frames (classic, FD frames).

The Bosch M_CAN driver does not support FD frame filter,
so inmplement driver to handle it in software.

Signed-off-by: Cong Nguyen Huu <cong.nguyenhuu@nxp.com>
2023-01-04 08:18:13 -06:00
Henrik Brix Andersen 1e496a1ebc drivers: can: document can_add_rx_filter_msgq() overrun behavior
Document the behavior of can_add_rx_filter_msgq() on message queue overrun.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-01-04 10:33:57 +01: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 f393e2205c drivers: can: mark CAN-FD data phase timing API as optional
The can_set_timing_data() API function is optional and is not supported by
all CAN controller drivers.

On a board with only either classic CAN or CAN-FD capable controllers the
current behavior is fine, but on boards with a mix of classic CAN and
CAN-FD capable controllers/drivers, calling can_set_timing_data() on a
classic CAN controller will lead to a NULL pointer dereference.

To ensure a proper behavior when calling can_set_timing_data() on classic
CAN controller drivers, change the API wrapper function to return -ENOSYS
if the CAN driver does not implement CAN-FD support.

Note that the data phase timing calculation functions are fine as-is, as
the timing calculations will fail due to minimum/maximum data phase timing
parameters being all zeros for drivers only supporting classic CAN.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-10-12 18:49:33 +09:00
Henrik Brix Andersen dad8364d5f drivers: can: document which API functions are optional
Document that the can_get_max_filters() and can_get_max_bitrate() API
functions are optional.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-10-12 18:49:33 +09:00
Gerard Marull-Paretas ac63bca423 include: add missing sys_clock.h include
Some headers made use of types defined in sys_clock.h (e.g. k_timeout_t)
without including it.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-11 18:05:17 +02:00
Gerard Marull-Paretas 6a0f554ffa include: add missing kernel.h include
Some files make use of Kernel APIs without including kernel.h, fix this
problem.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-11 18:05:17 +02:00
Gerard Marull-Paretas e63b0bb3fe include: add missing errno.h include
With the incoming removal of kernel.h/types.h from init.h, lots of files
start to show compile errors because they relied on indirect
definitions, including errno.h.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-11 18:05:17 +02:00