-
Notifications
You must be signed in to change notification settings - Fork 3
/
CANOPNR_MCP2515.cpp
953 lines (828 loc) · 22.6 KB
/
CANOPNR_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
/*
This file is included as part of the CANOPNR distribution.
You are free to use this project as you see fit, provided credit is given to all
contributors (original and subsequent).
Copyright 2012 Jose Denrie Enriquez, Alex Bautista, Paloma Field, Sun-il Kim
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
------------------------------------------------------------------------------------------------------------
Based on original file: MCP2515.cpp - CAN library -- Written by Frank Kienast in November, 2010
Connections to MCP2515:
Arduino MCP2515
11 MOSI
12 MISO
13 SCK
10 CS
//////////////////////////////////////////////////////////////////////////////////////////////
*****Modified for the CANOPNR. **********
Modifications include:
-Added 7 new methods:
1. boolean MCP2515::setSleepMode()
2. boolean MCP2515::isAwake()
3. boolean MCP2515::getMSG(CANMSG *msg, unsigned long timeout, unsigned long message_addr)
4. boolean MCP2515::SNIFF_ALL(CANMSG *msg)
5. boolean MCP2515::CANSNIFF(CANMSG *msg, unsigned short address, unsigned long timeout)
6. boolean MCP2515::receiveCANMessage(CANMSG *msg, unsigned long timeout)
7. long MCP2515::queryOBD(unsigned char pid, char *buffer)
-Modified 1 existing method:
1. boolean MCP2515::setCANNormalMode()
//////////////////////////////////////////////////////////////////////////////////////////////
*/
#include "Arduino.h"
#include "SPI.h"
#include "CANOPNR_MCP2515.h"
#define SLAVESELECT 10
//MCP2515 Registers
#define RXF0SIDH 0x00
#define RXF0SIDL 0x01
#define RXF0EID8 0x02
#define RXF0EID0 0x03
#define RXF1SIDH 0x04
#define RXF1SIDL 0x05
#define RXF1EID8 0x06
#define RXF1EID0 0x07
#define RXF2SIDH 0x08
#define RXF2SIDL 0x09
#define RXF2EID8 0x0A
#define RXF2EID0 0x0B
#define BFPCTRL 0x0C
#define TXRTSCTRL 0x0D
#define CANSTAT 0x0E
#define CANCTRL 0x0F
#define RXF3SIDH 0x10
#define RXF3SIDL 0x11
#define RXF3EID8 0x12
#define RXF3EID0 0x13
#define RXF4SIDH 0x14
#define RXF4SIDL 0x15
#define RXF4EID8 0x16
#define RXF4EID0 0x17
#define RXF5SIDH 0x18
#define RXF5SIDL 0x19
#define RXF5EID8 0x1A
#define RXF5EID0 0x1B
#define TEC 0x1C
#define REC 0x1D
#define RXM0SIDH 0x20
#define RXM0SIDL 0x21
#define RXM0EID8 0x22
#define RXM0EID0 0x23
#define RXM1SIDH 0x24
#define RXM1SIDL 0x25
#define RXM1EID8 0x26
#define RXM1EID0 0x27
#define CNF3 0x28
#define CNF2 0x29
#define CNF1 0x2A
#define CANINTE 0x2B
#define MERRE 7
#define WAKIE 6
#define ERRIE 5
#define TX2IE 4
#define TX1IE 3
#define TX0IE 2
#define RX1IE 1
#define RX0IE 0
#define CANINTF 0x2C
#define MERRF 7
#define WAKIF 6
#define ERRIF 5
#define TX2IF 4
#define TX1IF 3
#define TX0IF 2
#define RX1IF 1
#define RX0IF 0
#define EFLG 0x2D
#define TXB0CTRL 0x30
#define TXREQ 3
#define TXB0SIDH 0x31
#define TXB0SIDL 0x32
#define EXIDE 3
#define TXB0EID8 0x33
#define TXB0EID0 0x34
#define TXB0DLC 0x35
#define TXRTR 7
#define TXB0D0 0x36
#define RXB0CTRL 0x60
#define RXM1 6
#define RXM0 5
#define RXRTR 3
// Bits 2:0 FILHIT2:0
#define RXB0SIDH 0x61
#define RXB0SIDL 0x62
#define RXB0EID8 0x63
#define RXB0EID0 0x64
#define RXB0DLC 0x65
#define RXB0D0 0x66
//MCP2515 Command Bytes
#define RESET 0xC0
#define READ 0x03
#define READ_RX_BUFFER 0x90
#define WRITE 0x02
#define LOAD_TX_BUFFER 0x40
#define RTS 0x80
#define READ_STATUS 0xA0
#define RX_STATUS 0xB0
#define BIT_MODIFY 0x05
boolean MCP2515::initCAN(int baudConst)
{
byte mode;
SPI.begin();
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(RESET); //Reset cmd
digitalWrite(SLAVESELECT,HIGH);
//Read mode and make sure it is config
delay(100);
mode = readReg(CANSTAT) >> 5;
if(mode != 0b100)
return false;
return(setCANBaud(baudConst));
}
boolean MCP2515::setCANBaud(int baudConst)
{
byte brp;
//BRP<5:0> = 00h, so divisor (0+1)*2 for 125ns per quantum at 16MHz for 500K
//SJW<1:0> = 00h, Sync jump width = 1
switch(baudConst)
{
case CAN_BAUD_500K: brp = 0; break;
case CAN_BAUD_250K: brp = 1; break;
case CAN_BAUD_125K: brp = 3; break;
case CAN_BAUD_100K: brp = 4; break;
default: return false;
}
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(WRITE);
SPI.transfer(CNF1);
SPI.transfer(brp & 0b00111111);
digitalWrite(SLAVESELECT,HIGH);
//PRSEG<2:0> = 0x01, 2 time quantum for prop
//PHSEG<2:0> = 0x06, 7 time constants to PS1 sample
//SAM = 0, just 1 sampling
//BTLMODE = 1, PS2 determined by CNF3
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(WRITE);
SPI.transfer(CNF2);
SPI.transfer(0b10110001);
digitalWrite(SLAVESELECT,HIGH);
//PHSEG2<2:0> = 5 for 6 time constants after sample
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(WRITE);
SPI.transfer(CNF3);
SPI.transfer(0x05);
digitalWrite(SLAVESELECT,HIGH);
//SyncSeg + PropSeg + PS1 + PS2 = 1 + 2 + 7 + 6 = 16
return true;
}
boolean MCP2515::setSleepMode(){//also need to set CANINTE.WAKIE = 1
//might need to reset CANINTF.WAKIF bit when awaken.
//REQOP<2:0> = 001
//ABAT = 0, do not abort pending transmission, or 01 to abort all pending transmit buffers
//OSM = 0, messages will reattempt transmission, message will only attempt to transmit one time
//CLKEN = 1, disable output clock
//CLKPRE = clk/8
byte mode;
writeReg(CANINTE, 0b01000000);
writeReg(CANCTRL, 0b00100000); //puts to sleep mode
//sets interrupt to awake on activity
mode = readReg(CANSTAT) >> 5; //bitwise shift to the right 5 bits
if(mode != 1)
return false;
return true;
}
boolean MCP2515::isAwake(){
byte mode;
mode = readReg(CANSTAT) >> 5;
if(mode != 1){ //if canbus is not in sleep mode
return true;
}
else{
return false; //return false is canbus is still asleep
}
}
boolean MCP2515::setCANNormalMode()
{
//REQOP2<2:0> = 000 for normal mode
//ABAT = 0, do not abort pending transmission
//OSM = 0, not one shot
//CLKEN = 1, disable output clock
//CLKPRE = 0b11, clk/8
byte mode;
writeReg(CANINTF,0b00000000);
writeReg(CANCTRL,0b00000111);
writeReg(CANINTE,0b00000000); //sets interrupt to awake on activity
//Read mode and make sure it is normal
mode = readReg(CANSTAT) >> 5;
if(mode != 0)
return false; //if CAN is not normal mode, return false
return true; //if CAN is normal mode, return true
}
boolean MCP2515::setCANReceiveonlyMode()
{
//REQOP2<2:0> = 011 for receive-only mode
//ABAT = 0, do not abort pending transmission
//OSM = 0, not one shot
//CLKEN = 1, disable output clock
//CLKPRE = 0b11, clk/8
byte mode;
writeReg(CANCTRL,0b01100111);//
//Read mode and make sure it is receive-only
mode = readReg(CANSTAT) >> 5;
if(mode != 3)
return false;
return true;
}
boolean MCP2515::getMSG(CANMSG *msg, unsigned long timeout, unsigned long message_addr){
unsigned long startTime, endTime;
boolean gotMessage;
byte val;
int i;
int y = 0; //timeout
while(msg->adrsValue != message_addr){
startTime = millis();
endTime = startTime + timeout;
gotMessage = false;
while(millis() < endTime)
{
val = readReg(CANINTF);
//If we have a message available, read it
if(bitRead(val,RX0IF) == 1)
{
gotMessage = true;
break;
}
}
if(gotMessage)
{
val = readReg(RXB0CTRL);
msg->rtr = ((bitRead(val,3) == 1) ? true : false);
//Address received from
msg->adrsValue = 0;
val = readReg(RXB0SIDH);
msg->adrsValue |= (val << 3);
val = readReg(RXB0SIDL);
msg->adrsValue |= (val >> 5);
msg->isExtendedAdrs = ((bitRead(val,EXIDE) == 1) ? true : false);
msg->extendedAdrsValue = 0;
if(msg->isExtendedAdrs)
{
msg->extendedAdrsValue = (val & 0x03) << 16;
val = readReg(RXB0EID8);
msg->extendedAdrsValue |= (val << 8);
val = readReg(RXB0EID0);
msg->extendedAdrsValue |= val;
}
//Read data bytes
val = readReg(RXB0DLC);
msg->dataLength = (val & 0xf);
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(READ);
SPI.transfer(RXB0D0);
for(i = 0; i < msg->dataLength; i++)
msg->data[i] = SPI.transfer(0);
digitalWrite(SLAVESELECT,HIGH);
//And clear read interrupt
writeRegBit(CANINTF,RX0IF,0);
}
else{//only if no message at all is received
if(y > 7){ //timeout ->this is the reason why everything is turning off
gotMessage = false;
break;
}
//break; //breaks out of while look
y++;
}
}
return gotMessage;
}
boolean MCP2515::SNIFF_ALL(CANMSG *msg){//Sniff all messages found in the CAN
byte val;
int i;
val = readReg(CANINTF);
if(bitRead(val, RX0IF) == 1){
val = readReg(RXB0CTRL);
msg->rtr = ((bitRead(val,3) == 1) ? true : false);
//Address received from
msg->adrsValue = 0;
val = readReg(RXB0SIDH);
msg->adrsValue |= (val << 3);
val = readReg(RXB0SIDL);
msg->adrsValue |= (val >> 5);
msg->isExtendedAdrs = ((bitRead(val,EXIDE) == 1) ? true : false);
msg->extendedAdrsValue = 0;
if(msg->isExtendedAdrs)
{
msg->extendedAdrsValue = (val & 0x03) << 16;
val = readReg(RXB0EID8);
msg->extendedAdrsValue |= (val << 8);
val = readReg(RXB0EID0);
msg->extendedAdrsValue |= val;
}
//Read data bytes
val = readReg(RXB0DLC);
msg->dataLength = (val & 0xf);
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(READ);
SPI.transfer(RXB0D0);
for(i = 0; i < msg->dataLength; i++)
msg->data[i] = SPI.transfer(0);
digitalWrite(SLAVESELECT,HIGH);
//And clear read interrupt
writeRegBit(CANINTF,RX0IF,0);
return true;
}
return false;
}
boolean MCP2515::CANSNIFF(CANMSG *msg, unsigned short address, unsigned long timeout){ //Sniff specific messages for further testing
unsigned long startTime, endTime;
boolean gotMessage;
byte val;
int i;
int y = 0; //timeout
while(msg->adrsValue != address){
startTime = millis();
endTime = startTime + timeout;
gotMessage = false;
while(millis() < endTime)
{
val = readReg(CANINTF);
//If we have a message available, read it
if(bitRead(val,RX0IF) == 1)
{
gotMessage = true;
break;
}
}
if(gotMessage)
{
val = readReg(RXB0CTRL);
msg->rtr = ((bitRead(val,3) == 1) ? true : false);
//Address received from
msg->adrsValue = 0;
val = readReg(RXB0SIDH);
msg->adrsValue |= (val << 3);
val = readReg(RXB0SIDL);
msg->adrsValue |= (val >> 5);
msg->isExtendedAdrs = ((bitRead(val,EXIDE) == 1) ? true : false);
msg->extendedAdrsValue = 0;
if(msg->isExtendedAdrs)
{
msg->extendedAdrsValue = (val & 0x03) << 16;
val = readReg(RXB0EID8);
msg->extendedAdrsValue |= (val << 8);
val = readReg(RXB0EID0);
msg->extendedAdrsValue |= val;
}
//Read data bytes
val = readReg(RXB0DLC);
msg->dataLength = (val & 0xf);
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(READ);
SPI.transfer(RXB0D0);
for(i = 0; i < msg->dataLength; i++)
msg->data[i] = SPI.transfer(0);
digitalWrite(SLAVESELECT,HIGH);
//And clear read interrupt
writeRegBit(CANINTF,RX0IF,0);
}
}
return gotMessage;
}
boolean MCP2515::receiveCANMessage(CANMSG *msg, unsigned long timeout)
{
unsigned long startTime, endTime;
boolean gotMessage;
byte val;
int i;
startTime = millis();
endTime = startTime + timeout;
gotMessage = false;
while(millis() < endTime)
{
val = readReg(CANINTF);
//If we have a message available, read it
if(bitRead(val,RX0IF) == 1)
{
gotMessage = true;
break;
}
}
if(gotMessage)
{
val = readReg(RXB0CTRL);
msg->rtr = ((bitRead(val,3) == 1) ? true : false);
//Address received from
msg->adrsValue = 0;
val = readReg(RXB0SIDH);
msg->adrsValue |= (val << 3);
val = readReg(RXB0SIDL);
msg->adrsValue |= (val >> 5);
msg->isExtendedAdrs = ((bitRead(val,EXIDE) == 1) ? true : false);
msg->extendedAdrsValue = 0;
if(msg->isExtendedAdrs)
{
msg->extendedAdrsValue = (val & 0x03) << 16;
val = readReg(RXB0EID8);
msg->extendedAdrsValue |= (val << 8);
val = readReg(RXB0EID0);
msg->extendedAdrsValue |= val;
}
//Read data bytes
val = readReg(RXB0DLC);
msg->dataLength = (val & 0xf);
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(READ);
SPI.transfer(RXB0D0);
for(i = 0; i < msg->dataLength; i++)
msg->data[i] = SPI.transfer(0);
digitalWrite(SLAVESELECT,HIGH);
//And clear read interrupt
writeRegBit(CANINTF,RX0IF,0);
}
return gotMessage;
}
boolean MCP2515::transmitCANMessage(CANMSG msg, unsigned long timeout)
{
unsigned long startTime, endTime;
boolean sentMessage;
unsigned short val;
int i;
startTime = millis();
endTime = startTime + timeout;
sentMessage = false;
val = msg.adrsValue >> 3;
writeReg(TXB0SIDH,val);
val = msg.adrsValue << 5;
if(msg.isExtendedAdrs)
val |= 1 << EXIDE;
writeReg(TXB0SIDL,val);
if(msg.isExtendedAdrs)
{
val = msg.extendedAdrsValue >> 8;
writeReg(TXB0EID8,val);
val = msg.extendedAdrsValue;
writeReg(TXB0EID0,val);
}
val = msg.dataLength & 0x0f;
if(msg.rtr)
bitWrite(val,TXRTR,1);
writeReg(TXB0DLC,val);
//Message bytes
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(WRITE);
SPI.transfer(TXB0D0);
for(i = 0; i < msg.dataLength; i++)
SPI.transfer(msg.data[i]);
digitalWrite(SLAVESELECT,HIGH);
//Transmit the message
writeRegBit(TXB0CTRL,TXREQ,1);
sentMessage = false;
while(millis() < endTime)
{
val = readReg(CANINTF);
if(bitRead(val,TX0IF) == 1)
{
sentMessage = true;
break;
}
}
//Abort the send if failed
writeRegBit(TXB0CTRL,TXREQ,0);
//And clear write interrupt
writeRegBit(CANINTF,TX0IF,0);
return sentMessage;
}
byte MCP2515::getCANTxErrCnt()
{
return(readReg(TEC));
}
byte MCP2515::getCANRxErrCnt()
{
return(readReg(REC));
}
void MCP2515::writeReg(byte regno, byte val)
{
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(WRITE);
SPI.transfer(regno);
SPI.transfer(val);
digitalWrite(SLAVESELECT,HIGH);
}
void MCP2515::writeRegBit(byte regno, byte bitno, byte val)
{
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(BIT_MODIFY);
SPI.transfer(regno);
SPI.transfer(1 << bitno);
if(val != 0)
SPI.transfer(0xff);
else
SPI.transfer(0x00);
digitalWrite(SLAVESELECT,HIGH);
}
byte MCP2515::readReg(byte regno)
{
byte val;
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(READ);
SPI.transfer(regno);
val = SPI.transfer(0);
digitalWrite(SLAVESELECT,HIGH);
return val;
}
long MCP2515::queryOBD(unsigned char pid, char *buffer)
{
CANMSG msg;
//long val;
float engine_data;
boolean rxSuccess;
int noMatch;
msg.adrsValue = 0x7Df;
msg.isExtendedAdrs = false;
msg.extendedAdrsValue = 0;
msg.rtr = false;
msg.dataLength = 8;
msg.data[0] = 0x02;
msg.data[1] = 0x01;
msg.data[2] = pid;
msg.data[3] = 0;
msg.data[4] = 0;
msg.data[5] = 0;
msg.data[6] = 0;
msg.data[7] = 0;
if(!transmitCANMessage(msg,300))
return 0;
rxSuccess = receiveCANMessage(&msg,300);
if (rxSuccess)
{
//Check if the PIDs match (in case other messages are also on bus)
noMatch = 0;
while(msg.data[2] != pid)
{
rxSuccess = receiveCANMessage(&msg,300);
noMatch++;
if (!rxSuccess || noMatch >= 5)
{
return 0; // if we dont recv a message / query 5 fails
}
}
}
else{ //if message is not recieved return 0;
return 0;
}
//if(msg.data[0] == 3) // runt time code?
// val = msg.data[3];
//else
// val = 256 * msg.data[3] + msg.data[4];
///inefficent double double error check lol
if((msg.adrsValue == PID_REPLY) && (msg.data[2] == pid))
{
switch(msg.data[2])
{ /* Details from http://en.wikipedia.org/wiki/OBD-II_PIDs */
case ENGINE_COOLANT_TEMP: // A-40 [degree C]
engine_data = msg.data[3] - 40;
sprintf(buffer,"%d",(int) engine_data);
break;
case ENGINE_RPM:
engine_data = ((msg.data[3]*256) + msg.data[4])/4;
sprintf(buffer, "%d", (int)engine_data);
break;
case FUEL_LEVEL:
engine_data = ((100*msg.data[3])/255);
sprintf(buffer,"%d",(int) engine_data);
break;
case VEHICLE_SPEED: // A [km]
engine_data = msg.data[3];
sprintf(buffer,"%d",(int) engine_data);
break;
case RUN_TIME:
engine_data = ((msg.data[3]*256)+msg.data[4]);
sprintf(buffer,"%d", (int) engine_data);
break;
case INTAKE_TEMP:
engine_data = (msg.data[3]-40);
sprintf(buffer, "%d",(int) engine_data);
break;
case MAF_SENSOR: // ((256*A)+B) / 100 [g/s]
engine_data = ((msg.data[3]*256) + msg.data[4])/100;
sprintf(buffer,"%d",(int) engine_data);
break;
case O2_VOLTAGE: // A * 0.005 (B-128) * 100/128 (if B==0xFF, sensor is not used in trim calc)
engine_data = msg.data[3]*0.005;
sprintf(buffer,"%d",(int) engine_data);
break;
/* case THROTTLE: // Throttle Position
engine_data = ((((msg.data[3]*100)/255)-15)/.7);//Subaru vehicles idle at 15%, max at 85%
if(engine_data < 0){
engine_data = 0;
}
sprintf(buffer,"%d",(int) engine_data);
break;
*/
case REAL_THROTTLE:
engine_data = (msg.data[3]*100)/255;
sprintf(buffer,"%d",(int) engine_data);
break;
}
return 1;// need 0?
}
return 0;
}
/*
boolean MCP2515::ACCELERATOR(CANMSG *msg, unsigned long timeout){
unsigned long startTime, endTime;
boolean gotMessage;
byte val;
int i;
int y = 0;
while(msg->adrsValue != 0x410){
startTime = millis();
endTime = startTime + timeout;
gotMessage = false;
while(millis() < endTime)
{
val = readReg(CANINTF);
//If we have a message available, read it
if(bitRead(val,RX0IF) == 1)
{
gotMessage = true;
break;
}
}
if(gotMessage)
{
val = readReg(RXB0CTRL);
msg->rtr = ((bitRead(val,3) == 1) ? true : false);
//Address received from
msg->adrsValue = 0;
val = readReg(RXB0SIDH);
msg->adrsValue |= (val << 3);
val = readReg(RXB0SIDL);
msg->adrsValue |= (val >> 5);
msg->isExtendedAdrs = ((bitRead(val,EXIDE) == 1) ? true : false);
msg->extendedAdrsValue = 0;
if(msg->isExtendedAdrs)
{
msg->extendedAdrsValue = (val & 0x03) << 16;
val = readReg(RXB0EID8);
msg->extendedAdrsValue |= (val << 8);
val = readReg(RXB0EID0);
msg->extendedAdrsValue |= val;
}
//Read data bytes
val = readReg(RXB0DLC);
msg->dataLength = (val & 0xf);
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(READ);
SPI.transfer(RXB0D0);
for(i = 0; i < msg->dataLength; i++)
msg->data[i] = SPI.transfer(0);
digitalWrite(SLAVESELECT,HIGH);
//And clear read interrupt
writeRegBit(CANINTF,RX0IF,0);
}
else{
if(y > 7){ //timeout ->this is the reason why everything is turning off
gotMessage = false;
break;
}
//break; //breaks out of while look
y++;
}
}
return gotMessage;
}
boolean MCP2515::BRAKE_PRESSURE(CANMSG *msg, unsigned long timeout){
unsigned long startTime, endTime;
boolean gotMessage;
byte val;
int i;
int y = 0;
while(msg->adrsValue != 0x511){
startTime = millis();
endTime = startTime + timeout;
gotMessage = false;
while(millis() < endTime)
{
val = readReg(CANINTF);
//If we have a message available, read it
if(bitRead(val,RX0IF) == 1)
{
gotMessage = true;
break;
}
}
if(gotMessage)
{
val = readReg(RXB0CTRL);
msg->rtr = ((bitRead(val,3) == 1) ? true : false);
//Address received from
msg->adrsValue = 0;
val = readReg(RXB0SIDH);
msg->adrsValue |= (val << 3);
val = readReg(RXB0SIDL);
msg->adrsValue |= (val >> 5);
msg->isExtendedAdrs = ((bitRead(val,EXIDE) == 1) ? true : false);
msg->extendedAdrsValue = 0;
if(msg->isExtendedAdrs)
{
msg->extendedAdrsValue = (val & 0x03) << 16;
val = readReg(RXB0EID8);
msg->extendedAdrsValue |= (val << 8);
val = readReg(RXB0EID0);
msg->extendedAdrsValue |= val;
}
//Read data bytes
val = readReg(RXB0DLC);
msg->dataLength = (val & 0xf);
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(READ);
SPI.transfer(RXB0D0);
for(i = 0; i < msg->dataLength; i++)
msg->data[i] = SPI.transfer(0);
digitalWrite(SLAVESELECT,HIGH);
//And clear read interrupt
writeRegBit(CANINTF,RX0IF,0);
}
else{
if(y > 7){ //timeout ->this is the reason why everything is turning off
gotMessage = false;
break;
}
//break; //breaks out of while look
y++;
}
}
return gotMessage;
}
boolean MCP2515::ABSCAN(CANMSG *msg, unsigned long timeout){
unsigned long startTime, endTime;
boolean gotMessage;
byte val;
int i;
int y = 0; //timeout
while(msg->adrsValue != 0x513){
startTime = millis();
endTime = startTime + timeout;
gotMessage = false;
while(millis() < endTime)
{
val = readReg(CANINTF);
//If we have a message available, read it
if(bitRead(val,RX0IF) == 1)
{
gotMessage = true;
break;
}
}
if(gotMessage)
{
val = readReg(RXB0CTRL);
msg->rtr = ((bitRead(val,3) == 1) ? true : false);
//Address received from
msg->adrsValue = 0;
val = readReg(RXB0SIDH);
msg->adrsValue |= (val << 3);
val = readReg(RXB0SIDL);
msg->adrsValue |= (val >> 5);
msg->isExtendedAdrs = ((bitRead(val,EXIDE) == 1) ? true : false);
msg->extendedAdrsValue = 0;
if(msg->isExtendedAdrs)
{
msg->extendedAdrsValue = (val & 0x03) << 16;
val = readReg(RXB0EID8);
msg->extendedAdrsValue |= (val << 8);
val = readReg(RXB0EID0);
msg->extendedAdrsValue |= val;
}
//Read data bytes
val = readReg(RXB0DLC);
msg->dataLength = (val & 0xf);
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(READ);
SPI.transfer(RXB0D0);
for(i = 0; i < msg->dataLength; i++)
msg->data[i] = SPI.transfer(0);
digitalWrite(SLAVESELECT,HIGH);
//And clear read interrupt
writeRegBit(CANINTF,RX0IF,0);
}
else{//only if no message at all is received
if(y > 7){ //timeout ->this is the reason why everything is turning off
gotMessage = false;
break;
}
//break; //breaks out of while look
y++;
}
}
return gotMessage;
}
*/