-
Notifications
You must be signed in to change notification settings - Fork 570
/
can.h
1372 lines (1207 loc) · 43.2 KB
/
can.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2021 Vestas Wind Systems A/S
* Copyright (c) 2018 Alexander Wachter
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_DRIVERS_CAN_H_
#define ZEPHYR_INCLUDE_DRIVERS_CAN_H_
#include <errno.h>
#include <zephyr/types.h>
#include <zephyr/device.h>
#include <zephyr/kernel.h>
#include <string.h>
#include <zephyr/sys_clock.h>
#include <zephyr/sys/util.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief CAN Interface
* @defgroup can_interface CAN Interface
* @ingroup io_interfaces
* @{
*/
/**
* @name CAN frame definitions
* @{
*/
/**
* @brief Bit mask for a standard (11-bit) CAN identifier.
*/
#define CAN_STD_ID_MASK 0x7FFU
/**
* @brief Maximum value for a standard (11-bit) CAN identifier.
*/
#define CAN_MAX_STD_ID CAN_STD_ID_MASK
/**
* @brief Bit mask for an extended (29-bit) CAN identifier.
*/
#define CAN_EXT_ID_MASK 0x1FFFFFFFU
/**
* @brief Maximum value for an extended (29-bit) CAN identifier.
*/
#define CAN_MAX_EXT_ID CAN_EXT_ID_MASK
/**
* @brief Maximum data length code for CAN 2.0A/2.0B.
*/
#define CAN_MAX_DLC 8U
/**
* @brief Maximum data length code for CAN-FD.
*/
#define CANFD_MAX_DLC 15U
/**
* @cond INTERNAL_HIDDEN
* Internally calculated maximum data length
*/
#ifndef CONFIG_CAN_FD_MODE
#define CAN_MAX_DLEN 8U
#else
#define CAN_MAX_DLEN 64U
#endif /* CONFIG_CAN_FD_MODE */
/** @endcond */
/** @} */
/**
* @name CAN controller mode flags
* @anchor CAN_MODE_FLAGS
*
* @{
*/
/** Normal mode. */
#define CAN_MODE_NORMAL 0
/** Controller is in loopback mode (receives own frames). */
#define CAN_MODE_LOOPBACK BIT(0)
/** Controller is not allowed to send dominant bits. */
#define CAN_MODE_LISTENONLY BIT(1)
/** Controller allows transmitting/receiving CAN-FD frames. */
#define CAN_MODE_FD BIT(2)
/** Controller does not retransmit in case of lost arbitration or missing ACK */
#define CAN_MODE_ONE_SHOT BIT(3)
/** Controller uses triple sampling mode */
#define CAN_MODE_3_SAMPLES BIT(4)
/** @} */
/**
* @brief Provides a type to hold CAN controller configuration flags.
*
* The lower 24 bits are reserved for common CAN controller mode flags. The upper 8 bits are
* reserved for CAN controller/driver specific flags.
*
* @see @ref CAN_MODE_FLAGS.
*/
typedef uint32_t can_mode_t;
/**
* @brief Defines the state of the CAN controller
*/
enum can_state {
/** Error-active state (RX/TX error count < 96). */
CAN_STATE_ERROR_ACTIVE,
/** Error-warning state (RX/TX error count < 128). */
CAN_STATE_ERROR_WARNING,
/** Error-passive state (RX/TX error count < 256). */
CAN_STATE_ERROR_PASSIVE,
/** Bus-off state (RX/TX error count >= 256). */
CAN_STATE_BUS_OFF,
/** CAN controller is stopped and does not participate in CAN communication. */
CAN_STATE_STOPPED,
};
/**
* @name CAN frame flags
* @anchor CAN_FRAME_FLAGS
*
* @{
*/
/** Frame uses extended (29-bit) CAN ID */
#define CAN_FRAME_IDE BIT(0)
/** Frame is a Remote Transmission Request (RTR) */
#define CAN_FRAME_RTR BIT(1)
/** Frame uses CAN-FD format (FDF) */
#define CAN_FRAME_FDF BIT(2)
/** Frame uses CAN-FD Baud Rate Switch (BRS). Only valid in combination with ``CAN_FRAME_FDF``. */
#define CAN_FRAME_BRS BIT(3)
/** @} */
/**
* @brief CAN frame structure
*/
struct can_frame {
/** Standard (11-bit) or extended (29-bit) CAN identifier. */
uint32_t id : 29;
/** @cond INTERNAL_HIDDEN */
uint8_t res0 : 3; /* reserved/padding. */
/** @endcond */
/** Data Length Code (DLC) indicating data length in bytes. */
uint8_t dlc;
/** Flags. @see @ref CAN_FRAME_FLAGS. */
uint8_t flags;
#if defined(CONFIG_CAN_RX_TIMESTAMP) || defined(__DOXYGEN__)
/** Captured value of the free-running timer in the CAN controller when
* this frame was received. The timer is incremented every bit time and
* captured at the start of frame bit (SOF).
*
* @note @kconfig{CONFIG_CAN_RX_TIMESTAMP} must be selected for this
* field to be available.
*/
uint16_t timestamp;
#else
/** @cond INTERNAL_HIDDEN */
uint16_t res1; /* reserved/padding. */
/** @endcond */
#endif
/** The frame payload data. */
union {
uint8_t data[CAN_MAX_DLEN];
uint32_t data_32[ceiling_fraction(CAN_MAX_DLEN, sizeof(uint32_t))];
};
};
/**
* @name CAN filter flags
* @anchor CAN_FILTER_FLAGS
*
* @{
*/
/** Filter matches frames with extended (29-bit) CAN IDs */
#define CAN_FILTER_IDE BIT(0)
/** Filter matches Remote Transmission Request (RTR) frames */
#define CAN_FILTER_RTR BIT(1)
/** Filter matches data frames */
#define CAN_FILTER_DATA BIT(2)
/** @} */
/**
* @brief CAN filter structure
*/
struct can_filter {
/** CAN identifier to match. */
uint32_t id : 29;
/** @cond INTERNAL_HIDDEN */
uint32_t res0 : 3;
/** @endcond */
/** CAN identifier matching mask. If a bit in this mask is 0, the value
* of the corresponding bit in the ``id`` field is ignored by the filter.
*/
uint32_t mask : 29;
/** Flags. @see @ref CAN_FILTER_FLAGS. */
uint8_t flags : 3;
};
/**
* @brief CAN controller error counters
*/
struct can_bus_err_cnt {
/** Value of the CAN controller transmit error counter. */
uint8_t tx_err_cnt;
/** Value of the CAN controller receive error counter. */
uint8_t rx_err_cnt;
};
/**
* @brief CAN bus timing structure
*
* This struct is used to pass bus timing values to the configuration and
* bitrate calculation functions.
*
* The propagation segment represents the time of the signal propagation. Phase
* segment 1 and phase segment 2 define the sampling point. The ``prop_seg`` and
* ``phase_seg1`` values affect the sampling point in the same way and some
* controllers only have a register for the sum of those two. The sync segment
* always has a length of 1 time quantum (see below).
*
* @code{.text}
*
* +---------+----------+------------+------------+
* |sync_seg | prop_seg | phase_seg1 | phase_seg2 |
* +---------+----------+------------+------------+
* ^
* Sampling-Point
*
* @endcode
*
* 1 time quantum (tq) has the length of 1/(core_clock / prescaler). The bitrate
* is defined by the core clock divided by the prescaler and the sum of the
* segments:
*
* br = (core_clock / prescaler) / (1 + prop_seg + phase_seg1 + phase_seg2)
*
* The Synchronization Jump Width (SJW) defines the amount of time quanta the
* sample point can be moved. The sample point is moved when resynchronization
* is needed.
*/
struct can_timing {
/** Synchronisation jump width. */
uint16_t sjw;
/** Propagation segment. */
uint16_t prop_seg;
/** Phase segment 1. */
uint16_t phase_seg1;
/** Phase segment 2. */
uint16_t phase_seg2;
/** Prescaler value. */
uint16_t prescaler;
};
/**
* @brief Defines the application callback handler function signature
*
* @param dev Pointer to the device structure for the driver instance.
* @param error Status of the performed send operation. See the list of
* return values for @a can_send() for value descriptions.
* @param user_data User data provided when the frame was sent.
*/
typedef void (*can_tx_callback_t)(const struct device *dev, int error, void *user_data);
/**
* @brief Defines the application callback handler function signature for receiving.
*
* @param dev Pointer to the device structure for the driver instance.
* @param frame Received frame.
* @param user_data User data provided when the filter was added.
*/
typedef void (*can_rx_callback_t)(const struct device *dev, struct can_frame *frame,
void *user_data);
/**
* @brief Defines the state change callback handler function signature
*
* @param dev Pointer to the device structure for the driver instance.
* @param state State of the CAN controller.
* @param err_cnt CAN controller error counter values.
* @param user_data User data provided the callback was set.
*/
typedef void (*can_state_change_callback_t)(const struct device *dev,
enum can_state state,
struct can_bus_err_cnt err_cnt,
void *user_data);
/**
* @cond INTERNAL_HIDDEN
*
* For internal driver use only, skip these in public documentation.
*/
/**
* @brief Callback API upon setting CAN bus timing
* See @a can_set_timing() for argument description
*/
typedef int (*can_set_timing_t)(const struct device *dev,
const struct can_timing *timing);
/**
* @brief Optional callback API upon setting CAN-FD bus timing for the data phase.
* See @a can_set_timing_data() for argument description
*/
typedef int (*can_set_timing_data_t)(const struct device *dev,
const struct can_timing *timing_data);
/**
* @brief Callback API upon getting CAN controller capabilities
* See @a can_get_capabilities() for argument description
*/
typedef int (*can_get_capabilities_t)(const struct device *dev, can_mode_t *cap);
/**
* @brief Callback API upon starting CAN controller
* See @a can_start() for argument description
*/
typedef int (*can_start_t)(const struct device *dev);
/**
* @brief Callback API upon stopping CAN controller
* See @a can_stop() for argument description
*/
typedef int (*can_stop_t)(const struct device *dev);
/**
* @brief Callback API upon setting CAN controller mode
* See @a can_set_mode() for argument description
*/
typedef int (*can_set_mode_t)(const struct device *dev, can_mode_t mode);
/**
* @brief Callback API upon sending a CAN frame
* See @a can_send() for argument description
*
* @note From a driver perspective `callback` will never be `NULL` as a default callback will be
* provided if none is provided by the caller. This allows for simplifying the driver handling.
*/
typedef 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);
/**
* @brief Callback API upon adding an RX filter
* See @a can_add_rx_callback() for argument description
*/
typedef int (*can_add_rx_filter_t)(const struct device *dev,
can_rx_callback_t callback,
void *user_data,
const struct can_filter *filter);
/**
* @brief Callback API upon removing an RX filter
* See @a can_remove_rx_filter() for argument description
*/
typedef void (*can_remove_rx_filter_t)(const struct device *dev, int filter_id);
/**
* @brief Callback API upon recovering the CAN bus
* See @a can_recover() for argument description
*/
typedef int (*can_recover_t)(const struct device *dev, k_timeout_t timeout);
/**
* @brief Callback API upon getting the CAN controller state
* See @a can_get_state() for argument description
*/
typedef int (*can_get_state_t)(const struct device *dev, enum can_state *state,
struct can_bus_err_cnt *err_cnt);
/**
* @brief Callback API upon setting a state change callback
* See @a can_set_state_change_callback() for argument description
*/
typedef void(*can_set_state_change_callback_t)(const struct device *dev,
can_state_change_callback_t callback,
void *user_data);
/**
* @brief Callback API upon getting the CAN core clock rate
* See @a can_get_core_clock() for argument description
*/
typedef int (*can_get_core_clock_t)(const struct device *dev, uint32_t *rate);
/**
* @brief Optional callback API upon getting the maximum number of concurrent CAN RX filters
* See @a can_get_max_filters() for argument description
*/
typedef int (*can_get_max_filters_t)(const struct device *dev, bool ide);
/**
* @brief Optional callback API upon getting the maximum supported bitrate
* See @a can_get_max_bitrate() for argument description
*/
typedef int (*can_get_max_bitrate_t)(const struct device *dev, uint32_t *max_bitrate);
__subsystem struct can_driver_api {
can_get_capabilities_t get_capabilities;
can_start_t start;
can_stop_t stop;
can_set_mode_t set_mode;
can_set_timing_t set_timing;
can_send_t send;
can_add_rx_filter_t add_rx_filter;
can_remove_rx_filter_t remove_rx_filter;
#if !defined(CONFIG_CAN_AUTO_BUS_OFF_RECOVERY) || defined(__DOXYGEN__)
can_recover_t recover;
#endif /* CONFIG_CAN_AUTO_BUS_OFF_RECOVERY */
can_get_state_t get_state;
can_set_state_change_callback_t set_state_change_callback;
can_get_core_clock_t get_core_clock;
can_get_max_filters_t get_max_filters;
can_get_max_bitrate_t get_max_bitrate;
/* Min values for the timing registers */
struct can_timing timing_min;
/* Max values for the timing registers */
struct can_timing timing_max;
#if defined(CONFIG_CAN_FD_MODE) || defined(__DOXYGEN__)
can_set_timing_data_t set_timing_data;
/* Min values for the timing registers during the data phase */
struct can_timing timing_data_min;
/* Max values for the timing registers during the data phase */
struct can_timing timing_data_max;
#endif /* CONFIG_CAN_FD_MODE */
};
/** @endcond */
#if defined(CONFIG_CAN_STATS) || defined(__DOXYGEN__)
#include <zephyr/stats/stats.h>
/** @cond INTERNAL_HIDDEN */
STATS_SECT_START(can)
STATS_SECT_ENTRY32(bit0_error)
STATS_SECT_ENTRY32(bit1_error)
STATS_SECT_ENTRY32(stuff_error)
STATS_SECT_ENTRY32(crc_error)
STATS_SECT_ENTRY32(form_error)
STATS_SECT_ENTRY32(ack_error)
STATS_SECT_END;
STATS_NAME_START(can)
STATS_NAME(can, bit0_error)
STATS_NAME(can, bit1_error)
STATS_NAME(can, stuff_error)
STATS_NAME(can, crc_error)
STATS_NAME(can, form_error)
STATS_NAME(can, ack_error)
STATS_NAME_END(can);
/** @endcond */
/**
* @brief CAN specific device state which allows for CAN device class specific
* additions
*/
struct can_device_state {
struct device_state devstate;
struct stats_can stats;
};
/** @cond INTERNAL_HIDDEN */
/**
* @brief Get pointer to CAN statistics structure
*/
#define Z_CAN_GET_STATS(dev_) \
CONTAINER_OF(dev_->state, struct can_device_state, devstate)->stats
/** @endcond */
/**
* @brief Increment the bit0 error counter for a CAN device
*
* The bit0 error counter is incremented when the CAN controller is unable to
* transmit a dominant bit.
*
* @param dev_ Pointer to the device structure for the driver instance.
*/
#define CAN_STATS_BIT0_ERROR_INC(dev_) \
STATS_INC(Z_CAN_GET_STATS(dev_), bit0_error)
/**
* @brief Increment the bit1 (recessive) error counter for a CAN device
*
* The bit1 error counter is incremented when the CAN controller is unable to
* transmit a recessive bit.
*
* @param dev_ Pointer to the device structure for the driver instance.
*/
#define CAN_STATS_BIT1_ERROR_INC(dev_) \
STATS_INC(Z_CAN_GET_STATS(dev_), bit1_error)
/**
* @brief Increment the stuffing error counter for a CAN device
*
* The stuffing error counter is incremented when the CAN controller detects a
* bit stuffing error.
*
* @param dev_ Pointer to the device structure for the driver instance.
*/
#define CAN_STATS_STUFF_ERROR_INC(dev_) \
STATS_INC(Z_CAN_GET_STATS(dev_), stuff_error)
/**
* @brief Increment the CRC error counter for a CAN device
*
* The CRC error counter is incremented when the CAN controller detects a frame
* with an invalid CRC.
*
* @param dev_ Pointer to the device structure for the driver instance.
*/
#define CAN_STATS_CRC_ERROR_INC(dev_) \
STATS_INC(Z_CAN_GET_STATS(dev_), crc_error)
/**
* @brief Increment the form error counter for a CAN device
*
* The form error counter is incremented when the CAN controller detects a
* fixed-form bit field containing illegal bits.
*
* @param dev_ Pointer to the device structure for the driver instance.
*/
#define CAN_STATS_FORM_ERROR_INC(dev_) \
STATS_INC(Z_CAN_GET_STATS(dev_), form_error)
/**
* @brief Increment the acknowledge error counter for a CAN device
*
* The acknowledge error counter is incremented when the CAN controller does not
* monitor a dominant bit in the ACK slot.
*
* @param dev_ Pointer to the device structure for the driver instance.
*/
#define CAN_STATS_ACK_ERROR_INC(dev_) \
STATS_INC(Z_CAN_GET_STATS(dev_), ack_error)
/** @cond INTERNAL_HIDDEN */
/**
* @brief Define a statically allocated and section assigned CAN device state
*/
#define Z_CAN_DEVICE_STATE_DEFINE(dev_id) \
static struct can_device_state Z_DEVICE_STATE_NAME(dev_id) \
__attribute__((__section__(".z_devstate")))
/**
* @brief Define a CAN device init wrapper function
*
* This does device instance specific initialization of common data (such as stats)
* and calls the given init_fn
*/
#define Z_CAN_INIT_FN(dev_id, init_fn) \
static inline int UTIL_CAT(dev_id, _init)(const struct device *dev) \
{ \
struct can_device_state *state = \
CONTAINER_OF(dev->state, struct can_device_state, devstate); \
stats_init(&state->stats.s_hdr, STATS_SIZE_32, 6, \
STATS_NAME_INIT_PARMS(can)); \
stats_register(dev->name, &(state->stats.s_hdr)); \
return init_fn(dev); \
}
/** @endcond */
/**
* @brief Like DEVICE_DT_DEFINE() with CAN device specifics.
*
* @details Defines a device which implements the CAN API. May generate a custom
* device_state container struct and init_fn wrapper when needed depending on
* @kconfig{CONFIG_CAN_STATS}.
*
* @param node_id The devicetree node identifier.
* @param init_fn Name of the init function of the driver.
* @param pm PM device resources reference (NULL if device does not use PM).
* @param data Pointer to the device's private data.
* @param config The address to the structure containing the configuration
* information for this instance of the driver.
* @param level The initialization level. See SYS_INIT() for
* details.
* @param prio Priority within the selected initialization level. See
* SYS_INIT() for details.
* @param api Provides an initial pointer to the API function struct
* used by the driver. Can be NULL.
*/
#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
prio, api, ...) \
Z_CAN_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
Z_CAN_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn) \
Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
DEVICE_DT_NAME(node_id), \
&UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init), \
pm, data, config, level, prio, api, \
&(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
__VA_ARGS__)
#else /* CONFIG_CAN_STATS */
#define CAN_STATS_BIT0_ERROR_INC(dev_)
#define CAN_STATS_BIT1_ERROR_INC(dev_)
#define CAN_STATS_STUFF_ERROR_INC(dev_)
#define CAN_STATS_CRC_ERROR_INC(dev_)
#define CAN_STATS_FORM_ERROR_INC(dev_)
#define CAN_STATS_ACK_ERROR_INC(dev_)
#define CAN_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
prio, api, ...) \
DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
prio, api, __VA_ARGS__)
#endif /* CONFIG_CAN_STATS */
/**
* @brief Like CAN_DEVICE_DT_DEFINE() for an instance of a DT_DRV_COMPAT compatible
*
* @param inst Instance number. This is replaced by <tt>DT_DRV_COMPAT(inst)</tt>
* in the call to CAN_DEVICE_DT_DEFINE().
* @param ... Other parameters as expected by CAN_DEVICE_DT_DEFINE().
*/
#define CAN_DEVICE_DT_INST_DEFINE(inst, ...) \
CAN_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
/**
* @name CAN controller configuration
*
* @{
*/
/**
* @brief Get the CAN core clock rate
*
* Returns the CAN core clock rate. One time quantum is 1/(core clock rate).
*
* @param dev Pointer to the device structure for the driver instance.
* @param[out] rate CAN core clock rate in Hz.
*
* @return 0 on success, or a negative error code on error
*/
__syscall int can_get_core_clock(const struct device *dev, uint32_t *rate);
static inline int z_impl_can_get_core_clock(const struct device *dev, uint32_t *rate)
{
const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
return api->get_core_clock(dev, rate);
}
/**
* @brief Get maximum supported bitrate
*
* Get the maximum supported bitrate for the CAN controller/transceiver combination.
*
* @param dev Pointer to the device structure for the driver instance.
* @param[out] max_bitrate Maximum supported bitrate in bits/s
*
* @retval -EIO General input/output error.
* @retval -ENOSYS If this function is not implemented by the driver.
*/
__syscall int can_get_max_bitrate(const struct device *dev, uint32_t *max_bitrate);
static inline int z_impl_can_get_max_bitrate(const struct device *dev, uint32_t *max_bitrate)
{
const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
if (api->get_max_bitrate == NULL) {
return -ENOSYS;
}
return api->get_max_bitrate(dev, max_bitrate);
}
/**
* @brief Get the minimum supported timing parameter values.
*
* @param dev Pointer to the device structure for the driver instance.
*
* @return Pointer to the minimum supported timing parameter values.
*/
__syscall const struct can_timing *can_get_timing_min(const struct device *dev);
static inline const struct can_timing *z_impl_can_get_timing_min(const struct device *dev)
{
const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
return &api->timing_min;
}
/**
* @brief Get the maximum supported timing parameter values.
*
* @param dev Pointer to the device structure for the driver instance.
*
* @return Pointer to the maximum supported timing parameter values.
*/
__syscall const struct can_timing *can_get_timing_max(const struct device *dev);
static inline const struct can_timing *z_impl_can_get_timing_max(const struct device *dev)
{
const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
return &api->timing_max;
}
/**
* @brief Calculate timing parameters from bitrate and sample point
*
* Calculate the timing parameters from a given bitrate in bits/s and the
* sampling point in permill (1/1000) of the entire bit time. The bitrate must
* always match perfectly. If no result can be reached for the given parameters,
* -EINVAL is returned.
*
* @note The requested ``sample_pnt`` will not always be matched perfectly. The
* algorithm calculates the best possible match.
*
* @param dev Pointer to the device structure for the driver instance.
* @param[out] res Result is written into the @a can_timing struct provided.
* @param bitrate Target bitrate in bits/s.
* @param sample_pnt Sampling point in permill of the entire bit time.
*
* @retval 0 or positive sample point error on success.
* @retval -EINVAL if the requested bitrate or sample point is out of range.
* @retval -ENOTSUP if the requested bitrate is not supported.
* @retval -EIO if @a can_get_core_clock() is not available.
*/
__syscall int can_calc_timing(const struct device *dev, struct can_timing *res,
uint32_t bitrate, uint16_t sample_pnt);
/**
* @brief Get the minimum supported timing parameter values for the data phase.
*
* Same as @a can_get_timing_min() but for the minimum values for the data phase.
*
* @note @kconfig{CONFIG_CAN_FD_MODE} must be selected for this function to be
* available.
*
* @param dev Pointer to the device structure for the driver instance.
*
* @return Pointer to the minimum supported timing parameter values, or NULL if
* CAN-FD support is not implemented by the driver.
*/
__syscall const struct can_timing *can_get_timing_data_min(const struct device *dev);
#ifdef CONFIG_CAN_FD_MODE
static inline const struct can_timing *z_impl_can_get_timing_data_min(const struct device *dev)
{
const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
return &api->timing_data_min;
}
#endif /* CONFIG_CAN_FD_MODE */
/**
* @brief Get the maximum supported timing parameter values for the data phase.
*
* Same as @a can_get_timing_max() but for the maximum values for the data phase.
*
* @note @kconfig{CONFIG_CAN_FD_MODE} must be selected for this function to be
* available.
*
* @param dev Pointer to the device structure for the driver instance.
*
* @return Pointer to the maximum supported timing parameter values, or NULL if
* CAN-FD support is not implemented by the driver.
*/
__syscall const struct can_timing *can_get_timing_data_max(const struct device *dev);
#ifdef CONFIG_CAN_FD_MODE
static inline const struct can_timing *z_impl_can_get_timing_data_max(const struct device *dev)
{
const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
return &api->timing_data_max;
}
#endif /* CONFIG_CAN_FD_MODE */
/**
* @brief Calculate timing parameters for the data phase
*
* Same as @a can_calc_timing() but with the maximum and minimum values from the
* data phase.
*
* @note @kconfig{CONFIG_CAN_FD_MODE} must be selected for this function to be
* available.
*
* @param dev Pointer to the device structure for the driver instance.
* @param[out] res Result is written into the @a can_timing struct provided.
* @param bitrate Target bitrate for the data phase in bits/s
* @param sample_pnt Sampling point for the data phase in permille of the entire bit time.
*
* @retval 0 or positive sample point error on success.
* @retval -EINVAL if the requested bitrate or sample point is out of range.
* @retval -ENOTSUP if the requested bitrate is not supported.
* @retval -EIO if @a can_get_core_clock() is not available.
*/
__syscall int can_calc_timing_data(const struct device *dev, struct can_timing *res,
uint32_t bitrate, uint16_t sample_pnt);
/**
* @brief Configure the bus timing for the data phase of a CAN-FD controller.
*
* If the sjw equals CAN_SJW_NO_CHANGE, the sjw parameter is not changed.
*
* @note @kconfig{CONFIG_CAN_FD_MODE} must be selected for this function to be
* available.
*
* @see can_set_timing()
*
* @param dev Pointer to the device structure for the driver instance.
* @param timing_data Bus timings for data phase
*
* @retval 0 If successful.
* @retval -EBUSY if the CAN controller is not in stopped state.
* @retval -EIO General input/output error, failed to configure device.
* @retval -ENOSYS if CAN-FD support is not implemented by the driver.
*/
__syscall int can_set_timing_data(const struct device *dev,
const struct can_timing *timing_data);
#ifdef CONFIG_CAN_FD_MODE
static inline int z_impl_can_set_timing_data(const struct device *dev,
const struct can_timing *timing_data)
{
const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
if (api->set_timing_data == NULL) {
return -ENOSYS;
}
return api->set_timing_data(dev, timing_data);
}
#endif /* CONFIG_CAN_FD_MODE */
/**
* @brief Set the bitrate for the data phase of the CAN-FD controller
*
* CAN in Automation (CiA) 301 v4.2.0 recommends a sample point location of
* 87.5% percent for all bitrates. However, some CAN controllers have
* difficulties meeting this for higher bitrates.
*
* This function defaults to using a sample point of 75.0% for bitrates over 800
* kbit/s, 80.0% for bitrates over 500 kbit/s, and 87.5% for all other
* bitrates. This is in line with the sample point locations used by the Linux
* kernel.
*
* @note @kconfig{CONFIG_CAN_FD_MODE} must be selected for this function to be
* available.
*
* @see can_set_bitrate()
* @param dev Pointer to the device structure for the driver instance.
* @param bitrate_data Desired data phase bitrate.
*
* @retval 0 If successful.
* @retval -EBUSY if the CAN controller is not in stopped state.
* @retval -EINVAL if the requested bitrate is out of range.
* @retval -ENOTSUP if the requested bitrate not supported by the CAN controller/transceiver
* combination.
* @retval -ERANGE if the resulting sample point is off by more than +/- 5%.
* @retval -EIO General input/output error, failed to set bitrate.
*/
__syscall int can_set_bitrate_data(const struct device *dev, uint32_t bitrate_data);
/**
* @brief Fill in the prescaler value for a given bitrate and timing
*
* Fill the prescaler value in the timing struct. The sjw, prop_seg, phase_seg1
* and phase_seg2 must be given.
*
* The returned bitrate error is remainder of the division of the clock rate by
* the bitrate times the timing segments.
*
* @param dev Pointer to the device structure for the driver instance.
* @param timing Result is written into the can_timing struct provided.
* @param bitrate Target bitrate.
*
* @retval 0 or positive bitrate error.
* @retval Negative error code on error.
*/
int can_calc_prescaler(const struct device *dev, struct can_timing *timing,
uint32_t bitrate);
/** Synchronization Jump Width (SJW) value to indicate that the SJW should not
* be changed by the timing calculation.
*/
#define CAN_SJW_NO_CHANGE 0
/**
* @brief Configure the bus timing of a CAN controller.
*
* If the sjw equals CAN_SJW_NO_CHANGE, the sjw parameter is not changed.
*
* @see can_set_timing_data()
*
* @param dev Pointer to the device structure for the driver instance.
* @param timing Bus timings.
*
* @retval 0 If successful.
* @retval -EBUSY if the CAN controller is not in stopped state.
* @retval -EIO General input/output error, failed to configure device.
*/
__syscall int can_set_timing(const struct device *dev,
const struct can_timing *timing);
static inline int z_impl_can_set_timing(const struct device *dev,
const struct can_timing *timing)
{
const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
return api->set_timing(dev, timing);
}
/**
* @brief Get the supported modes of the CAN controller
*
* The returned capabilities may not necessarily be supported at the same time (e.g. some CAN
* controllers support both ``CAN_MODE_LOOPBACK`` and ``CAN_MODE_LISTENONLY``, but not at the same
* time).
*
* @param dev Pointer to the device structure for the driver instance.
* @param[out] cap Supported capabilities.
*
* @retval 0 If successful.
* @retval -EIO General input/output error, failed to get capabilities.
*/
__syscall int can_get_capabilities(const struct device *dev, can_mode_t *cap);
static inline int z_impl_can_get_capabilities(const struct device *dev, can_mode_t *cap)
{
const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
return api->get_capabilities(dev, cap);
}
/**
* @brief Start the CAN controller
*
* Bring the CAN controller out of `CAN_STATE_STOPPED`. This will reset the RX/TX error counters,
* enable the CAN controller to participate in CAN communication, and enable the CAN tranceiver, if
* supported.
*
* @see can_stop()
* @see can_transceiver_enable()
*
* @param dev Pointer to the device structure for the driver instance.
* @retval 0 if successful.
* @retval -EALREADY if the device is already started.
* @retval -EIO General input/output error, failed to start device.
*/
__syscall int can_start(const struct device *dev);
static inline int z_impl_can_start(const struct device *dev)
{
const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
return api->start(dev);
}
/**
* @brief Stop the CAN controller
*
* Bring the CAN controller into `CAN_STATE_STOPPED`. This will disallow the CAN controller from
* participating in CAN communication, abort any pending CAN frame transmissions, and disable the
* CAN transceiver, if supported.
*
* @see can_start()
* @see can_transceiver_disable()
*
* @param dev Pointer to the device structure for the driver instance.
* @retval 0 if successful.
* @retval -EALREADY if the device is already stopped.
* @retval -EIO General input/output error, failed to stop device.
*/
__syscall int can_stop(const struct device *dev);
static inline int z_impl_can_stop(const struct device *dev)
{
const struct can_driver_api *api = (const struct can_driver_api *)dev->api;
return api->stop(dev);