Zephyr API Documentation 4.5.0-rc1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
can.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021-2026 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
315
324typedef void (*can_state_change_callback_handler_t)(const struct device *dev,
325 struct can_state_change_callback *callback,
326 enum can_state state,
327 struct can_bus_err_cnt err_cnt);
348
353
367#define CAN_CALC_TDCO(_timing_data, _tdco_min, _tdco_max) \
368 CLAMP((1U + _timing_data->prop_seg + _timing_data->phase_seg1) * _timing_data->prescaler, \
369 _tdco_min, _tdco_max)
370
379 const struct device *phy;
388#if defined(CONFIG_CAN_FD_MODE) || defined(__DOXYGEN__)
393#endif /* CONFIG_CAN_FD_MODE */
394};
395
403#define CAN_DT_DRIVER_CONFIG_GET(node_id, _min_bitrate, _max_bitrate) \
404 { \
405 .phy = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(node_id, phys)), \
406 .min_bitrate = DT_CAN_TRANSCEIVER_MIN_BITRATE(node_id, _min_bitrate), \
407 .max_bitrate = DT_CAN_TRANSCEIVER_MAX_BITRATE(node_id, _max_bitrate), \
408 .bitrate = DT_PROP_OR(node_id, bitrate, CONFIG_CAN_DEFAULT_BITRATE), \
409 .sample_point = DT_PROP_OR(node_id, sample_point, 0), \
410 IF_ENABLED(CONFIG_CAN_FD_MODE, \
411 (.bitrate_data = DT_PROP_OR(node_id, bitrate_data, \
412 CONFIG_CAN_DEFAULT_BITRATE_DATA), \
413 .sample_point_data = DT_PROP_OR(node_id, sample_point_data, 0),)) \
414 }
415
424#define CAN_DT_DRIVER_CONFIG_INST_GET(inst, _min_bitrate, _max_bitrate) \
425 CAN_DT_DRIVER_CONFIG_GET(DT_DRV_INST(inst), _min_bitrate, _max_bitrate)
426
449
454typedef int (*can_set_timing_t)(const struct device *dev,
455 const struct can_timing *timing);
456
461typedef int (*can_set_timing_data_t)(const struct device *dev,
462 const struct can_timing *timing_data);
463
468typedef int (*can_get_capabilities_t)(const struct device *dev, can_mode_t *cap);
469
474typedef int (*can_start_t)(const struct device *dev);
475
480typedef int (*can_stop_t)(const struct device *dev);
481
486typedef int (*can_set_mode_t)(const struct device *dev, can_mode_t mode);
487
495typedef int (*can_send_t)(const struct device *dev,
496 const struct can_frame *frame,
497 k_timeout_t timeout, can_tx_callback_t callback,
498 void *user_data);
499
504typedef int (*can_add_rx_filter_t)(const struct device *dev,
505 can_rx_callback_t callback,
506 void *user_data,
507 const struct can_filter *filter);
508
513typedef void (*can_remove_rx_filter_t)(const struct device *dev, int filter_id);
514
519typedef int (*can_recover_t)(const struct device *dev, k_timeout_t timeout);
520
525typedef int (*can_get_state_t)(const struct device *dev, enum can_state *state,
526 struct can_bus_err_cnt *err_cnt);
527
532typedef int (*can_get_core_clock_t)(const struct device *dev, uint32_t *rate);
533
538typedef int (*can_get_max_filters_t)(const struct device *dev, bool ide);
539
551typedef int (*can_state_change_callbacks_enabled_t)(const struct device *dev, bool enabled);
552
561 struct can_bus_err_cnt err_cnt);
562
642
645
646#if defined(CONFIG_CAN_STATS) || defined(__DOXYGEN__)
647
648#include <zephyr/stats/stats.h>
649
651
653STATS_SECT_ENTRY32(bit_error)
654STATS_SECT_ENTRY32(bit0_error)
655STATS_SECT_ENTRY32(bit1_error)
656STATS_SECT_ENTRY32(stuff_error)
657STATS_SECT_ENTRY32(crc_error)
658STATS_SECT_ENTRY32(form_error)
659STATS_SECT_ENTRY32(ack_error)
660STATS_SECT_ENTRY32(rx_overrun)
662
664STATS_NAME(can, bit_error)
665STATS_NAME(can, bit0_error)
666STATS_NAME(can, bit1_error)
667STATS_NAME(can, stuff_error)
668STATS_NAME(can, crc_error)
669STATS_NAME(can, form_error)
670STATS_NAME(can, ack_error)
671STATS_NAME(can, rx_overrun)
672STATS_NAME_END(can);
673
675
686 struct stats_can stats;
687};
688
690
694#define Z_CAN_GET_STATS(dev_) \
695 CONTAINER_OF(dev_->state, struct can_device_state, devstate)->stats
696
698
717#define CAN_STATS_BIT_ERROR_INC(dev_) \
718 STATS_INC(Z_CAN_GET_STATS(dev_), bit_error)
719
733#define CAN_STATS_BIT0_ERROR_INC(dev_) \
734 do { \
735 STATS_INC(Z_CAN_GET_STATS(dev_), bit0_error); \
736 CAN_STATS_BIT_ERROR_INC(dev_); \
737 } while (0)
738
752#define CAN_STATS_BIT1_ERROR_INC(dev_) \
753 do { \
754 STATS_INC(Z_CAN_GET_STATS(dev_), bit1_error); \
755 CAN_STATS_BIT_ERROR_INC(dev_); \
756 } while (0)
757
768#define CAN_STATS_STUFF_ERROR_INC(dev_) \
769 STATS_INC(Z_CAN_GET_STATS(dev_), stuff_error)
770
781#define CAN_STATS_CRC_ERROR_INC(dev_) \
782 STATS_INC(Z_CAN_GET_STATS(dev_), crc_error)
783
794#define CAN_STATS_FORM_ERROR_INC(dev_) \
795 STATS_INC(Z_CAN_GET_STATS(dev_), form_error)
796
807#define CAN_STATS_ACK_ERROR_INC(dev_) \
808 STATS_INC(Z_CAN_GET_STATS(dev_), ack_error)
809
821#define CAN_STATS_RX_OVERRUN_INC(dev_) \
822 STATS_INC(Z_CAN_GET_STATS(dev_), rx_overrun)
823
834#define CAN_STATS_RESET(dev_) \
835 stats_reset(&(Z_CAN_GET_STATS(dev_).s_hdr))
836
838
844#define Z_CAN_DEVICE_STATE_DEFINE(dev_id) \
845 static struct can_device_state Z_DEVICE_STATE_NAME(dev_id) \
846 __attribute__((__section__(".z_devstate")))
847
856#define Z_CAN_INIT_FN(dev_id, init_fn) \
857 static inline int UTIL_CAT(dev_id, _init)(const struct device *dev) \
858 { \
859 struct can_device_state *state = \
860 CONTAINER_OF(dev->state, struct can_device_state, devstate); \
861 stats_init(&state->stats.s_hdr, STATS_SIZE_32, 8, \
862 STATS_NAME_INIT_PARMS(can)); \
863 stats_register(dev->name, &(state->stats.s_hdr)); \
864 if (!is_null_no_warn(init_fn)) { \
865 return init_fn(dev); \
866 } \
867 \
868 return 0; \
869 }
870
872
894#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
895 prio, api, ...) \
896 IF_ENABLED(CONFIG_NET_CANBUS, (Z_NET_CANBUS_DEVICE_DT_DEFINE(node_id))) \
897 Z_CAN_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
898 Z_CAN_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn) \
899 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
900 DEVICE_DT_NAME(node_id), \
901 &UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init), \
902 NULL, Z_DEVICE_DT_FLAGS(node_id), pm, data, \
903 config, level, prio, api, \
904 &(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
905 __VA_ARGS__)
906
907#else /* CONFIG_CAN_STATS */
908
909#define CAN_STATS_BIT_ERROR_INC(dev_)
910#define CAN_STATS_BIT0_ERROR_INC(dev_)
911#define CAN_STATS_BIT1_ERROR_INC(dev_)
912#define CAN_STATS_STUFF_ERROR_INC(dev_)
913#define CAN_STATS_CRC_ERROR_INC(dev_)
914#define CAN_STATS_FORM_ERROR_INC(dev_)
915#define CAN_STATS_ACK_ERROR_INC(dev_)
916#define CAN_STATS_RX_OVERRUN_INC(dev_)
917#define CAN_STATS_RESET(dev_)
918
919#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
920 prio, api, ...) \
921 IF_ENABLED(CONFIG_NET_CANBUS, (Z_NET_CANBUS_DEVICE_DT_DEFINE(node_id))) \
922 DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
923 prio, api, __VA_ARGS__)
924
925#endif /* CONFIG_CAN_STATS */
926
934#define CAN_DEVICE_DT_INST_DEFINE(inst, ...) \
935 CAN_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
936
942
955__syscall int can_get_core_clock(const struct device *dev, uint32_t *rate);
956
957static inline int z_impl_can_get_core_clock(const struct device *dev, uint32_t *rate)
958{
959 return DEVICE_API_GET(can, dev)->get_core_clock(dev, rate);
960}
961
978__syscall uint32_t can_get_bitrate_min(const struct device *dev);
979
980static inline uint32_t z_impl_can_get_bitrate_min(const struct device *dev)
981{
982 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
983
984 return common->min_bitrate;
985}
986
1003__syscall uint32_t can_get_bitrate_max(const struct device *dev);
1004
1005static inline uint32_t z_impl_can_get_bitrate_max(const struct device *dev)
1006{
1007 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
1008
1009 return common->max_bitrate;
1010}
1011
1019__syscall const struct can_timing *can_get_timing_min(const struct device *dev);
1020
1021static inline const struct can_timing *z_impl_can_get_timing_min(const struct device *dev)
1022{
1023 return &DEVICE_API_GET(can, dev)->timing_min;
1024}
1025
1033__syscall const struct can_timing *can_get_timing_max(const struct device *dev);
1034
1035static inline const struct can_timing *z_impl_can_get_timing_max(const struct device *dev)
1036{
1037 return &DEVICE_API_GET(can, dev)->timing_max;
1038}
1039
1066__syscall int can_calc_timing(const struct device *dev, struct can_timing *res,
1067 uint32_t bitrate, uint16_t sample_pnt);
1068
1082__syscall const struct can_timing *can_get_timing_data_min(const struct device *dev);
1083
1084#ifdef CONFIG_CAN_FD_MODE
1085static inline const struct can_timing *z_impl_can_get_timing_data_min(const struct device *dev)
1086{
1087 return &DEVICE_API_GET(can, dev)->timing_data_min;
1088}
1089#endif /* CONFIG_CAN_FD_MODE */
1090
1104__syscall const struct can_timing *can_get_timing_data_max(const struct device *dev);
1105
1106#ifdef CONFIG_CAN_FD_MODE
1107static inline const struct can_timing *z_impl_can_get_timing_data_max(const struct device *dev)
1108{
1109 return &DEVICE_API_GET(can, dev)->timing_data_max;
1110}
1111#endif /* CONFIG_CAN_FD_MODE */
1112
1133__syscall int can_calc_timing_data(const struct device *dev, struct can_timing *res,
1134 uint32_t bitrate, uint16_t sample_pnt);
1135
1153__syscall int can_set_timing_data(const struct device *dev,
1154 const struct can_timing *timing_data);
1155
1184__syscall int can_set_bitrate_data(const struct device *dev, uint32_t bitrate_data);
1185
1199__syscall int can_set_timing(const struct device *dev,
1200 const struct can_timing *timing);
1201
1215__syscall int can_get_capabilities(const struct device *dev, can_mode_t *cap);
1216
1217static inline int z_impl_can_get_capabilities(const struct device *dev, can_mode_t *cap)
1218{
1219 return DEVICE_API_GET(can, dev)->get_capabilities(dev, cap);
1220}
1221
1231__syscall const struct device *can_get_transceiver(const struct device *dev);
1232
1233static const struct device *z_impl_can_get_transceiver(const struct device *dev)
1234{
1235 const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
1236
1237 return common->phy;
1238}
1239
1257__syscall int can_start(const struct device *dev);
1258
1259static inline int z_impl_can_start(const struct device *dev)
1260{
1261 return DEVICE_API_GET(can, dev)->start(dev);
1262}
1263
1279__syscall int can_stop(const struct device *dev);
1280
1281static inline int z_impl_can_stop(const struct device *dev)
1282{
1283 return DEVICE_API_GET(can, dev)->stop(dev);
1284}
1285
1297__syscall int can_set_mode(const struct device *dev, can_mode_t mode);
1298
1299static inline int z_impl_can_set_mode(const struct device *dev, can_mode_t mode)
1300{
1301 return DEVICE_API_GET(can, dev)->set_mode(dev, mode);
1302}
1303
1311__syscall can_mode_t can_get_mode(const struct device *dev);
1312
1313static inline can_mode_t z_impl_can_get_mode(const struct device *dev)
1314{
1315 const struct can_driver_data *common = (const struct can_driver_data *)dev->data;
1316
1317 return common->mode;
1318}
1319
1345__syscall int can_set_bitrate(const struct device *dev, uint32_t bitrate);
1346
1348
1354
1399__syscall int can_send(const struct device *dev, const struct can_frame *frame,
1400 k_timeout_t timeout, can_tx_callback_t callback,
1401 void *user_data);
1402
1404
1410
1435int can_add_rx_filter(const struct device *dev, can_rx_callback_t callback,
1436 void *user_data, const struct can_filter *filter);
1437
1448#define CAN_MSGQ_DEFINE(name, max_frames) \
1449 K_MSGQ_DEFINE_TYPE(name, struct can_frame, max_frames)
1450
1491__syscall int can_add_rx_filter_msgq(const struct device *dev, struct k_msgq *msgq,
1492 const struct can_filter *filter);
1493
1503__syscall void can_remove_rx_filter(const struct device *dev, int filter_id);
1504
1505static inline void z_impl_can_remove_rx_filter(const struct device *dev, int filter_id)
1506{
1507 DEVICE_API_GET(can, dev)->remove_rx_filter(dev, filter_id);
1508}
1509
1523__syscall int can_get_max_filters(const struct device *dev, bool ide);
1524
1525static inline int z_impl_can_get_max_filters(const struct device *dev, bool ide)
1526{
1527 const struct can_driver_api *api = DEVICE_API_GET(can, dev);
1528
1529 if (api->get_max_filters == NULL) {
1530 return -ENOSYS;
1531 }
1532
1533 return api->get_max_filters(dev, ide);
1534}
1535
1537
1543
1557__syscall int can_get_state(const struct device *dev, enum can_state *state,
1558 struct can_bus_err_cnt *err_cnt);
1559
1560static inline int z_impl_can_get_state(const struct device *dev, enum can_state *state,
1561 struct can_bus_err_cnt *err_cnt)
1562{
1563 return DEVICE_API_GET(can, dev)->get_state(dev, state, err_cnt);
1564}
1565
1584__syscall int can_recover(const struct device *dev, k_timeout_t timeout);
1585
1586#ifdef CONFIG_CAN_MANUAL_RECOVERY_MODE
1587static inline int z_impl_can_recover(const struct device *dev, k_timeout_t timeout)
1588{
1589 const struct can_driver_api *api = DEVICE_API_GET(can, dev);
1590
1591 if (api->recover == NULL) {
1592 return -ENOSYS;
1593 }
1594
1595 return api->recover(dev, timeout);
1596}
1597#endif /* CONFIG_CAN_MANUAL_RECOVERY_MODE */
1598
1614__deprecated void can_set_state_change_callback(const struct device *dev,
1616 void *user_data);
1617
1626{
1627 __ASSERT_NO_MSG(callback != NULL);
1628 __ASSERT_NO_MSG(handler != NULL);
1629
1630 callback->handler = handler;
1631}
1632
1644 struct can_state_change_callback *callback);
1645
1657 struct can_state_change_callback *callback);
1658
1660
1666
1679__syscall uint32_t can_stats_get_bit_errors(const struct device *dev);
1680
1681#ifdef CONFIG_CAN_STATS
1682static inline uint32_t z_impl_can_stats_get_bit_errors(const struct device *dev)
1683{
1684 return Z_CAN_GET_STATS(dev).bit_error;
1685}
1686#endif /* CONFIG_CAN_STATS */
1687
1702__syscall uint32_t can_stats_get_bit0_errors(const struct device *dev);
1703
1704#ifdef CONFIG_CAN_STATS
1705static inline uint32_t z_impl_can_stats_get_bit0_errors(const struct device *dev)
1706{
1707 return Z_CAN_GET_STATS(dev).bit0_error;
1708}
1709#endif /* CONFIG_CAN_STATS */
1710
1725__syscall uint32_t can_stats_get_bit1_errors(const struct device *dev);
1726
1727#ifdef CONFIG_CAN_STATS
1728static inline uint32_t z_impl_can_stats_get_bit1_errors(const struct device *dev)
1729{
1730 return Z_CAN_GET_STATS(dev).bit1_error;
1731}
1732#endif /* CONFIG_CAN_STATS */
1733
1746__syscall uint32_t can_stats_get_stuff_errors(const struct device *dev);
1747
1748#ifdef CONFIG_CAN_STATS
1749static inline uint32_t z_impl_can_stats_get_stuff_errors(const struct device *dev)
1750{
1751 return Z_CAN_GET_STATS(dev).stuff_error;
1752}
1753#endif /* CONFIG_CAN_STATS */
1754
1767__syscall uint32_t can_stats_get_crc_errors(const struct device *dev);
1768
1769#ifdef CONFIG_CAN_STATS
1770static inline uint32_t z_impl_can_stats_get_crc_errors(const struct device *dev)
1771{
1772 return Z_CAN_GET_STATS(dev).crc_error;
1773}
1774#endif /* CONFIG_CAN_STATS */
1775
1788__syscall uint32_t can_stats_get_form_errors(const struct device *dev);
1789
1790#ifdef CONFIG_CAN_STATS
1791static inline uint32_t z_impl_can_stats_get_form_errors(const struct device *dev)
1792{
1793 return Z_CAN_GET_STATS(dev).form_error;
1794}
1795#endif /* CONFIG_CAN_STATS */
1796
1809__syscall uint32_t can_stats_get_ack_errors(const struct device *dev);
1810
1811#ifdef CONFIG_CAN_STATS
1812static inline uint32_t z_impl_can_stats_get_ack_errors(const struct device *dev)
1813{
1814 return Z_CAN_GET_STATS(dev).ack_error;
1815}
1816#endif /* CONFIG_CAN_STATS */
1817
1831__syscall uint32_t can_stats_get_rx_overruns(const struct device *dev);
1832
1833#ifdef CONFIG_CAN_STATS
1834static inline uint32_t z_impl_can_stats_get_rx_overruns(const struct device *dev)
1835{
1836 return Z_CAN_GET_STATS(dev).rx_overrun;
1837}
1838#endif /* CONFIG_CAN_STATS */
1839
1841
1847
1856{
1857 static const uint8_t dlc_table[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12,
1858 16, 20, 24, 32, 48, 64};
1859
1860 return dlc_table[MIN(dlc, ARRAY_SIZE(dlc_table) - 1)];
1861}
1862
1870static inline uint8_t can_bytes_to_dlc(uint8_t num_bytes)
1871{
1872 return num_bytes <= 8 ? num_bytes :
1873 num_bytes <= 12 ? 9 :
1874 num_bytes <= 16 ? 10 :
1875 num_bytes <= 20 ? 11 :
1876 num_bytes <= 24 ? 12 :
1877 num_bytes <= 32 ? 13 :
1878 num_bytes <= 48 ? 14 :
1879 15;
1880}
1881
1889static inline bool can_frame_matches_filter(const struct can_frame *frame,
1890 const struct can_filter *filter)
1891{
1892 if ((frame->flags & CAN_FRAME_IDE) != 0 && (filter->flags & CAN_FILTER_IDE) == 0) {
1893 /* Extended (29-bit) ID frame, standard (11-bit) filter */
1894 return false;
1895 }
1896
1897 if ((frame->flags & CAN_FRAME_IDE) == 0 && (filter->flags & CAN_FILTER_IDE) != 0) {
1898 /* Standard (11-bit) ID frame, extended (29-bit) filter */
1899 return false;
1900 }
1901
1902 if ((frame->id ^ filter->id) & filter->mask) {
1903 /* Masked ID mismatch */
1904 return false;
1905 }
1906
1907 return true;
1908}
1909
1911
1915
1916#ifdef __cplusplus
1917}
1918#endif
1919
1920#include <zephyr/syscalls/can.h>
1921
1922#if defined(CONFIG_NET_CANBUS)
1923/* Provides Z_NET_CANBUS_DEVICE_DT_DEFINE() for CAN_DEVICE_DT_DEFINE() */
1924#include <zephyr/net/canbus.h>
1925#endif
1926
1927#endif /* ZEPHYR_INCLUDE_DRIVERS_CAN_H_ */
CAN bus socket API definitions.
System clock APIs.
APIs and macros for the Zephyr device model.
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1486
System error numbers.
int(* can_set_timing_t)(const struct device *dev, const struct can_timing *timing)
Callback API upon setting CAN bus timing See can_set_timing() for argument description.
Definition can.h:454
int(* can_get_state_t)(const struct device *dev, enum can_state *state, struct can_bus_err_cnt *err_cnt)
Callback API upon getting the CAN controller state See can_get_state() for argument description.
Definition can.h:525
int(* can_stop_t)(const struct device *dev)
Callback API upon stopping CAN controller See can_stop() for argument description.
Definition can.h:480
void(* can_remove_rx_filter_t)(const struct device *dev, int filter_id)
Callback API upon removing an RX filter See can_remove_rx_filter() for argument description.
Definition can.h:513
int(* can_set_mode_t)(const struct device *dev, can_mode_t mode)
Callback API upon setting CAN controller mode See can_set_mode() for argument description.
Definition can.h:486
int(* can_get_capabilities_t)(const struct device *dev, can_mode_t *cap)
Callback API upon getting CAN controller capabilities See can_get_capabilities() for argument descrip...
Definition can.h:468
int(* can_add_rx_filter_t)(const struct device *dev, can_rx_callback_t callback, void *user_data, const struct can_filter *filter)
Callback API upon adding an RX filter See can_add_rx_filter() for argument description.
Definition can.h:504
int(* can_get_core_clock_t)(const struct device *dev, uint32_t *rate)
Callback API upon getting the CAN core clock rate See can_get_core_clock() for argument description.
Definition can.h:532
int(* can_set_timing_data_t)(const struct device *dev, const struct can_timing *timing_data)
Optional callback API upon setting CAN FD bus timing for the data phase.
Definition can.h:461
int(* can_state_change_callbacks_enabled_t)(const struct device *dev, bool enabled)
Optional callback API for notification on when state change callbacks are enabled.
Definition can.h:551
int(* can_send_t)(const struct device *dev, const struct can_frame *frame, k_timeout_t timeout, can_tx_callback_t callback, void *user_data)
Callback API upon sending a CAN frame See can_send() for argument description.
Definition can.h:495
int(* can_recover_t)(const struct device *dev, k_timeout_t timeout)
Optional callback API upon manually recovering the CAN controller from bus-off state See can_recover(...
Definition can.h:519
int(* can_get_max_filters_t)(const struct device *dev, bool ide)
Optional callback API upon getting the maximum number of concurrent CAN RX filters See can_get_max_fi...
Definition can.h:538
void can_fire_state_change_callbacks(const struct device *dev, enum can_state state, struct can_bus_err_cnt err_cnt)
Fire registered CAN state change handler callbacks.
int(* can_start_t)(const struct device *dev)
Callback API upon starting CAN controller See can_start() for argument description.
Definition can.h:474
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.
void(* can_state_change_callback_handler_t)(const struct device *dev, struct can_state_change_callback *callback, enum can_state state, struct can_bus_err_cnt err_cnt)
Defines the state change callback handler function signature.
Definition can.h:324
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_remove_state_change_callback(const struct device *dev, struct can_state_change_callback *callback)
Remove a callback for CAN controller state change events.
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:1870
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:1855
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:1889
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.
static void can_init_state_change_callback(struct can_state_change_callback *callback, can_state_change_callback_handler_t handler)
Initialize a CAN controller state change callback structure.
Definition can.h:1624
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
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.
int can_add_state_change_callback(const struct device *dev, struct can_state_change_callback *callback)
Add a callback for CAN controller state change events.
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
struct _slist sys_slist_t
Single-linked list structure.
Definition slist.h:54
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:44
#define STATS_SECT_END
Ends a stats group struct definition.
Definition stats.h:169
#define STATS_NAME(sectname__, entry__)
Add an entry name to a statistics entry name map.
Definition stats.h:519
#define STATS_SECT_ENTRY32(var__)
Declares a 32-bit stat entry inside a group struct.
Definition stats.h:204
#define STATS_NAME_START(sectname__)
Begin a statistics entry name map.
Definition stats.h:510
#define STATS_NAME_END(sectname__)
End a statistics entry name map.
Definition stats.h:527
#define STATS_SECT_START(group__)
Begins a stats group struct definition.
Definition stats.h:181
#define MIN(a, b)
Obtain the minimum of two values.
Definition util.h:406
#define ARRAY_SIZE(array)
Number of elements in the given array.
Definition util.h:118
#define DIV_ROUND_UP(n, d)
Divide and round up.
Definition util.h:348
#define ENOSYS
Function not implemented.
Definition errno.h:83
Public kernel APIs.
state
Definition parser_state.h:29
Statistics.
__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:682
struct device_state devstate
Common device state.
Definition can.h:684
struct stats_can stats
CAN device statistics.
Definition can.h:686
<span class="mlabel">Driver Operations</span> CAN Controller driver operations
Definition can.h:566
can_get_capabilities_t get_capabilities
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:570
can_set_timing_data_t set_timing_data
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition can.h:631
can_get_max_filters_t get_max_filters
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition can.h:621
can_set_timing_t set_timing
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:586
can_stop_t stop
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:578
can_add_rx_filter_t add_rx_filter
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:594
struct can_timing timing_data_min
Min values for the timing registers during the data phase.
Definition can.h:635
can_state_change_callbacks_enabled_t state_change_callbacks_enabled
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition can.h:613
struct can_timing timing_max
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:625
can_send_t send
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:590
can_get_state_t get_state
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:609
can_recover_t recover
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition can.h:604
struct can_timing timing_data_max
Max values for the timing registers during the data phase.
Definition can.h:639
can_remove_rx_filter_t remove_rx_filter
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:598
can_start_t start
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:574
can_set_mode_t set_mode
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:582
can_get_core_clock_t get_core_clock
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:617
struct can_timing timing_min
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition can.h:623
Common CAN controller driver configuration.
Definition can.h:377
uint16_t sample_point
Initial CAN classic/CAN FD arbitration phase sample point in permille.
Definition can.h:387
uint32_t max_bitrate
The maximum bitrate supported by the CAN controller/transceiver combination.
Definition can.h:383
uint32_t bitrate
Initial CAN classic/CAN FD arbitration phase bitrate.
Definition can.h:385
const struct device * phy
Pointer to the device structure for the associated CAN transceiver device or NULL.
Definition can.h:379
uint32_t min_bitrate
The minimum bitrate supported by the CAN controller/transceiver combination.
Definition can.h:381
uint16_t sample_point_data
Initial CAN FD data phase sample point in permille.
Definition can.h:390
uint32_t bitrate_data
Initial CAN FD data phase bitrate.
Definition can.h:392
Common CAN controller driver data.
Definition can.h:433
can_mode_t mode
Current CAN controller mode.
Definition can.h:435
struct can_state_change_callback legacy_state_change_cb
Legacy state change callback.
Definition can.h:439
bool started
True if the CAN controller is started, false otherwise.
Definition can.h:437
struct k_spinlock state_change_callback_lock
Lock for changing the list of state change callbacks.
Definition can.h:445
void * legacy_state_change_cb_user_data
Legacy state change callback user data pointer or NULL.
Definition can.h:443
can_state_change_callback_t legacy_state_change_cb_handler
Legacy state change callback function pointer or NULL.
Definition can.h:441
sys_slist_t state_change_callbacks
List of state change callbacks.
Definition can.h:447
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 state change callback.
Definition can.h:342
sys_snode_t node
Single-linked list node.
Definition can.h:344
can_state_change_callback_handler_t handler
Callback function.
Definition can.h:346
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:488
Runtime device structure (in ROM) per driver instance.
Definition device.h:543
void * data
Address of the device instance private data.
Definition device.h:553
const void * config
Address of device instance config information.
Definition device.h:547
Message Queue Structure.
Definition kernel.h:5257
Kernel Spin Lock.
Definition spinlock.h:45
Kernel timeout type.
Definition clock.h:65
Misc utilities.