Zephyr API Documentation 4.3.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
can.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 Vestas Wind Systems A/S
3 * Copyright (c) 2018 Karsten Koenig
4 * Copyright (c) 2018 Alexander Wachter
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
14
15#ifndef ZEPHYR_INCLUDE_DRIVERS_CAN_H_
16#define ZEPHYR_INCLUDE_DRIVERS_CAN_H_
17
18#include <errno.h>
19
20#include <zephyr/types.h>
21#include <zephyr/device.h>
22#include <zephyr/kernel.h>
23#include <string.h>
24#include <zephyr/sys_clock.h>
25#include <zephyr/sys/util.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
44
49
53#define CAN_STD_ID_MASK 0x7FFU
54
58#define CAN_EXT_ID_MASK 0x1FFFFFFFU
59
63#define CAN_MAX_DLC 8U
64
68#define CANFD_MAX_DLC 15U
69
74#ifndef CONFIG_CAN_FD_MODE
75#define CAN_MAX_DLEN 8U
76#else
77#define CAN_MAX_DLEN 64U
78#endif /* CONFIG_CAN_FD_MODE */
79
81
83
90
92#define CAN_MODE_NORMAL 0
93
95#define CAN_MODE_LOOPBACK BIT(0)
96
98#define CAN_MODE_LISTENONLY BIT(1)
99
101#define CAN_MODE_FD BIT(2)
102
104#define CAN_MODE_ONE_SHOT BIT(3)
105
107#define CAN_MODE_3_SAMPLES BIT(4)
108
110#define CAN_MODE_MANUAL_RECOVERY BIT(5)
111
113
123
139
146
148#define CAN_FRAME_IDE BIT(0)
149
151#define CAN_FRAME_RTR BIT(1)
152
154#define CAN_FRAME_FDF BIT(2)
155
157#define CAN_FRAME_BRS BIT(3)
158
162#define CAN_FRAME_ESI BIT(4)
163
165
169struct can_frame {
176#if defined(CONFIG_CAN_RX_TIMESTAMP) || defined(__DOXYGEN__)
185#else
188 uint16_t reserved;
190#endif
192 union {
194 uint8_t data[CAN_MAX_DLEN];
196 uint32_t data_32[DIV_ROUND_UP(CAN_MAX_DLEN, sizeof(uint32_t))];
197 };
198};
199
206
208#define CAN_FILTER_IDE BIT(0)
209
211
225
235
280
289typedef void (*can_tx_callback_t)(const struct device *dev, int error, void *user_data);
290
298typedef void (*can_rx_callback_t)(const struct device *dev, struct can_frame *frame,
299 void *user_data);
300
309typedef void (*can_state_change_callback_t)(const struct device *dev,
310 enum can_state state,
311 struct can_bus_err_cnt err_cnt,
312 void *user_data);
313
318
332#define CAN_CALC_TDCO(_timing_data, _tdco_min, _tdco_max) \
333 CLAMP((1U + _timing_data->prop_seg + _timing_data->phase_seg1) * _timing_data->prescaler, \
334 _tdco_min, _tdco_max)
335
344 const struct device *phy;
353#ifdef CONFIG_CAN_FD_MODE
355 uint16_t sample_point_data;
357 uint32_t bitrate_data;
358#endif /* CONFIG_CAN_FD_MODE */
359};
360
368#define CAN_DT_DRIVER_CONFIG_GET(node_id, _min_bitrate, _max_bitrate) \
369 { \
370 .phy = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(node_id, phys)), \
371 .min_bitrate = DT_CAN_TRANSCEIVER_MIN_BITRATE(node_id, _min_bitrate), \
372 .max_bitrate = DT_CAN_TRANSCEIVER_MAX_BITRATE(node_id, _max_bitrate), \
373 .bitrate = DT_PROP_OR(node_id, bitrate, \
374 DT_PROP_OR(node_id, bus_speed, CONFIG_CAN_DEFAULT_BITRATE)), \
375 .sample_point = DT_PROP_OR(node_id, sample_point, 0), \
376 IF_ENABLED(CONFIG_CAN_FD_MODE, \
377 (.bitrate_data = DT_PROP_OR(node_id, bitrate_data, \
378 DT_PROP_OR(node_id, bus_speed_data, CONFIG_CAN_DEFAULT_BITRATE_DATA)), \
379 .sample_point_data = DT_PROP_OR(node_id, sample_point_data, 0),)) \
380 }
381
390#define CAN_DT_DRIVER_CONFIG_INST_GET(inst, _min_bitrate, _max_bitrate) \
391 CAN_DT_DRIVER_CONFIG_GET(DT_DRV_INST(inst), _min_bitrate, _max_bitrate)
392
409
414typedef int (*can_set_timing_t)(const struct device *dev,
415 const struct can_timing *timing);
416
421typedef int (*can_set_timing_data_t)(const struct device *dev,
422 const struct can_timing *timing_data);
423
428typedef int (*can_get_capabilities_t)(const struct device *dev, can_mode_t *cap);
429
434typedef int (*can_start_t)(const struct device *dev);
435
440typedef int (*can_stop_t)(const struct device *dev);
441
446typedef int (*can_set_mode_t)(const struct device *dev, can_mode_t mode);
447
455typedef int (*can_send_t)(const struct device *dev,
456 const struct can_frame *frame,
457 k_timeout_t timeout, can_tx_callback_t callback,
458 void *user_data);
459
464typedef int (*can_add_rx_filter_t)(const struct device *dev,
465 can_rx_callback_t callback,
466 void *user_data,
467 const struct can_filter *filter);
468
473typedef void (*can_remove_rx_filter_t)(const struct device *dev, int filter_id);
474
479typedef int (*can_recover_t)(const struct device *dev, k_timeout_t timeout);
480
485typedef int (*can_get_state_t)(const struct device *dev, enum can_state *state,
486 struct can_bus_err_cnt *err_cnt);
487
492typedef void(*can_set_state_change_callback_t)(const struct device *dev,
494 void *user_data);
495
500typedef int (*can_get_core_clock_t)(const struct device *dev, uint32_t *rate);
501
506typedef int (*can_get_max_filters_t)(const struct device *dev, bool ide);
507
587
590
591#if defined(CONFIG_CAN_STATS) || defined(__DOXYGEN__)
592
593#include <zephyr/stats/stats.h>
594
596
598STATS_SECT_ENTRY32(bit_error)
599STATS_SECT_ENTRY32(bit0_error)
600STATS_SECT_ENTRY32(bit1_error)
601STATS_SECT_ENTRY32(stuff_error)
602STATS_SECT_ENTRY32(crc_error)
603STATS_SECT_ENTRY32(form_error)
604STATS_SECT_ENTRY32(ack_error)
605STATS_SECT_ENTRY32(rx_overrun)
607
609STATS_NAME(can, bit_error)
610STATS_NAME(can, bit0_error)
611STATS_NAME(can, bit1_error)
612STATS_NAME(can, stuff_error)
613STATS_NAME(can, crc_error)
614STATS_NAME(can, form_error)
615STATS_NAME(can, ack_error)
616STATS_NAME(can, rx_overrun)
617STATS_NAME_END(can);
618
620
629 struct stats_can stats;
630};
631
633
637#define Z_CAN_GET_STATS(dev_) \
638 CONTAINER_OF(dev_->state, struct can_device_state, devstate)->stats
639
641
658#define CAN_STATS_BIT_ERROR_INC(dev_) \
659 STATS_INC(Z_CAN_GET_STATS(dev_), bit_error)
660
672#define CAN_STATS_BIT0_ERROR_INC(dev_) \
673 do { \
674 STATS_INC(Z_CAN_GET_STATS(dev_), bit0_error); \
675 CAN_STATS_BIT_ERROR_INC(dev_); \
676 } while (0)
677
689#define CAN_STATS_BIT1_ERROR_INC(dev_) \
690 do { \
691 STATS_INC(Z_CAN_GET_STATS(dev_), bit1_error); \
692 CAN_STATS_BIT_ERROR_INC(dev_); \
693 } while (0)
694
703#define CAN_STATS_STUFF_ERROR_INC(dev_) \
704 STATS_INC(Z_CAN_GET_STATS(dev_), stuff_error)
705
714#define CAN_STATS_CRC_ERROR_INC(dev_) \
715 STATS_INC(Z_CAN_GET_STATS(dev_), crc_error)
716
725#define CAN_STATS_FORM_ERROR_INC(dev_) \
726 STATS_INC(Z_CAN_GET_STATS(dev_), form_error)
727
736#define CAN_STATS_ACK_ERROR_INC(dev_) \
737 STATS_INC(Z_CAN_GET_STATS(dev_), ack_error)
738
748#define CAN_STATS_RX_OVERRUN_INC(dev_) \
749 STATS_INC(Z_CAN_GET_STATS(dev_), rx_overrun)
750
759#define CAN_STATS_RESET(dev_) \
760 stats_reset(&(Z_CAN_GET_STATS(dev_).s_hdr))
761
763
767#define Z_CAN_DEVICE_STATE_DEFINE(dev_id) \
768 static struct can_device_state Z_DEVICE_STATE_NAME(dev_id) \
769 __attribute__((__section__(".z_devstate")))
770
777#define Z_CAN_INIT_FN(dev_id, init_fn) \
778 static inline int UTIL_CAT(dev_id, _init)(const struct device *dev) \
779 { \
780 struct can_device_state *state = \
781 CONTAINER_OF(dev->state, struct can_device_state, devstate); \
782 stats_init(&state->stats.s_hdr, STATS_SIZE_32, 8, \
783 STATS_NAME_INIT_PARMS(can)); \
784 stats_register(dev->name, &(state->stats.s_hdr)); \
785 if (!is_null_no_warn(init_fn)) { \
786 return init_fn(dev); \
787 } \
788 \
789 return 0; \
790 }
791
793
814#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
815 prio, api, ...) \
816 Z_CAN_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
817 Z_CAN_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn) \
818 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
819 DEVICE_DT_NAME(node_id), \
820 &UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init), \
821 NULL, Z_DEVICE_DT_FLAGS(node_id), pm, data, \
822 config, level, prio, api, \
823 &(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
824 __VA_ARGS__)
825
826#else /* CONFIG_CAN_STATS */
827
828#define CAN_STATS_BIT_ERROR_INC(dev_)
829#define CAN_STATS_BIT0_ERROR_INC(dev_)
830#define CAN_STATS_BIT1_ERROR_INC(dev_)
831#define CAN_STATS_STUFF_ERROR_INC(dev_)
832#define CAN_STATS_CRC_ERROR_INC(dev_)
833#define CAN_STATS_FORM_ERROR_INC(dev_)
834#define CAN_STATS_ACK_ERROR_INC(dev_)
835#define CAN_STATS_RX_OVERRUN_INC(dev_)
836#define CAN_STATS_RESET(dev_)
837
838#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
839 prio, api, ...) \
840 DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
841 prio, api, __VA_ARGS__)
842
843#endif /* CONFIG_CAN_STATS */
844
852#define CAN_DEVICE_DT_INST_DEFINE(inst, ...) \
853 CAN_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
854
860
873__syscall int can_get_core_clock(const struct device *dev, uint32_t *rate);
874
875static inline int z_impl_can_get_core_clock(const struct device *dev, uint32_t *rate)
876{
877 return DEVICE_API_GET(can, dev)->get_core_clock(dev, rate);
878}
879
896__syscall uint32_t can_get_bitrate_min(const struct device *dev);
897
898static inline uint32_t z_impl_can_get_bitrate_min(const struct device *dev)
899{
900 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
901
902 return common->min_bitrate;
903}
904
921__syscall uint32_t can_get_bitrate_max(const struct device *dev);
922
923static inline uint32_t z_impl_can_get_bitrate_max(const struct device *dev)
924{
925 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
926
927 return common->max_bitrate;
928}
929
937__syscall const struct can_timing *can_get_timing_min(const struct device *dev);
938
939static inline const struct can_timing *z_impl_can_get_timing_min(const struct device *dev)
940{
941 return &DEVICE_API_GET(can, dev)->timing_min;
942}
943
951__syscall const struct can_timing *can_get_timing_max(const struct device *dev);
952
953static inline const struct can_timing *z_impl_can_get_timing_max(const struct device *dev)
954{
955 return &DEVICE_API_GET(can, dev)->timing_max;
956}
957
984__syscall int can_calc_timing(const struct device *dev, struct can_timing *res,
985 uint32_t bitrate, uint16_t sample_pnt);
986
1000__syscall const struct can_timing *can_get_timing_data_min(const struct device *dev);
1001
1002#ifdef CONFIG_CAN_FD_MODE
1003static inline const struct can_timing *z_impl_can_get_timing_data_min(const struct device *dev)
1004{
1005 return &DEVICE_API_GET(can, dev)->timing_data_min;
1006}
1007#endif /* CONFIG_CAN_FD_MODE */
1008
1022__syscall const struct can_timing *can_get_timing_data_max(const struct device *dev);
1023
1024#ifdef CONFIG_CAN_FD_MODE
1025static inline const struct can_timing *z_impl_can_get_timing_data_max(const struct device *dev)
1026{
1027 return &DEVICE_API_GET(can, dev)->timing_data_max;
1028}
1029#endif /* CONFIG_CAN_FD_MODE */
1030
1051__syscall int can_calc_timing_data(const struct device *dev, struct can_timing *res,
1052 uint32_t bitrate, uint16_t sample_pnt);
1053
1071__syscall int can_set_timing_data(const struct device *dev,
1072 const struct can_timing *timing_data);
1073
1102__syscall int can_set_bitrate_data(const struct device *dev, uint32_t bitrate_data);
1103
1117__syscall int can_set_timing(const struct device *dev,
1118 const struct can_timing *timing);
1119
1133__syscall int can_get_capabilities(const struct device *dev, can_mode_t *cap);
1134
1135static inline int z_impl_can_get_capabilities(const struct device *dev, can_mode_t *cap)
1136{
1137 return DEVICE_API_GET(can, dev)->get_capabilities(dev, cap);
1138}
1139
1149__syscall const struct device *can_get_transceiver(const struct device *dev);
1150
1151static const struct device *z_impl_can_get_transceiver(const struct device *dev)
1152{
1153 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
1154
1155 return common->phy;
1156}
1157
1175__syscall int can_start(const struct device *dev);
1176
1177static inline int z_impl_can_start(const struct device *dev)
1178{
1179 return DEVICE_API_GET(can, dev)->start(dev);
1180}
1181
1197__syscall int can_stop(const struct device *dev);
1198
1199static inline int z_impl_can_stop(const struct device *dev)
1200{
1201 return DEVICE_API_GET(can, dev)->stop(dev);
1202}
1203
1214__syscall int can_set_mode(const struct device *dev, can_mode_t mode);
1215
1216static inline int z_impl_can_set_mode(const struct device *dev, can_mode_t mode)
1217{
1218 return DEVICE_API_GET(can, dev)->set_mode(dev, mode);
1219}
1220
1228__syscall can_mode_t can_get_mode(const struct device *dev);
1229
1230static inline can_mode_t z_impl_can_get_mode(const struct device *dev)
1231{
1232 const struct can_driver_data *common = (const struct can_driver_data *)dev->data;
1233
1234 return common->mode;
1235}
1236
1262__syscall int can_set_bitrate(const struct device *dev, uint32_t bitrate);
1263
1265
1271
1316__syscall int can_send(const struct device *dev, const struct can_frame *frame,
1317 k_timeout_t timeout, can_tx_callback_t callback,
1318 void *user_data);
1319
1321
1327
1352int can_add_rx_filter(const struct device *dev, can_rx_callback_t callback,
1353 void *user_data, const struct can_filter *filter);
1354
1365#define CAN_MSGQ_DEFINE(name, max_frames) \
1366 K_MSGQ_DEFINE(name, sizeof(struct can_frame), max_frames, 4)
1367
1396__syscall int can_add_rx_filter_msgq(const struct device *dev, struct k_msgq *msgq,
1397 const struct can_filter *filter);
1398
1408__syscall void can_remove_rx_filter(const struct device *dev, int filter_id);
1409
1410static inline void z_impl_can_remove_rx_filter(const struct device *dev, int filter_id)
1411{
1412 DEVICE_API_GET(can, dev)->remove_rx_filter(dev, filter_id);
1413}
1414
1428__syscall int can_get_max_filters(const struct device *dev, bool ide);
1429
1430static inline int z_impl_can_get_max_filters(const struct device *dev, bool ide)
1431{
1432 const struct can_driver_api *api = DEVICE_API_GET(can, dev);
1433
1434 if (api->get_max_filters == NULL) {
1435 return -ENOSYS;
1436 }
1437
1438 return api->get_max_filters(dev, ide);
1439}
1440
1442
1448
1462__syscall int can_get_state(const struct device *dev, enum can_state *state,
1463 struct can_bus_err_cnt *err_cnt);
1464
1465static inline int z_impl_can_get_state(const struct device *dev, enum can_state *state,
1466 struct can_bus_err_cnt *err_cnt)
1467{
1468 return DEVICE_API_GET(can, dev)->get_state(dev, state, err_cnt);
1469}
1470
1489__syscall int can_recover(const struct device *dev, k_timeout_t timeout);
1490
1491#ifdef CONFIG_CAN_MANUAL_RECOVERY_MODE
1492static inline int z_impl_can_recover(const struct device *dev, k_timeout_t timeout)
1493{
1494 const struct can_driver_api *api = DEVICE_API_GET(can, dev);
1495
1496 if (api->recover == NULL) {
1497 return -ENOSYS;
1498 }
1499
1500 return api->recover(dev, timeout);
1501}
1502#endif /* CONFIG_CAN_MANUAL_RECOVERY_MODE */
1503
1517static inline void can_set_state_change_callback(const struct device *dev,
1519 void *user_data)
1520{
1521 DEVICE_API_GET(can, dev)->set_state_change_callback(dev, callback, user_data);
1522}
1523
1525
1531
1544__syscall uint32_t can_stats_get_bit_errors(const struct device *dev);
1545
1546#ifdef CONFIG_CAN_STATS
1547static inline uint32_t z_impl_can_stats_get_bit_errors(const struct device *dev)
1548{
1549 return Z_CAN_GET_STATS(dev).bit_error;
1550}
1551#endif /* CONFIG_CAN_STATS */
1552
1567__syscall uint32_t can_stats_get_bit0_errors(const struct device *dev);
1568
1569#ifdef CONFIG_CAN_STATS
1570static inline uint32_t z_impl_can_stats_get_bit0_errors(const struct device *dev)
1571{
1572 return Z_CAN_GET_STATS(dev).bit0_error;
1573}
1574#endif /* CONFIG_CAN_STATS */
1575
1590__syscall uint32_t can_stats_get_bit1_errors(const struct device *dev);
1591
1592#ifdef CONFIG_CAN_STATS
1593static inline uint32_t z_impl_can_stats_get_bit1_errors(const struct device *dev)
1594{
1595 return Z_CAN_GET_STATS(dev).bit1_error;
1596}
1597#endif /* CONFIG_CAN_STATS */
1598
1611__syscall uint32_t can_stats_get_stuff_errors(const struct device *dev);
1612
1613#ifdef CONFIG_CAN_STATS
1614static inline uint32_t z_impl_can_stats_get_stuff_errors(const struct device *dev)
1615{
1616 return Z_CAN_GET_STATS(dev).stuff_error;
1617}
1618#endif /* CONFIG_CAN_STATS */
1619
1632__syscall uint32_t can_stats_get_crc_errors(const struct device *dev);
1633
1634#ifdef CONFIG_CAN_STATS
1635static inline uint32_t z_impl_can_stats_get_crc_errors(const struct device *dev)
1636{
1637 return Z_CAN_GET_STATS(dev).crc_error;
1638}
1639#endif /* CONFIG_CAN_STATS */
1640
1653__syscall uint32_t can_stats_get_form_errors(const struct device *dev);
1654
1655#ifdef CONFIG_CAN_STATS
1656static inline uint32_t z_impl_can_stats_get_form_errors(const struct device *dev)
1657{
1658 return Z_CAN_GET_STATS(dev).form_error;
1659}
1660#endif /* CONFIG_CAN_STATS */
1661
1674__syscall uint32_t can_stats_get_ack_errors(const struct device *dev);
1675
1676#ifdef CONFIG_CAN_STATS
1677static inline uint32_t z_impl_can_stats_get_ack_errors(const struct device *dev)
1678{
1679 return Z_CAN_GET_STATS(dev).ack_error;
1680}
1681#endif /* CONFIG_CAN_STATS */
1682
1696__syscall uint32_t can_stats_get_rx_overruns(const struct device *dev);
1697
1698#ifdef CONFIG_CAN_STATS
1699static inline uint32_t z_impl_can_stats_get_rx_overruns(const struct device *dev)
1700{
1701 return Z_CAN_GET_STATS(dev).rx_overrun;
1702}
1703#endif /* CONFIG_CAN_STATS */
1704
1706
1712
1721{
1722 static const uint8_t dlc_table[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12,
1723 16, 20, 24, 32, 48, 64};
1724
1725 return dlc_table[MIN(dlc, ARRAY_SIZE(dlc_table) - 1)];
1726}
1727
1735static inline uint8_t can_bytes_to_dlc(uint8_t num_bytes)
1736{
1737 return num_bytes <= 8 ? num_bytes :
1738 num_bytes <= 12 ? 9 :
1739 num_bytes <= 16 ? 10 :
1740 num_bytes <= 20 ? 11 :
1741 num_bytes <= 24 ? 12 :
1742 num_bytes <= 32 ? 13 :
1743 num_bytes <= 48 ? 14 :
1744 15;
1745}
1746
1754static inline bool can_frame_matches_filter(const struct can_frame *frame,
1755 const struct can_filter *filter)
1756{
1757 if ((frame->flags & CAN_FRAME_IDE) != 0 && (filter->flags & CAN_FILTER_IDE) == 0) {
1758 /* Extended (29-bit) ID frame, standard (11-bit) filter */
1759 return false;
1760 }
1761
1762 if ((frame->flags & CAN_FRAME_IDE) == 0 && (filter->flags & CAN_FILTER_IDE) != 0) {
1763 /* Standard (11-bit) ID frame, extended (29-bit) filter */
1764 return false;
1765 }
1766
1767 if ((frame->id ^ filter->id) & filter->mask) {
1768 /* Masked ID mismatch */
1769 return false;
1770 }
1771
1772 return true;
1773}
1774
1776
1780
1781#ifdef __cplusplus
1782}
1783#endif
1784
1785#include <zephyr/syscalls/can.h>
1786
1787#endif /* ZEPHYR_INCLUDE_DRIVERS_CAN_H_ */
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1353
System error numbers.
int(* can_set_timing_t)(const struct device *dev, const struct can_timing *timing)
Callback API upon setting CAN bus timing See can_set_timing() for argument description.
Definition can.h:414
int(* can_get_state_t)(const struct device *dev, enum can_state *state, struct can_bus_err_cnt *err_cnt)
Callback API upon getting the CAN controller state See can_get_state() for argument description.
Definition can.h:485
int(* can_stop_t)(const struct device *dev)
Callback API upon stopping CAN controller See can_stop() for argument description.
Definition can.h:440
void(* can_remove_rx_filter_t)(const struct device *dev, int filter_id)
Callback API upon removing an RX filter See can_remove_rx_filter() for argument description.
Definition can.h:473
int(* can_set_mode_t)(const struct device *dev, can_mode_t mode)
Callback API upon setting CAN controller mode See can_set_mode() for argument description.
Definition can.h:446
int(* can_get_capabilities_t)(const struct device *dev, can_mode_t *cap)
Callback API upon getting CAN controller capabilities See can_get_capabilities() for argument descrip...
Definition can.h:428
int(* can_add_rx_filter_t)(const struct device *dev, can_rx_callback_t callback, void *user_data, const struct can_filter *filter)
Callback API upon adding an RX filter See can_add_rx_callback() for argument description.
Definition can.h:464
int(* can_get_core_clock_t)(const struct device *dev, uint32_t *rate)
Callback API upon getting the CAN core clock rate See can_get_core_clock() for argument description.
Definition can.h:500
int(* can_set_timing_data_t)(const struct device *dev, const struct can_timing *timing_data)
Optional callback API upon setting CAN FD bus timing for the data phase.
Definition can.h:421
int(* can_send_t)(const struct device *dev, const struct can_frame *frame, k_timeout_t timeout, can_tx_callback_t callback, void *user_data)
Callback API upon sending a CAN frame See can_send() for argument description.
Definition can.h:455
int(* can_recover_t)(const struct device *dev, k_timeout_t timeout)
Optional callback API upon manually recovering the CAN controller from bus-off state See can_recover(...
Definition can.h:479
int(* can_get_max_filters_t)(const struct device *dev, bool ide)
Optional callback API upon getting the maximum number of concurrent CAN RX filters See can_get_max_fi...
Definition can.h:506
void(* can_set_state_change_callback_t)(const struct device *dev, can_state_change_callback_t callback, void *user_data)
Callback API upon setting a state change callback See can_set_state_change_callback() for argument de...
Definition can.h:492
int(* can_start_t)(const struct device *dev)
Callback API upon starting CAN controller See can_start() for argument description.
Definition can.h:434
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:309
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:122
uint32_t can_stats_get_stuff_errors(const struct device *dev)
Get the stuffing error counter for a CAN device.
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.
uint32_t can_get_bitrate_max(const struct device *dev)
Get maximum supported bitrate.
uint32_t can_stats_get_bit_errors(const struct device *dev)
Get the bit error counter for a CAN device.
uint32_t can_stats_get_crc_errors(const struct device *dev)
Get the CRC error counter for a CAN device.
uint32_t can_get_bitrate_min(const struct device *dev)
Get minimum supported bitrate.
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.
uint32_t can_stats_get_bit1_errors(const struct device *dev)
Get the bit1 error counter for a CAN device.
uint32_t can_stats_get_ack_errors(const struct device *dev)
Get the acknowledge error counter for a CAN device.
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.
uint32_t can_stats_get_bit0_errors(const struct device *dev)
Get the bit0 error counter for a CAN device.
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:1735
uint32_t can_stats_get_rx_overruns(const struct device *dev)
Get the RX overrun counter for a CAN device.
#define CAN_FRAME_IDE
Frame uses extended (29-bit) CAN ID.
Definition can.h:148
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:1720
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:298
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:1754
void(* can_tx_callback_t)(const struct device *dev, int error, void *user_data)
Defines the application callback handler function signature.
Definition can.h:289
int can_get_state(const struct device *dev, enum can_state *state, struct can_bus_err_cnt *err_cnt)
Get current CAN controller state.
can_mode_t can_get_mode(const struct device *dev)
Get the operation mode of the CAN controller.
const struct can_timing * can_get_timing_max(const struct device *dev)
Get the maximum supported timing parameter values.
uint32_t can_stats_get_form_errors(const struct device *dev)
Get the form error counter for a CAN device.
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:127
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:1517
const struct device * can_get_transceiver(const struct device *dev)
Get the CAN transceiver associated with the CAN controller.
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.
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.
#define CAN_FILTER_IDE
Filter matches frames with extended (29-bit) CAN IDs.
Definition can.h:208
@ CAN_STATE_ERROR_ACTIVE
Error-active state (RX/TX error count < 96).
Definition can.h:129
@ CAN_STATE_ERROR_WARNING
Error-warning state (RX/TX error count < 128).
Definition can.h:131
@ CAN_STATE_STOPPED
CAN controller is stopped and does not participate in CAN communication.
Definition can.h:137
@ CAN_STATE_BUS_OFF
Bus-off state (RX/TX error count >= 256).
Definition can.h:135
@ CAN_STATE_ERROR_PASSIVE
Error-passive state (RX/TX error count < 256).
Definition can.h:133
#define MIN(a, b)
Obtain the minimum of two values.
Definition util.h:448
#define ARRAY_SIZE(array)
Number of elements in the given array.
Definition util.h:118
#define DIV_ROUND_UP(n, d)
Divide and round up.
Definition util.h:348
#define ENOSYS
Function not implemented.
Definition errno.h:82
#define NULL
Definition iar_missing_defs.h:20
Public kernel APIs.
state
Definition parser_state.h:29
Statistics.
#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:229
uint8_t tx_err_cnt
Value of the CAN controller transmit error counter.
Definition can.h:231
uint8_t rx_err_cnt
Value of the CAN controller receive error counter.
Definition can.h:233
CAN specific device state which allows for CAN device class specific additions.
Definition can.h:625
struct device_state devstate
Common device state.
Definition can.h:627
struct stats_can stats
CAN device statistics.
Definition can.h:629
<span class="mlabel">Driver Operations</span> CAN Controller driver operations
Definition can.h:511
can_get_capabilities_t get_capabilities
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:515
can_set_timing_data_t set_timing_data
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition can.h:576
can_get_max_filters_t get_max_filters
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition can.h:566
can_set_timing_t set_timing
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:531
can_stop_t stop
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:523
can_set_state_change_callback_t set_state_change_callback
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:558
can_add_rx_filter_t add_rx_filter
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:539
struct can_timing timing_data_min
Min values for the timing registers during the data phase.
Definition can.h:580
struct can_timing timing_max
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:570
can_send_t send
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:535
can_get_state_t get_state
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:554
can_recover_t recover
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition can.h:549
struct can_timing timing_data_max
Max values for the timing registers during the data phase.
Definition can.h:584
can_remove_rx_filter_t remove_rx_filter
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:543
can_start_t start
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:519
can_set_mode_t set_mode
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:527
can_get_core_clock_t get_core_clock
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:562
struct can_timing timing_min
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:568
Common CAN controller driver configuration.
Definition can.h:342
uint16_t sample_point
Initial CAN classic/CAN FD arbitration phase sample point in permille.
Definition can.h:352
uint32_t max_bitrate
The maximum bitrate supported by the CAN controller/transceiver combination.
Definition can.h:348
uint32_t bitrate
Initial CAN classic/CAN FD arbitration phase bitrate.
Definition can.h:350
const struct device * phy
Pointer to the device structure for the associated CAN transceiver device or NULL.
Definition can.h:344
uint32_t min_bitrate
The minimum bitrate supported by the CAN controller/transceiver combination.
Definition can.h:346
Common CAN controller driver data.
Definition can.h:399
can_mode_t mode
Current CAN controller mode.
Definition can.h:401
bool started
True if the CAN controller is started, false otherwise.
Definition can.h:403
void * state_change_cb_user_data
State change callback user data pointer or NULL.
Definition can.h:407
can_state_change_callback_t state_change_cb
State change callback function pointer or NULL.
Definition can.h:405
CAN filter structure.
Definition can.h:215
uint32_t mask
CAN identifier matching mask.
Definition can.h:221
uint8_t flags
Flags.
Definition can.h:223
uint32_t id
CAN identifier to match.
Definition can.h:217
CAN frame structure.
Definition can.h:169
uint8_t dlc
Data Length Code (DLC) indicating data length in bytes.
Definition can.h:173
uint8_t flags
Flags.
Definition can.h:175
uint32_t id
Standard (11-bit) or extended (29-bit) CAN identifier.
Definition can.h:171
uint8_t data[CAN_MAX_DLEN]
Payload data accessed as unsigned 8 bit values.
Definition can.h:194
uint32_t data_32[DIV_ROUND_UP(CAN_MAX_DLEN, sizeof(uint32_t))]
Payload data accessed as unsigned 32 bit values.
Definition can.h:196
uint16_t timestamp
Captured value of the free-running timer in the CAN controller when this frame was received.
Definition can.h:184
CAN bus timing structure.
Definition can.h:268
uint16_t sjw
Synchronisation jump width.
Definition can.h:270
uint16_t phase_seg2
Phase segment 2.
Definition can.h:276
uint16_t prescaler
Prescaler value.
Definition can.h:278
uint16_t phase_seg1
Phase segment 1.
Definition can.h:274
uint16_t prop_seg
Propagation segment.
Definition can.h:272
Runtime device dynamic structure (in RAM) per driver instance.
Definition device.h:458
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
void * data
Address of the device instance private data.
Definition device.h:523
const void * config
Address of device instance config information.
Definition device.h:517
Message Queue Structure.
Definition kernel.h:5070
Kernel timeout type.
Definition clock.h:65
Misc utilities.