Zephyr API Documentation 4.2.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
319
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 min_bitrate;
349 uint32_t max_bitrate;
351 uint32_t bitrate;
353 uint16_t sample_point;
354#ifdef CONFIG_CAN_FD_MODE
356 uint16_t sample_point_data;
358 uint32_t bitrate_data;
359#endif /* CONFIG_CAN_FD_MODE */
360};
361
369#define CAN_DT_DRIVER_CONFIG_GET(node_id, _min_bitrate, _max_bitrate) \
370 { \
371 .phy = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(node_id, phys)), \
372 .min_bitrate = DT_CAN_TRANSCEIVER_MIN_BITRATE(node_id, _min_bitrate), \
373 .max_bitrate = DT_CAN_TRANSCEIVER_MAX_BITRATE(node_id, _max_bitrate), \
374 .bitrate = DT_PROP_OR(node_id, bitrate, \
375 DT_PROP_OR(node_id, bus_speed, CONFIG_CAN_DEFAULT_BITRATE)), \
376 .sample_point = DT_PROP_OR(node_id, sample_point, 0), \
377 IF_ENABLED(CONFIG_CAN_FD_MODE, \
378 (.bitrate_data = DT_PROP_OR(node_id, bitrate_data, \
379 DT_PROP_OR(node_id, bus_speed_data, CONFIG_CAN_DEFAULT_BITRATE_DATA)), \
380 .sample_point_data = DT_PROP_OR(node_id, sample_point_data, 0),)) \
381 }
382
391#define CAN_DT_DRIVER_CONFIG_INST_GET(inst, _min_bitrate, _max_bitrate) \
392 CAN_DT_DRIVER_CONFIG_GET(DT_DRV_INST(inst), _min_bitrate, _max_bitrate)
393
400struct can_driver_data {
402 can_mode_t mode;
404 bool started;
406 can_state_change_callback_t state_change_cb;
408 void *state_change_cb_user_data;
409};
410
415typedef int (*can_set_timing_t)(const struct device *dev,
416 const struct can_timing *timing);
417
422typedef int (*can_set_timing_data_t)(const struct device *dev,
423 const struct can_timing *timing_data);
424
429typedef int (*can_get_capabilities_t)(const struct device *dev, can_mode_t *cap);
430
435typedef int (*can_start_t)(const struct device *dev);
436
441typedef int (*can_stop_t)(const struct device *dev);
442
447typedef int (*can_set_mode_t)(const struct device *dev, can_mode_t mode);
448
456typedef int (*can_send_t)(const struct device *dev,
457 const struct can_frame *frame,
458 k_timeout_t timeout, can_tx_callback_t callback,
459 void *user_data);
460
465typedef int (*can_add_rx_filter_t)(const struct device *dev,
466 can_rx_callback_t callback,
467 void *user_data,
468 const struct can_filter *filter);
469
474typedef void (*can_remove_rx_filter_t)(const struct device *dev, int filter_id);
475
480typedef int (*can_recover_t)(const struct device *dev, k_timeout_t timeout);
481
486typedef int (*can_get_state_t)(const struct device *dev, enum can_state *state,
487 struct can_bus_err_cnt *err_cnt);
488
493typedef void(*can_set_state_change_callback_t)(const struct device *dev,
495 void *user_data);
496
501typedef int (*can_get_core_clock_t)(const struct device *dev, uint32_t *rate);
502
507typedef int (*can_get_max_filters_t)(const struct device *dev, bool ide);
508
509__subsystem struct can_driver_api {
510 can_get_capabilities_t get_capabilities;
511 can_start_t start;
512 can_stop_t stop;
513 can_set_mode_t set_mode;
514 can_set_timing_t set_timing;
515 can_send_t send;
516 can_add_rx_filter_t add_rx_filter;
517 can_remove_rx_filter_t remove_rx_filter;
518#if defined(CONFIG_CAN_MANUAL_RECOVERY_MODE) || defined(__DOXYGEN__)
519 can_recover_t recover;
520#endif /* CONFIG_CAN_MANUAL_RECOVERY_MODE */
521 can_get_state_t get_state;
522 can_set_state_change_callback_t set_state_change_callback;
523 can_get_core_clock_t get_core_clock;
524 can_get_max_filters_t get_max_filters;
525 /* Min values for the timing registers */
526 struct can_timing timing_min;
527 /* Max values for the timing registers */
528 struct can_timing timing_max;
529#if defined(CONFIG_CAN_FD_MODE) || defined(__DOXYGEN__)
530 can_set_timing_data_t set_timing_data;
531 /* Min values for the timing registers during the data phase */
532 struct can_timing timing_data_min;
533 /* Max values for the timing registers during the data phase */
534 struct can_timing timing_data_max;
535#endif /* CONFIG_CAN_FD_MODE */
536};
537
539
540#if defined(CONFIG_CAN_STATS) || defined(__DOXYGEN__)
541
542#include <zephyr/stats/stats.h>
543
545
547STATS_SECT_ENTRY32(bit_error)
548STATS_SECT_ENTRY32(bit0_error)
549STATS_SECT_ENTRY32(bit1_error)
550STATS_SECT_ENTRY32(stuff_error)
551STATS_SECT_ENTRY32(crc_error)
552STATS_SECT_ENTRY32(form_error)
553STATS_SECT_ENTRY32(ack_error)
554STATS_SECT_ENTRY32(rx_overrun)
556
558STATS_NAME(can, bit_error)
559STATS_NAME(can, bit0_error)
560STATS_NAME(can, bit1_error)
561STATS_NAME(can, stuff_error)
562STATS_NAME(can, crc_error)
563STATS_NAME(can, form_error)
564STATS_NAME(can, ack_error)
565STATS_NAME(can, rx_overrun)
566STATS_NAME_END(can);
567
569
578 struct stats_can stats;
579};
580
582
586#define Z_CAN_GET_STATS(dev_) \
587 CONTAINER_OF(dev_->state, struct can_device_state, devstate)->stats
588
590
607#define CAN_STATS_BIT_ERROR_INC(dev_) \
608 STATS_INC(Z_CAN_GET_STATS(dev_), bit_error)
609
621#define CAN_STATS_BIT0_ERROR_INC(dev_) \
622 do { \
623 STATS_INC(Z_CAN_GET_STATS(dev_), bit0_error); \
624 CAN_STATS_BIT_ERROR_INC(dev_); \
625 } while (0)
626
638#define CAN_STATS_BIT1_ERROR_INC(dev_) \
639 do { \
640 STATS_INC(Z_CAN_GET_STATS(dev_), bit1_error); \
641 CAN_STATS_BIT_ERROR_INC(dev_); \
642 } while (0)
643
652#define CAN_STATS_STUFF_ERROR_INC(dev_) \
653 STATS_INC(Z_CAN_GET_STATS(dev_), stuff_error)
654
663#define CAN_STATS_CRC_ERROR_INC(dev_) \
664 STATS_INC(Z_CAN_GET_STATS(dev_), crc_error)
665
674#define CAN_STATS_FORM_ERROR_INC(dev_) \
675 STATS_INC(Z_CAN_GET_STATS(dev_), form_error)
676
685#define CAN_STATS_ACK_ERROR_INC(dev_) \
686 STATS_INC(Z_CAN_GET_STATS(dev_), ack_error)
687
697#define CAN_STATS_RX_OVERRUN_INC(dev_) \
698 STATS_INC(Z_CAN_GET_STATS(dev_), rx_overrun)
699
708#define CAN_STATS_RESET(dev_) \
709 stats_reset(&(Z_CAN_GET_STATS(dev_).s_hdr))
710
712
716#define Z_CAN_DEVICE_STATE_DEFINE(dev_id) \
717 static struct can_device_state Z_DEVICE_STATE_NAME(dev_id) \
718 __attribute__((__section__(".z_devstate")))
719
726#define Z_CAN_INIT_FN(dev_id, init_fn) \
727 static inline int UTIL_CAT(dev_id, _init)(const struct device *dev) \
728 { \
729 struct can_device_state *state = \
730 CONTAINER_OF(dev->state, struct can_device_state, devstate); \
731 stats_init(&state->stats.s_hdr, STATS_SIZE_32, 8, \
732 STATS_NAME_INIT_PARMS(can)); \
733 stats_register(dev->name, &(state->stats.s_hdr)); \
734 if (!is_null_no_warn(init_fn)) { \
735 return init_fn(dev); \
736 } \
737 \
738 return 0; \
739 }
740
742
763#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
764 prio, api, ...) \
765 Z_CAN_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
766 Z_CAN_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn) \
767 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
768 DEVICE_DT_NAME(node_id), \
769 &UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init), \
770 NULL, Z_DEVICE_DT_FLAGS(node_id), pm, data, \
771 config, level, prio, api, \
772 &(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
773 __VA_ARGS__)
774
775#else /* CONFIG_CAN_STATS */
776
777#define CAN_STATS_BIT_ERROR_INC(dev_)
778#define CAN_STATS_BIT0_ERROR_INC(dev_)
779#define CAN_STATS_BIT1_ERROR_INC(dev_)
780#define CAN_STATS_STUFF_ERROR_INC(dev_)
781#define CAN_STATS_CRC_ERROR_INC(dev_)
782#define CAN_STATS_FORM_ERROR_INC(dev_)
783#define CAN_STATS_ACK_ERROR_INC(dev_)
784#define CAN_STATS_RX_OVERRUN_INC(dev_)
785#define CAN_STATS_RESET(dev_)
786
787#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
788 prio, api, ...) \
789 DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
790 prio, api, __VA_ARGS__)
791
792#endif /* CONFIG_CAN_STATS */
793
801#define CAN_DEVICE_DT_INST_DEFINE(inst, ...) \
802 CAN_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
803
809
822__syscall int can_get_core_clock(const struct device *dev, uint32_t *rate);
823
824static inline int z_impl_can_get_core_clock(const struct device *dev, uint32_t *rate)
825{
826 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
827
828 return api->get_core_clock(dev, rate);
829}
830
847__syscall uint32_t can_get_bitrate_min(const struct device *dev);
848
849static inline uint32_t z_impl_can_get_bitrate_min(const struct device *dev)
850{
851 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
852
853 return common->min_bitrate;
854}
855
872__syscall uint32_t can_get_bitrate_max(const struct device *dev);
873
874static inline uint32_t z_impl_can_get_bitrate_max(const struct device *dev)
875{
876 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
877
878 return common->max_bitrate;
879}
880
888__syscall const struct can_timing *can_get_timing_min(const struct device *dev);
889
890static inline const struct can_timing *z_impl_can_get_timing_min(const struct device *dev)
891{
892 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
893
894 return &api->timing_min;
895}
896
904__syscall const struct can_timing *can_get_timing_max(const struct device *dev);
905
906static inline const struct can_timing *z_impl_can_get_timing_max(const struct device *dev)
907{
908 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
909
910 return &api->timing_max;
911}
912
939__syscall int can_calc_timing(const struct device *dev, struct can_timing *res,
940 uint32_t bitrate, uint16_t sample_pnt);
941
955__syscall const struct can_timing *can_get_timing_data_min(const struct device *dev);
956
957#ifdef CONFIG_CAN_FD_MODE
958static inline const struct can_timing *z_impl_can_get_timing_data_min(const struct device *dev)
959{
960 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
961
962 return &api->timing_data_min;
963}
964#endif /* CONFIG_CAN_FD_MODE */
965
979__syscall const struct can_timing *can_get_timing_data_max(const struct device *dev);
980
981#ifdef CONFIG_CAN_FD_MODE
982static inline const struct can_timing *z_impl_can_get_timing_data_max(const struct device *dev)
983{
984 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
985
986 return &api->timing_data_max;
987}
988#endif /* CONFIG_CAN_FD_MODE */
989
1010__syscall int can_calc_timing_data(const struct device *dev, struct can_timing *res,
1011 uint32_t bitrate, uint16_t sample_pnt);
1012
1030__syscall int can_set_timing_data(const struct device *dev,
1031 const struct can_timing *timing_data);
1032
1061__syscall int can_set_bitrate_data(const struct device *dev, uint32_t bitrate_data);
1062
1076__syscall int can_set_timing(const struct device *dev,
1077 const struct can_timing *timing);
1078
1092__syscall int can_get_capabilities(const struct device *dev, can_mode_t *cap);
1093
1094static inline int z_impl_can_get_capabilities(const struct device *dev, can_mode_t *cap)
1095{
1096 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1097
1098 return api->get_capabilities(dev, cap);
1099}
1100
1110__syscall const struct device *can_get_transceiver(const struct device *dev);
1111
1112static const struct device *z_impl_can_get_transceiver(const struct device *dev)
1113{
1114 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
1115
1116 return common->phy;
1117}
1118
1136__syscall int can_start(const struct device *dev);
1137
1138static inline int z_impl_can_start(const struct device *dev)
1139{
1140 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1141
1142 return api->start(dev);
1143}
1144
1160__syscall int can_stop(const struct device *dev);
1161
1162static inline int z_impl_can_stop(const struct device *dev)
1163{
1164 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1165
1166 return api->stop(dev);
1167}
1168
1179__syscall int can_set_mode(const struct device *dev, can_mode_t mode);
1180
1181static inline int z_impl_can_set_mode(const struct device *dev, can_mode_t mode)
1182{
1183 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1184
1185 return api->set_mode(dev, mode);
1186}
1187
1195__syscall can_mode_t can_get_mode(const struct device *dev);
1196
1197static inline can_mode_t z_impl_can_get_mode(const struct device *dev)
1198{
1199 const struct can_driver_data *common = (const struct can_driver_data *)dev->data;
1200
1201 return common->mode;
1202}
1203
1229__syscall int can_set_bitrate(const struct device *dev, uint32_t bitrate);
1230
1232
1238
1283__syscall int can_send(const struct device *dev, const struct can_frame *frame,
1284 k_timeout_t timeout, can_tx_callback_t callback,
1285 void *user_data);
1286
1288
1294
1318int can_add_rx_filter(const struct device *dev, can_rx_callback_t callback,
1319 void *user_data, const struct can_filter *filter);
1320
1331#define CAN_MSGQ_DEFINE(name, max_frames) \
1332 K_MSGQ_DEFINE(name, sizeof(struct can_frame), max_frames, 4)
1333
1360__syscall int can_add_rx_filter_msgq(const struct device *dev, struct k_msgq *msgq,
1361 const struct can_filter *filter);
1362
1372__syscall void can_remove_rx_filter(const struct device *dev, int filter_id);
1373
1374static inline void z_impl_can_remove_rx_filter(const struct device *dev, int filter_id)
1375{
1376 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1377
1378 api->remove_rx_filter(dev, filter_id);
1379}
1380
1394__syscall int can_get_max_filters(const struct device *dev, bool ide);
1395
1396static inline int z_impl_can_get_max_filters(const struct device *dev, bool ide)
1397{
1398 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1399
1400 if (api->get_max_filters == NULL) {
1401 return -ENOSYS;
1402 }
1403
1404 return api->get_max_filters(dev, ide);
1405}
1406
1408
1414
1428__syscall int can_get_state(const struct device *dev, enum can_state *state,
1429 struct can_bus_err_cnt *err_cnt);
1430
1431static inline int z_impl_can_get_state(const struct device *dev, enum can_state *state,
1432 struct can_bus_err_cnt *err_cnt)
1433{
1434 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1435
1436 return api->get_state(dev, state, err_cnt);
1437}
1438
1456__syscall int can_recover(const struct device *dev, k_timeout_t timeout);
1457
1458#ifdef CONFIG_CAN_MANUAL_RECOVERY_MODE
1459static inline int z_impl_can_recover(const struct device *dev, k_timeout_t timeout)
1460{
1461 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1462
1463 if (api->recover == NULL) {
1464 return -ENOSYS;
1465 }
1466
1467 return api->recover(dev, timeout);
1468}
1469#endif /* CONFIG_CAN_MANUAL_RECOVERY_MODE */
1470
1484static inline void can_set_state_change_callback(const struct device *dev,
1486 void *user_data)
1487{
1488 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1489
1490 api->set_state_change_callback(dev, callback, user_data);
1491}
1492
1494
1500
1513__syscall uint32_t can_stats_get_bit_errors(const struct device *dev);
1514
1515#ifdef CONFIG_CAN_STATS
1516static inline uint32_t z_impl_can_stats_get_bit_errors(const struct device *dev)
1517{
1518 return Z_CAN_GET_STATS(dev).bit_error;
1519}
1520#endif /* CONFIG_CAN_STATS */
1521
1536__syscall uint32_t can_stats_get_bit0_errors(const struct device *dev);
1537
1538#ifdef CONFIG_CAN_STATS
1539static inline uint32_t z_impl_can_stats_get_bit0_errors(const struct device *dev)
1540{
1541 return Z_CAN_GET_STATS(dev).bit0_error;
1542}
1543#endif /* CONFIG_CAN_STATS */
1544
1559__syscall uint32_t can_stats_get_bit1_errors(const struct device *dev);
1560
1561#ifdef CONFIG_CAN_STATS
1562static inline uint32_t z_impl_can_stats_get_bit1_errors(const struct device *dev)
1563{
1564 return Z_CAN_GET_STATS(dev).bit1_error;
1565}
1566#endif /* CONFIG_CAN_STATS */
1567
1580__syscall uint32_t can_stats_get_stuff_errors(const struct device *dev);
1581
1582#ifdef CONFIG_CAN_STATS
1583static inline uint32_t z_impl_can_stats_get_stuff_errors(const struct device *dev)
1584{
1585 return Z_CAN_GET_STATS(dev).stuff_error;
1586}
1587#endif /* CONFIG_CAN_STATS */
1588
1601__syscall uint32_t can_stats_get_crc_errors(const struct device *dev);
1602
1603#ifdef CONFIG_CAN_STATS
1604static inline uint32_t z_impl_can_stats_get_crc_errors(const struct device *dev)
1605{
1606 return Z_CAN_GET_STATS(dev).crc_error;
1607}
1608#endif /* CONFIG_CAN_STATS */
1609
1622__syscall uint32_t can_stats_get_form_errors(const struct device *dev);
1623
1624#ifdef CONFIG_CAN_STATS
1625static inline uint32_t z_impl_can_stats_get_form_errors(const struct device *dev)
1626{
1627 return Z_CAN_GET_STATS(dev).form_error;
1628}
1629#endif /* CONFIG_CAN_STATS */
1630
1643__syscall uint32_t can_stats_get_ack_errors(const struct device *dev);
1644
1645#ifdef CONFIG_CAN_STATS
1646static inline uint32_t z_impl_can_stats_get_ack_errors(const struct device *dev)
1647{
1648 return Z_CAN_GET_STATS(dev).ack_error;
1649}
1650#endif /* CONFIG_CAN_STATS */
1651
1665__syscall uint32_t can_stats_get_rx_overruns(const struct device *dev);
1666
1667#ifdef CONFIG_CAN_STATS
1668static inline uint32_t z_impl_can_stats_get_rx_overruns(const struct device *dev)
1669{
1670 return Z_CAN_GET_STATS(dev).rx_overrun;
1671}
1672#endif /* CONFIG_CAN_STATS */
1673
1675
1681
1690{
1691 static const uint8_t dlc_table[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12,
1692 16, 20, 24, 32, 48, 64};
1693
1694 return dlc_table[MIN(dlc, ARRAY_SIZE(dlc_table) - 1)];
1695}
1696
1704static inline uint8_t can_bytes_to_dlc(uint8_t num_bytes)
1705{
1706 return num_bytes <= 8 ? num_bytes :
1707 num_bytes <= 12 ? 9 :
1708 num_bytes <= 16 ? 10 :
1709 num_bytes <= 20 ? 11 :
1710 num_bytes <= 24 ? 12 :
1711 num_bytes <= 32 ? 13 :
1712 num_bytes <= 48 ? 14 :
1713 15;
1714}
1715
1723static inline bool can_frame_matches_filter(const struct can_frame *frame,
1724 const struct can_filter *filter)
1725{
1726 if ((frame->flags & CAN_FRAME_IDE) != 0 && (filter->flags & CAN_FILTER_IDE) == 0) {
1727 /* Extended (29-bit) ID frame, standard (11-bit) filter */
1728 return false;
1729 }
1730
1731 if ((frame->flags & CAN_FRAME_IDE) == 0 && (filter->flags & CAN_FILTER_IDE) != 0) {
1732 /* Standard (11-bit) ID frame, extended (29-bit) filter */
1733 return false;
1734 }
1735
1736 if ((frame->id ^ filter->id) & filter->mask) {
1737 /* Masked ID mismatch */
1738 return false;
1739 }
1740
1741 return true;
1742}
1743
1745
1749
1750#ifdef __cplusplus
1751}
1752#endif
1753
1754#include <zephyr/syscalls/can.h>
1755
1756#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: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:1704
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:1689
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:1723
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:1484
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:402
#define ARRAY_SIZE(array)
Number of elements in the given array.
Definition util.h:121
#define DIV_ROUND_UP(n, d)
Divide and round up.
Definition util.h:353
#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: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:574
struct device_state devstate
Common device state.
Definition can.h:576
struct stats_can stats
CAN device statistics.
Definition can.h:578
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:455
Runtime device structure (in ROM) per driver instance.
Definition device.h:510
void * data
Address of the device instance private data.
Definition device.h:520
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:516
const void * config
Address of device instance config information.
Definition device.h:514
Message Queue Structure.
Definition kernel.h:4731
Kernel timeout type.
Definition clock.h:65
Misc utilities.