Zephyr API Documentation  3.0.0
A Scalable Open Source RTOS
3.0.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
CAN Interface

CAN Interface. More...

Data Structures

struct  zcan_frame
 CAN frame structure. More...
 
struct  zcan_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_frame
 CAN frame for Linux SocketCAN compatibility. More...
 
struct  can_filter
 CAN filter for Linux SocketCAN compatibility. More...
 

Typedefs

typedef void(* can_tx_callback_t) (int error, void *user_data)
 Defines the application callback handler function signature. More...
 
typedef void(* can_rx_callback_t) (struct zcan_frame *frame, void *user_data)
 Defines the application callback handler function signature for receiving. More...
 
typedef void(* can_state_change_callback_t) (enum can_state state, struct can_bus_err_cnt err_cnt, void *user_data)
 Defines the state change callback handler function signature. More...
 

Enumerations

enum  can_mode { CAN_NORMAL_MODE , CAN_SILENT_MODE , CAN_LOOPBACK_MODE , CAN_SILENT_LOOPBACK_MODE }
 Defines the mode of the CAN controller. More...
 
enum  can_state { CAN_ERROR_ACTIVE , CAN_ERROR_WARNING , CAN_ERROR_PASSIVE , CAN_BUS_OFF }
 Defines the state of the CAN bus. More...
 
enum  can_ide { CAN_STANDARD_IDENTIFIER , CAN_EXTENDED_IDENTIFIER }
 Defines if the CAN frame has a standard (11-bit) or extended (29-bit) CAN identifier. More...
 
enum  can_rtr { CAN_DATAFRAME , CAN_REMOTEREQUEST }
 Defines if the CAN frame is a data frame or a Remote Transmission Request (RTR) frame. More...
 

Linux SocketCAN compatibility

The following structures and functions provide compatibility with the CAN frame and CAN filter formats used by Linux SocketCAN.

typedef uint32_t canid_t
 
static void can_copy_frame_to_zframe (const struct can_frame *frame, struct zcan_frame *zframe)
 Translate a can_frame struct to a zcan_frame struct. More...
 
static void can_copy_zframe_to_frame (const struct zcan_frame *zframe, struct can_frame *frame)
 Translate a zcan_frame struct to a can_frame struct. More...
 
static void can_copy_filter_to_zfilter (const struct can_filter *filter, struct zcan_filter *zfilter)
 Translate a can_filter struct to a zcan_filter struct. More...
 
static void can_copy_zfilter_to_filter (const struct zcan_filter *zfilter, struct can_filter *filter)
 Translate a zcan_filter struct to a can_filter struct. More...
 

CAN controller configuration

int can_get_core_clock (const struct device *dev, uint32_t *rate)
 Get the CAN core clock rate. More...
 
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. More...
 
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. More...
 
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. More...
 
int can_set_timing (const struct device *dev, const struct can_timing *timing, const struct can_timing *timing_data)
 Configure the bus timing of a CAN controller. More...
 
int can_set_mode (const struct device *dev, enum can_mode mode)
 Set the CAN controller to the given operation mode. More...
 
static int can_set_bitrate (const struct device *dev, uint32_t bitrate, uint32_t bitrate_data)
 Set the bitrate of the CAN controller. More...
 
#define CAN_SJW_NO_CHANGE   0
 

Transmitting CAN frames

int can_send (const struct device *dev, const struct zcan_frame *frame, k_timeout_t timeout, can_tx_callback_t callback, void *user_data)
 Transmit a CAN frame on the CAN bus. More...
 

Receiving CAN frames

static int can_add_rx_filter (const struct device *dev, can_rx_callback_t callback, void *user_data, const struct zcan_filter *filter)
 Add a callback function for a given CAN filter. More...
 
int can_add_rx_filter_msgq (const struct device *dev, struct k_msgq *msgq, const struct zcan_filter *filter)
 Wrapper function for adding a message queue for a given filter. More...
 
void can_remove_rx_filter (const struct device *dev, int filter_id)
 Remove a CAN RX filter. More...
 
int can_get_max_filters (const struct device *dev, enum can_ide id_type)
 Get maximum number of RX filters. More...
 
#define CAN_MSGQ_DEFINE(name, max_frames)    K_MSGQ_DEFINE(name, sizeof(struct zcan_frame), max_frames, 4)
 Statically define and initialize a CAN RX message queue. More...
 

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. More...
 
int can_recover (const struct device *dev, k_timeout_t timeout)
 Recover from bus-off state. More...
 
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. More...
 

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. More...
 
static uint8_t can_bytes_to_dlc (uint8_t num_bytes)
 Convert from number of bytes to Data Length Code (DLC) More...
 

CAN frame definitions

#define CAN_STD_ID_MASK   0x7FFU
 Bit mask for a standard (11-bit) CAN identifier. More...
 
#define CAN_MAX_STD_ID   CAN_STD_ID_MASK
 Maximum value for a standard (11-bit) CAN identifier. More...
 
#define CAN_EXT_ID_MASK   0x1FFFFFFFU
 Bit mask for an extended (29-bit) CAN identifier. More...
 
#define CAN_MAX_EXT_ID   CAN_EXT_ID_MASK
 Maximum value for an extended (29-bit) CAN identifier. More...
 
#define CAN_MAX_DLC   8U
 Maximum data length code for CAN 2.0A/2.0B. More...
 
#define CANFD_MAX_DLC   CONFIG_CANFD_MAX_DLC
 Maximum data length code for CAN-FD. More...
 

Detailed Description

CAN Interface.

Macro Definition Documentation

◆ CAN_EXT_ID_MASK

#define CAN_EXT_ID_MASK   0x1FFFFFFFU

#include <include/drivers/can.h>

Bit mask for an extended (29-bit) CAN identifier.

◆ CAN_MAX_DLC

#define CAN_MAX_DLC   8U

#include <include/drivers/can.h>

Maximum data length code for CAN 2.0A/2.0B.

◆ CAN_MAX_EXT_ID

#define CAN_MAX_EXT_ID   CAN_EXT_ID_MASK

#include <include/drivers/can.h>

Maximum value for an extended (29-bit) CAN identifier.

◆ CAN_MAX_STD_ID

#define CAN_MAX_STD_ID   CAN_STD_ID_MASK

#include <include/drivers/can.h>

Maximum value for a standard (11-bit) CAN identifier.

◆ CAN_MSGQ_DEFINE

#define CAN_MSGQ_DEFINE (   name,
  max_frames 
)     K_MSGQ_DEFINE(name, sizeof(struct zcan_frame), max_frames, 4)

#include <include/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.

See also
can_add_rx_filter_msgq()
Parameters
nameName of the message queue.
max_framesMaximum number of CAN frames that can be queued.

◆ CAN_SJW_NO_CHANGE

#define CAN_SJW_NO_CHANGE   0

#include <include/drivers/can.h>

Synchronization Jump Width (SJW) value to indicate that the SJW should not be changed by the timing calculation.

◆ CAN_STD_ID_MASK

#define CAN_STD_ID_MASK   0x7FFU

#include <include/drivers/can.h>

Bit mask for a standard (11-bit) CAN identifier.

◆ CANFD_MAX_DLC

#define CANFD_MAX_DLC   CONFIG_CANFD_MAX_DLC

#include <include/drivers/can.h>

Maximum data length code for CAN-FD.

Typedef Documentation

◆ can_rx_callback_t

can_rx_callback_t

#include <include/drivers/can.h>

Defines the application callback handler function signature for receiving.

Parameters
frameReceived frame.
user_dataUser data provided when the filter was added.

◆ can_state_change_callback_t

can_state_change_callback_t

#include <include/drivers/can.h>

Defines the state change callback handler function signature.

Parameters
stateState of the CAN controller.
err_cntCAN controller error counter values.
user_dataUser data provided the callback was set.

◆ can_tx_callback_t

can_tx_callback_t

#include <include/drivers/can.h>

Defines the application callback handler function signature.

Parameters
errorStatus of the performed send operation. See the list of return values for can_send() for value descriptions.
user_dataUser data provided when the frame was sent.

◆ canid_t

typedef uint32_t canid_t

#include <include/drivers/can.h>

CAN Identifier structure for Linux SocketCAN compatibility.

The fields in this type are:

+------+--------------------------------------------------------------+
| Bits | Description |
+======+==============================================================+
| 0-28 | CAN identifier (11/29 bit) |
+------+--------------------------------------------------------------+
| 29 | Error message frame flag (0 = data frame, 1 = error message) |
+------+--------------------------------------------------------------+
| 30 | Remote transmission request flag (1 = RTR frame) |
+------+--------------------------------------------------------------+
| 31 | Frame format flag (0 = standard 11 bit, 1 = extended 29 bit) |
+------+--------------------------------------------------------------+

Enumeration Type Documentation

◆ can_ide

enum can_ide

#include <include/drivers/can.h>

Defines if the CAN frame has a standard (11-bit) or extended (29-bit) CAN identifier.

Enumerator
CAN_STANDARD_IDENTIFIER 

Standard (11-bit) CAN identifier.

CAN_EXTENDED_IDENTIFIER 

Extended (29-bit) CAN identifier.

◆ can_mode

enum can_mode

#include <include/drivers/can.h>

Defines the mode of the CAN controller.

Enumerator
CAN_NORMAL_MODE 

Normal mode.

CAN_SILENT_MODE 

Controller is not allowed to send dominant bits.

CAN_LOOPBACK_MODE 

Controller is in loopback mode (receives own frames).

CAN_SILENT_LOOPBACK_MODE 

Combination of loopback and silent modes.

◆ can_rtr

enum can_rtr

#include <include/drivers/can.h>

Defines if the CAN frame is a data frame or a Remote Transmission Request (RTR) frame.

Enumerator
CAN_DATAFRAME 

Data frame.

CAN_REMOTEREQUEST 

Remote Transmission Request (RTR) frame.

◆ can_state

enum can_state

#include <include/drivers/can.h>

Defines the state of the CAN bus.

Enumerator
CAN_ERROR_ACTIVE 

Error-active state (RX/TX error count < 96).

CAN_ERROR_WARNING 

Error-warning state (RX/TX error count < 128).

CAN_ERROR_PASSIVE 

Error-passive state (RX/TX error count < 256).

CAN_BUS_OFF 

Bus-off state (RX/TX error count >= 256).

Function Documentation

◆ can_add_rx_filter()

static int can_add_rx_filter ( const struct device dev,
can_rx_callback_t  callback,
void *  user_data,
const struct zcan_filter filter 
)
inlinestatic

#include <include/drivers/can.h>

Add a callback function for a given CAN filter.

Add a callback to CAN identifiers specified by a filter. When a recevied CAN frame matching the filter is received by the CAN controller, the callback function is called in interrupt context.

If a frame matches more than one attached filter, the priority of the match is hardware dependent.

The same callback function can be used for multiple filters.

Parameters
devPointer to the device structure for the driver instance.
callbackThis function is called by the CAN controller driver whenever a frame matching the filter is received.
user_dataUser data to pass to callback function.
filterPointer to a zcan_filter structure defining the filter.
Return values
filter_idon success.
-ENOSPCif there are no free filters.

◆ can_add_rx_filter_msgq()

int can_add_rx_filter_msgq ( const struct device dev,
struct k_msgq msgq,
const struct zcan_filter filter 
)

#include <include/drivers/can.h>

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 frame matches more than one attached filter, the priority of the match is hardware dependent.

The same message queue can be used for multiple filters.

Note
The message queue must be initialized before calling this function and the caller must have appropriate permissions on it.
Parameters
devPointer to the device structure for the driver instance.
msgqPointer to the already initialized k_msgq struct.
filterPointer to a zcan_filter structure defining the filter.
Return values
filter_idon success.
-ENOSPCif there are no free filters.

◆ can_bytes_to_dlc()

static uint8_t can_bytes_to_dlc ( uint8_t  num_bytes)
inlinestatic

#include <include/drivers/can.h>

Convert from number of bytes to Data Length Code (DLC)

Parameters
num_bytesNumber of bytes.
Return values
DataLength Code (DLC).

◆ can_calc_prescaler()

int can_calc_prescaler ( const struct device dev,
struct can_timing timing,
uint32_t  bitrate 
)

#include <include/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 reminder of the devision of the clock rate by the bitrate times the timing segments.

Parameters
devPointer to the device structure for the driver instance.
timingResult is written into the can_timing struct provided.
bitrateTarget bitrate.
Return values
0or positive bitrate error.
Negativeerror code on error.

◆ can_calc_timing()

int can_calc_timing ( const struct device dev,
struct can_timing res,
uint32_t  bitrate,
uint16_t  sample_pnt 
)

#include <include/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 alway match perfectly. If no result can be reached for the given parameters, -EINVAL is returned.

Note
The requested sample_pnt will not always be matched perfectly. The algorithm calculates the best possible match.
Parameters
devPointer to the device structure for the driver instance.
[out]resResult is written into the can_timing struct provided.
bitrateTarget bitrate in bits/s.
sample_pntSampling point in permill of the entire bit time.
Return values
0or positive sample point error on success.
-EINVALif there is no solution for the desired values.
-EIOif can_get_core_clock() is not available.

◆ can_calc_timing_data()

int can_calc_timing_data ( const struct device dev,
struct can_timing res,
uint32_t  bitrate,
uint16_t  sample_pnt 
)

#include <include/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.

Note
embed:rst:inline :kconfig:`CONFIG_CAN_FD_MODE` 
must be selected for this function to be available.
Parameters
devPointer to the device structure for the driver instance.
[out]resResult is written into the can_timing struct provided.
bitrateTarget bitrate for the data phase in bits/s
sample_pntSampling point for the data phase in permille of the entire bit time.
Return values
0or positive sample point error on success.
-EINVALif there is no solution for the desired values.
-EIOif can_get_core_clock() is not available.

◆ can_copy_filter_to_zfilter()

static void can_copy_filter_to_zfilter ( const struct can_filter filter,
struct zcan_filter zfilter 
)
inlinestatic

#include <include/drivers/can.h>

Translate a can_filter struct to a zcan_filter struct.

Parameters
filterPointer to can_filter struct.
zfilterPointer to zcan_filter struct.

◆ can_copy_frame_to_zframe()

static void can_copy_frame_to_zframe ( const struct can_frame frame,
struct zcan_frame zframe 
)
inlinestatic

#include <include/drivers/can.h>

Translate a can_frame struct to a zcan_frame struct.

Parameters
framePointer to can_frame struct.
zframePointer to zcan_frame struct.

◆ can_copy_zfilter_to_filter()

static void can_copy_zfilter_to_filter ( const struct zcan_filter zfilter,
struct can_filter filter 
)
inlinestatic

#include <include/drivers/can.h>

Translate a zcan_filter struct to a can_filter struct.

Parameters
zfilterPointer to zcan_filter struct.
filterPointer to can_filter struct.

◆ can_copy_zframe_to_frame()

static void can_copy_zframe_to_frame ( const struct zcan_frame zframe,
struct can_frame frame 
)
inlinestatic

#include <include/drivers/can.h>

Translate a zcan_frame struct to a can_frame struct.

Parameters
zframePointer to zcan_frame struct.
framePointer to can_frame struct.

◆ can_dlc_to_bytes()

static uint8_t can_dlc_to_bytes ( uint8_t  dlc)
inlinestatic

#include <include/drivers/can.h>

Convert from Data Length Code (DLC) to the number of data bytes.

Parameters
dlcData Length Code (DLC).
Return values
Numberof bytes.

◆ can_get_core_clock()

int can_get_core_clock ( const struct device dev,
uint32_t rate 
)

#include <include/drivers/can.h>

Get the CAN core clock rate.

Returns the CAN core clock rate. One time quantum is 1/(core clock rate).

Parameters
devPointer to the device structure for the driver instance.
[out]rateCAN core clock rate in Hz.
Returns
0 on success, or a negative error code on error

◆ can_get_max_filters()

int can_get_max_filters ( const struct device dev,
enum can_ide  id_type 
)

#include <include/drivers/can.h>

Get maximum number of RX filters.

Get the maximum number of concurrent RX filters for the CAN controller.

Parameters
devPointer to the device structure for the driver instance.
id_typeCAN identifier type (standard or extended).
Return values
Positivenumber of maximum concurrent filters.
-EIOGeneral input/output error.
-ENOSYSIf this function is not implemented by the driver.

◆ can_get_state()

int can_get_state ( const struct device dev,
enum can_state state,
struct can_bus_err_cnt err_cnt 
)

#include <include/drivers/can.h>

Get current CAN controller state.

Returns the current state and optionally the error counter values of the CAN controller.

Parameters
devPointer to the device structure for the driver instance.
[out]statePointer to the state destination enum or NULL.
[out]err_cntPointer to the err_cnt destination structure or NULL.
Return values
0If successful.
-EIOGeneral input/output error, failed to get state.

◆ can_recover()

int can_recover ( const struct device dev,
k_timeout_t  timeout 
)

#include <include/drivers/can.h>

Recover from bus-off state.

Recover the CAN controller from bus-off state to error-active state.

Note
embed:rst:inline :kconfig:`CONFIG_CAN_AUTO_BUS_OFF_RECOVERY` 
must be deselected for this function to be available.
Parameters
devPointer to the device structure for the driver instance.
timeoutTimeout for waiting for the recovery or K_FOREVER.
Return values
0on success.
-EAGAINon timeout.

◆ can_remove_rx_filter()

void can_remove_rx_filter ( const struct device dev,
int  filter_id 
)

#include <include/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().

Parameters
devPointer to the device structure for the driver instance.
filter_idFilter ID

◆ can_send()

int can_send ( const struct device dev,
const struct zcan_frame frame,
k_timeout_t  timeout,
can_tx_callback_t  callback,
void *  user_data 
)

#include <include/drivers/can.h>

Transmit a CAN frame on the CAN bus.

Transmit a CAN frame on the CAN bus with optional timeout and completion callback function.

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 ("one-shot" mode) via a devicetree property.

Parameters
devPointer to the device structure for the driver instance.
frameCAN frame to transmit.
timeoutTimeout waiting for a empty TX mailbox or K_FOREVER.
callbackOptional 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_dataUser data to pass to callback function.
Return values
0if successful.
-EINVALif an invalid parameter was passed to the function.
-ENETDOWNif the CAN controller is in bus-off state.
-EBUSYif CAN bus arbitration was lost (only applicable if automatic retransmissions are disabled).
-EIOif a general transmit error occurred (e.g. missing ACK if automatic retransmissions are disabled).
-EAGAINon timeout.

◆ can_set_bitrate()

static int can_set_bitrate ( const struct device dev,
uint32_t  bitrate,
uint32_t  bitrate_data 
)
inlinestatic

#include <include/drivers/can.h>

Set the bitrate of the CAN controller.

The sample point is set to the CiA DS 301 recommended value of 87.5%.

Note
The parameter bitrate_data is only relevant for CAN-FD. If the controller does not support CAN-FD or if
embed:rst:inline :kconfig:`CONFIG_CAN_FD_MODE` 
is not selected, the value of this parameter is ignored.
Parameters
devPointer to the device structure for the driver instance.
bitrateDesired arbitration phase bitrate.
bitrate_dataDesired data phase bitrate.
Return values
0If successful.
-EINVALbitrate cannot be met.
-EIOGeneral input/output error, failed to set bitrate.

◆ can_set_mode()

int can_set_mode ( const struct device dev,
enum can_mode  mode 
)

#include <include/drivers/can.h>

Set the CAN controller to the given operation mode.

Parameters
devPointer to the device structure for the driver instance.
modeOperation mode.
Return values
0If successful.
-EIOGeneral input/output error, failed to configure device.

◆ can_set_state_change_callback()

static void can_set_state_change_callback ( const struct device dev,
can_state_change_callback_t  callback,
void *  user_data 
)
inlinestatic

#include <include/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.

Parameters
devPointer to the device structure for the driver instance.
callbackCallback function.
user_dataUser data to pass to callback function.

◆ can_set_timing()

int can_set_timing ( const struct device dev,
const struct can_timing timing,
const struct can_timing timing_data 
)

#include <include/drivers/can.h>

Configure the bus timing of a CAN controller.

If the sjw equals CAN_SJW_NO_CHANGE, the sjw parameter is not changed.

Note
The parameter timing_data is only relevant for CAN-FD. If the controller does not support CAN-FD or if
embed:rst:inline :kconfig:`CONFIG_CAN_FD_MODE` 
is not selected, the value of this parameter is ignored.
Parameters
devPointer to the device structure for the driver instance.
timingBus timings.
timing_dataBus timings for data phase (CAN-FD only).
Return values
0If successful.
-EIOGeneral input/output error, failed to configure device.