-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathI2C_CAN_Firmware.ino
677 lines (541 loc) · 17.2 KB
/
I2C_CAN_Firmware.ino
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
/* send a frame from can bus
CAN Baudrate,
#define CAN_5KBPS 1
#define CAN_10KBPS 2
#define CAN_20KBPS 3
#define CAN_25KBPS 4
#define CAN_31K25BPS 5
#define CAN_33KBPS 6
#define CAN_40KBPS 7
#define CAN_50KBPS 8
#define CAN_80KBPS 9
#define CAN_83K3BPS 10
#define CAN_95KBPS 11
#define CAN_100KBPS 12
#define CAN_125KBPS 13
#define CAN_200KBPS 14
#define CAN_250KBPS 15
#define CAN_500KBPS 16
#define CAN_666KBPS 17
#define CAN_1000KBPS 18
*/
#include <mcp_can.h>
#include <SPI.h>
#include <Wire.h>
#include <EEPROM.h>
#include "I2C_CAN_dfs.h"
#define MAX_RECV_CAN_LEN 8 // BUF FOR CAN FRAME RECVING
#define EEPADDR_TESTMODE 4
unsigned char can_frame_dta[MAX_RECV_CAN_LEN][16];
int cnt_can_frame_dta = 0;
int index_can_frame_dta = 0;
int index_can_frame_read = 0;
const int pinLed = 3; // 3
const int SPI_CS_PIN = 9; // CAN Bus Shield
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
#define LEDON() digitalWrite(pinLed, HIGH)
#define LEDOFF() digitalWrite(pinLed, LOW)
#define LEDFUN() digitalWrite(pinLed, 1-digitalRead(pinLed))
unsigned char i2c_get_len = 0;
unsigned char i2c_dta[20];
unsigned char flg_i2c_get = 0;
unsigned char reg_read = 0;
unsigned char makeCheckSum(unsigned char *dta, int len)
{
unsigned long sum = 0;
for(int i=0; i<len; i++)sum += dta[i];
if(sum > 0xff)
{
sum = ~sum;
sum += 1;
}
sum = sum & 0xff;
return sum;
}
void blinkTestMode()
{
static unsigned long timer_s = millis();
if(millis()-timer_s < 100)return;
timer_s = millis();
LEDFUN();
}
void testModeCANSend()
{
static unsigned long timer_s = millis();
if(millis()-timer_s < 500)return;
timer_s = millis();
unsigned char stmp[8] = {0x55, 0xff, 0x55, 0xaa, 0x0f, 0x5a, 0xfa, 0xf5};
CAN.sendMsgBuf(0x00, 0, 8, stmp);
}
void testMode()
{
if(EEPROM.read(EEPADDR_TESTMODE) == 0xAB)return;
pinMode(3, OUTPUT);
//Serial.begin(9600);
pinMode(A4, INPUT);
pinMode(A5, OUTPUT);
int cnt = 0;
PINTEST:
unsigned char flgPinTest = 1; // 0 fail, 1 ok
unsigned long timer_s = millis();
unsigned char str[10];
unsigned char len = 0;
digitalWrite(A5, HIGH);
delay(10);
if(HIGH != digitalRead(A4)) // test ok
{
flgPinTest = 0;
}
digitalWrite(A5, LOW);
delay(10);
if(LOW != digitalRead(A4)) // test ok
{
flgPinTest = 0;
}
if(!flgPinTest) // pin test fail
{
EEPROM.write(EEPADDR_TESTMODE, 0);
while(1)
{
LEDON();
delay(50);
LEDOFF();
delay(200);
LEDON();
delay(50);
LEDOFF();
delay(700);
}
}
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
delay(500);
LEDFUN();
}
EEPROM.write(EEPADDR_TESTMODE, 0xAB);
while(1)
{
delay(50);
LEDFUN();
testModeCANSend();
}
//Serial.begin(9600);
}
void setup()
{
pinMode(3, OUTPUT);
testMode();
for(int i=0; i<20; i++)
{
LEDFUN();
delay(20);
}
int __baud = EEPROM.read(REG_BAUD);
if(__baud == 0 || __baud > 18) // not setting baud rate
{
__baud = 16;
}
if(0x5a != EEPROM.read(REG_ADDR_SET)) // if not 0x5a, no set yet, set to default
{
EEPROM.write(REG_ADDR_SET, 0x5a);
EEPROM.write(REG_ADDR, DEFAULT_I2C_ADDR);
}
Wire.begin(EEPROM.read(REG_ADDR)); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Wire.onRequest(requestEvent); // register event
while (CAN_OK != CAN.begin(__baud)) // init can bus : baudrate = 500k
{
delay(100);
LEDFUN();
}
LEDON();
WD_SET(WD_RST, WDTO_1S);
}
void loop()
{
taskCANRecv();
WDR();
if(flg_i2c_get)
{
flg_i2c_get = 0;
if(i2c_get_len > 0)
switch(i2c_dta[0])
{
//***********************SEND A FRAME***********************************
case REG_SEND: // send
if(17 == i2c_get_len)
{
unsigned long id = 0;
id = i2c_dta[1];
id <<= 8;
id += i2c_dta[2];
id <<= 8;
id += i2c_dta[3];
id <<= 8;
id += i2c_dta[4];
int __len = i2c_dta[7];
int __ext = i2c_dta[5];
unsigned char __checksum = makeCheckSum(&i2c_dta[1], 15);
if(__checksum == i2c_dta[16]) // check sum ok
{
if(__len <= 8)
{
CAN.sendMsgBuf(id, __ext, __len, &i2c_dta[8]);
}
}
}
break;
//***********************SET BAUD******************************************
case REG_BAUD:
if(1 == i2c_get_len) // read
{
reg_read = REG_BAUD;
}
else if(2 == i2c_get_len) // write
{
if(i2c_dta[1] >= 1 && i2c_dta[1] <= 18)
{
EEPROM.write(REG_BAUD, i2c_dta[1]);
while (CAN_OK != CAN.begin(i2c_dta[1])) // init can bus : baudrate = 500k
{
delay(100);
}
}
}
break;
/************************GET CAN FRAME NUMBER******************************/
case REG_DNUM:
if(1 == i2c_get_len)
{
reg_read = REG_DNUM;
}
break;
//***********************SET ADDR*******************************************
case REG_ADDR:
if(2 == i2c_get_len)
{
EEPROM.write(REG_ADDR, i2c_dta[1]);
while(1);
}
break;
//***********************GET CAN FRAME**************************************
case REG_RECV:
if(1 == i2c_get_len)
{
reg_read = REG_RECV;
}
break;
//***********************MASK0*********************************************
case REG_MASK0:
if(1 == i2c_get_len) // read mask0
{
reg_read = REG_MASK0;
}
else if(6 == i2c_get_len) // set mask0
{
for(int i=0; i<5; i++)
{
EEPROM.write(REG_MASK0+i, i2c_dta[1+i]);
}
unsigned long mask = i2c_dta[2];
mask <<= 8;
mask += i2c_dta[3];
mask <<= 8;
mask += i2c_dta[4];
mask <<= 8;
mask += i2c_dta[5];
CAN.init_Mask(0, i2c_dta[1], mask);
}
break;
//***********************MASK1*********************************************
case REG_MASK1:
if(1 == i2c_get_len) // read mask0
{
reg_read = REG_MASK1;
}
else if(6 == i2c_get_len) // set mask0
{
for(int i=0; i<5; i++)
{
EEPROM.write(REG_MASK1+i, i2c_dta[1+i]);
}
unsigned long mask = i2c_dta[2];
mask <<= 8;
mask += i2c_dta[3];
mask <<= 8;
mask += i2c_dta[4];
mask <<= 8;
mask += i2c_dta[5];
CAN.init_Mask(1, i2c_dta[1], mask);
}
break;
//***********************FILTER 0*********************************************
case REG_FILT0:
if(1 == i2c_get_len) // read mask0
{
reg_read = REG_FILT0;
}
else if(6 == i2c_get_len) // set mask0
{
for(int i=0; i<5; i++)
{
EEPROM.write(REG_FILT0+i, i2c_dta[1+i]);
}
unsigned long filt = i2c_dta[2];
filt <<= 8;
filt += i2c_dta[3];
filt <<= 8;
filt += i2c_dta[4];
filt <<= 8;
filt += i2c_dta[5];
CAN.init_Filt(0, i2c_dta[1], filt);
}
break;
//***********************FILTER 1*********************************************
case REG_FILT1:
if(1 == i2c_get_len) // read mask0
{
reg_read = REG_FILT1;
}
else if(6 == i2c_get_len) // set mask0
{
for(int i=0; i<5; i++)
{
EEPROM.write(REG_FILT1+i, i2c_dta[1+i]);
}
unsigned long filt = i2c_dta[2];
filt <<= 8;
filt += i2c_dta[3];
filt <<= 8;
filt += i2c_dta[4];
filt <<= 8;
filt += i2c_dta[5];
CAN.init_Filt(1, i2c_dta[1], filt);
}
break;
//***********************FILTER 2*********************************************
case REG_FILT2:
if(1 == i2c_get_len) // read mask0
{
reg_read = REG_FILT2;
}
else if(6 == i2c_get_len) // set mask0
{
for(int i=0; i<5; i++)
{
EEPROM.write(REG_FILT2+i, i2c_dta[1+i]);
}
unsigned long filt = i2c_dta[2];
filt <<= 8;
filt += i2c_dta[3];
filt <<= 8;
filt += i2c_dta[4];
filt <<= 8;
filt += i2c_dta[5];
CAN.init_Filt(2, i2c_dta[1], filt);
}
break;
//***********************FILTER 3*********************************************
case REG_FILT3:
if(1 == i2c_get_len) // read mask0
{
reg_read = REG_FILT3;
}
else if(6 == i2c_get_len) // set mask0
{
for(int i=0; i<5; i++)
{
EEPROM.write(REG_FILT3+i, i2c_dta[1+i]);
}
unsigned long filt = i2c_dta[2];
filt <<= 8;
filt += i2c_dta[3];
filt <<= 8;
filt += i2c_dta[4];
filt <<= 8;
filt += i2c_dta[5];
CAN.init_Filt(3, i2c_dta[1], filt);
}
break;
//***********************FILTER 4*********************************************
case REG_FILT4:
if(1 == i2c_get_len) // read mask0
{
reg_read = REG_FILT4;
}
else if(6 == i2c_get_len) // set mask0
{
for(int i=0; i<5; i++)
{
EEPROM.write(REG_FILT4+i, i2c_dta[1+i]);
}
unsigned long filt = i2c_dta[2];
filt <<= 8;
filt += i2c_dta[3];
filt <<= 8;
filt += i2c_dta[4];
filt <<= 8;
filt += i2c_dta[5];
CAN.init_Filt(4, i2c_dta[1], filt);
}
break;
//***********************FILTER 5*********************************************
case REG_FILT5:
if(1 == i2c_get_len) // read mask0
{
reg_read = REG_FILT5;
}
else if(6 == i2c_get_len) // set mask0
{
for(int i=0; i<5; i++)
{
EEPROM.write(REG_FILT5+i, i2c_dta[1+i]);
}
unsigned long filt = i2c_dta[2];
filt <<= 8;
filt += i2c_dta[3];
filt <<= 8;
filt += i2c_dta[4];
filt <<= 8;
filt += i2c_dta[5];
CAN.init_Filt(5, i2c_dta[1], filt);
}
break;
default:;
}
i2c_get_len = 0;
}
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
while (0 < Wire.available()) { // loop through all but the last
i2c_dta[i2c_get_len++] = Wire.read();
flg_i2c_get = 1;
}
if(i2c_get_len > 0)
{
flg_i2c_get = 1;
}
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
unsigned char __tmp[10];
switch(reg_read)
{
case REG_BAUD:
Wire.write(EEPROM.read(REG_BAUD));
break;
case REG_DNUM:
Wire.write(cnt_can_frame_dta);
break;
case REG_RECV:
if(cnt_can_frame_dta > 0)
{
Wire.write(can_frame_dta[index_can_frame_read], 16);
index_can_frame_read++;
if(index_can_frame_read >= MAX_RECV_CAN_LEN)index_can_frame_read = 0;
cnt_can_frame_dta--;
}
break;
case REG_MASK0:
for(int i=0; i<5; i++)
{
__tmp[i] = EEPROM.read(REG_MASK0+i);
}
Wire.write(__tmp, 5);
break;
case REG_MASK1:
for(int i=0; i<5; i++)
{
__tmp[i] = EEPROM.read(REG_MASK1+i);
}
Wire.write(__tmp, 5);
break;
case REG_FILT0:
for(int i=0; i<5; i++)
{
__tmp[i] = EEPROM.read(REG_FILT0+i);
}
Wire.write(__tmp, 5);
break;
case REG_FILT1:
for(int i=0; i<5; i++)
{
__tmp[i] = EEPROM.read(REG_FILT1+i);
}
Wire.write(__tmp, 5);
break;
case REG_FILT2:
for(int i=0; i<5; i++)
{
__tmp[i] = EEPROM.read(REG_FILT1+i);
}
Wire.write(__tmp, 5);
break;
case REG_FILT3:
for(int i=0; i<5; i++)
{
__tmp[i] = EEPROM.read(REG_FILT3+i);
}
Wire.write(__tmp, 5);
break;
case REG_FILT4:
for(int i=0; i<5; i++)
{
__tmp[i] = EEPROM.read(REG_FILT4+i);
}
Wire.write(__tmp, 5);
break;
case REG_FILT5:
for(int i=0; i<5; i++)
{
__tmp[i] = EEPROM.read(REG_FILT5+i);
}
Wire.write(__tmp, 5);
break;
default:;
}
}
/*
unsigned char can_frame_dta[MAX_RECV_CAN_LEN][16];
int cnt_can_frame_dta = 0;
int index_can_frame_dta = 0;
*/
void taskCANRecv()
{
unsigned char len = 0;
unsigned char buf[8];
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned long canId = CAN.getCanId();
if(cnt_can_frame_dta < MAX_RECV_CAN_LEN)
{
cnt_can_frame_dta++;
}
else
{
index_can_frame_read++;
if(index_can_frame_read >= MAX_RECV_CAN_LEN)index_can_frame_read = 0;
}
can_frame_dta[index_can_frame_dta][0] = (canId>>24)&0xff;
can_frame_dta[index_can_frame_dta][1] = (canId>>16)&0xff;
can_frame_dta[index_can_frame_dta][2] = (canId>>8)&0xff;
can_frame_dta[index_can_frame_dta][3] = (canId>>0)&0xff;
can_frame_dta[index_can_frame_dta][4] = CAN.isExtendedFrame(); // extend frame?
can_frame_dta[index_can_frame_dta][5] = CAN.isRemoteRequest(); // remote frame?
can_frame_dta[index_can_frame_dta][6] = len;
for(int i=0; i<len; i++)
{
can_frame_dta[index_can_frame_dta][7+i] = buf[i];
}
can_frame_dta[index_can_frame_dta][15] = makeCheckSum(&can_frame_dta[index_can_frame_dta][0], 15);
index_can_frame_dta++;
if(index_can_frame_dta >= (MAX_RECV_CAN_LEN)) index_can_frame_dta = 0;
}
}
// END FILE