Zephyr API Documentation  3.7.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
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 {
179#if defined(CONFIG_CAN_RX_TIMESTAMP) || defined(__DOXYGEN__)
188#else
191 uint16_t reserved;
193#endif
195 union {
197 uint8_t data[CAN_MAX_DLEN];
199 uint32_t data_32[DIV_ROUND_UP(CAN_MAX_DLEN, sizeof(uint32_t))];
200 };
201};
202
211#define CAN_FILTER_IDE BIT(0)
212
227};
228
237};
238
282};
283
292typedef void (*can_tx_callback_t)(const struct device *dev, int error, void *user_data);
293
301typedef void (*can_rx_callback_t)(const struct device *dev, struct can_frame *frame,
302 void *user_data);
303
312typedef void (*can_state_change_callback_t)(const struct device *dev,
313 enum can_state state,
314 struct can_bus_err_cnt err_cnt,
315 void *user_data);
316
336#define CAN_CALC_TDCO(_timing_data, _tdco_min, _tdco_max) \
337 CLAMP((1U + _timing_data->prop_seg + _timing_data->phase_seg1) * _timing_data->prescaler, \
338 _tdco_min, _tdco_max)
339
346struct can_driver_config {
348 const struct device *phy;
350 uint32_t min_bitrate;
352 uint32_t max_bitrate;
354 uint32_t bitrate;
356 uint16_t sample_point;
357#ifdef CONFIG_CAN_FD_MODE
359 uint16_t sample_point_data;
361 uint32_t bitrate_data;
362#endif /* CONFIG_CAN_FD_MODE */
363};
364
372#define CAN_DT_DRIVER_CONFIG_GET(node_id, _min_bitrate, _max_bitrate) \
373 { \
374 .phy = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(node_id, phys)), \
375 .min_bitrate = DT_CAN_TRANSCEIVER_MIN_BITRATE(node_id, _min_bitrate), \
376 .max_bitrate = DT_CAN_TRANSCEIVER_MAX_BITRATE(node_id, _max_bitrate), \
377 .bitrate = DT_PROP_OR(node_id, bitrate, \
378 DT_PROP_OR(node_id, bus_speed, CONFIG_CAN_DEFAULT_BITRATE)), \
379 .sample_point = DT_PROP_OR(node_id, sample_point, 0), \
380 IF_ENABLED(CONFIG_CAN_FD_MODE, \
381 (.bitrate_data = DT_PROP_OR(node_id, bitrate_data, \
382 DT_PROP_OR(node_id, bus_speed_data, CONFIG_CAN_DEFAULT_BITRATE_DATA)), \
383 .sample_point_data = DT_PROP_OR(node_id, sample_point_data, 0),)) \
384 }
385
394#define CAN_DT_DRIVER_CONFIG_INST_GET(inst, _min_bitrate, _max_bitrate) \
395 CAN_DT_DRIVER_CONFIG_GET(DT_DRV_INST(inst), _min_bitrate, _max_bitrate)
396
403struct can_driver_data {
405 can_mode_t mode;
407 bool started;
409 can_state_change_callback_t state_change_cb;
411 void *state_change_cb_user_data;
412};
413
418typedef int (*can_set_timing_t)(const struct device *dev,
419 const struct can_timing *timing);
420
425typedef int (*can_set_timing_data_t)(const struct device *dev,
426 const struct can_timing *timing_data);
427
432typedef int (*can_get_capabilities_t)(const struct device *dev, can_mode_t *cap);
433
438typedef int (*can_start_t)(const struct device *dev);
439
444typedef int (*can_stop_t)(const struct device *dev);
445
450typedef int (*can_set_mode_t)(const struct device *dev, can_mode_t mode);
451
459typedef int (*can_send_t)(const struct device *dev,
460 const struct can_frame *frame,
461 k_timeout_t timeout, can_tx_callback_t callback,
462 void *user_data);
463
468typedef int (*can_add_rx_filter_t)(const struct device *dev,
469 can_rx_callback_t callback,
470 void *user_data,
471 const struct can_filter *filter);
472
477typedef void (*can_remove_rx_filter_t)(const struct device *dev, int filter_id);
478
483typedef int (*can_recover_t)(const struct device *dev, k_timeout_t timeout);
484
489typedef int (*can_get_state_t)(const struct device *dev, enum can_state *state,
490 struct can_bus_err_cnt *err_cnt);
491
496typedef void(*can_set_state_change_callback_t)(const struct device *dev,
498 void *user_data);
499
504typedef int (*can_get_core_clock_t)(const struct device *dev, uint32_t *rate);
505
510typedef int (*can_get_max_filters_t)(const struct device *dev, bool ide);
511
512__subsystem struct can_driver_api {
513 can_get_capabilities_t get_capabilities;
514 can_start_t start;
515 can_stop_t stop;
516 can_set_mode_t set_mode;
517 can_set_timing_t set_timing;
518 can_send_t send;
519 can_add_rx_filter_t add_rx_filter;
520 can_remove_rx_filter_t remove_rx_filter;
521#if defined(CONFIG_CAN_MANUAL_RECOVERY_MODE) || defined(__DOXYGEN__)
522 can_recover_t recover;
523#endif /* CONFIG_CAN_MANUAL_RECOVERY_MODE */
524 can_get_state_t get_state;
525 can_set_state_change_callback_t set_state_change_callback;
526 can_get_core_clock_t get_core_clock;
527 can_get_max_filters_t get_max_filters;
528 /* Min values for the timing registers */
529 struct can_timing timing_min;
530 /* Max values for the timing registers */
531 struct can_timing timing_max;
532#if defined(CONFIG_CAN_FD_MODE) || defined(__DOXYGEN__)
533 can_set_timing_data_t set_timing_data;
534 /* Min values for the timing registers during the data phase */
535 struct can_timing timing_data_min;
536 /* Max values for the timing registers during the data phase */
537 struct can_timing timing_data_max;
538#endif /* CONFIG_CAN_FD_MODE */
539};
540
543#if defined(CONFIG_CAN_STATS) || defined(__DOXYGEN__)
544
545#include <zephyr/stats/stats.h>
546
550STATS_SECT_ENTRY32(bit_error)
551STATS_SECT_ENTRY32(bit0_error)
552STATS_SECT_ENTRY32(bit1_error)
553STATS_SECT_ENTRY32(stuff_error)
554STATS_SECT_ENTRY32(crc_error)
555STATS_SECT_ENTRY32(form_error)
556STATS_SECT_ENTRY32(ack_error)
557STATS_SECT_ENTRY32(rx_overrun)
559
561STATS_NAME(can, bit_error)
562STATS_NAME(can, bit0_error)
563STATS_NAME(can, bit1_error)
564STATS_NAME(can, stuff_error)
565STATS_NAME(can, crc_error)
566STATS_NAME(can, form_error)
567STATS_NAME(can, ack_error)
568STATS_NAME(can, rx_overrun)
569STATS_NAME_END(can);
570
581 struct stats_can stats;
582};
583
589#define Z_CAN_GET_STATS(dev_) \
590 CONTAINER_OF(dev_->state, struct can_device_state, devstate)->stats
591
610#define CAN_STATS_BIT_ERROR_INC(dev_) \
611 STATS_INC(Z_CAN_GET_STATS(dev_), bit_error)
612
624#define CAN_STATS_BIT0_ERROR_INC(dev_) \
625 do { \
626 STATS_INC(Z_CAN_GET_STATS(dev_), bit0_error); \
627 CAN_STATS_BIT_ERROR_INC(dev_); \
628 } while (0)
629
641#define CAN_STATS_BIT1_ERROR_INC(dev_) \
642 do { \
643 STATS_INC(Z_CAN_GET_STATS(dev_), bit1_error); \
644 CAN_STATS_BIT_ERROR_INC(dev_); \
645 } while (0)
646
655#define CAN_STATS_STUFF_ERROR_INC(dev_) \
656 STATS_INC(Z_CAN_GET_STATS(dev_), stuff_error)
657
666#define CAN_STATS_CRC_ERROR_INC(dev_) \
667 STATS_INC(Z_CAN_GET_STATS(dev_), crc_error)
668
677#define CAN_STATS_FORM_ERROR_INC(dev_) \
678 STATS_INC(Z_CAN_GET_STATS(dev_), form_error)
679
688#define CAN_STATS_ACK_ERROR_INC(dev_) \
689 STATS_INC(Z_CAN_GET_STATS(dev_), ack_error)
690
700#define CAN_STATS_RX_OVERRUN_INC(dev_) \
701 STATS_INC(Z_CAN_GET_STATS(dev_), rx_overrun)
702
711#define CAN_STATS_RESET(dev_) \
712 stats_reset(&(Z_CAN_GET_STATS(dev_).s_hdr))
713
719#define Z_CAN_DEVICE_STATE_DEFINE(dev_id) \
720 static struct can_device_state Z_DEVICE_STATE_NAME(dev_id) \
721 __attribute__((__section__(".z_devstate")))
722
729#define Z_CAN_INIT_FN(dev_id, init_fn) \
730 static inline int UTIL_CAT(dev_id, _init)(const struct device *dev) \
731 { \
732 struct can_device_state *state = \
733 CONTAINER_OF(dev->state, struct can_device_state, devstate); \
734 stats_init(&state->stats.s_hdr, STATS_SIZE_32, 8, \
735 STATS_NAME_INIT_PARMS(can)); \
736 stats_register(dev->name, &(state->stats.s_hdr)); \
737 if (!is_null_no_warn(init_fn)) { \
738 return init_fn(dev); \
739 } \
740 \
741 return 0; \
742 }
743
766#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
767 prio, api, ...) \
768 Z_CAN_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
769 Z_CAN_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn) \
770 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
771 DEVICE_DT_NAME(node_id), \
772 &UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init), \
773 pm, data, config, level, prio, api, \
774 &(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
775 __VA_ARGS__)
776
777#else /* CONFIG_CAN_STATS */
778
779#define CAN_STATS_BIT_ERROR_INC(dev_)
780#define CAN_STATS_BIT0_ERROR_INC(dev_)
781#define CAN_STATS_BIT1_ERROR_INC(dev_)
782#define CAN_STATS_STUFF_ERROR_INC(dev_)
783#define CAN_STATS_CRC_ERROR_INC(dev_)
784#define CAN_STATS_FORM_ERROR_INC(dev_)
785#define CAN_STATS_ACK_ERROR_INC(dev_)
786#define CAN_STATS_RX_OVERRUN_INC(dev_)
787#define CAN_STATS_RESET(dev_)
788
789#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
790 prio, api, ...) \
791 DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
792 prio, api, __VA_ARGS__)
793
794#endif /* CONFIG_CAN_STATS */
795
803#define CAN_DEVICE_DT_INST_DEFINE(inst, ...) \
804 CAN_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
805
824__syscall int can_get_core_clock(const struct device *dev, uint32_t *rate);
825
826static inline int z_impl_can_get_core_clock(const struct device *dev, uint32_t *rate)
827{
828 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
829
830 return api->get_core_clock(dev, rate);
831}
832
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
863__deprecated static inline int can_get_min_bitrate(const struct device *dev, uint32_t *min_bitrate)
864{
865 *min_bitrate = can_get_bitrate_min(dev);
866
867 return 0;
868}
869
878__syscall uint32_t can_get_bitrate_max(const struct device *dev);
879
880static inline uint32_t z_impl_can_get_bitrate_max(const struct device *dev)
881{
882 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
883
884 return common->max_bitrate;
885}
886
901__deprecated static inline int can_get_max_bitrate(const struct device *dev, uint32_t *max_bitrate)
902{
903 *max_bitrate = can_get_bitrate_max(dev);
904
905 return 0;
906}
907
915__syscall const struct can_timing *can_get_timing_min(const struct device *dev);
916
917static inline const struct can_timing *z_impl_can_get_timing_min(const struct device *dev)
918{
919 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
920
921 return &api->timing_min;
922}
923
931__syscall const struct can_timing *can_get_timing_max(const struct device *dev);
932
933static inline const struct can_timing *z_impl_can_get_timing_max(const struct device *dev)
934{
935 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
936
937 return &api->timing_max;
938}
939
966__syscall int can_calc_timing(const struct device *dev, struct can_timing *res,
967 uint32_t bitrate, uint16_t sample_pnt);
968
982__syscall const struct can_timing *can_get_timing_data_min(const struct device *dev);
983
984#ifdef CONFIG_CAN_FD_MODE
985static inline const struct can_timing *z_impl_can_get_timing_data_min(const struct device *dev)
986{
987 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
988
989 return &api->timing_data_min;
990}
991#endif /* CONFIG_CAN_FD_MODE */
992
1006__syscall const struct can_timing *can_get_timing_data_max(const struct device *dev);
1007
1008#ifdef CONFIG_CAN_FD_MODE
1009static inline const struct can_timing *z_impl_can_get_timing_data_max(const struct device *dev)
1010{
1011 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1012
1013 return &api->timing_data_max;
1014}
1015#endif /* CONFIG_CAN_FD_MODE */
1016
1037__syscall int can_calc_timing_data(const struct device *dev, struct can_timing *res,
1038 uint32_t bitrate, uint16_t sample_pnt);
1039
1057__syscall int can_set_timing_data(const struct device *dev,
1058 const struct can_timing *timing_data);
1059
1088__syscall int can_set_bitrate_data(const struct device *dev, uint32_t bitrate_data);
1089
1110__deprecated int can_calc_prescaler(const struct device *dev, struct can_timing *timing,
1111 uint32_t bitrate);
1112
1126__syscall int can_set_timing(const struct device *dev,
1127 const struct can_timing *timing);
1128
1142__syscall int can_get_capabilities(const struct device *dev, can_mode_t *cap);
1143
1144static inline int z_impl_can_get_capabilities(const struct device *dev, can_mode_t *cap)
1145{
1146 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1147
1148 return api->get_capabilities(dev, cap);
1149}
1150
1160__syscall const struct device *can_get_transceiver(const struct device *dev);
1161
1162static const struct device *z_impl_can_get_transceiver(const struct device *dev)
1163{
1164 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
1165
1166 return common->phy;
1167}
1168
1186__syscall int can_start(const struct device *dev);
1187
1188static inline int z_impl_can_start(const struct device *dev)
1189{
1190 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1191
1192 return api->start(dev);
1193}
1194
1210__syscall int can_stop(const struct device *dev);
1211
1212static inline int z_impl_can_stop(const struct device *dev)
1213{
1214 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1215
1216 return api->stop(dev);
1217}
1218
1229__syscall int can_set_mode(const struct device *dev, can_mode_t mode);
1230
1231static inline int z_impl_can_set_mode(const struct device *dev, can_mode_t mode)
1232{
1233 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1234
1235 return api->set_mode(dev, mode);
1236}
1237
1245__syscall can_mode_t can_get_mode(const struct device *dev);
1246
1247static inline can_mode_t z_impl_can_get_mode(const struct device *dev)
1248{
1249 const struct can_driver_data *common = (const struct can_driver_data *)dev->data;
1250
1251 return common->mode;
1252}
1253
1279__syscall int can_set_bitrate(const struct device *dev, uint32_t bitrate);
1280
1333__syscall int can_send(const struct device *dev, const struct can_frame *frame,
1334 k_timeout_t timeout, can_tx_callback_t callback,
1335 void *user_data);
1336
1368int can_add_rx_filter(const struct device *dev, can_rx_callback_t callback,
1369 void *user_data, const struct can_filter *filter);
1370
1381#define CAN_MSGQ_DEFINE(name, max_frames) \
1382 K_MSGQ_DEFINE(name, sizeof(struct can_frame), max_frames, 4)
1383
1410__syscall int can_add_rx_filter_msgq(const struct device *dev, struct k_msgq *msgq,
1411 const struct can_filter *filter);
1412
1422__syscall void can_remove_rx_filter(const struct device *dev, int filter_id);
1423
1424static inline void z_impl_can_remove_rx_filter(const struct device *dev, int filter_id)
1425{
1426 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1427
1428 return api->remove_rx_filter(dev, filter_id);
1429}
1430
1444__syscall int can_get_max_filters(const struct device *dev, bool ide);
1445
1446static inline int z_impl_can_get_max_filters(const struct device *dev, bool ide)
1447{
1448 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1449
1450 if (api->get_max_filters == NULL) {
1451 return -ENOSYS;
1452 }
1453
1454 return api->get_max_filters(dev, ide);
1455}
1456
1478__syscall int can_get_state(const struct device *dev, enum can_state *state,
1479 struct can_bus_err_cnt *err_cnt);
1480
1481static inline int z_impl_can_get_state(const struct device *dev, enum can_state *state,
1482 struct can_bus_err_cnt *err_cnt)
1483{
1484 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1485
1486 return api->get_state(dev, state, err_cnt);
1487}
1488
1506__syscall int can_recover(const struct device *dev, k_timeout_t timeout);
1507
1508#ifdef CONFIG_CAN_MANUAL_RECOVERY_MODE
1509static inline int z_impl_can_recover(const struct device *dev, k_timeout_t timeout)
1510{
1511 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1512
1513 if (api->recover == NULL) {
1514 return -ENOSYS;
1515 }
1516
1517 return api->recover(dev, timeout);
1518}
1519#endif /* CONFIG_CAN_MANUAL_RECOVERY_MODE */
1520
1534static inline void can_set_state_change_callback(const struct device *dev,
1536 void *user_data)
1537{
1538 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1539
1540 api->set_state_change_callback(dev, callback, user_data);
1541}
1542
1563__syscall uint32_t can_stats_get_bit_errors(const struct device *dev);
1564
1565#ifdef CONFIG_CAN_STATS
1566static inline uint32_t z_impl_can_stats_get_bit_errors(const struct device *dev)
1567{
1568 return Z_CAN_GET_STATS(dev).bit_error;
1569}
1570#endif /* CONFIG_CAN_STATS */
1571
1586__syscall uint32_t can_stats_get_bit0_errors(const struct device *dev);
1587
1588#ifdef CONFIG_CAN_STATS
1589static inline uint32_t z_impl_can_stats_get_bit0_errors(const struct device *dev)
1590{
1591 return Z_CAN_GET_STATS(dev).bit0_error;
1592}
1593#endif /* CONFIG_CAN_STATS */
1594
1609__syscall uint32_t can_stats_get_bit1_errors(const struct device *dev);
1610
1611#ifdef CONFIG_CAN_STATS
1612static inline uint32_t z_impl_can_stats_get_bit1_errors(const struct device *dev)
1613{
1614 return Z_CAN_GET_STATS(dev).bit1_error;
1615}
1616#endif /* CONFIG_CAN_STATS */
1617
1630__syscall uint32_t can_stats_get_stuff_errors(const struct device *dev);
1631
1632#ifdef CONFIG_CAN_STATS
1633static inline uint32_t z_impl_can_stats_get_stuff_errors(const struct device *dev)
1634{
1635 return Z_CAN_GET_STATS(dev).stuff_error;
1636}
1637#endif /* CONFIG_CAN_STATS */
1638
1651__syscall uint32_t can_stats_get_crc_errors(const struct device *dev);
1652
1653#ifdef CONFIG_CAN_STATS
1654static inline uint32_t z_impl_can_stats_get_crc_errors(const struct device *dev)
1655{
1656 return Z_CAN_GET_STATS(dev).crc_error;
1657}
1658#endif /* CONFIG_CAN_STATS */
1659
1672__syscall uint32_t can_stats_get_form_errors(const struct device *dev);
1673
1674#ifdef CONFIG_CAN_STATS
1675static inline uint32_t z_impl_can_stats_get_form_errors(const struct device *dev)
1676{
1677 return Z_CAN_GET_STATS(dev).form_error;
1678}
1679#endif /* CONFIG_CAN_STATS */
1680
1693__syscall uint32_t can_stats_get_ack_errors(const struct device *dev);
1694
1695#ifdef CONFIG_CAN_STATS
1696static inline uint32_t z_impl_can_stats_get_ack_errors(const struct device *dev)
1697{
1698 return Z_CAN_GET_STATS(dev).ack_error;
1699}
1700#endif /* CONFIG_CAN_STATS */
1701
1715__syscall uint32_t can_stats_get_rx_overruns(const struct device *dev);
1716
1717#ifdef CONFIG_CAN_STATS
1718static inline uint32_t z_impl_can_stats_get_rx_overruns(const struct device *dev)
1719{
1720 return Z_CAN_GET_STATS(dev).rx_overrun;
1721}
1722#endif /* CONFIG_CAN_STATS */
1723
1740{
1741 static const uint8_t dlc_table[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12,
1742 16, 20, 24, 32, 48, 64};
1743
1744 return dlc_table[MIN(dlc, ARRAY_SIZE(dlc_table) - 1)];
1745}
1746
1754static inline uint8_t can_bytes_to_dlc(uint8_t num_bytes)
1755{
1756 return num_bytes <= 8 ? num_bytes :
1757 num_bytes <= 12 ? 9 :
1758 num_bytes <= 16 ? 10 :
1759 num_bytes <= 20 ? 11 :
1760 num_bytes <= 24 ? 12 :
1761 num_bytes <= 32 ? 13 :
1762 num_bytes <= 48 ? 14 :
1763 15;
1764}
1765
1773static inline bool can_frame_matches_filter(const struct can_frame *frame,
1774 const struct can_filter *filter)
1775{
1776 if ((frame->flags & CAN_FRAME_IDE) != 0 && (filter->flags & CAN_FILTER_IDE) == 0) {
1777 /* Extended (29-bit) ID frame, standard (11-bit) filter */
1778 return false;
1779 }
1780
1781 if ((frame->flags & CAN_FRAME_IDE) == 0 && (filter->flags & CAN_FILTER_IDE) != 0) {
1782 /* Standard (11-bit) ID frame, extended (29-bit) filter */
1783 return false;
1784 }
1785
1786 if ((frame->id ^ filter->id) & filter->mask) {
1787 /* Masked ID mismatch */
1788 return false;
1789 }
1790
1791 return true;
1792}
1793
1800#ifdef __cplusplus
1801}
1802#endif
1803
1804#include <zephyr/syscalls/can.h>
1805
1806#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:916
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:312
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
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:1754
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:1739
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:301
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:1773
void(* can_tx_callback_t)(const struct device *dev, int error, void *user_data)
Defines the application callback handler function signature.
Definition: can.h:292
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:1534
static int can_get_max_bitrate(const struct device *dev, uint32_t *max_bitrate)
Get maximum supported bitrate.
Definition: can.h:901
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.
static int can_get_min_bitrate(const struct device *dev, uint32_t *min_bitrate)
Get minimum supported bitrate.
Definition: can.h:863
#define CAN_FILTER_IDE
Filter matches frames with extended (29-bit) CAN IDs.
Definition: can.h:211
@ 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:391
#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:336
#define ENOSYS
Function not implemented.
Definition: errno.h:82
Public kernel APIs.
state
Definition: parser_state.h:29
Statistics.
#define STATS_NAME_END(name__)
Definition: stats.h:391
#define STATS_NAME(name__, entry__)
Definition: stats.h:390
#define STATS_SECT_END
Ends a stats group struct definition.
Definition: stats.h:89
#define STATS_SECT_ENTRY32(var__)
Definition: stats.h:359
#define STATS_NAME_START(name__)
Definition: stats.h:389
#define STATS_SECT_START(group__)
Definition: stats.h:354
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
CAN controller error counters.
Definition: can.h:232
uint8_t tx_err_cnt
Value of the CAN controller transmit error counter.
Definition: can.h:234
uint8_t rx_err_cnt
Value of the CAN controller receive error counter.
Definition: can.h:236
CAN specific device state which allows for CAN device class specific additions.
Definition: can.h:577
struct device_state devstate
Common device state.
Definition: can.h:579
struct stats_can stats
CAN device statistics.
Definition: can.h:581
CAN filter structure.
Definition: can.h:218
uint32_t mask
CAN identifier matching mask.
Definition: can.h:224
uint8_t flags
Flags.
Definition: can.h:226
uint32_t id
CAN identifier to match.
Definition: can.h:220
CAN frame structure.
Definition: can.h:172
uint8_t dlc
Data Length Code (DLC) indicating data length in bytes.
Definition: can.h:176
uint8_t flags
Flags.
Definition: can.h:178
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:197
uint32_t data_32[DIV_ROUND_UP(CAN_MAX_DLEN, sizeof(uint32_t))]
Payload data accessed as unsigned 32 bit values.
Definition: can.h:199
uint16_t timestamp
Captured value of the free-running timer in the CAN controller when this frame was received.
Definition: can.h:187
CAN bus timing structure.
Definition: can.h:271
uint16_t sjw
Synchronisation jump width.
Definition: can.h:273
uint16_t phase_seg2
Phase segment 2.
Definition: can.h:279
uint16_t prescaler
Prescaler value.
Definition: can.h:281
uint16_t phase_seg1
Phase segment 1.
Definition: can.h:277
uint16_t prop_seg
Propagation segment.
Definition: can.h:275
Runtime device dynamic structure (in RAM) per driver instance.
Definition: device.h:371
Runtime device structure (in ROM) per driver instance.
Definition: device.h:403
void * data
Address of the device instance private data.
Definition: device.h:413
const void * api
Address of the API structure exposed by the device instance.
Definition: device.h:409
const void * config
Address of device instance config information.
Definition: device.h:407
Message Queue Structure.
Definition: kernel.h:4426
Kernel timeout type.
Definition: sys_clock.h:65
Variables needed for system clock.
Misc utilities.