Skip to content

Commit 0838960

Browse files
committed
add canbus monitor exmaple,compiled
1 parent 57d8f93 commit 0838960

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

examples/canbus-monitor/canbus-monitor.ino

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,27 @@
1414
#include <SPI.h>
1515
#include "mcp_can.h"
1616
#include "can-serial.h"
17+
18+
#define CAN_2515
19+
20+
#ifdef CAN_2518FD
21+
#include "mcp2518fd_can.h"
22+
const int SPI_CS_PIN = BCM8;
23+
const int CAN_INT_PIN = BCM25;
24+
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
25+
#endif
26+
27+
#ifdef CAN_2515
28+
#include "mcp2515_can.h"
29+
const int SPI_CS_PIN = 9;
30+
const int CAN_INT_PIN = 2;
31+
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
32+
#endif // Set CS pin
33+
34+
35+
1736
void setup() {
1837
Serial.begin(LWUART_DEFAULT_BAUD_RATE); // default COM baud rate is 115200.
19-
2038
// Can232::init (RATE, CLOCK)
2139
// Rates: CAN_10KBPS, CAN_20KBPS, CAN_50KBPS, CAN_100KBPS, CAN_125KBPS, CAN_250KBPS, CAN_500KBPS, CAN_500KBPS, CAN_1000KBPS, CAN_83K3BPS
2240
// Default is CAN_83K3BPS ;)))))))))
@@ -26,9 +44,9 @@ void setup() {
2644

2745
// Can232::init(); // rate and clock = LW232_DEFAULT_CAN_RATE and LW232_DEFAULT_CLOCK_FREQ
2846
// Can232::init(CAN_125KBPS); // rate = 125, clock = LW232_DEFAULT_CLOCK_FREQ
29-
CanSerial::init(CAN_125KBPS, MCP_16MHz); // set default rate you need here and clock frequency of CAN shield. Typically it is 16MHz, but on some MCP2515 + TJA1050 it is 8Mhz
30-
47+
//CanSerial::init(CAN_125KBPS, MCP_16MHz); // set default rate you need here and clock frequency of CAN shield. Typically it is 16MHz, but on some MCP2515 + TJA1050 it is 8Mhz
3148

49+
CanSerial::init(CAN);
3250
// optional custom packet filter to reduce number of messages comingh through to canhacker
3351
// Can232::setFilter(myCustomAddressFilter);
3452
}

src/can-serial.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ void CanSerial::init(INT8U defaultCanSpeed, const INT8U clock) {
5858
instance()->initFunc();
5959
}
6060

61+
62+
void CanSerial::init(MCP_CAN *CAN) {
63+
LWUARTCAN = CAN;
64+
}
65+
66+
6167
void CanSerial::setFilter(INT8U (*userFunc)(INT32U)) {
6268
instance()->setFilterFunc(userFunc);
6369
}
@@ -159,7 +165,7 @@ INT8U CanSerial::parseAndRunCommand() {
159165
// Sn[CR] Setup with standard CAN bit-rates where n is 0-9.
160166
if (LWUARTCanChannelMode == LWUART_STATUS_CAN_CLOSED) {
161167
idx = HexHelper::parseNibbleWithLimit(LWUARTMessage[1], LWUART_CAN_BAUD_NUM);
162-
LWUARTCanSpeedSelection = LWUARTCanBaudRates[idx];
168+
LWUARTCanSpeedSelection = idx; //todo
163169
}
164170
else {
165171
ret = LWUART_ERR;
@@ -297,9 +303,9 @@ INT8U CanSerial::parseAndRunCommand() {
297303
// F[CR] Read Status Flags.
298304
// LAWICEL CanSerial and CANUSB have some specific errors which differ from MCP2515/MCP2551 errors. We just return MCP2515 error.
299305
Serial.print(LWUART_FLAG);
300-
if (LWUARTCAN.checkError(&err) == CAN_OK)
306+
if (LWUARTCAN->checkError() == CAN_OK)
301307
err = 0;
302-
HexHelper::printFullByte(err & MCP_EFLG_ERRORMASK);
308+
HexHelper::printFullByte(err);
303309
break;
304310
case LWUART_CMD_AUTOPOLL:
305311
// Xn[CR] Sets Auto Poll/Send ON/OFF for received frames.
@@ -378,15 +384,15 @@ INT8U CanSerial::parseAndRunCommand() {
378384

379385
INT8U CanSerial::checkReceive() {
380386
#ifndef _MCP_FAKE_MODE_
381-
return LWUARTCAN.checkReceive();
387+
return LWUARTCAN->checkReceive();
382388
#else
383389
return CAN_MSGAVAIL;
384390
#endif
385391
}
386392

387393
INT8U CanSerial::readMsgBufID(INT32U *ID, INT8U *len, INT8U buf[]) {
388394
#ifndef _MCP_FAKE_MODE_
389-
return LWUARTCAN.readMsgBufID(ID, len, buf);
395+
return LWUARTCAN->readMsgBufID(ID, len, buf);
390396
#else
391397
*ID = random(0x100, 0x110);
392398
*len = 4;
@@ -451,7 +457,7 @@ INT8U CanSerial::receiveSingleFrame() {
451457

452458
INT8U CanSerial::isExtendedFrame() {
453459
#ifndef _MCP_FAKE_MODE_
454-
return LWUARTCAN.isExtendedFrame();
460+
return LWUARTCAN->isExtendedFrame();
455461
#else
456462
return LWUARTCanId > 0x7FF ? 1 : 0; //simple hack for fake mode
457463
#endif
@@ -468,7 +474,7 @@ INT8U CanSerial::checkPassFilter(INT32U addr) {
468474
INT8U CanSerial::openCanBus() {
469475
INT8U ret = LWUART_OK;
470476
#ifndef _MCP_FAKE_MODE_
471-
if (CAN_OK != LWUARTCAN.begin(LWUARTCanSpeedSelection, LWUARTMcpModuleClock))
477+
if (CAN_OK != LWUARTCAN->begin(LWUARTCanSpeedSelection, LWUARTMcpModuleClock))
472478
ret = LWUART_ERR;
473479
#endif
474480
return ret;
@@ -477,7 +483,7 @@ INT8U CanSerial::openCanBus() {
477483

478484
INT8U CanSerial::sendMsgBuf(INT32U id, INT8U ext, INT8U rtr, INT8U len, INT8U *buf) {
479485
#ifndef _MCP_FAKE_MODE_
480-
return LWUARTCAN.sendMsgBuf(id, ext, rtr, len, buf);
486+
return LWUARTCAN->sendMsgBuf(id, ext, rtr, len, buf);
481487
#else
482488
Serial.print("<sending:");
483489
Serial.print(id, HEX);

src/can-serial.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,23 +157,31 @@
157157

158158

159159
#define LWUART_DEFAULT_BAUD_RATE 115200
160-
#define LWUART_DEFAULT_CAN_RATE CAN_500KBPS
161-
#define LWUART_DEFAULT_CLOCK_FREQ MCP_16MHz
160+
// #define LWUART_DEFAULT_CAN_RATE CAN_500KBPS
161+
// #define LWUART_DEFAULT_CLOCK_FREQ MCP_16MHz
162162

163163
#define LWUART_CAN_BAUD_NUM 0x0a
164164
#define LWUART_UART_BAUD_NUM 0x07
165165

166166

167+
#define CAN_OK (0)
168+
#define CAN_FAILINIT (1)
169+
#define CAN_FAILTX (2)
170+
#define CAN_MSGAVAIL (3)
171+
172+
173+
167174
const INT32U LWUARTSerialBaudRates[] //PROGMEM
168175
= { 230400, 115200, 57600, 38400, 19200, 9600, 2400 };
169176

170-
const INT32U LWUARTCanBaudRates[] //PROGMEM
171-
= { CAN_10KBPS, CAN_20KBPS, CAN_50KBPS, CAN_100KBPS, CAN_125KBPS, CAN_250KBPS, CAN_500KBPS, CAN_500KBPS /*CAN_800KBPS*/, CAN_1000KBPS, CAN_83K3BPS };
177+
// const INT32U LWUARTCanBaudRates[] //PROGMEM
178+
// = { CAN_10KBPS, CAN_20KBPS, CAN_50KBPS, CAN_100KBPS, CAN_125KBPS, CAN_250KBPS, CAN_500KBPS, CAN_500KBPS /*CAN_800KBPS*/, CAN_1000KBPS, CAN_83K3BPS };
172179

173180
class CanSerial
174181
{
175182
public:
176-
static void init(INT8U defaultCanSpeed = LWUART_DEFAULT_CAN_RATE, const INT8U clock = LWUART_DEFAULT_CLOCK_FREQ);
183+
static void init(INT8U defaultCanSpeed, const INT8U clock);
184+
void init(MCP_CAN *can);
177185
static void setFilter(INT8U (*userFunc)(INT32U));
178186
static void loop();
179187
static void serialEvent();
@@ -189,10 +197,11 @@ class CanSerial
189197

190198
INT8U (*userAddressFilterFunc)(INT32U addr) = 0;
191199

192-
MCP_CAN LWUARTCAN = MCP_CAN(LWUART_CAN_BUS_SHIELD_CS_PIN);
200+
MCP_CAN *LWUARTCAN;
201+
//MCP_CAN LWUARTCAN = MCP_CAN(LWUART_CAN_BUS_SHIELD_CS_PIN);
193202

194-
INT8U LWUARTCanSpeedSelection = CAN_83K3BPS;
195-
INT8U LWUARTMcpModuleClock = MCP_16MHz;
203+
INT8U LWUARTCanSpeedSelection ;
204+
INT8U LWUARTMcpModuleClock ;
196205
INT8U LWUARTCanChannelMode = LWUART_STATUS_CAN_CLOSED;
197206
INT8U LWUARTLastErr = LWUART_OK;
198207

0 commit comments

Comments
 (0)