Zephyr API Documentation
3.0.0
A Scalable Open Source RTOS
|
|
3.0.0 |
CAN Interface. More...
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... | |
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... | |
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... | |
CAN controller configuration | |
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... | |
#define | CAN_SJW_NO_CHANGE 0 |
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... | |
Receiving CAN frames | |
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... | |
#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... | |
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 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 Interface.
#define CAN_EXT_ID_MASK 0x1FFFFFFFU |
#include <include/drivers/can.h>
Bit mask for an extended (29-bit) CAN identifier.
#define CAN_MAX_DLC 8U |
#include <include/drivers/can.h>
Maximum data length code for CAN 2.0A/2.0B.
#define CAN_MAX_EXT_ID CAN_EXT_ID_MASK |
#include <include/drivers/can.h>
Maximum value for an extended (29-bit) CAN identifier.
#define CAN_MAX_STD_ID CAN_STD_ID_MASK |
#include <include/drivers/can.h>
Maximum value for a standard (11-bit) CAN identifier.
#define CAN_MSGQ_DEFINE | ( | name, | |
max_frames | |||
) | K_MSGQ_DEFINE(name, sizeof(struct zcan_frame), max_frames, 4) |
#include <include/drivers/can.h>
Statically define and initialize a CAN RX message queue.
The message queue's ring buffer contains space for max_frames CAN frames.
name | Name of the message queue. |
max_frames | Maximum number of CAN frames that can be queued. |
#define CAN_SJW_NO_CHANGE 0 |
#include <include/drivers/can.h>
Synchronization Jump Width (SJW) value to indicate that the SJW should not be changed by the timing calculation.
#define CAN_STD_ID_MASK 0x7FFU |
#include <include/drivers/can.h>
Bit mask for a standard (11-bit) CAN identifier.
#define CANFD_MAX_DLC CONFIG_CANFD_MAX_DLC |
#include <include/drivers/can.h>
Maximum data length code for CAN-FD.
can_rx_callback_t |
#include <include/drivers/can.h>
Defines the application callback handler function signature for receiving.
frame | Received frame. |
user_data | User data provided when the filter was added. |
can_state_change_callback_t |
#include <include/drivers/can.h>
Defines the state change callback handler function signature.
state | State of the CAN controller. |
err_cnt | CAN controller error counter values. |
user_data | User data provided the callback was set. |
can_tx_callback_t |
#include <include/drivers/can.h>
Defines the application callback handler function signature.
error | Status of the performed send operation. See the list of return values for can_send() for value descriptions. |
user_data | User data provided when the frame was sent. |
#include <include/drivers/can.h>
CAN Identifier structure for Linux SocketCAN compatibility.
The fields in this type are:
enum can_ide |
#include <include/drivers/can.h>
Defines if the CAN frame has a standard (11-bit) or extended (29-bit) CAN identifier.
Enumerator | |
---|---|
CAN_STANDARD_IDENTIFIER | Standard (11-bit) CAN identifier. |
CAN_EXTENDED_IDENTIFIER | Extended (29-bit) CAN identifier. |
enum can_mode |
#include <include/drivers/can.h>
Defines the mode of the CAN controller.
enum can_rtr |
#include <include/drivers/can.h>
Defines if the CAN frame is a data frame or a Remote Transmission Request (RTR) frame.
Enumerator | |
---|---|
CAN_DATAFRAME | Data frame. |
CAN_REMOTEREQUEST | Remote Transmission Request (RTR) frame. |
enum can_state |
#include <include/drivers/can.h>
Defines the state of the CAN bus.
|
inlinestatic |
#include <include/drivers/can.h>
Add a callback function for a given CAN filter.
Add a callback to CAN identifiers specified by a filter. When a recevied CAN frame matching the filter is received by the CAN controller, the callback function is called in interrupt context.
If a frame matches more than one attached filter, the priority of the match is hardware dependent.
The same callback function can be used for multiple filters.
dev | Pointer to the device structure for the driver instance. |
callback | This function is called by the CAN controller driver whenever a frame matching the filter is received. |
user_data | User data to pass to callback function. |
filter | Pointer to a zcan_filter structure defining the filter. |
filter_id | on success. |
-ENOSPC | if there are no free filters. |
int can_add_rx_filter_msgq | ( | const struct device * | dev, |
struct k_msgq * | msgq, | ||
const struct zcan_filter * | filter | ||
) |
#include <include/drivers/can.h>
Wrapper function for adding a message queue for a given filter.
Wrapper function for can_add_rx_filter() which puts received CAN frames matching the filter in a message queue instead of calling a callback.
If a frame matches more than one attached filter, the priority of the match is hardware dependent.
The same message queue can be used for multiple filters.
dev | Pointer to the device structure for the driver instance. |
msgq | Pointer to the already initialized k_msgq struct. |
filter | Pointer to a zcan_filter structure defining the filter. |
filter_id | on success. |
-ENOSPC | if there are no free filters. |
#include <include/drivers/can.h>
Convert from number of bytes to Data Length Code (DLC)
num_bytes | Number of bytes. |
Data | Length Code (DLC). |
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. The sjw, prop_seg, phase_seg1 and phase_seg2 must be given.
The returned bitrate error is reminder of the devision of the clock rate by the bitrate times the timing segments.
dev | Pointer to the device structure for the driver instance. |
timing | Result is written into the can_timing struct provided. |
bitrate | Target bitrate. |
0 | or positive bitrate error. |
Negative | error code on error. |
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 reached for the given parameters, -EINVAL is returned.
sample_pnt
will not always be matched perfectly. The algorithm calculates the best possible match.dev | Pointer to the device structure for the driver instance. | |
[out] | 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. |
0 | or positive sample point error on success. |
-EINVAL | if there is no solution for the desired values. |
-EIO | if can_get_core_clock() is not available. |
int can_calc_timing_data | ( | const struct device * | dev, |
struct can_timing * | res, | ||
uint32_t | bitrate, | ||
uint16_t | sample_pnt | ||
) |
#include <include/drivers/can.h>
Calculate timing parameters for the data phase.
Same as can_calc_timing() but with the maximum and minimum values from the data phase.
embed:rst:inline :kconfig:`CONFIG_CAN_FD_MODE`must be selected for this function to be available.
dev | Pointer to the device structure for the driver instance. | |
[out] | res | Result is written into the can_timing struct provided. |
bitrate | Target bitrate for the data phase in bits/s | |
sample_pnt | Sampling point for the data phase in permille of the entire bit time. |
0 | or positive sample point error on success. |
-EINVAL | if there is no solution for the desired values. |
-EIO | if can_get_core_clock() is not available. |
|
inlinestatic |
#include <include/drivers/can.h>
Translate a can_filter struct to a zcan_filter struct.
filter | Pointer to can_filter struct. |
zfilter | Pointer to zcan_filter struct. |
|
inlinestatic |
#include <include/drivers/can.h>
Translate a can_frame struct to a zcan_frame struct.
frame | Pointer to can_frame struct. |
zframe | Pointer to zcan_frame struct. |
|
inlinestatic |
#include <include/drivers/can.h>
Translate a zcan_filter struct to a can_filter struct.
zfilter | Pointer to zcan_filter struct. |
filter | Pointer to can_filter struct. |
|
inlinestatic |
#include <include/drivers/can.h>
Translate a zcan_frame struct to a can_frame struct.
zframe | Pointer to zcan_frame struct. |
frame | Pointer to can_frame struct. |
#include <include/drivers/can.h>
Convert from Data Length Code (DLC) to the number of data bytes.
dlc | Data Length Code (DLC). |
Number | of bytes. |
#include <include/drivers/can.h>
Get the CAN core clock rate.
Returns the CAN core clock rate. One time quantum is 1/(core clock rate).
dev | Pointer to the device structure for the driver instance. | |
[out] | rate | CAN core clock rate in Hz. |
#include <include/drivers/can.h>
Get maximum number of RX filters.
Get the maximum number of concurrent RX filters for the CAN controller.
dev | Pointer to the device structure for the driver instance. |
id_type | CAN identifier type (standard or extended). |
Positive | number of maximum concurrent filters. |
-EIO | General input/output error. |
-ENOSYS | If this function is not implemented by the driver. |
int can_get_state | ( | const struct device * | dev, |
enum can_state * | state, | ||
struct can_bus_err_cnt * | err_cnt | ||
) |
#include <include/drivers/can.h>
Get current CAN controller state.
Returns the current state and optionally the error counter values of the CAN controller.
dev | Pointer to the device structure for the driver instance. | |
[out] | state | Pointer to the state destination enum or NULL. |
[out] | err_cnt | Pointer to the err_cnt destination structure or NULL. |
0 | If successful. |
-EIO | General input/output error, failed to get state. |
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.
embed:rst:inline :kconfig:`CONFIG_CAN_AUTO_BUS_OFF_RECOVERY`must be deselected for this function to be available.
dev | Pointer to the device structure for the driver instance. |
timeout | Timeout for waiting for the recovery or K_FOREVER . |
0 | on success. |
-EAGAIN | on timeout. |
void can_remove_rx_filter | ( | const struct device * | dev, |
int | filter_id | ||
) |
#include <include/drivers/can.h>
Remove a CAN RX filter.
This routine removes a CAN RX filter based on the filter ID returned by can_add_rx_filter() or can_add_rx_filter_msgq().
dev | Pointer to the device structure for the driver instance. |
filter_id | Filter ID |
int can_send | ( | const struct device * | dev, |
const struct zcan_frame * | frame, | ||
k_timeout_t | timeout, | ||
can_tx_callback_t | callback, | ||
void * | user_data | ||
) |
#include <include/drivers/can.h>
Transmit a CAN frame on the CAN bus.
Transmit a CAN frame on the CAN bus with optional timeout and completion callback function.
By default, the CAN controller will automatically retry transmission in case of lost bus arbitration or missing acknowledge. Some CAN controllers support disabling automatic retransmissions ("one-shot" mode) via a devicetree property.
dev | Pointer to the device structure for the driver instance. |
frame | CAN frame to transmit. |
timeout | Timeout waiting for a empty TX mailbox or K_FOREVER . |
callback | Optional callback for when the frame was sent or a transmission error occurred. If NULL , this function is blocking until frame is sent. The callback must be NULL if called from user mode. |
user_data | User data to pass to callback function. |
0 | if successful. |
-EINVAL | if an invalid parameter was passed to the function. |
-ENETDOWN | if the CAN controller is in bus-off state. |
-EBUSY | if CAN bus arbitration was lost (only applicable if automatic retransmissions are disabled). |
-EIO | if a general transmit error occurred (e.g. missing ACK if automatic retransmissions are disabled). |
-EAGAIN | on timeout. |
|
inlinestatic |
#include <include/drivers/can.h>
Set the bitrate of the CAN controller.
The sample point is set to the CiA DS 301 recommended value of 87.5%.
bitrate_data
is only relevant for CAN-FD. If the controller does not support CAN-FD or if embed:rst:inline :kconfig:`CONFIG_CAN_FD_MODE`is not selected, the value of this parameter is ignored.
dev | Pointer to the device structure for the driver instance. |
bitrate | Desired arbitration phase bitrate. |
bitrate_data | Desired data phase bitrate. |
0 | If successful. |
-EINVAL | bitrate cannot be met. |
-EIO | General input/output error, failed to set bitrate. |
#include <include/drivers/can.h>
Set the CAN controller to the given operation mode.
dev | Pointer to the device structure for the driver instance. |
mode | Operation mode. |
0 | If successful. |
-EIO | General input/output error, failed to configure device. |
|
inlinestatic |
#include <include/drivers/can.h>
Set a callback for CAN controller state change events.
Set the callback for CAN controller state change events. The callback function will be called in interrupt context.
Only one callback can be registered per controller. Calling this function again overrides any previously registered callback.
dev | Pointer to the device structure for the driver instance. |
callback | Callback function. |
user_data | User data to pass to callback function. |
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 the bus timing of a CAN controller.
If the sjw equals CAN_SJW_NO_CHANGE, the sjw parameter is not changed.
timing_data
is only relevant for CAN-FD. If the controller does not support CAN-FD or if embed:rst:inline :kconfig:`CONFIG_CAN_FD_MODE`is not selected, the value of this parameter is ignored.
dev | Pointer to the device structure for the driver instance. |
timing | Bus timings. |
timing_data | Bus timings for data phase (CAN-FD only). |
0 | If successful. |
-EIO | General input/output error, failed to configure device. |