Skip to content

Commit b8281ce

Browse files
committed
Split out common code to new CANController class, add MCP2515 specific class
1 parent e153872 commit b8281ce

File tree

5 files changed

+378
-255
lines changed

5 files changed

+378
-255
lines changed

src/CAN.h

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -4,88 +4,6 @@
44
#ifndef CAN_H
55
#define CAN_H
66

7-
#include <Arduino.h>
8-
#include <SPI.h>
9-
10-
#define CAN_DEFAULT_CLOCK_FREQUENCY 16e6
11-
#define CAN_DEFAULT_SS_PIN 10
12-
#define CAN_DEFAULT_INT_PIN 2
13-
14-
class CANClass : public Stream {
15-
16-
public:
17-
CANClass();
18-
19-
int begin(long baudRate);
20-
void end();
21-
22-
int beginPacket(int id, int dlc = -1, bool rtr = false);
23-
int beginExtendedPacket(long id, int dlc = -1, bool rtr = false);
24-
int endPacket();
25-
26-
int parsePacket();
27-
long packetId();
28-
bool packetExtended();
29-
bool packetRtr();
30-
int packetDlc();
31-
32-
// from Print
33-
virtual size_t write(uint8_t byte);
34-
virtual size_t write(const uint8_t *buffer, size_t size);
35-
36-
// from Stream
37-
virtual int available();
38-
virtual int read();
39-
virtual int peek();
40-
virtual void flush();
41-
42-
void onReceive(void(*callback)(int));
43-
44-
int loopback();
45-
int sleep();
46-
int wakeup();
47-
48-
void setPins(int ss = CAN_DEFAULT_SS_PIN, int irq = CAN_DEFAULT_INT_PIN);
49-
void setSPIFrequency(uint32_t frequency);
50-
void setClockFrequency(long clockFrequency);
51-
52-
void dumpRegisters(Stream& out);
53-
54-
private:
55-
void reset();
56-
57-
void handleInterrupt();
58-
59-
uint8_t readRegister(uint8_t address);
60-
void modifyRegister(uint8_t address, uint8_t mask, uint8_t value);
61-
void writeRegister(uint8_t address, uint8_t value);
62-
63-
static void onInterrupt();
64-
65-
private:
66-
SPISettings _spiSettings;
67-
int _ss;
68-
int _irq;
69-
long _clockFrequency;
70-
void (*_onReceive)(int);
71-
72-
bool _packetBegun;
73-
long _txId;
74-
bool _txExtended;
75-
bool _txRtr;
76-
int _txDlc;
77-
int _txLength;
78-
uint8_t _txData[8];
79-
80-
long _rxId;
81-
bool _rxExtended;
82-
bool _rxRtr;
83-
int _rxDlc;
84-
int _rxLength;
85-
int _rxIndex;
86-
uint8_t _rxData[8];
87-
};
88-
89-
extern CANClass CAN;
7+
#include "MCP2515.h"
908

919
#endif

src/CANController.cpp

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
// Copyright (c) Sandeep Mistry. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
#include "CANController.h"
5+
6+
CANControllerClass::CANControllerClass() :
7+
_onReceive(NULL),
8+
9+
_packetBegun(false),
10+
_txId(-1),
11+
_txExtended(-1),
12+
_txRtr(false),
13+
_txDlc(0),
14+
_txLength(0),
15+
16+
_rxId(-1),
17+
_rxExtended(false),
18+
_rxRtr(false),
19+
_rxDlc(0),
20+
_rxLength(0),
21+
_rxIndex(0)
22+
{
23+
// overide Stream timeout value
24+
setTimeout(0);
25+
}
26+
27+
CANControllerClass::~CANControllerClass()
28+
{
29+
}
30+
31+
int CANControllerClass::begin(long /*baudRate*/)
32+
{
33+
_packetBegun = false;
34+
_txId = -1;
35+
_txRtr =false;
36+
_txDlc = 0;
37+
_txLength = 0;
38+
39+
_rxId = -1;
40+
_rxRtr = false;
41+
_rxDlc = 0;
42+
_rxLength = 0;
43+
_rxIndex = 0;
44+
45+
return 1;
46+
}
47+
48+
void CANControllerClass::end()
49+
{
50+
}
51+
52+
int CANControllerClass::beginPacket(int id, int dlc, bool rtr)
53+
{
54+
if (id < 0 || id > 0x7FF) {
55+
return 0;
56+
}
57+
58+
if (dlc > 8) {
59+
return 0;
60+
}
61+
62+
_packetBegun = true;
63+
_txId = id;
64+
_txExtended = false;
65+
_txRtr = rtr;
66+
_txDlc = dlc;
67+
_txLength = 0;
68+
69+
memset(_txData, 0x00, sizeof(_txData));
70+
71+
return 1;
72+
}
73+
74+
int CANControllerClass::beginExtendedPacket(long id, int dlc, bool rtr)
75+
{
76+
if (id < 0 || id > 0x1FFFFFFF) {
77+
return 0;
78+
}
79+
80+
if (dlc > 8) {
81+
return 0;
82+
}
83+
84+
_packetBegun = true;
85+
_txId = id;
86+
_txExtended = true;
87+
_txRtr = rtr;
88+
_txDlc = dlc;
89+
_txLength = 0;
90+
91+
memset(_txData, 0x00, sizeof(_txData));
92+
93+
return 1;
94+
}
95+
96+
int CANControllerClass::endPacket()
97+
{
98+
if (!_packetBegun) {
99+
return 0;
100+
}
101+
_packetBegun = false;
102+
103+
if (_txDlc >= 0) {
104+
_txLength = _txDlc;
105+
}
106+
107+
return 1;
108+
}
109+
110+
int CANControllerClass::parsePacket()
111+
{
112+
return 0;
113+
}
114+
115+
long CANControllerClass::packetId()
116+
{
117+
return _rxId;
118+
}
119+
120+
bool CANControllerClass::packetExtended()
121+
{
122+
return _rxExtended;
123+
}
124+
125+
bool CANControllerClass::packetRtr()
126+
{
127+
return _rxRtr;
128+
}
129+
130+
int CANControllerClass::packetDlc()
131+
{
132+
return _rxDlc;
133+
}
134+
135+
size_t CANControllerClass::write(uint8_t byte)
136+
{
137+
return write(&byte, sizeof(byte));
138+
}
139+
140+
size_t CANControllerClass::write(const uint8_t *buffer, size_t size)
141+
{
142+
if (!_packetBegun) {
143+
return 0;
144+
}
145+
146+
if (size > (sizeof(_txData) - _txLength)) {
147+
size = sizeof(_txData) - _txLength;
148+
}
149+
150+
memcpy(&_txData[_txLength], buffer, size);
151+
_txLength += size;
152+
153+
return size;
154+
}
155+
156+
int CANControllerClass::available()
157+
{
158+
return (_rxLength - _rxIndex);
159+
}
160+
161+
int CANControllerClass::read()
162+
{
163+
if (!available()) {
164+
return -1;
165+
}
166+
167+
return _rxData[_rxIndex++];
168+
}
169+
170+
int CANControllerClass::peek()
171+
{
172+
if (!available()) {
173+
return -1;
174+
}
175+
176+
return _rxData[_rxIndex];
177+
}
178+
179+
void CANControllerClass::flush()
180+
{
181+
}
182+
183+
void CANControllerClass::onReceive(void(*callback)(int))
184+
{
185+
_onReceive = callback;
186+
}
187+
188+
int CANControllerClass::observe()
189+
{
190+
return 0;
191+
}
192+
193+
int CANControllerClass::loopback()
194+
{
195+
return 0;
196+
}
197+
198+
int CANControllerClass::sleep()
199+
{
200+
return 0;
201+
}
202+
203+
int CANControllerClass::wakeup()
204+
{
205+
return 0;
206+
}

src/CANController.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (c) Sandeep Mistry. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
#ifndef CAN_CONTROLLER_H
5+
#define CAN_CONTROLLER_H
6+
7+
#include <Arduino.h>
8+
9+
class CANControllerClass : public Stream {
10+
11+
public:
12+
virtual int begin(long baudRate);
13+
virtual void end();
14+
15+
int beginPacket(int id, int dlc = -1, bool rtr = false);
16+
int beginExtendedPacket(long id, int dlc = -1, bool rtr = false);
17+
virtual int endPacket();
18+
19+
virtual int parsePacket();
20+
long packetId();
21+
bool packetExtended();
22+
bool packetRtr();
23+
int packetDlc();
24+
25+
// from Print
26+
virtual size_t write(uint8_t byte);
27+
virtual size_t write(const uint8_t *buffer, size_t size);
28+
29+
// from Stream
30+
virtual int available();
31+
virtual int read();
32+
virtual int peek();
33+
virtual void flush();
34+
35+
virtual void onReceive(void(*callback)(int));
36+
37+
virtual int observe();
38+
virtual int loopback();
39+
virtual int sleep();
40+
virtual int wakeup();
41+
42+
protected:
43+
CANControllerClass();
44+
virtual ~CANControllerClass();
45+
46+
protected:
47+
void (*_onReceive)(int);
48+
49+
bool _packetBegun;
50+
long _txId;
51+
bool _txExtended;
52+
bool _txRtr;
53+
int _txDlc;
54+
int _txLength;
55+
uint8_t _txData[8];
56+
57+
long _rxId;
58+
bool _rxExtended;
59+
bool _rxRtr;
60+
int _rxDlc;
61+
int _rxLength;
62+
int _rxIndex;
63+
uint8_t _rxData[8];
64+
};
65+
66+
#endif

0 commit comments

Comments
 (0)