Zephyr Project API 3.6.0
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
45#define CAN_STD_ID_MASK 0x7FFU
49#define CAN_MAX_STD_ID CAN_STD_ID_MASK
53#define CAN_EXT_ID_MASK 0x1FFFFFFFU
57#define CAN_MAX_EXT_ID CAN_EXT_ID_MASK
61#define CAN_MAX_DLC 8U
65#define CANFD_MAX_DLC 15U
66
71#ifndef CONFIG_CAN_FD_MODE
72#define CAN_MAX_DLEN 8U
73#else
74#define CAN_MAX_DLEN 64U
75#endif /* CONFIG_CAN_FD_MODE */
76
89#define CAN_MODE_NORMAL 0
90
92#define CAN_MODE_LOOPBACK BIT(0)
93
95#define CAN_MODE_LISTENONLY BIT(1)
96
98#define CAN_MODE_FD BIT(2)
99
101#define CAN_MODE_ONE_SHOT BIT(3)
102
104#define CAN_MODE_3_SAMPLES BIT(4)
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 {
167 uint8_t res0 : 3; /* reserved/padding. */
173#if defined(CONFIG_CAN_RX_TIMESTAMP) || defined(__DOXYGEN__)
182#else
184 uint16_t res1; /* reserved/padding. */
186#endif
188 union {
190 uint8_t data[CAN_MAX_DLEN];
192 uint32_t data_32[DIV_ROUND_UP(CAN_MAX_DLEN, sizeof(uint32_t))];
193 };
194};
195
204#define CAN_FILTER_IDE BIT(0)
205
206
216 uint32_t res0 : 3;
224};
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
333#define CAN_CALC_TDCO(_timing_data, _tdco_min, _tdco_max) \
334 CLAMP((1U + _timing_data->prop_seg + _timing_data->phase_seg1) * _timing_data->prescaler, \
335 _tdco_min, _tdco_max)
336
343struct can_driver_config {
345 const struct device *phy;
347 uint32_t max_bitrate;
349 uint32_t bus_speed;
351 uint16_t sample_point;
352#ifdef CONFIG_CAN_FD_MODE
354 uint16_t sample_point_data;
356 uint32_t bus_speed_data;
357#endif /* CONFIG_CAN_FD_MODE */
358};
359
366#define CAN_DT_DRIVER_CONFIG_GET(node_id, _max_bitrate) \
367 { \
368 .phy = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(node_id, phys)), \
369 .max_bitrate = DT_CAN_TRANSCEIVER_MAX_BITRATE(node_id, _max_bitrate), \
370 .bus_speed = DT_PROP(node_id, bus_speed), \
371 .sample_point = DT_PROP_OR(node_id, sample_point, 0), \
372 IF_ENABLED(CONFIG_CAN_FD_MODE, \
373 (.bus_speed_data = DT_PROP_OR(node_id, bus_speed_data, 0), \
374 .sample_point_data = DT_PROP_OR(node_id, sample_point_data, 0),)) \
375 }
376
384#define CAN_DT_DRIVER_CONFIG_INST_GET(inst, _max_bitrate) \
385 CAN_DT_DRIVER_CONFIG_GET(DT_DRV_INST(inst), _max_bitrate)
386
393struct can_driver_data {
395 can_mode_t mode;
397 bool started;
399 can_state_change_callback_t state_change_cb;
401 void *state_change_cb_user_data;
402};
403
408typedef int (*can_set_timing_t)(const struct device *dev,
409 const struct can_timing *timing);
410
415typedef int (*can_set_timing_data_t)(const struct device *dev,
416 const struct can_timing *timing_data);
417
422typedef int (*can_get_capabilities_t)(const struct device *dev, can_mode_t *cap);
423
428typedef int (*can_start_t)(const struct device *dev);
429
434typedef int (*can_stop_t)(const struct device *dev);
435
440typedef int (*can_set_mode_t)(const struct device *dev, can_mode_t mode);
441
449typedef int (*can_send_t)(const struct device *dev,
450 const struct can_frame *frame,
452 void *user_data);
453
458typedef int (*can_add_rx_filter_t)(const struct device *dev,
459 can_rx_callback_t callback,
460 void *user_data,
461 const struct can_filter *filter);
462
467typedef void (*can_remove_rx_filter_t)(const struct device *dev, int filter_id);
468
473typedef int (*can_recover_t)(const struct device *dev, k_timeout_t timeout);
474
479typedef int (*can_get_state_t)(const struct device *dev, enum can_state *state,
480 struct can_bus_err_cnt *err_cnt);
481
486typedef void(*can_set_state_change_callback_t)(const struct device *dev,
488 void *user_data);
489
494typedef int (*can_get_core_clock_t)(const struct device *dev, uint32_t *rate);
495
500typedef int (*can_get_max_filters_t)(const struct device *dev, bool ide);
501
502__subsystem struct can_driver_api {
503 can_get_capabilities_t get_capabilities;
504 can_start_t start;
505 can_stop_t stop;
506 can_set_mode_t set_mode;
507 can_set_timing_t set_timing;
508 can_send_t send;
509 can_add_rx_filter_t add_rx_filter;
510 can_remove_rx_filter_t remove_rx_filter;
511#if !defined(CONFIG_CAN_AUTO_BUS_OFF_RECOVERY) || defined(__DOXYGEN__)
512 can_recover_t recover;
513#endif /* CONFIG_CAN_AUTO_BUS_OFF_RECOVERY */
514 can_get_state_t get_state;
515 can_set_state_change_callback_t set_state_change_callback;
516 can_get_core_clock_t get_core_clock;
517 can_get_max_filters_t get_max_filters;
518 /* Min values for the timing registers */
519 struct can_timing timing_min;
520 /* Max values for the timing registers */
521 struct can_timing timing_max;
522#if defined(CONFIG_CAN_FD_MODE) || defined(__DOXYGEN__)
523 can_set_timing_data_t set_timing_data;
524 /* Min values for the timing registers during the data phase */
525 struct can_timing timing_data_min;
526 /* Max values for the timing registers during the data phase */
527 struct can_timing timing_data_max;
528#endif /* CONFIG_CAN_FD_MODE */
529};
530
533#if defined(CONFIG_CAN_STATS) || defined(__DOXYGEN__)
534
535#include <zephyr/stats/stats.h>
536
540STATS_SECT_ENTRY32(bit_error)
541STATS_SECT_ENTRY32(bit0_error)
542STATS_SECT_ENTRY32(bit1_error)
543STATS_SECT_ENTRY32(stuff_error)
544STATS_SECT_ENTRY32(crc_error)
545STATS_SECT_ENTRY32(form_error)
546STATS_SECT_ENTRY32(ack_error)
547STATS_SECT_ENTRY32(rx_overrun)
549
551STATS_NAME(can, bit_error)
552STATS_NAME(can, bit0_error)
553STATS_NAME(can, bit1_error)
554STATS_NAME(can, stuff_error)
555STATS_NAME(can, crc_error)
556STATS_NAME(can, form_error)
557STATS_NAME(can, ack_error)
558STATS_NAME(can, rx_overrun)
559STATS_NAME_END(can);
560
571 struct stats_can stats;
572};
573
579#define Z_CAN_GET_STATS(dev_) \
580 CONTAINER_OF(dev_->state, struct can_device_state, devstate)->stats
581
600#define CAN_STATS_BIT_ERROR_INC(dev_) \
601 STATS_INC(Z_CAN_GET_STATS(dev_), bit_error)
602
614#define CAN_STATS_BIT0_ERROR_INC(dev_) \
615 do { \
616 STATS_INC(Z_CAN_GET_STATS(dev_), bit0_error); \
617 CAN_STATS_BIT_ERROR_INC(dev_); \
618 } while (0)
619
631#define CAN_STATS_BIT1_ERROR_INC(dev_) \
632 do { \
633 STATS_INC(Z_CAN_GET_STATS(dev_), bit1_error); \
634 CAN_STATS_BIT_ERROR_INC(dev_); \
635 } while (0)
636
645#define CAN_STATS_STUFF_ERROR_INC(dev_) \
646 STATS_INC(Z_CAN_GET_STATS(dev_), stuff_error)
647
656#define CAN_STATS_CRC_ERROR_INC(dev_) \
657 STATS_INC(Z_CAN_GET_STATS(dev_), crc_error)
658
667#define CAN_STATS_FORM_ERROR_INC(dev_) \
668 STATS_INC(Z_CAN_GET_STATS(dev_), form_error)
669
678#define CAN_STATS_ACK_ERROR_INC(dev_) \
679 STATS_INC(Z_CAN_GET_STATS(dev_), ack_error)
680
690#define CAN_STATS_RX_OVERRUN_INC(dev_) \
691 STATS_INC(Z_CAN_GET_STATS(dev_), rx_overrun)
692
701#define CAN_STATS_RESET(dev_) \
702 stats_reset(&(Z_CAN_GET_STATS(dev_).s_hdr))
703
709#define Z_CAN_DEVICE_STATE_DEFINE(dev_id) \
710 static struct can_device_state Z_DEVICE_STATE_NAME(dev_id) \
711 __attribute__((__section__(".z_devstate")))
712
719#define Z_CAN_INIT_FN(dev_id, init_fn) \
720 static inline int UTIL_CAT(dev_id, _init)(const struct device *dev) \
721 { \
722 struct can_device_state *state = \
723 CONTAINER_OF(dev->state, struct can_device_state, devstate); \
724 stats_init(&state->stats.s_hdr, STATS_SIZE_32, 8, \
725 STATS_NAME_INIT_PARMS(can)); \
726 stats_register(dev->name, &(state->stats.s_hdr)); \
727 if (init_fn != NULL) { \
728 return init_fn(dev); \
729 } \
730 \
731 return 0; \
732 }
733
756#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
757 prio, api, ...) \
758 Z_CAN_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
759 Z_CAN_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn) \
760 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
761 DEVICE_DT_NAME(node_id), \
762 &UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init), \
763 pm, data, config, level, prio, api, \
764 &(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
765 __VA_ARGS__)
766
767#else /* CONFIG_CAN_STATS */
768
769#define CAN_STATS_BIT_ERROR_INC(dev_)
770#define CAN_STATS_BIT0_ERROR_INC(dev_)
771#define CAN_STATS_BIT1_ERROR_INC(dev_)
772#define CAN_STATS_STUFF_ERROR_INC(dev_)
773#define CAN_STATS_CRC_ERROR_INC(dev_)
774#define CAN_STATS_FORM_ERROR_INC(dev_)
775#define CAN_STATS_ACK_ERROR_INC(dev_)
776#define CAN_STATS_RX_OVERRUN_INC(dev_)
777#define CAN_STATS_RESET(dev_)
778
779#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
780 prio, api, ...) \
781 DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
782 prio, api, __VA_ARGS__)
783
784#endif /* CONFIG_CAN_STATS */
785
793#define CAN_DEVICE_DT_INST_DEFINE(inst, ...) \
794 CAN_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
795
812__syscall int can_get_core_clock(const struct device *dev, uint32_t *rate);
813
814static inline int z_impl_can_get_core_clock(const struct device *dev, uint32_t *rate)
815{
816 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
817
818 return api->get_core_clock(dev, rate);
819}
820
833__syscall int can_get_max_bitrate(const struct device *dev, uint32_t *max_bitrate);
834
835static inline int z_impl_can_get_max_bitrate(const struct device *dev, uint32_t *max_bitrate)
836{
837 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
838
839 if (common->max_bitrate == 0U) {
840 return -ENOSYS;
841 }
842
843 *max_bitrate = common->max_bitrate;
844
845 return 0;
846}
847
855__syscall const struct can_timing *can_get_timing_min(const struct device *dev);
856
857static inline const struct can_timing *z_impl_can_get_timing_min(const struct device *dev)
858{
859 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
860
861 return &api->timing_min;
862}
863
871__syscall const struct can_timing *can_get_timing_max(const struct device *dev);
872
873static inline const struct can_timing *z_impl_can_get_timing_max(const struct device *dev)
874{
875 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
876
877 return &api->timing_max;
878}
879
901__syscall int can_calc_timing(const struct device *dev, struct can_timing *res,
902 uint32_t bitrate, uint16_t sample_pnt);
903
917__syscall const struct can_timing *can_get_timing_data_min(const struct device *dev);
918
919#ifdef CONFIG_CAN_FD_MODE
920static inline const struct can_timing *z_impl_can_get_timing_data_min(const struct device *dev)
921{
922 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
923
924 return &api->timing_data_min;
925}
926#endif /* CONFIG_CAN_FD_MODE */
927
941__syscall const struct can_timing *can_get_timing_data_max(const struct device *dev);
942
943#ifdef CONFIG_CAN_FD_MODE
944static inline const struct can_timing *z_impl_can_get_timing_data_max(const struct device *dev)
945{
946 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
947
948 return &api->timing_data_max;
949}
950#endif /* CONFIG_CAN_FD_MODE */
951
971__syscall int can_calc_timing_data(const struct device *dev, struct can_timing *res,
972 uint32_t bitrate, uint16_t sample_pnt);
973
991__syscall int can_set_timing_data(const struct device *dev,
992 const struct can_timing *timing_data);
993
1022__syscall int can_set_bitrate_data(const struct device *dev, uint32_t bitrate_data);
1023
1040int can_calc_prescaler(const struct device *dev, struct can_timing *timing,
1041 uint32_t bitrate);
1042
1056__syscall int can_set_timing(const struct device *dev,
1057 const struct can_timing *timing);
1058
1072__syscall int can_get_capabilities(const struct device *dev, can_mode_t *cap);
1073
1074static inline int z_impl_can_get_capabilities(const struct device *dev, can_mode_t *cap)
1075{
1076 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1077
1078 return api->get_capabilities(dev, cap);
1079}
1080
1090__syscall const struct device *can_get_transceiver(const struct device *dev);
1091
1092static const struct device *z_impl_can_get_transceiver(const struct device *dev)
1093{
1094 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
1095
1096 return common->phy;
1097}
1098
1116__syscall int can_start(const struct device *dev);
1117
1118static inline int z_impl_can_start(const struct device *dev)
1119{
1120 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1121
1122 return api->start(dev);
1123}
1124
1140__syscall int can_stop(const struct device *dev);
1141
1142static inline int z_impl_can_stop(const struct device *dev)
1143{
1144 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1145
1146 return api->stop(dev);
1147}
1148
1159__syscall int can_set_mode(const struct device *dev, can_mode_t mode);
1160
1161static inline int z_impl_can_set_mode(const struct device *dev, can_mode_t mode)
1162{
1163 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1164
1165 return api->set_mode(dev, mode);
1166}
1167
1175__syscall can_mode_t can_get_mode(const struct device *dev);
1176
1177static inline can_mode_t z_impl_can_get_mode(const struct device *dev)
1178{
1179 const struct can_driver_data *common = (const struct can_driver_data *)dev->data;
1180
1181 return common->mode;
1182}
1183
1209__syscall int can_set_bitrate(const struct device *dev, uint32_t bitrate);
1210
1263__syscall int can_send(const struct device *dev, const struct can_frame *frame,
1265 void *user_data);
1266
1298static inline int can_add_rx_filter(const struct device *dev, can_rx_callback_t callback,
1299 void *user_data, const struct can_filter *filter)
1300{
1301 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1302
1303 if (filter == NULL) {
1304 return -EINVAL;
1305 }
1306
1307 return api->add_rx_filter(dev, callback, user_data, filter);
1308}
1309
1320#define CAN_MSGQ_DEFINE(name, max_frames) \
1321 K_MSGQ_DEFINE(name, sizeof(struct can_frame), max_frames, 4)
1322
1349__syscall int can_add_rx_filter_msgq(const struct device *dev, struct k_msgq *msgq,
1350 const struct can_filter *filter);
1351
1361__syscall void can_remove_rx_filter(const struct device *dev, int filter_id);
1362
1363static inline void z_impl_can_remove_rx_filter(const struct device *dev, int filter_id)
1364{
1365 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1366
1367 return api->remove_rx_filter(dev, filter_id);
1368}
1369
1383__syscall int can_get_max_filters(const struct device *dev, bool ide);
1384
1385static inline int z_impl_can_get_max_filters(const struct device *dev, bool ide)
1386{
1387 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1388
1389 if (api->get_max_filters == NULL) {
1390 return -ENOSYS;
1391 }
1392
1393 return api->get_max_filters(dev, ide);
1394}
1395
1417__syscall int can_get_state(const struct device *dev, enum can_state *state,
1418 struct can_bus_err_cnt *err_cnt);
1419
1420static inline int z_impl_can_get_state(const struct device *dev, enum can_state *state,
1421 struct can_bus_err_cnt *err_cnt)
1422{
1423 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1424
1425 return api->get_state(dev, state, err_cnt);
1426}
1427
1443#if !defined(CONFIG_CAN_AUTO_BUS_OFF_RECOVERY) || defined(__DOXYGEN__)
1444__syscall int can_recover(const struct device *dev, k_timeout_t timeout);
1445
1446static inline int z_impl_can_recover(const struct device *dev, k_timeout_t timeout)
1447{
1448 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1449
1450 return api->recover(dev, timeout);
1451}
1452#else /* CONFIG_CAN_AUTO_BUS_OFF_RECOVERY */
1453/* This implementation prevents inking errors for auto recovery */
1454static inline int z_impl_can_recover(const struct device *dev, k_timeout_t timeout)
1455{
1456 ARG_UNUSED(dev);
1457 ARG_UNUSED(timeout);
1458 return 0;
1459}
1460#endif /* !CONFIG_CAN_AUTO_BUS_OFF_RECOVERY */
1461
1475static inline void can_set_state_change_callback(const struct device *dev,
1477 void *user_data)
1478{
1479 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1480
1481 api->set_state_change_callback(dev, callback, user_data);
1482}
1483
1504__syscall uint32_t can_stats_get_bit_errors(const struct device *dev);
1505
1506#ifdef CONFIG_CAN_STATS
1507static inline uint32_t z_impl_can_stats_get_bit_errors(const struct device *dev)
1508{
1509 return Z_CAN_GET_STATS(dev).bit_error;
1510}
1511#endif /* CONFIG_CAN_STATS */
1512
1527__syscall uint32_t can_stats_get_bit0_errors(const struct device *dev);
1528
1529#ifdef CONFIG_CAN_STATS
1530static inline uint32_t z_impl_can_stats_get_bit0_errors(const struct device *dev)
1531{
1532 return Z_CAN_GET_STATS(dev).bit0_error;
1533}
1534#endif /* CONFIG_CAN_STATS */
1535
1550__syscall uint32_t can_stats_get_bit1_errors(const struct device *dev);
1551
1552#ifdef CONFIG_CAN_STATS
1553static inline uint32_t z_impl_can_stats_get_bit1_errors(const struct device *dev)
1554{
1555 return Z_CAN_GET_STATS(dev).bit1_error;
1556}
1557#endif /* CONFIG_CAN_STATS */
1558
1571__syscall uint32_t can_stats_get_stuff_errors(const struct device *dev);
1572
1573#ifdef CONFIG_CAN_STATS
1574static inline uint32_t z_impl_can_stats_get_stuff_errors(const struct device *dev)
1575{
1576 return Z_CAN_GET_STATS(dev).stuff_error;
1577}
1578#endif /* CONFIG_CAN_STATS */
1579
1592__syscall uint32_t can_stats_get_crc_errors(const struct device *dev);
1593
1594#ifdef CONFIG_CAN_STATS
1595static inline uint32_t z_impl_can_stats_get_crc_errors(const struct device *dev)
1596{
1597 return Z_CAN_GET_STATS(dev).crc_error;
1598}
1599#endif /* CONFIG_CAN_STATS */
1600
1613__syscall uint32_t can_stats_get_form_errors(const struct device *dev);
1614
1615#ifdef CONFIG_CAN_STATS
1616static inline uint32_t z_impl_can_stats_get_form_errors(const struct device *dev)
1617{
1618 return Z_CAN_GET_STATS(dev).form_error;
1619}
1620#endif /* CONFIG_CAN_STATS */
1621
1634__syscall uint32_t can_stats_get_ack_errors(const struct device *dev);
1635
1636#ifdef CONFIG_CAN_STATS
1637static inline uint32_t z_impl_can_stats_get_ack_errors(const struct device *dev)
1638{
1639 return Z_CAN_GET_STATS(dev).ack_error;
1640}
1641#endif /* CONFIG_CAN_STATS */
1642
1656__syscall uint32_t can_stats_get_rx_overruns(const struct device *dev);
1657
1658#ifdef CONFIG_CAN_STATS
1659static inline uint32_t z_impl_can_stats_get_rx_overruns(const struct device *dev)
1660{
1661 return Z_CAN_GET_STATS(dev).rx_overrun;
1662}
1663#endif /* CONFIG_CAN_STATS */
1664
1681{
1682 static const uint8_t dlc_table[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12,
1683 16, 20, 24, 32, 48, 64};
1684
1685 return dlc_table[MIN(dlc, ARRAY_SIZE(dlc_table) - 1)];
1686}
1687
1695static inline uint8_t can_bytes_to_dlc(uint8_t num_bytes)
1696{
1697 return num_bytes <= 8 ? num_bytes :
1698 num_bytes <= 12 ? 9 :
1699 num_bytes <= 16 ? 10 :
1700 num_bytes <= 20 ? 11 :
1701 num_bytes <= 24 ? 12 :
1702 num_bytes <= 32 ? 13 :
1703 num_bytes <= 48 ? 14 :
1704 15;
1705}
1706
1714static inline bool can_frame_matches_filter(const struct can_frame *frame,
1715 const struct can_filter *filter)
1716{
1717 if ((frame->flags & CAN_FRAME_IDE) != 0 && (filter->flags & CAN_FILTER_IDE) == 0) {
1718 /* Extended (29-bit) ID frame, standard (11-bit) filter */
1719 return false;
1720 }
1721
1722 if ((frame->flags & CAN_FRAME_IDE) == 0 && (filter->flags & CAN_FILTER_IDE) != 0) {
1723 /* Standard (11-bit) ID frame, extended (29-bit) filter */
1724 return false;
1725 }
1726
1727 if ((frame->id ^ filter->id) & filter->mask) {
1728 /* Masked ID mismatch */
1729 return false;
1730 }
1731
1732 return true;
1733}
1734
1741#ifdef __cplusplus
1742}
1743#endif
1744
1745#include <syscalls/can.h>
1746
1747#endif /* ZEPHYR_INCLUDE_DRIVERS_CAN_H_ */
ZTEST_BMEM int timeout
Definition main.c:31
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:882
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:116
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:1298
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_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.
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:1695
uint32_t can_stats_get_rx_overruns(const struct device *dev)
Get the RX overrun counter for a CAN device.
int can_get_max_bitrate(const struct device *dev, uint32_t *max_bitrate)
Get maximum supported bitrate.
#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:1680
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:1714
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: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:1475
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.
#define CAN_FILTER_IDE
Filter matches frames with extended (29-bit) CAN IDs.
Definition can.h:204
@ 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:373
#define ARRAY_SIZE(array)
Number of elements in the given array.
Definition util.h:124
#define DIV_ROUND_UP(n, d)
Divide and round up.
Definition util.h:318
#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.
struct k_msgq msgq
Definition test_msgq_contexts.c:12
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:567
struct device_state devstate
Common device state.
Definition can.h:569
struct stats_can stats
CAN device statistics.
Definition can.h:571
CAN filter structure.
Definition can.h:212
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:214
CAN frame structure.
Definition can.h:163
uint8_t dlc
Data Length Code (DLC) indicating data length in bytes.
Definition can.h:170
uint8_t flags
Flags.
Definition can.h:172
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:190
uint32_t data_32[DIV_ROUND_UP(CAN_MAX_DLEN, sizeof(uint32_t))]
Payload data accessed as unsigned 32 bit values.
Definition can.h:192
uint16_t timestamp
Captured value of the free-running timer in the CAN controller when this frame was received.
Definition can.h:181
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:358
Runtime device structure (in ROM) per driver instance.
Definition device.h:387
void * data
Address of the device instance private data.
Definition device.h:397
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:393
const void * config
Address of device instance config information.
Definition device.h:391
Message Queue Structure.
Definition kernel.h:4402
Kernel timeout type.
Definition sys_clock.h:65
static const intptr_t user_data[5]
Definition main.c:588
Misc utilities.