9#ifndef ZEPHYR_INCLUDE_DRIVERS_CAN_H_
10#define ZEPHYR_INCLUDE_DRIVERS_CAN_H_
40#define CAN_STD_ID_MASK 0x7FFU
44#define CAN_MAX_STD_ID CAN_STD_ID_MASK
48#define CAN_EXT_ID_MASK 0x1FFFFFFFU
52#define CAN_MAX_EXT_ID CAN_EXT_ID_MASK
60#define CANFD_MAX_DLC 15U
66#ifndef CONFIG_CAN_FD_MODE
67#define CAN_MAX_DLEN 8U
69#define CAN_MAX_DLEN 64U
84#define CAN_MODE_NORMAL 0
87#define CAN_MODE_LOOPBACK BIT(0)
90#define CAN_MODE_LISTENONLY BIT(1)
93#define CAN_MODE_FD BIT(2)
96#define CAN_MODE_ONE_SHOT BIT(3)
99#define CAN_MODE_3_SAMPLES BIT(4)
137#define CAN_FRAME_IDE BIT(0)
140#define CAN_FRAME_RTR BIT(1)
143#define CAN_FRAME_FDF BIT(2)
146#define CAN_FRAME_BRS BIT(3)
151#define CAN_FRAME_ESI BIT(4)
168#if defined(CONFIG_CAN_RX_TIMESTAMP) || defined(__DOXYGEN__)
197#define CAN_FILTER_IDE BIT(0)
200#define CAN_FILTER_RTR BIT(1)
203#define CAN_FILTER_DATA BIT(2)
206#define CAN_FILTER_FDF BIT(3)
325typedef int (*can_set_timing_t)(
const struct device *dev,
332typedef int (*can_set_timing_data_t)(
const struct device *dev,
339typedef int (*can_get_capabilities_t)(
const struct device *dev,
can_mode_t *cap);
345typedef int (*can_start_t)(
const struct device *dev);
351typedef int (*can_stop_t)(
const struct device *dev);
366typedef int (*can_send_t)(
const struct device *dev,
375typedef int (*can_add_rx_filter_t)(
const struct device *dev,
384typedef void (*can_remove_rx_filter_t)(
const struct device *dev,
int filter_id);
403typedef void(*can_set_state_change_callback_t)(
const struct device *dev,
411typedef int (*can_get_core_clock_t)(
const struct device *dev,
uint32_t *rate);
417typedef int (*can_get_max_filters_t)(
const struct device *dev,
bool ide);
423typedef int (*can_get_max_bitrate_t)(
const struct device *dev,
uint32_t *max_bitrate);
425__subsystem
struct can_driver_api {
426 can_get_capabilities_t get_capabilities;
429 can_set_mode_t set_mode;
430 can_set_timing_t set_timing;
432 can_add_rx_filter_t add_rx_filter;
433 can_remove_rx_filter_t remove_rx_filter;
434#if !defined(CONFIG_CAN_AUTO_BUS_OFF_RECOVERY) || defined(__DOXYGEN__)
435 can_recover_t recover;
437 can_get_state_t get_state;
438 can_set_state_change_callback_t set_state_change_callback;
439 can_get_core_clock_t get_core_clock;
440 can_get_max_filters_t get_max_filters;
441 can_get_max_bitrate_t get_max_bitrate;
446#if defined(CONFIG_CAN_FD_MODE) || defined(__DOXYGEN__)
447 can_set_timing_data_t set_timing_data;
457#if defined(CONFIG_CAN_STATS) || defined(__DOXYGEN__)
499#define Z_CAN_GET_STATS(dev_) \
500 CONTAINER_OF(dev_->state, struct can_device_state, devstate)->stats
512#define CAN_STATS_BIT0_ERROR_INC(dev_) \
513 STATS_INC(Z_CAN_GET_STATS(dev_), bit0_error)
523#define CAN_STATS_BIT1_ERROR_INC(dev_) \
524 STATS_INC(Z_CAN_GET_STATS(dev_), bit1_error)
534#define CAN_STATS_STUFF_ERROR_INC(dev_) \
535 STATS_INC(Z_CAN_GET_STATS(dev_), stuff_error)
545#define CAN_STATS_CRC_ERROR_INC(dev_) \
546 STATS_INC(Z_CAN_GET_STATS(dev_), crc_error)
556#define CAN_STATS_FORM_ERROR_INC(dev_) \
557 STATS_INC(Z_CAN_GET_STATS(dev_), form_error)
567#define CAN_STATS_ACK_ERROR_INC(dev_) \
568 STATS_INC(Z_CAN_GET_STATS(dev_), ack_error)
579#define CAN_STATS_RX_OVERRUN_INC(dev_) \
580 STATS_INC(Z_CAN_GET_STATS(dev_), rx_overrun)
590#define CAN_STATS_RESET(dev_) \
591 stats_reset(&(Z_CAN_GET_STATS(dev_).s_hdr))
598#define Z_CAN_DEVICE_STATE_DEFINE(dev_id) \
599 static struct can_device_state Z_DEVICE_STATE_NAME(dev_id) \
600 __attribute__((__section__(".z_devstate")))
608#define Z_CAN_INIT_FN(dev_id, init_fn) \
609 static inline int UTIL_CAT(dev_id, _init)(const struct device *dev) \
611 struct can_device_state *state = \
612 CONTAINER_OF(dev->state, struct can_device_state, devstate); \
613 stats_init(&state->stats.s_hdr, STATS_SIZE_32, 7, \
614 STATS_NAME_INIT_PARMS(can)); \
615 stats_register(dev->name, &(state->stats.s_hdr)); \
616 if (init_fn != NULL) { \
617 return init_fn(dev); \
645#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
647 Z_CAN_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
648 Z_CAN_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn) \
649 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
650 DEVICE_DT_NAME(node_id), \
651 &UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init), \
652 pm, data, config, level, prio, api, \
653 &(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
658#define CAN_STATS_BIT0_ERROR_INC(dev_)
659#define CAN_STATS_BIT1_ERROR_INC(dev_)
660#define CAN_STATS_STUFF_ERROR_INC(dev_)
661#define CAN_STATS_CRC_ERROR_INC(dev_)
662#define CAN_STATS_FORM_ERROR_INC(dev_)
663#define CAN_STATS_ACK_ERROR_INC(dev_)
664#define CAN_STATS_RX_OVERRUN_INC(dev_)
665#define CAN_STATS_RESET(dev_)
667#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
669 DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
670 prio, api, __VA_ARGS__)
681#define CAN_DEVICE_DT_INST_DEFINE(inst, ...) \
682 CAN_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
702static inline int z_impl_can_get_core_clock(
const struct device *dev,
uint32_t *rate)
704 const struct can_driver_api *api = (
const struct can_driver_api *)dev->
api;
706 return api->get_core_clock(dev, rate);
723static inline int z_impl_can_get_max_bitrate(
const struct device *dev,
uint32_t *max_bitrate)
725 const struct can_driver_api *api = (
const struct can_driver_api *)dev->
api;
727 if (api->get_max_bitrate == NULL) {
731 return api->get_max_bitrate(dev, max_bitrate);
743static inline const struct can_timing *z_impl_can_get_timing_min(
const struct device *dev)
745 const struct can_driver_api *api = (
const struct can_driver_api *)dev->
api;
747 return &api->timing_min;
759static inline const struct can_timing *z_impl_can_get_timing_max(
const struct device *dev)
761 const struct can_driver_api *api = (
const struct can_driver_api *)dev->
api;
763 return &api->timing_max;
805#ifdef CONFIG_CAN_FD_MODE
806static inline const struct can_timing *z_impl_can_get_timing_data_min(
const struct device *dev)
808 const struct can_driver_api *api = (
const struct can_driver_api *)dev->
api;
810 return &api->timing_data_min;
829#ifdef CONFIG_CAN_FD_MODE
830static inline const struct can_timing *z_impl_can_get_timing_data_max(
const struct device *dev)
832 const struct can_driver_api *api = (
const struct can_driver_api *)dev->
api;
834 return &api->timing_data_max;
960static inline int z_impl_can_get_capabilities(
const struct device *dev,
can_mode_t *cap)
962 const struct can_driver_api *api = (
const struct can_driver_api *)dev->
api;
964 return api->get_capabilities(dev, cap);
984static inline int z_impl_can_start(
const struct device *dev)
986 const struct can_driver_api *api = (
const struct can_driver_api *)dev->
api;
988 return api->start(dev);
1008static inline int z_impl_can_stop(
const struct device *dev)
1010 const struct can_driver_api *api = (
const struct can_driver_api *)dev->
api;
1012 return api->stop(dev);
1027static inline int z_impl_can_set_mode(
const struct device *dev,
can_mode_t mode)
1029 const struct can_driver_api *api = (
const struct can_driver_api *)dev->
api;
1031 return api->set_mode(dev, mode);
1149 void *user_data,
const struct can_filter *filter)
1151 const struct can_driver_api *api = (
const struct can_driver_api *)dev->
api;
1157 return api->add_rx_filter(dev, callback, user_data, filter);
1170#define CAN_MSGQ_DEFINE(name, max_frames) \
1171 K_MSGQ_DEFINE(name, sizeof(struct can_frame), max_frames, 4)
1213static inline void z_impl_can_remove_rx_filter(
const struct device *dev,
int filter_id)
1215 const struct can_driver_api *api = (
const struct can_driver_api *)dev->
api;
1217 return api->remove_rx_filter(dev, filter_id);
1235static inline int z_impl_can_get_max_filters(
const struct device *dev,
bool ide)
1237 const struct can_driver_api *api = (
const struct can_driver_api *)dev->
api;
1239 if (api->get_max_filters == NULL) {
1243 return api->get_max_filters(dev, ide);
1273 const struct can_driver_api *api = (
const struct can_driver_api *)dev->
api;
1275 return api->get_state(dev,
state, err_cnt);
1293#if !defined(CONFIG_CAN_AUTO_BUS_OFF_RECOVERY) || defined(__DOXYGEN__)
1296static inline int z_impl_can_recover(
const struct device *dev,
k_timeout_t timeout)
1298 const struct can_driver_api *api = (
const struct can_driver_api *)dev->
api;
1300 return api->recover(dev, timeout);
1304static inline int z_impl_can_recover(
const struct device *dev,
k_timeout_t timeout)
1307 ARG_UNUSED(timeout);
1329 const struct can_driver_api *api = (
const struct can_driver_api *)dev->
api;
1331 api->set_state_change_callback(dev, callback, user_data);
1351 static const uint8_t dlc_table[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12,
1352 16, 20, 24, 32, 48, 64};
1366 return num_bytes <= 8 ? num_bytes :
1367 num_bytes <= 12 ? 9 :
1368 num_bytes <= 16 ? 10 :
1369 num_bytes <= 20 ? 11 :
1370 num_bytes <= 24 ? 12 :
1371 num_bytes <= 32 ? 13 :
1372 num_bytes <= 48 ? 14 :
1416 if ((frame->
id ^ filter->
id) & filter->
mask) {
1434#include <syscalls/can.h>
static ssize_t send(int sock, const void *buf, size_t len, int flags)
POSIX wrapper for zsock_send.
Definition: socket.h:803
void(* can_state_change_callback_t)(const struct device *dev, enum can_state state, struct can_bus_err_cnt err_cnt, void *user_data)
Defines the state change callback handler function signature.
Definition: can.h:310
int can_set_bitrate(const struct device *dev, uint32_t bitrate)
Set the bitrate of the CAN controller.
int can_set_bitrate_data(const struct device *dev, uint32_t bitrate_data)
Set the bitrate for the data phase of the CAN-FD controller.
uint32_t can_mode_t
Provides a type to hold CAN controller configuration flags.
Definition: can.h:111
static int can_add_rx_filter(const struct device *dev, can_rx_callback_t callback, void *user_data, const struct can_filter *filter)
Add a callback function for a given CAN filter.
Definition: can.h:1148
#define CAN_FILTER_FDF
Filter matches CAN-FD frames (FDF)
Definition: can.h:206
int can_stop(const struct device *dev)
Stop the CAN controller.
int can_set_timing(const struct device *dev, const struct can_timing *timing)
Configure the bus timing of a CAN controller.
#define CAN_FRAME_FDF
Frame uses CAN-FD format (FDF)
Definition: can.h:143
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.
int can_send(const struct device *dev, const struct can_frame *frame, k_timeout_t timeout, can_tx_callback_t callback, void *user_data)
Queue a CAN frame for transmission on the CAN bus.
const struct can_timing * can_get_timing_data_max(const struct device *dev)
Get the maximum supported timing parameter values for the data phase.
int can_get_core_clock(const struct device *dev, uint32_t *rate)
Get the CAN core clock rate.
int can_get_capabilities(const struct device *dev, can_mode_t *cap)
Get the supported modes of the CAN controller.
int can_get_max_filters(const struct device *dev, bool ide)
Get maximum number of RX filters.
const struct can_timing * can_get_timing_min(const struct device *dev)
Get the minimum supported timing parameter values.
int can_set_timing_data(const struct device *dev, const struct can_timing *timing_data)
Configure the bus timing for the data phase of a CAN-FD controller.
const struct can_timing * can_get_timing_data_min(const struct device *dev)
Get the minimum supported timing parameter values for the data phase.
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.
void can_remove_rx_filter(const struct device *dev, int filter_id)
Remove a CAN RX filter.
static uint8_t can_bytes_to_dlc(uint8_t num_bytes)
Convert from number of bytes to Data Length Code (DLC)
Definition: can.h:1364
int can_get_max_bitrate(const struct device *dev, uint32_t *max_bitrate)
Get maximum supported bitrate.
#define CAN_FILTER_RTR
Filter matches Remote Transmission Request (RTR) frames.
Definition: can.h:200
#define CAN_FRAME_IDE
Frame uses extended (29-bit) CAN ID.
Definition: can.h:137
static uint8_t can_dlc_to_bytes(uint8_t dlc)
Convert from Data Length Code (DLC) to the number of data bytes.
Definition: can.h:1349
#define CAN_FILTER_DATA
Filter matches data frames.
Definition: can.h:203
int can_add_rx_filter_msgq(const struct device *dev, struct k_msgq *msgq, const struct can_filter *filter)
Simple wrapper function for adding a message queue for a given filter.
void(* can_rx_callback_t)(const struct device *dev, struct can_frame *frame, void *user_data)
Defines the application callback handler function signature for receiving.
Definition: can.h:299
static bool can_frame_matches_filter(const struct can_frame *frame, const struct can_filter *filter)
Check if a CAN frame matches a CAN filter.
Definition: can.h:1383
void(* can_tx_callback_t)(const struct device *dev, int error, void *user_data)
Defines the application callback handler function signature.
Definition: can.h:290
int can_get_state(const struct device *dev, enum can_state *state, struct can_bus_err_cnt *err_cnt)
Get current CAN controller state.
const struct can_timing * can_get_timing_max(const struct device *dev)
Get the maximum supported timing parameter values.
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.
int can_recover(const struct device *dev, k_timeout_t timeout)
Recover from bus-off state.
can_state
Defines the state of the CAN controller.
Definition: can.h:116
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.
Definition: can.h:1325
int can_set_mode(const struct device *dev, can_mode_t mode)
Set the CAN controller to the given operation mode.
int can_start(const struct device *dev)
Start the CAN controller.
#define CAN_FRAME_RTR
Frame is a Remote Transmission Request (RTR)
Definition: can.h:140
#define CAN_FILTER_IDE
Filter matches frames with extended (29-bit) CAN IDs.
Definition: can.h:197
@ CAN_STATE_ERROR_ACTIVE
Error-active state (RX/TX error count < 96).
Definition: can.h:118
@ CAN_STATE_ERROR_WARNING
Error-warning state (RX/TX error count < 128).
Definition: can.h:120
@ CAN_STATE_STOPPED
CAN controller is stopped and does not participate in CAN communication.
Definition: can.h:126
@ CAN_STATE_BUS_OFF
Bus-off state (RX/TX error count >= 256).
Definition: can.h:124
@ CAN_STATE_ERROR_PASSIVE
Error-passive state (RX/TX error count < 256).
Definition: can.h:122
#define MIN(a, b)
Obtain the minimum of two values.
Definition: util.h:341
#define ARRAY_SIZE(array)
Number of elements in the given array.
Definition: util.h:124
#define DIV_ROUND_UP(n, d)
Divide and round up.
Definition: util.h:286
#define EINVAL
Invalid argument.
Definition: errno.h:61
#define ENOSYS
Function not implemented.
Definition: errno.h:83
Variables needed for system clock.
state
Definition: parser_state.h:29
#define STATS_NAME_END(name__)
Definition: stats.h:391
#define STATS_NAME(name__, entry__)
Definition: stats.h:390
#define STATS_SECT_END
Ends a stats group struct definition.
Definition: stats.h:89
#define STATS_SECT_ENTRY32(var__)
Definition: stats.h:359
#define STATS_NAME_START(name__)
Definition: stats.h:389
#define STATS_SECT_START(group__)
Definition: stats.h:354
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
CAN controller error counters.
Definition: can.h:230
uint8_t tx_err_cnt
Value of the CAN controller transmit error counter.
Definition: can.h:232
uint8_t rx_err_cnt
Value of the CAN controller receive error counter.
Definition: can.h:234
CAN specific device state which allows for CAN device class specific additions.
Definition: can.h:489
struct device_state devstate
Definition: can.h:490
struct stats_can stats
Definition: can.h:491
CAN filter structure.
Definition: can.h:213
uint32_t mask
CAN identifier matching mask.
Definition: can.h:222
uint8_t flags
Flags.
Definition: can.h:224
uint32_t id
CAN identifier to match.
Definition: can.h:215
CAN frame structure.
Definition: can.h:158
uint8_t dlc
Data Length Code (DLC) indicating data length in bytes.
Definition: can.h:165
uint8_t flags
Flags.
Definition: can.h:167
uint32_t id
Standard (11-bit) or extended (29-bit) CAN identifier.
Definition: can.h:160
uint8_t data[CAN_MAX_DLEN]
Definition: can.h:184
uint32_t data_32[DIV_ROUND_UP(CAN_MAX_DLEN, sizeof(uint32_t))]
Definition: can.h:185
uint16_t timestamp
Captured value of the free-running timer in the CAN controller when this frame was received.
Definition: can.h:176
CAN bus timing structure.
Definition: can.h:269
uint16_t sjw
Synchronisation jump width.
Definition: can.h:271
uint16_t phase_seg2
Phase segment 2.
Definition: can.h:277
uint16_t prescaler
Prescaler value.
Definition: can.h:279
uint16_t phase_seg1
Phase segment 1.
Definition: can.h:275
uint16_t prop_seg
Propagation segment.
Definition: can.h:273
Runtime device dynamic structure (in RAM) per driver instance.
Definition: device.h:354
Runtime device structure (in ROM) per driver instance.
Definition: device.h:381
const void * api
Address of the API structure exposed by the device instance.
Definition: device.h:387
Message Queue Structure.
Definition: kernel.h:4406
Kernel timeout type.
Definition: sys_clock.h:65