Controller Area Network (CAN)

Overview

Controller Area Network is a two-wire serial bus specified by the Bosch CAN Specification, Bosch CAN with Flexible Data-Rate specification and the ISO 11898-1:2003 standard. CAN is mostly known for its application in the automotive domain. However, it is also used in home and industrial automation and other products.

A CAN transceiver is an external device that converts the logic level signals from the CAN controller to the bus-levels. The bus lines are called CAN High (CAN H) and CAN Low (CAN L). The transmit wire from the controller to the transceiver is called CAN TX, and the receive wire is called CAN RX. These wires use the logic levels whereas the bus-level is interpreted differentially between CAN H and CAN L. The bus can be either in the recessive (logical one) or dominant (logical zero) state. The recessive state is when both lines, CAN H and CAN L, at roughly at the same voltage level. This state is also the idle state. To write a dominant bit to the bus, open-drain transistors tie CAN H to Vdd and CAN L to ground. The first and last node use a 120-ohm resistor between CAN H and CAN L to terminate the bus. The dominant state always overrides the recessive state. This structure is called a wired-AND.

Warning

CAN controllers can only initialize when the bus is in the idle (recessive) state for at least 11 recessive bits. Therefore you have to make sure that CAN RX is high, at least for a short time. This is also necessary for loopback mode.

CAN Transceiver

The bit-timing as defined in ISO 11898-1:2003 looks as following:

CAN Timing

A single bit is split into four segments.

  • Sync_Seg: The nodes synchronize at the edge of the Sync_Seg. It is always one time quantum in length.

  • Prop_Seg: The signal propagation delay of the bus and other delays of the transceiver and node.

  • Phase_Seg1 and Phase_Seg2 :Define the sampling point. The bit is sampled at the end of Phase_Seg1.

The bit-rate is calculated from the time of a time quantum and the values defined above. A bit has the length of Sync_Seg plus Prop_Seg plus Phase_Seg1 plus Phase_Seg2 multiplied by the time of single time quantum. The bit-rate is the inverse of the length of a single bit.

A bit is sampled at the sampling point. The sample point is between Phase_Seg1 and PhaseSeg2 and therefore is a parameter that the user needs to choose. The CiA recommends setting the sample point to 87.5% of the bit.

The resynchronization jump width (SJW) defines the amount of time quantum the sample point can be moved. The sample point is moved when resynchronization is needed.

The timing parameters (SJW, bitrate and sampling point, or bitrate, Prop_Seg, Phase_Seg1and Phase_Seg2) are initially set from the device-tree and can be changed at run-time from the timing-API.

CAN uses so-called identifiers to identify the frame instead of addresses to identify a node. This identifier can either have 11-bit width (Standard or Basic Frame) or 29-bit in case of an Extended Frame. The Zephyr CAN API supports both Standard and Extended identifiers concurrently. A CAN frame starts with a dominant Start Of Frame bit. After that, the identifiers follow. This phase is called the arbitration phase. During the arbitration phase, write collisions are allowed. They resolve by the fact that dominant bits override recessive bits. Nodes monitor the bus and notice when their transmission is being overridden and in case, abort their transmission. This effectively gives lower number identifiers priority over higher number identifiers.

Filters are used to whitelist identifiers that are of interest for the specific node. An identifier that doesn’t match any filter is ignored. Filters can either match exactly or a specified part of the identifier. This method is called masking. As an example, a mask with 11 bits set for standard or 29 bits set for extended identifiers must match perfectly. Bits that are set to zero in the mask are ignored when matching an identifier. Most CAN controllers implement a limited number of filters in hardware. The number of filters is also limited in Kconfig to save memory.

Errors may occur during transmission. In case a node detects an erroneous frame, it partially overrides the current frame with an error-frame. Error-frames can either be error passive or error active, depending on the state of the controller. In case the controller is in error active state, it sends six consecutive dominant bits, which is a violation of the stuffing rule that all nodes can detect. The sender may resend the frame right after.

An initialized node can be in one of the following states:

  • Error-active

  • Error-passive

  • Bus-off

After initialization, the node is in the error-active state. In this state, the node is allowed to send active error frames, ACK, and overload frames. Every node has a receive- and transmit-error counter. If either the receive- or the transmit-error counter exceeds 127, the node changes to error-passive state. In this state, the node is not allowed to send error-active frames anymore. If the transmit-error counter increases further to 255, the node changes to the bus-off state. In this state, the node is not allowed to send any dominant bits to the bus. Nodes in the bus-off state may recover after receiving 128 occurrences of 11 concurrent recessive bits.

You can read more about CAN bus in this CAN Wikipedia article.

Zephyr supports following CAN features:

  • Standard and Extended Identifers

  • Filters with Masking

  • Loopback and Silent mode

  • Remote Request

Sending

The following code snippets show how to send data.

This basic sample sends a CAN frame with standard identifier 0x123 and eight bytes of data. When passing NULL as the callback, as shown in this example, the send function blocks until the frame is sent and acknowledged by at least one other node or an error occurred. The timeout only takes effect on acquiring a mailbox. When a transmitting mailbox is assigned, sending cannot be canceled.

struct zcan_frame frame = {
        .id_type = CAN_STANDARD_IDENTIFIER,
        .rtr = CAN_DATAFRAME,
        .id = 0x123,
        .dlc = 8,
        .data = {1,2,3,4,5,6,7,8}
};
const struct device *can_dev;
int ret;

can_dev = device_get_binding("CAN_0");

ret = can_send(can_dev, &frame, K_MSEC(100), NULL, NULL);
if (ret != CAN_TX_OK) {
        LOG_ERR("Sending failed [%d]", ret);
}

This example shows how to send a frame with extended identifier 0x1234567 and two bytes of data. The provided callback is called when the message is sent, or an error occurred. Passing K_FOREVER to the timeout causes the function to block until a transfer mailbox is assigned to the frame or an error occurred. It does not block until the message is sent like the example above.

void tx_irq_callback(uint32_t error_flags, void *arg)
{
        char *sender = (char *)arg;

        if (error_flags) {
                LOG_ERR("Sendig failed [%d]\nSender: %s\n", error_flags, sender);
        }
}

int send_function(const struct device *can_dev)
{
        struct zcan_frame frame = {
                .id_type = CAN_EXTENDED_IDENTIFIER,
                .rtr = CAN_DATAFRAME,
                .id = 0x1234567,
                .dlc = 2
        };

        frame.data[0] = 1;
        frame.data[1] = 2;

        return can_send(can_dev, &frame, K_FOREVER, tx_irq_callback, "Sender 1");
}

Receiving

Frames are only received when they match a filter. The following code snippets show how to receive frames by attaching filters.

Here we have an example for a receiving callback. It is used for can_attach_isr or can_attach_workq. The argument arg is passed when the filter is attached.

void rx_callback_function(struct zcan_frame *frame, void *arg)
{
        ... do something with the frame ...
}

The following snippet shows how to attach a filter with an interrupt callback. It is the most efficient but also the most critical way to receive messages. The callback function is called from an interrupt context, which means that the callback function should be as short as possible and must not block. Attaching ISRs is not allowed from userspace context.

The filter for this example is configured to match the identifier 0x123 exactly.

const struct zcan_filter my_filter = {
        .id_type = CAN_STANDARD_IDENTIFIER,
        .rtr = CAN_DATAFRAME,
        .id = 0x123,
        .rtr_mask = 1,
        .id_mask = CAN_STD_ID_MASK
};
int filter_id;
const struct device *can_dev;

can_dev = device_get_binding("CAN_0");

filter_id = can_attach_isr(can_dev, rx_callback_function, callback_arg, &my_filter);
if (filter_id < 0) {
  LOG_ERR("Unable to attach isr [%d]", filter_id);
}

This example shows how to attach a callback from a work-queue. In contrast to the can_attach_isr function, here the callback is called from the work-queue provided. In this case, it is the system work queue. Blocking is generally allowed in the callback but could result in a frame backlog when it is not limited. For the reason of a backlog, a ring-buffer is applied for every attached filter. The size of this buffer can be adjusted in Kconfig. This function is not yet callable from userspace context but will be in the future.

The filter for this example is configured to match a filter range from 0x120 to x12f.

const struct zcan_filter my_filter = {
        .id_type = CAN_STANDARD_IDENTIFIER,
        .rtr = CAN_DATAFRAME,
        .id = 0x120,
        .rtr_mask = 1,
        .id_mask = 0x7F0
};
struct zcan_work rx_work;
int filter_id;
const struct device *can_dev;

can_dev = device_get_binding("CAN_0");

filter_id = can_attach_workq(can_dev, &k_sys_work_q, &rx_work, callback_arg, callback_arg, &my_filter);
if (filter_id < 0) {
  LOG_ERR("Unable to attach isr [%d]", filter_id);
}

Here an example for can_attach_msgq is shown. With this function, it is possible to receive frames synchronously. This function can be called from userspace context. The size of the message queue should be as big as the expected backlog.

The filter for this example is configured to match the extended identifier 0x1234567 exactly.

const struct zcan_filter my_filter = {
        .id_type = CAN_EXTENDED_IDENTIFIER,
        .rtr = CAN_DATAFRAME,
        .id = 0x1234567,
        .rtr_mask = 1,
        .id_mask = CAN_EXT_ID_MASK
};
CAN_DEFINE_MSGQ(my_can_msgq, 2);
struct zcan_frame rx_frame;
int filter_id;
const struct device *can_dev;

can_dev = device_get_binding("CAN_0");

filter_id = can_attach_msgq(can_dev, &my_can_msgq, &my_filter);
if (filter_id < 0) {
  LOG_ERR("Unable to attach isr [%d]", filter_id);
  return;
}

while (true) {
  k_msgq_get(&my_can_msgq, &rx_frame, K_FOREVER);
  ... do something with the frame ...
}

can_detach removes the given filter.

can_detach(can_dev, filter_id);

Setting the bitrate

The bitrate and sampling point is initially set at runtime. To change it from the application, one can use the can_set_timing API. This function takes three arguments. The first timing parameter sets the timing for classic CAN and arbitration phase for CAN-FD. The second parameter sets the timing of the data phase for CAN-FD. For classic CAN, you can use only the first parameter and put NULL to the second one. The can_calc_timing function can calculate timing from a bitrate and sampling point in permille. The following example sets the bitrate to 250k baud with the sampling point at 87.5%.

struct can_timing timing;
const struct device *can_dev;
int ret;

can_dev = device_get_binding("CAN_0");

ret = can_calc_timing(can_dev, &timing, 250000, 875);
if (ret > 0) {
  LOG_INF("Sample-Point error: %d", ret);
}

if (ret < 0) {
  LOG_ERR("Failed to calc a valid timing");
  return;
}

ret = can_set_timing(can_dev, &timing, NULL);
if (ret != 0) {
  LOG_ERR("Failed to set timing");
}

SocketCAN

Zephyr additionally supports SocketCAN, a BSD socket implementation of the Zephyr CAN API. SocketCAN brings the convenience of the well-known BSD Socket API to Controller Area Networks. It is compatible with the Linux SocketCAN implementation, where many other high-level CAN projects build on top. Note that frames are routed to the network stack instead of passed directly, which adds some computation and memory overhead.

Samples

We have two ready-to-build samples demonstrating use of the Zephyr CAN API Zephyr CAN sample and SocketCAN sample.

API Reference

group can_interface

CAN Interface.

Defines

CAN_EX_ID
CAN_MAX_STD_ID
CAN_STD_ID_MASK
CAN_EXT_ID_MASK
CAN_MAX_DLC
CANFD_MAX_DLC
CAN_MAX_DLEN
CAN_TX_OK

send successfully

CAN_TX_ERR

general send error

CAN_TX_ARB_LOST

bus arbitration lost during sending

CAN_TX_BUS_OFF

controller is in bus off state

CAN_TX_UNKNOWN

unexpected error

CAN_TX_EINVAL

invalid parameter

CAN_NO_FREE_FILTER

attach_* failed because there is no unused filter left

CAN_TIMEOUT

operation timed out

CAN_DEFINE_MSGQ(name, size)

Statically define and initialize a can message queue.

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

Parameters
  • name – Name of the message queue.

  • size – Number of can messages.

CONFIG_CAN_WORKQ_FRAMES_BUF_CNT

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.

Parameters
  • error_flags – status of the performed send operation

  • arg – argument that was passed when the message was sent

typedef void (*can_rx_callback_t)(struct zcan_frame *msg, void *arg)

Define the application callback handler function signature for receiving.

Parameters
  • msg – received message

  • arg – argument that was passed when the filter was attached

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.

Parameters
  • state – state of the node

  • err_cnt – struct with the error counter values

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)

Enums

enum can_ide

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

Values:

enumerator CAN_STANDARD_IDENTIFIER
enumerator CAN_EXTENDED_IDENTIFIER
enum can_rtr

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

Values:

enumerator CAN_DATAFRAME
enumerator CAN_REMOTEREQUEST
enum can_mode

can_mode enum Defines the mode of the can controller

Values:

enumerator CAN_NORMAL_MODE
enumerator CAN_SILENT_MODE
enumerator CAN_LOOPBACK_MODE
enumerator CAN_SILENT_LOOPBACK_MODE
enum can_state

can_state enum Defines the possible states of the CAN bus

Values:

enumerator CAN_ERROR_ACTIVE
enumerator CAN_ERROR_PASSIVE
enumerator CAN_BUS_OFF
enumerator CAN_BUS_UNKNOWN

Functions

static inline uint8_t can_dlc_to_bytes(uint8_t dlc)

Convert the DLC to the number of bytes.

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

Parameters
  • dlc – The Data Length Code

Returns Number

of bytes

static inline uint8_t can_bytes_to_dlc(uint8_t num_bytes)

Convert a number of bytes to the DLC.

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

Parameters
  • num_bytes – The number of bytes

Returns The

DLC

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.

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

Parameters
  • dev – Pointer to the device structure for the driver instance.

  • msg – Message to transfer.

  • timeout – Waiting for empty tx mailbox timeout or K_FOREVER.

  • callback_isr – Is 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_arg – This will be passed whenever the isr is called.

Returns 0

If successful.

Returns CAN_TX_*

on failure.

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

This routine writes a set amount of data synchronously.

Parameters
  • dev – Pointer to the device structure for the driver instance.

  • data – Data to send.

  • length – Number of bytes to write (max. 8).

  • id – Identifier of the can message.

  • rtr – Send remote transmission request or data frame

  • timeout – Waiting for empty tx mailbox timeout or K_FOREVER

Returns 0

If successful.

Returns -EIO

General input / output error.

Returns -EINVAL

if length > 8.

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.

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
  • dev – Pointer to the device structure for the driver instance.

  • work_q – Pointer to the already initialized work queue.

  • work – Pointer to a zcan_work. The work will be initialized.

  • callback – This function is called by workq whenever a message arrives.

  • callback_arg – Is passed to the callback when called.

  • filter – Pointer to a zcan_filter structure defining the id filtering.

Returns filter_id

on success.

Returns CAN_NO_FREE_FILTER

if there is no filter left.

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.

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
  • dev – Pointer to the device structure for the driver instance.

  • msg_q – Pointer to the already initialized message queue.

  • filter – Pointer to a zcan_filter structure defining the id filtering.

Returns filter_id

on success.

Returns CAN_NO_FREE_FILTER

if there is no filter left.

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

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
  • dev – Pointer to the device structure for the driver instance.

  • isr – Callback function pointer.

  • callback_arg – This will be passed whenever the isr is called.

  • filter – Pointer to a zcan_filter structure defining the id filtering.

Returns filter_id

on success.

Returns CAN_NO_FREE_FILTER

if there is no filter left.

void can_detach(const struct device *dev, int filter_id)

Detach an isr or message queue from the identifier filtering.

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

Parameters
  • dev – Pointer to the device structure for the driver instance.

  • filter_id – filter id returned by can_attach_isr or can_attach_msgq.

Returns none

int can_get_core_clock(const struct device *dev, uint32_t *rate)

Read the core clock value.

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

Parameters
  • dev – Pointer to the device structure for the driver instance.

  • rate[out] controller clock rate

Returns 0

on success

Returns negative

on error

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.

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
  • dev – Pointer to the device structure for the driver instance.

  • res – Result is written into the can_timing struct provided.

  • bitrate – Target bitrate in bits/s

  • sample_pnt – Sampling point in permill of the entire bit time.

Returns Positive

sample point error on success

Returns -EINVAL

if there is no solution for the desired values

Returns -EIO

if core_clock is not available

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.

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
  • dev – Pointer to the device structure for the driver instance.

  • timing – Result is written into the can_timing struct provided.

  • bitrate – Target bitrate.

Returns bitrate

error

Returns negative

on error

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

Set the controller to the given mode.

Parameters
  • dev – Pointer to the device structure for the driver instance.

  • mode – Operation mode

Returns 0

If successful.

Returns -EIO

General input / output error, failed to configure device.

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.

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
  • dev – Pointer to the device structure for the driver instance.

  • timing – Bus timings

  • timing_data – Bus timings for data phase (CAN-FD only)

Returns 0

If successful.

Returns -EIO

General input / output error, failed to configure device.

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

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
  • dev – Pointer to the device structure for the driver instance.

  • bitrate – Desired arbitration phase bitrate

  • bitrate_data – Desired data phase bitrate

Returns 0

If successful.

Returns -EINVAL

bitrate cannot be reached.

Returns -EIO

General input / output error, failed to set bitrate.

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

Configure operation of a host controller.

Parameters
  • dev – Pointer to the device structure for the driver instance.

  • mode – Operation mode

  • bitrate – bus-speed in Baud/s

Returns 0

If successful.

Returns -EIO

General input / output error, failed to configure device.

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

Get current state.

Returns the actual state of the CAN controller.

Parameters
  • dev – Pointer to the device structure for the driver instance.

  • err_cnt – Pointer to the err_cnt destination structure or NULL.

Returns state

int can_recover(const struct device *dev, k_timeout_t timeout)

Recover from bus-off state.

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

Parameters
  • dev – Pointer to the device structure for the driver instance.

  • timeout – Timeout for waiting for the recovery or K_FOREVER.

Returns 0

on success.

Returns CAN_TIMEOUT

on timeout.

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

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
  • dev – Pointer to the device structure for the driver instance.

  • isr – Pointer to ISR

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

Parameters
static inline 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.

Parameters
static inline 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.

Parameters
  • filter – Pointer to can_filter struct.

  • zfilter – Pointer to zcan_frame_filter struct.

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

Parameters
struct can_frame
#include <can.h>

CAN frame structure that is compatible with Linux. This is mainly used by Socket CAN code.

Used to pass CAN messages from userspace to the socket CAN and vice versa.

Public Members

canid_t can_id

32 bit CAN_ID + EFF/RTR/ERR flags

uint8_t can_dlc

The length of the message

uint8_t data[8]

The message data

struct can_filter
#include <can.h>

CAN filter that is compatible with Linux. This is mainly used by Socket CAN code.

A filter matches, when “received_can_id & mask == can_id & mask”

struct zcan_frame
#include <can.h>

CAN message structure.

Used to pass can messages from userspace to the driver and from driver to userspace

Public Members

uint32_t id

Message identifier

uint32_t fd

Frame is in the CAN-FD frame format

uint32_t rtr

Set the message to a transmission request instead of data frame use can_rtr enum for assignment

uint32_t id_type

Indicates the identifier type (standard or extended) use can_ide enum for assignment

uint8_t dlc

The length of the message (max. 8) in byte

uint8_t brs

Baud Rate Switch. Frame transfer with different timing during the data phase. Only valid for CAN-FD

uint8_t res

Reserved for future flags

union zcan_frame.[anonymous] [anonymous]

The frame payload data.

struct zcan_filter
#include <can.h>

CAN filter structure.

Used to pass can identifier filter information to the driver. rtr_mask and *_id_mask are used to mask bits of the rtr and id fields. If the mask bit is 0, the value of the corresponding bit in the id or rtr field don’t care for the filter matching.

Public Members

uint32_t id

target state of the identifier

uint32_t rtr

target state of the rtr bit

uint32_t id_type

Indicates the identifier type (standard or extended) use can_ide enum for assignment

uint32_t id_mask

identifier mask

uint32_t rtr_mask

rtr bit mask

struct can_bus_err_cnt
#include <can.h>

can bus error count structure

Used to pass the bus error counters to userspace

struct can_timing
#include <can.h>

canbus timings

Used to pass bus timing values to the config and bitrate calculator function.

The propagation segment represents the time of the signal propagation. Phase segment 1 and phase segment 2 define the sampling point. prop_seg and phase_seg1 affect the sampling-point in the same way and some controllers only have a register for the sum of those two. The sync segment always has a length of 1 tq +——+——-+———+———+ |sync_seg | prop_seg | phase_seg1 | phase_seg2 | +——+——-+———+———+ ^ Sampling-Point 1 tq (time quantum) has the length of 1/(core_clock / prescaler) The bitrate is defined by the core clock divided by the prescaler and the sum of the segments. br = (core_clock / prescaler) / (1 + prop_seg + phase_seg1 + phase_seg2) The resynchronization jump width (SJW) defines the amount of time quantum the sample point can be moved. The sample point is moved when resynchronization is needed.

Public Members

uint16_t sjw

Synchronisation jump width

uint16_t prop_seg

Propagation Segment

uint16_t phase_seg1

Phase Segment 1

uint16_t phase_seg2

Phase Segment 2

uint16_t prescaler

Prescaler value

struct can_frame_buffer
#include <can.h>
struct zcan_work
#include <can.h>

CAN work structure.

Used to attach a work queue to a filter.

struct can_driver_api
#include <can.h>