Zephyr API Documentation
2.7.0-rc2
A Scalable Open Source RTOS
|
CAN Interface. More...
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... | |
CAN Interface.
#define CAN_DEFINE_MSGQ | ( | name, | |
size | |||
) | K_MSGQ_DEFINE(name, sizeof(struct zcan_frame), size, 4) |
#include <include/drivers/can.h>
Statically define and initialize a can message queue.
The message queue's ring buffer contains space for size messages.
name | Name of the message queue. |
size | Number of can messages. |
#define CAN_EX_ID (1 << 31) |
#include <include/drivers/can.h>
#define CAN_EXT_ID_MASK (0x1FFFFFFF) |
#include <include/drivers/can.h>
#define CAN_MAX_DLC (8) |
#include <include/drivers/can.h>
#define CAN_MAX_DLEN 8 |
#include <include/drivers/can.h>
#define CAN_MAX_STD_ID (0x7FF) |
#include <include/drivers/can.h>
#define CAN_NO_FREE_FILTER (-1) |
#include <include/drivers/can.h>
attach_* failed because there is no unused filter left
#define CAN_SJW_NO_CHANGE 0 |
#include <include/drivers/can.h>
SWJ value to indicate that the SJW should not be changed
#define CAN_STD_ID_MASK CAN_MAX_STD_ID |
#include <include/drivers/can.h>
#define CAN_TIMEOUT (-1) |
#include <include/drivers/can.h>
operation timed out
#define CAN_TX_ARB_LOST (-3) |
#include <include/drivers/can.h>
bus arbitration lost during sending
#define CAN_TX_BUS_OFF (-4) |
#include <include/drivers/can.h>
controller is in bus off state
#define CAN_TX_EINVAL (-22) |
#include <include/drivers/can.h>
invalid parameter
#define CAN_TX_ERR (-2) |
#include <include/drivers/can.h>
general send error
#define CAN_TX_OK (0) |
#include <include/drivers/can.h>
send successfully
#define CAN_TX_UNKNOWN (-5) |
#include <include/drivers/can.h>
unexpected error
#define CANFD_MAX_DLC CONFIG_CANFD_MAX_DLC |
#include <include/drivers/can.h>
#define CONFIG_CAN_WORKQ_FRAMES_BUF_CNT 4 |
#include <include/drivers/can.h>
typedef int(* can_attach_isr_t) (const struct device *dev, can_rx_callback_t isr, void *callback_arg, const struct zcan_filter *filter) |
#include <include/drivers/can.h>
typedef int(* can_attach_msgq_t) (const struct device *dev, struct k_msgq *msg_q, const struct zcan_filter *filter) |
#include <include/drivers/can.h>
#include <include/drivers/can.h>
#include <include/drivers/can.h>
typedef enum can_state(* can_get_state_t) (const struct device *dev, struct can_bus_err_cnt *err_cnt) |
#include <include/drivers/can.h>
typedef int(* can_recover_t) (const struct device *dev, k_timeout_t timeout) |
#include <include/drivers/can.h>
typedef void(* can_register_state_change_isr_t) (const struct device *dev, can_state_change_isr_t isr) |
#include <include/drivers/can.h>
can_rx_callback_t |
#include <include/drivers/can.h>
Define the application callback handler function signature for receiving.
msg | received message |
arg | argument that was passed when the filter was attached |
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) |
#include <include/drivers/can.h>
#include <include/drivers/can.h>
typedef int(* can_set_timing_t) (const struct device *dev, const struct can_timing *timing, const struct can_timing *timing_data) |
#include <include/drivers/can.h>
can_state_change_isr_t |
#include <include/drivers/can.h>
Defines the state change isr handler function signature.
state | state of the node |
err_cnt | struct with the error counter values |
can_tx_callback_t |
#include <include/drivers/can.h>
Define the application callback handler function signature.
error_flags | status of the performed send operation |
arg | argument that was passed when the message was sent |
#include <include/drivers/can.h>
enum can_ide |
#include <include/drivers/can.h>
can_ide enum Define if the message has a standard (11bit) or extended (29bit) identifier
Enumerator | |
---|---|
CAN_STANDARD_IDENTIFIER | |
CAN_EXTENDED_IDENTIFIER |
enum can_mode |
#include <include/drivers/can.h>
can_mode enum Defines the mode of the can controller
Enumerator | |
---|---|
CAN_NORMAL_MODE | |
CAN_SILENT_MODE | |
CAN_LOOPBACK_MODE | |
CAN_SILENT_LOOPBACK_MODE |
enum can_rtr |
#include <include/drivers/can.h>
can_rtr enum Define if the message is a data or remote frame
Enumerator | |
---|---|
CAN_DATAFRAME | |
CAN_REMOTEREQUEST |
enum can_state |
#include <include/drivers/can.h>
can_state enum Defines the possible states of the CAN bus
Enumerator | |
---|---|
CAN_ERROR_ACTIVE | |
CAN_ERROR_PASSIVE | |
CAN_BUS_OFF | |
CAN_BUS_UNKNOWN |
|
inlinestatic |
#include <include/drivers/can.h>
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.
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. |
filter_id | on success. |
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 | ||
) |
#include <include/drivers/can.h>
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.
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. |
filter_id | on success. |
CAN_NO_FREE_FILTER | if there is no filter left. |
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 | ||
) |
#include <include/drivers/can.h>
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.
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. |
filter_id | on success. |
CAN_NO_FREE_FILTER | if there is no filter left. |
#include <include/drivers/can.h>
Convert a number of bytes to the DLC.
This function converts a number of bytes to the Data Length Code
num_bytes | The number of bytes |
The | 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. 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.
dev | Pointer to the device structure for the driver instance. |
timing | Result is written into the can_timing struct provided. |
bitrate | Target bitrate. |
bitrate | error |
negative | 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 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.
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. |
Positive | sample point error on success |
-EINVAL | if there is no solution for the desired values |
-EIO | if core_clock is not available |
|
inlinestatic |
#include <include/drivers/can.h>
Configure operation of a host controller.
dev | Pointer to the device structure for the driver instance. |
mode | Operation mode |
bitrate | bus-speed in Baud/s |
0 | If successful. |
-EIO | General input / output error, failed to configure device. |
|
inlinestatic |
#include <include/drivers/can.h>
Converter that translates between can_filter and zcan_frame_filter structs.
filter | Pointer to can_filter struct. |
zfilter | Pointer to zcan_frame_filter struct. |
|
inlinestatic |
#include <include/drivers/can.h>
Converter that translates between can_frame and zcan_frame structs.
frame | Pointer to can_frame struct. |
zframe | Pointer to zcan_frame struct. |
|
inlinestatic |
#include <include/drivers/can.h>
Converter that translates between zcan_filter and can_filter structs.
zfilter | Pointer to zcan_filter struct. |
filter | Pointer to can_filter struct. |
|
inlinestatic |
#include <include/drivers/can.h>
Converter that translates between zcan_frame and can_frame structs.
zframe | Pointer to zcan_frame struct. |
frame | Pointer to can_frame struct. |
#include <include/drivers/can.h>
Detach an isr or message queue from the identifier filtering.
This routine detaches an isr callback or message queue from the identifier filtering.
dev | Pointer to the device structure for the driver instance. |
filter_id | filter id returned by can_attach_isr or can_attach_msgq. |
none |
#include <include/drivers/can.h>
Convert the DLC to the number of bytes.
This function converts a the Data Length Code to the number of bytes.
dlc | The Data Length Code |
Number | of bytes |
#include <include/drivers/can.h>
Read the core clock value.
Returns the core clock value. One time quantum is 1/core clock.
dev | Pointer to the device structure for the driver instance. | |
[out] | rate | controller clock rate |
0 | on success |
negative | on error |
enum can_state can_get_state | ( | const struct device * | dev, |
struct can_bus_err_cnt * | err_cnt | ||
) |
#include <include/drivers/can.h>
Get current state.
Returns the actual state of the CAN controller.
dev | Pointer to the device structure for the driver instance. |
err_cnt | Pointer to the err_cnt destination structure or NULL. |
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.
dev | Pointer to the device structure for the driver instance. |
timeout | Timeout for waiting for the recovery or K_FOREVER. |
0 | on success. |
CAN_TIMEOUT | on timeout. |
|
inlinestatic |
#include <include/drivers/can.h>
Register an ISR callback for state change interrupt.
Only one callback can be registered per controller. Calling this function again, overrides the previous call.
dev | Pointer to the device structure for the driver instance. |
isr | Pointer to ISR |
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 | ||
) |
#include <include/drivers/can.h>
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.
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. |
0 | If successful. |
CAN_TX_* | on failure. |
|
inlinestatic |
#include <include/drivers/can.h>
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%
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 reached. |
-EIO | General input / output error, failed to set bitrate. |
#include <include/drivers/can.h>
Set the controller to the given 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. |
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 timing of a host controller.
If the sjw equals CAN_SJW_NO_CHANGE, the sjw parameter is not changed.
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.
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. |
|
inlinestatic |
#include <include/drivers/can.h>
Write a set amount of data to the can bus.
This routine writes a set amount of data synchronously.
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 |
0 | If successful. |
-EIO | General input / output error. |
-EINVAL | if length > 8. |