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.h File Reference
#include <zephyr/types.h>
#include <device.h>
#include <string.h>
#include <sys/util.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_frame
 CAN frame for Linux SocketCAN compatibility. More...
 
struct  can_filter
 CAN filter for Linux SocketCAN compatibility. More...
 

Macros

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

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

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)
 Transmit a CAN frame 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_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...
 

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