-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDueCANLayer.cpp
More file actions
118 lines (96 loc) · 3.03 KB
/
DueCANLayer.cpp
File metadata and controls
118 lines (96 loc) · 3.03 KB
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
// ------------------------------------------------------------------------
// Due CAN Layer - Created for easier interfacing
// ------------------------------------------------------------------------
#include "DueCANLayer.h"
// ------------------------------------------------------------------------
// Initialize the CAN Controller
// ------------------------------------------------------------------------
// cPort = 0/1 - other numbers will be ignored
// lBaudRate according to due_can.h
//
// Returns CAN_OK/CAN_ERROR
//
byte canInit(byte cPort, long lBaudrate)
{
// Declarations
byte cRetCode = CAN_ERROR;
// Initialize the desired CAN port
if(cPort == 0){
cRetCode = Can0.begin(lBaudrate, ENABLE_PIN_CAN0);
Serial.print(cRetCode);
} else {
cRetCode = Can1.begin(lBaudrate, ENABLE_PIN_CAN1);
}
// Allow all filters after Init was successful
if(cRetCode != CAN_ERROR)
{
if(cPort == 0)
Can0.watchFor();
else
Can1.watchFor();
}
return cRetCode;
}// end canInit
// ------------------------------------------------------------------------
// Transmit CAN message frame
// ------------------------------------------------------------------------
// cPort = 0/1 - other numbers will be ignored
//
// Returns CAN_OK/CAN_ERROR
//
byte canTx(byte cPort, long lMsgID, bool bExtendedFormat, byte* cData, byte cDataLen)
{
// Declarations
byte cRetCode = CAN_ERROR;
CAN_FRAME frameCANMsg;
// Port the message
frameCANMsg.id = lMsgID;
frameCANMsg.extended = bExtendedFormat;
frameCANMsg.length = cDataLen;
// Copy the data, making sure there are 8 data bytes total
byte cDataCopy[8];
for(byte cIndex = 0; cIndex < 8; cIndex++)
{
if(cIndex < cDataLen)
cDataCopy[cIndex] = cData[cIndex];
else
cDataCopy[cIndex] = 0x00;
}// end for
frameCANMsg.data.low = (long)cDataCopy[0] + ((long)cDataCopy[1] << 8) + ((long)cDataCopy[2] << 16) + ((long)cDataCopy[3] << 24);
frameCANMsg.data.high = (long)cDataCopy[4] + ((long)cDataCopy[5] << 8) + ((long)cDataCopy[6] << 16) + ((long)cDataCopy[7] << 24);
// Send the message
if(cPort == 0)
cRetCode = Can0.sendFrame(frameCANMsg);
else
cRetCode = Can1.sendFrame(frameCANMsg);
return cRetCode;
}// end canTx
// ------------------------------------------------------------------------
// Receive CAN message frame
// ------------------------------------------------------------------------
// cPort = 0/1 - other numbers will be ignored
//
// Returns CAN_OK/CAN_ERROR
//
byte canRx(byte cPort, long* lMsgID, bool* bExtendedFormat, byte* cData, byte* cDataLen)
{
// Declarations
byte cLen = 0;
CAN_FRAME frameIncoming;
cLen = Can0.available();
if (cLen > 0) {
Can0.read(frameIncoming);
} else {
cLen = Can1.available();
if (cLen > 0) {
Can1.read(frameIncoming);
} else {
return CAN_ERROR;
}
}
*lMsgID = frameIncoming.id;
*bExtendedFormat = frameIncoming.extended;
memcpy(cData, frameIncoming.data.bytes, 8);
*cDataLen = frameIncoming.length;
return CAN_OK;
}// end canRx