Zephyr API Documentation 3.7.99
A Scalable Open Source RTOS
|
CAN Interface . More...
Data Structures | |
struct | can_frame |
CAN frame structure. More... | |
struct | can_filter |
CAN filter structure. More... | |
struct | can_bus_err_cnt |
CAN controller error counters. More... | |
struct | can_timing |
CAN bus timing structure. More... | |
struct | can_device_state |
CAN specific device state which allows for CAN device class specific additions. More... | |
Macros | |
#define | CAN_STATS_BIT_ERROR_INC(dev_) |
Increment the bit error counter for a CAN device. | |
#define | CAN_STATS_BIT0_ERROR_INC(dev_) |
Increment the bit0 error counter for a CAN device. | |
#define | CAN_STATS_BIT1_ERROR_INC(dev_) |
Increment the bit1 (recessive) error counter for a CAN device. | |
#define | CAN_STATS_STUFF_ERROR_INC(dev_) |
Increment the stuffing error counter for a CAN device. | |
#define | CAN_STATS_CRC_ERROR_INC(dev_) |
Increment the CRC error counter for a CAN device. | |
#define | CAN_STATS_FORM_ERROR_INC(dev_) |
Increment the form error counter for a CAN device. | |
#define | CAN_STATS_ACK_ERROR_INC(dev_) |
Increment the acknowledge error counter for a CAN device. | |
#define | CAN_STATS_RX_OVERRUN_INC(dev_) |
Increment the RX overrun counter for a CAN device. | |
#define | CAN_STATS_RESET(dev_) |
Zero all statistics for a CAN device. | |
#define | CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, prio, api, ...) |
Like DEVICE_DT_DEFINE() with CAN device specifics. | |
#define | CAN_DEVICE_DT_INST_DEFINE(inst, ...) |
Like CAN_DEVICE_DT_DEFINE() for an instance of a DT_DRV_COMPAT compatible. | |
Typedefs | |
typedef uint32_t | can_mode_t |
Provides a type to hold CAN controller configuration flags. | |
typedef void(* | can_tx_callback_t) (const struct device *dev, int error, void *user_data) |
Defines the application callback handler function signature. | |
typedef void(* | can_rx_callback_t) (const struct device *dev, struct can_frame *frame, void *user_data) |
Defines the application callback handler function signature for receiving. | |
typedef void(* | can_state_change_callback_t) (const struct device *dev, enum can_state state, struct can_bus_err_cnt err_cnt, void *user_data) |
Defines the state change callback handler function signature. | |
Enumerations | |
enum | can_state { CAN_STATE_ERROR_ACTIVE , CAN_STATE_ERROR_WARNING , CAN_STATE_ERROR_PASSIVE , CAN_STATE_BUS_OFF , CAN_STATE_STOPPED } |
Defines the state of the CAN controller. More... | |
CAN controller configuration | |
int | can_get_core_clock (const struct device *dev, uint32_t *rate) |
Get the CAN core clock rate. | |
uint32_t | can_get_bitrate_min (const struct device *dev) |
Get minimum supported bitrate. | |
static int | can_get_min_bitrate (const struct device *dev, uint32_t *min_bitrate) |
Get minimum supported bitrate. | |
uint32_t | can_get_bitrate_max (const struct device *dev) |
Get maximum supported bitrate. | |
static int | can_get_max_bitrate (const struct device *dev, uint32_t *max_bitrate) |
Get maximum supported bitrate. | |
const struct can_timing * | can_get_timing_min (const struct device *dev) |
Get the minimum supported timing parameter values. | |
const struct can_timing * | can_get_timing_max (const struct device *dev) |
Get the maximum supported timing parameter values. | |
int | can_calc_timing (const struct device *dev, struct can_timing *res, uint32_t bitrate, uint16_t sample_pnt) |
Calculate timing parameters from bitrate and sample point. | |
const struct can_timing * | can_get_timing_data_min (const struct device *dev) |
Get the minimum supported timing parameter values for the data phase. | |
const struct can_timing * | can_get_timing_data_max (const struct device *dev) |
Get the maximum supported timing parameter values for the data phase. | |
int | can_calc_timing_data (const struct device *dev, struct can_timing *res, uint32_t bitrate, uint16_t sample_pnt) |
Calculate timing parameters for the data phase. | |
int | can_set_timing_data (const struct device *dev, const struct can_timing *timing_data) |
Configure the bus timing for the data phase of a CAN FD controller. | |
int | can_set_bitrate_data (const struct device *dev, uint32_t bitrate_data) |
Set the bitrate for the data phase of the CAN FD controller. | |
int | can_calc_prescaler (const struct device *dev, struct can_timing *timing, uint32_t bitrate) |
Fill in the prescaler value for a given bitrate and timing. | |
int | can_set_timing (const struct device *dev, const struct can_timing *timing) |
Configure the bus timing of a CAN controller. | |
int | can_get_capabilities (const struct device *dev, can_mode_t *cap) |
Get the supported modes of the CAN controller. | |
const struct device * | can_get_transceiver (const struct device *dev) |
Get the CAN transceiver associated with the CAN controller. | |
int | can_start (const struct device *dev) |
Start the CAN controller. | |
int | can_stop (const struct device *dev) |
Stop the CAN controller. | |
int | can_set_mode (const struct device *dev, can_mode_t mode) |
Set the CAN controller to the given operation mode. | |
can_mode_t | can_get_mode (const struct device *dev) |
Get the operation mode of the CAN controller. | |
int | can_set_bitrate (const struct device *dev, uint32_t bitrate) |
Set the bitrate of the CAN controller. | |
Transmitting CAN frames | |
int | can_send (const struct device *dev, const struct can_frame *frame, k_timeout_t timeout, can_tx_callback_t callback, void *user_data) |
Queue a CAN frame for transmission on the CAN bus. | |
Receiving CAN frames | |
int | can_add_rx_filter (const struct device *dev, can_rx_callback_t callback, void *user_data, const struct can_filter *filter) |
Add a callback function for a given CAN filter. | |
int | can_add_rx_filter_msgq (const struct device *dev, struct k_msgq *msgq, const struct can_filter *filter) |
Simple wrapper function for adding a message queue for a given filter. | |
void | can_remove_rx_filter (const struct device *dev, int filter_id) |
Remove a CAN RX filter. | |
int | can_get_max_filters (const struct device *dev, bool ide) |
Get maximum number of RX filters. | |
#define | CAN_MSGQ_DEFINE(name, max_frames) |
Statically define and initialize a CAN RX message queue. | |
CAN bus error reporting and handling | |
int | can_get_state (const struct device *dev, enum can_state *state, struct can_bus_err_cnt *err_cnt) |
Get current CAN controller state. | |
int | can_recover (const struct device *dev, k_timeout_t timeout) |
Recover from bus-off state. | |
static void | can_set_state_change_callback (const struct device *dev, can_state_change_callback_t callback, void *user_data) |
Set a callback for CAN controller state change events. | |
CAN statistics | |
uint32_t | can_stats_get_bit_errors (const struct device *dev) |
Get the bit error counter for a CAN device. | |
uint32_t | can_stats_get_bit0_errors (const struct device *dev) |
Get the bit0 error counter for a CAN device. | |
uint32_t | can_stats_get_bit1_errors (const struct device *dev) |
Get the bit1 error counter for a CAN device. | |
uint32_t | can_stats_get_stuff_errors (const struct device *dev) |
Get the stuffing error counter for a CAN device. | |
uint32_t | can_stats_get_crc_errors (const struct device *dev) |
Get the CRC error counter for a CAN device. | |
uint32_t | can_stats_get_form_errors (const struct device *dev) |
Get the form error counter for a CAN device. | |
uint32_t | can_stats_get_ack_errors (const struct device *dev) |
Get the acknowledge error counter for a CAN device. | |
uint32_t | can_stats_get_rx_overruns (const struct device *dev) |
Get the RX overrun counter for a CAN device. | |
CAN utility functions | |
static uint8_t | can_dlc_to_bytes (uint8_t dlc) |
Convert from Data Length Code (DLC) to the number of data bytes. | |
static uint8_t | can_bytes_to_dlc (uint8_t num_bytes) |
Convert from number of bytes to Data Length Code (DLC) | |
static bool | can_frame_matches_filter (const struct can_frame *frame, const struct can_filter *filter) |
Check if a CAN frame matches a CAN filter. | |
CAN frame definitions | |
#define | CAN_STD_ID_MASK 0x7FFU |
Bit mask for a standard (11-bit) CAN identifier. | |
#define | CAN_MAX_STD_ID CAN_STD_ID_MASK __DEPRECATED_MACRO |
Maximum value for a standard (11-bit) CAN identifier. | |
#define | CAN_EXT_ID_MASK 0x1FFFFFFFU |
Bit mask for an extended (29-bit) CAN identifier. | |
#define | CAN_MAX_EXT_ID CAN_EXT_ID_MASK __DEPRECATED_MACRO |
Maximum value for an extended (29-bit) CAN identifier. | |
#define | CAN_MAX_DLC 8U |
Maximum data length code for CAN 2.0A/2.0B. | |
#define | CANFD_MAX_DLC 15U |
Maximum data length code for CAN FD. | |
CAN controller mode flags | |
#define | CAN_MODE_NORMAL 0 |
Normal mode. | |
#define | CAN_MODE_LOOPBACK BIT(0) |
Controller is in loopback mode (receives own frames). | |
#define | CAN_MODE_LISTENONLY BIT(1) |
Controller is not allowed to send dominant bits. | |
#define | CAN_MODE_FD BIT(2) |
Controller allows transmitting/receiving CAN FD frames. | |
#define | CAN_MODE_ONE_SHOT BIT(3) |
Controller does not retransmit in case of lost arbitration or missing ACK. | |
#define | CAN_MODE_3_SAMPLES BIT(4) |
Controller uses triple sampling mode. | |
#define | CAN_MODE_MANUAL_RECOVERY BIT(5) |
Controller requires manual recovery after entering bus-off state. | |
CAN frame flags | |
#define | CAN_FRAME_IDE BIT(0) |
Frame uses extended (29-bit) CAN ID. | |
#define | CAN_FRAME_RTR BIT(1) |
Frame is a Remote Transmission Request (RTR) | |
#define | CAN_FRAME_FDF BIT(2) |
Frame uses CAN FD format (FDF) | |
#define | CAN_FRAME_BRS BIT(3) |
Frame uses CAN FD Baud Rate Switch (BRS). | |
#define | CAN_FRAME_ESI BIT(4) |
CAN FD Error State Indicator (ESI). | |
CAN filter flags | |
#define | CAN_FILTER_IDE BIT(0) |
Filter matches frames with extended (29-bit) CAN IDs. | |
CAN Interface .
#define CAN_DEVICE_DT_DEFINE | ( | node_id, | |
init_fn, | |||
pm, | |||
data, | |||
config, | |||
level, | |||
prio, | |||
api, | |||
... ) |
#include <zephyr/drivers/can.h>
Like DEVICE_DT_DEFINE() with CAN device specifics.
Defines a device which implements the CAN API. May generate a custom device_state container struct and init_fn wrapper when needed depending on CONFIG_CAN_STATS
.
node_id | The devicetree node identifier. |
init_fn | Name of the init function of the driver. |
pm | PM device resources reference (NULL if device does not use PM). |
data | Pointer to the device's private data. |
config | The address to the structure containing the configuration information for this instance of the driver. |
level | The initialization level. See SYS_INIT() for details. |
prio | Priority within the selected initialization level. See SYS_INIT() for details. |
api | Provides an initial pointer to the API function struct used by the driver. Can be NULL. |
#define CAN_DEVICE_DT_INST_DEFINE | ( | inst, | |
... ) |
#include <zephyr/drivers/can.h>
Like CAN_DEVICE_DT_DEFINE() for an instance of a DT_DRV_COMPAT compatible.
inst | Instance number. This is replaced by DT_DRV_COMPAT(inst) in the call to CAN_DEVICE_DT_DEFINE(). |
... | Other parameters as expected by CAN_DEVICE_DT_DEFINE(). |
#define CAN_EXT_ID_MASK 0x1FFFFFFFU |
#include <zephyr/drivers/can.h>
Bit mask for an extended (29-bit) CAN identifier.
#define CAN_FILTER_IDE BIT(0) |
#include <zephyr/drivers/can.h>
Filter matches frames with extended (29-bit) CAN IDs.
#define CAN_FRAME_BRS BIT(3) |
#include <zephyr/drivers/can.h>
Frame uses CAN FD Baud Rate Switch (BRS).
Only valid in combination with CAN_FRAME_FDF
.
#define CAN_FRAME_ESI BIT(4) |
#include <zephyr/drivers/can.h>
CAN FD Error State Indicator (ESI).
Indicates that the transmitting node is in error-passive state. Only valid in combination with CAN_FRAME_FDF
.
#define CAN_FRAME_FDF BIT(2) |
#include <zephyr/drivers/can.h>
Frame uses CAN FD format (FDF)
#define CAN_FRAME_IDE BIT(0) |
#include <zephyr/drivers/can.h>
Frame uses extended (29-bit) CAN ID.
#define CAN_FRAME_RTR BIT(1) |
#include <zephyr/drivers/can.h>
Frame is a Remote Transmission Request (RTR)
#define CAN_MAX_DLC 8U |
#include <zephyr/drivers/can.h>
Maximum data length code for CAN 2.0A/2.0B.
#define CAN_MAX_EXT_ID CAN_EXT_ID_MASK __DEPRECATED_MACRO |
#include <zephyr/drivers/can.h>
Maximum value for an extended (29-bit) CAN identifier.
CAN_EXT_ID_MASK
instead. #define CAN_MAX_STD_ID CAN_STD_ID_MASK __DEPRECATED_MACRO |
#include <zephyr/drivers/can.h>
Maximum value for a standard (11-bit) CAN identifier.
CAN_STD_ID_MASK
instead. #define CAN_MODE_3_SAMPLES BIT(4) |
#include <zephyr/drivers/can.h>
Controller uses triple sampling mode.
#define CAN_MODE_FD BIT(2) |
#include <zephyr/drivers/can.h>
Controller allows transmitting/receiving CAN FD frames.
#define CAN_MODE_LISTENONLY BIT(1) |
#include <zephyr/drivers/can.h>
Controller is not allowed to send dominant bits.
#define CAN_MODE_LOOPBACK BIT(0) |
#include <zephyr/drivers/can.h>
Controller is in loopback mode (receives own frames).
#define CAN_MODE_MANUAL_RECOVERY BIT(5) |
#include <zephyr/drivers/can.h>
Controller requires manual recovery after entering bus-off state.
#define CAN_MODE_NORMAL 0 |
#include <zephyr/drivers/can.h>
Normal mode.
#define CAN_MODE_ONE_SHOT BIT(3) |
#include <zephyr/drivers/can.h>
Controller does not retransmit in case of lost arbitration or missing ACK.
#define CAN_MSGQ_DEFINE | ( | name, | |
max_frames ) |
#include <zephyr/drivers/can.h>
Statically define and initialize a CAN RX message queue.
The message queue's ring buffer contains space for max_frames CAN frames.
name | Name of the message queue. |
max_frames | Maximum number of CAN frames that can be queued. |
#define CAN_STATS_ACK_ERROR_INC | ( | dev_ | ) |
#include <zephyr/drivers/can.h>
Increment the acknowledge error counter for a CAN device.
The acknowledge error counter is incremented when the CAN controller does not monitor a dominant bit in the ACK slot.
dev_ | Pointer to the device structure for the driver instance. |
#define CAN_STATS_BIT0_ERROR_INC | ( | dev_ | ) |
#include <zephyr/drivers/can.h>
Increment the bit0 error counter for a CAN device.
The bit0 error counter is incremented when the CAN controller is unable to transmit a dominant bit.
Incrementing this counter will automatically increment the bit error counter.
dev_ | Pointer to the device structure for the driver instance. |
#define CAN_STATS_BIT1_ERROR_INC | ( | dev_ | ) |
#include <zephyr/drivers/can.h>
Increment the bit1 (recessive) error counter for a CAN device.
The bit1 error counter is incremented when the CAN controller is unable to transmit a recessive bit.
Incrementing this counter will automatically increment the bit error counter.
dev_ | Pointer to the device structure for the driver instance. |
#define CAN_STATS_BIT_ERROR_INC | ( | dev_ | ) |
#include <zephyr/drivers/can.h>
Increment the bit error counter for a CAN device.
The bit error counter is incremented when the CAN controller is unable to transmit either a dominant or a recessive bit.
bit0
or bit1
error counter shall be incremented instead.dev_ | Pointer to the device structure for the driver instance. |
#define CAN_STATS_CRC_ERROR_INC | ( | dev_ | ) |
#include <zephyr/drivers/can.h>
Increment the CRC error counter for a CAN device.
The CRC error counter is incremented when the CAN controller detects a frame with an invalid CRC.
dev_ | Pointer to the device structure for the driver instance. |
#define CAN_STATS_FORM_ERROR_INC | ( | dev_ | ) |
#include <zephyr/drivers/can.h>
Increment the form error counter for a CAN device.
The form error counter is incremented when the CAN controller detects a fixed-form bit field containing illegal bits.
dev_ | Pointer to the device structure for the driver instance. |
#define CAN_STATS_RESET | ( | dev_ | ) |
#include <zephyr/drivers/can.h>
Zero all statistics for a CAN device.
The driver is responsible for resetting the statistics before starting the CAN controller.
dev_ | Pointer to the device structure for the driver instance. |
#define CAN_STATS_RX_OVERRUN_INC | ( | dev_ | ) |
#include <zephyr/drivers/can.h>
Increment the RX overrun counter for a CAN device.
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).
dev_ | Pointer to the device structure for the driver instance. |
#define CAN_STATS_STUFF_ERROR_INC | ( | dev_ | ) |
#include <zephyr/drivers/can.h>
Increment the stuffing error counter for a CAN device.
The stuffing error counter is incremented when the CAN controller detects a bit stuffing error.
dev_ | Pointer to the device structure for the driver instance. |
#define CAN_STD_ID_MASK 0x7FFU |
#include <zephyr/drivers/can.h>
Bit mask for a standard (11-bit) CAN identifier.
#define CANFD_MAX_DLC 15U |
#include <zephyr/drivers/can.h>
Maximum data length code for CAN FD.
typedef uint32_t can_mode_t |
#include <zephyr/drivers/can.h>
Provides a type to hold CAN controller configuration flags.
The lower 24 bits are reserved for common CAN controller mode flags. The upper 8 bits are reserved for CAN controller/driver specific flags.
typedef void(* can_rx_callback_t) (const struct device *dev, struct can_frame *frame, void *user_data) |
#include <zephyr/drivers/can.h>
Defines the application callback handler function signature for receiving.
dev | Pointer to the device structure for the driver instance. |
frame | Received frame. |
user_data | User data provided when the filter was added. |
typedef void(* can_state_change_callback_t) (const struct device *dev, enum can_state state, struct can_bus_err_cnt err_cnt, void *user_data) |
#include <zephyr/drivers/can.h>
Defines the state change callback handler function signature.
dev | Pointer to the device structure for the driver instance. |
state | State of the CAN controller. |
err_cnt | CAN controller error counter values. |
user_data | User data provided the callback was set. |
typedef void(* can_tx_callback_t) (const struct device *dev, int error, void *user_data) |
#include <zephyr/drivers/can.h>
Defines the application callback handler function signature.
dev | Pointer to the device structure for the driver instance. |
error | Status of the performed send operation. See the list of return values for can_send() for value descriptions. |
user_data | User data provided when the frame was sent. |
enum can_state |
#include <zephyr/drivers/can.h>
Defines the state of the CAN controller.
int can_add_rx_filter | ( | const struct device * | dev, |
can_rx_callback_t | callback, | ||
void * | user_data, | ||
const struct can_filter * | filter ) |
#include <zephyr/drivers/can.h>
Add a callback function for a given CAN filter.
Add a callback to CAN identifiers specified by a filter. When a received CAN frame matching the filter is received by the CAN controller, the callback function is called in interrupt context.
If a received frame matches more than one filter (i.e., the filter IDs/masks or flags overlap), the priority of the match is hardware dependent.
The same callback function can be used for multiple filters.
dev | Pointer to the device structure for the driver instance. |
callback | This function is called by the CAN controller driver whenever a frame matching the filter is received. |
user_data | User data to pass to callback function. |
filter | Pointer to a can_filter structure defining the filter. |
filter_id | on success. |
-ENOSPC | if there are no free filters. |
-EINVAL | if the requested filter type is invalid. |
-ENOTSUP | if the requested filter type is not supported. |
int can_add_rx_filter_msgq | ( | const struct device * | dev, |
struct k_msgq * | msgq, | ||
const struct can_filter * | filter ) |
#include <zephyr/drivers/can.h>
Simple wrapper function for adding a message queue for a given filter.
Wrapper function for can_add_rx_filter() which puts received CAN frames matching the filter in a message queue instead of calling a callback.
If a received frame matches more than one filter (i.e., the filter IDs/masks or flags overlap), the priority of the match is hardware dependent.
The same message queue can be used for multiple filters.
dev | Pointer to the device structure for the driver instance. |
msgq | Pointer to the already initialized k_msgq struct. |
filter | Pointer to a can_filter structure defining the filter. |
filter_id | on success. |
-ENOSPC | if there are no free filters. |
-ENOTSUP | if the requested filter type is not supported. |
#include <zephyr/drivers/can.h>
Convert from number of bytes to Data Length Code (DLC)
num_bytes | Number of bytes. |
Data | Length Code (DLC). |
int can_calc_prescaler | ( | const struct device * | dev, |
struct can_timing * | timing, | ||
uint32_t | bitrate ) |
#include <zephyr/drivers/can.h>
Fill in the prescaler value for a given bitrate and timing.
Fill the prescaler value in the timing struct. The sjw, prop_seg, phase_seg1 and phase_seg2 must be given.
The returned bitrate error is remainder of the division of the clock rate by the bitrate times the timing segments.
dev | Pointer to the device structure for the driver instance. |
timing | Result is written into the can_timing struct provided. |
bitrate | Target bitrate. |
0 | or positive bitrate error. |
Negative | error code on error. |
int can_calc_timing | ( | const struct device * | dev, |
struct can_timing * | res, | ||
uint32_t | bitrate, | ||
uint16_t | sample_pnt ) |
#include <zephyr/drivers/can.h>
Calculate timing parameters from bitrate and sample point.
Calculate the timing parameters from a given bitrate in bits/s and the sampling point in permill (1/1000) of the entire bit time. The bitrate must always match perfectly. If no result can be reached for the given parameters, -EINVAL is returned.
If the sample point is set to 0, this function defaults to a sample point of 75.0% for bitrates over 800 kbit/s, 80.0% for bitrates over 500 kbit/s, and 87.5% for all other bitrates.
sample_pnt
will not always be matched perfectly. The algorithm calculates the best possible match.dev | Pointer to the device structure for the driver instance. | |
[out] | res | Result is written into the can_timing struct provided. |
bitrate | Target bitrate in bits/s. | |
sample_pnt | Sample point in permille of the entire bit time or 0 for automatic sample point location. |
0 | or positive sample point error on success. |
-EINVAL | if the requested bitrate or sample point is out of range. |
-ENOTSUP | if the requested bitrate is not supported. |
-EIO | if can_get_core_clock() is not available. |
int can_calc_timing_data | ( | const struct device * | dev, |
struct can_timing * | res, | ||
uint32_t | bitrate, | ||
uint16_t | sample_pnt ) |
#include <zephyr/drivers/can.h>
Calculate timing parameters for the data phase.
Same as can_calc_timing() but with the maximum and minimum values from the data phase.
CONFIG_CAN_FD_MODE
must be selected for this function to be available.dev | Pointer to the device structure for the driver instance. | |
[out] | res | Result is written into the can_timing struct provided. |
bitrate | Target bitrate for the data phase in bits/s | |
sample_pnt | Sample point for the data phase in permille of the entire bit time or 0 for automatic sample point location. |
0 | or positive sample point error on success. |
-EINVAL | if the requested bitrate or sample point is out of range. |
-ENOTSUP | if the requested bitrate is not supported. |
-EIO | if can_get_core_clock() is not available. |
#include <zephyr/drivers/can.h>
Convert from Data Length Code (DLC) to the number of data bytes.
dlc | Data Length Code (DLC). |
Number | of bytes. |
|
inlinestatic |
#include <zephyr/drivers/can.h>
Check if a CAN frame matches a CAN filter.
frame | CAN frame. |
filter | CAN filter. |
#include <zephyr/drivers/can.h>
Get maximum supported bitrate.
Get the maximum supported bitrate for the CAN controller/transceiver combination.
dev | Pointer to the device structure for the driver instance. |
#include <zephyr/drivers/can.h>
Get minimum supported bitrate.
Get the minimum supported bitrate for the CAN controller/transceiver combination.
dev | Pointer to the device structure for the driver instance. |
int can_get_capabilities | ( | const struct device * | dev, |
can_mode_t * | cap ) |
#include <zephyr/drivers/can.h>
Get the supported modes of the CAN controller.
The returned capabilities may not necessarily be supported at the same time (e.g. some CAN controllers support both CAN_MODE_LOOPBACK
and CAN_MODE_LISTENONLY
, but not at the same time).
dev | Pointer to the device structure for the driver instance. | |
[out] | cap | Supported capabilities. |
0 | If successful. |
-EIO | General input/output error, failed to get capabilities. |
#include <zephyr/drivers/can.h>
Get the CAN core clock rate.
Returns the CAN core clock rate. One minimum time quantum (mtq) is 1/(core clock rate). The CAN core clock can be further divided by the CAN clock prescaler (see the can_timing struct), providing the time quantum (tq).
dev | Pointer to the device structure for the driver instance. | |
[out] | rate | CAN core clock rate in Hz. |
#include <zephyr/drivers/can.h>
Get maximum supported bitrate.
Get the maximum supported bitrate for the CAN controller/transceiver combination.
dev | Pointer to the device structure for the driver instance. | |
[out] | max_bitrate | Maximum supported bitrate in bits/s |
0 | If successful. |
-EIO | General input/output error. |
-ENOSYS | If this function is not implemented by the driver. |
#include <zephyr/drivers/can.h>
Get maximum number of RX filters.
Get the maximum number of concurrent RX filters for the CAN controller.
dev | Pointer to the device structure for the driver instance. |
ide | Get the maximum standard (11-bit) CAN ID filters if false, or extended (29-bit) CAN ID filters if true. |
Positive | number of maximum concurrent filters. |
-EIO | General input/output error. |
-ENOSYS | If this function is not implemented by the driver. |
#include <zephyr/drivers/can.h>
Get minimum supported bitrate.
Get the minimum supported bitrate for the CAN controller/transceiver combination.
dev | Pointer to the device structure for the driver instance. | |
[out] | min_bitrate | Minimum supported bitrate in bits/s |
-EIO | General input/output error. |
-ENOSYS | If this function is not implemented by the driver. |
can_mode_t can_get_mode | ( | const struct device * | dev | ) |
#include <zephyr/drivers/can.h>
Get the operation mode of the CAN controller.
dev | Pointer to the device structure for the driver instance. |
int can_get_state | ( | const struct device * | dev, |
enum can_state * | state, | ||
struct can_bus_err_cnt * | err_cnt ) |
#include <zephyr/drivers/can.h>
Get current CAN controller state.
Returns the current state and optionally the error counter values of the CAN controller.
dev | Pointer to the device structure for the driver instance. | |
[out] | state | Pointer to the state destination enum or NULL. |
[out] | err_cnt | Pointer to the err_cnt destination structure or NULL. |
0 | If successful. |
-EIO | General input/output error, failed to get state. |
const struct can_timing * can_get_timing_data_max | ( | const struct device * | dev | ) |
#include <zephyr/drivers/can.h>
Get the maximum supported timing parameter values for the data phase.
Same as can_get_timing_max() but for the maximum values for the data phase.
CONFIG_CAN_FD_MODE
must be selected for this function to be available.dev | Pointer to the device structure for the driver instance. |
const struct can_timing * can_get_timing_data_min | ( | const struct device * | dev | ) |
#include <zephyr/drivers/can.h>
Get the minimum supported timing parameter values for the data phase.
Same as can_get_timing_min() but for the minimum values for the data phase.
CONFIG_CAN_FD_MODE
must be selected for this function to be available.dev | Pointer to the device structure for the driver instance. |
const struct can_timing * can_get_timing_max | ( | const struct device * | dev | ) |
#include <zephyr/drivers/can.h>
Get the maximum supported timing parameter values.
dev | Pointer to the device structure for the driver instance. |
const struct can_timing * can_get_timing_min | ( | const struct device * | dev | ) |
#include <zephyr/drivers/can.h>
Get the minimum supported timing parameter values.
dev | Pointer to the device structure for the driver instance. |
#include <zephyr/drivers/can.h>
Get the CAN transceiver associated with the CAN controller.
Get a pointer to the device structure for the CAN transceiver associated with the CAN controller.
dev | Pointer to the device structure for the driver instance. |
int can_recover | ( | const struct device * | dev, |
k_timeout_t | timeout ) |
#include <zephyr/drivers/can.h>
Recover from bus-off state.
Recover the CAN controller from bus-off state to error-active state.
CONFIG_CAN_MANUAL_RECOVERY_MODE
must be enabled for this function to be available.dev | Pointer to the device structure for the driver instance. |
timeout | Timeout for waiting for the recovery or K_FOREVER . |
0 | on success. |
-ENOTSUP | if the CAN controller is not in manual recovery mode. |
-ENETDOWN | if the CAN controller is in stopped state. |
-EAGAIN | on timeout. |
-ENOSYS | If this function is not implemented by the driver. |
void can_remove_rx_filter | ( | const struct device * | dev, |
int | filter_id ) |
#include <zephyr/drivers/can.h>
Remove a CAN RX filter.
This routine removes a CAN RX filter based on the filter ID returned by can_add_rx_filter() or can_add_rx_filter_msgq().
dev | Pointer to the device structure for the driver instance. |
filter_id | Filter ID |
int can_send | ( | const struct device * | dev, |
const struct can_frame * | frame, | ||
k_timeout_t | timeout, | ||
can_tx_callback_t | callback, | ||
void * | user_data ) |
#include <zephyr/drivers/can.h>
Queue a CAN frame for transmission on the CAN bus.
Queue a CAN frame for transmission on the CAN bus with optional timeout and completion callback function.
Queued CAN frames are transmitted in order according to the their priority:
By default, the CAN controller will automatically retry transmission in case of lost bus arbitration or missing acknowledge. Some CAN controllers support disabling automatic retransmissions via CAN_MODE_ONE_SHOT
.
dev | Pointer to the device structure for the driver instance. |
frame | CAN frame to transmit. |
timeout | Timeout waiting for a empty TX mailbox or K_FOREVER . |
callback | Optional callback for when the frame was sent or a transmission error occurred. If NULL , this function is blocking until frame is sent. The callback must be NULL if called from user mode. |
user_data | User data to pass to callback function. |
0 | if successful. |
-EINVAL | if an invalid parameter was passed to the function. |
-ENOTSUP | if an unsupported parameter was passed to the function. |
-ENETDOWN | if the CAN controller is in stopped state. |
-ENETUNREACH | if the CAN controller is in bus-off state. |
-EBUSY | if CAN bus arbitration was lost (only applicable if automatic retransmissions are disabled). |
-EIO | if a general transmit error occurred (e.g. missing ACK if automatic retransmissions are disabled). |
-EAGAIN | on timeout. |
#include <zephyr/drivers/can.h>
Set the bitrate of the CAN controller.
CAN in Automation (CiA) 301 v4.2.0 recommends a sample point location of 87.5% percent for all bitrates. However, some CAN controllers have difficulties meeting this for higher bitrates.
This function defaults to using a sample point of 75.0% for bitrates over 800 kbit/s, 80.0% for bitrates over 500 kbit/s, and 87.5% for all other bitrates. This is in line with the sample point locations used by the Linux kernel.
dev | Pointer to the device structure for the driver instance. |
bitrate | Desired arbitration phase bitrate. |
0 | If successful. |
-EBUSY | if the CAN controller is not in stopped state. |
-EINVAL | if the requested bitrate is out of range. |
-ENOTSUP | if the requested bitrate not supported by the CAN controller/transceiver combination. |
-ERANGE | if the resulting sample point is off by more than +/- 5%. |
-EIO | General input/output error, failed to set bitrate. |
#include <zephyr/drivers/can.h>
Set the bitrate for the data phase of the CAN FD controller.
CAN in Automation (CiA) 301 v4.2.0 recommends a sample point location of 87.5% percent for all bitrates. However, some CAN controllers have difficulties meeting this for higher bitrates.
This function defaults to using a sample point of 75.0% for bitrates over 800 kbit/s, 80.0% for bitrates over 500 kbit/s, and 87.5% for all other bitrates. This is in line with the sample point locations used by the Linux kernel.
CONFIG_CAN_FD_MODE
must be selected for this function to be available.dev | Pointer to the device structure for the driver instance. |
bitrate_data | Desired data phase bitrate. |
0 | If successful. |
-EBUSY | if the CAN controller is not in stopped state. |
-EINVAL | if the requested bitrate is out of range. |
-ENOTSUP | if the requested bitrate not supported by the CAN controller/transceiver combination. |
-ERANGE | if the resulting sample point is off by more than +/- 5%. |
-EIO | General input/output error, failed to set bitrate. |
int can_set_mode | ( | const struct device * | dev, |
can_mode_t | mode ) |
#include <zephyr/drivers/can.h>
Set the CAN controller to the given operation mode.
dev | Pointer to the device structure for the driver instance. |
mode | Operation mode. |
0 | If successful. |
-EBUSY | if the CAN controller is not in stopped state. |
-EIO | General input/output error, failed to configure device. |
|
inlinestatic |
#include <zephyr/drivers/can.h>
Set a callback for CAN controller state change events.
Set the callback for CAN controller state change events. The callback function will be called in interrupt context.
Only one callback can be registered per controller. Calling this function again overrides any previously registered callback.
dev | Pointer to the device structure for the driver instance. |
callback | Callback function. |
user_data | User data to pass to callback function. |
int can_set_timing | ( | const struct device * | dev, |
const struct can_timing * | timing ) |
#include <zephyr/drivers/can.h>
Configure the bus timing of a CAN controller.
dev | Pointer to the device structure for the driver instance. |
timing | Bus timings. |
0 | If successful. |
-EBUSY | if the CAN controller is not in stopped state. |
-ENOTSUP | if the timing parameters are not supported by the driver. |
-EIO | General input/output error, failed to configure device. |
int can_set_timing_data | ( | const struct device * | dev, |
const struct can_timing * | timing_data ) |
#include <zephyr/drivers/can.h>
Configure the bus timing for the data phase of a CAN FD controller.
CONFIG_CAN_FD_MODE
must be selected for this function to be available.dev | Pointer to the device structure for the driver instance. |
timing_data | Bus timings for data phase |
0 | If successful. |
-EBUSY | if the CAN controller is not in stopped state. |
-EIO | General input/output error, failed to configure device. |
-ENOTSUP | if the timing parameters are not supported by the driver. |
-ENOSYS | if CAN FD support is not implemented by the driver. |
int can_start | ( | const struct device * | dev | ) |
#include <zephyr/drivers/can.h>
Start the CAN controller.
Bring the CAN controller out of CAN_STATE_STOPPED
. This will reset the RX/TX error counters, enable the CAN controller to participate in CAN communication, and enable the CAN transceiver, if supported.
Starting the CAN controller resets all the CAN controller statistics.
dev | Pointer to the device structure for the driver instance. |
0 | if successful. |
-EALREADY | if the device is already started. |
-EIO | General input/output error, failed to start device. |
#include <zephyr/drivers/can.h>
Get the acknowledge error counter for a CAN device.
The acknowledge error counter is incremented when the CAN controller does not monitor a dominant bit in the ACK slot.
CONFIG_CAN_STATS
must be selected for this function to be available.dev | Pointer to the device structure for the driver instance. |
#include <zephyr/drivers/can.h>
Get the bit0 error counter for a CAN device.
The bit0 error counter is incremented when the CAN controller is unable to transmit a dominant bit.
CONFIG_CAN_STATS
must be selected for this function to be available.dev | Pointer to the device structure for the driver instance. |
#include <zephyr/drivers/can.h>
Get the bit1 error counter for a CAN device.
The bit1 error counter is incremented when the CAN controller is unable to transmit a recessive bit.
CONFIG_CAN_STATS
must be selected for this function to be available.dev | Pointer to the device structure for the driver instance. |
#include <zephyr/drivers/can.h>
Get the bit error counter for a CAN device.
The bit error counter is incremented when the CAN controller is unable to transmit either a dominant or a recessive bit.
CONFIG_CAN_STATS
must be selected for this function to be available.dev | Pointer to the device structure for the driver instance. |
#include <zephyr/drivers/can.h>
Get the CRC error counter for a CAN device.
The CRC error counter is incremented when the CAN controller detects a frame with an invalid CRC.
CONFIG_CAN_STATS
must be selected for this function to be available.dev | Pointer to the device structure for the driver instance. |
#include <zephyr/drivers/can.h>
Get the form error counter for a CAN device.
The form error counter is incremented when the CAN controller detects a fixed-form bit field containing illegal bits.
CONFIG_CAN_STATS
must be selected for this function to be available.dev | Pointer to the device structure for the driver instance. |
#include <zephyr/drivers/can.h>
Get the RX overrun counter for a CAN device.
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).
CONFIG_CAN_STATS
must be selected for this function to be available.dev | Pointer to the device structure for the driver instance. |
#include <zephyr/drivers/can.h>
Get the stuffing error counter for a CAN device.
The stuffing error counter is incremented when the CAN controller detects a bit stuffing error.
CONFIG_CAN_STATS
must be selected for this function to be available.dev | Pointer to the device structure for the driver instance. |
int can_stop | ( | const struct device * | dev | ) |
#include <zephyr/drivers/can.h>
Stop the CAN controller.
Bring the CAN controller into CAN_STATE_STOPPED
. This will disallow the CAN controller from participating in CAN communication, abort any pending CAN frame transmissions, and disable the CAN transceiver, if supported.
dev | Pointer to the device structure for the driver instance. |
0 | if successful. |
-EALREADY | if the device is already stopped. |
-EIO | General input/output error, failed to stop device. |