-
Notifications
You must be signed in to change notification settings - Fork 15
/
mcp2518fd_can_dfs.h
1592 lines (1338 loc) · 39.6 KB
/
mcp2518fd_can_dfs.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
/*******************************************************************************
CAN FD SPI Driver: Register Header File
Company:
Microchip Technology Inc.
File Name:
drv_canfdspi_register.h
Summary:
This header file contains SPI instruction defines, register address defines,
register structures, and reset values of registers.
Description:
This file is used by the API.
*******************************************************************************/
// DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright (c) 2018 Microchip Technology Inc. and its subsidiaries.
Subject to your compliance with these terms, you may use Microchip software and
any derivatives exclusively with Microchip products. It is your responsibility
to comply with third party license terms applicable to your use of third party
software (including open source software) that may accompany Microchip software.
THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER EXPRESS,
IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES
OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER
RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF
THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT ALLOWED
BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO
THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID
DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*******************************************************************************/
// DOM-IGNORE-END
#ifndef _DRV_CANFDSPI_REGISTER_H
#define _DRV_CANFDSPI_REGISTER_H
// *****************************************************************************
// *****************************************************************************
// Section: Included Files
#include <Arduino.h>
#include <inttypes.h>
#include "mcp_can_fd.h"
// DOM-IGNORE-BEGIN
#ifdef __cplusplus // Provide C++ Compatibility
extern "C" {
#endif
// DOM-IGNORE-END
// *****************************************************************************
// *****************************************************************************
/* SPI Instruction Set */
#define cINSTRUCTION_RESET 0x00
#define cINSTRUCTION_READ 0x03
#define cINSTRUCTION_READ_CRC 0x0B
#define cINSTRUCTION_WRITE 0x02
#define cINSTRUCTION_WRITE_CRC 0x0A
#define cINSTRUCTION_WRITE_SAFE 0x0C
// *****************************************************************************
// *****************************************************************************
/* Register Addresses */
/* CAN FD Controller */
#define cREGADDR_CiCON 0x000
#define cREGADDR_CiNBTCFG 0x004
#define cREGADDR_CiDBTCFG 0x008
#define cREGADDR_CiTDC 0x00C
#define cREGADDR_CiTBC 0x010
#define cREGADDR_CiTSCON 0x014
#define cREGADDR_CiVEC 0x018
#define cREGADDR_CiINT 0x01C
#define cREGADDR_CiINTFLAG cREGADDR_CiINT
#define cREGADDR_CiINTENABLE (cREGADDR_CiINT + 2)
#define cREGADDR_CiRXIF 0x020
#define cREGADDR_CiTXIF 0x024
#define cREGADDR_CiRXOVIF 0x028
#define cREGADDR_CiTXATIF 0x02C
#define cREGADDR_CiTXREQ 0x030
#define cREGADDR_CiTREC 0x034
#define cREGADDR_CiBDIAG0 0x038
#define cREGADDR_CiBDIAG1 0x03C
#define cREGADDR_CiTEFCON 0x040
#define cREGADDR_CiTEFSTA 0x044
#define cREGADDR_CiTEFUA 0x048
#define cREGADDR_CiFIFOBA 0x04C
#define cREGADDR_CiFIFOCON 0x050
#define cREGADDR_CiFIFOSTA 0x054
#define cREGADDR_CiFIFOUA 0x058
#define CiFIFO_OFFSET (3 * 4)
#define cREGADDR_CiTXQCON 0x050
#define cREGADDR_CiTXQSTA 0x054
#define cREGADDR_CiTXQUA 0x058
// The filters start right after the FIFO control/status registers
#define cREGADDR_CiFLTCON \
(cREGADDR_CiFIFOCON + (CiFIFO_OFFSET * CAN_FIFO_TOTAL_CHANNELS)) // 0x50+12*32
#define cREGADDR_CiFLTOBJ (cREGADDR_CiFLTCON + CAN_FIFO_TOTAL_CHANNELS)
#define cREGADDR_CiMASK (cREGADDR_CiFLTOBJ + 4)
#define CiFILTER_OFFSET (2 * 4)
/* MCP25xxFD Specific */
#define cREGADDR_OSC 0xE00
#define cREGADDR_IOCON 0xE04
#define cREGADDR_CRC 0xE08
#define cREGADDR_ECCCON 0xE0C
#define cREGADDR_ECCSTA 0xE10
#ifndef MCP2517FD
#define cREGADDR_DEVID 0xE14
#endif
/* RAM addresses */
#define cRAM_SIZE 2048
#define cRAMADDR_START 0x400
#define cRAMADDR_END (cRAMADDR_START + cRAM_SIZE)
/* SPI Chip Select */
#define MCP2518fd_SELECT() \
pinMode(SPICS, OUTPUT); \
digitalWrite(SPICS, LOW)
#define MCP2518fd_UNSELECT() \
pinMode(SPICS, OUTPUT); \
digitalWrite(SPICS, HIGH)
// *****************************************************************************
// *****************************************************************************
/* Register Structures */
// DOM-IGNORE-END
// *****************************************************************************
// *****************************************************************************
// Section: Implementation
// Device selection
//#define MCP2517FD
#define MCP2518FD
// Maximum Size of TX/RX Object
#define MAX_MSG_SIZE 76
// Maximum number of data bytes in message
#define MAX_DATA_BYTES 64
// *****************************************************************************
// *****************************************************************************
// Section: Object definitions
//! CAN FIFO Channels
typedef enum {
CAN_FIFO_CH0, // CAN_TXQUEUE_CH0
CAN_FIFO_CH1,
CAN_FIFO_CH2,
CAN_FIFO_CH3,
CAN_FIFO_CH4,
CAN_FIFO_CH5,
CAN_FIFO_CH6,
CAN_FIFO_CH7,
CAN_FIFO_CH8,
CAN_FIFO_CH9,
CAN_FIFO_CH10,
CAN_FIFO_CH11,
CAN_FIFO_CH12,
CAN_FIFO_CH13,
CAN_FIFO_CH14,
CAN_FIFO_CH15,
CAN_FIFO_CH16,
CAN_FIFO_CH17,
CAN_FIFO_CH18,
CAN_FIFO_CH19,
CAN_FIFO_CH20,
CAN_FIFO_CH21,
CAN_FIFO_CH22,
CAN_FIFO_CH23,
CAN_FIFO_CH24,
CAN_FIFO_CH25,
CAN_FIFO_CH26,
CAN_FIFO_CH27,
CAN_FIFO_CH28,
CAN_FIFO_CH29,
CAN_FIFO_CH30,
CAN_FIFO_CH31,
CAN_FIFO_TOTAL_CHANNELS
} CAN_FIFO_CHANNEL;
// FIFO0 is a special FIFO, the TX Queue
#define CAN_TXQUEUE_CH0 CAN_FIFO_CH0
//! CAN Filter Channels
typedef enum {
CAN_FILTER0,
CAN_FILTER1,
CAN_FILTER2,
CAN_FILTER3,
CAN_FILTER4,
CAN_FILTER5,
CAN_FILTER6,
CAN_FILTER7,
CAN_FILTER8,
CAN_FILTER9,
CAN_FILTER10,
CAN_FILTER11,
CAN_FILTER12,
CAN_FILTER13,
CAN_FILTER14,
CAN_FILTER15,
CAN_FILTER16,
CAN_FILTER17,
CAN_FILTER18,
CAN_FILTER19,
CAN_FILTER20,
CAN_FILTER21,
CAN_FILTER22,
CAN_FILTER23,
CAN_FILTER24,
CAN_FILTER25,
CAN_FILTER26,
CAN_FILTER27,
CAN_FILTER28,
CAN_FILTER29,
CAN_FILTER30,
CAN_FILTER31,
CAN_FILTER_TOTAL,
} CAN_FILTER;
//! CAN Operation Modes
typedef enum {
CAN_NORMAL_MODE = 0x00,
CAN_SLEEP_MODE = 0x01,
CAN_INTERNAL_LOOPBACK_MODE = 0x02,
CAN_LISTEN_ONLY_MODE = 0x03,
CAN_CONFIGURATION_MODE = 0x04,
CAN_EXTERNAL_LOOPBACK_MODE = 0x05,
CAN_CLASSIC_MODE = 0x06,
CAN_RESTRICTED_MODE = 0x07,
CAN_INVALID_MODE = 0xFF
} CAN_OPERATION_MODE;
//! Transmit Bandwidth Sharing
typedef enum {
CAN_TXBWS_NO_DELAY,
CAN_TXBWS_2,
CAN_TXBWS_4,
CAN_TXBWS_8,
CAN_TXBWS_16,
CAN_TXBWS_32,
CAN_TXBWS_64,
CAN_TXBWS_128,
CAN_TXBWS_256,
CAN_TXBWS_512,
CAN_TXBWS_1024,
CAN_TXBWS_2048,
CAN_TXBWS_4096
} CAN_TX_BANDWITH_SHARING;
//! Wake-up Filter Time
typedef enum {
CAN_WFT00,
CAN_WFT01,
CAN_WFT10,
CAN_WFT11
} CAN_WAKEUP_FILTER_TIME;
//! Data Byte Filter Number
typedef enum {
CAN_DNET_FILTER_DISABLE = 0,
CAN_DNET_FILTER_SIZE_1_BIT,
CAN_DNET_FILTER_SIZE_2_BIT,
CAN_DNET_FILTER_SIZE_3_BIT,
CAN_DNET_FILTER_SIZE_4_BIT,
CAN_DNET_FILTER_SIZE_5_BIT,
CAN_DNET_FILTER_SIZE_6_BIT,
CAN_DNET_FILTER_SIZE_7_BIT,
CAN_DNET_FILTER_SIZE_8_BIT,
CAN_DNET_FILTER_SIZE_9_BIT,
CAN_DNET_FILTER_SIZE_10_BIT,
CAN_DNET_FILTER_SIZE_11_BIT,
CAN_DNET_FILTER_SIZE_12_BIT,
CAN_DNET_FILTER_SIZE_13_BIT,
CAN_DNET_FILTER_SIZE_14_BIT,
CAN_DNET_FILTER_SIZE_15_BIT,
CAN_DNET_FILTER_SIZE_16_BIT,
CAN_DNET_FILTER_SIZE_17_BIT,
CAN_DNET_FILTER_SIZE_18_BIT
} CAN_DNET_FILTER_SIZE;
//! FIFO Payload Size
typedef enum {
CAN_PLSIZE_8,
CAN_PLSIZE_12,
CAN_PLSIZE_16,
CAN_PLSIZE_20,
CAN_PLSIZE_24,
CAN_PLSIZE_32,
CAN_PLSIZE_48,
CAN_PLSIZE_64
} CAN_FIFO_PLSIZE;
//! CAN Configure
typedef struct _CAN_CONFIG {
uint32_t DNetFilterCount : 5;
uint32_t IsoCrcEnable : 1;
uint32_t ProtocolExpectionEventDisable : 1;
uint32_t WakeUpFilterEnable : 1;
uint32_t WakeUpFilterTime : 2;
uint32_t BitRateSwitchDisable : 1;
uint32_t RestrictReTxAttempts : 1;
uint32_t EsiInGatewayMode : 1;
uint32_t SystemErrorToListenOnly : 1;
uint32_t StoreInTEF : 1;
uint32_t TXQEnable : 1;
uint32_t TxBandWidthSharing : 4;
} CAN_CONFIG;
//! CAN Transmit Channel Configure
typedef struct _CAN_TX_FIFO_CONFIG {
uint32_t RTREnable : 1;
uint32_t TxPriority : 5;
uint32_t TxAttempts : 2;
uint32_t FifoSize : 5;
uint32_t PayLoadSize : 3;
} CAN_TX_FIFO_CONFIG;
//! CAN Transmit Queue Configure
typedef struct _CAN_TX_QUEUE_CONFIG {
uint32_t TxPriority : 5;
uint32_t TxAttempts : 2;
uint32_t FifoSize : 5;
uint32_t PayLoadSize : 3;
} CAN_TX_QUEUE_CONFIG;
//! CAN Receive Channel Configure
typedef struct _CAN_RX_FIFO_CONFIG {
uint32_t RxTimeStampEnable : 1;
uint32_t FifoSize : 5;
uint32_t PayLoadSize : 3;
} CAN_RX_FIFO_CONFIG;
//! CAN Transmit Event FIFO Configure
typedef struct _CAN_TEF_CONFIG {
uint32_t TimeStampEnable : 1;
uint32_t FifoSize : 5;
} CAN_TEF_CONFIG;
/* CAN Message Objects */
//! CAN Message Object ID
typedef struct _CAN_MSGOBJ_ID {
uint32_t SID : 11;
uint32_t EID : 18;
uint32_t SID11 : 1;
uint32_t unimplemented1 : 2;
} CAN_MSGOBJ_ID;
//! CAN Data Length Code
typedef enum {
CAN_DLC_0,
CAN_DLC_1,
CAN_DLC_2,
CAN_DLC_3,
CAN_DLC_4,
CAN_DLC_5,
CAN_DLC_6,
CAN_DLC_7,
CAN_DLC_8,
CAN_DLC_12,
CAN_DLC_16,
CAN_DLC_20,
CAN_DLC_24,
CAN_DLC_32,
CAN_DLC_48,
CAN_DLC_64
} CAN_DLC;
//! CAN TX Message Object Control
typedef struct _CAN_TX_MSGOBJ_CTRL {
uint32_t DLC : 4;
uint32_t IDE : 1;
uint32_t RTR : 1;
uint32_t BRS : 1;
uint32_t FDF : 1;
uint32_t ESI : 1;
#ifdef MCP2517FD
uint32_t SEQ : 7;
uint32_t unimplemented1 : 16;
#else
uint32_t SEQ : 23;
#endif
} CAN_TX_MSGOBJ_CTRL;
//! CAN RX Message Object Control
typedef struct _CAN_RX_MSGOBJ_CTRL {
uint32_t DLC : 4;
uint32_t IDE : 1;
uint32_t RTR : 1;
uint32_t BRS : 1;
uint32_t FDF : 1;
uint32_t ESI : 1;
uint32_t unimplemented1 : 2;
uint32_t FilterHit : 5;
uint32_t unimplemented2 : 16;
} CAN_RX_MSGOBJ_CTRL;
//! CAN Message Time Stamp
typedef uint32_t CAN_MSG_TIMESTAMP;
//! CAN TX Message Object
typedef union _CAN_TX_MSGOBJ {
struct {
CAN_MSGOBJ_ID id;
CAN_TX_MSGOBJ_CTRL ctrl;
CAN_MSG_TIMESTAMP timeStamp;
} bF;
uint32_t word[3];
uint8_t byte[12];
} CAN_TX_MSGOBJ;
//! CAN RX Message Object
typedef union _CAN_RX_MSGOBJ {
struct {
CAN_MSGOBJ_ID id;
CAN_RX_MSGOBJ_CTRL ctrl;
CAN_MSG_TIMESTAMP timeStamp;
} bF;
uint32_t word[3];
uint8_t byte[12];
} CAN_RX_MSGOBJ;
//! CAN TEF Message Object
typedef union _CAN_TEF_MSGOBJ {
struct {
CAN_MSGOBJ_ID id;
CAN_TX_MSGOBJ_CTRL ctrl;
CAN_MSG_TIMESTAMP timeStamp;
} bF;
uint32_t word[3];
uint8_t byte[12];
} CAN_TEF_MSGOBJ;
//! CAN Filter Object ID
typedef struct _CAN_FILTEROBJ_ID {
uint32_t SID : 11;
uint32_t EID : 18;
uint32_t SID11 : 1;
uint32_t EXIDE : 1;
uint32_t unimplemented1 : 1;
} CAN_FILTEROBJ_ID;
//! CAN Mask Object ID
typedef struct _CAN_MASKOBJ_ID {
uint32_t MSID : 11;
uint32_t MEID : 18;
uint32_t MSID11 : 1;
uint32_t MIDE : 1;
uint32_t unimplemented1 : 1;
} CAN_MASKOBJ_ID;
//! CAN RX FIFO Status
typedef enum {
CAN_RX_FIFO_EMPTY = 0,
CAN_RX_FIFO_STATUS_MASK = 0x0F,
CAN_RX_FIFO_NOT_EMPTY = 0x01,
CAN_RX_FIFO_HALF_FULL = 0x02,
CAN_RX_FIFO_FULL = 0x04,
CAN_RX_FIFO_OVERFLOW = 0x08
} CAN_RX_FIFO_STATUS;
//! CAN TX FIFO Status
typedef enum {
CAN_TX_FIFO_FULL = 0,
CAN_TX_FIFO_STATUS_MASK = 0x1F7,
CAN_TX_FIFO_NOT_FULL = 0x01,
CAN_TX_FIFO_HALF_FULL = 0x02,
CAN_TX_FIFO_EMPTY = 0x04,
CAN_TX_FIFO_ATTEMPTS_EXHAUSTED = 0x10,
CAN_TX_FIFO_ERROR = 0x20,
CAN_TX_FIFO_ARBITRATION_LOST = 0x40,
CAN_TX_FIFO_ABORTED = 0x80,
CAN_TX_FIFO_TRANSMITTING = 0x100
} CAN_TX_FIFO_STATUS;
//! CAN TEF FIFO Status
typedef enum {
CAN_TEF_FIFO_EMPTY = 0,
CAN_TEF_FIFO_STATUS_MASK = 0x0F,
CAN_TEF_FIFO_NOT_EMPTY = 0x01,
CAN_TEF_FIFO_HALF_FULL = 0x02,
CAN_TEF_FIFO_FULL = 0x04,
CAN_TEF_FIFO_OVERFLOW = 0x08
} CAN_TEF_FIFO_STATUS;
//! CAN Module Event (Interrupts)
typedef enum {
CAN_NO_EVENT = 0,
CAN_ALL_EVENTS = 0xFF1F,
CAN_TX_EVENT = 0x0001,
CAN_RX_EVENT = 0x0002,
CAN_TIME_BASE_COUNTER_EVENT = 0x0004,
CAN_OPERATION_MODE_CHANGE_EVENT = 0x0008,
CAN_TEF_EVENT = 0x0010,
CAN_RAM_ECC_EVENT = 0x0100,
CAN_SPI_CRC_EVENT = 0x0200,
CAN_TX_ATTEMPTS_EVENT = 0x0400,
CAN_RX_OVERFLOW_EVENT = 0x0800,
CAN_SYSTEM_ERROR_EVENT = 0x1000,
CAN_BUS_ERROR_EVENT = 0x2000,
CAN_BUS_WAKEUP_EVENT = 0x4000,
CAN_RX_INVALID_MESSAGE_EVENT = 0x8000
} CAN_MODULE_EVENT;
//! CAN TX FIFO Event (Interrupts)
typedef enum {
CAN_TX_FIFO_NO_EVENT = 0,
CAN_TX_FIFO_ALL_EVENTS = 0x17,
CAN_TX_FIFO_NOT_FULL_EVENT = 0x01,
CAN_TX_FIFO_HALF_FULL_EVENT = 0x02,
CAN_TX_FIFO_EMPTY_EVENT = 0x04,
CAN_TX_FIFO_ATTEMPTS_EXHAUSTED_EVENT = 0x10
} CAN_TX_FIFO_EVENT;
//! CAN RX FIFO Event (Interrupts)
typedef enum {
CAN_RX_FIFO_NO_EVENT = 0,
CAN_RX_FIFO_ALL_EVENTS = 0x0F,
CAN_RX_FIFO_NOT_EMPTY_EVENT = 0x01,
CAN_RX_FIFO_HALF_FULL_EVENT = 0x02,
CAN_RX_FIFO_FULL_EVENT = 0x04,
CAN_RX_FIFO_OVERFLOW_EVENT = 0x08
} CAN_RX_FIFO_EVENT;
//! CAN TEF FIFO Event (Interrupts)
typedef enum {
CAN_TEF_FIFO_NO_EVENT = 0,
CAN_TEF_FIFO_ALL_EVENTS = 0x0F,
CAN_TEF_FIFO_NOT_EMPTY_EVENT = 0x01,
CAN_TEF_FIFO_HALF_FULL_EVENT = 0x02,
CAN_TEF_FIFO_FULL_EVENT = 0x04,
CAN_TEF_FIFO_OVERFLOW_EVENT = 0x08
} CAN_TEF_FIFO_EVENT;
//! CAN Bit Time Setup: Arbitration/Data Bit Phase
/* not apply to AVR platform */
// typedef enum {
static const uint32_t CAN_125K_500K = ( 4UL << 24) | (125000UL);
static const uint32_t CAN_250K_500K = ( 2UL << 24) | (250000UL);
static const uint32_t CAN_250K_750K = ( 3UL << 24) | (250000UL);
static const uint32_t CAN_250K_1M = ( 4UL << 24) | (250000UL);
static const uint32_t CAN_250K_1M5 = ( 6UL << 24) | (250000UL);
static const uint32_t CAN_250K_2M = ( 8UL << 24) | (250000UL);
static const uint32_t CAN_250K_3M = (12UL << 24) | (250000UL);
static const uint32_t CAN_250K_4M = (16UL << 24) | (250000UL);
static const uint32_t CAN_500K_1M = ( 2UL << 24) | (500000UL);
static const uint32_t CAN_500K_2M = ( 4UL << 24) | (500000UL);
static const uint32_t CAN_500K_3M = ( 6UL << 24) | (500000UL);
static const uint32_t CAN_500K_4M = ( 8UL << 24) | (500000UL);
static const uint32_t CAN_500K_5M = (10UL << 24) | (500000UL);
static const uint32_t CAN_500K_6M5 = (13UL << 24) | (500000UL);
static const uint32_t CAN_500K_8M = (16UL << 24) | (500000UL);
static const uint32_t CAN_500K_10M = (20UL << 24) | (500000UL);
static const uint32_t CAN_1000K_4M = ( 4UL << 24) |(1000000UL);
static const uint32_t CAN_1000K_8M = ( 8UL << 24) |(1000000UL);
//} MCP2518FD_BITTIME_SETUP;
//! Secondary Sample Point Mode
typedef enum {
CAN_SSP_MODE_OFF,
CAN_SSP_MODE_MANUAL,
CAN_SSP_MODE_AUTO
} CAN_SSP_MODE;
//! CAN Error State
typedef enum {
CAN_ERROR_FREE_STATE = 0,
CAN_ERROR_ALL = 0x3F,
CAN_TX_RX_WARNING_STATE = 0x01,
CAN_RX_WARNING_STATE = 0x02,
CAN_TX_WARNING_STATE = 0x04,
CAN_RX_BUS_PASSIVE_STATE = 0x08,
CAN_TX_BUS_PASSIVE_STATE = 0x10,
CAN_TX_BUS_OFF_STATE = 0x20
} CAN_ERROR_STATE;
//! CAN Time Stamp Mode Select
typedef enum {
CAN_TS_SOF = 0x00,
CAN_TS_EOF = 0x01,
CAN_TS_RES = 0x02
} CAN_TS_MODE;
//! CAN ECC EVENT
typedef enum {
CAN_ECC_NO_EVENT = 0x00,
CAN_ECC_ALL_EVENTS = 0x06,
CAN_ECC_SEC_EVENT = 0x02,
CAN_ECC_DED_EVENT = 0x04
} CAN_ECC_EVENT;
//! CAN CRC EVENT
typedef enum {
CAN_CRC_NO_EVENT = 0x00,
CAN_CRC_ALL_EVENTS = 0x03,
CAN_CRC_CRCERR_EVENT = 0x01,
CAN_CRC_FORMERR_EVENT = 0x02
} CAN_CRC_EVENT;
//! GPIO Pin Position
typedef enum { GPIO_PIN_0, GPIO_PIN_1 } GPIO_PIN_POS;
//! GPIO Pin Modes
typedef enum { GPIO_MODE_INT, GPIO_MODE_GPIO } GPIO_PIN_MODE;
//! GPIO Pin Directions
typedef enum { GPIO_OUTPUT, GPIO_INPUT } GPIO_PIN_DIRECTION;
//! GPIO Open Drain Mode
typedef enum { GPIO_PUSH_PULL, GPIO_OPEN_DRAIN } GPIO_OPEN_DRAIN_MODE;
//! GPIO Pin State
typedef enum { GPIO_LOW, GPIO_HIGH } GPIO_PIN_STATE;
//! Clock Output Mode
typedef enum { GPIO_CLKO_CLOCK, GPIO_CLKO_SOF } GPIO_CLKO_MODE;
//! CAN Bus Diagnostic flags
typedef struct _CAN_BUS_DIAG_FLAGS {
uint32_t NBIT0_ERR : 1;
uint32_t NBIT1_ERR : 1;
uint32_t NACK_ERR : 1;
uint32_t NFORM_ERR : 1;
uint32_t NSTUFF_ERR : 1;
uint32_t NCRC_ERR : 1;
uint32_t unimplemented1 : 1;
uint32_t TXBO_ERR : 1;
uint32_t DBIT0_ERR : 1;
uint32_t DBIT1_ERR : 1;
uint32_t unimplemented2 : 1;
uint32_t DFORM_ERR : 1;
uint32_t DSTUFF_ERR : 1;
uint32_t DCRC_ERR : 1;
uint32_t ESI : 1;
uint32_t DLC_MISMATCH : 1;
} CAN_BUS_DIAG_FLAGS;
//! CAN Bus Diagnostic Error Counts
typedef struct _CAN_BUS_ERROR_COUNT {
uint8_t NREC;
uint8_t NTEC;
uint8_t DREC;
uint8_t DTEC;
} CAN_BUS_ERROR_COUNT;
//! CAN BUS DIAGNOSTICS
typedef union _CAN_BUS_DIAGNOSTIC {
struct {
CAN_BUS_ERROR_COUNT errorCount;
uint16_t errorFreeMsgCount;
CAN_BUS_DIAG_FLAGS flag;
} bF;
uint32_t word[3];
uint8_t byte[12];
} CAN_BUS_DIAGNOSTIC;
//! TXREQ Channel Bits
// Multiple channels can be or'ed together
typedef enum {
CAN_TXREQ_CH0 = 0x00000001,
CAN_TXREQ_CH1 = 0x00000002,
CAN_TXREQ_CH2 = 0x00000004,
CAN_TXREQ_CH3 = 0x00000008,
CAN_TXREQ_CH4 = 0x00000010,
CAN_TXREQ_CH5 = 0x00000020,
CAN_TXREQ_CH6 = 0x00000040,
CAN_TXREQ_CH7 = 0x00000080,
CAN_TXREQ_CH8 = 0x00000100,
CAN_TXREQ_CH9 = 0x00000200,
CAN_TXREQ_CH10 = 0x00000400,
CAN_TXREQ_CH11 = 0x00000800,
CAN_TXREQ_CH12 = 0x00001000,
CAN_TXREQ_CH13 = 0x00002000,
CAN_TXREQ_CH14 = 0x00004000,
CAN_TXREQ_CH15 = 0x00008000,
CAN_TXREQ_CH16 = 0x00010000,
CAN_TXREQ_CH17 = 0x00020000,
CAN_TXREQ_CH18 = 0x00040000,
CAN_TXREQ_CH19 = 0x00080000,
CAN_TXREQ_CH20 = 0x00100000,
CAN_TXREQ_CH21 = 0x00200000,
CAN_TXREQ_CH22 = 0x00400000,
CAN_TXREQ_CH23 = 0x00800000,
CAN_TXREQ_CH24 = 0x01000000,
CAN_TXREQ_CH25 = 0x02000000,
CAN_TXREQ_CH26 = 0x04000000,
CAN_TXREQ_CH27 = 0x08000000,
CAN_TXREQ_CH28 = 0x10000000,
CAN_TXREQ_CH29 = 0x20000000,
CAN_TXREQ_CH30 = 0x40000000,
CAN_TXREQ_CH31 = 0x80000000
} CAN_TXREQ_CHANNEL;
//! Oscillator Control
typedef struct _CAN_OSC_CTRL {
uint32_t PllEnable : 1;
uint32_t OscDisable : 1;
uint32_t SclkDivide : 1;
uint32_t ClkOutDivide : 2;
#ifndef MCP2517FD
uint32_t LowPowerModeEnable : 1;
#endif
} CAN_OSC_CTRL;
//! Oscillator Status
typedef struct _CAN_OSC_STATUS {
uint32_t PllReady : 1;
uint32_t OscReady : 1;
uint32_t SclkReady : 1;
} CAN_OSC_STATUS;
//! ICODE
typedef enum {
CAN_ICODE_FIFO_CH0,
CAN_ICODE_FIFO_CH1,
CAN_ICODE_FIFO_CH2,
CAN_ICODE_FIFO_CH3,
CAN_ICODE_FIFO_CH4,
CAN_ICODE_FIFO_CH5,
CAN_ICODE_FIFO_CH6,
CAN_ICODE_FIFO_CH7,
CAN_ICODE_FIFO_CH8,
CAN_ICODE_FIFO_CH9,
CAN_ICODE_FIFO_CH10,
CAN_ICODE_FIFO_CH11,
CAN_ICODE_FIFO_CH12,
CAN_ICODE_FIFO_CH13,
CAN_ICODE_FIFO_CH14,
CAN_ICODE_FIFO_CH15,
CAN_ICODE_FIFO_CH16,
CAN_ICODE_FIFO_CH17,
CAN_ICODE_FIFO_CH18,
CAN_ICODE_FIFO_CH19,
CAN_ICODE_FIFO_CH20,
CAN_ICODE_FIFO_CH21,
CAN_ICODE_FIFO_CH22,
CAN_ICODE_FIFO_CH23,
CAN_ICODE_FIFO_CH24,
CAN_ICODE_FIFO_CH25,
CAN_ICODE_FIFO_CH26,
CAN_ICODE_FIFO_CH27,
CAN_ICODE_FIFO_CH28,
CAN_ICODE_FIFO_CH29,
CAN_ICODE_FIFO_CH30,
CAN_ICODE_FIFO_CH31,
CAN_ICODE_TOTAL_CHANNELS,
CAN_ICODE_NO_INT = 0x40,
CAN_ICODE_CERRIF,
CAN_ICODE_WAKIF,
CAN_ICODE_RXOVIF,
CAN_ICODE_ADDRERR_SERRIF,
CAN_ICODE_MABOV_SERRIF,
CAN_ICODE_TBCIF,
CAN_ICODE_MODIF,
CAN_ICODE_IVMIF,
CAN_ICODE_TEFIF,
CAN_ICODE_TXATIF,
CAN_ICODE_RESERVED
} CAN_ICODE;
//! RXCODE
typedef enum {
CAN_RXCODE_FIFO_CH1 = 1,
CAN_RXCODE_FIFO_CH2,
CAN_RXCODE_FIFO_CH3,
CAN_RXCODE_FIFO_CH4,
CAN_RXCODE_FIFO_CH5,
CAN_RXCODE_FIFO_CH6,
CAN_RXCODE_FIFO_CH7,
CAN_RXCODE_FIFO_CH8,
CAN_RXCODE_FIFO_CH9,
CAN_RXCODE_FIFO_CH10,
CAN_RXCODE_FIFO_CH11,
CAN_RXCODE_FIFO_CH12,
CAN_RXCODE_FIFO_CH13,
CAN_RXCODE_FIFO_CH14,
CAN_RXCODE_FIFO_CH15,
CAN_RXCODE_FIFO_CH16,
CAN_RXCODE_FIFO_CH17,
CAN_RXCODE_FIFO_CH18,
CAN_RXCODE_FIFO_CH19,
CAN_RXCODE_FIFO_CH20,
CAN_RXCODE_FIFO_CH21,
CAN_RXCODE_FIFO_CH22,
CAN_RXCODE_FIFO_CH23,
CAN_RXCODE_FIFO_CH24,
CAN_RXCODE_FIFO_CH25,
CAN_RXCODE_FIFO_CH26,
CAN_RXCODE_FIFO_CH27,
CAN_RXCODE_FIFO_CH28,
CAN_RXCODE_FIFO_CH29,
CAN_RXCODE_FIFO_CH30,
CAN_RXCODE_FIFO_CH31,
CAN_RXCODE_TOTAL_CHANNELS,
CAN_RXCODE_NO_INT = 0x40,
CAN_RXCODE_RESERVED
} CAN_RXCODE;
//! TXCODE
typedef enum {
CAN_TXCODE_FIFO_CH0,
CAN_TXCODE_FIFO_CH1,
CAN_TXCODE_FIFO_CH2,
CAN_TXCODE_FIFO_CH3,
CAN_TXCODE_FIFO_CH4,
CAN_TXCODE_FIFO_CH5,
CAN_TXCODE_FIFO_CH6,
CAN_TXCODE_FIFO_CH7,
CAN_TXCODE_FIFO_CH8,
CAN_TXCODE_FIFO_CH9,
CAN_TXCODE_FIFO_CH10,
CAN_TXCODE_FIFO_CH11,
CAN_TXCODE_FIFO_CH12,
CAN_TXCODE_FIFO_CH13,
CAN_TXCODE_FIFO_CH14,
CAN_TXCODE_FIFO_CH15,
CAN_TXCODE_FIFO_CH16,
CAN_TXCODE_FIFO_CH17,
CAN_TXCODE_FIFO_CH18,
CAN_TXCODE_FIFO_CH19,
CAN_TXCODE_FIFO_CH20,
CAN_TXCODE_FIFO_CH21,
CAN_TXCODE_FIFO_CH22,
CAN_TXCODE_FIFO_CH23,
CAN_TXCODE_FIFO_CH24,
CAN_TXCODE_FIFO_CH25,
CAN_TXCODE_FIFO_CH26,
CAN_TXCODE_FIFO_CH27,
CAN_TXCODE_FIFO_CH28,
CAN_TXCODE_FIFO_CH29,
CAN_TXCODE_FIFO_CH30,
CAN_TXCODE_FIFO_CH31,
CAN_TXCODE_TOTAL_CHANNELS,
CAN_TXCODE_NO_INT = 0x40,
CAN_TXCODE_RESERVED
} CAN_TXCODE;
//! System Clock Selection
typedef enum {
CAN_SYSCLK_40M = MCP2518FD_40MHz,
CAN_SYSCLK_20M = MCP2518FD_20MHz,
CAN_SYSCLK_10M = MCP2518FD_10MHz,
} CAN_SYSCLK_SPEED;
//! CLKO Divide
typedef enum {
OSC_CLKO_DIV1,
OSC_CLKO_DIV2,
OSC_CLKO_DIV4,
OSC_CLKO_DIV10
} OSC_CLKO_DIVIDE;
// *****************************************************************************
//! General 32-bit Register
typedef union _REG_t {
uint8_t byte[4];
uint32_t word;
} REG_t;
// *****************************************************************************
// *****************************************************************************
/* CAN FD Controller */
// *****************************************************************************
//! CAN Control Register
typedef union _REG_CiCON {
struct {
uint32_t DNetFilterCount : 5;
uint32_t IsoCrcEnable : 1;
uint32_t ProtocolExceptionEventDisable : 1;
uint32_t unimplemented1 : 1;
uint32_t WakeUpFilterEnable : 1;
uint32_t WakeUpFilterTime : 2;
uint32_t unimplemented2 : 1;
uint32_t BitRateSwitchDisable : 1;
uint32_t unimplemented3 : 3;
uint32_t RestrictReTxAttempts : 1;
uint32_t EsiInGatewayMode : 1;
uint32_t SystemErrorToListenOnly : 1;
uint32_t StoreInTEF : 1;
uint32_t TXQEnable : 1;
uint32_t OpMode : 3;
uint32_t RequestOpMode : 3;
uint32_t AbortAllTx : 1;
uint32_t TxBandWidthSharing : 4;
} bF;
uint32_t word;
uint8_t byte[4];
} REG_CiCON;
// *****************************************************************************
//! Nominal Bit Time Configuration Register
typedef union _REG_CiNBTCFG {
struct {
uint32_t SJW : 7;
uint32_t unimplemented1 : 1;
uint32_t TSEG2 : 7;
uint32_t unimplemented2 : 1;
uint32_t TSEG1 : 8;
uint32_t BRP : 8;
} bF;
uint32_t word;
uint8_t byte[4];
} REG_CiNBTCFG;
// *****************************************************************************
//! Data Bit Time Configuration Register
typedef union _REG_CiDBTCFG {
struct {
uint32_t SJW : 4;
uint32_t unimplemented1 : 4;