Zephyr API Documentation  3.6.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#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
53#define CAN_MAX_STD_ID CAN_STD_ID_MASK __DEPRECATED_MACRO
57#define CAN_EXT_ID_MASK 0x1FFFFFFFU
63#define CAN_MAX_EXT_ID CAN_EXT_ID_MASK __DEPRECATED_MACRO
67#define CAN_MAX_DLC 8U
71#define CANFD_MAX_DLC 15U
72
77#ifndef CONFIG_CAN_FD_MODE
78#define CAN_MAX_DLEN 8U
79#else
80#define CAN_MAX_DLEN 64U
81#endif /* CONFIG_CAN_FD_MODE */
82
95#define CAN_MODE_NORMAL 0
96
98#define CAN_MODE_LOOPBACK BIT(0)
99
101#define CAN_MODE_LISTENONLY BIT(1)
102
104#define CAN_MODE_FD BIT(2)
105
107#define CAN_MODE_ONE_SHOT BIT(3)
108
110#define CAN_MODE_3_SAMPLES BIT(4)
111
113#define CAN_MODE_MANUAL_RECOVERY BIT(5)
114
126
141};
142
151#define CAN_FRAME_IDE BIT(0)
152
154#define CAN_FRAME_RTR BIT(1)
155
157#define CAN_FRAME_FDF BIT(2)
158
160#define CAN_FRAME_BRS BIT(3)
161
165#define CAN_FRAME_ESI BIT(4)
166
172struct can_frame {
176 uint8_t res0 : 3; /* reserved/padding. */
182#if defined(CONFIG_CAN_RX_TIMESTAMP) || defined(__DOXYGEN__)
191#else
193 uint16_t res1; /* reserved/padding. */
195#endif
197 union {
199 uint8_t data[CAN_MAX_DLEN];
201 uint32_t data_32[DIV_ROUND_UP(CAN_MAX_DLEN, sizeof(uint32_t))];
202 };
203};
204
213#define CAN_FILTER_IDE BIT(0)
214
215
225 uint32_t res0 : 3;
233};
234
243};
244
288};
289
298typedef void (*can_tx_callback_t)(const struct device *dev, int error, void *user_data);
299
307typedef void (*can_rx_callback_t)(const struct device *dev, struct can_frame *frame,
308 void *user_data);
309
318typedef void (*can_state_change_callback_t)(const struct device *dev,
319 enum can_state state,
320 struct can_bus_err_cnt err_cnt,
321 void *user_data);
322
342#define CAN_CALC_TDCO(_timing_data, _tdco_min, _tdco_max) \
343 CLAMP((1U + _timing_data->prop_seg + _timing_data->phase_seg1) * _timing_data->prescaler, \
344 _tdco_min, _tdco_max)
345
352struct can_driver_config {
354 const struct device *phy;
356 uint32_t min_bitrate;
358 uint32_t max_bitrate;
360 uint32_t bus_speed;
362 uint16_t sample_point;
363#ifdef CONFIG_CAN_FD_MODE
365 uint16_t sample_point_data;
367 uint32_t bus_speed_data;
368#endif /* CONFIG_CAN_FD_MODE */
369};
370
378#define CAN_DT_DRIVER_CONFIG_GET(node_id, _min_bitrate, _max_bitrate) \
379 { \
380 .phy = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(node_id, phys)), \
381 .min_bitrate = DT_CAN_TRANSCEIVER_MIN_BITRATE(node_id, _min_bitrate), \
382 .max_bitrate = DT_CAN_TRANSCEIVER_MAX_BITRATE(node_id, _max_bitrate), \
383 .bus_speed = DT_PROP(node_id, bus_speed), \
384 .sample_point = DT_PROP_OR(node_id, sample_point, 0), \
385 IF_ENABLED(CONFIG_CAN_FD_MODE, \
386 (.bus_speed_data = DT_PROP_OR(node_id, bus_speed_data, 0), \
387 .sample_point_data = DT_PROP_OR(node_id, sample_point_data, 0),)) \
388 }
389
398#define CAN_DT_DRIVER_CONFIG_INST_GET(inst, _min_bitrate, _max_bitrate) \
399 CAN_DT_DRIVER_CONFIG_GET(DT_DRV_INST(inst), _min_bitrate, _max_bitrate)
400
407struct can_driver_data {
409 can_mode_t mode;
411 bool started;
413 can_state_change_callback_t state_change_cb;
415 void *state_change_cb_user_data;
416};
417
422typedef int (*can_set_timing_t)(const struct device *dev,
423 const struct can_timing *timing);
424
429typedef int (*can_set_timing_data_t)(const struct device *dev,
430 const struct can_timing *timing_data);
431
436typedef int (*can_get_capabilities_t)(const struct device *dev, can_mode_t *cap);
437
442typedef int (*can_start_t)(const struct device *dev);
443
448typedef int (*can_stop_t)(const struct device *dev);
449
454typedef int (*can_set_mode_t)(const struct device *dev, can_mode_t mode);
455
463typedef int (*can_send_t)(const struct device *dev,
464 const struct can_frame *frame,
465 k_timeout_t timeout, can_tx_callback_t callback,
466 void *user_data);
467
472typedef int (*can_add_rx_filter_t)(const struct device *dev,
473 can_rx_callback_t callback,
474 void *user_data,
475 const struct can_filter *filter);
476
481typedef void (*can_remove_rx_filter_t)(const struct device *dev, int filter_id);
482
487typedef int (*can_recover_t)(const struct device *dev, k_timeout_t timeout);
488
493typedef int (*can_get_state_t)(const struct device *dev, enum can_state *state,
494 struct can_bus_err_cnt *err_cnt);
495
500typedef void(*can_set_state_change_callback_t)(const struct device *dev,
502 void *user_data);
503
508typedef int (*can_get_core_clock_t)(const struct device *dev, uint32_t *rate);
509
514typedef int (*can_get_max_filters_t)(const struct device *dev, bool ide);
515
516__subsystem struct can_driver_api {
517 can_get_capabilities_t get_capabilities;
518 can_start_t start;
519 can_stop_t stop;
520 can_set_mode_t set_mode;
521 can_set_timing_t set_timing;
522 can_send_t send;
523 can_add_rx_filter_t add_rx_filter;
524 can_remove_rx_filter_t remove_rx_filter;
525#if defined(CONFIG_CAN_MANUAL_RECOVERY_MODE) || defined(__DOXYGEN__)
526 can_recover_t recover;
527#endif /* CONFIG_CAN_MANUAL_RECOVERY_MODE */
528 can_get_state_t get_state;
529 can_set_state_change_callback_t set_state_change_callback;
530 can_get_core_clock_t get_core_clock;
531 can_get_max_filters_t get_max_filters;
532 /* Min values for the timing registers */
533 struct can_timing timing_min;
534 /* Max values for the timing registers */
535 struct can_timing timing_max;
536#if defined(CONFIG_CAN_FD_MODE) || defined(__DOXYGEN__)
537 can_set_timing_data_t set_timing_data;
538 /* Min values for the timing registers during the data phase */
539 struct can_timing timing_data_min;
540 /* Max values for the timing registers during the data phase */
541 struct can_timing timing_data_max;
542#endif /* CONFIG_CAN_FD_MODE */
543};
544
547#if defined(CONFIG_CAN_STATS) || defined(__DOXYGEN__)
548
549#include <zephyr/stats/stats.h>
550
554STATS_SECT_ENTRY32(bit_error)
555STATS_SECT_ENTRY32(bit0_error)
556STATS_SECT_ENTRY32(bit1_error)
557STATS_SECT_ENTRY32(stuff_error)
558STATS_SECT_ENTRY32(crc_error)
559STATS_SECT_ENTRY32(form_error)
560STATS_SECT_ENTRY32(ack_error)
561STATS_SECT_ENTRY32(rx_overrun)
563
565STATS_NAME(can, bit_error)
566STATS_NAME(can, bit0_error)
567STATS_NAME(can, bit1_error)
568STATS_NAME(can, stuff_error)
569STATS_NAME(can, crc_error)
570STATS_NAME(can, form_error)
571STATS_NAME(can, ack_error)
572STATS_NAME(can, rx_overrun)
573STATS_NAME_END(can);
574
585 struct stats_can stats;
586};
587
593#define Z_CAN_GET_STATS(dev_) \
594 CONTAINER_OF(dev_->state, struct can_device_state, devstate)->stats
595
614#define CAN_STATS_BIT_ERROR_INC(dev_) \
615 STATS_INC(Z_CAN_GET_STATS(dev_), bit_error)
616
628#define CAN_STATS_BIT0_ERROR_INC(dev_) \
629 do { \
630 STATS_INC(Z_CAN_GET_STATS(dev_), bit0_error); \
631 CAN_STATS_BIT_ERROR_INC(dev_); \
632 } while (0)
633
645#define CAN_STATS_BIT1_ERROR_INC(dev_) \
646 do { \
647 STATS_INC(Z_CAN_GET_STATS(dev_), bit1_error); \
648 CAN_STATS_BIT_ERROR_INC(dev_); \
649 } while (0)
650
659#define CAN_STATS_STUFF_ERROR_INC(dev_) \
660 STATS_INC(Z_CAN_GET_STATS(dev_), stuff_error)
661
670#define CAN_STATS_CRC_ERROR_INC(dev_) \
671 STATS_INC(Z_CAN_GET_STATS(dev_), crc_error)
672
681#define CAN_STATS_FORM_ERROR_INC(dev_) \
682 STATS_INC(Z_CAN_GET_STATS(dev_), form_error)
683
692#define CAN_STATS_ACK_ERROR_INC(dev_) \
693 STATS_INC(Z_CAN_GET_STATS(dev_), ack_error)
694
704#define CAN_STATS_RX_OVERRUN_INC(dev_) \
705 STATS_INC(Z_CAN_GET_STATS(dev_), rx_overrun)
706
715#define CAN_STATS_RESET(dev_) \
716 stats_reset(&(Z_CAN_GET_STATS(dev_).s_hdr))
717
723#define Z_CAN_DEVICE_STATE_DEFINE(dev_id) \
724 static struct can_device_state Z_DEVICE_STATE_NAME(dev_id) \
725 __attribute__((__section__(".z_devstate")))
726
733#define Z_CAN_INIT_FN(dev_id, init_fn) \
734 static inline int UTIL_CAT(dev_id, _init)(const struct device *dev) \
735 { \
736 struct can_device_state *state = \
737 CONTAINER_OF(dev->state, struct can_device_state, devstate); \
738 stats_init(&state->stats.s_hdr, STATS_SIZE_32, 8, \
739 STATS_NAME_INIT_PARMS(can)); \
740 stats_register(dev->name, &(state->stats.s_hdr)); \
741 if (!is_null_no_warn(init_fn)) { \
742 return init_fn(dev); \
743 } \
744 \
745 return 0; \
746 }
747
770#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
771 prio, api, ...) \
772 Z_CAN_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
773 Z_CAN_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn) \
774 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
775 DEVICE_DT_NAME(node_id), \
776 &UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init), \
777 pm, data, config, level, prio, api, \
778 &(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
779 __VA_ARGS__)
780
781#else /* CONFIG_CAN_STATS */
782
783#define CAN_STATS_BIT_ERROR_INC(dev_)
784#define CAN_STATS_BIT0_ERROR_INC(dev_)
785#define CAN_STATS_BIT1_ERROR_INC(dev_)
786#define CAN_STATS_STUFF_ERROR_INC(dev_)
787#define CAN_STATS_CRC_ERROR_INC(dev_)
788#define CAN_STATS_FORM_ERROR_INC(dev_)
789#define CAN_STATS_ACK_ERROR_INC(dev_)
790#define CAN_STATS_RX_OVERRUN_INC(dev_)
791#define CAN_STATS_RESET(dev_)
792
793#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
794 prio, api, ...) \
795 DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
796 prio, api, __VA_ARGS__)
797
798#endif /* CONFIG_CAN_STATS */
799
807#define CAN_DEVICE_DT_INST_DEFINE(inst, ...) \
808 CAN_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
809
828__syscall int can_get_core_clock(const struct device *dev, uint32_t *rate);
829
830static inline int z_impl_can_get_core_clock(const struct device *dev, uint32_t *rate)
831{
832 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
833
834 return api->get_core_clock(dev, rate);
835}
836
845__syscall uint32_t can_get_bitrate_min(const struct device *dev);
846
847static inline uint32_t z_impl_can_get_bitrate_min(const struct device *dev)
848{
849 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
850
851 return common->min_bitrate;
852}
853
867__deprecated static inline int can_get_min_bitrate(const struct device *dev, uint32_t *min_bitrate)
868{
869 *min_bitrate = can_get_bitrate_min(dev);
870
871 return 0;
872}
873
882__syscall uint32_t can_get_bitrate_max(const struct device *dev);
883
884static inline uint32_t z_impl_can_get_bitrate_max(const struct device *dev)
885{
886 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
887
888 return common->max_bitrate;
889}
890
905__deprecated static inline int can_get_max_bitrate(const struct device *dev, uint32_t *max_bitrate)
906{
907 *max_bitrate = can_get_bitrate_max(dev);
908
909 return 0;
910}
911
919__syscall const struct can_timing *can_get_timing_min(const struct device *dev);
920
921static inline const struct can_timing *z_impl_can_get_timing_min(const struct device *dev)
922{
923 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
924
925 return &api->timing_min;
926}
927
935__syscall const struct can_timing *can_get_timing_max(const struct device *dev);
936
937static inline const struct can_timing *z_impl_can_get_timing_max(const struct device *dev)
938{
939 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
940
941 return &api->timing_max;
942}
943
970__syscall int can_calc_timing(const struct device *dev, struct can_timing *res,
971 uint32_t bitrate, uint16_t sample_pnt);
972
986__syscall const struct can_timing *can_get_timing_data_min(const struct device *dev);
987
988#ifdef CONFIG_CAN_FD_MODE
989static inline const struct can_timing *z_impl_can_get_timing_data_min(const struct device *dev)
990{
991 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
992
993 return &api->timing_data_min;
994}
995#endif /* CONFIG_CAN_FD_MODE */
996
1010__syscall const struct can_timing *can_get_timing_data_max(const struct device *dev);
1011
1012#ifdef CONFIG_CAN_FD_MODE
1013static inline const struct can_timing *z_impl_can_get_timing_data_max(const struct device *dev)
1014{
1015 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1016
1017 return &api->timing_data_max;
1018}
1019#endif /* CONFIG_CAN_FD_MODE */
1020
1041__syscall int can_calc_timing_data(const struct device *dev, struct can_timing *res,
1042 uint32_t bitrate, uint16_t sample_pnt);
1043
1061__syscall int can_set_timing_data(const struct device *dev,
1062 const struct can_timing *timing_data);
1063
1092__syscall int can_set_bitrate_data(const struct device *dev, uint32_t bitrate_data);
1093
1114__deprecated int can_calc_prescaler(const struct device *dev, struct can_timing *timing,
1115 uint32_t bitrate);
1116
1130__syscall int can_set_timing(const struct device *dev,
1131 const struct can_timing *timing);
1132
1146__syscall int can_get_capabilities(const struct device *dev, can_mode_t *cap);
1147
1148static inline int z_impl_can_get_capabilities(const struct device *dev, can_mode_t *cap)
1149{
1150 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1151
1152 return api->get_capabilities(dev, cap);
1153}
1154
1164__syscall const struct device *can_get_transceiver(const struct device *dev);
1165
1166static const struct device *z_impl_can_get_transceiver(const struct device *dev)
1167{
1168 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
1169
1170 return common->phy;
1171}
1172
1190__syscall int can_start(const struct device *dev);
1191
1192static inline int z_impl_can_start(const struct device *dev)
1193{
1194 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1195
1196 return api->start(dev);
1197}
1198
1214__syscall int can_stop(const struct device *dev);
1215
1216static inline int z_impl_can_stop(const struct device *dev)
1217{
1218 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1219
1220 return api->stop(dev);
1221}
1222
1233__syscall int can_set_mode(const struct device *dev, can_mode_t mode);
1234
1235static inline int z_impl_can_set_mode(const struct device *dev, can_mode_t mode)
1236{
1237 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1238
1239 return api->set_mode(dev, mode);
1240}
1241
1249__syscall can_mode_t can_get_mode(const struct device *dev);
1250
1251static inline can_mode_t z_impl_can_get_mode(const struct device *dev)
1252{
1253 const struct can_driver_data *common = (const struct can_driver_data *)dev->data;
1254
1255 return common->mode;
1256}
1257
1283__syscall int can_set_bitrate(const struct device *dev, uint32_t bitrate);
1284
1337__syscall int can_send(const struct device *dev, const struct can_frame *frame,
1338 k_timeout_t timeout, can_tx_callback_t callback,
1339 void *user_data);
1340
1372static inline int can_add_rx_filter(const struct device *dev, can_rx_callback_t callback,
1373 void *user_data, const struct can_filter *filter)
1374{
1375 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1376
1377 if (filter == NULL) {
1378 return -EINVAL;
1379 }
1380
1381 return api->add_rx_filter(dev, callback, user_data, filter);
1382}
1383
1394#define CAN_MSGQ_DEFINE(name, max_frames) \
1395 K_MSGQ_DEFINE(name, sizeof(struct can_frame), max_frames, 4)
1396
1423__syscall int can_add_rx_filter_msgq(const struct device *dev, struct k_msgq *msgq,
1424 const struct can_filter *filter);
1425
1435__syscall void can_remove_rx_filter(const struct device *dev, int filter_id);
1436
1437static inline void z_impl_can_remove_rx_filter(const struct device *dev, int filter_id)
1438{
1439 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1440
1441 return api->remove_rx_filter(dev, filter_id);
1442}
1443
1457__syscall int can_get_max_filters(const struct device *dev, bool ide);
1458
1459static inline int z_impl_can_get_max_filters(const struct device *dev, bool ide)
1460{
1461 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1462
1463 if (api->get_max_filters == NULL) {
1464 return -ENOSYS;
1465 }
1466
1467 return api->get_max_filters(dev, ide);
1468}
1469
1491__syscall int can_get_state(const struct device *dev, enum can_state *state,
1492 struct can_bus_err_cnt *err_cnt);
1493
1494static inline int z_impl_can_get_state(const struct device *dev, enum can_state *state,
1495 struct can_bus_err_cnt *err_cnt)
1496{
1497 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1498
1499 return api->get_state(dev, state, err_cnt);
1500}
1501
1519__syscall int can_recover(const struct device *dev, k_timeout_t timeout);
1520
1521#ifdef CONFIG_CAN_MANUAL_RECOVERY_MODE
1522static inline int z_impl_can_recover(const struct device *dev, k_timeout_t timeout)
1523{
1524 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1525
1526 if (api->recover == NULL) {
1527 return -ENOSYS;
1528 }
1529
1530 return api->recover(dev, timeout);
1531}
1532#endif /* CONFIG_CAN_MANUAL_RECOVERY_MODE */
1533
1547static inline void can_set_state_change_callback(const struct device *dev,
1549 void *user_data)
1550{
1551 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1552
1553 api->set_state_change_callback(dev, callback, user_data);
1554}
1555
1576__syscall uint32_t can_stats_get_bit_errors(const struct device *dev);
1577
1578#ifdef CONFIG_CAN_STATS
1579static inline uint32_t z_impl_can_stats_get_bit_errors(const struct device *dev)
1580{
1581 return Z_CAN_GET_STATS(dev).bit_error;
1582}
1583#endif /* CONFIG_CAN_STATS */
1584
1599__syscall uint32_t can_stats_get_bit0_errors(const struct device *dev);
1600
1601#ifdef CONFIG_CAN_STATS
1602static inline uint32_t z_impl_can_stats_get_bit0_errors(const struct device *dev)
1603{
1604 return Z_CAN_GET_STATS(dev).bit0_error;
1605}
1606#endif /* CONFIG_CAN_STATS */
1607
1622__syscall uint32_t can_stats_get_bit1_errors(const struct device *dev);
1623
1624#ifdef CONFIG_CAN_STATS
1625static inline uint32_t z_impl_can_stats_get_bit1_errors(const struct device *dev)
1626{
1627 return Z_CAN_GET_STATS(dev).bit1_error;
1628}
1629#endif /* CONFIG_CAN_STATS */
1630
1643__syscall uint32_t can_stats_get_stuff_errors(const struct device *dev);
1644
1645#ifdef CONFIG_CAN_STATS
1646static inline uint32_t z_impl_can_stats_get_stuff_errors(const struct device *dev)
1647{
1648 return Z_CAN_GET_STATS(dev).stuff_error;
1649}
1650#endif /* CONFIG_CAN_STATS */
1651
1664__syscall uint32_t can_stats_get_crc_errors(const struct device *dev);
1665
1666#ifdef CONFIG_CAN_STATS
1667static inline uint32_t z_impl_can_stats_get_crc_errors(const struct device *dev)
1668{
1669 return Z_CAN_GET_STATS(dev).crc_error;
1670}
1671#endif /* CONFIG_CAN_STATS */
1672
1685__syscall uint32_t can_stats_get_form_errors(const struct device *dev);
1686
1687#ifdef CONFIG_CAN_STATS
1688static inline uint32_t z_impl_can_stats_get_form_errors(const struct device *dev)
1689{
1690 return Z_CAN_GET_STATS(dev).form_error;
1691}
1692#endif /* CONFIG_CAN_STATS */
1693
1706__syscall uint32_t can_stats_get_ack_errors(const struct device *dev);
1707
1708#ifdef CONFIG_CAN_STATS
1709static inline uint32_t z_impl_can_stats_get_ack_errors(const struct device *dev)
1710{
1711 return Z_CAN_GET_STATS(dev).ack_error;
1712}
1713#endif /* CONFIG_CAN_STATS */
1714
1728__syscall uint32_t can_stats_get_rx_overruns(const struct device *dev);
1729
1730#ifdef CONFIG_CAN_STATS
1731static inline uint32_t z_impl_can_stats_get_rx_overruns(const struct device *dev)
1732{
1733 return Z_CAN_GET_STATS(dev).rx_overrun;
1734}
1735#endif /* CONFIG_CAN_STATS */
1736
1753{
1754 static const uint8_t dlc_table[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12,
1755 16, 20, 24, 32, 48, 64};
1756
1757 return dlc_table[MIN(dlc, ARRAY_SIZE(dlc_table) - 1)];
1758}
1759
1767static inline uint8_t can_bytes_to_dlc(uint8_t num_bytes)
1768{
1769 return num_bytes <= 8 ? num_bytes :
1770 num_bytes <= 12 ? 9 :
1771 num_bytes <= 16 ? 10 :
1772 num_bytes <= 20 ? 11 :
1773 num_bytes <= 24 ? 12 :
1774 num_bytes <= 32 ? 13 :
1775 num_bytes <= 48 ? 14 :
1776 15;
1777}
1778
1786static inline bool can_frame_matches_filter(const struct can_frame *frame,
1787 const struct can_filter *filter)
1788{
1789 if ((frame->flags & CAN_FRAME_IDE) != 0 && (filter->flags & CAN_FILTER_IDE) == 0) {
1790 /* Extended (29-bit) ID frame, standard (11-bit) filter */
1791 return false;
1792 }
1793
1794 if ((frame->flags & CAN_FRAME_IDE) == 0 && (filter->flags & CAN_FILTER_IDE) != 0) {
1795 /* Standard (11-bit) ID frame, extended (29-bit) filter */
1796 return false;
1797 }
1798
1799 if ((frame->id ^ filter->id) & filter->mask) {
1800 /* Masked ID mismatch */
1801 return false;
1802 }
1803
1804 return true;
1805}
1806
1813#ifdef __cplusplus
1814}
1815#endif
1816
1817#include <syscalls/can.h>
1818
1819#endif /* ZEPHYR_INCLUDE_DRIVERS_CAN_H_ */
System error numbers.
static ssize_t send(int sock, const void *buf, size_t len, int flags)
POSIX wrapper for zsock_send.
Definition: socket.h:913
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:318
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:125
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:1372
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.
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:1767
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:151
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:1752
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:307
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:1786
void(* can_tx_callback_t)(const struct device *dev, int error, void *user_data)
Defines the application callback handler function signature.
Definition: can.h:298
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:130
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:1547
static int can_get_max_bitrate(const struct device *dev, uint32_t *max_bitrate)
Get maximum supported bitrate.
Definition: can.h:905
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.
static int can_get_min_bitrate(const struct device *dev, uint32_t *min_bitrate)
Get minimum supported bitrate.
Definition: can.h:867
#define CAN_FILTER_IDE
Filter matches frames with extended (29-bit) CAN IDs.
Definition: can.h:213
@ CAN_STATE_ERROR_ACTIVE
Error-active state (RX/TX error count < 96).
Definition: can.h:132
@ CAN_STATE_ERROR_WARNING
Error-warning state (RX/TX error count < 128).
Definition: can.h:134
@ CAN_STATE_STOPPED
CAN controller is stopped and does not participate in CAN communication.
Definition: can.h:140
@ CAN_STATE_BUS_OFF
Bus-off state (RX/TX error count >= 256).
Definition: can.h:138
@ CAN_STATE_ERROR_PASSIVE
Error-passive state (RX/TX error count < 256).
Definition: can.h:136
#define MIN(a, b)
Obtain the minimum of two values.
Definition: util.h:381
#define ARRAY_SIZE(array)
Number of elements in the given array.
Definition: util.h:127
#define DIV_ROUND_UP(n, d)
Divide and round up.
Definition: util.h:326
#define EINVAL
Invalid argument.
Definition: errno.h:61
#define ENOSYS
Function not implemented.
Definition: errno.h:83
Public kernel APIs.
Variables needed for system clock.
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:238
uint8_t tx_err_cnt
Value of the CAN controller transmit error counter.
Definition: can.h:240
uint8_t rx_err_cnt
Value of the CAN controller receive error counter.
Definition: can.h:242
CAN specific device state which allows for CAN device class specific additions.
Definition: can.h:581
struct device_state devstate
Common device state.
Definition: can.h:583
struct stats_can stats
CAN device statistics.
Definition: can.h:585
CAN filter structure.
Definition: can.h:221
uint32_t mask
CAN identifier matching mask.
Definition: can.h:230
uint8_t flags
Flags.
Definition: can.h:232
uint32_t id
CAN identifier to match.
Definition: can.h:223
CAN frame structure.
Definition: can.h:172
uint8_t dlc
Data Length Code (DLC) indicating data length in bytes.
Definition: can.h:179
uint8_t flags
Flags.
Definition: can.h:181
uint32_t id
Standard (11-bit) or extended (29-bit) CAN identifier.
Definition: can.h:174
uint8_t data[CAN_MAX_DLEN]
Payload data accessed as unsigned 8 bit values.
Definition: can.h:199
uint32_t data_32[DIV_ROUND_UP(CAN_MAX_DLEN, sizeof(uint32_t))]
Payload data accessed as unsigned 32 bit values.
Definition: can.h:201
uint16_t timestamp
Captured value of the free-running timer in the CAN controller when this frame was received.
Definition: can.h:190
CAN bus timing structure.
Definition: can.h:277
uint16_t sjw
Synchronisation jump width.
Definition: can.h:279
uint16_t phase_seg2
Phase segment 2.
Definition: can.h:285
uint16_t prescaler
Prescaler value.
Definition: can.h:287
uint16_t phase_seg1
Phase segment 1.
Definition: can.h:283
uint16_t prop_seg
Propagation segment.
Definition: can.h:281
Runtime device dynamic structure (in RAM) per driver instance.
Definition: device.h:370
Runtime device structure (in ROM) per driver instance.
Definition: device.h:399
void * data
Address of the device instance private data.
Definition: device.h:409
const void * api
Address of the API structure exposed by the device instance.
Definition: device.h:405
const void * config
Address of device instance config information.
Definition: device.h:403
Message Queue Structure.
Definition: kernel.h:4404
Kernel timeout type.
Definition: sys_clock.h:65
Misc utilities.