Zephyr API Documentation  3.6.0
A Scalable Open Source RTOS
3.6.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
can.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 Vestas Wind Systems A/S
3 * Copyright (c) 2018 Karsten Koenig
4 * Copyright (c) 2018 Alexander Wachter
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
14#ifndef ZEPHYR_INCLUDE_DRIVERS_CAN_H_
15#define ZEPHYR_INCLUDE_DRIVERS_CAN_H_
16
17#include <errno.h>
18
19#include <zephyr/types.h>
20#include <zephyr/device.h>
21#include <zephyr/kernel.h>
22#include <string.h>
23#include <zephyr/sys_clock.h>
24#include <zephyr/sys/util.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
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
132};
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
234};
235
279};
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
326struct can_driver_config {
328 const struct device *phy;
330 uint32_t max_bitrate;
332 uint32_t bus_speed;
334 uint16_t sample_point;
335#ifdef CONFIG_CAN_FD_MODE
337 uint16_t sample_point_data;
339 uint32_t bus_speed_data;
340#endif /* CONFIG_CAN_FD_MODE */
341};
342
349#define CAN_DT_DRIVER_CONFIG_GET(node_id, _max_bitrate) \
350 { \
351 .phy = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(node_id, phys)), \
352 .max_bitrate = DT_CAN_TRANSCEIVER_MAX_BITRATE(node_id, _max_bitrate), \
353 .bus_speed = DT_PROP(node_id, bus_speed), \
354 .sample_point = DT_PROP_OR(node_id, sample_point, 0), \
355 IF_ENABLED(CONFIG_CAN_FD_MODE, \
356 (.bus_speed_data = DT_PROP_OR(node_id, bus_speed_data, 0), \
357 .sample_point_data = DT_PROP_OR(node_id, sample_point_data, 0),)) \
358 }
359
367#define CAN_DT_DRIVER_CONFIG_INST_GET(inst, _max_bitrate) \
368 CAN_DT_DRIVER_CONFIG_GET(DT_DRV_INST(inst), _max_bitrate)
369
376struct can_driver_data {
378 can_mode_t mode;
380 bool started;
382 can_state_change_callback_t state_change_cb;
384 void *state_change_cb_user_data;
385};
386
391typedef int (*can_set_timing_t)(const struct device *dev,
392 const struct can_timing *timing);
393
398typedef int (*can_set_timing_data_t)(const struct device *dev,
399 const struct can_timing *timing_data);
400
405typedef int (*can_get_capabilities_t)(const struct device *dev, can_mode_t *cap);
406
411typedef int (*can_start_t)(const struct device *dev);
412
417typedef int (*can_stop_t)(const struct device *dev);
418
423typedef int (*can_set_mode_t)(const struct device *dev, can_mode_t mode);
424
432typedef int (*can_send_t)(const struct device *dev,
433 const struct can_frame *frame,
434 k_timeout_t timeout, can_tx_callback_t callback,
435 void *user_data);
436
441typedef int (*can_add_rx_filter_t)(const struct device *dev,
442 can_rx_callback_t callback,
443 void *user_data,
444 const struct can_filter *filter);
445
450typedef void (*can_remove_rx_filter_t)(const struct device *dev, int filter_id);
451
456typedef int (*can_recover_t)(const struct device *dev, k_timeout_t timeout);
457
462typedef int (*can_get_state_t)(const struct device *dev, enum can_state *state,
463 struct can_bus_err_cnt *err_cnt);
464
469typedef void(*can_set_state_change_callback_t)(const struct device *dev,
471 void *user_data);
472
477typedef int (*can_get_core_clock_t)(const struct device *dev, uint32_t *rate);
478
483typedef int (*can_get_max_filters_t)(const struct device *dev, bool ide);
484
485__subsystem struct can_driver_api {
486 can_get_capabilities_t get_capabilities;
487 can_start_t start;
488 can_stop_t stop;
489 can_set_mode_t set_mode;
490 can_set_timing_t set_timing;
491 can_send_t send;
492 can_add_rx_filter_t add_rx_filter;
493 can_remove_rx_filter_t remove_rx_filter;
494#if !defined(CONFIG_CAN_AUTO_BUS_OFF_RECOVERY) || defined(__DOXYGEN__)
495 can_recover_t recover;
496#endif /* CONFIG_CAN_AUTO_BUS_OFF_RECOVERY */
497 can_get_state_t get_state;
498 can_set_state_change_callback_t set_state_change_callback;
499 can_get_core_clock_t get_core_clock;
500 can_get_max_filters_t get_max_filters;
501 /* Min values for the timing registers */
502 struct can_timing timing_min;
503 /* Max values for the timing registers */
504 struct can_timing timing_max;
505#if defined(CONFIG_CAN_FD_MODE) || defined(__DOXYGEN__)
506 can_set_timing_data_t set_timing_data;
507 /* Min values for the timing registers during the data phase */
508 struct can_timing timing_data_min;
509 /* Max values for the timing registers during the data phase */
510 struct can_timing timing_data_max;
511#endif /* CONFIG_CAN_FD_MODE */
512};
513
516#if defined(CONFIG_CAN_STATS) || defined(__DOXYGEN__)
517
518#include <zephyr/stats/stats.h>
519
523STATS_SECT_ENTRY32(bit_error)
524STATS_SECT_ENTRY32(bit0_error)
525STATS_SECT_ENTRY32(bit1_error)
526STATS_SECT_ENTRY32(stuff_error)
527STATS_SECT_ENTRY32(crc_error)
528STATS_SECT_ENTRY32(form_error)
529STATS_SECT_ENTRY32(ack_error)
530STATS_SECT_ENTRY32(rx_overrun)
532
534STATS_NAME(can, bit_error)
535STATS_NAME(can, bit0_error)
536STATS_NAME(can, bit1_error)
537STATS_NAME(can, stuff_error)
538STATS_NAME(can, crc_error)
539STATS_NAME(can, form_error)
540STATS_NAME(can, ack_error)
541STATS_NAME(can, rx_overrun)
542STATS_NAME_END(can);
543
554 struct stats_can stats;
555};
556
562#define Z_CAN_GET_STATS(dev_) \
563 CONTAINER_OF(dev_->state, struct can_device_state, devstate)->stats
564
583#define CAN_STATS_BIT_ERROR_INC(dev_) \
584 STATS_INC(Z_CAN_GET_STATS(dev_), bit_error)
585
597#define CAN_STATS_BIT0_ERROR_INC(dev_) \
598 do { \
599 STATS_INC(Z_CAN_GET_STATS(dev_), bit0_error); \
600 CAN_STATS_BIT_ERROR_INC(dev_); \
601 } while (0)
602
614#define CAN_STATS_BIT1_ERROR_INC(dev_) \
615 do { \
616 STATS_INC(Z_CAN_GET_STATS(dev_), bit1_error); \
617 CAN_STATS_BIT_ERROR_INC(dev_); \
618 } while (0)
619
628#define CAN_STATS_STUFF_ERROR_INC(dev_) \
629 STATS_INC(Z_CAN_GET_STATS(dev_), stuff_error)
630
639#define CAN_STATS_CRC_ERROR_INC(dev_) \
640 STATS_INC(Z_CAN_GET_STATS(dev_), crc_error)
641
650#define CAN_STATS_FORM_ERROR_INC(dev_) \
651 STATS_INC(Z_CAN_GET_STATS(dev_), form_error)
652
661#define CAN_STATS_ACK_ERROR_INC(dev_) \
662 STATS_INC(Z_CAN_GET_STATS(dev_), ack_error)
663
673#define CAN_STATS_RX_OVERRUN_INC(dev_) \
674 STATS_INC(Z_CAN_GET_STATS(dev_), rx_overrun)
675
684#define CAN_STATS_RESET(dev_) \
685 stats_reset(&(Z_CAN_GET_STATS(dev_).s_hdr))
686
692#define Z_CAN_DEVICE_STATE_DEFINE(dev_id) \
693 static struct can_device_state Z_DEVICE_STATE_NAME(dev_id) \
694 __attribute__((__section__(".z_devstate")))
695
702#define Z_CAN_INIT_FN(dev_id, init_fn) \
703 static inline int UTIL_CAT(dev_id, _init)(const struct device *dev) \
704 { \
705 struct can_device_state *state = \
706 CONTAINER_OF(dev->state, struct can_device_state, devstate); \
707 stats_init(&state->stats.s_hdr, STATS_SIZE_32, 8, \
708 STATS_NAME_INIT_PARMS(can)); \
709 stats_register(dev->name, &(state->stats.s_hdr)); \
710 if (init_fn != NULL) { \
711 return init_fn(dev); \
712 } \
713 \
714 return 0; \
715 }
716
739#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
740 prio, api, ...) \
741 Z_CAN_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
742 Z_CAN_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn) \
743 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
744 DEVICE_DT_NAME(node_id), \
745 &UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init), \
746 pm, data, config, level, prio, api, \
747 &(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
748 __VA_ARGS__)
749
750#else /* CONFIG_CAN_STATS */
751
752#define CAN_STATS_BIT_ERROR_INC(dev_)
753#define CAN_STATS_BIT0_ERROR_INC(dev_)
754#define CAN_STATS_BIT1_ERROR_INC(dev_)
755#define CAN_STATS_STUFF_ERROR_INC(dev_)
756#define CAN_STATS_CRC_ERROR_INC(dev_)
757#define CAN_STATS_FORM_ERROR_INC(dev_)
758#define CAN_STATS_ACK_ERROR_INC(dev_)
759#define CAN_STATS_RX_OVERRUN_INC(dev_)
760#define CAN_STATS_RESET(dev_)
761
762#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
763 prio, api, ...) \
764 DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
765 prio, api, __VA_ARGS__)
766
767#endif /* CONFIG_CAN_STATS */
768
776#define CAN_DEVICE_DT_INST_DEFINE(inst, ...) \
777 CAN_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
778
795__syscall int can_get_core_clock(const struct device *dev, uint32_t *rate);
796
797static inline int z_impl_can_get_core_clock(const struct device *dev, uint32_t *rate)
798{
799 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
800
801 return api->get_core_clock(dev, rate);
802}
803
816__syscall int can_get_max_bitrate(const struct device *dev, uint32_t *max_bitrate);
817
818static inline int z_impl_can_get_max_bitrate(const struct device *dev, uint32_t *max_bitrate)
819{
820 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
821
822 if (common->max_bitrate == 0U) {
823 return -ENOSYS;
824 }
825
826 *max_bitrate = common->max_bitrate;
827
828 return 0;
829}
830
838__syscall const struct can_timing *can_get_timing_min(const struct device *dev);
839
840static inline const struct can_timing *z_impl_can_get_timing_min(const struct device *dev)
841{
842 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
843
844 return &api->timing_min;
845}
846
854__syscall const struct can_timing *can_get_timing_max(const struct device *dev);
855
856static inline const struct can_timing *z_impl_can_get_timing_max(const struct device *dev)
857{
858 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
859
860 return &api->timing_max;
861}
862
884__syscall int can_calc_timing(const struct device *dev, struct can_timing *res,
885 uint32_t bitrate, uint16_t sample_pnt);
886
900__syscall const struct can_timing *can_get_timing_data_min(const struct device *dev);
901
902#ifdef CONFIG_CAN_FD_MODE
903static inline const struct can_timing *z_impl_can_get_timing_data_min(const struct device *dev)
904{
905 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
906
907 return &api->timing_data_min;
908}
909#endif /* CONFIG_CAN_FD_MODE */
910
924__syscall const struct can_timing *can_get_timing_data_max(const struct device *dev);
925
926#ifdef CONFIG_CAN_FD_MODE
927static inline const struct can_timing *z_impl_can_get_timing_data_max(const struct device *dev)
928{
929 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
930
931 return &api->timing_data_max;
932}
933#endif /* CONFIG_CAN_FD_MODE */
934
954__syscall int can_calc_timing_data(const struct device *dev, struct can_timing *res,
955 uint32_t bitrate, uint16_t sample_pnt);
956
974__syscall int can_set_timing_data(const struct device *dev,
975 const struct can_timing *timing_data);
976
1005__syscall int can_set_bitrate_data(const struct device *dev, uint32_t bitrate_data);
1006
1023int can_calc_prescaler(const struct device *dev, struct can_timing *timing,
1024 uint32_t bitrate);
1025
1039__syscall int can_set_timing(const struct device *dev,
1040 const struct can_timing *timing);
1041
1055__syscall int can_get_capabilities(const struct device *dev, can_mode_t *cap);
1056
1057static inline int z_impl_can_get_capabilities(const struct device *dev, can_mode_t *cap)
1058{
1059 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1060
1061 return api->get_capabilities(dev, cap);
1062}
1063
1073__syscall const struct device *can_get_transceiver(const struct device *dev);
1074
1075static const struct device *z_impl_can_get_transceiver(const struct device *dev)
1076{
1077 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
1078
1079 return common->phy;
1080}
1081
1099__syscall int can_start(const struct device *dev);
1100
1101static inline int z_impl_can_start(const struct device *dev)
1102{
1103 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1104
1105 return api->start(dev);
1106}
1107
1123__syscall int can_stop(const struct device *dev);
1124
1125static inline int z_impl_can_stop(const struct device *dev)
1126{
1127 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1128
1129 return api->stop(dev);
1130}
1131
1142__syscall int can_set_mode(const struct device *dev, can_mode_t mode);
1143
1144static inline int z_impl_can_set_mode(const struct device *dev, can_mode_t mode)
1145{
1146 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1147
1148 return api->set_mode(dev, mode);
1149}
1150
1158__syscall can_mode_t can_get_mode(const struct device *dev);
1159
1160static inline can_mode_t z_impl_can_get_mode(const struct device *dev)
1161{
1162 const struct can_driver_data *common = (const struct can_driver_data *)dev->data;
1163
1164 return common->mode;
1165}
1166
1192__syscall int can_set_bitrate(const struct device *dev, uint32_t bitrate);
1193
1246__syscall int can_send(const struct device *dev, const struct can_frame *frame,
1247 k_timeout_t timeout, can_tx_callback_t callback,
1248 void *user_data);
1249
1281static inline int can_add_rx_filter(const struct device *dev, can_rx_callback_t callback,
1282 void *user_data, const struct can_filter *filter)
1283{
1284 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1285
1286 if (filter == NULL) {
1287 return -EINVAL;
1288 }
1289
1290 return api->add_rx_filter(dev, callback, user_data, filter);
1291}
1292
1303#define CAN_MSGQ_DEFINE(name, max_frames) \
1304 K_MSGQ_DEFINE(name, sizeof(struct can_frame), max_frames, 4)
1305
1332__syscall int can_add_rx_filter_msgq(const struct device *dev, struct k_msgq *msgq,
1333 const struct can_filter *filter);
1334
1344__syscall void can_remove_rx_filter(const struct device *dev, int filter_id);
1345
1346static inline void z_impl_can_remove_rx_filter(const struct device *dev, int filter_id)
1347{
1348 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1349
1350 return api->remove_rx_filter(dev, filter_id);
1351}
1352
1366__syscall int can_get_max_filters(const struct device *dev, bool ide);
1367
1368static inline int z_impl_can_get_max_filters(const struct device *dev, bool ide)
1369{
1370 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1371
1372 if (api->get_max_filters == NULL) {
1373 return -ENOSYS;
1374 }
1375
1376 return api->get_max_filters(dev, ide);
1377}
1378
1400__syscall int can_get_state(const struct device *dev, enum can_state *state,
1401 struct can_bus_err_cnt *err_cnt);
1402
1403static inline int z_impl_can_get_state(const struct device *dev, enum can_state *state,
1404 struct can_bus_err_cnt *err_cnt)
1405{
1406 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1407
1408 return api->get_state(dev, state, err_cnt);
1409}
1410
1426#if !defined(CONFIG_CAN_AUTO_BUS_OFF_RECOVERY) || defined(__DOXYGEN__)
1427__syscall int can_recover(const struct device *dev, k_timeout_t timeout);
1428
1429static inline int z_impl_can_recover(const struct device *dev, k_timeout_t timeout)
1430{
1431 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1432
1433 return api->recover(dev, timeout);
1434}
1435#else /* CONFIG_CAN_AUTO_BUS_OFF_RECOVERY */
1436/* This implementation prevents inking errors for auto recovery */
1437static inline int z_impl_can_recover(const struct device *dev, k_timeout_t timeout)
1438{
1439 ARG_UNUSED(dev);
1440 ARG_UNUSED(timeout);
1441 return 0;
1442}
1443#endif /* !CONFIG_CAN_AUTO_BUS_OFF_RECOVERY */
1444
1458static inline void can_set_state_change_callback(const struct device *dev,
1460 void *user_data)
1461{
1462 const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
1463
1464 api->set_state_change_callback(dev, callback, user_data);
1465}
1466
1487__syscall uint32_t can_stats_get_bit_errors(const struct device *dev);
1488
1489#ifdef CONFIG_CAN_STATS
1490static inline uint32_t z_impl_can_stats_get_bit_errors(const struct device *dev)
1491{
1492 return Z_CAN_GET_STATS(dev).bit_error;
1493}
1494#endif /* CONFIG_CAN_STATS */
1495
1510__syscall uint32_t can_stats_get_bit0_errors(const struct device *dev);
1511
1512#ifdef CONFIG_CAN_STATS
1513static inline uint32_t z_impl_can_stats_get_bit0_errors(const struct device *dev)
1514{
1515 return Z_CAN_GET_STATS(dev).bit0_error;
1516}
1517#endif /* CONFIG_CAN_STATS */
1518
1533__syscall uint32_t can_stats_get_bit1_errors(const struct device *dev);
1534
1535#ifdef CONFIG_CAN_STATS
1536static inline uint32_t z_impl_can_stats_get_bit1_errors(const struct device *dev)
1537{
1538 return Z_CAN_GET_STATS(dev).bit1_error;
1539}
1540#endif /* CONFIG_CAN_STATS */
1541
1554__syscall uint32_t can_stats_get_stuff_errors(const struct device *dev);
1555
1556#ifdef CONFIG_CAN_STATS
1557static inline uint32_t z_impl_can_stats_get_stuff_errors(const struct device *dev)
1558{
1559 return Z_CAN_GET_STATS(dev).stuff_error;
1560}
1561#endif /* CONFIG_CAN_STATS */
1562
1575__syscall uint32_t can_stats_get_crc_errors(const struct device *dev);
1576
1577#ifdef CONFIG_CAN_STATS
1578static inline uint32_t z_impl_can_stats_get_crc_errors(const struct device *dev)
1579{
1580 return Z_CAN_GET_STATS(dev).crc_error;
1581}
1582#endif /* CONFIG_CAN_STATS */
1583
1596__syscall uint32_t can_stats_get_form_errors(const struct device *dev);
1597
1598#ifdef CONFIG_CAN_STATS
1599static inline uint32_t z_impl_can_stats_get_form_errors(const struct device *dev)
1600{
1601 return Z_CAN_GET_STATS(dev).form_error;
1602}
1603#endif /* CONFIG_CAN_STATS */
1604
1617__syscall uint32_t can_stats_get_ack_errors(const struct device *dev);
1618
1619#ifdef CONFIG_CAN_STATS
1620static inline uint32_t z_impl_can_stats_get_ack_errors(const struct device *dev)
1621{
1622 return Z_CAN_GET_STATS(dev).ack_error;
1623}
1624#endif /* CONFIG_CAN_STATS */
1625
1639__syscall uint32_t can_stats_get_rx_overruns(const struct device *dev);
1640
1641#ifdef CONFIG_CAN_STATS
1642static inline uint32_t z_impl_can_stats_get_rx_overruns(const struct device *dev)
1643{
1644 return Z_CAN_GET_STATS(dev).rx_overrun;
1645}
1646#endif /* CONFIG_CAN_STATS */
1647
1664{
1665 static const uint8_t dlc_table[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12,
1666 16, 20, 24, 32, 48, 64};
1667
1668 return dlc_table[MIN(dlc, ARRAY_SIZE(dlc_table) - 1)];
1669}
1670
1678static inline uint8_t can_bytes_to_dlc(uint8_t num_bytes)
1679{
1680 return num_bytes <= 8 ? num_bytes :
1681 num_bytes <= 12 ? 9 :
1682 num_bytes <= 16 ? 10 :
1683 num_bytes <= 20 ? 11 :
1684 num_bytes <= 24 ? 12 :
1685 num_bytes <= 32 ? 13 :
1686 num_bytes <= 48 ? 14 :
1687 15;
1688}
1689
1697static inline bool can_frame_matches_filter(const struct can_frame *frame,
1698 const struct can_filter *filter)
1699{
1700 if ((frame->flags & CAN_FRAME_IDE) != 0 && (filter->flags & CAN_FILTER_IDE) == 0) {
1701 /* Extended (29-bit) ID frame, standard (11-bit) filter */
1702 return false;
1703 }
1704
1705 if ((frame->flags & CAN_FRAME_IDE) == 0 && (filter->flags & CAN_FILTER_IDE) != 0) {
1706 /* Standard (11-bit) ID frame, extended (29-bit) filter */
1707 return false;
1708 }
1709
1710 if ((frame->id ^ filter->id) & filter->mask) {
1711 /* Masked ID mismatch */
1712 return false;
1713 }
1714
1715 return true;
1716}
1717
1724#ifdef __cplusplus
1725}
1726#endif
1727
1728#include <syscalls/can.h>
1729
1730#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: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:1281
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:1678
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:1663
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:1697
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:1458
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.
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:550
struct device_state devstate
Common device state.
Definition: can.h:552
struct stats_can stats
CAN device statistics.
Definition: can.h:554
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
Misc utilities.