-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ECAN.h
1715 lines (1521 loc) · 63.2 KB
/
ECAN.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
/*********************************************************************
*
* ECAN C Library Source Code
*
*********************************************************************
* FileName: ECAN.h
* Dependencies: ECAN.def
* Processor: Any PIC18 with ECAN module
* Complier: Microchip C 2.10.06 or higher
* Company: Microchip Technology, Inc.
*
*
* Software License Agreement
*
* The software supplied herewith by Microchip Technology Incorporated
* (the “Company”) for its PICmicro® Microcontroller is intended and
* supplied to you, the Company’s customer, for use solely and
* exclusively on Microchip PICmicro Microcontroller products. The
* software is owned by the Company and/or its supplier, and is
* protected under applicable copyright laws. All rights are reserved.
* Any use in violation of the foregoing restrictions may subject the
* user to criminal sanctions under applicable laws, as well as to
* civil liability for the breach of the terms and conditions of this
* license.
*
* THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Caio Gubel 5/5/03 Version 1.0 - Initial Release
* Nilesh R. 7/22/03 Improved
*********************************************************************/
#ifndef ECAN_H // To avoid duplicate inclusion
#define ECAN_H
#include "ECAN.def"
#if defined(HI_TECH_C)
#define HITECH_C18
#else
#define MCHP_C18
#endif
#if defined(MCHP_C18) && defined(HITECH_C18)
#error "Invalid Compiler selection."
#endif
#if !defined(MCHP_C18) && !defined(HITECH_C18)
#error "Compiler not supported."
#endif
#if defined(MCHP_C18)
#include <p18cxxx.h> // p18cxxx.h must have current processor
// defined.
#endif
#if defined(HITECH_C18)
#include <pic18.h>
#endif
/*********************************************************************
*
* General purpose typedef's
*
* Remove these definations if they are already defined in one of your
* application files.
********************************************************************/
//typedef enum _BOOL { FALSE = 0, TRUE } BOOL;
typedef unsigned char BYTE;
typedef union _BYTE_VAL
{
struct
{
unsigned int b0:1;
unsigned int b1:1;
unsigned int b2:1;
unsigned int b3:1;
unsigned int b4:1;
unsigned int b5:1;
unsigned int b6:1;
unsigned int b7:1;
} bits;
BYTE Val;
} BYTE_VAL;
#define ECAN_LIB_MODE_FIXED 0
#define ECAN_LIB_MODE_RUN_TIME 1
#define ECAN_TX2_MODE_DISABLE 0
#define ECAN_TX2_MODE_ENABLE 1
#define ECAN_INIT_NORMAL 0x00
#define ECAN_INIT_LOOPBACK 0x40
#define ECAN_INIT_CONFIGURATION 0x80
#define ECAN_INIT_DISABLE 0x20
#define ECAN_INIT_LISTEN_ONLY 0X60
#define ECAN_RXFn_ENABLE 1
#define ECAN_RXFn_DISABLE 0
// Enable auto RTR code when at least one buffer is configured in
// auto RTR mode.
#if (ECAN_FUNC_MODE_VAL != MODE_0 )
#if ( (ECAN_B0_AUTORTR_MODE == ECAN_AUTORTR_MODE_ENABLE) || \
(ECAN_B1_AUTORTR_MODE == ECAN_AUTORTR_MODE_ENABLE) || \
(ECAN_B2_AUTORTR_MODE == ECAN_AUTORTR_MODE_ENABLE) || \
(ECAN_B3_AUTORTR_MODE == ECAN_AUTORTR_MODE_ENABLE) || \
(ECAN_B4_AUTORTR_MODE == ECAN_AUTORTR_MODE_ENABLE) || \
(ECAN_B5_AUTORTR_MODE == ECAN_AUTORTR_MODE_ENABLE) )
#define ECAN_ENABLE_AUTO_RTR
#endif
#endif
/*********************************************************************
* Function: void ECANInitialize(void)
*
* Overview: Use this function to initialize ECAN module with
* options defined in ECAN.def file.
* You may manually edit ECAN.def file as per your
* requirements, or use Microchip Application
* Maestro tool.
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: All pending transmissions are aborted.
********************************************************************/
void ECANInitialize(void);
/*********************************************************************
*
* ECAN_TX_MSG_FLAGS
*
* This enumeration values define flags related to transmission of a
* CAN message. There could be more than one this flag
* ORed together to form multiple flags.
*
*********************************************************************/
typedef enum _ECAN_TX_MSG_FLAGS
{
ECAN_TX_PRIORITY_BITS = 0b00000011,
ECAN_TX_PRIORITY_0 = 0b00000000,
ECAN_TX_PRIORITY_1 = 0b00000001,
ECAN_TX_PRIORITY_2 = 0b00000010,
ECAN_TX_PRIORITY_3 = 0b00000011,
ECAN_TX_FRAME_BIT = 0b00100000,
ECAN_TX_STD_FRAME = 0b00000000,
ECAN_TX_XTD_FRAME = 0b00100000,
ECAN_TX_RTR_BIT = 0b01000000,
ECAN_TX_NO_RTR_FRAME = 0b00000000,
ECAN_TX_RTR_FRAME = 0b01000000
} ECAN_TX_MSG_FLAGS;
/*********************************************************************
* Function: BOOL ECANSendMessage(unsigned long id,
* BYTE *data,
* BYTE dataLen,
* ECAN_TX_MSG_FLAGS msgFlags)
*
* Overview: Use this function to transmit a CAN message.
* This function searches for empty transmit buffer
* and loads it with given messages. Buffer is then
* marked for ready to transmit.
*
* PreCondition: None
*
* Input: id - CAN message identifier.
* Only 11 or 29 bits may be used
* depending on standard or extended
* message type as specified in
* msgFlags parameter.
* data - Data bytes of upto 8 bytes in length
* dataLen - Data length from 0 thru 8.
* If 0, data may be NULL.
* msgFlags - One or ECAN_TX_MSG_FLAGS values ORed
* together
*
* Output: TRUE, if an empty buffer was found and loaded with
* given data
* FALSE, if otherwise.
*
* Side Effects: None
*
********************************************************************/
BOOL ECANSendMessage( unsigned long id,
BYTE *data,
BYTE dataLen,
ECAN_TX_MSG_FLAGS msgFlags);
/*********************************************************************
* Function: BOOL ECANLoadRTRBuffer(BYTE buffer,
* unsigned long id,
* BYTE *data,
* BYTE dataLen,
* BYTE type)
*
* Overview: Use this function to update automatic RTR buffer.
*
*
* PreCondition: None
*
* Input: buffer - Buffer number that is to be loaded
* id - CAN message identifier.
* Only 11 or 29 bits may be used
* depending on standard or extended
* message type as specified in
* type parameter.
* data - Data bytes of upto 8 bytes in length
* dataLen - Data length from 0 thru 8.
* If 0, data may be NULL.
* type - Buffer type
* Must be ECAN_MSG_STD for Standard
* ECAN_MSG_XTD for Extended
*
* Output: TRUE, if given data was successfully loaded
* FALSE, if RTR buffer was in process of transmitting
* automated response.
*
* Side Effects: None
*
********************************************************************/
#if ( defined(ECAN_ENABLE_AUTO_RTR) )
BOOL ECANLoadRTRBuffer(BYTE buffer,
unsigned long id,
BYTE *data,
BYTE dataLen,
BYTE type);
#endif
/*********************************************************************
*
* ECAN_RX_MSG_FLAGS
*
* This enumeration values define flags related to reception of a ECAN
* message. There could be more than one this flag
* ORed together to form multiple flags.
* If a particular bit is set, corresponding meaning is TRUE or else
* it will be FALSE.
*
* e.g.
* if (msgFlag & ECAN_RX_OVERFLOW)
* {
* // Receiver overflow has occured.
* ...
* }
*
********************************************************************/
typedef enum _ECAN_RX_MSG_FLAGS
{
ECAN_RX_OVERFLOW = 0b00001000,
ECAN_RX_INVALID_MSG = 0b00010000,
ECAN_RX_XTD_FRAME = 0b00100000,
ECAN_RX_RTR_FRAME = 0b01000000,
ECAN_RX_DBL_BUFFERED = 0b10000000
} ECAN_RX_MSG_FLAGS;
/*********************************************************************
* Function: BOOL ECANReceiveMessage(unsigned long *id,
* BYTE *data,
* BYTE *dataLen,
* ECAN_RX_MSG_FLAGS *msgFlags)
*
* Overview: Use this function to check for full receive buffer
* and extract received data into local buffers.
*
* PreCondition: None
*
* Input: id - Pointer to buffer that will be
* populated with receive ID.
* data - Pointer to buffer that will be
* populated with data if there is any
* dataLen - Pointer to buffer that will be
* populated with count of bytes
* copied in data buffer.
* msgFlags - Pointer to buffer that will be
* copied with information about
* received message. More than
* one information is ORed together.
*
* Output: TRUE, if a full receive buffer was found and
* given parameters were populated.
* FALSE, if otherwise.
*
* Side Effects: None
*
* Note: If you need to know the filter number that caused
* this message to get accepted, call
* ECANGetFilterHitInfo().
*
********************************************************************/
BOOL ECANReceiveMessage( unsigned long* id,
BYTE *Data,
BYTE *DataLen,
ECAN_RX_MSG_FLAGS *MsgFlags);
/*********************************************************************
* Macro: BYTE ECANGetFilterHitInfo(void)
*
* Overview: Use this macro to extract filter number that
* caused a message to get accepted.
* You must call this macro immediately after calling
* ECANReceiveMessage function.
*
* PreCondition: ECANReceiveMessage is called and returned TRUE
*
* Input: None
*
* Output: Number of filter that caused acceptance.
* 0 means RXF0, 1 means RXF1 and so on.
*
* Side Effects: None
*
* Note: This macro will return filter hit information
* for previously received message only.
*
********************************************************************/
#define ECANGetFilterHitInfo() (_ECANRxFilterHitInfo.Val)
extern BYTE_VAL _ECANRxFilterHitInfo;
/*********************************************************************
*
* ECAN_OP_MODE
*
* This enumeration values define codes related to ECAN module
* operation mode. ECANSetOperationMode() routine requires this code.
* These values must be used by itself
* i.e. it cannot be ORed to form * multiple values.
*
********************************************************************/
typedef enum _ECAN_OP_MODE
{
ECAN_OP_MODE_BITS = 0xe0, // Use this to access opmode bits
ECAN_OP_MODE_NORMAL = 0x00,
ECAN_OP_MODE_SLEEP = 0x20,
ECAN_OP_MODE_LOOP = 0x40,
ECAN_OP_MODE_LISTEN = 0x60,
ECAN_OP_MODE_CONFIG = 0x80
} ECAN_OP_MODE;
/*********************************************************************
* Function: void ECANSetOperationMode(ECAN_OP_MODE mode)
*
* Overview: Use this function to switch ECAN module into
* given operational mode.
* You may not need to call this function if
* your application does not require run-time
* changes in ECAN mode.
*
* PreCondition: None
*
* Input: mode - Operation mode code
* must be of type ECAN_OP_MODES
*
* Output: MCU is set to requested mode
*
* Side Effects: None
*
* Note: This is a blocking call. It will not return until
* requested mode is set.
********************************************************************/
void ECANSetOperationMode(ECAN_OP_MODE mode);
/*********************************************************************
* Macro: void ECANSetOperationModeNoWait(ECAN_OP_MODE mode)
*
* Overview: Use this macro to request operation mode but
* do not want macro to wait for it get accepted.
* You must use ECANGetOperationMode() to esnure
* requested operation mode is accepted before
* performing operation critical steps.
* You may not need to call this macro if your
* application does not require run-time changes
* in ECAN mode.
*
* PreCondition: None
*
* Input: mode - Operation mode code
* must be of type enum ECAN_OP_MODES
*
* Output: MCU is set to requested mode
*
* Side Effects: None
*
* Note: This is a non-blocking call.
* It does not verify that
* ECAN module is switched to requested mode or not.
* Caller must use ECANGetOperationMode() to verify
* correct operation mode before performing mode
* specific operation.
*
********************************************************************/
#define ECANSetOperationModeNoWait(mode) CANCON = mode
/*********************************************************************
* Macro: ECAN_OP_MODE ECANGetOperationMode()
*
* Overview: Use this macro to obtain current operation mode
* ECAN module.
* You may not need to call this macro if your
* application does not require run-time changes.
* PreCondition:
*
* Input:
*
* Output: Current operational mode of ECAN module is returned
*
* Side Effects: None
*
********************************************************************/
#define ECANGetOperationMode() (CANCON & ECAN_OP_MODE_BITS)
/*********************************************************************
* Macro: ECANSetFunctionalMode(mode)
*
* Overview: Use this macro to set functional mode of ECAN.
* You may not need to call this macro if
* your application will not change mode at run-time.
*
* PreCondition: ECAN must be in configuration mode
* and ECAN_LIB_MODE_VAL = ECAN_LIB_MODE_RUN_TIME
*
* Input: mode - New mode
* Allowable values are
* ECAN_MODE_0 for Mode 0
* ECAN_MODE_1 for Mode 1
* ECAN_MODE_2 for Mode 2
*
* Output: None
*
* Side Effects:
*
********************************************************************/
#if ( ECAN_LIB_MODE_VAL == ECAN_LIB_MODE_RUN_TIME )
#define ECANSetFunctionalMode(mode) \
ECANCON_MDSEL1 = mode >> 7; \
ECANCON_MDSEL0 = mode >> 6;
#endif
#define ECAN_MODE_0 0x00
#define ECAN_MODE_1 0x40
#define ECAN_MODE_2 0x80
/*********************************************************************
* Macro: BYTE ECANGetFunctionalMode()
*
* Overview: Use this macro to get functional mode of ECAN.
* You may not need to call this macro if
* your application will not change mode at run-time.
*
* PreCondition: None
*
* Input: None
*
* Output: mode - ECAN_MODE_0
* ECAN_MODE_1
* ECAN_MODE_2
*
* Side Effects:
*
********************************************************************/
#define ECANGetFunctionalMode() (ECANCON & 0b11000000)
/*********************************************************************
* Macro: ECANSetBusSampleMode(mode)
*
* Overview: Use this macro to set CAN bus sample mode.
* You may not need to call this macro if your
* application does not require run-time change
* to sampling mode.
*
* PreCondition: ECAN must be in configuration mode
*
* Input: mode - Bus sample mode
* Allowable values are
* ECAN_BUS_SAMPLE_MODE_ONCE to sample once
* ECAN_BUS_SAMPLE_THRICE to sample thrice.
*
* Output: None
*
* Side Effects:
*
********************************************************************/
#define ECANSetBusSampleMode(mode) BRGCON2_SAM = mode
#define ECAN_BUS_SAMPLE_MODE_ONCE 0
#define ECAN_BUS_SAMPLE_MODE_THRICE 1
/*********************************************************************
* Macro: ECANSetWakeupMode(mode)
*
* Overview: Use this macro to set CAN bus activity bus mode.
* You may not need to call this macro if your
* application does not require run-time changes.
*
* PreCondition: ECAN must be in configuration mode
*
* Input: mode - CAN Bus activity wakeup mode
* Allowable values are
* ECAN_WAKEUP_MODE_ENABLE to enable bus wakeup
* ECAN_WAKEUP_MODE_DISABLE to disable bus wakeup
*
* Output: None
*
* Side Effects:
*
********************************************************************/
#define ECANSetWakeupMode(mode) BRGCON3_WAKDIS = mode
#define ECAN_WAKEUP_MODE_ENABLE 0
#define ECAN_WAKEUP_MODE_DISABLE 1
/*********************************************************************
* Macro: ECANSetFilterMode(mode)
*
* Overview: Use this macro to CAN wakeup low pass filter mode.
* You may not need to call this macro if your application
* does not require run-time changes.
*
* PreCondition: ECAN must be in configuration mode
*
* Input: mode - CAN wakeup filter mode
* Allowable values are
* ECAN_FILTER_MODE_DISABLE to not use wakeup filter
* ECAN_FILTER_MODE_ENABLE to use wakeup filter
*
* Output: None
*
* Side Effects:
*
********************************************************************/
#define ECANSetFilterMode(mode) BRGCON3_WAKFIL = mode
#define ECAN_FILTER_MODE_DISABLE 0
#define ECAN_FILTER_MODE_ENABLE 1
/*********************************************************************
* Macro: ECANSetTxDriveMode(mode)
*
* Overview: Use this macro to set CANTX pin drive options.
* You may not need to call this macro if your
* application does not require run-time change.
*
* PreCondition: None
*
* Input: mode - CANTX drive mode when trnasmitting
* recessive bit.
* Allowable values are
* ECAN_TXDRIVE_MODE_TRISTATE to drive tri-state
* ECAN_TXDRIVE_MODE_VDD to drive to Vdd
*
* Output: None
*
* Side Effects:
*
********************************************************************/
#define ECANSetTxDriveMode(mode) CIOCON_ENDRHI = mode
#define ECAN_TXDRIVE_MODE_TRISTATE 0
#define ECAN_TXDRIVE_MODE_VDD 1
/*********************************************************************
* Macro: ECANSetCANTX2Source(source)
*
* Overview: Use this macro to set CANTX2 pin source.
* This macro automatically enables CANTX2 pin.
* You may not need to call this macro if your
* application does not require run-time change.
*
* PreCondition: None
*
* Input: source - CANTX2 source
* Allowable values are
* ECAN_TX2_CAN_CLOCK to output CAN clock on TX2
* ECAN_TX2_CAN_DATA to output complete CAN
*
* Output: None
*
* Side Effects:
*
********************************************************************/
#define ECANSetCANTX2Mode(mode) CIOCON_TX2SRC = mode << 7; \
CIOCON_TX2EN = 1
#define ECAN_TX2_SOURCE_COMP 0
#define ECAN_TX2_SOURCE_CLOCK 1
/*********************************************************************
* Macro: ECANDisableCANTX2()
*
* Overview: Use this macro to disable CANTX2 pin.
* You may not need to call this macro if your
* application does not require run-time change.
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects:
*
********************************************************************/
#define ECANDisableCANTX2() CIOCON_TX2EN = 0
/*********************************************************************
* Macro: ECANSetCaptureMode(mode)
*
* Overview: Use this macro to set CAN CAPTURE mode. This is
* mainly used to timestamp incoming CAN messages.
* You would also need to setup and enable CCP module
* to obtain timeestamp information.
* You may not need to call this macro if your
* application does not require run-time change.
*
* PreCondition: None
*
* Input: mode - Capture mode
* Allowable values are
* ECAN_CAPTURE_MODE_DISABLE
* ECAN_CAPTURE_MODE_ENABLE
*
* Output: None
*
* Side Effects:
*
********************************************************************/
#define ECANSetCaptureMode(mode) CIOCON_CANCAP = mode
#define ECAN_CAPTURE_MODE_DISABLE 0
#define ECAN_CAPTURE_MODE_ENABLE 1
/*********************************************************************
* Macro: ECANSetPHSEG2Mode(mode)
*
* Overview: Use this macro to set PHSEG2 mode.
* You may not need to call this macro if your
* application does not require run-time change.
*
* PreCondition: None
*
* Input: mode - PHSEG2 Mode
* Allowable values are
* ECAN_PHSEG2_MODE_AUTOMATIC
* - Max. of PHSEG1 of IPT, whichever is greater
* ECAN_PHSEG2_MODE_PROGRAMMABLE
* - Freely programmable
*
* Output: None
*
* Side Effects:
*
********************************************************************/
#define ECANSetPHSEG2Mode(mode) BRGCON2_SEG2PHTS = mode
#define ECAN_PHSEG2_MODE_AUTOMATIC 0
#define ECAN_PHSEG2_MODE_PROGRAMMABLE 1
/*********************************************************************
* Macro: ECANSetB0AutoRTRMode(mode)
* ECANSetB1AutoRTRMode(mode)
* ECANSetB2AutoRTRMode(mode)
* ECANSetB3AutoRTRMode(mode)
* ECANSetB4AutoRTRMode(mode)
* ECANSetB5AutoRTRMode(mode)
*
* Overview: Use these macros to set automatic RTR handling
* mode for given programmable buffer.
* You may not need to call this macro if your
* application does not require run-time change.
*
* PreCondition: ECAN_LIB_MODE_VAL = ECAN_LIB_MODE_RUN_TIME
* ECAN_FUNC_MODE_VAL != ECAN_MODE_0
*
* Input: mode - AutoRTR mode
* Allowable values are
* ECAN_AUTORTR_MODE_DISABLE
* - To disable automatic RTR handling
* ECAN_AUTORTR_MODE_ENABLE
* - To enable automatic RTR handling
*
* Output: None
*
* Side Effects:
*
********************************************************************/
#if ( (ECAN_LIB_MODE_VAL == ECAN_LIB_MODE_RUN_TIME) || \
(ECAN_FUNC_MODE_VAL != ECAN_MODE_0) )
#define ECANSetB0AutoRTRMode(mode) B0CON_RTREN = mode; \
BSEL0_B0TXEN = 1
#define ECANSetB1AutoRTRMode(mode) B1CON_RTREN = mode; \
BSEL0_B1TXEN = 1
#define ECANSetB2AutoRTRMode(mode) B2CON_RTREN = mode; \
BSEL0_B2TXEN = 1
#define ECANSetB3AutoRTRMode(mode) B3CON_RTREN = mode; \
BSEL0_B3TXEN = 1
#define ECANSetB4AutoRTRMode(mode) B4CON_RTREN = mode; \
BSEL0_B4TXEN = 1
#define ECANSetB5AutoRTRMode(mode) B5CON_RTREN = mode; \
BSEL0_B5TXEN = 1
#endif
#define ECAN_AUTORTR_MODE_DISABLE 0
#define ECAN_AUTORTR_MODE_ENABLE 1
/*********************************************************************
* Macro: ECANSetRXMnValue(val, type)
*
* Overview: Use these macros to set specific mask value.
* You may not need to call this macro if your
* application does not require run-time change.
*
* PreCondition: ECAN must be in Configuration mode
*
* Input: val - Value to be set
* Actual value would depend on type.
* If type is Standard, value will be 11-bit long
* If type is Extended, value will be 29-bit long
* type - Mask type
* Allowable values are
* ECAN_MSG_STD for Standard type
* ECAN_MSG_XTD for Extended type
*
* Output: None
*
* Side Effects: None
*
*
* Overview: This function sets mask value for given mask number.
* Type of mask, whether standard or extended is defined
* by type parameter.
*
* Note: The MASK_RXF15 is only available in enhanced and
* FIFO modes
********************************************************************/
#define ECANSetRXM0Value(val, type) \
_CANIDToRegs((BYTE*)&RXM0SIDH, val, type)
#define ECANSetRXM1Value(val, type) \
_CANIDToRegs((BYTE*)&RXM1SIDH, val, type)
/*********************************************************************
* Macro: ECANSetRXFnValue(val, type)
*
* Overview: Use these macros to set receive filter values.
* You may not need to call this macro if your
* application does not require run-time change.
*
* PreCondition: ECAN must be in Configuration mode
* To set RXF6-RXF15 values,
* ECAN_LIB_MODE_VAL = ECAN_LIB_MODE_RUN_TIME OR
* ECAN_FUNC_MODE != ECAN_MODE_0
*
* Input: val - Value to be set
* type - Type of filter
* Allowable values are
* ECAN_MSG_STD if this is Standard filter
* ECAN_MSG_XTD if this is Extended filter
*
* Output: None
*
* Side Effects: None
*
* Overview: Use this macro to assign value to a receive filter.
*
* Note: There are total of sixteen macros - one for each
* receive filter. e.g. for RXF0 use ECANSetRXF0Value()
* for RXF2 use ECANSetRXF2Value()
********************************************************************/
#define ECANSetRXF0Value(val, type) \
RXFCON0_RXF0EN = 1; \
_CANIDToRegs((BYTE*)&RXF0SIDH, val, type)
#define ECANSetRXF1Value(val, type) \
RXFCON0_RXF1EN = 1; \
_CANIDToRegs((BYTE*)&RXF1SIDH, val, type);
#define ECANSetRXF2Value(val, type) \
RXFCON0_RXF2EN = 1; \
_CANIDToRegs((BYTE*)&RXF2SIDH, val, type);
#define ECANSetRXF3Value(val, type) \
RXFCON0_RXF3EN = 1; \
_CANIDToRegs((BYTE*)&RXF3SIDH, val, type);
#define ECANSetRXF4Value(val, type) \
RXFCON0_RXF4EN = 1; \
_CANIDToRegs((BYTE*)&RXF4SIDH, val, type);
#define ECANSetRXF5Value(val, type) \
RXFCON0_RXF5EN = 1; \
_CANIDToRegs((BYTE*)&RXF5SIDH, val, type);
#if ( (ECAN_LIB_MODE_VAL == ECAN_LIB_MODE_RUN_TIME) || \
(ECAN_FUNC_MODE_VAL != ECAN_MODE_0) )
#define ECANSetRXF6Value(val, type) \
RXFCON0_RXF6EN = 1; \
_CANIDToRegs((BYTE*)&RXF6SIDH, val, type);
#define ECANSetRXF7Value(val, type) \
RXFCON0_RXF7EN = 1; \
_CANIDToRegs((BYTE*)&RXF7SIDH, val, type);
#define ECANSetRXF8Value(val, type) \
RXFCON1_RXF8EN = 1; \
_CANIDToRegs((BYTE*)&RXF8SIDH, val, type);
#define ECANSetRXF9Value(val, type) \
RXFCON1_RXF9EN = 1; \
_CANIDToRegs((BYTE*)&RXF9SIDH, val, type);
#define ECANSetRXF10Value(val, type) \
RXFCON1_RXF10EN = 1; \
_CANIDToRegs((BYTE*)&RXF10SIDH, val, type);
#define ECANSetRXF11Value(val, type) \
RXFCON1_RXF11EN = 1; \
_CANIDToRegs((BYTE*)&RXF11SIDH, val, type);
#define ECANSetRXF12Value(val, type) \
RXFCON1_RXF12EN = 1; \
_CANIDToRegs((BYTE*)&RXF12SIDH, val, type);
#define ECANSetRXF13Value(val, type) \
RXFCON1_RXF13EN = 1; \
_CANIDToRegs((BYTE*)&RXF13SIDH, val, type);
#define ECANSetRXF14Value(val, type) \
RXFCON1_RXF14EN = 1; \
_CANIDToRegs((BYTE*)&RXF14SIDH, val, type);
#define ECANSetRXF15Value(val, type) \
RXFCON1_RXF15EN = 1; \
_CANIDToRegs((BYTE*)&RXF15SIDH, val, type);
#endif
#define ECAN_MSG_STD 0
#define ECAN_MSG_XTD 1
/*********************************************************************
* Macro: ECANSetBnTxRxMode(buffer, mode)
*
* Overview: Use this macro to configure a programmable buffer (B0-B5)
* as either transmit or receive buffer.
* You may not need to call this macro if your
* application does not require run-time change.
*
*
* PreCondition: ECAN_LIB_MODE = ECAN_LIB_MODE_RUN_TIME OR
* ECAN_FUNC_MODE_VAL != ECAN_MODE_0
*
* Input: buffer - Buffer that needs to be setup
* Allowable values are:
* B0, B1, B2, B3, B4, B5
* mode - Mode to be set.
* Allowable values are:
* ECAN_BUFFER_TX, ECAN_BUFFER_RX
*
* Output: None
*
* Side Effects:
*
* Example: // Set B0 as Transmit buffer
* ECANSetBnTxRxMode(B0, ECAN_TX)
*
* // Set B2 as receive buffer
* ECANSetBnTxRxMode(B2, ECAN_RX)
*
* Note: Parameter buffer must be a constant symbol of either
* B0, B1, B2, B3, B4 or B5.
* A variable parameter would result in compiler error.
*
* e.g. ECANSetBnTxRxMode(myBuffer, ECAN_BUFFER_TX)
* would not compile.
*
*
********************************************************************/
#if ( (ECAN_LIB_MODE_VAL == ECAN_LIB_MODE_RUN_TIME) || \
(ECAN_FUNC_MODE_VAL != ECAN_MODE_0) )
#define ECANSetBnTxRxMode(buffer, mode) \
BSEL0_##buffer##TXEN = mode
#endif
#define RXB0 0
#define RXB1 1
#define B0 2
#define B1 3
#define B2 4
#define B3 5
#define B4 6
#define B5 7
#define TXB0 8
#define TXB1 9
#define TXB2 10
#define ECAN_BUFFER_RX 0
#define ECAN_BUFFER_TX 1
/*********************************************************************
* Macro: ECANSetRXB0DblBuffer(mode)
*
* Overview: Use this macro to configure RXB0 in double buffering mode
* You may not need to call this macro if your
* application does not require run-time change.
*
*
* PreCondition: None
*
* Input: mode - Double buffer mode
* Allowable values are
* ECAN_DBL_BUFFER_ENABLE
* ECAN_DBL_BUFFER_DISABLE,
*
* Output: None
*
* Example: // Enable Double buffering mode
* ECANSetRXB0DblBuffer(ECAN_DBL_BUFFER_ENABLE)
*
*
* Side Effects:
*
* Note: None
*
********************************************************************/
#if ( (ECAN_LIB_MODE_VAL == ECAN_LIB_MODE_RUN_TIME) || \
(ECAN_FUNC_MODE_VAL == ECAN_MODE_0) )
#define ECANSetRXB0DblBuffer(mode) RXB0CON_RXB0DBEN = mode
#endif
#define ECAN_DBL_BUFFER_MODE_DISABLE 0
#define ECAN_DBL_BUFFER_MODE_ENABLE 1
/*********************************************************************
* Macro: ECANSetRxBnRxMode(buffer, mode)
*
* Overview: Use this macro to configure receive mode of a fixed receive buffer.
* You may not need to call this macro if your
* application does not require run-time change.
*
*
* PreCondition: None
*
* Input: buffer - Buffer that needs to be configured
* Allowable values are
* RXB0, RXB1
* mode - Mode to be setup.
* Allowable values are
* ECAN_RECEIVE_ALL,
* ECAN_RECEIVE_STANDARD,
* ECAN_RECEIVE_EXTENDED,
* ECAN_RECEIVE_ALL_VALID
*
* Output: None
*
* Example: // Configure RXB0 buffer to receive all valid messages.
* ECANSetRxBnRxMode(RXB0, ECAN_RECEIVE_ALL_VALID)
*
* // Configure RXB1 buffer to receive only Standard messages.
* ECANSetRxBnRxMode(RXB1, ECAN_RECEIVE_STANDARD)