Zephyr API Documentation 4.1.99
A Scalable Open Source RTOS
 4.1.99
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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#ifndef ZEPHYR_INCLUDE_DRIVERS_CAN_H_
15#define ZEPHYR_INCLUDE_DRIVERS_CAN_H_
16
17#include <errno.h>
18
19#include <zephyr/types.h>
20#include <zephyr/device.h>
21#include <zephyr/kernel.h>
22#include <string.h>
23#include <zephyr/sys_clock.h>
24#include <zephyr/sys/util.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
47#define CAN_STD_ID_MASK 0x7FFU
48
52#define CAN_EXT_ID_MASK 0x1FFFFFFFU
53
57#define CAN_MAX_DLC 8U
58
62#define CANFD_MAX_DLC 15U
63
68#ifndef CONFIG_CAN_FD_MODE
69#define CAN_MAX_DLEN 8U
70#else
71#define CAN_MAX_DLEN 64U
72#endif /* CONFIG_CAN_FD_MODE */
73
86#define CAN_MODE_NORMAL 0
87
89#define CAN_MODE_LOOPBACK BIT(0)
90
92#define CAN_MODE_LISTENONLY BIT(1)
93
95#define CAN_MODE_FD BIT(2)
96
98#define CAN_MODE_ONE_SHOT BIT(3)
99
101#define CAN_MODE_3_SAMPLES BIT(4)
102
104#define CAN_MODE_MANUAL_RECOVERY BIT(5)
105
117
133
142#define CAN_FRAME_IDE BIT(0)
143
145#define CAN_FRAME_RTR BIT(1)
146
148#define CAN_FRAME_FDF BIT(2)
149
151#define CAN_FRAME_BRS BIT(3)
152
156#define CAN_FRAME_ESI BIT(4)
157
163struct can_frame {
170#if defined(CONFIG_CAN_RX_TIMESTAMP) || defined(__DOXYGEN__)
179#else
182 uint16_t reserved;
184#endif
186 union {
188 uint8_t data[CAN_MAX_DLEN];
190 uint32_t data_32[DIV_ROUND_UP(CAN_MAX_DLEN, sizeof(uint32_t))];
191 };
192};
193
202#define CAN_FILTER_IDE BIT(0)
203
219
229
274
283typedef void (*can_tx_callback_t)(const struct device *dev, int error, void *user_data);
284
292typedef void (*can_rx_callback_t)(const struct device *dev, struct can_frame *frame,
293 void *user_data);
294
303typedef void (*can_state_change_callback_t)(const struct device *dev,
304 enum can_state state,
305 struct can_bus_err_cnt err_cnt,
306 void *user_data);
307
327#define CAN_CALC_TDCO(_timing_data, _tdco_min, _tdco_max) \
328 CLAMP((1U + _timing_data->prop_seg + _timing_data->phase_seg1) * _timing_data->prescaler, \
329 _tdco_min, _tdco_max)
330
337struct can_driver_config {
339 const struct device *phy;
341 uint32_t min_bitrate;
343 uint32_t max_bitrate;
345 uint32_t bitrate;
347 uint16_t sample_point;
348#ifdef CONFIG_CAN_FD_MODE
350 uint16_t sample_point_data;
352 uint32_t bitrate_data;
353#endif /* CONFIG_CAN_FD_MODE */
354};
355
363#define CAN_DT_DRIVER_CONFIG_GET(node_id, _min_bitrate, _max_bitrate) \
364 { \
365 .phy = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(node_id, phys)), \
366 .min_bitrate = DT_CAN_TRANSCEIVER_MIN_BITRATE(node_id, _min_bitrate), \
367 .max_bitrate = DT_CAN_TRANSCEIVER_MAX_BITRATE(node_id, _max_bitrate), \
368 .bitrate = DT_PROP_OR(node_id, bitrate, \
369 DT_PROP_OR(node_id, bus_speed, CONFIG_CAN_DEFAULT_BITRATE)), \
370 .sample_point = DT_PROP_OR(node_id, sample_point, 0), \
371 IF_ENABLED(CONFIG_CAN_FD_MODE, \
372 (.bitrate_data = DT_PROP_OR(node_id, bitrate_data, \
373 DT_PROP_OR(node_id, bus_speed_data, CONFIG_CAN_DEFAULT_BITRATE_DATA)), \
374 .sample_point_data = DT_PROP_OR(node_id, sample_point_data, 0),)) \
375 }
376
385#define CAN_DT_DRIVER_CONFIG_INST_GET(inst, _min_bitrate, _max_bitrate) \
386 CAN_DT_DRIVER_CONFIG_GET(DT_DRV_INST(inst), _min_bitrate, _max_bitrate)
387
394struct can_driver_data {
396 can_mode_t mode;
398 bool started;
400 can_state_change_callback_t state_change_cb;
402 void *state_change_cb_user_data;
403};
404
409typedef int (*can_set_timing_t)(const struct device *dev,
410 const struct can_timing *timing);
411
416typedef int (*can_set_timing_data_t)(const struct device *dev,
417 const struct can_timing *timing_data);
418
423typedef int (*can_get_capabilities_t)(const struct device *dev, can_mode_t *cap);
424
429typedef int (*can_start_t)(const struct device *dev);
430
435typedef int (*can_stop_t)(const struct device *dev);
436
441typedef int (*can_set_mode_t)(const struct device *dev, can_mode_t mode);
442
450typedef int (*can_send_t)(const struct device *dev,
451 const struct can_frame *frame,
452 k_timeout_t timeout, can_tx_callback_t callback,
453 void *user_data);
454
459typedef int (*can_add_rx_filter_t)(const struct device *dev,
460 can_rx_callback_t callback,
461 void *user_data,
462 const struct can_filter *filter);
463
468typedef void (*can_remove_rx_filter_t)(const struct device *dev, int filter_id);
469
474typedef int (*can_recover_t)(const struct device *dev, k_timeout_t timeout);
475
480typedef int (*can_get_state_t)(const struct device *dev, enum can_state *state,
481 struct can_bus_err_cnt *err_cnt);
482
487typedef void(*can_set_state_change_callback_t)(const struct device *dev,
489 void *user_data);
490
495typedef int (*can_get_core_clock_t)(const struct device *dev, uint32_t *rate);
496
501typedef int (*can_get_max_filters_t)(const struct device *dev, bool ide);
502
503__subsystem struct can_driver_api {
504 can_get_capabilities_t get_capabilities;
505 can_start_t start;
506 can_stop_t stop;
507 can_set_mode_t set_mode;
508 can_set_timing_t set_timing;
509 can_send_t send;
510 can_add_rx_filter_t add_rx_filter;
511 can_remove_rx_filter_t remove_rx_filter;
512#if defined(CONFIG_CAN_MANUAL_RECOVERY_MODE) || defined(__DOXYGEN__)
513 can_recover_t recover;
514#endif /* CONFIG_CAN_MANUAL_RECOVERY_MODE */
515 can_get_state_t get_state;
516 can_set_state_change_callback_t set_state_change_callback;
517 can_get_core_clock_t get_core_clock;
518 can_get_max_filters_t get_max_filters;
519 /* Min values for the timing registers */
520 struct can_timing timing_min;
521 /* Max values for the timing registers */
522 struct can_timing timing_max;
523#if defined(CONFIG_CAN_FD_MODE) || defined(__DOXYGEN__)
524 can_set_timing_data_t set_timing_data;
525 /* Min values for the timing registers during the data phase */
526 struct can_timing timing_data_min;
527 /* Max values for the timing registers during the data phase */
528 struct can_timing timing_data_max;
529#endif /* CONFIG_CAN_FD_MODE */
530};
531
534#if defined(CONFIG_CAN_STATS) || defined(__DOXYGEN__)
535
536#include <zephyr/stats/stats.h>
537
541STATS_SECT_ENTRY32(bit_error)
542STATS_SECT_ENTRY32(bit0_error)
543STATS_SECT_ENTRY32(bit1_error)
544STATS_SECT_ENTRY32(stuff_error)
545STATS_SECT_ENTRY32(crc_error)
546STATS_SECT_ENTRY32(form_error)
547STATS_SECT_ENTRY32(ack_error)
548STATS_SECT_ENTRY32(rx_overrun)
550
552STATS_NAME(can, bit_error)
553STATS_NAME(can, bit0_error)
554STATS_NAME(can, bit1_error)
555STATS_NAME(can, stuff_error)
556STATS_NAME(can, crc_error)
557STATS_NAME(can, form_error)
558STATS_NAME(can, ack_error)
559STATS_NAME(can, rx_overrun)
560STATS_NAME_END(can);
561
572 struct stats_can stats;
573};
574
580#define Z_CAN_GET_STATS(dev_) \
581 CONTAINER_OF(dev_->state, struct can_device_state, devstate)->stats
582
601#define CAN_STATS_BIT_ERROR_INC(dev_) \
602 STATS_INC(Z_CAN_GET_STATS(dev_), bit_error)
603
615#define CAN_STATS_BIT0_ERROR_INC(dev_) \
616 do { \
617 STATS_INC(Z_CAN_GET_STATS(dev_), bit0_error); \
618 CAN_STATS_BIT_ERROR_INC(dev_); \
619 } while (0)
620
632#define CAN_STATS_BIT1_ERROR_INC(dev_) \
633 do { \
634 STATS_INC(Z_CAN_GET_STATS(dev_), bit1_error); \
635 CAN_STATS_BIT_ERROR_INC(dev_); \
636 } while (0)
637
646#define CAN_STATS_STUFF_ERROR_INC(dev_) \
647 STATS_INC(Z_CAN_GET_STATS(dev_), stuff_error)
648
657#define CAN_STATS_CRC_ERROR_INC(dev_) \
658 STATS_INC(Z_CAN_GET_STATS(dev_), crc_error)
659
668#define CAN_STATS_FORM_ERROR_INC(dev_) \
669 STATS_INC(Z_CAN_GET_STATS(dev_), form_error)
670
679#define CAN_STATS_ACK_ERROR_INC(dev_) \
680 STATS_INC(Z_CAN_GET_STATS(dev_), ack_error)
681
691#define CAN_STATS_RX_OVERRUN_INC(dev_) \
692 STATS_INC(Z_CAN_GET_STATS(dev_), rx_overrun)
693
702#define CAN_STATS_RESET(dev_) \
703 stats_reset(&(Z_CAN_GET_STATS(dev_).s_hdr))
704
710#define Z_CAN_DEVICE_STATE_DEFINE(dev_id) \
711 static struct can_device_state Z_DEVICE_STATE_NAME(dev_id) \
712 __attribute__((__section__(".z_devstate")))
713
720#define Z_CAN_INIT_FN(dev_id, init_fn) \
721 static inline int UTIL_CAT(dev_id, _init)(const struct device *dev) \
722 { \
723 struct can_device_state *state = \
724 CONTAINER_OF(dev->state, struct can_device_state, devstate); \
725 stats_init(&state->stats.s_hdr, STATS_SIZE_32, 8, \
726 STATS_NAME_INIT_PARMS(can)); \
727 stats_register(dev->name, &(state->stats.s_hdr)); \
728 if (!is_null_no_warn(init_fn)) { \
729 return init_fn(dev); \
730 } \
731 \
732 return 0; \
733 }
734
757#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
758 prio, api, ...) \
759 Z_CAN_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
760 Z_CAN_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn) \
761 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
762 DEVICE_DT_NAME(node_id), \
763 &UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init), \
764 NULL, Z_DEVICE_DT_FLAGS(node_id), pm, data, \
765 config, level, prio, api, \
766 &(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
767 __VA_ARGS__)
768
769#else /* CONFIG_CAN_STATS */
770
771#define CAN_STATS_BIT_ERROR_INC(dev_)
772#define CAN_STATS_BIT0_ERROR_INC(dev_)
773#define CAN_STATS_BIT1_ERROR_INC(dev_)
774#define CAN_STATS_STUFF_ERROR_INC(dev_)
775#define CAN_STATS_CRC_ERROR_INC(dev_)
776#define CAN_STATS_FORM_ERROR_INC(dev_)
777#define CAN_STATS_ACK_ERROR_INC(dev_)
778#define CAN_STATS_RX_OVERRUN_INC(dev_)
779#define CAN_STATS_RESET(dev_)
780
781#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
782 prio, api, ...) \
783 DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
784 prio, api, __VA_ARGS__)
785
786#endif /* CONFIG_CAN_STATS */
787
795#define CAN_DEVICE_DT_INST_DEFINE(inst, ...) \
796 CAN_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
797
816__syscall int can_get_core_clock(const struct device *dev, uint32_t *rate);
817
818static inline int z_impl_can_get_core_clock(const struct device *dev, uint32_t *rate)
819{
820 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
821
822 return api->get_core_clock(dev, rate);
823}
824
841__syscall uint32_t can_get_bitrate_min(const struct device *dev);
842
843static inline uint32_t z_impl_can_get_bitrate_min(const struct device *dev)
844{
845 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
846
847 return common->min_bitrate;
848}
849
866__syscall uint32_t can_get_bitrate_max(const struct device *dev);
867
868static inline uint32_t z_impl_can_get_bitrate_max(const struct device *dev)
869{
870 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
871
872 return common->max_bitrate;
873}
874
882__syscall const struct can_timing *can_get_timing_min(const struct device *dev);
883
884static inline const struct can_timing *z_impl_can_get_timing_min(const struct device *dev)
885{
886 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
887
888 return &api->timing_min;
889}
890
898__syscall const struct can_timing *can_get_timing_max(const struct device *dev);
899
900static inline const struct can_timing *z_impl_can_get_timing_max(const struct device *dev)
901{
902 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
903
904 return &api->timing_max;
905}
906
933__syscall int can_calc_timing(const struct device *dev, struct can_timing *res,
934 uint32_t bitrate, uint16_t sample_pnt);
935
949__syscall const struct can_timing *can_get_timing_data_min(const struct device *dev);
950
951#ifdef CONFIG_CAN_FD_MODE
952static inline const struct can_timing *z_impl_can_get_timing_data_min(const struct device *dev)
953{
954 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
955
956 return &api->timing_data_min;
957}
958#endif /* CONFIG_CAN_FD_MODE */
959
973__syscall const struct can_timing *can_get_timing_data_max(const struct device *dev);
974
975#ifdef CONFIG_CAN_FD_MODE
976static inline const struct can_timing *z_impl_can_get_timing_data_max(const struct device *dev)
977{
978 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
979
980 return &api->timing_data_max;
981}
982#endif /* CONFIG_CAN_FD_MODE */
983
1004__syscall int can_calc_timing_data(const struct device *dev, struct can_timing *res,
1005 uint32_t bitrate, uint16_t sample_pnt);
1006
1024__syscall int can_set_timing_data(const struct device *dev,
1025 const struct can_timing *timing_data);
1026
1055__syscall int can_set_bitrate_data(const struct device *dev, uint32_t bitrate_data);
1056
1070__syscall int can_set_timing(const struct device *dev,
1071 const struct can_timing *timing);
1072
1086__syscall int can_get_capabilities(const struct device *dev, can_mode_t *cap);
1087
1088static inline int z_impl_can_get_capabilities(const struct device *dev, can_mode_t *cap)
1089{
1090 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1091
1092 return api->get_capabilities(dev, cap);
1093}
1094
1104__syscall const struct device *can_get_transceiver(const struct device *dev);
1105
1106static const struct device *z_impl_can_get_transceiver(const struct device *dev)
1107{
1108 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
1109
1110 return common->phy;
1111}
1112
1130__syscall int can_start(const struct device *dev);
1131
1132static inline int z_impl_can_start(const struct device *dev)
1133{
1134 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1135
1136 return api->start(dev);
1137}
1138
1154__syscall int can_stop(const struct device *dev);
1155
1156static inline int z_impl_can_stop(const struct device *dev)
1157{
1158 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1159
1160 return api->stop(dev);
1161}
1162
1173__syscall int can_set_mode(const struct device *dev, can_mode_t mode);
1174
1175static inline int z_impl_can_set_mode(const struct device *dev, can_mode_t mode)
1176{
1177 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1178
1179 return api->set_mode(dev, mode);
1180}
1181
1189__syscall can_mode_t can_get_mode(const struct device *dev);
1190
1191static inline can_mode_t z_impl_can_get_mode(const struct device *dev)
1192{
1193 const struct can_driver_data *common = (const struct can_driver_data *)dev->data;
1194
1195 return common->mode;
1196}
1197
1223__syscall int can_set_bitrate(const struct device *dev, uint32_t bitrate);
1224
1277__syscall int can_send(const struct device *dev, const struct can_frame *frame,
1278 k_timeout_t timeout, can_tx_callback_t callback,
1279 void *user_data);
1280
1312int can_add_rx_filter(const struct device *dev, can_rx_callback_t callback,
1313 void *user_data, const struct can_filter *filter);
1314
1325#define CAN_MSGQ_DEFINE(name, max_frames) \
1326 K_MSGQ_DEFINE(name, sizeof(struct can_frame), max_frames, 4)
1327
1354__syscall int can_add_rx_filter_msgq(const struct device *dev, struct k_msgq *msgq,
1355 const struct can_filter *filter);
1356
1366__syscall void can_remove_rx_filter(const struct device *dev, int filter_id);
1367
1368static inline void z_impl_can_remove_rx_filter(const struct device *dev, int filter_id)
1369{
1370 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1371
1372 api->remove_rx_filter(dev, filter_id);
1373}
1374
1388__syscall int can_get_max_filters(const struct device *dev, bool ide);
1389
1390static inline int z_impl_can_get_max_filters(const struct device *dev, bool ide)
1391{
1392 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1393
1394 if (api->get_max_filters == NULL) {
1395 return -ENOSYS;
1396 }
1397
1398 return api->get_max_filters(dev, ide);
1399}
1400
1422__syscall int can_get_state(const struct device *dev, enum can_state *state,
1423 struct can_bus_err_cnt *err_cnt);
1424
1425static inline int z_impl_can_get_state(const struct device *dev, enum can_state *state,
1426 struct can_bus_err_cnt *err_cnt)
1427{
1428 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1429
1430 return api->get_state(dev, state, err_cnt);
1431}
1432
1450__syscall int can_recover(const struct device *dev, k_timeout_t timeout);
1451
1452#ifdef CONFIG_CAN_MANUAL_RECOVERY_MODE
1453static inline int z_impl_can_recover(const struct device *dev, k_timeout_t timeout)
1454{
1455 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1456
1457 if (api->recover == NULL) {
1458 return -ENOSYS;
1459 }
1460
1461 return api->recover(dev, timeout);
1462}
1463#endif /* CONFIG_CAN_MANUAL_RECOVERY_MODE */
1464
1478static inline void can_set_state_change_callback(const struct device *dev,
1480 void *user_data)
1481{
1482 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1483
1484 api->set_state_change_callback(dev, callback, user_data);
1485}
1486
1507__syscall uint32_t can_stats_get_bit_errors(const struct device *dev);
1508
1509#ifdef CONFIG_CAN_STATS
1510static inline uint32_t z_impl_can_stats_get_bit_errors(const struct device *dev)
1511{
1512 return Z_CAN_GET_STATS(dev).bit_error;
1513}
1514#endif /* CONFIG_CAN_STATS */
1515
1530__syscall uint32_t can_stats_get_bit0_errors(const struct device *dev);
1531
1532#ifdef CONFIG_CAN_STATS
1533static inline uint32_t z_impl_can_stats_get_bit0_errors(const struct device *dev)
1534{
1535 return Z_CAN_GET_STATS(dev).bit0_error;
1536}
1537#endif /* CONFIG_CAN_STATS */
1538
1553__syscall uint32_t can_stats_get_bit1_errors(const struct device *dev);
1554
1555#ifdef CONFIG_CAN_STATS
1556static inline uint32_t z_impl_can_stats_get_bit1_errors(const struct device *dev)
1557{
1558 return Z_CAN_GET_STATS(dev).bit1_error;
1559}
1560#endif /* CONFIG_CAN_STATS */
1561
1574__syscall uint32_t can_stats_get_stuff_errors(const struct device *dev);
1575
1576#ifdef CONFIG_CAN_STATS
1577static inline uint32_t z_impl_can_stats_get_stuff_errors(const struct device *dev)
1578{
1579 return Z_CAN_GET_STATS(dev).stuff_error;
1580}
1581#endif /* CONFIG_CAN_STATS */
1582
1595__syscall uint32_t can_stats_get_crc_errors(const struct device *dev);
1596
1597#ifdef CONFIG_CAN_STATS
1598static inline uint32_t z_impl_can_stats_get_crc_errors(const struct device *dev)
1599{
1600 return Z_CAN_GET_STATS(dev).crc_error;
1601}
1602#endif /* CONFIG_CAN_STATS */
1603
1616__syscall uint32_t can_stats_get_form_errors(const struct device *dev);
1617
1618#ifdef CONFIG_CAN_STATS
1619static inline uint32_t z_impl_can_stats_get_form_errors(const struct device *dev)
1620{
1621 return Z_CAN_GET_STATS(dev).form_error;
1622}
1623#endif /* CONFIG_CAN_STATS */
1624
1637__syscall uint32_t can_stats_get_ack_errors(const struct device *dev);
1638
1639#ifdef CONFIG_CAN_STATS
1640static inline uint32_t z_impl_can_stats_get_ack_errors(const struct device *dev)
1641{
1642 return Z_CAN_GET_STATS(dev).ack_error;
1643}
1644#endif /* CONFIG_CAN_STATS */
1645
1659__syscall uint32_t can_stats_get_rx_overruns(const struct device *dev);
1660
1661#ifdef CONFIG_CAN_STATS
1662static inline uint32_t z_impl_can_stats_get_rx_overruns(const struct device *dev)
1663{
1664 return Z_CAN_GET_STATS(dev).rx_overrun;
1665}
1666#endif /* CONFIG_CAN_STATS */
1667
1684{
1685 static const uint8_t dlc_table[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12,
1686 16, 20, 24, 32, 48, 64};
1687
1688 return dlc_table[MIN(dlc, ARRAY_SIZE(dlc_table) - 1)];
1689}
1690
1698static inline uint8_t can_bytes_to_dlc(uint8_t num_bytes)
1699{
1700 return num_bytes <= 8 ? num_bytes :
1701 num_bytes <= 12 ? 9 :
1702 num_bytes <= 16 ? 10 :
1703 num_bytes <= 20 ? 11 :
1704 num_bytes <= 24 ? 12 :
1705 num_bytes <= 32 ? 13 :
1706 num_bytes <= 48 ? 14 :
1707 15;
1708}
1709
1717static inline bool can_frame_matches_filter(const struct can_frame *frame,
1718 const struct can_filter *filter)
1719{
1720 if ((frame->flags & CAN_FRAME_IDE) != 0 && (filter->flags & CAN_FILTER_IDE) == 0) {
1721 /* Extended (29-bit) ID frame, standard (11-bit) filter */
1722 return false;
1723 }
1724
1725 if ((frame->flags & CAN_FRAME_IDE) == 0 && (filter->flags & CAN_FILTER_IDE) != 0) {
1726 /* Standard (11-bit) ID frame, extended (29-bit) filter */
1727 return false;
1728 }
1729
1730 if ((frame->id ^ filter->id) & filter->mask) {
1731 /* Masked ID mismatch */
1732 return false;
1733 }
1734
1735 return true;
1736}
1737
1744#ifdef __cplusplus
1745}
1746#endif
1747
1748#include <zephyr/syscalls/can.h>
1749
1750#endif /* ZEPHYR_INCLUDE_DRIVERS_CAN_H_ */
System error numbers.
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:303
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:116
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:1698
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:142
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:1683
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:292
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:1717
void(* can_tx_callback_t)(const struct device *dev, int error, void *user_data)
Defines the application callback handler function signature.
Definition can.h:283
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:121
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:1478
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:202
@ CAN_STATE_ERROR_ACTIVE
Error-active state (RX/TX error count < 96).
Definition can.h:123
@ CAN_STATE_ERROR_WARNING
Error-warning state (RX/TX error count < 128).
Definition can.h:125
@ CAN_STATE_STOPPED
CAN controller is stopped and does not participate in CAN communication.
Definition can.h:131
@ CAN_STATE_BUS_OFF
Bus-off state (RX/TX error count >= 256).
Definition can.h:129
@ CAN_STATE_ERROR_PASSIVE
Error-passive state (RX/TX error count < 256).
Definition can.h:127
#define MIN(a, b)
Obtain the minimum of two values.
Definition util.h:401
#define ARRAY_SIZE(array)
Number of elements in the given array.
Definition util.h:120
#define DIV_ROUND_UP(n, d)
Divide and round up.
Definition util.h:352
#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
ssize_t send(int sock, const void *buf, size_t len, int flags)
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:223
uint8_t tx_err_cnt
Value of the CAN controller transmit error counter.
Definition can.h:225
uint8_t rx_err_cnt
Value of the CAN controller receive error counter.
Definition can.h:227
CAN specific device state which allows for CAN device class specific additions.
Definition can.h:568
struct device_state devstate
Common device state.
Definition can.h:570
struct stats_can stats
CAN device statistics.
Definition can.h:572
CAN filter structure.
Definition can.h:209
uint32_t mask
CAN identifier matching mask.
Definition can.h:215
uint8_t flags
Flags.
Definition can.h:217
uint32_t id
CAN identifier to match.
Definition can.h:211
CAN frame structure.
Definition can.h:163
uint8_t dlc
Data Length Code (DLC) indicating data length in bytes.
Definition can.h:167
uint8_t flags
Flags.
Definition can.h:169
uint32_t id
Standard (11-bit) or extended (29-bit) CAN identifier.
Definition can.h:165
uint8_t data[CAN_MAX_DLEN]
Payload data accessed as unsigned 8 bit values.
Definition can.h:188
uint32_t data_32[DIV_ROUND_UP(CAN_MAX_DLEN, sizeof(uint32_t))]
Payload data accessed as unsigned 32 bit values.
Definition can.h:190
uint16_t timestamp
Captured value of the free-running timer in the CAN controller when this frame was received.
Definition can.h:178
CAN bus timing structure.
Definition can.h:262
uint16_t sjw
Synchronisation jump width.
Definition can.h:264
uint16_t phase_seg2
Phase segment 2.
Definition can.h:270
uint16_t prescaler
Prescaler value.
Definition can.h:272
uint16_t phase_seg1
Phase segment 1.
Definition can.h:268
uint16_t prop_seg
Propagation segment.
Definition can.h:266
Runtime device dynamic structure (in RAM) per driver instance.
Definition device.h:451
Runtime device structure (in ROM) per driver instance.
Definition device.h:504
void * data
Address of the device instance private data.
Definition device.h:514
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:510
const void * config
Address of device instance config information.
Definition device.h:508
Message Queue Structure.
Definition kernel.h:4553
Kernel timeout type.
Definition sys_clock.h:65
Misc utilities.
Variables needed for system clock.