13#ifndef ZEPHYR_INCLUDE_DRIVERS_CAN_H_
14#define ZEPHYR_INCLUDE_DRIVERS_CAN_H_
32#define CAN_EX_ID (1 << 31)
33#define CAN_MAX_STD_ID (0x7FF)
34#define CAN_STD_ID_MASK CAN_MAX_STD_ID
35#define CAN_EXT_ID_MASK (0x1FFFFFFF)
36#define CAN_MAX_DLC (8)
37#define CANFD_MAX_DLC CONFIG_CANFD_MAX_DLC
38#ifndef CONFIG_CANFD_MAX_DLC
41#if CONFIG_CANFD_MAX_DLC <= 8
42#define CAN_MAX_DLEN CONFIG_CANFD_MAX_DLC
43#elif CONFIG_CANFD_MAX_DLC <= 12
44#define CAN_MAX_DLEN CONFIG_CANFD_MAX_DLC + (CONFIG_CANFD_MAX_DLC - 8) * 4
45#elif CONFIG_CANFD_MAX_DLC == 13
46#define CAN_MAX_DLEN 32
47#elif CONFIG_CANFD_MAX_DLC == 14
48#define CAN_MAX_DLEN 48
49#elif CONFIG_CANFD_MAX_DLC == 15
50#define CAN_MAX_DLEN 64
58#define CAN_TX_ERR (-2)
60#define CAN_TX_ARB_LOST (-3)
62#define CAN_TX_BUS_OFF (-4)
64#define CAN_TX_UNKNOWN (-5)
67#define CAN_TX_EINVAL (-22)
70#define CAN_NO_FREE_FILTER (-1)
73#define CAN_TIMEOUT (-1)
83#define CAN_DEFINE_MSGQ(name, size) \
84 K_MSGQ_DEFINE(name, sizeof(struct zcan_frame), size, 4)
206#if defined(CONFIG_CAN_RX_TIMESTAMP)
263#define CAN_SJW_NO_CHANGE 0
363#ifndef CONFIG_CAN_WORKQ_FRAMES_BUF_CNT
364#define CONFIG_CAN_WORKQ_FRAMES_BUF_CNT 4
391#ifndef CONFIG_CAN_AUTO_BUS_OFF_RECOVERY
401#ifdef CONFIG_CAN_FD_MODE
420 static const uint8_t dlc_table[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12,
421 16, 20, 24, 32, 48, 64};
423 return dlc > 0x0F ? 64 : dlc_table[dlc];
438 return num_bytes <= 8 ? num_bytes :
439 num_bytes <= 12 ? 9 :
440 num_bytes <= 16 ? 10 :
441 num_bytes <= 20 ? 11 :
442 num_bytes <= 24 ? 12 :
443 num_bytes <= 32 ? 13 :
444 num_bytes <= 48 ? 14 :
470static inline int z_impl_can_send(
const struct device *dev,
479 return api->
send(dev, msg,
timeout, callback_isr, callback_arg);
604 return api->
attach_isr(dev, isr, callback_arg, filter);
620static inline void z_impl_can_detach(
const struct device *dev,
int filter_id)
625 return api->
detach(dev, filter_id);
641static inline int z_impl_can_get_core_clock(
const struct device *dev,
672#ifdef CONFIG_CAN_FD_MODE
720static inline int z_impl_can_set_mode(
const struct device *dev,
749static inline int z_impl_can_set_timing(
const struct device *dev,
756 return api->
set_timing(dev, timing, timing_data);
780#ifdef CONFIG_CAN_FD_MODE
792#ifdef CONFIG_CAN_FD_MODE
793 ret = can_calc_timing_data(dev, &timing_data, bitrate_data, 875);
864#ifndef CONFIG_CAN_AUTO_BUS_OFF_RECOVERY
867static inline int z_impl_can_recover(
const struct device *dev,
877static inline int z_impl_can_recover(
const struct device *dev,
964 (zfilter->
rtr << 30) | zfilter->
id;
975#include <syscalls/can.h>
ZTEST_BMEM int timeout
Definition: main.c:31
#define BIT_MASK(n)
Definition: adc.h:14
void
Definition: eswifi_shell.c:15
int can_set_mode(const struct device *dev, enum can_mode mode)
Set the controller to the given mode.
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.
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.
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)
Definition: can.h:336
#define CAN_SJW_NO_CHANGE
Definition: can.h:263
void(* can_rx_callback_t)(struct zcan_frame *msg, void *arg)
Define the application callback handler function signature for receiving.
Definition: can.h:318
int can_get_core_clock(const struct device *dev, uint32_t *rate)
Read the core clock value.
void(* can_tx_callback_t)(uint32_t error_flags, void *arg)
Define the application callback handler function signature.
Definition: can.h:308
int(* can_get_core_clock_t)(const struct device *dev, uint32_t *rate)
Definition: can.h:361
can_mode
can_mode enum Defines the mode of the can controller
Definition: can.h:109
int(* can_set_timing_t)(const struct device *dev, const struct can_timing *timing, const struct can_timing *timing_data)
Definition: can.h:330
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.
Definition: can.h:327
int(* can_attach_isr_t)(const struct device *dev, can_rx_callback_t isr, void *callback_arg, const struct zcan_filter *filter)
Definition: can.h:346
void(* can_detach_t)(const struct device *dev, int filter_id)
Definition: can.h:351
#define CAN_MAX_STD_ID
Definition: can.h:33
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.
static uint8_t can_bytes_to_dlc(uint8_t num_bytes)
Convert a number of bytes to the DLC.
Definition: can.h:436
can_rtr
can_rtr enum Define if the message is a data or remote frame
Definition: can.h:100
int(* can_recover_t)(const struct device *dev, k_timeout_t timeout)
Definition: can.h:353
enum can_state(* can_get_state_t)(const struct device *dev, struct can_bus_err_cnt *err_cnt)
Definition: can.h:355
can_ide
can_ide enum Define if the message has a standard (11bit) or extended (29bit) identifier
Definition: can.h:91
void(* can_register_state_change_isr_t)(const struct device *dev, can_state_change_isr_t isr)
Definition: can.h:358
int(* can_attach_msgq_t)(const struct device *dev, struct k_msgq *msg_q, const struct zcan_filter *filter)
Definition: can.h:342
static uint8_t can_dlc_to_bytes(uint8_t dlc)
Convert the DLC to the number of bytes.
Definition: can.h:418
enum can_state can_get_state(const struct device *dev, struct can_bus_err_cnt *err_cnt)
Get current state.
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.
int(* can_set_mode_t)(const struct device *dev, enum can_mode mode)
Definition: can.h:334
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
can_state enum Defines the possible states of the CAN bus
Definition: can.h:124
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.
Definition: can.h:894
static int can_configure(const struct device *dev, enum can_mode mode, uint32_t bitrate)
Configure operation of a host controller.
Definition: can.h:816
void can_detach(const struct device *dev, int filter_id)
Detach an isr or message queue from the identifier filtering.
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.
Definition: can.h:925
static int can_set_bitrate(const struct device *dev, uint32_t bitrate, uint32_t bitrate_data)
Set the bitrate of the CAN controller.
Definition: can.h:775
#define CONFIG_CAN_WORKQ_FRAMES_BUF_CNT
Definition: can.h:364
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.
Definition: can.h:596
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.
Definition: can.h:960
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.
uint32_t canid_t
Definition: can.h:141
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.
Definition: can.h:502
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.
Definition: can.h:909
#define CAN_MAX_DLEN
Definition: can.h:39
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.
Definition: can.h:942
@ CAN_SILENT_MODE
Definition: can.h:113
@ CAN_LOOPBACK_MODE
Definition: can.h:115
@ CAN_NORMAL_MODE
Definition: can.h:111
@ CAN_SILENT_LOOPBACK_MODE
Definition: can.h:117
@ CAN_DATAFRAME
Definition: can.h:101
@ CAN_REMOTEREQUEST
Definition: can.h:102
@ CAN_EXTENDED_IDENTIFIER
Definition: can.h:93
@ CAN_STANDARD_IDENTIFIER
Definition: can.h:92
@ CAN_ERROR_PASSIVE
Definition: can.h:126
@ CAN_ERROR_ACTIVE
Definition: can.h:125
@ CAN_BUS_OFF
Definition: can.h:127
@ CAN_BUS_UNKNOWN
Definition: can.h:128
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition: util_macro.h:44
#define ceiling_fraction(numerator, divider)
Ceiling function applied to numerator / divider as a fraction.
Definition: util.h:158
#define EINVAL
Definition: errno.h:61
state
Definition: http_parser_state.h:30
static ZTEST_BMEM volatile int ret
Definition: k_float_disable.c:28
static struct k_work work[2]
Definition: main.c:16
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
__UINT16_TYPE__ uint16_t
Definition: stdint.h:59
void * memcpy(void *_MLIBC_RESTRICT d, const void *_MLIBC_RESTRICT s, size_t n)
can bus error count structure
Definition: can.h:257
uint8_t tx_err_cnt
Definition: can.h:258
uint8_t rx_err_cnt
Definition: can.h:259
can_attach_isr_t attach_isr
Definition: can.h:389
can_set_timing_t set_timing
Definition: can.h:387
can_detach_t detach
Definition: can.h:390
struct can_timing timing_max
Definition: can.h:400
can_send_t send
Definition: can.h:388
can_get_state_t get_state
Definition: can.h:394
can_recover_t recover
Definition: can.h:392
can_register_state_change_isr_t register_state_change_isr
Definition: can.h:395
can_set_mode_t set_mode
Definition: can.h:386
can_get_core_clock_t get_core_clock
Definition: can.h:396
struct can_timing timing_min
Definition: can.h:398
CAN filter that is compatible with Linux. This is mainly used by Socket CAN code.
Definition: can.h:173
canid_t can_mask
Definition: can.h:175
canid_t can_id
Definition: can.h:174
uint16_t tail
Definition: can.h:369
uint16_t head
Definition: can.h:368
struct zcan_frame buf[4]
Definition: can.h:367
CAN frame structure that is compatible with Linux. This is mainly used by Socket CAN code.
Definition: can.h:150
uint8_t can_dlc
Definition: can.h:155
canid_t can_id
Definition: can.h:152
uint8_t data[8]
Definition: can.h:164
canbus timings
Definition: can.h:288
uint16_t sjw
Definition: can.h:290
uint16_t phase_seg2
Definition: can.h:296
uint16_t prescaler
Definition: can.h:298
uint16_t phase_seg1
Definition: can.h:294
uint16_t prop_seg
Definition: can.h:292
Runtime device structure (in ROM) per driver instance.
Definition: device.h:367
const void * api
Definition: device.h:373
Message Queue Structure.
Definition: kernel.h:4290
Kernel timeout type.
Definition: sys_clock.h:65
A structure used to hold work until it can be processed.
Definition: kernel.h:3770
A structure used to submit work.
Definition: kernel.h:3623
CAN filter structure.
Definition: can.h:234
uint32_t res0
Definition: can.h:237
uint32_t id_mask
Definition: can.h:245
uint32_t rtr
Definition: can.h:239
uint32_t rtr_mask
Definition: can.h:248
uint32_t res1
Definition: can.h:246
uint32_t id_type
Definition: can.h:243
uint32_t id
Definition: can.h:236
uint32_t res2
Definition: can.h:249
CAN message structure.
Definition: can.h:185
uint8_t dlc
Definition: can.h:199
uint32_t id_type
Definition: can.h:197
uint32_t data_32[ceiling_fraction(8, sizeof(uint32_t))]
Definition: can.h:221
uint8_t data[8]
Definition: can.h:220
uint32_t rtr
Definition: can.h:193
uint8_t res
Definition: can.h:205
uint8_t brs
Definition: can.h:203
uint32_t fd
Definition: can.h:189
uint32_t id
Definition: can.h:187
CAN work structure.
Definition: can.h:377
void * cb_arg
Definition: can.h:382
can_rx_callback_t cb
Definition: can.h:381
struct k_work_q * work_queue
Definition: can.h:379
struct can_frame_buffer buf
Definition: can.h:380
struct k_work work_item
Definition: can.h:378
static fdata_t data[2]
Definition: test_fifo_contexts.c:15