Zephyr Project API  3.1.0
A Scalable Open Source RTOS
can.h File Reference
#include <zephyr/types.h>
#include <zephyr/device.h>
#include <string.h>
#include <zephyr/sys/util.h>
#include <zephyr/stats/stats.h>
#include <syscalls/can.h>

Go to the source code of this file.

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_device_state
 CAN specific device state which allows for CAN device class specific additions. More...
 
struct  can_frame
 CAN frame for Linux SocketCAN compatibility. More...
 
struct  can_filter
 CAN filter for Linux SocketCAN compatibility. More...
 

Macros

#define CAN_STATS_BIT0_ERROR_INC(dev_)    STATS_INC(Z_CAN_GET_STATS(dev_), bit0_error)
 Increment the bit0 error counter for a CAN device. More...
 
#define CAN_STATS_BIT1_ERROR_INC(dev_)    STATS_INC(Z_CAN_GET_STATS(dev_), bit1_error)
 Increment the bit1 (recessive) error counter for a CAN device. More...
 
#define CAN_STATS_STUFF_ERROR_INC(dev_)    STATS_INC(Z_CAN_GET_STATS(dev_), stuff_error)
 Increment the stuffing error counter for a CAN device. More...
 
#define CAN_STATS_CRC_ERROR_INC(dev_)    STATS_INC(Z_CAN_GET_STATS(dev_), crc_error)
 Increment the CRC error counter for a CAN device. More...
 
#define CAN_STATS_FORM_ERROR_INC(dev_)    STATS_INC(Z_CAN_GET_STATS(dev_), form_error)
 Increment the form error counter for a CAN device. More...
 
#define CAN_STATS_ACK_ERROR_INC(dev_)    STATS_INC(Z_CAN_GET_STATS(dev_), ack_error)
 Increment the acknowledge error counter for a CAN device. More...
 
#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm_device, data_ptr, cfg_ptr, level, prio, api_ptr, ...)
 Like DEVICE_DT_DEFINE() with CAN device specifics. More...
 
#define CAN_DEVICE_DT_INST_DEFINE(inst, ...)    CAN_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
 Like CAN_DEVICE_DT_DEFINE() for an instance of a DT_DRV_COMPAT compatible. 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...
 
CAN controller mode flags

#define CAN_MODE_NORMAL   0
 
#define CAN_MODE_LOOPBACK   BIT(0)
 
#define CAN_MODE_LISTENONLY   BIT(1)
 
#define CAN_MODE_FD   BIT(2)
 

Typedefs

typedef uint32_t can_mode_t
 Provides a type to hold CAN controller configuration flags. More...
 
typedef void(* can_tx_callback_t) (const struct device *dev, int error, void *user_data)
 Defines the application callback handler function signature. More...
 
typedef void(* can_rx_callback_t) (const struct device *dev, struct zcan_frame *frame, void *user_data)
 Defines the application callback handler function signature for receiving. More...
 
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. More...
 

Enumerations

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

Functions

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)
 Queue a CAN frame for transmission on the CAN bus. 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 controller configuration

#define CAN_SJW_NO_CHANGE   0
 
int can_get_core_clock (const struct device *dev, uint32_t *rate)
 Get the CAN core clock rate. More...
 
int can_get_max_bitrate (const struct device *dev, uint32_t *max_bitrate)
 Get maximum supported bitrate. More...
 
const struct can_timingcan_get_timing_min (const struct device *dev)
 Get the minimum supported timing parameter values. More...
 
const struct can_timingcan_get_timing_max (const struct device *dev)
 Get the maximum supported timing parameter values. 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...
 
const struct can_timingcan_get_timing_data_min (const struct device *dev)
 Get the minimum supported timing parameter values for the data phase. More...
 
const struct can_timingcan_get_timing_data_max (const struct device *dev)
 Get the maximum supported timing parameter values for the data phase. 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_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. More...
 
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. 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)
 Configure the bus timing of a CAN controller. More...
 
int can_set_mode (const struct device *dev, can_mode_t mode)
 Set the CAN controller to the given operation mode. More...
 
int can_set_bitrate (const struct device *dev, uint32_t bitrate)
 Set the bitrate of the CAN controller. More...
 

Receiving CAN frames

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

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