Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
can.h File Reference

Public APIs for the CAN drivers. More...

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

Public APIs for the CAN drivers.