forked from McNeight/CAN_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CAN_MCP2515.cpp
1503 lines (1330 loc) · 37.3 KB
/
CAN_MCP2515.cpp
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 © 2007-2015 Fabian Greif, David Harding, Kyle Crockett,
Nuno Alves, Stevenh, Collin Kidder, Daniel Kasamis, Cory Fowler, teachop,
Pedro Cevallos, Neil McNeight
This file is part of CAN_Library.
CAN_Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2.1 of the License, or
(at your option) any later version.
CAN_Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Acknowledgements:
Fabian Greif for the initial libraries for MCP2515, SJA1000 and AT90CAN
http://www.kreatives-chaos.com/artikel/universelle-can-bibliothek
as well as his updates at https://github.com/dergraaf/avr-can-lib
David Harding for his version of the MCP2515 library
http://forum.arduino.cc/index.php/topic,8730.0.html
Kyle Crockett CANduino library with 16Mhz oscillator
http://code.google.com/p/canduino/
Nuno Alves for the help on Extended ID messaging
Stevenh for his work on library and all of the MCP research/work
http://modelrail.otenko.com/arduino/arduino-controller-area-network-can
Collin Kidder (collin80) for his work on the Arduino Due CAN interface
https://github.com/collin80/due_can
Daniel Kasamis (togglebit) both for his code at
https://github.com/togglebit/ArduinoDUE_OBD_FreeRunningCAN as well as his
DUE CANshield http://togglebit.net/product/arduino-due-can-shield/
Cory Fowler (coryjfowler) for 16 MHz bitrate timing information
https://github.com/coryjfowler/MCP2515_lib
teachop for the FlexCAN library for the Teensy 3.1
https://github.com/teachop/FlexCAN_Library
-------------------------------------------------------------------------------
Change Log
DATE VER WHO WHAT
07/07/13 0.1 PC Modified and merge all MCP2515 libraries found. Stripped
away most unused functions and corrected MCP2515 defs
09/12/13 0.2 PC Added selectable CS SPI for CAN controller to use 1 IC
to control several mcp2515
02/05/14 0.3 PC Added filter and mask controls
05/01/14 0.4 PC Cleaned up functions, variables and added message
structures for J1939, CANopen and CAN.
05/07/14 1.0 PC Released Library to the public through GitHub
06/18/14 1.5 NEM Preparing a unified CAN library across three different
CAN controllers
06/14/15 1.6.0 NEM Code cleanup and compatibility with Arduino 1.6.*
-------------------------------------------------------------------------------
*/
#if defined(ARDUINO_ARCH_AVR) && !defined(__MK20DX256__)
#ifndef NORDIC_CAN
#include <Arduino.h>
#include <SPI.h>
#endif
#ifdef NORDIC_CAN
#include "SPI_nordic.h"
#include "nrf_delay.h"
SPIClass SPI;
SerialClass Serial;
#endif //NORDIC_CAN
#include "CAN.h"
#include "CAN_MCP2515.h"
///////////////////////////////////////////////////////////////////
/// ///
/// CAN library for MCP2515 ///
/// ///
///////////////////////////////////////////////////////////////////
//Initialize SPI communications and set MCP2515 into Config mode
CAN_MCP2515::CAN_MCP2515()
{
//Use a default of pin 10 for SPI chip select
#ifndef NORDIC_CAN
CS = 10;
#else
CS = SPI_SS_PIN;
#endif
_init();
}
void CAN_MCP2515::_init()
{
pinMode(CS, OUTPUT);
digitalWrite(CS, HIGH);
}
CAN_MCP2515::CAN_MCP2515(uint8_t CS_Pin)
{
CS = CS_Pin;
_init();
}
//Start MCP2515 communications
void CAN_MCP2515::begin(uint32_t bitrate, uint32_t osc, uint8_t mode)
{
SPI.begin(); //SPI communication begin
reset(); //Set MCP2515 into Config mode by soft reset. Note MCP2515 is in Config mode by default at power up.
clearRxBuffers();
clearTxBuffers();
//clearFilters();
// enable Transmit Buffer Empty Interrupt Enable bits
//enableInterrupts(MCP2515_TXnIE, MCP2515_TXnIE);
setBitrate(bitrate, osc); //Set CAN bit rate
setMode(mode); //Set CAN mode
}
void CAN_MCP2515::end()
{
SPI.end();
}
// Check to see if message is available
uint8_t CAN_MCP2515::available()
{
uint8_t msgStatus = readStatus();
// (msgStatus & 0x01) means message in RX buffer 0
// (msgStatus & 0x02) means message in RX buffer 1
// Returns number of messages available
return (msgStatus & MCP2515_STATUS_CANINTF_RXnIF);
}
// Receive and display CAN message.
// This allows use of the message structure for easier message handling.
CAN_Frame CAN_MCP2515::read()
{
CAN_Frame message;
uint8_t buffer, msgStatus;
uint8_t RXBnSIDH, RXBnSIDL, RXBnEID8, RXBnEID0, RXBnDLC;
msgStatus = readStatus();
if (msgStatus & MCP2515_STATUS_CANINTF_RX0IF)
{
buffer = MCP2515_READ_RX_BUFFER_0_ID;
}
else if (msgStatus & MCP2515_STATUS_CANINTF_RX1IF)
{
buffer = MCP2515_READ_RX_BUFFER_1_ID;
}
else
{
// No message?
message.valid = false;
return message;
}
digitalWrite(CS, LOW);
SPI.transfer(buffer);
RXBnSIDH = SPI.transfer(0xFF); // SID<10:3>
RXBnSIDL = SPI.transfer(0xFF); // SID<2:0>, SRR, IDE, EID<17:16>
RXBnEID8 = SPI.transfer(0xFF); // EID<15:8>
RXBnEID0 = SPI.transfer(0xFF); // EID<7:0>
RXBnDLC = SPI.transfer(0xFF); // RTR, RB<1:0>, DLC<3:0>
message.length = (RXBnDLC & MCP2515_DLC);
for (int i = 0; i < message.length; i++)
{
message.data[i] = SPI.transfer(0xFF);
}
digitalWrite(CS, HIGH);
message.extended = bitRead(RXBnSIDL, MCP2515_IDE);
// check to see if this is an Extended ID Msg.
if (message.extended == CAN_EXTENDED_FRAME)
{
// Serial.println(message.id, HEX);
// Serial.print(RXBnSIDH, HEX);
// Serial.print(RXBnSIDL, HEX);
// Serial.print(RXBnEID8, HEX);
// Serial.println(RXBnEID0, HEX);
// If you don't cast to a larger int _before_ assignment, then
// sign extension _WILL_ bite you!!!
// https://en.wikipedia.org/wiki/Sign_extension
message.id = ((uint32_t) RXBnSIDH << 21); // ID<28:21> = SIDH<7:0>
message.id |= ((uint32_t)(RXBnSIDL & MCP2515_SIDL_SID) << 13); // ID<20:18> = SIDL<7:5>
message.id |= ((uint32_t)(RXBnSIDL & MCP2515_SIDL_EID) << 16); // ID<17:16> = SIDL<1:0>
//Serial.println(message.id, HEX);
message.id |= ((uint32_t) RXBnEID8 << 8); // ID<15:8> = EID8<7:0>
//Serial.println(message.id, HEX);
message.id |= ((uint32_t) RXBnEID0 << 0); // ID<7:0> = EID0<7:0>
//Serial.println(message.id, HEX);
message.rtr = bitRead(RXBnDLC, MCP2515_RTR);
//Serial.println(message.id, HEX);
}
else if (message.extended == CAN_STANDARD_FRAME)
{
message.id = (RXBnSIDH << 3); // ID<10:3> = SIDH<7:0>
message.id |= ((RXBnSIDL & MCP2515_SIDL_SID) >> 5); // ID<2:0> = SIDL<7:5>
message.rtr = bitRead(RXBnSIDL, MCP2515_SRR);
}
// everything checks out!
message.valid = true;
return message;
}
// Receive and display any message (J1939, CANopen, CAN).
// This functions provides an easy way to see the message if user doesn't care about the actual message protocol.
// No message struct is used here.
void CAN_MCP2515::read(uint32_t *ID, uint8_t *length_out, uint8_t *data_out)
{
CAN_Frame message_to_be_parsed;
message_to_be_parsed = read();
(*ID) = message_to_be_parsed.id;
(*length_out) = message_to_be_parsed.length;
memcpy(data_out, message_to_be_parsed.data, sizeof(message_to_be_parsed.data));
}
void CAN_MCP2515::flush()
{
clearRxBuffers();
clearTxBuffers();
}
uint8_t CAN_MCP2515::write(const CAN_Frame &message)
{
uint8_t TXBnSIDH, TXBnSIDL, TXBnEID8, TXBnEID0, TXBnDLC, msgStatus, loadBuffer, sendBuffer;
msgStatus = readStatus();
if (!(msgStatus & MCP2515_STATUS_TXB0CNTRL_TXREQ)) //transmit buffer 0 is open
{
loadBuffer = MCP2515_LOAD_TX_BUFFER_0_ID;
sendBuffer = MCP2515_RTS_TXB0;
}
else if (!(msgStatus & MCP2515_STATUS_TXB1CNTRL_TXREQ)) //transmit buffer 1 is open
{
loadBuffer = MCP2515_LOAD_TX_BUFFER_1_ID;
sendBuffer = MCP2515_RTS_TXB1;
}
else if (!(msgStatus & MCP2515_STATUS_TXB2CNTRL_TXREQ)) //transmit buffer 2 is open
{
loadBuffer = MCP2515_LOAD_TX_BUFFER_2_ID;
sendBuffer = MCP2515_RTS_TXB2;
}
else
{
// No transmit buffers available; no message sent
return 0;
}
TXBnDLC = (message.length & MCP2515_DLC);
if (message.extended == CAN_EXTENDED_FRAME)
{
//generate id bytes before SPI write
TXBnSIDH = (message.id >> 21); // SIDH<7:0> = ID<28:21>
TXBnSIDL = ((message.id >> 13) & MCP2515_SIDL_SID); // SIDL<7:5> = ID<20:18>
TXBnSIDL |= ((message.id >> 16) & MCP2515_SIDL_EID); // SIDL<1:0> = ID<17:16>
bitSet(TXBnSIDL, MCP2515_IDE);
TXBnEID8 = (message.id >> 8); // EID8<7:0> = ID<15:8>
TXBnEID0 = (message.id >> 0); // EID0<7:0> = ID<7:0>
}
else if (message.extended == CAN_STANDARD_FRAME)
{
TXBnSIDH = (message.id >> 3); // SIDH<7:0> = ID<10:3>
TXBnSIDL = ((message.id << 5) & MCP2515_SIDL_SID); // SIDL<8:5> = ID<2:0>
TXBnEID8 = 0x00; // zero out extended ID registers
TXBnEID0 = 0x00; // zero out extended ID registers
}
if (message.rtr)
{
bitSet(TXBnDLC, MCP2515_RTR);
}
digitalWrite(CS, LOW);
SPI.transfer(loadBuffer);
SPI.transfer(TXBnSIDH); //ID high bits
SPI.transfer(TXBnSIDL); //ID low bits
SPI.transfer(TXBnEID8); //extended ID high bits
SPI.transfer(TXBnEID0); //extended ID low bits
SPI.transfer(TXBnDLC); //data length code
#ifdef MCP2515_SERIAL_DEBUG
Serial.print("write2");
#endif
for (int i = 0; i < message.length; i++) //load data buffer
{
SPI.transfer(message.data[i]);
#ifdef MCP2515_SERIAL_DEBUG
Serial.print(',');
if (message.data[i] < 0x10) // If the data is less than 10 hex it will assign a zero to the front as leading zeros are ignored...
{
Serial.print('0');
}
Serial.print(message.data[i], HEX);
#endif
}
#ifdef MCP2515_SERIAL_DEBUG
Serial.println();
#endif
digitalWrite(CS, HIGH);
digitalWrite(CS, LOW);
SPI.transfer(sendBuffer);
digitalWrite(CS, HIGH);
return message.length;
}
// Function to load and send any message. (J1939, CANopen, CAN). It assumes user knows what the ID is supposed to be
uint8_t CAN_MCP2515::write(uint32_t ID, uint8_t frameType, uint8_t length, uint8_t *data) // changed from send() to write()
{
#ifdef MCP2515_SERIAL_DEBUG
Serial.print(F("write1"));
for (uint8_t i = 0; i < length; i++)
{
Serial.print(',');
if (data[i] < 0x10) // If the data is less than 10 hex it will assign a zero to the front as leading zeros are ignored...
{
Serial.print('0');
}
Serial.print(data[i], HEX); // Displays message data
}
Serial.println(); // adds a line
#endif
CAN_Frame message_to_be_sent;
message_to_be_sent.id = ID;
message_to_be_sent.length = length;
message_to_be_sent.extended = frameType;
// memcpy(message_to_be_sent.data, data, sizeof(data));
for (uint8_t i = 0; i < length; i++)
{
message_to_be_sent.data[i] = data[i];
}
return (write(message_to_be_sent));
}
// MCP2515 SPI INTERFACE COMMANDS
// Reset command
void CAN_MCP2515::reset()
{
digitalWrite(CS, LOW);
SPI.transfer(MCP2515_SPI_RESET);
digitalWrite(CS, HIGH);
nrf_delay_ms(100);
if(readStatus() && 0x80 != 0)
NRF_LOG_INFO("BULLSHIT");
}
//Reads a single MCP2515 register
uint8_t CAN_MCP2515::readAddress(uint8_t address)
{
digitalWrite(CS, LOW);
SPI.transfer(MCP2515_SPI_READ);
SPI.transfer(address);
uint8_t retVal = SPI.transfer(0xFF);
digitalWrite(CS, HIGH);
return retVal;
}
// Writes a single MCP2515 register
void CAN_MCP2515::writeAddress(uint8_t address, uint8_t value)
{
digitalWrite(CS, LOW);
SPI.transfer(MCP2515_SPI_WRITE);
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(CS, HIGH);
}
// Modifies a single MCP2515 register
void CAN_MCP2515::modifyAddress(uint8_t address, uint8_t mask, uint8_t value)
{
digitalWrite(CS, LOW);
SPI.transfer(MCP2515_SPI_BIT_MODIFY);
SPI.transfer(address);
SPI.transfer(mask);
SPI.transfer(value);
digitalWrite(CS, HIGH);
}
//Function that reads several status bits for transmit and receive functions.
uint8_t CAN_MCP2515::readStatus()
{
digitalWrite(CS, LOW);
SPI.transfer(MCP2515_SPI_READ_STATUS);
uint8_t retVal = SPI.transfer(0xFF);
digitalWrite(CS, HIGH);
return retVal;
}
//Function that reads receive functions and filter hits
uint8_t CAN_MCP2515::readRXStatus()
{
digitalWrite(CS, LOW);
SPI.transfer(MCP2515_SPI_RX_STATUS);
uint8_t retVal = SPI.transfer(0xFF);
digitalWrite(CS, HIGH);
return retVal;
/*
Values are as follows
|bit|bit|bit|bit|bit|bit|bit|bit| Received Message
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | -----------------
| 0 | 0 |---|---|---|---|---|---| No RX message
| 0 | 1 |---|---|---|---|---|---| Message in RXB0
| 1 | 0 |---|---|---|---|---|---| Message in RXB1
| 1 | 1 |---|---|---|---|---|---| Message in both buffers* (Buffer 0 has higher priority,
therefore, RXB0 status is reflected in bits 4:0)
|bit|bit|bit|bit|bit|bit|bit|bit| Msg Type Received
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | -----------------
|---|---|---| 0 | 0 |---|---|---| Standard data frame
|---|---|---| 0 | 1 |---|---|---| Standard remote frame
|---|---|---| 1 | 0 |---|---|---| Extended data frame
|---|---|---| 1 | 1 |---|---|---| Extended remote frame
|bit|bit|bit|bit|bit|bit|bit|bit| Filter Match
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | -----------------
|---|---|---|---|---| 0 | 0 | 0 | RXF0
|---|---|---|---|---| 0 | 0 | 1 | RXF1
|---|---|---|---|---| 0 | 1 | 0 | RXF2
|---|---|---|---|---| 0 | 1 | 1 | RXF3
|---|---|---|---|---| 1 | 0 | 0 | RXF4
|---|---|---|---|---| 1 | 0 | 1 | RXF5
|---|---|---|---|---| 1 | 1 | 0 | RXF0 (rollover to RXB1)
|---|---|---|---|---| 1 | 1 | 1 | RXF1 (rollover to RXB1)
*/
}
// MCP2515 INITIALIZATION COMMANDS.
//MCP2515 can be set into 5 different modes: CONFIG, NORMAL, SLEEP, LISTEN, LOOPBACK
void CAN_MCP2515::setMode(uint8_t mode)
{
modifyAddress(MCP2515_CANCTRL, MCP2515_REQOPn, mode); //Writes config values to registers
}
// Function to read mode back
uint8_t CAN_MCP2515::getMode()
{
return (readAddress(MCP2515_CANSTAT) & MCP2515_REQOPn);
}
//Sets MCP2515 controller bitrate.
// Configuration speeds are determined by Crystal Oscillator.
// See MCP2515 datasheet Pg39 for more info.
void CAN_MCP2515::setBitrate(uint32_t bitrate, uint32_t canClock)
{
uint8_t set, cfg1, cfg2, cfg3;
set = 1;
switch (canClock)
{
case (MCP_4MHZ):
switch (bitrate)
{
case (CAN_BPS_5K):
cfg1 = MCP_4MHz_5kBPS_CFG1;
cfg2 = MCP_4MHz_5kBPS_CFG2;
cfg3 = MCP_4MHz_5kBPS_CFG3;
break;
case (CAN_BPS_10K):
cfg1 = MCP_4MHz_10kBPS_CFG1;
cfg2 = MCP_4MHz_10kBPS_CFG2;
cfg3 = MCP_4MHz_10kBPS_CFG3;
break;
case (CAN_BPS_20K):
cfg1 = MCP_4MHz_20kBPS_CFG1;
cfg2 = MCP_4MHz_20kBPS_CFG2;
cfg3 = MCP_4MHz_20kBPS_CFG3;
break;
case (CAN_BPS_25K):
cfg1 = MCP_4MHz_25kBPS_CFG1;
cfg2 = MCP_4MHz_25kBPS_CFG2;
cfg3 = MCP_4MHz_25kBPS_CFG3;
break;
case (CAN_BPS_31K25):
cfg1 = MCP_4MHz_31k25BPS_CFG1;
cfg2 = MCP_4MHz_31k25BPS_CFG2;
cfg3 = MCP_4MHz_31k25BPS_CFG3;
break;
case (CAN_BPS_33K3):
cfg1 = MCP_4MHz_33k3BPS_CFG1;
cfg2 = MCP_4MHz_33k3BPS_CFG2;
cfg3 = MCP_4MHz_33k3BPS_CFG3;
break;
case (CAN_BPS_40K):
cfg1 = MCP_4MHz_40kBPS_CFG1;
cfg2 = MCP_4MHz_40kBPS_CFG2;
cfg3 = MCP_4MHz_40kBPS_CFG3;
break;
case (CAN_BPS_50K):
cfg1 = MCP_4MHz_50kBPS_CFG1;
cfg2 = MCP_4MHz_50kBPS_CFG2;
cfg3 = MCP_4MHz_50kBPS_CFG3;
break;
case (CAN_BPS_80K):
cfg1 = MCP_4MHz_80kBPS_CFG1;
cfg2 = MCP_4MHz_80kBPS_CFG2;
cfg3 = MCP_4MHz_80kBPS_CFG3;
break;
case (CAN_BPS_83K33):
cfg1 = MCP_4MHz_83k3BPS_CFG1;
cfg2 = MCP_4MHz_83k3BPS_CFG2;
cfg3 = MCP_4MHz_83k3BPS_CFG3;
break;
case (CAN_BPS_95K): // 95.238Kbps
cfg1 = MCP_4MHz_100kBPS_CFG1;
cfg2 = MCP_4MHz_100kBPS_CFG2;
cfg3 = MCP_4MHz_100kBPS_CFG3;
break;
case (CAN_BPS_100K):
cfg1 = MCP_4MHz_100kBPS_CFG1;
cfg2 = MCP_4MHz_100kBPS_CFG2;
cfg3 = MCP_4MHz_100kBPS_CFG3;
break;
case (CAN_BPS_125K):
cfg1 = MCP_4MHz_125kBPS_CFG1;
cfg2 = MCP_4MHz_125kBPS_CFG2;
cfg3 = MCP_4MHz_125kBPS_CFG3;
break;
case (CAN_BPS_200K):
cfg1 = MCP_4MHz_200kBPS_CFG1;
cfg2 = MCP_4MHz_200kBPS_CFG2;
cfg3 = MCP_4MHz_200kBPS_CFG3;
break;
case (CAN_BPS_250K):
cfg1 = MCP_4MHz_250kBPS_CFG1;
cfg2 = MCP_4MHz_250kBPS_CFG2;
cfg3 = MCP_4MHz_250kBPS_CFG3;
break;
default:
set = 0;
#if DEBUG_EN
Serial.print("bitrate invalid or not supported with this canClock...\r\n");
#endif
return;
break;
}
break;
case (MCP_8MHZ):
switch (bitrate)
{
case (CAN_BPS_5K):
cfg1 = MCP_8MHz_5kBPS_CFG1;
cfg2 = MCP_8MHz_5kBPS_CFG2;
cfg3 = MCP_8MHz_5kBPS_CFG3;
break;
case (CAN_BPS_10K):
cfg1 = MCP_8MHz_10kBPS_CFG1;
cfg2 = MCP_8MHz_10kBPS_CFG2;
cfg3 = MCP_8MHz_10kBPS_CFG3;
break;
case (CAN_BPS_20K):
cfg1 = MCP_8MHz_20kBPS_CFG1;
cfg2 = MCP_8MHz_20kBPS_CFG2;
cfg3 = MCP_8MHz_20kBPS_CFG3;
break;
case (CAN_BPS_25K):
cfg1 = MCP_8MHz_25kBPS_CFG1;
cfg2 = MCP_8MHz_25kBPS_CFG2;
cfg3 = MCP_8MHz_25kBPS_CFG3;
break;
case (CAN_BPS_31K25):
cfg1 = MCP_8MHz_31k25BPS_CFG1;
cfg2 = MCP_8MHz_31k25BPS_CFG2;
cfg3 = MCP_8MHz_31k25BPS_CFG3;
break;
case (CAN_BPS_33K3):
cfg1 = MCP_8MHz_33k3BPS_CFG1;
cfg2 = MCP_8MHz_33k3BPS_CFG2;
cfg3 = MCP_8MHz_33k3BPS_CFG3;
break;
case (CAN_BPS_40K):
cfg1 = MCP_8MHz_40kBPS_CFG1;
cfg2 = MCP_8MHz_40kBPS_CFG2;
cfg3 = MCP_8MHz_40kBPS_CFG3;
break;
case (CAN_BPS_50K):
cfg1 = MCP_8MHz_50kBPS_CFG1;
cfg2 = MCP_8MHz_50kBPS_CFG2;
cfg3 = MCP_8MHz_50kBPS_CFG3;
break;
case (CAN_BPS_80K):
cfg1 = MCP_8MHz_80kBPS_CFG1;
cfg2 = MCP_8MHz_80kBPS_CFG2;
cfg3 = MCP_8MHz_80kBPS_CFG3;
break;
case (CAN_BPS_83K33):
cfg1 = MCP_8MHz_83k3BPS_CFG1;
cfg2 = MCP_8MHz_83k3BPS_CFG2;
cfg3 = MCP_8MHz_83k3BPS_CFG3;
break;
case (CAN_BPS_95K): // 95.238Kbps
cfg1 = MCP_8MHz_100kBPS_CFG1;
cfg2 = MCP_8MHz_100kBPS_CFG2;
cfg3 = MCP_8MHz_100kBPS_CFG3;
break;
case (CAN_BPS_100K):
cfg1 = MCP_8MHz_100kBPS_CFG1;
cfg2 = MCP_8MHz_100kBPS_CFG2;
cfg3 = MCP_8MHz_100kBPS_CFG3;
break;
case (CAN_BPS_125K):
cfg1 = MCP_8MHz_125kBPS_CFG1;
cfg2 = MCP_8MHz_125kBPS_CFG2;
cfg3 = MCP_8MHz_125kBPS_CFG3;
break;
case (CAN_BPS_200K):
cfg1 = MCP_8MHz_200kBPS_CFG1;
cfg2 = MCP_8MHz_200kBPS_CFG2;
cfg3 = MCP_8MHz_200kBPS_CFG3;
break;
case (CAN_BPS_250K):
cfg1 = MCP_8MHz_250kBPS_CFG1;
cfg2 = MCP_8MHz_250kBPS_CFG2;
cfg3 = MCP_8MHz_250kBPS_CFG3;
break;
case (CAN_BPS_500K):
cfg1 = MCP_8MHz_500kBPS_CFG1;
cfg2 = MCP_8MHz_500kBPS_CFG2;
cfg3 = MCP_8MHz_500kBPS_CFG3;
break;
case (CAN_BPS_666K):
cfg1 = MCP_8MHz_666k6BPS_CFG1;
cfg2 = MCP_8MHz_666k6BPS_CFG2;
cfg3 = MCP_8MHz_666k6BPS_CFG3;
break;
default:
set = 0;
#if DEBUG_EN
Serial.print("bitrate invalid or not supported with this canClock...\r\n");
#endif
return;
break;
}
break;
case (MCP_10MHZ):
switch (bitrate)
{
case (CAN_BPS_5K):
cfg1 = MCP_10MHz_5kBPS_CFG1;
cfg2 = MCP_10MHz_5kBPS_CFG2;
cfg3 = MCP_10MHz_5kBPS_CFG3;
break;
case (CAN_BPS_10K):
cfg1 = MCP_10MHz_10kBPS_CFG1;
cfg2 = MCP_10MHz_10kBPS_CFG2;
cfg3 = MCP_10MHz_10kBPS_CFG3;
break;
case (CAN_BPS_20K):
cfg1 = MCP_10MHz_20kBPS_CFG1;
cfg2 = MCP_10MHz_20kBPS_CFG2;
cfg3 = MCP_10MHz_20kBPS_CFG3;
break;
case (CAN_BPS_25K):
cfg1 = MCP_10MHz_25kBPS_CFG1;
cfg2 = MCP_10MHz_25kBPS_CFG2;
cfg3 = MCP_10MHz_25kBPS_CFG3;
break;
case (CAN_BPS_31K25):
cfg1 = MCP_10MHz_31k25BPS_CFG1;
cfg2 = MCP_10MHz_31k25BPS_CFG2;
cfg3 = MCP_10MHz_31k25BPS_CFG3;
break;
case (CAN_BPS_33K3):
cfg1 = MCP_10MHz_33k3BPS_CFG1;
cfg2 = MCP_10MHz_33k3BPS_CFG2;
cfg3 = MCP_10MHz_33k3BPS_CFG3;
break;
case (CAN_BPS_40K):
cfg1 = MCP_10MHz_40kBPS_CFG1;
cfg2 = MCP_10MHz_40kBPS_CFG2;
cfg3 = MCP_10MHz_40kBPS_CFG3;
break;
case (CAN_BPS_50K):
cfg1 = MCP_10MHz_50kBPS_CFG1;
cfg2 = MCP_10MHz_50kBPS_CFG2;
cfg3 = MCP_10MHz_50kBPS_CFG3;
break;
case (CAN_BPS_83K33):
cfg1 = MCP_10MHz_83k3BPS_CFG1;
cfg2 = MCP_10MHz_83k3BPS_CFG2;
cfg3 = MCP_10MHz_83k3BPS_CFG3;
break;
case (CAN_BPS_100K):
cfg1 = MCP_10MHz_100kBPS_CFG1;
cfg2 = MCP_10MHz_100kBPS_CFG2;
cfg3 = MCP_10MHz_100kBPS_CFG3;
break;
case (CAN_BPS_125K):
cfg1 = MCP_10MHz_125kBPS_CFG1;
cfg2 = MCP_10MHz_125kBPS_CFG2;
cfg3 = MCP_10MHz_125kBPS_CFG3;
break;
case (CAN_BPS_200K):
cfg1 = MCP_10MHz_200kBPS_CFG1;
cfg2 = MCP_10MHz_200kBPS_CFG2;
cfg3 = MCP_10MHz_200kBPS_CFG3;
break;
case (CAN_BPS_250K):
cfg1 = MCP_10MHz_250kBPS_CFG1;
cfg2 = MCP_10MHz_250kBPS_CFG2;
cfg3 = MCP_10MHz_250kBPS_CFG3;
break;
case (CAN_BPS_500K):
cfg1 = MCP_10MHz_500kBPS_CFG1;
cfg2 = MCP_10MHz_500kBPS_CFG2;
cfg3 = MCP_10MHz_500kBPS_CFG3;
break;
case (CAN_BPS_1000K):
cfg1 = MCP_10MHz_1000kBPS_CFG1;
cfg2 = MCP_10MHz_1000kBPS_CFG2;
cfg3 = MCP_10MHz_1000kBPS_CFG3;
break;
default:
set = 0;
#if DEBUG_EN
Serial.print("bitrate invalid or not supported with this canClock...\r\n");
#endif
return;
break;
}
break;
case (MCP_16MHZ):
switch (bitrate)
{
case (CAN_BPS_5K):
cfg1 = MCP_16MHz_5kBPS_CFG1;
cfg2 = MCP_16MHz_5kBPS_CFG2;
cfg3 = MCP_16MHz_5kBPS_CFG3;
break;
case (CAN_BPS_10K):
cfg1 = MCP_16MHz_10kBPS_CFG1;
cfg2 = MCP_16MHz_10kBPS_CFG2;
cfg3 = MCP_16MHz_10kBPS_CFG3;
break;
case (CAN_BPS_20K):
cfg1 = MCP_16MHz_20kBPS_CFG1;
cfg2 = MCP_16MHz_20kBPS_CFG2;
cfg3 = MCP_16MHz_20kBPS_CFG3;
break;
case (CAN_BPS_25K):
cfg1 = MCP_16MHz_25kBPS_CFG1;
cfg2 = MCP_16MHz_25kBPS_CFG2;
cfg3 = MCP_16MHz_25kBPS_CFG3;
break;
case (CAN_BPS_31K25):
cfg1 = MCP_16MHz_31k25BPS_CFG1;
cfg2 = MCP_16MHz_31k25BPS_CFG2;
cfg3 = MCP_16MHz_31k25BPS_CFG3;
break;
case (CAN_BPS_33K3):
cfg1 = MCP_16MHz_33k3BPS_CFG1;
cfg2 = MCP_16MHz_33k3BPS_CFG2;
cfg3 = MCP_16MHz_33k3BPS_CFG3;
break;
case (CAN_BPS_40K):
cfg1 = MCP_16MHz_40kBPS_CFG1;
cfg2 = MCP_16MHz_40kBPS_CFG2;
cfg3 = MCP_16MHz_40kBPS_CFG3;
break;
case (CAN_BPS_50K):
cfg1 = MCP_16MHz_50kBPS_CFG1;
cfg2 = MCP_16MHz_50kBPS_CFG2;
cfg3 = MCP_16MHz_50kBPS_CFG3;
break;
case (CAN_BPS_80K):
cfg1 = MCP_16MHz_80kBPS_CFG1;
cfg2 = MCP_16MHz_80kBPS_CFG2;
cfg3 = MCP_16MHz_80kBPS_CFG3;
break;
case (CAN_BPS_83K33):
cfg1 = MCP_16MHz_83k3BPS_CFG1;
cfg2 = MCP_16MHz_83k3BPS_CFG2;
cfg3 = MCP_16MHz_83k3BPS_CFG3;
break;
case (CAN_BPS_95K):
cfg1 = MCP_16MHz_95kBPS_CFG1;
cfg2 = MCP_16MHz_95kBPS_CFG2;
cfg3 = MCP_16MHz_95kBPS_CFG3;
break;
case (CAN_BPS_100K):
cfg1 = MCP_16MHz_100kBPS_CFG1;
cfg2 = MCP_16MHz_100kBPS_CFG2;
cfg3 = MCP_16MHz_100kBPS_CFG3;
break;
case (CAN_BPS_125K):
cfg1 = MCP_16MHz_125kBPS_CFG1;
cfg2 = MCP_16MHz_125kBPS_CFG2;
cfg3 = MCP_16MHz_125kBPS_CFG3;
break;
case (CAN_BPS_200K):
cfg1 = MCP_16MHz_200kBPS_CFG1;
cfg2 = MCP_16MHz_200kBPS_CFG2;
cfg3 = MCP_16MHz_200kBPS_CFG3;
break;
case (CAN_BPS_250K):
cfg1 = MCP_16MHz_250kBPS_CFG1;
cfg2 = MCP_16MHz_250kBPS_CFG2;
cfg3 = MCP_16MHz_250kBPS_CFG3;
break;
case (CAN_BPS_500K):
cfg1 = MCP_16MHz_500kBPS_CFG1;
cfg2 = MCP_16MHz_500kBPS_CFG2;
cfg3 = MCP_16MHz_500kBPS_CFG3;
break;
case (CAN_BPS_666K):
cfg1 = MCP_16MHz_666k6BPS_CFG1;
cfg2 = MCP_16MHz_666k6BPS_CFG2;
cfg3 = MCP_16MHz_666k6BPS_CFG3;
break;
case (CAN_BPS_1000K):
cfg1 = MCP_16MHz_1000kBPS_CFG1;
cfg2 = MCP_16MHz_1000kBPS_CFG2;
cfg3 = MCP_16MHz_1000kBPS_CFG3;
break;
default:
set = 0;
#if DEBUG_EN
Serial.print("bitrate invalid or not supported with this canClock...\r\n");
#endif
return;
break;
}
break;
case (MCP_20MHZ):
switch (bitrate)
{
case (CAN_BPS_10K):
cfg1 = MCP_20MHz_10kBPS_CFG1;
cfg2 = MCP_20MHz_10kBPS_CFG2;
cfg3 = MCP_20MHz_10kBPS_CFG3;
break;
case (CAN_BPS_20K):
cfg1 = MCP_20MHz_20kBPS_CFG1;
cfg2 = MCP_20MHz_20kBPS_CFG2;
cfg3 = MCP_20MHz_20kBPS_CFG3;
break;
case (CAN_BPS_25K):
cfg1 = MCP_20MHz_25kBPS_CFG1;
cfg2 = MCP_20MHz_25kBPS_CFG2;
cfg3 = MCP_20MHz_25kBPS_CFG3;
break;
case (CAN_BPS_31K25):
cfg1 = MCP_20MHz_31k25BPS_CFG1;
cfg2 = MCP_20MHz_31k25BPS_CFG2;
cfg3 = MCP_20MHz_31k25BPS_CFG3;
break;
case (CAN_BPS_33K3):
cfg1 = MCP_20MHz_33k3BPS_CFG1;
cfg2 = MCP_20MHz_33k3BPS_CFG2;
cfg3 = MCP_20MHz_33k3BPS_CFG3;
break;
case (CAN_BPS_40K):
cfg1 = MCP_20MHz_40kBPS_CFG1;
cfg2 = MCP_20MHz_40kBPS_CFG2;
cfg3 = MCP_20MHz_40kBPS_CFG3;
break;
case (CAN_BPS_50K):
cfg1 = MCP_20MHz_50kBPS_CFG1;
cfg2 = MCP_20MHz_50kBPS_CFG2;
cfg3 = MCP_20MHz_50kBPS_CFG3;
break;
case (CAN_BPS_80K):
cfg1 = MCP_20MHz_80kBPS_CFG1;
cfg2 = MCP_20MHz_80kBPS_CFG2;
cfg3 = MCP_20MHz_80kBPS_CFG3;
break;
case (CAN_BPS_83K33):
cfg1 = MCP_20MHz_83k3BPS_CFG1;
cfg2 = MCP_20MHz_83k3BPS_CFG2;
cfg3 = MCP_20MHz_83k3BPS_CFG3;
break;
case (CAN_BPS_95K):
cfg1 = MCP_20MHz_95kBPS_CFG1;
cfg2 = MCP_20MHz_95kBPS_CFG2;
cfg3 = MCP_20MHz_95kBPS_CFG3;
break;
case (CAN_BPS_100K):
cfg1 = MCP_20MHz_100kBPS_CFG1;
cfg2 = MCP_20MHz_100kBPS_CFG2;
cfg3 = MCP_20MHz_100kBPS_CFG3;
break;
case (CAN_BPS_125K):
cfg1 = MCP_20MHz_125kBPS_CFG1;
cfg2 = MCP_20MHz_125kBPS_CFG2;
cfg3 = MCP_20MHz_125kBPS_CFG3;
break;
case (CAN_BPS_200K):
cfg1 = MCP_20MHz_200kBPS_CFG1;
cfg2 = MCP_20MHz_200kBPS_CFG2;
cfg3 = MCP_20MHz_200kBPS_CFG3;
break;
case (CAN_BPS_250K):
cfg1 = MCP_20MHz_250kBPS_CFG1;
cfg2 = MCP_20MHz_250kBPS_CFG2;
cfg3 = MCP_20MHz_250kBPS_CFG3;
break;
case (CAN_BPS_500K):
cfg1 = MCP_20MHz_500kBPS_CFG1;
cfg2 = MCP_20MHz_500kBPS_CFG2;
cfg3 = MCP_20MHz_500kBPS_CFG3;
break;
case (CAN_BPS_666K):
cfg1 = MCP_20MHz_666k6BPS_CFG1;
cfg2 = MCP_20MHz_666k6BPS_CFG2;
cfg3 = MCP_20MHz_666k6BPS_CFG3;