forked from coryjfowler/MCP_CAN_lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcp_can.cpp
1367 lines (1164 loc) · 47.7 KB
/
mcp_can.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
/*
mcp_can.cpp
2012 Copyright (c) Seeed Technology Inc. All right reserved.
2017 Copyright (c) Cory J. Fowler All Rights Reserved.
Author: Loovee
Contributor: Cory J. Fowler
2017-09-25
This 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.
This 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 library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-
1301 USA
*/
#include "mcp_can.h"
#define spi_readwrite SPI->transfer
#define spi_read() spi_readwrite(0x00)
/*********************************************************************************************************
** Function name: mcp2515_reset
** Descriptions: Performs a software reset
*********************************************************************************************************/
void MCP_CAN::mcp2515_reset(void)
{
SPI->beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
MCP2515_SELECT();
spi_readwrite(MCP_RESET);
MCP2515_UNSELECT();
SPI->endTransaction();
delay(5); // If the MCP2515 was in sleep mode when the reset command was issued then we need to wait a while for it to reset properly
}
/*********************************************************************************************************
** Function name: mcp2515_readRegister
** Descriptions: Read data register
*********************************************************************************************************/
INT8U MCP_CAN::mcp2515_readRegister(const INT8U address)
{
INT8U ret;
SPI->beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
MCP2515_SELECT();
spi_readwrite(MCP_READ);
spi_readwrite(address);
ret = spi_read();
MCP2515_UNSELECT();
SPI->endTransaction();
return ret;
}
/*********************************************************************************************************
** Function name: mcp2515_readRegisterS
** Descriptions: Reads successive data registers
*********************************************************************************************************/
void MCP_CAN::mcp2515_readRegisterS(const INT8U address, INT8U values[], const INT8U n)
{
INT8U i;
SPI->beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
MCP2515_SELECT();
spi_readwrite(MCP_READ);
spi_readwrite(address);
// mcp2515 has auto-increment of address-pointer
for (i=0; i<n; i++)
values[i] = spi_read();
MCP2515_UNSELECT();
SPI->endTransaction();
}
/*********************************************************************************************************
** Function name: mcp2515_setRegister
** Descriptions: Sets data register
*********************************************************************************************************/
void MCP_CAN::mcp2515_setRegister(const INT8U address, const INT8U value)
{
SPI->beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
MCP2515_SELECT();
spi_readwrite(MCP_WRITE);
spi_readwrite(address);
spi_readwrite(value);
MCP2515_UNSELECT();
SPI->endTransaction();
}
/*********************************************************************************************************
** Function name: mcp2515_setRegisterS
** Descriptions: Sets successive data registers
*********************************************************************************************************/
void MCP_CAN::mcp2515_setRegisterS(const INT8U address, const INT8U values[], const INT8U n)
{
INT8U i;
SPI->beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
MCP2515_SELECT();
spi_readwrite(MCP_WRITE);
spi_readwrite(address);
for (i=0; i<n; i++)
spi_readwrite(values[i]);
MCP2515_UNSELECT();
SPI->endTransaction();
}
/*********************************************************************************************************
** Function name: mcp2515_modifyRegister
** Descriptions: Sets specific bits of a register
*********************************************************************************************************/
void MCP_CAN::mcp2515_modifyRegister(const INT8U address, const INT8U mask, const INT8U data)
{
SPI->beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
MCP2515_SELECT();
spi_readwrite(MCP_BITMOD);
spi_readwrite(address);
spi_readwrite(mask);
spi_readwrite(data);
MCP2515_UNSELECT();
SPI->endTransaction();
}
/*********************************************************************************************************
** Function name: mcp2515_readStatus
** Descriptions: Reads status register
*********************************************************************************************************/
INT8U MCP_CAN::mcp2515_readStatus(void)
{
INT8U i;
SPI->beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
MCP2515_SELECT();
spi_readwrite(MCP_READ_STATUS);
i = spi_read();
MCP2515_UNSELECT();
SPI->endTransaction();
return i;
}
/*********************************************************************************************************
** Function name: setSleepWakeup
** Descriptions: Enable or disable the wake up interrupt (If disabled the MCP2515 will not be woken up by CAN bus activity)
*********************************************************************************************************/
void MCP_CAN::setSleepWakeup(const INT8U enable)
{
mcp2515_modifyRegister(MCP_CANINTE, MCP_WAKIF, enable ? MCP_WAKIF : 0);
}
/*********************************************************************************************************
** Function name: setMode
** Descriptions: Sets control mode
*********************************************************************************************************/
INT8U MCP_CAN::setMode(const INT8U opMode)
{
mcpMode = opMode;
return mcp2515_setCANCTRL_Mode(mcpMode);
}
/*********************************************************************************************************
** Function name: mcp2515_setCANCTRL_Mode
** Descriptions: Set control mode
*********************************************************************************************************/
INT8U MCP_CAN::mcp2515_setCANCTRL_Mode(const INT8U newmode)
{
// If the chip is asleep and we want to change mode then a manual wake needs to be done
// This is done by setting the wake up interrupt flag
// This undocumented trick was found at https://github.com/mkleemann/can/blob/master/can_sleep_mcp2515.c
if((mcp2515_readRegister(MCP_CANSTAT) & MODE_MASK) == MCP_SLEEP && newmode != MCP_SLEEP)
{
// Make sure wake interrupt is enabled
byte wakeIntEnabled = (mcp2515_readRegister(MCP_CANINTE) & MCP_WAKIF);
if(!wakeIntEnabled)
mcp2515_modifyRegister(MCP_CANINTE, MCP_WAKIF, MCP_WAKIF);
// Set wake flag (this does the actual waking up)
mcp2515_modifyRegister(MCP_CANINTF, MCP_WAKIF, MCP_WAKIF);
// Wait for the chip to exit SLEEP and enter LISTENONLY mode.
// If the chip is not connected to a CAN bus (or the bus has no other powered nodes) it will sometimes trigger the wake interrupt as soon
// as it's put to sleep, but it will stay in SLEEP mode instead of automatically switching to LISTENONLY mode.
// In this situation the mode needs to be manually set to LISTENONLY.
if(mcp2515_requestNewMode(MCP_LISTENONLY) != MCP2515_OK)
return MCP2515_FAIL;
// Turn wake interrupt back off if it was originally off
if(!wakeIntEnabled)
mcp2515_modifyRegister(MCP_CANINTE, MCP_WAKIF, 0);
}
// Clear wake flag
mcp2515_modifyRegister(MCP_CANINTF, MCP_WAKIF, 0);
return mcp2515_requestNewMode(newmode);
}
/*********************************************************************************************************
** Function name: mcp2515_requestNewMode
** Descriptions: Set control mode
*********************************************************************************************************/
INT8U MCP_CAN::mcp2515_requestNewMode(const INT8U newmode)
{
byte startTime = millis();
// Spam new mode request and wait for the operation to complete
while(1)
{
// Request new mode
// This is inside the loop as sometimes requesting the new mode once doesn't work (usually when attempting to sleep)
mcp2515_modifyRegister(MCP_CANCTRL, MODE_MASK, newmode);
byte statReg = mcp2515_readRegister(MCP_CANSTAT);
if((statReg & MODE_MASK) == newmode) // We're now in the new mode
return MCP2515_OK;
else if((byte)(millis() - startTime) > 200) // Wait no more than 200ms for the operation to complete
return MCP2515_FAIL;
}
}
/*********************************************************************************************************
** Function name: mcp2515_configRate
** Descriptions: Set baudrate
*********************************************************************************************************/
INT8U MCP_CAN::mcp2515_configRate(const INT8U canSpeed, const INT8U canClock)
{
INT8U set, cfg1, cfg2, cfg3;
set = 1;
switch (canClock & MCP_CLOCK_SELECT)
{
case (MCP_8MHZ):
switch (canSpeed)
{
case (CAN_5KBPS): // 5KBPS
cfg1 = MCP_8MHz_5kBPS_CFG1;
cfg2 = MCP_8MHz_5kBPS_CFG2;
cfg3 = MCP_8MHz_5kBPS_CFG3;
break;
case (CAN_10KBPS): // 10KBPS
cfg1 = MCP_8MHz_10kBPS_CFG1;
cfg2 = MCP_8MHz_10kBPS_CFG2;
cfg3 = MCP_8MHz_10kBPS_CFG3;
break;
case (CAN_20KBPS): // 20KBPS
cfg1 = MCP_8MHz_20kBPS_CFG1;
cfg2 = MCP_8MHz_20kBPS_CFG2;
cfg3 = MCP_8MHz_20kBPS_CFG3;
break;
case (CAN_31K25BPS): // 31.25KBPS
cfg1 = MCP_8MHz_31k25BPS_CFG1;
cfg2 = MCP_8MHz_31k25BPS_CFG2;
cfg3 = MCP_8MHz_31k25BPS_CFG3;
break;
case (CAN_33K3BPS): // 33.33KBPS
cfg1 = MCP_8MHz_33k3BPS_CFG1;
cfg2 = MCP_8MHz_33k3BPS_CFG2;
cfg3 = MCP_8MHz_33k3BPS_CFG3;
break;
case (CAN_40KBPS): // 40Kbps
cfg1 = MCP_8MHz_40kBPS_CFG1;
cfg2 = MCP_8MHz_40kBPS_CFG2;
cfg3 = MCP_8MHz_40kBPS_CFG3;
break;
case (CAN_50KBPS): // 50Kbps
cfg1 = MCP_8MHz_50kBPS_CFG1;
cfg2 = MCP_8MHz_50kBPS_CFG2;
cfg3 = MCP_8MHz_50kBPS_CFG3;
break;
case (CAN_80KBPS): // 80Kbps
cfg1 = MCP_8MHz_80kBPS_CFG1;
cfg2 = MCP_8MHz_80kBPS_CFG2;
cfg3 = MCP_8MHz_80kBPS_CFG3;
break;
case (CAN_100KBPS): // 100Kbps
cfg1 = MCP_8MHz_100kBPS_CFG1;
cfg2 = MCP_8MHz_100kBPS_CFG2;
cfg3 = MCP_8MHz_100kBPS_CFG3;
break;
case (CAN_125KBPS): // 125Kbps
cfg1 = MCP_8MHz_125kBPS_CFG1;
cfg2 = MCP_8MHz_125kBPS_CFG2;
cfg3 = MCP_8MHz_125kBPS_CFG3;
break;
case (CAN_200KBPS): // 200Kbps
cfg1 = MCP_8MHz_200kBPS_CFG1;
cfg2 = MCP_8MHz_200kBPS_CFG2;
cfg3 = MCP_8MHz_200kBPS_CFG3;
break;
case (CAN_250KBPS): // 250Kbps
cfg1 = MCP_8MHz_250kBPS_CFG1;
cfg2 = MCP_8MHz_250kBPS_CFG2;
cfg3 = MCP_8MHz_250kBPS_CFG3;
break;
case (CAN_500KBPS): // 500Kbps
cfg1 = MCP_8MHz_500kBPS_CFG1;
cfg2 = MCP_8MHz_500kBPS_CFG2;
cfg3 = MCP_8MHz_500kBPS_CFG3;
break;
case (CAN_1000KBPS): // 1Mbps
cfg1 = MCP_8MHz_1000kBPS_CFG1;
cfg2 = MCP_8MHz_1000kBPS_CFG2;
cfg3 = MCP_8MHz_1000kBPS_CFG3;
break;
default:
set = 0;
return MCP2515_FAIL;
break;
}
break;
case (MCP_16MHZ):
switch (canSpeed)
{
case (CAN_5KBPS): // 5Kbps
cfg1 = MCP_16MHz_5kBPS_CFG1;
cfg2 = MCP_16MHz_5kBPS_CFG2;
cfg3 = MCP_16MHz_5kBPS_CFG3;
break;
case (CAN_10KBPS): // 10Kbps
cfg1 = MCP_16MHz_10kBPS_CFG1;
cfg2 = MCP_16MHz_10kBPS_CFG2;
cfg3 = MCP_16MHz_10kBPS_CFG3;
break;
case (CAN_20KBPS): // 20Kbps
cfg1 = MCP_16MHz_20kBPS_CFG1;
cfg2 = MCP_16MHz_20kBPS_CFG2;
cfg3 = MCP_16MHz_20kBPS_CFG3;
break;
case (CAN_33K3BPS): // 20Kbps
cfg1 = MCP_16MHz_33k3BPS_CFG1;
cfg2 = MCP_16MHz_33k3BPS_CFG2;
cfg3 = MCP_16MHz_33k3BPS_CFG3;
break;
case (CAN_40KBPS): // 40Kbps
cfg1 = MCP_16MHz_40kBPS_CFG1;
cfg2 = MCP_16MHz_40kBPS_CFG2;
cfg3 = MCP_16MHz_40kBPS_CFG3;
break;
case (CAN_50KBPS): // 50Kbps
cfg2 = MCP_16MHz_50kBPS_CFG2;
cfg3 = MCP_16MHz_50kBPS_CFG3;
break;
case (CAN_80KBPS): // 80Kbps
cfg1 = MCP_16MHz_80kBPS_CFG1;
cfg2 = MCP_16MHz_80kBPS_CFG2;
cfg3 = MCP_16MHz_80kBPS_CFG3;
break;
case (CAN_100KBPS): // 100Kbps
cfg1 = MCP_16MHz_100kBPS_CFG1;
cfg2 = MCP_16MHz_100kBPS_CFG2;
cfg3 = MCP_16MHz_100kBPS_CFG3;
break;
case (CAN_125KBPS): // 125Kbps
cfg1 = MCP_16MHz_125kBPS_CFG1;
cfg2 = MCP_16MHz_125kBPS_CFG2;
cfg3 = MCP_16MHz_125kBPS_CFG3;
break;
case (CAN_200KBPS): // 200Kbps
cfg1 = MCP_16MHz_200kBPS_CFG1;
cfg2 = MCP_16MHz_200kBPS_CFG2;
cfg3 = MCP_16MHz_200kBPS_CFG3;
break;
case (CAN_250KBPS): // 250Kbps
cfg1 = MCP_16MHz_250kBPS_CFG1;
cfg2 = MCP_16MHz_250kBPS_CFG2;
cfg3 = MCP_16MHz_250kBPS_CFG3;
break;
case (CAN_500KBPS): // 500Kbps
cfg1 = MCP_16MHz_500kBPS_CFG1;
cfg2 = MCP_16MHz_500kBPS_CFG2;
cfg3 = MCP_16MHz_500kBPS_CFG3;
break;
case (CAN_1000KBPS): // 1Mbps
cfg1 = MCP_16MHz_1000kBPS_CFG1;
cfg2 = MCP_16MHz_1000kBPS_CFG2;
cfg3 = MCP_16MHz_1000kBPS_CFG3;
break;
default:
set = 0;
return MCP2515_FAIL;
break;
}
break;
case (MCP_20MHZ):
switch (canSpeed)
{
case (CAN_40KBPS): // 40Kbps
cfg1 = MCP_20MHz_40kBPS_CFG1;
cfg2 = MCP_20MHz_40kBPS_CFG2;
cfg3 = MCP_20MHz_40kBPS_CFG3;
break;
case (CAN_50KBPS): // 50Kbps
cfg1 = MCP_20MHz_50kBPS_CFG1;
cfg2 = MCP_20MHz_50kBPS_CFG2;
cfg3 = MCP_20MHz_50kBPS_CFG3;
break;
case (CAN_80KBPS): // 80Kbps
cfg1 = MCP_20MHz_80kBPS_CFG1;
cfg2 = MCP_20MHz_80kBPS_CFG2;
cfg3 = MCP_20MHz_80kBPS_CFG3;
break;
case (CAN_100KBPS): // 100Kbps
cfg1 = MCP_20MHz_100kBPS_CFG1;
cfg2 = MCP_20MHz_100kBPS_CFG2;
cfg3 = MCP_20MHz_100kBPS_CFG3;
break;
case (CAN_125KBPS): // 125Kbps
cfg1 = MCP_20MHz_125kBPS_CFG1;
cfg2 = MCP_20MHz_125kBPS_CFG2;
cfg3 = MCP_20MHz_125kBPS_CFG3;
break;
case (CAN_200KBPS): // 200Kbps
cfg1 = MCP_20MHz_200kBPS_CFG1;
cfg2 = MCP_20MHz_200kBPS_CFG2;
cfg3 = MCP_20MHz_200kBPS_CFG3;
break;
case (CAN_250KBPS): // 250Kbps
cfg1 = MCP_20MHz_250kBPS_CFG1;
cfg2 = MCP_20MHz_250kBPS_CFG2;
cfg3 = MCP_20MHz_250kBPS_CFG3;
break;
case (CAN_500KBPS): // 500Kbps
cfg1 = MCP_20MHz_500kBPS_CFG1;
cfg2 = MCP_20MHz_500kBPS_CFG2;
cfg3 = MCP_20MHz_500kBPS_CFG3;
break;
case (CAN_1000KBPS): // 1Mbps
cfg1 = MCP_20MHz_1000kBPS_CFG1;
cfg2 = MCP_20MHz_1000kBPS_CFG2;
cfg3 = MCP_20MHz_1000kBPS_CFG3;
break;
default:
set = 0;
return MCP2515_FAIL;
break;
}
break;
default:
set = 0;
return MCP2515_FAIL;
break;
}
if (canClock & MCP_CLKOUT_ENABLE) {
cfg3 &= (~SOF_ENABLE);
}
if (set) {
mcp2515_setRegister(MCP_CNF1, cfg1);
mcp2515_setRegister(MCP_CNF2, cfg2);
mcp2515_setRegister(MCP_CNF3, cfg3);
return MCP2515_OK;
}
return MCP2515_FAIL;
}
/*********************************************************************************************************
** Function name: mcp2515_initCANBuffers
** Descriptions: Initialize Buffers, Masks, and Filters
*********************************************************************************************************/
void MCP_CAN::mcp2515_initCANBuffers(void)
{
INT8U i, a1, a2, a3;
INT8U std = 0;
INT8U ext = 1;
INT32U ulMask = 0x00, ulFilt = 0x00;
mcp2515_write_mf(MCP_RXM0SIDH, ext, ulMask); /*Set both masks to 0 */
mcp2515_write_mf(MCP_RXM1SIDH, ext, ulMask); /*Mask register ignores ext bit */
/* Set all filters to 0 */
mcp2515_write_mf(MCP_RXF0SIDH, ext, ulFilt); /* RXB0: extended */
mcp2515_write_mf(MCP_RXF1SIDH, std, ulFilt); /* RXB1: standard */
mcp2515_write_mf(MCP_RXF2SIDH, ext, ulFilt); /* RXB2: extended */
mcp2515_write_mf(MCP_RXF3SIDH, std, ulFilt); /* RXB3: standard */
mcp2515_write_mf(MCP_RXF4SIDH, ext, ulFilt);
mcp2515_write_mf(MCP_RXF5SIDH, std, ulFilt);
/* Clear, deactivate the three */
/* transmit buffers */
/* TXBnCTRL -> TXBnD7 */
a1 = MCP_TXB0CTRL;
a2 = MCP_TXB1CTRL;
a3 = MCP_TXB2CTRL;
for (i = 0; i < 14; i++) { /* in-buffer loop */
mcp2515_setRegister(a1, 0);
mcp2515_setRegister(a2, 0);
mcp2515_setRegister(a3, 0);
a1++;
a2++;
a3++;
}
mcp2515_setRegister(MCP_RXB0CTRL, 0);
mcp2515_setRegister(MCP_RXB1CTRL, 0);
}
/*********************************************************************************************************
** Function name: mcp2515_init
** Descriptions: Initialize the controller
*********************************************************************************************************/
INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const INT8U canClock)
{
INT8U res;
mcp2515_reset();
mcpMode = MCP_LOOPBACK;
res = mcp2515_setCANCTRL_Mode(MODE_CONFIG);
if(res > 0)
{
#if DEBUG_MODE
Serial.println(F("Entering Configuration Mode Failure..."));
#endif
return res;
}
#if DEBUG_MODE
Serial.println(F("Entering Configuration Mode Successful!"));
#endif
// Set Baudrate
if(mcp2515_configRate(canSpeed, canClock))
{
#if DEBUG_MODE
Serial.println(F("Setting Baudrate Failure..."));
#endif
return res;
}
#if DEBUG_MODE
Serial.println(F("Setting Baudrate Successful!"));
#endif
if ( res == MCP2515_OK ) {
/* init canbuffers */
mcp2515_initCANBuffers();
/* interrupt mode */
mcp2515_setRegister(MCP_CANINTE, MCP_RX0IF | MCP_RX1IF);
//Sets BF pins as GPO
mcp2515_setRegister(MCP_BFPCTRL,MCP_BxBFS_MASK | MCP_BxBFE_MASK);
//Sets RTS pins as GPI
mcp2515_setRegister(MCP_TXRTSCTRL,0x00);
switch(canIDMode)
{
case (MCP_ANY):
mcp2515_modifyRegister(MCP_RXB0CTRL,
MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK,
MCP_RXB_RX_ANY | MCP_RXB_BUKT_MASK);
mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK,
MCP_RXB_RX_ANY);
break;
/* The followingn two functions of the MCP2515 do not work, there is a bug in the silicon.
case (MCP_STD):
mcp2515_modifyRegister(MCP_RXB0CTRL,
MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK,
MCP_RXB_RX_STD | MCP_RXB_BUKT_MASK );
mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK,
MCP_RXB_RX_STD);
break;
case (MCP_EXT):
mcp2515_modifyRegister(MCP_RXB0CTRL,
MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK,
MCP_RXB_RX_EXT | MCP_RXB_BUKT_MASK );
mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK,
MCP_RXB_RX_EXT);
break;
*/
case (MCP_STDEXT):
mcp2515_modifyRegister(MCP_RXB0CTRL,
MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK,
MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK );
mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK,
MCP_RXB_RX_STDEXT);
break;
default:
#if DEBUG_MODE
Serial.println(F("`Setting ID Mode Failure..."));
#endif
return MCP2515_FAIL;
break;
}
res = mcp2515_setCANCTRL_Mode(mcpMode);
if(res)
{
#if DEBUG_MODE
Serial.println(F("Returning to Previous Mode Failure..."));
#endif
return res;
}
}
return res;
}
/*********************************************************************************************************
** Function name: mcp2515_write_id
** Descriptions: Write CAN ID
*********************************************************************************************************/
void MCP_CAN::mcp2515_write_id( const INT8U mcp_addr, const INT8U ext, const INT32U id )
{
uint16_t canid;
INT8U tbufdata[4];
canid = (uint16_t)(id & 0x0FFFF);
if ( ext == 1)
{
tbufdata[MCP_EID0] = (INT8U) (canid & 0xFF);
tbufdata[MCP_EID8] = (INT8U) (canid >> 8);
canid = (uint16_t)(id >> 16);
tbufdata[MCP_SIDL] = (INT8U) (canid & 0x03);
tbufdata[MCP_SIDL] += (INT8U) ((canid & 0x1C) << 3);
tbufdata[MCP_SIDL] |= MCP_TXB_EXIDE_M;
tbufdata[MCP_SIDH] = (INT8U) (canid >> 5 );
}
else
{
tbufdata[MCP_SIDH] = (INT8U) (canid >> 3 );
tbufdata[MCP_SIDL] = (INT8U) ((canid & 0x07 ) << 5);
tbufdata[MCP_EID0] = 0;
tbufdata[MCP_EID8] = 0;
}
mcp2515_setRegisterS( mcp_addr, tbufdata, 4 );
}
/*********************************************************************************************************
** Function name: mcp2515_write_mf
** Descriptions: Write Masks and Filters
*********************************************************************************************************/
void MCP_CAN::mcp2515_write_mf( const INT8U mcp_addr, const INT8U ext, const INT32U id )
{
uint16_t canid;
INT8U tbufdata[4];
canid = (uint16_t)(id & 0x0FFFF);
if ( ext == 1)
{
tbufdata[MCP_EID0] = (INT8U) (canid & 0xFF);
tbufdata[MCP_EID8] = (INT8U) (canid >> 8);
canid = (uint16_t)(id >> 16);
tbufdata[MCP_SIDL] = (INT8U) (canid & 0x03);
tbufdata[MCP_SIDL] += (INT8U) ((canid & 0x1C) << 3);
tbufdata[MCP_SIDL] |= MCP_TXB_EXIDE_M;
tbufdata[MCP_SIDH] = (INT8U) (canid >> 5 );
}
else
{
tbufdata[MCP_EID0] = (INT8U) (canid & 0xFF);
tbufdata[MCP_EID8] = (INT8U) (canid >> 8);
canid = (uint16_t)(id >> 16);
tbufdata[MCP_SIDL] = (INT8U) ((canid & 0x07) << 5);
tbufdata[MCP_SIDH] = (INT8U) (canid >> 3 );
}
mcp2515_setRegisterS( mcp_addr, tbufdata, 4 );
}
/*********************************************************************************************************
** Function name: mcp2515_read_id
** Descriptions: Read CAN ID
*********************************************************************************************************/
void MCP_CAN::mcp2515_read_id( const INT8U mcp_addr, INT8U* ext, INT32U* id )
{
INT8U tbufdata[4];
*ext = 0;
*id = 0;
mcp2515_readRegisterS( mcp_addr, tbufdata, 4 );
*id = (tbufdata[MCP_SIDH]<<3) + (tbufdata[MCP_SIDL]>>5);
if ( (tbufdata[MCP_SIDL] & MCP_TXB_EXIDE_M) == MCP_TXB_EXIDE_M )
{
/* extended id */
*id = (*id<<2) + (tbufdata[MCP_SIDL] & 0x03);
*id = (*id<<8) + tbufdata[MCP_EID8];
*id = (*id<<8) + tbufdata[MCP_EID0];
*ext = 1;
}
}
/*********************************************************************************************************
** Function name: mcp2515_write_canMsg
** Descriptions: Write message
*********************************************************************************************************/
void MCP_CAN::mcp2515_write_canMsg( const INT8U buffer_sidh_addr)
{
INT8U mcp_addr;
mcp_addr = buffer_sidh_addr;
mcp2515_setRegisterS(mcp_addr+5, m_nDta, m_nDlc ); /* write data bytes */
if ( m_nRtr == 1) /* if RTR set bit in byte */
m_nDlc |= MCP_RTR_MASK;
mcp2515_setRegister((mcp_addr+4), m_nDlc ); /* write the RTR and DLC */
mcp2515_write_id(mcp_addr, m_nExtFlg, m_nID ); /* write CAN id */
}
/*********************************************************************************************************
** Function name: mcp2515_read_canMsg
** Descriptions: Read message
*********************************************************************************************************/
void MCP_CAN::mcp2515_read_canMsg( const INT8U buffer_sidh_addr) /* read can msg */
{
INT8U mcp_addr, ctrl;
mcp_addr = buffer_sidh_addr;
mcp2515_read_id( mcp_addr, &m_nExtFlg,&m_nID );
ctrl = mcp2515_readRegister( mcp_addr-1 );
m_nDlc = mcp2515_readRegister( mcp_addr+4 );
if (ctrl & 0x08)
m_nRtr = 1;
else
m_nRtr = 0;
m_nDlc &= MCP_DLC_MASK;
mcp2515_readRegisterS( mcp_addr+5, &(m_nDta[0]), m_nDlc );
}
/*********************************************************************************************************
** Function name: mcp2515_getNextFreeTXBuf
** Descriptions: Send message
*********************************************************************************************************/
INT8U MCP_CAN::mcp2515_getNextFreeTXBuf(INT8U *txbuf_n) /* get Next free txbuf */
{
INT8U res, i, ctrlval;
INT8U ctrlregs[MCP_N_TXBUFFERS] = { MCP_TXB0CTRL, MCP_TXB1CTRL, MCP_TXB2CTRL };
res = MCP_ALLTXBUSY;
*txbuf_n = 0x00;
/* check all 3 TX-Buffers */
for (i=0; i<MCP_N_TXBUFFERS; i++) {
ctrlval = mcp2515_readRegister( ctrlregs[i] );
if ( (ctrlval & MCP_TXB_TXREQ_M) == 0 ) {
*txbuf_n = ctrlregs[i]+1; /* return SIDH-address of Buffer*/
res = MCP2515_OK;
return res; /* ! function exit */
}
}
return res;
}
/*********************************************************************************************************
** Function name: MCP_CAN
** Descriptions: Public function to declare CAN class and the /CS pin.
*********************************************************************************************************/
MCP_CAN::MCP_CAN(INT8U _CS)
{
MCPCS = _CS;
MCP2515_UNSELECT();
pinMode(MCPCS, OUTPUT);
SPI = SPI;
}
/*********************************************************************************************************
** Function name: MCP_CAN
** Descriptions: Public function to declare CAN class with SPI and the /CS pin.
*********************************************************************************************************/
MCP_CAN::MCP_CAN(SPIClass *_SPI, INT8U _CS)
{
MCPCS = _CS;
MCP2515_UNSELECT();
pinMode(MCPCS, OUTPUT);
SPI = _SPI;
}
/*********************************************************************************************************
** Function name: begin
** Descriptions: Public function to declare controller initialization parameters.
*********************************************************************************************************/
INT8U MCP_CAN::begin(INT8U idmodeset, INT8U speedset, INT8U clockset)
{
INT8U res;
SPI->begin();
res = mcp2515_init(idmodeset, speedset, clockset);
if (res == MCP2515_OK)
return CAN_OK;
return CAN_FAILINIT;
}
/*********************************************************************************************************
** Function name: init_Mask
** Descriptions: Public function to set mask(s).
*********************************************************************************************************/
INT8U MCP_CAN::init_Mask(INT8U num, INT8U ext, INT32U ulData)
{
INT8U res = MCP2515_OK;
#if DEBUG_MODE
Serial.println(F("Starting to Set Mask!"));
#endif
res = mcp2515_setCANCTRL_Mode(MODE_CONFIG);
if(res > 0){
#if DEBUG_MODE
Serial.println(F("Entering Configuration Mode Failure..."));
#endif
return res;
}
if (num == 0){
mcp2515_write_mf(MCP_RXM0SIDH, ext, ulData);
}
else if(num == 1){
mcp2515_write_mf(MCP_RXM1SIDH, ext, ulData);
}
else res = MCP2515_FAIL;
res = mcp2515_setCANCTRL_Mode(mcpMode);
if(res > 0){
#if DEBUG_MODE
Serial.println(F("Entering Previous Mode Failure..."));
Serial.println(F("Setting Mask Failure..."));
#endif
return res;
}
#if DEBUG_MODE
Serial.println(F("Setting Mask Successful!"));
#endif
return res;
}
/*********************************************************************************************************
** Function name: init_Mask
** Descriptions: Public function to set mask(s).
*********************************************************************************************************/
INT8U MCP_CAN::init_Mask(INT8U num, INT32U ulData)
{
INT8U res = MCP2515_OK;
INT8U ext = 0;
#if DEBUG_MODE
Serial.println(F("Starting to Set Mask!"));
#endif
res = mcp2515_setCANCTRL_Mode(MODE_CONFIG);
if(res > 0){
#if DEBUG_MODE
Serial.println(F("Entering Configuration Mode Failure..."));
#endif
return res;
}
if((ulData & 0x80000000) == 0x80000000)
ext = 1;
if (num == 0){
mcp2515_write_mf(MCP_RXM0SIDH, ext, ulData);
}
else if(num == 1){
mcp2515_write_mf(MCP_RXM1SIDH, ext, ulData);
}
else res = MCP2515_FAIL;
res = mcp2515_setCANCTRL_Mode(mcpMode);
if(res > 0){
#if DEBUG_MODE
Serial.println(F("Entering Previous Mode Failure..."));
Serial.println(F("Setting Mask Failure..."));
#endif
return res;
}
#if DEBUG_MODE
Serial.println(F("Setting Mask Successful!"));
#endif
return res;
}
/*********************************************************************************************************
** Function name: init_Filt
** Descriptions: Public function to set filter(s).
*********************************************************************************************************/
INT8U MCP_CAN::init_Filt(INT8U num, INT8U ext, INT32U ulData)
{
INT8U res = MCP2515_OK;
#if DEBUG_MODE
Serial.println(F("Starting to Set Filter!"));
#endif
res = mcp2515_setCANCTRL_Mode(MODE_CONFIG);
if(res > 0)
{
#if DEBUG_MODE
Serial.println(F("Enter Configuration Mode Failure..."));
#endif
return res;
}
switch( num )
{
case 0:
mcp2515_write_mf(MCP_RXF0SIDH, ext, ulData);
break;
case 1:
mcp2515_write_mf(MCP_RXF1SIDH, ext, ulData);
break;
case 2:
mcp2515_write_mf(MCP_RXF2SIDH, ext, ulData);
break;
case 3:
mcp2515_write_mf(MCP_RXF3SIDH, ext, ulData);
break;
case 4:
mcp2515_write_mf(MCP_RXF4SIDH, ext, ulData);
break;
case 5:
mcp2515_write_mf(MCP_RXF5SIDH, ext, ulData);
break;
default:
res = MCP2515_FAIL;
}
res = mcp2515_setCANCTRL_Mode(mcpMode);
if(res > 0)
{
#if DEBUG_MODE
Serial.println(F("Entering Previous Mode Failure..."));
Serial.println(F("Setting Filter Failure..."));
#endif
return res;
}
#if DEBUG_MODE
Serial.println(F("Setting Filter Successful!"));
#endif
return res;
}
/*********************************************************************************************************