Commit Graph

390 Commits

Author SHA1 Message Date
Johan Hedberg 54513c70d1 drivers: can: kvaser_pci: Fix incorrect RAM context struct type
The can_kvaser_pci_data_##inst type was supposed to be
can_kvaser_pci_data and not can_kvaser_pci_config. This got unnoticed
because there were no struct members to pre-initialize, and a reference
to the struct was stored using a void pointer.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2022-11-11 12:10:09 +01:00
Perry Hung 9b5bc03f86 drivers: can: sam: select CACHE_MANAGEMENT for SAM M_CAN
A regression was introduced in ef804d8408
when the M_CAN driver was swapped to use the generic dcache API, but
cache management was not enabled on SAM devices.

Enable cache management on SAM devices when CAN_SAM is selected.

Signed-off-by: Perry Hung <perry@genrad.com>
2022-11-08 15:41:10 -05:00
Henrik Brix Andersen f30a5969d0 drivers: can: make the fake CAN driver generally available
Make the fake CAN controller driver available for use in tests outside of
the CAN shell test.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-11-02 08:19:33 -07:00
Henrik Brix Andersen 566c64f0c0 drivers: can: loopback: guard against out-of-bounds filter IDs
Guard against out-of-bounds filter IDs when removing RX filters.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-11-02 10:31:48 +01:00
Henrik Brix Andersen efd0d3ba96 drivers: can: loopback: add log message for full TX msgq
Emit a debug log message if the TX message queue is full.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-11-02 10:31:48 +01:00
Henrik Brix Andersen 728138023b drivers: can: loopback: rename receive function
Rename the CAN loopback driver receive function to make the code easier to
understand. Add proper check against NULL for RX callback function
pointers.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-11-02 10:31:48 +01:00
Henrik Brix Andersen a5e534a283 drivers: can: loopback: process TX callbacks in non-loopback mode
The TX callbacks must be processed regardless of the device being in
loopback mode or not.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-11-02 10:31:48 +01:00
Henrik Brix Andersen abf82013ac drivers: can: add support for configuring CAN emulation in QEMU
Add support for configuring CAN emulation support in QEMU. For now, the
only supported CAN controller is the single-channel Kvaser PCIcan PCI card.

Signed-off-by: Henrik Brix Andersen <henrik@brixandersen.dk>
2022-11-01 15:22:31 -04:00
Henrik Brix Andersen 58e12726bf drivers: can: add driver for the Kvaser PCIcan CAN controller PCI card
Add driver for the Kvaser PCIcan CAN controller PCI card for use with QEMU.

Signed-off-by: Henrik Brix Andersen <henrik@brixandersen.dk>
2022-11-01 15:22:31 -04: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 fa54e3aa1d drivers: can: sja1000: remove unused variables
Remove unused variables from the CAN SJA1000 driver.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-10-22 14:37:18 +09:00
Henrik Brix Andersen 5f8983f2ec drivers: can: npl: ensure CANFD_FDF flag is defined
Linux kernels before v5.14 do not define the CANFD_FDF flag. The flag is
disregarded within the Linux kernel. It is just there for being able to
reuse the canfd_frame struct for classic CAN frames in Linux user-space
applications.

Define the CANFD_FDF flag locally if not already defined to allow compiling
against older Linux kernel headers.

See the following Linux kernel commit for further details:
02546884221279da2725e87e35348290470363d7

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-10-17 17:04:16 +02:00
Gerard Marull-Paretas 178bdc4afc include: add missing zephyr/irq.h include
Change automated searching for files using "IRQ_CONNECT()" API not
including <zephyr/irq.h>.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-17 22:57:39 +09:00
Martin Jäger 93681feaba drivers: can: sja1000: configure PHY from devicetree
The phys property was not considered during initialization of the
driver config, so that the transceiver was not enabled properly
in case it has an enable/standby pin.

Signed-off-by: Martin Jäger <martin@libre.solar>
2022-10-14 14:24:00 +02:00
Martin Jäger 2a035f775b drivers: can: esp32_twai: add support for newer MCUs
Newer ESP32 series MCUs like the ESP32-C3 contain some register changes
incompatible to the original ESP32 and the SJA1000.

The additions in this commit consider these changes and fix the
incompatibilities in the TWAI front-end for the SJA1000 driver.

Signed-off-by: Martin Jäger <martin@libre.solar>
2022-10-14 09:55:09 +02:00
Martin Jäger f936d68916 drivers: can: sja1000: use generic can_set_timing API
This is a preparation to use the custom implementation in ESP32 TWAI
frontend for newer ESP32 series chips like the ESP32-C3, which have
timing registers incompatible with the SJA1000.

Signed-off-by: Martin Jäger <martin@libre.solar>
2022-10-14 09:55:09 +02:00
Thad House f37db90541 drivers: Correct tx and rx error counts in mcan
TX and RX error counts are swapped in the mcan driver.

Signed-off-by: Thad House <thadhouse@fb.com>
2022-09-28 20:34:36 +00: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 f239c3d0e9 drivers: can: stm32: abort transfers after entering init mode
Abort any pending transmissions after entering init mode and notify the
senders.

Fixes: #50545

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-09-23 17:25:01 +00:00
Henrik Brix Andersen 6cf34d0d33 drivers: can: mcan: notify of aborted transmission on can_stop()
Notify of any aborted transmission when entering init mode via can_stop().

Fixes: #50545

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-09-23 17:25:01 +00:00
Henrik Brix Andersen b8eb11de3b drivers: can: mcux: flexcan: abort transfers before entering freeze mode
Abort any pending transmissions before entering freeze mode and notify the
senders.

Fixes: #50545

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-09-23 17:25:01 +00:00
Henrik Brix Andersen 94d4638ccc drivers: can: sja1000: notify of aborted transmission on can_stop()
Notify of any aborted transmission when entering reset mode via can_stop().

Fixes: #50545

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-09-23 17:25:01 +00:00
Henrik Brix Andersen a99a55eefb drivers: can: npl: 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 50d77ab854 drivers: can: mcan: 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.

This saves approximately 500 bytes of SRAM on the lpcxpressso55s16 board
when running in default configuration.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-09-23 17:25:01 +00:00
Henrik Brix Andersen d3427d991a drivers: can: rcar: 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 10cd28a61a drivers: can: loopback: 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 4165186d13 drivers: can: stm32: 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 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 eb06e6aed0 drivers: can: sja1000: 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 fcf7c6e6d1 drivers: can: mcux: flexcan: 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.

This saves approximately 500 bytes of SRAM on the frdm_k64f board, while on
the mimxrt1024_evk board the saving is approximately 2.5 kbytes of SRAM,
both when running in default configuration.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-09-23 17:25:01 +00:00
Henrik Brix Andersen d6fc3f6596 drivers: can: provide default callback to can_send() if NULL
Provide a default, internal callback to CAN controller drivers
implementation of can_send() if no callback was provided by the caller.

This allows for simplifying the CAN driver implementations of can_send() as
these no longer need special handling for callback/no callback operation.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-09-23 17:25:01 +00:00
Henrik Brix Andersen 5ec7acca86 drivers: can: rcar: remote transmission request filters are not supported
The receive path for the Renesas R-Car CAN controller supports two RX
FIFOs, which are currently configured for standard 11-bit CAN ID data
frames in FIFO0, and extended 29-bit CAN ID data frames in FIFO1.

As this leaves no room for matching remote transmission request (RTR)
frames, have the driver return -ENOTSUP instead of silently accepting RTR
filters.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-09-21 08:50:47 +00:00
Thomas Stranger fc33325fc7 drivers: can: native_posix_linux: drop rx frames while stopped
The driver should drop rx frames while in stopped state.

Signed-off-by: Thomas Stranger <thomas.stranger@outlook.com>
2022-09-15 16:20:04 +00:00
Thomas Stranger 0c365ac0ea drivers: can: mcan: don't enabled trasceiver in init
With the move to start/stop functions the transceiver is not expected
to be enabled after driver initialization.

Signed-off-by: Thomas Stranger <thomas.stranger@outlook.com>
2022-09-15 16:19:32 +00:00
Henrik Brix Andersen 6f1ff5ea83 drivers: can: shell: add CAN start/stop shell commands
Add shell commands for starting/stopping a CAN controller.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-09-13 16:06:50 +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
Martin Jäger 77e4c6515a drivers: can: native_posix_linux: initial implementation
This driver provides an interface to SocketCAN interfaces of the Linux
system running a Zephyr application with the native_posix board. These
interfaces may be virtual or actual CAN buses.

Signed-off-by: Martin Jäger <martin@libre.solar>
2022-09-09 10:08:59 +02:00
Henrik Brix Andersen b01ea7c0c9 drivers: can: transceiver: Update drivers to use dts Kconfig symbol
Update the GPIO-controlled CAN transceiver driver to use the
DT_HAS_CAN_TRANSCEIVER_GPIO_ENABLED Kconfig symbol to expose the driver and
enable it by default based on devicetree.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-09-08 15:30:23 +00:00
Thomas Stranger 402752c07c drivers/can: move STM32FD clock divider configuration to dts
Remove the CAN_STM32FD_CLOCK_DIVISOR configuration option,
and add configuration via dts property clk-divider instead.

Signed-off-by: Thomas Stranger <thomas.stranger@outlook.com>
2022-09-05 14:44:45 +00:00
Thomas Stranger 09f45840f1 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 <thomas.stranger@outlook.com>
2022-09-05 14:44:45 +00:00
Thomas Stranger a5cc6c2e16 drivers/can: stm32_fd: convert domain clk src sel. from kconfig to dts
Use domain clocks if they are defined in dts.

Until now domain clock sources could be selected via kconfig.
STM32 platform now can configure domain clock sources via
clock control driver, therefore this commit makes use of it.

The configuration is shared between canfd instances, so a domain clock
has to be defined for only one instance. Otherwise, only the
configuration from the latest initialized instance will remain.

The dependency on clock source PCLK1 for CAN_STM32FD_CLOCK_DIVISOR
was removed, because the divider also divides other clocks.
Note that setting that divider does not work at all at the moment,
because the write protection is not disabled.

Signed-off-by: Thomas Stranger <thomas.stranger@outlook.com>
2022-09-05 14:44:45 +00:00
Gerard Marull-Paretas 79e6b0e0f6 includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.

The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.

NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-09-05 16:31:47 +02:00
Henrik Brix Andersen b0c1b9715f drivers: can: shell: add command for showing CAN controller information
Add a "can show <device>" command for showing information about a CAN
controller.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-08-31 10:45:35 +02:00
Henrik Brix Andersen 2f73225aa8 drivers: can: rewrite the CAN shell module
Rewrite the CAN shell module to catch up with the added CAN controller API
functionality.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-08-31 10:45:35 +02:00
Gerard Marull-Paretas a202341958 devices: constify device pointers initialized at compile time
Many device pointers are initialized at compile and never changed. This
means that the device pointer can be constified (immutable).

Automated using:

```
perl -i -pe 's/const struct device \*(?!const)(.*)= DEVICE/const struct
device *const $1= DEVICE/g' **/*.c
```

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-22 17:08:26 +02:00
Henrik Brix Andersen fda3f54002 drivers: can: return -ENETUNREACH from can_send() if in bus off state
Change the error code for can_send() when the CAN controller is in bus off
state from -ENETDOWN to -ENETUNREACH.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-08-22 14:33:17 +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 7e3814b262 drivers: can: expand return values reported by the CAN timing functions
Expand the error return values reported by the CAN timing calculation
functions to be able to distinguish between an an out of range
bitrate/sample point, an unsupported bitrate, and a resulting sample point
outside the guard limit.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-08-18 21:39:16 +00:00
Henrik Brix Andersen 2b99b05d0a drivers: can: move bitrate limit checks to arbitration/data functions
Move the check for the maximum allowed CAN bitrate to the corresponding
can_calc_timing()/can_calc_timing_data() function.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2022-08-18 21:39:16 +00:00