can.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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. /**
  9. * @file
  10. * @brief Controller Area Network (CAN) driver API.
  11. */
  12. #ifndef CAN_H_
  13. #define CAN_H_
  14. #include <errno.h>
  15. #include <string.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /**
  20. * @brief CAN Interface
  21. * @defgroup can_interface CAN Interface
  22. * @since 1.12
  23. * @version 1.1.0
  24. * @ingroup io_interfaces
  25. * @{
  26. */
  27. /**
  28. * @name CAN frame definitions
  29. * @{
  30. */
  31. /**
  32. * @brief Bit mask for a standard (11-bit) CAN identifier.
  33. */
  34. #define CAN_STD_ID_MASK 0x7FFU
  35. /**
  36. * @brief Maximum value for a standard (11-bit) CAN identifier.
  37. *
  38. * @deprecated Use ``CAN_STD_ID_MASK`` instead.
  39. */
  40. #define CAN_MAX_STD_ID CAN_STD_ID_MASK __DEPRECATED_MACRO
  41. /**
  42. * @brief Bit mask for an extended (29-bit) CAN identifier.
  43. */
  44. #define CAN_EXT_ID_MASK 0x1FFFFFFFU
  45. /**
  46. * @brief Maximum value for an extended (29-bit) CAN identifier.
  47. *
  48. * @deprecated Use ``CAN_EXT_ID_MASK`` instead.
  49. */
  50. #define CAN_MAX_EXT_ID CAN_EXT_ID_MASK __DEPRECATED_MACRO
  51. /**
  52. * @brief Maximum data length code for CAN 2.0A/2.0B.
  53. */
  54. #define CAN_MAX_DLC 8U
  55. /**
  56. * @brief Maximum data length code for CAN FD.
  57. */
  58. #define CANFD_MAX_DLC 15U
  59. /**
  60. * @cond INTERNAL_HIDDEN
  61. * Internally calculated maximum data length
  62. */
  63. #ifndef CONFIG_CAN_FD_MODE
  64. #define CAN_MAX_DLEN 8U
  65. #else
  66. #define CAN_MAX_DLEN 64U
  67. #endif /* CONFIG_CAN_FD_MODE */
  68. /** @endcond */
  69. /** @} */
  70. /**
  71. * @name CAN controller mode flags
  72. * @anchor CAN_MODE_FLAGS
  73. *
  74. * @{
  75. */
  76. /** Normal mode. */
  77. #define CAN_MODE_NORMAL 0
  78. /** Controller is in loopback mode (receives own frames). */
  79. #define CAN_MODE_LOOPBACK BIT(0)
  80. /** Controller is not allowed to send dominant bits. */
  81. #define CAN_MODE_LISTENONLY BIT(1)
  82. /** Controller allows transmitting/receiving CAN FD frames. */
  83. #define CAN_MODE_FD BIT(2)
  84. /** Controller does not retransmit in case of lost arbitration or missing ACK */
  85. #define CAN_MODE_ONE_SHOT BIT(3)
  86. /** Controller uses triple sampling mode */
  87. #define CAN_MODE_3_SAMPLES BIT(4)
  88. /** Controller requires manual recovery after entering bus-off state */
  89. #define CAN_MODE_MANUAL_RECOVERY BIT(5)
  90. /** @} */
  91. struct device {
  92. void *config;
  93. void *data;
  94. const void *api;
  95. };
  96. /**
  97. * @brief Divide and round up.
  98. *
  99. * Example:
  100. * @code{.c}
  101. * DIV_ROUND_UP(1, 2); // 1
  102. * DIV_ROUND_UP(3, 2); // 2
  103. * @endcode
  104. *
  105. * @param n Numerator.
  106. * @param d Denominator.
  107. *
  108. * @return The result of @p n / @p d, rounded up.
  109. */
  110. #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
  111. typedef uint32_t k_ticks_t;
  112. typedef struct {
  113. k_ticks_t ticks;
  114. } k_timeout_t;
  115. #define K_NO_WAIT ((k_timeout_t) {0})
  116. #ifndef BIT
  117. #if defined(_ASMLANGUAGE)
  118. #define BIT(n) (1 << (n))
  119. #else
  120. /**
  121. * @brief Unsigned integer with bit position @p n set (signed in
  122. * assembly language).
  123. */
  124. #define BIT(n) (1UL << (n))
  125. #endif
  126. #endif
  127. #define ARG_UNUSED(x) (void)(x)
  128. /**
  129. * @brief Provides a type to hold CAN controller configuration flags.
  130. *
  131. * The lower 24 bits are reserved for common CAN controller mode flags. The upper 8 bits are
  132. * reserved for CAN controller/driver specific flags.
  133. *
  134. * @see @ref CAN_MODE_FLAGS.
  135. */
  136. typedef uint32_t can_mode_t;
  137. /**
  138. * @brief Defines the state of the CAN controller
  139. */
  140. enum can_state {
  141. /** Error-active state (RX/TX error count < 96). */
  142. CAN_STATE_ERROR_ACTIVE,
  143. /** Error-warning state (RX/TX error count < 128). */
  144. CAN_STATE_ERROR_WARNING,
  145. /** Error-passive state (RX/TX error count < 256). */
  146. CAN_STATE_ERROR_PASSIVE,
  147. /** Bus-off state (RX/TX error count >= 256). */
  148. CAN_STATE_BUS_OFF,
  149. /** CAN controller is stopped and does not participate in CAN communication. */
  150. CAN_STATE_STOPPED,
  151. };
  152. /**
  153. * @name CAN frame flags
  154. * @anchor CAN_FRAME_FLAGS
  155. *
  156. * @{
  157. */
  158. /** Frame uses extended (29-bit) CAN ID */
  159. #define CAN_FRAME_IDE BIT(0)
  160. /** Frame is a Remote Transmission Request (RTR) */
  161. #define CAN_FRAME_RTR BIT(1)
  162. /** Frame uses CAN FD format (FDF) */
  163. #define CAN_FRAME_FDF BIT(2)
  164. /** Frame uses CAN FD Baud Rate Switch (BRS). Only valid in combination with ``CAN_FRAME_FDF``. */
  165. #define CAN_FRAME_BRS BIT(3)
  166. /** CAN FD Error State Indicator (ESI). Indicates that the transmitting node is in error-passive
  167. * state. Only valid in combination with ``CAN_FRAME_FDF``.
  168. */
  169. #define CAN_FRAME_ESI BIT(4)
  170. /** @} */
  171. /**
  172. * @brief CAN frame structure
  173. */
  174. struct can_frame {
  175. /** Standard (11-bit) or extended (29-bit) CAN identifier. */
  176. uint32_t id;
  177. /** Data Length Code (DLC) indicating data length in bytes. */
  178. uint8_t dlc;
  179. /** Flags. @see @ref CAN_FRAME_FLAGS. */
  180. uint8_t flags;
  181. #if defined(CONFIG_CAN_RX_TIMESTAMP) || defined(__DOXYGEN__)
  182. /** Captured value of the free-running timer in the CAN controller when
  183. * this frame was received. The timer is incremented every bit time and
  184. * captured at the start of frame bit (SOF).
  185. *
  186. * @note @kconfig{CONFIG_CAN_RX_TIMESTAMP} must be selected for this
  187. * field to be available.
  188. */
  189. uint16_t timestamp;
  190. #else
  191. /** @cond INTERNAL_HIDDEN */
  192. /** Padding. */
  193. uint16_t reserved;
  194. /** @endcond */
  195. #endif
  196. /** The frame payload data. */
  197. union {
  198. /** Payload data accessed as unsigned 8 bit values. */
  199. uint8_t data[CAN_MAX_DLEN];
  200. /** Payload data accessed as unsigned 32 bit values. */
  201. uint32_t data_32[DIV_ROUND_UP(CAN_MAX_DLEN, sizeof(uint32_t))];
  202. };
  203. };
  204. /**
  205. * @name CAN filter flags
  206. * @anchor CAN_FILTER_FLAGS
  207. *
  208. * @{
  209. */
  210. /** Filter matches frames with extended (29-bit) CAN IDs */
  211. #define CAN_FILTER_IDE BIT(0)
  212. /** @} */
  213. /**
  214. * @brief CAN filter structure
  215. */
  216. struct can_filter {
  217. /** CAN identifier to match. */
  218. uint32_t id;
  219. /** CAN identifier matching mask. If a bit in this mask is 0, the value
  220. * of the corresponding bit in the ``id`` field is ignored by the filter.
  221. */
  222. uint32_t mask;
  223. /** Flags. @see @ref CAN_FILTER_FLAGS. */
  224. uint8_t flags;
  225. };
  226. /**
  227. * @brief CAN controller error counters
  228. */
  229. struct can_bus_err_cnt {
  230. /** Value of the CAN controller transmit error counter. */
  231. uint8_t tx_err_cnt;
  232. /** Value of the CAN controller receive error counter. */
  233. uint8_t rx_err_cnt;
  234. };
  235. /**
  236. * @brief CAN bus timing structure
  237. *
  238. * This struct is used to pass bus timing values to the configuration and
  239. * bitrate calculation functions.
  240. *
  241. * The propagation segment represents the time of the signal propagation. Phase
  242. * segment 1 and phase segment 2 define the sampling point. The ``prop_seg`` and
  243. * ``phase_seg1`` values affect the sampling point in the same way and some
  244. * controllers only have a register for the sum of those two. The sync segment
  245. * always has a length of 1 time quantum (see below).
  246. *
  247. * @code{.text}
  248. *
  249. * +---------+----------+------------+------------+
  250. * |sync_seg | prop_seg | phase_seg1 | phase_seg2 |
  251. * +---------+----------+------------+------------+
  252. * ^
  253. * Sampling-Point
  254. *
  255. * @endcode
  256. *
  257. * 1 time quantum (tq) has the length of 1/(core_clock / prescaler). The bitrate
  258. * is defined by the core clock divided by the prescaler and the sum of the
  259. * segments:
  260. *
  261. * br = (core_clock / prescaler) / (1 + prop_seg + phase_seg1 + phase_seg2)
  262. *
  263. * The Synchronization Jump Width (SJW) defines the amount of time quanta the
  264. * sample point can be moved. The sample point is moved when resynchronization
  265. * is needed.
  266. */
  267. struct can_timing {
  268. /** Synchronisation jump width. */
  269. uint16_t sjw;
  270. /** Propagation segment. */
  271. uint16_t prop_seg;
  272. /** Phase segment 1. */
  273. uint16_t phase_seg1;
  274. /** Phase segment 2. */
  275. uint16_t phase_seg2;
  276. /** Prescaler value. */
  277. uint16_t prescaler;
  278. };
  279. /**
  280. * @brief Defines the application callback handler function signature
  281. *
  282. * @param dev Pointer to the device structure for the driver instance.
  283. * @param error Status of the performed send operation. See the list of
  284. * return values for @a can_send() for value descriptions.
  285. * @param user_data User data provided when the frame was sent.
  286. */
  287. typedef void (*can_tx_callback_t)(const struct device *dev, int error, void *user_data);
  288. /**
  289. * @brief Defines the application callback handler function signature for receiving.
  290. *
  291. * @param dev Pointer to the device structure for the driver instance.
  292. * @param frame Received frame.
  293. * @param user_data User data provided when the filter was added.
  294. */
  295. typedef void (*can_rx_callback_t)(const struct device *dev, struct can_frame *frame,
  296. void *user_data);
  297. /**
  298. * @brief Defines the state change callback handler function signature
  299. *
  300. * @param dev Pointer to the device structure for the driver instance.
  301. * @param state State of the CAN controller.
  302. * @param err_cnt CAN controller error counter values.
  303. * @param user_data User data provided the callback was set.
  304. */
  305. typedef void (*can_state_change_callback_t)(const struct device *dev,
  306. enum can_state state,
  307. struct can_bus_err_cnt err_cnt,
  308. void *user_data);
  309. /**
  310. * @cond INTERNAL_HIDDEN
  311. *
  312. * For internal driver use only, skip these in public documentation.
  313. */
  314. /**
  315. * @brief Calculate Transmitter Delay Compensation Offset from data phase timing parameters.
  316. *
  317. * Calculates the TDC Offset in minimum time quanta (mtq) using the sample point and CAN core clock
  318. * prescaler specified by a set of data phase timing parameters.
  319. *
  320. * The result is clamped to the minimum/maximum supported TDC Offset values provided.
  321. *
  322. * @param _timing_data Pointer to data phase timing parameters.
  323. * @param _tdco_min Minimum supported TDC Offset value in mtq.
  324. * @param _tdco_max Maximum supported TDC Offset value in mtq.
  325. * @return Calculated TDC Offset value in mtq.
  326. */
  327. #define CAN_CALC_TDCO(_timing_data, _tdco_min, _tdco_max) \
  328. CLAMP((1U + _timing_data->prop_seg + _timing_data->phase_seg1) * _timing_data->prescaler, \
  329. _tdco_min, _tdco_max)
  330. /**
  331. * @brief Common CAN controller driver configuration.
  332. *
  333. * This structure is common to all CAN controller drivers and is expected to be the first element in
  334. * the object pointed to by the config field in the device structure.
  335. */
  336. struct can_driver_config {
  337. /** Pointer to the device structure for the associated CAN transceiver device or NULL. */
  338. const struct device *phy;
  339. /** The minimum bitrate supported by the CAN controller/transceiver combination. */
  340. uint32_t min_bitrate;
  341. /** The maximum bitrate supported by the CAN controller/transceiver combination. */
  342. uint32_t max_bitrate;
  343. /** Initial CAN classic/CAN FD arbitration phase bitrate. */
  344. uint32_t bitrate;
  345. /** Initial CAN classic/CAN FD arbitration phase sample point in permille. */
  346. uint16_t sample_point;
  347. #ifdef CONFIG_CAN_FD_MODE
  348. /** Initial CAN FD data phase sample point in permille. */
  349. uint16_t sample_point_data;
  350. /** Initial CAN FD data phase bitrate. */
  351. uint32_t bitrate_data;
  352. #endif /* CONFIG_CAN_FD_MODE */
  353. };
  354. /**
  355. * @brief Static initializer for @p can_driver_config struct
  356. *
  357. * @param node_id Devicetree node identifier
  358. * @param _min_bitrate minimum bitrate supported by the CAN controller
  359. * @param _max_bitrate maximum bitrate supported by the CAN controller
  360. */
  361. #define CAN_DT_DRIVER_CONFIG_GET(node_id, _min_bitrate, _max_bitrate) \
  362. { \
  363. .phy = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(node_id, phys)), \
  364. .min_bitrate = DT_CAN_TRANSCEIVER_MIN_BITRATE(node_id, _min_bitrate), \
  365. .max_bitrate = DT_CAN_TRANSCEIVER_MAX_BITRATE(node_id, _max_bitrate), \
  366. .bitrate = DT_PROP_OR(node_id, bitrate, \
  367. DT_PROP_OR(node_id, bus_speed, CONFIG_CAN_DEFAULT_BITRATE)), \
  368. .sample_point = DT_PROP_OR(node_id, sample_point, 0), \
  369. IF_ENABLED(CONFIG_CAN_FD_MODE, \
  370. (.bitrate_data = DT_PROP_OR(node_id, bitrate_data, \
  371. DT_PROP_OR(node_id, bus_speed_data, CONFIG_CAN_DEFAULT_BITRATE_DATA)), \
  372. .sample_point_data = DT_PROP_OR(node_id, sample_point_data, 0),)) \
  373. }
  374. /**
  375. * @brief Static initializer for @p can_driver_config struct from DT_DRV_COMPAT instance
  376. *
  377. * @param inst DT_DRV_COMPAT instance number
  378. * @param _min_bitrate minimum bitrate supported by the CAN controller
  379. * @param _max_bitrate maximum bitrate supported by the CAN controller
  380. * @see CAN_DT_DRIVER_CONFIG_GET()
  381. */
  382. #define CAN_DT_DRIVER_CONFIG_INST_GET(inst, _min_bitrate, _max_bitrate) \
  383. CAN_DT_DRIVER_CONFIG_GET(DT_DRV_INST(inst), _min_bitrate, _max_bitrate)
  384. /**
  385. * @brief Common CAN controller driver data.
  386. *
  387. * This structure is common to all CAN controller drivers and is expected to be the first element in
  388. * the driver's struct driver_data declaration.
  389. */
  390. struct can_driver_data {
  391. /** Current CAN controller mode. */
  392. can_mode_t mode;
  393. /** True if the CAN controller is started, false otherwise. */
  394. bool started;
  395. /** State change callback function pointer or NULL. */
  396. can_state_change_callback_t state_change_cb;
  397. /** State change callback user data pointer or NULL. */
  398. void *state_change_cb_user_data;
  399. };
  400. /**
  401. * @brief Callback API upon setting CAN bus timing
  402. * See @a can_set_timing() for argument description
  403. */
  404. typedef int (*can_set_timing_t)(const struct device *dev,
  405. const struct can_timing *timing);
  406. /**
  407. * @brief Optional callback API upon setting CAN FD bus timing for the data phase.
  408. * See @a can_set_timing_data() for argument description
  409. */
  410. typedef int (*can_set_timing_data_t)(const struct device *dev,
  411. const struct can_timing *timing_data);
  412. /**
  413. * @brief Callback API upon getting CAN controller capabilities
  414. * See @a can_get_capabilities() for argument description
  415. */
  416. typedef int (*can_get_capabilities_t)(const struct device *dev, can_mode_t *cap);
  417. /**
  418. * @brief Callback API upon starting CAN controller
  419. * See @a can_start() for argument description
  420. */
  421. typedef int (*can_start_t)(const struct device *dev);
  422. /**
  423. * @brief Callback API upon stopping CAN controller
  424. * See @a can_stop() for argument description
  425. */
  426. typedef int (*can_stop_t)(const struct device *dev);
  427. /**
  428. * @brief Callback API upon setting CAN controller mode
  429. * See @a can_set_mode() for argument description
  430. */
  431. typedef int (*can_set_mode_t)(const struct device *dev, can_mode_t mode);
  432. /**
  433. * @brief Callback API upon sending a CAN frame
  434. * See @a can_send() for argument description
  435. *
  436. * @note From a driver perspective `callback` will never be `NULL` as a default callback will be
  437. * provided if none is provided by the caller. This allows for simplifying the driver handling.
  438. */
  439. typedef int (*can_send_t)(const struct device *dev,
  440. const struct can_frame *frame,
  441. k_timeout_t timeout, can_tx_callback_t callback,
  442. void *user_data);
  443. /**
  444. * @brief Callback API upon adding an RX filter
  445. * See @a can_add_rx_callback() for argument description
  446. */
  447. typedef int (*can_add_rx_filter_t)(const struct device *dev,
  448. can_rx_callback_t callback,
  449. void *user_data,
  450. const struct can_filter *filter);
  451. /**
  452. * @brief Callback API upon removing an RX filter
  453. * See @a can_remove_rx_filter() for argument description
  454. */
  455. typedef void (*can_remove_rx_filter_t)(const struct device *dev, int filter_id);
  456. /**
  457. * @brief Optional callback API upon manually recovering the CAN controller from bus-off state
  458. * See @a can_recover() for argument description
  459. */
  460. typedef int (*can_recover_t)(const struct device *dev, k_timeout_t timeout);
  461. /**
  462. * @brief Callback API upon getting the CAN controller state
  463. * See @a can_get_state() for argument description
  464. */
  465. typedef int (*can_get_state_t)(const struct device *dev, enum can_state *state,
  466. struct can_bus_err_cnt *err_cnt);
  467. /**
  468. * @brief Callback API upon setting a state change callback
  469. * See @a can_set_state_change_callback() for argument description
  470. */
  471. typedef void(*can_set_state_change_callback_t)(const struct device *dev,
  472. can_state_change_callback_t callback,
  473. void *user_data);
  474. /**
  475. * @brief Callback API upon getting the CAN core clock rate
  476. * See @a can_get_core_clock() for argument description
  477. */
  478. typedef int (*can_get_core_clock_t)(const struct device *dev, uint32_t *rate);
  479. /**
  480. * @brief Optional callback API upon getting the maximum number of concurrent CAN RX filters
  481. * See @a can_get_max_filters() for argument description
  482. */
  483. typedef int (*can_get_max_filters_t)(const struct device *dev, bool ide);
  484. struct can_driver_api {
  485. can_get_capabilities_t get_capabilities;
  486. can_start_t start;
  487. can_stop_t stop;
  488. can_set_mode_t set_mode;
  489. can_set_timing_t set_timing;
  490. can_send_t send;
  491. can_add_rx_filter_t add_rx_filter;
  492. can_remove_rx_filter_t remove_rx_filter;
  493. #if defined(CONFIG_CAN_MANUAL_RECOVERY_MODE) || defined(__DOXYGEN__)
  494. can_recover_t recover;
  495. #endif /* CONFIG_CAN_MANUAL_RECOVERY_MODE */
  496. can_get_state_t get_state;
  497. can_set_state_change_callback_t set_state_change_callback;
  498. can_get_core_clock_t get_core_clock;
  499. can_get_max_filters_t get_max_filters;
  500. /* Min values for the timing registers */
  501. struct can_timing timing_min;
  502. /* Max values for the timing registers */
  503. struct can_timing timing_max;
  504. #if defined(CONFIG_CAN_FD_MODE) || defined(__DOXYGEN__)
  505. can_set_timing_data_t set_timing_data;
  506. /* Min values for the timing registers during the data phase */
  507. struct can_timing timing_data_min;
  508. /* Max values for the timing registers during the data phase */
  509. struct can_timing timing_data_max;
  510. #endif /* CONFIG_CAN_FD_MODE */
  511. };
  512. /** @endcond */
  513. /**
  514. * @name CAN controller configuration
  515. *
  516. * @{
  517. */
  518. /**
  519. * @brief Get the CAN core clock rate
  520. *
  521. * Returns the CAN core clock rate. One minimum time quantum (mtq) is 1/(core clock rate). The CAN
  522. * core clock can be further divided by the CAN clock prescaler (see the @a can_timing struct),
  523. * providing the time quantum (tq).
  524. *
  525. * @param dev Pointer to the device structure for the driver instance.
  526. * @param[out] rate CAN core clock rate in Hz.
  527. *
  528. * @return 0 on success, or a negative error code on error
  529. */
  530. static inline int can_get_core_clock(const struct device *dev, uint32_t *rate)
  531. {
  532. const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
  533. return api->get_core_clock(dev, rate);
  534. }
  535. /**
  536. * @brief Get minimum supported bitrate
  537. *
  538. * Get the minimum supported bitrate for the CAN controller/transceiver combination.
  539. *
  540. * @param dev Pointer to the device structure for the driver instance.
  541. * @return Minimum supported bitrate in bits/s
  542. */
  543. static inline uint32_t can_get_bitrate_min(const struct device *dev)
  544. {
  545. const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
  546. return common->min_bitrate;
  547. }
  548. /**
  549. * @brief Get minimum supported bitrate
  550. *
  551. * Get the minimum supported bitrate for the CAN controller/transceiver combination.
  552. *
  553. * @deprecated Use @a can_get_bitrate_min() instead.
  554. *
  555. * @param dev Pointer to the device structure for the driver instance.
  556. * @param[out] min_bitrate Minimum supported bitrate in bits/s
  557. *
  558. * @retval -EIO General input/output error.
  559. * @retval -ENOSYS If this function is not implemented by the driver.
  560. */
  561. static inline int can_get_min_bitrate(const struct device *dev, uint32_t *min_bitrate)
  562. {
  563. *min_bitrate = can_get_bitrate_min(dev);
  564. return 0;
  565. }
  566. /**
  567. * @brief Get maximum supported bitrate
  568. *
  569. * Get the maximum supported bitrate for the CAN controller/transceiver combination.
  570. *
  571. * @param dev Pointer to the device structure for the driver instance.
  572. * @return Maximum supported bitrate in bits/s
  573. */
  574. static inline uint32_t can_get_bitrate_max(const struct device *dev)
  575. {
  576. const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
  577. return common->max_bitrate;
  578. }
  579. /**
  580. * @brief Get maximum supported bitrate
  581. *
  582. * Get the maximum supported bitrate for the CAN controller/transceiver combination.
  583. *
  584. * @deprecated Use @a can_get_bitrate_max() instead.
  585. *
  586. * @param dev Pointer to the device structure for the driver instance.
  587. * @param[out] max_bitrate Maximum supported bitrate in bits/s
  588. *
  589. * @retval 0 If successful.
  590. * @retval -EIO General input/output error.
  591. * @retval -ENOSYS If this function is not implemented by the driver.
  592. */
  593. static inline int can_get_max_bitrate(const struct device *dev, uint32_t *max_bitrate)
  594. {
  595. *max_bitrate = can_get_bitrate_max(dev);
  596. return 0;
  597. }
  598. /**
  599. * @brief Get the minimum supported timing parameter values.
  600. *
  601. * @param dev Pointer to the device structure for the driver instance.
  602. *
  603. * @return Pointer to the minimum supported timing parameter values.
  604. */
  605. static inline const struct can_timing *can_get_timing_min(const struct device *dev)
  606. {
  607. const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
  608. return &api->timing_min;
  609. }
  610. /**
  611. * @brief Get the maximum supported timing parameter values.
  612. *
  613. * @param dev Pointer to the device structure for the driver instance.
  614. *
  615. * @return Pointer to the maximum supported timing parameter values.
  616. */
  617. static inline const struct can_timing *can_get_timing_max(const struct device *dev)
  618. {
  619. const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
  620. return &api->timing_max;
  621. }
  622. /**
  623. * @brief Calculate timing parameters from bitrate and sample point
  624. *
  625. * Calculate the timing parameters from a given bitrate in bits/s and the
  626. * sampling point in permill (1/1000) of the entire bit time. The bitrate must
  627. * always match perfectly. If no result can be reached for the given parameters,
  628. * -EINVAL is returned.
  629. *
  630. * If the sample point is set to 0, this function defaults to a sample point of 75.0%
  631. * for bitrates over 800 kbit/s, 80.0% for bitrates over 500 kbit/s, and 87.5% for
  632. * all other bitrates.
  633. *
  634. * @note The requested ``sample_pnt`` will not always be matched perfectly. The
  635. * algorithm calculates the best possible match.
  636. *
  637. * @param dev Pointer to the device structure for the driver instance.
  638. * @param[out] res Result is written into the @a can_timing struct provided.
  639. * @param bitrate Target bitrate in bits/s.
  640. * @param sample_pnt Sample point in permille of the entire bit time or 0 for
  641. * automatic sample point location.
  642. *
  643. * @retval 0 or positive sample point error on success.
  644. * @retval -EINVAL if the requested bitrate or sample point is out of range.
  645. * @retval -ENOTSUP if the requested bitrate is not supported.
  646. * @retval -EIO if @a can_get_core_clock() is not available.
  647. */
  648. int can_calc_timing(const struct device *dev, struct can_timing *res,
  649. uint32_t bitrate, uint16_t sample_pnt);
  650. /**
  651. * @brief Get the minimum supported timing parameter values for the data phase.
  652. *
  653. * Same as @a can_get_timing_min() but for the minimum values for the data phase.
  654. *
  655. * @note @kconfig{CONFIG_CAN_FD_MODE} must be selected for this function to be
  656. * available.
  657. *
  658. * @param dev Pointer to the device structure for the driver instance.
  659. *
  660. * @return Pointer to the minimum supported timing parameter values, or NULL if
  661. * CAN FD support is not implemented by the driver.
  662. */
  663. const struct can_timing *can_get_timing_data_min(const struct device *dev);
  664. /**
  665. * @brief Get the maximum supported timing parameter values for the data phase.
  666. *
  667. * Same as @a can_get_timing_max() but for the maximum values for the data phase.
  668. *
  669. * @note @kconfig{CONFIG_CAN_FD_MODE} must be selected for this function to be
  670. * available.
  671. *
  672. * @param dev Pointer to the device structure for the driver instance.
  673. *
  674. * @return Pointer to the maximum supported timing parameter values, or NULL if
  675. * CAN FD support is not implemented by the driver.
  676. */
  677. const struct can_timing *can_get_timing_data_max(const struct device *dev);
  678. /**
  679. * @brief Calculate timing parameters for the data phase
  680. *
  681. * Same as @a can_calc_timing() but with the maximum and minimum values from the
  682. * data phase.
  683. *
  684. * @note @kconfig{CONFIG_CAN_FD_MODE} must be selected for this function to be
  685. * available.
  686. *
  687. * @param dev Pointer to the device structure for the driver instance.
  688. * @param[out] res Result is written into the @a can_timing struct provided.
  689. * @param bitrate Target bitrate for the data phase in bits/s
  690. * @param sample_pnt Sample point for the data phase in permille of the entire bit
  691. * time or 0 for automatic sample point location.
  692. *
  693. * @retval 0 or positive sample point error on success.
  694. * @retval -EINVAL if the requested bitrate or sample point is out of range.
  695. * @retval -ENOTSUP if the requested bitrate is not supported.
  696. * @retval -EIO if @a can_get_core_clock() is not available.
  697. */
  698. int can_calc_timing_data(const struct device *dev, struct can_timing *res,
  699. uint32_t bitrate, uint16_t sample_pnt);
  700. /**
  701. * @brief Configure the bus timing for the data phase of a CAN FD controller.
  702. *
  703. * @note @kconfig{CONFIG_CAN_FD_MODE} must be selected for this function to be
  704. * available.
  705. *
  706. * @see can_set_timing()
  707. *
  708. * @param dev Pointer to the device structure for the driver instance.
  709. * @param timing_data Bus timings for data phase
  710. *
  711. * @retval 0 If successful.
  712. * @retval -EBUSY if the CAN controller is not in stopped state.
  713. * @retval -EIO General input/output error, failed to configure device.
  714. * @retval -ENOTSUP if the timing parameters are not supported by the driver.
  715. * @retval -ENOSYS if CAN FD support is not implemented by the driver.
  716. */
  717. int can_set_timing_data(const struct device *dev,
  718. const struct can_timing *timing_data);
  719. /**
  720. * @brief Set the bitrate for the data phase of the CAN FD controller
  721. *
  722. * CAN in Automation (CiA) 301 v4.2.0 recommends a sample point location of
  723. * 87.5% percent for all bitrates. However, some CAN controllers have
  724. * difficulties meeting this for higher bitrates.
  725. *
  726. * This function defaults to using a sample point of 75.0% for bitrates over 800
  727. * kbit/s, 80.0% for bitrates over 500 kbit/s, and 87.5% for all other
  728. * bitrates. This is in line with the sample point locations used by the Linux
  729. * kernel.
  730. *
  731. * @note @kconfig{CONFIG_CAN_FD_MODE} must be selected for this function to be
  732. * available.
  733. *
  734. * @see can_set_bitrate()
  735. * @param dev Pointer to the device structure for the driver instance.
  736. * @param bitrate_data Desired data phase bitrate.
  737. *
  738. * @retval 0 If successful.
  739. * @retval -EBUSY if the CAN controller is not in stopped state.
  740. * @retval -EINVAL if the requested bitrate is out of range.
  741. * @retval -ENOTSUP if the requested bitrate not supported by the CAN controller/transceiver
  742. * combination.
  743. * @retval -ERANGE if the resulting sample point is off by more than +/- 5%.
  744. * @retval -EIO General input/output error, failed to set bitrate.
  745. */
  746. int can_set_bitrate_data(const struct device *dev, uint32_t bitrate_data);
  747. /**
  748. * @brief Fill in the prescaler value for a given bitrate and timing
  749. *
  750. * Fill the prescaler value in the timing struct. The sjw, prop_seg, phase_seg1
  751. * and phase_seg2 must be given.
  752. *
  753. * The returned bitrate error is remainder of the division of the clock rate by
  754. * the bitrate times the timing segments.
  755. *
  756. *
  757. * @param dev Pointer to the device structure for the driver instance.
  758. * @param timing Result is written into the can_timing struct provided.
  759. * @param bitrate Target bitrate.
  760. *
  761. * @retval 0 or positive bitrate error.
  762. * @retval Negative error code on error.
  763. */
  764. int can_calc_prescaler(const struct device *dev, struct can_timing *timing,
  765. uint32_t bitrate);
  766. /**
  767. * @brief Configure the bus timing of a CAN controller.
  768. *
  769. * @see can_set_timing_data()
  770. *
  771. * @param dev Pointer to the device structure for the driver instance.
  772. * @param timing Bus timings.
  773. *
  774. * @retval 0 If successful.
  775. * @retval -EBUSY if the CAN controller is not in stopped state.
  776. * @retval -ENOTSUP if the timing parameters are not supported by the driver.
  777. * @retval -EIO General input/output error, failed to configure device.
  778. */
  779. int can_set_timing(const struct device *dev,
  780. const struct can_timing *timing);
  781. /**
  782. * @brief Get the supported modes of the CAN controller
  783. *
  784. * The returned capabilities may not necessarily be supported at the same time (e.g. some CAN
  785. * controllers support both ``CAN_MODE_LOOPBACK`` and ``CAN_MODE_LISTENONLY``, but not at the same
  786. * time).
  787. *
  788. * @param dev Pointer to the device structure for the driver instance.
  789. * @param[out] cap Supported capabilities.
  790. *
  791. * @retval 0 If successful.
  792. * @retval -EIO General input/output error, failed to get capabilities.
  793. */
  794. static inline int can_get_capabilities(const struct device *dev, can_mode_t *cap)
  795. {
  796. const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
  797. return api->get_capabilities(dev, cap);
  798. }
  799. /**
  800. * @brief Get the CAN transceiver associated with the CAN controller
  801. *
  802. * Get a pointer to the device structure for the CAN transceiver associated with the CAN controller.
  803. *
  804. * @param dev Pointer to the device structure for the driver instance.
  805. * @return Pointer to the device structure for the associated CAN transceiver driver instance, or
  806. * NULL if no transceiver is associated.
  807. */
  808. static const struct device *can_get_transceiver(const struct device *dev)
  809. {
  810. const struct can_driver_config *common = (const struct can_driver_config *)dev->config;
  811. return common->phy;
  812. }
  813. /**
  814. * @brief Start the CAN controller
  815. *
  816. * Bring the CAN controller out of `CAN_STATE_STOPPED`. This will reset the RX/TX error counters,
  817. * enable the CAN controller to participate in CAN communication, and enable the CAN transceiver, if
  818. * supported.
  819. *
  820. * Starting the CAN controller resets all the CAN controller statistics.
  821. *
  822. * @see can_stop()
  823. * @see can_transceiver_enable()
  824. *
  825. * @param dev Pointer to the device structure for the driver instance.
  826. * @retval 0 if successful.
  827. * @retval -EALREADY if the device is already started.
  828. * @retval -EIO General input/output error, failed to start device.
  829. */
  830. static inline int can_start(const struct device *dev)
  831. {
  832. const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
  833. return api->start(dev);
  834. }
  835. /**
  836. * @brief Stop the CAN controller
  837. *
  838. * Bring the CAN controller into `CAN_STATE_STOPPED`. This will disallow the CAN controller from
  839. * participating in CAN communication, abort any pending CAN frame transmissions, and disable the
  840. * CAN transceiver, if supported.
  841. *
  842. * @see can_start()
  843. * @see can_transceiver_disable()
  844. *
  845. * @param dev Pointer to the device structure for the driver instance.
  846. * @retval 0 if successful.
  847. * @retval -EALREADY if the device is already stopped.
  848. * @retval -EIO General input/output error, failed to stop device.
  849. */
  850. static inline int can_stop(const struct device *dev)
  851. {
  852. const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
  853. return api->stop(dev);
  854. }
  855. /**
  856. * @brief Set the CAN controller to the given operation mode
  857. *
  858. * @param dev Pointer to the device structure for the driver instance.
  859. * @param mode Operation mode.
  860. *
  861. * @retval 0 If successful.
  862. * @retval -EBUSY if the CAN controller is not in stopped state.
  863. * @retval -EIO General input/output error, failed to configure device.
  864. */
  865. static inline int can_set_mode(const struct device *dev, can_mode_t mode)
  866. {
  867. const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
  868. return api->set_mode(dev, mode);
  869. }
  870. /**
  871. * @brief Get the operation mode of the CAN controller
  872. *
  873. * @param dev Pointer to the device structure for the driver instance.
  874. *
  875. * @return Current operation mode.
  876. */
  877. static inline can_mode_t can_get_mode(const struct device *dev)
  878. {
  879. const struct can_driver_data *common = (const struct can_driver_data *)dev->data;
  880. return common->mode;
  881. }
  882. /**
  883. * @brief Set the bitrate of the CAN controller
  884. *
  885. * CAN in Automation (CiA) 301 v4.2.0 recommends a sample point location of
  886. * 87.5% percent for all bitrates. However, some CAN controllers have
  887. * difficulties meeting this for higher bitrates.
  888. *
  889. * This function defaults to using a sample point of 75.0% for bitrates over 800
  890. * kbit/s, 80.0% for bitrates over 500 kbit/s, and 87.5% for all other
  891. * bitrates. This is in line with the sample point locations used by the Linux
  892. * kernel.
  893. *
  894. * @see can_set_bitrate_data()
  895. *
  896. * @param dev Pointer to the device structure for the driver instance.
  897. * @param bitrate Desired arbitration phase bitrate.
  898. *
  899. * @retval 0 If successful.
  900. * @retval -EBUSY if the CAN controller is not in stopped state.
  901. * @retval -EINVAL if the requested bitrate is out of range.
  902. * @retval -ENOTSUP if the requested bitrate not supported by the CAN controller/transceiver
  903. * combination.
  904. * @retval -ERANGE if the resulting sample point is off by more than +/- 5%.
  905. * @retval -EIO General input/output error, failed to set bitrate.
  906. */
  907. int can_set_bitrate(const struct device *dev, uint32_t bitrate);
  908. /** @} */
  909. /**
  910. * @name Transmitting CAN frames
  911. *
  912. * @{
  913. */
  914. /**
  915. * @brief Queue a CAN frame for transmission on the CAN bus
  916. *
  917. * Queue a CAN frame for transmission on the CAN bus with optional timeout and
  918. * completion callback function.
  919. *
  920. * Queued CAN frames are transmitted in order according to the their priority:
  921. * - The lower the CAN-ID, the higher the priority.
  922. * - Data frames have higher priority than Remote Transmission Request (RTR)
  923. * frames with identical CAN-IDs.
  924. * - Frames with standard (11-bit) identifiers have higher priority than frames
  925. * with extended (29-bit) identifiers with identical base IDs (the higher 11
  926. * bits of the extended identifier).
  927. * - Transmission order for queued frames with the same priority is hardware
  928. * dependent.
  929. *
  930. * @note If transmitting segmented messages spanning multiple CAN frames with
  931. * identical CAN-IDs, the sender must ensure to only queue one frame at a time
  932. * if FIFO order is required.
  933. *
  934. * By default, the CAN controller will automatically retry transmission in case
  935. * of lost bus arbitration or missing acknowledge. Some CAN controllers support
  936. * disabling automatic retransmissions via ``CAN_MODE_ONE_SHOT``.
  937. *
  938. * @param dev Pointer to the device structure for the driver instance.
  939. * @param frame CAN frame to transmit.
  940. * @param timeout Timeout waiting for a empty TX mailbox or ``K_FOREVER``.
  941. * @param callback Optional callback for when the frame was sent or a
  942. * transmission error occurred. If ``NULL``, this function is
  943. * blocking until frame is sent. The callback must be ``NULL``
  944. * if called from user mode.
  945. * @param user_data User data to pass to callback function.
  946. *
  947. * @retval 0 if successful.
  948. * @retval -EINVAL if an invalid parameter was passed to the function.
  949. * @retval -ENOTSUP if an unsupported parameter was passed to the function.
  950. * @retval -ENETDOWN if the CAN controller is in stopped state.
  951. * @retval -ENETUNREACH if the CAN controller is in bus-off state.
  952. * @retval -EBUSY if CAN bus arbitration was lost (only applicable if automatic
  953. * retransmissions are disabled).
  954. * @retval -EIO if a general transmit error occurred (e.g. missing ACK if
  955. * automatic retransmissions are disabled).
  956. * @retval -EAGAIN on timeout.
  957. */
  958. int can_send(const struct device *dev, const struct can_frame *frame,
  959. k_timeout_t timeout, can_tx_callback_t callback,
  960. void *user_data);
  961. /** @} */
  962. /**
  963. * @name Receiving CAN frames
  964. *
  965. * @{
  966. */
  967. /**
  968. * @brief Add a callback function for a given CAN filter
  969. *
  970. * Add a callback to CAN identifiers specified by a filter. When a received CAN
  971. * frame matching the filter is received by the CAN controller, the callback
  972. * function is called in interrupt context.
  973. *
  974. * If a received frame matches more than one filter (i.e., the filter IDs/masks or
  975. * flags overlap), the priority of the match is hardware dependent.
  976. *
  977. * The same callback function can be used for multiple filters.
  978. *
  979. * @param dev Pointer to the device structure for the driver instance.
  980. * @param callback This function is called by the CAN controller driver whenever
  981. * a frame matching the filter is received.
  982. * @param user_data User data to pass to callback function.
  983. * @param filter Pointer to a @a can_filter structure defining the filter.
  984. *
  985. * @retval filter_id on success.
  986. * @retval -ENOSPC if there are no free filters.
  987. * @retval -EINVAL if the requested filter type is invalid.
  988. * @retval -ENOTSUP if the requested filter type is not supported.
  989. */
  990. int can_add_rx_filter(const struct device *dev, can_rx_callback_t callback,
  991. void *user_data, const struct can_filter *filter);
  992. /**
  993. * @brief Simple wrapper function for adding a message queue for a given filter
  994. *
  995. * Wrapper function for @a can_add_rx_filter() which puts received CAN frames
  996. * matching the filter in a message queue instead of calling a callback.
  997. *
  998. * If a received frame matches more than one filter (i.e., the filter IDs/masks or
  999. * flags overlap), the priority of the match is hardware dependent.
  1000. *
  1001. * The same message queue can be used for multiple filters.
  1002. *
  1003. * @note The message queue must be initialized before calling this function and
  1004. * the caller must have appropriate permissions on it.
  1005. *
  1006. * @warning Message queue overruns are silently ignored and overrun frames
  1007. * discarded. Custom error handling can be implemented by using
  1008. * @a can_add_rx_filter() and @a k_msgq_put() directly.
  1009. *
  1010. * @param dev Pointer to the device structure for the driver instance.
  1011. * @param msgq Pointer to the already initialized @a k_msgq struct.
  1012. * @param filter Pointer to a @a can_filter structure defining the filter.
  1013. *
  1014. * @retval filter_id on success.
  1015. * @retval -ENOSPC if there are no free filters.
  1016. * @retval -ENOTSUP if the requested filter type is not supported.
  1017. */
  1018. int can_add_rx_filter_msgq(const struct device *dev, struct k_msgq *msgq,
  1019. const struct can_filter *filter);
  1020. /**
  1021. * @brief Remove a CAN RX filter
  1022. *
  1023. * This routine removes a CAN RX filter based on the filter ID returned by @a
  1024. * can_add_rx_filter() or @a can_add_rx_filter_msgq().
  1025. *
  1026. * @param dev Pointer to the device structure for the driver instance.
  1027. * @param filter_id Filter ID
  1028. */
  1029. static inline void can_remove_rx_filter(const struct device *dev, int filter_id)
  1030. {
  1031. const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
  1032. api->remove_rx_filter(dev, filter_id);
  1033. }
  1034. /**
  1035. * @brief Get maximum number of RX filters
  1036. *
  1037. * Get the maximum number of concurrent RX filters for the CAN controller.
  1038. *
  1039. * @param dev Pointer to the device structure for the driver instance.
  1040. * @param ide Get the maximum standard (11-bit) CAN ID filters if false, or extended (29-bit) CAN ID
  1041. * filters if true.
  1042. *
  1043. * @retval Positive number of maximum concurrent filters.
  1044. * @retval -EIO General input/output error.
  1045. * @retval -1 If this function is not implemented by the driver.
  1046. */
  1047. static inline int can_get_max_filters(const struct device *dev, bool ide)
  1048. {
  1049. const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
  1050. if (api->get_max_filters == NULL) {
  1051. return -1;
  1052. }
  1053. return api->get_max_filters(dev, ide);
  1054. }
  1055. /** @} */
  1056. /**
  1057. * @name CAN bus error reporting and handling
  1058. *
  1059. * @{
  1060. */
  1061. /**
  1062. * @brief Get current CAN controller state
  1063. *
  1064. * Returns the current state and optionally the error counter values of the CAN
  1065. * controller.
  1066. *
  1067. * @param dev Pointer to the device structure for the driver instance.
  1068. * @param[out] state Pointer to the state destination enum or NULL.
  1069. * @param[out] err_cnt Pointer to the err_cnt destination structure or NULL.
  1070. *
  1071. * @retval 0 If successful.
  1072. * @retval -EIO General input/output error, failed to get state.
  1073. */
  1074. static inline int can_get_state(const struct device *dev, enum can_state *state,
  1075. struct can_bus_err_cnt *err_cnt)
  1076. {
  1077. const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
  1078. return api->get_state(dev, state, err_cnt);
  1079. }
  1080. /**
  1081. * @brief Set a callback for CAN controller state change events
  1082. *
  1083. * Set the callback for CAN controller state change events. The callback
  1084. * function will be called in interrupt context.
  1085. *
  1086. * Only one callback can be registered per controller. Calling this function
  1087. * again overrides any previously registered callback.
  1088. *
  1089. * @param dev Pointer to the device structure for the driver instance.
  1090. * @param callback Callback function.
  1091. * @param user_data User data to pass to callback function.
  1092. */
  1093. static inline void can_set_state_change_callback(const struct device *dev,
  1094. can_state_change_callback_t callback,
  1095. void *user_data)
  1096. {
  1097. const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
  1098. api->set_state_change_callback(dev, callback, user_data);
  1099. }
  1100. /**
  1101. * @brief Convert from Data Length Code (DLC) to the number of data bytes
  1102. *
  1103. * @param dlc Data Length Code (DLC).
  1104. *
  1105. * @retval Number of bytes.
  1106. */
  1107. static inline uint8_t can_dlc_to_bytes(uint8_t dlc)
  1108. {
  1109. static const uint8_t dlc_table[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12,
  1110. 16, 20, 24, 32, 48, 64};
  1111. return dlc_table[MIN(dlc, ARRAY_SIZE(dlc_table) - 1)];
  1112. }
  1113. /**
  1114. * @brief Convert from number of bytes to Data Length Code (DLC)
  1115. *
  1116. * @param num_bytes Number of bytes.
  1117. *
  1118. * @retval Data Length Code (DLC).
  1119. */
  1120. static inline uint8_t can_bytes_to_dlc(uint8_t num_bytes)
  1121. {
  1122. return num_bytes <= 8 ? num_bytes :
  1123. num_bytes <= 12 ? 9 :
  1124. num_bytes <= 16 ? 10 :
  1125. num_bytes <= 20 ? 11 :
  1126. num_bytes <= 24 ? 12 :
  1127. num_bytes <= 32 ? 13 :
  1128. num_bytes <= 48 ? 14 :
  1129. 15;
  1130. }
  1131. /**
  1132. * @brief Check if a CAN frame matches a CAN filter
  1133. *
  1134. * @param frame CAN frame.
  1135. * @param filter CAN filter.
  1136. * @return true if the CAN frame matches the CAN filter, false otherwise
  1137. */
  1138. static inline bool can_frame_matches_filter(const struct can_frame *frame,
  1139. const struct can_filter *filter)
  1140. {
  1141. if ((frame->flags & CAN_FRAME_IDE) != 0 && (filter->flags & CAN_FILTER_IDE) == 0) {
  1142. /* Extended (29-bit) ID frame, standard (11-bit) filter */
  1143. return false;
  1144. }
  1145. if ((frame->flags & CAN_FRAME_IDE) == 0 && (filter->flags & CAN_FILTER_IDE) != 0) {
  1146. /* Standard (11-bit) ID frame, extended (29-bit) filter */
  1147. return false;
  1148. }
  1149. if ((frame->id ^ filter->id) & filter->mask) {
  1150. /* Masked ID mismatch */
  1151. return false;
  1152. }
  1153. return true;
  1154. }
  1155. /** @} */
  1156. /**
  1157. * @}
  1158. */
  1159. #ifdef __cplusplus
  1160. }
  1161. #endif
  1162. #endif /* CAN_H_ */