Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
CAN Interface

CAN Interface. More...

Data Structures

struct  can_frame
 CAN frame structure that is compatible with Linux. This is mainly used by Socket CAN code. More...
 
struct  can_filter
 CAN filter that is compatible with Linux. This is mainly used by Socket CAN code. More...
 
struct  zcan_frame
 CAN message structure. More...
 
struct  zcan_filter
 CAN filter structure. More...
 
struct  can_bus_err_cnt
 can bus error count structure More...
 
struct  can_timing
 canbus timings More...
 
struct  can_frame_buffer
 
struct  zcan_work
 CAN work structure. More...
 
struct  can_driver_api
 

Macros

#define CAN_EX_ID   (1 << 31)
 
#define CAN_MAX_STD_ID   (0x7FF)
 
#define CAN_STD_ID_MASK   CAN_MAX_STD_ID
 
#define CAN_EXT_ID_MASK   (0x1FFFFFFF)
 
#define CAN_MAX_DLC   (8)
 
#define CANFD_MAX_DLC   CONFIG_CANFD_MAX_DLC
 
#define CAN_MAX_DLEN   8
 
#define CAN_TX_OK   (0)
 
#define CAN_TX_ERR   (-2)
 
#define CAN_TX_ARB_LOST   (-3)
 
#define CAN_TX_BUS_OFF   (-4)
 
#define CAN_TX_UNKNOWN   (-5)
 
#define CAN_TX_EINVAL   (-22)
 
#define CAN_NO_FREE_FILTER   (-1)
 
#define CAN_TIMEOUT   (-1)
 
#define CAN_DEFINE_MSGQ(name, size)    K_MSGQ_DEFINE(name, sizeof(struct zcan_frame), size, 4)
 Statically define and initialize a can message queue. More...
 
#define CAN_SJW_NO_CHANGE   0
 
#define CONFIG_CAN_WORKQ_FRAMES_BUF_CNT   4
 

Typedefs

typedef uint32_t canid_t
 
typedef void(* can_tx_callback_t) (uint32_t error_flags, void *arg)
 Define the application callback handler function signature. More...
 
typedef void(* can_rx_callback_t) (struct zcan_frame *msg, void *arg)
 Define the application callback handler function signature for receiving. More...
 
typedef void(* can_state_change_isr_t) (enum can_state state, struct can_bus_err_cnt err_cnt)
 Defines the state change isr handler function signature. More...
 
typedef int(* can_set_timing_t) (const struct device *dev, const struct can_timing *timing, const struct can_timing *timing_data)
 
typedef int(* can_set_mode_t) (const struct device *dev, enum can_mode mode)
 
typedef int(* can_send_t) (const struct device *dev, const struct zcan_frame *msg, k_timeout_t timeout, can_tx_callback_t callback_isr, void *callback_arg)
 
typedef int(* can_attach_msgq_t) (const struct device *dev, struct k_msgq *msg_q, const struct zcan_filter *filter)
 
typedef int(* can_attach_isr_t) (const struct device *dev, can_rx_callback_t isr, void *callback_arg, const struct zcan_filter *filter)
 
typedef void(* can_detach_t) (const struct device *dev, int filter_id)
 
typedef int(* can_recover_t) (const struct device *dev, k_timeout_t timeout)
 
typedef enum can_state(* can_get_state_t) (const struct device *dev, struct can_bus_err_cnt *err_cnt)
 
typedef void(* can_register_state_change_isr_t) (const struct device *dev, can_state_change_isr_t isr)
 
typedef int(* can_get_core_clock_t) (const struct device *dev, uint32_t *rate)
 

Enumerations

enum  can_ide { CAN_STANDARD_IDENTIFIER , CAN_EXTENDED_IDENTIFIER }
 can_ide enum Define if the message has a standard (11bit) or extended (29bit) identifier More...
 
enum  can_rtr { CAN_DATAFRAME , CAN_REMOTEREQUEST }
 can_rtr enum Define if the message is a data or remote frame More...
 
enum  can_mode { CAN_NORMAL_MODE , CAN_SILENT_MODE , CAN_LOOPBACK_MODE , CAN_SILENT_LOOPBACK_MODE }
 can_mode enum Defines the mode of the can controller More...
 
enum  can_state { CAN_ERROR_ACTIVE , CAN_ERROR_PASSIVE , CAN_BUS_OFF , CAN_BUS_UNKNOWN }
 can_state enum Defines the possible states of the CAN bus More...
 

Functions

static uint8_t can_dlc_to_bytes (uint8_t dlc)
 Convert the DLC to the number of bytes. More...
 
static uint8_t can_bytes_to_dlc (uint8_t num_bytes)
 Convert a number of bytes to the DLC. More...
 
int can_send (const struct device *dev, const struct zcan_frame *msg, k_timeout_t timeout, can_tx_callback_t callback_isr, void *callback_arg)
 Perform data transfer to CAN bus. More...
 
static int can_write (const struct device *dev, const uint8_t *data, uint8_t length, uint32_t id, enum can_rtr rtr, k_timeout_t timeout)
 Write a set amount of data to the can bus. More...
 
int can_attach_workq (const struct device *dev, struct k_work_q *work_q, struct zcan_work *work, can_rx_callback_t callback, void *callback_arg, const struct zcan_filter *filter)
 Attach a CAN work queue to a single or group of identifiers. More...
 
int can_attach_msgq (const struct device *dev, struct k_msgq *msg_q, const struct zcan_filter *filter)
 Attach a message queue to a single or group of identifiers. More...
 
static int can_attach_isr (const struct device *dev, can_rx_callback_t isr, void *callback_arg, const struct zcan_filter *filter)
 Attach an isr callback function to a single or group of identifiers. More...
 
void can_detach (const struct device *dev, int filter_id)
 Detach an isr or message queue from the identifier filtering. More...
 
int can_get_core_clock (const struct device *dev, uint32_t *rate)
 Read the core clock value. 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_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_mode (const struct device *dev, enum can_mode mode)
 Set the controller to the given mode. More...
 
int can_set_timing (const struct device *dev, const struct can_timing *timing, const struct can_timing *timing_data)
 Configure timing of a host controller. 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...
 
static int can_configure (const struct device *dev, enum can_mode mode, uint32_t bitrate)
 Configure operation of a host controller. More...
 
enum can_state can_get_state (const struct device *dev, struct can_bus_err_cnt *err_cnt)
 Get current state. More...
 
int can_recover (const struct device *dev, k_timeout_t timeout)
 Recover from bus-off state. More...
 
static void can_register_state_change_isr (const struct device *dev, can_state_change_isr_t isr)
 Register an ISR callback for state change interrupt. More...
 
static void can_copy_frame_to_zframe (const struct can_frame *frame, struct zcan_frame *zframe)
 Converter that translates between can_frame and zcan_frame structs. More...
 
static void can_copy_zframe_to_frame (const struct zcan_frame *zframe, struct can_frame *frame)
 Converter that translates between zcan_frame and can_frame structs. More...
 
static void can_copy_filter_to_zfilter (const struct can_filter *filter, struct zcan_filter *zfilter)
 Converter that translates between can_filter and zcan_frame_filter structs. More...
 
static void can_copy_zfilter_to_filter (const struct zcan_filter *zfilter, struct can_filter *filter)
 Converter that translates between zcan_filter and can_filter structs. More...
 

Detailed Description

CAN Interface.

Macro Definition Documentation

◆ CAN_DEFINE_MSGQ

#define CAN_DEFINE_MSGQ (   name,
  size 
)     K_MSGQ_DEFINE(name, sizeof(struct zcan_frame), size, 4)

#include <include/drivers/can.h>

Statically define and initialize a can message queue.

The message queue's ring buffer contains space for size messages.

Parameters
nameName of the message queue.
sizeNumber of can messages.

◆ CAN_EX_ID

#define CAN_EX_ID   (1 << 31)

◆ CAN_EXT_ID_MASK

#define CAN_EXT_ID_MASK   (0x1FFFFFFF)

◆ CAN_MAX_DLC

#define CAN_MAX_DLC   (8)

◆ CAN_MAX_DLEN

#define CAN_MAX_DLEN   8

◆ CAN_MAX_STD_ID

#define CAN_MAX_STD_ID   (0x7FF)

◆ CAN_NO_FREE_FILTER

#define CAN_NO_FREE_FILTER   (-1)

#include <include/drivers/can.h>

attach_* failed because there is no unused filter left

◆ CAN_SJW_NO_CHANGE

#define CAN_SJW_NO_CHANGE   0

#include <include/drivers/can.h>

SWJ value to indicate that the SJW should not be changed

◆ CAN_STD_ID_MASK

#define CAN_STD_ID_MASK   CAN_MAX_STD_ID

◆ CAN_TIMEOUT

#define CAN_TIMEOUT   (-1)

#include <include/drivers/can.h>

operation timed out

◆ CAN_TX_ARB_LOST

#define CAN_TX_ARB_LOST   (-3)

#include <include/drivers/can.h>

bus arbitration lost during sending

◆ CAN_TX_BUS_OFF

#define CAN_TX_BUS_OFF   (-4)

#include <include/drivers/can.h>

controller is in bus off state

◆ CAN_TX_EINVAL

#define CAN_TX_EINVAL   (-22)

#include <include/drivers/can.h>

invalid parameter

◆ CAN_TX_ERR

#define CAN_TX_ERR   (-2)

#include <include/drivers/can.h>

general send error

◆ CAN_TX_OK

#define CAN_TX_OK   (0)

#include <include/drivers/can.h>

send successfully

◆ CAN_TX_UNKNOWN

#define CAN_TX_UNKNOWN   (-5)

#include <include/drivers/can.h>

unexpected error

◆ CANFD_MAX_DLC

#define CANFD_MAX_DLC   CONFIG_CANFD_MAX_DLC

◆ CONFIG_CAN_WORKQ_FRAMES_BUF_CNT

#define CONFIG_CAN_WORKQ_FRAMES_BUF_CNT   4

Typedef Documentation

◆ can_attach_isr_t

typedef int(* can_attach_isr_t) (const struct device *dev, can_rx_callback_t isr, void *callback_arg, const struct zcan_filter *filter)

◆ can_attach_msgq_t

typedef int(* can_attach_msgq_t) (const struct device *dev, struct k_msgq *msg_q, const struct zcan_filter *filter)

◆ can_detach_t

typedef void(* can_detach_t) (const struct device *dev, int filter_id)

◆ can_get_core_clock_t

typedef int(* can_get_core_clock_t) (const struct device *dev, uint32_t *rate)

◆ can_get_state_t

typedef enum can_state(* can_get_state_t) (const struct device *dev, struct can_bus_err_cnt *err_cnt)

◆ can_recover_t

typedef int(* can_recover_t) (const struct device *dev, k_timeout_t timeout)

◆ can_register_state_change_isr_t

typedef void(* can_register_state_change_isr_t) (const struct device *dev, can_state_change_isr_t isr)

◆ can_rx_callback_t

can_rx_callback_t

#include <include/drivers/can.h>

Define the application callback handler function signature for receiving.

Parameters
msgreceived message
argargument that was passed when the filter was attached

◆ can_send_t

typedef int(* can_send_t) (const struct device *dev, const struct zcan_frame *msg, k_timeout_t timeout, can_tx_callback_t callback_isr, void *callback_arg)

◆ can_set_mode_t

typedef int(* can_set_mode_t) (const struct device *dev, enum can_mode mode)

◆ can_set_timing_t

typedef int(* can_set_timing_t) (const struct device *dev, const struct can_timing *timing, const struct can_timing *timing_data)

◆ can_state_change_isr_t

can_state_change_isr_t

#include <include/drivers/can.h>

Defines the state change isr handler function signature.

Parameters
statestate of the node
err_cntstruct with the error counter values

◆ can_tx_callback_t

can_tx_callback_t

#include <include/drivers/can.h>

Define the application callback handler function signature.

Parameters
error_flagsstatus of the performed send operation
argargument that was passed when the message was sent

◆ canid_t

Enumeration Type Documentation

◆ can_ide

enum can_ide

#include <include/drivers/can.h>

can_ide enum Define if the message has a standard (11bit) or extended (29bit) identifier

Enumerator
CAN_STANDARD_IDENTIFIER 
CAN_EXTENDED_IDENTIFIER 

◆ can_mode

enum can_mode

#include <include/drivers/can.h>

can_mode enum Defines the mode of the can controller

Enumerator
CAN_NORMAL_MODE 
CAN_SILENT_MODE 
CAN_LOOPBACK_MODE 
CAN_SILENT_LOOPBACK_MODE 

◆ can_rtr

enum can_rtr

#include <include/drivers/can.h>

can_rtr enum Define if the message is a data or remote frame

Enumerator
CAN_DATAFRAME 
CAN_REMOTEREQUEST 

◆ can_state

enum can_state

#include <include/drivers/can.h>

can_state enum Defines the possible states of the CAN bus

Enumerator
CAN_ERROR_ACTIVE 
CAN_ERROR_PASSIVE 
CAN_BUS_OFF 
CAN_BUS_UNKNOWN 

Function Documentation

◆ can_attach_isr()

static int can_attach_isr ( const struct device dev,
can_rx_callback_t  isr,
void callback_arg,
const struct zcan_filter filter 
)
inlinestatic

#include <include/drivers/can.h>

Attach an isr callback function to a single or group of identifiers.

This routine attaches an isr callback to identifiers specified by a filter. Whenever the filter matches, the callback function is called with isr context. If a message passes more than one filter the priority of the match is hardware dependent. A callback function can be attached to more than one filter.

  • Parameters
    devPointer to the device structure for the driver instance.
    isrCallback function pointer.
    callback_argThis will be passed whenever the isr is called.
    filterPointer to a zcan_filter structure defining the id filtering.
    Return values
    filter_idon success.
    CAN_NO_FREE_FILTERif there is no filter left.

◆ can_attach_msgq()

int can_attach_msgq ( const struct device dev,
struct k_msgq msg_q,
const struct zcan_filter filter 
)

#include <include/drivers/can.h>

Attach a message queue to a single or group of identifiers.

This routine attaches a message queue to identifiers specified by a filter. Whenever the filter matches, the message is pushed to the queue If a message passes more than one filter the priority of the match is hardware dependent. A message queue can be attached to more than one filter. The message queue must me initialized before, and the caller must have appropriate permissions on it.

Parameters
devPointer to the device structure for the driver instance.
msg_qPointer to the already initialized message queue.
filterPointer to a zcan_filter structure defining the id filtering.
Return values
filter_idon success.
CAN_NO_FREE_FILTERif there is no filter left.

◆ can_attach_workq()

int can_attach_workq ( const struct device dev,
struct k_work_q work_q,
struct zcan_work work,
can_rx_callback_t  callback,
void callback_arg,
const struct zcan_filter filter 
)

#include <include/drivers/can.h>

Attach a CAN work queue to a single or group of identifiers.

This routine attaches a work queue to identifiers specified by a filter. Whenever the filter matches, the message is pushed to the buffer of the zcan_work structure and the work element is put to the workqueue. If a message passes more than one filter the priority of the match is hardware dependent. A CAN work queue can be attached to more than one filter. The work queue must be initialized before and the caller must have appropriate permissions on it.

Parameters
devPointer to the device structure for the driver instance.
work_qPointer to the already initialized work queue.
workPointer to a zcan_work. The work will be initialized.
callbackThis function is called by workq whenever a message arrives.
callback_argIs passed to the callback when called.
filterPointer to a zcan_filter structure defining the id filtering.
Return values
filter_idon success.
CAN_NO_FREE_FILTERif there is no filter left.

◆ can_bytes_to_dlc()

static uint8_t can_bytes_to_dlc ( uint8_t  num_bytes)
inlinestatic

#include <include/drivers/can.h>

Convert a number of bytes to the DLC.

This function converts a number of bytes to the Data Length Code

Parameters
num_bytesThe number of bytes
Return values
TheDLC

◆ 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. sjw, prop_seg, phase_seg1 and phase_seg2 must be given. The returned bitrate error is reminder of the devision of the clockrate 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
bitrateerror
negativeon 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 given for the, give parameters, -EINVAL is returned. The sample_pnt does not always match perfectly. The algorithm tries to find the best match possible.

Parameters
devPointer to the device structure for the driver instance.
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
Positivesample point error on success
-EINVALif there is no solution for the desired values
-EIOif core_clock is not available

◆ can_configure()

static int can_configure ( const struct device dev,
enum can_mode  mode,
uint32_t  bitrate 
)
inlinestatic

#include <include/drivers/can.h>

Configure operation of a host controller.

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

◆ 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>

Converter that translates between can_filter and zcan_frame_filter structs.

Parameters
filterPointer to can_filter struct.
zfilterPointer to zcan_frame_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>

Converter that translates between can_frame and zcan_frame structs.

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>

Converter that translates between zcan_filter and can_filter structs.

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>

Converter that translates between zcan_frame and can_frame structs.

Parameters
zframePointer to zcan_frame struct.
framePointer to can_frame struct.

◆ can_detach()

void can_detach ( const struct device dev,
int  filter_id 
)

#include <include/drivers/can.h>

Detach an isr or message queue from the identifier filtering.

This routine detaches an isr callback or message queue from the identifier filtering.

  • Parameters
    devPointer to the device structure for the driver instance.
    filter_idfilter id returned by can_attach_isr or can_attach_msgq.
    Return values
    none

◆ can_dlc_to_bytes()

static uint8_t can_dlc_to_bytes ( uint8_t  dlc)
inlinestatic

#include <include/drivers/can.h>

Convert the DLC to the number of bytes.

This function converts a the Data Length Code to the number of bytes.

Parameters
dlcThe Data Length Code
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>

Read the core clock value.

Returns the core clock value. One time quantum is 1/core clock.

Parameters
devPointer to the device structure for the driver instance.
[out]ratecontroller clock rate
Return values
0on success
negativeon error

◆ can_get_state()

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

#include <include/drivers/can.h>

Get current state.

Returns the actual state of the CAN controller.

Parameters
devPointer to the device structure for the driver instance.
err_cntPointer to the err_cnt destination structure or NULL.
Return values
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.

Parameters
devPointer to the device structure for the driver instance.
timeoutTimeout for waiting for the recovery or K_FOREVER.
Return values
0on success.
CAN_TIMEOUTon timeout.

◆ can_register_state_change_isr()

static void can_register_state_change_isr ( const struct device dev,
can_state_change_isr_t  isr 
)
inlinestatic

#include <include/drivers/can.h>

Register an ISR callback for state change interrupt.

Only one callback can be registered per controller. Calling this function again, overrides the previous call.

Parameters
devPointer to the device structure for the driver instance.
isrPointer to ISR

◆ can_send()

int can_send ( const struct device dev,
const struct zcan_frame msg,
k_timeout_t  timeout,
can_tx_callback_t  callback_isr,
void callback_arg 
)

#include <include/drivers/can.h>

Perform data transfer to CAN bus.

This routine provides a generic interface to perform data transfer to the can bus. Use can_write() for simple write.

  • Parameters
    devPointer to the device structure for the driver instance.
    msgMessage to transfer.
    timeoutWaiting for empty tx mailbox timeout or K_FOREVER.
    callback_isrIs called when message was sent or a transmission error occurred. If NULL, this function is blocking until message is sent. This must be NULL if called from user mode.
    callback_argThis will be passed whenever the isr is called.
    Return values
    0If successful.
    CAN_TX_*on failure.

◆ 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 second parameter bitrate_data is only relevant for CAN-FD. If the controller does not support CAN-FD or the FD mode is not enabled, this parameter is ignored. The sample point is set to the CiA DS 301 reccommended value of 87.5%

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 reached.
-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 controller to the given 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_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 timing of a host controller.

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

The second parameter timing_data is only relevant for CAN-FD. If the controller does not support CAN-FD or the FD mode is not enabled, 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.

◆ can_write()

static int can_write ( const struct device dev,
const uint8_t data,
uint8_t  length,
uint32_t  id,
enum can_rtr  rtr,
k_timeout_t  timeout 
)
inlinestatic

#include <include/drivers/can.h>

Write a set amount of data to the can bus.

This routine writes a set amount of data synchronously.

Parameters
devPointer to the device structure for the driver instance.
dataData to send.
lengthNumber of bytes to write (max. 8).
idIdentifier of the can message.
rtrSend remote transmission request or data frame
timeoutWaiting for empty tx mailbox timeout or K_FOREVER
Return values
0If successful.
-EIOGeneral input / output error.
-EINVALif length > 8.