Skip to content

Commit 5b08605

Browse files
committed
first commit
0 parents  commit 5b08605

File tree

8 files changed

+982
-0
lines changed

8 files changed

+982
-0
lines changed

CanHacker.cpp

Lines changed: 717 additions & 0 deletions
Large diffs are not rendered by default.

CanHacker.h

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#include <can.h>
2+
#include <mcp_can.h>
3+
4+
/*
5+
* CanHacker.h
6+
*
7+
* Created on: 17 ���. 2015 �.
8+
* Author: Dmitry
9+
*/
10+
11+
#ifndef CANHACKER_H_
12+
#define CANHACKER_H_
13+
14+
#include <can.h>
15+
16+
#define CAN_MIN_DLEN 1
17+
#define HEX_PER_BYTE 2
18+
#define MIN_MESSAGE_DATA_HEX_LENGTH CAN_MIN_DLEN * HEX_PER_BYTE
19+
#define MAX_MESSAGE_DATA_HEX_LENGTH CAN_MAX_DLEN * HEX_PER_BYTE
20+
#define MIN_MESSAGE_LENGTH 5
21+
22+
#define CANHACKER_CMD_MAX_LENGTH 26
23+
24+
#define CANHACKER_SERIAL_RESPONSE "N0001\r"
25+
#define CANHACKER_SW_VERSION_RESPONSE "v0107\r"
26+
#define CANHACKER_VERSION_RESPONSE "V1010\r"
27+
28+
class CanHacker {
29+
public:
30+
enum ERROR {
31+
ERROR_OK,
32+
ERROR_CONNECTED,
33+
ERROR_NOT_CONNECTED,
34+
ERROR_UNKNOWN_COMMAND,
35+
ERROR_INVALID_COMMAND,
36+
ERROR_ERROR_FRAME_NOT_SUPPORTED,
37+
ERROR_BUFFER_OVERFLOW,
38+
ERROR_SERIAL_TX_OVERRUN,
39+
ERROR_LISTEN_ONLY,
40+
ERROR_MCP2515_INIT,
41+
ERROR_MCP2515_INIT_CONFIG,
42+
ERROR_MCP2515_INIT_BITRATE,
43+
ERROR_MCP2515_INIT_SET_MODE,
44+
ERROR_MCP2515_SEND,
45+
ERROR_MCP2515_READ,
46+
ERROR_MCP2515_FILTER,
47+
ERROR_MCP2515_ERRIF,
48+
ERROR_MCP2515_MERRF
49+
};
50+
51+
CanHacker(Stream *stream, Stream *debugStream, uint8_t cs);
52+
~CanHacker();
53+
ERROR receiveCommand(const char *buffer, const int length);
54+
ERROR receiveCanFrame(const struct can_frame *frame);
55+
ERROR sendFrame(const struct can_frame *);
56+
ERROR setLoopbackEnabled(const bool value);
57+
ERROR pollReceiveCan();
58+
ERROR receiveCan(const MCP_CAN::RXBn rxBuffer);
59+
MCP_CAN *getMcp2515();
60+
ERROR processInterrupt();
61+
Stream *getInterfaceStream();
62+
63+
private:
64+
65+
static const char CR = '\r';
66+
static const char BEL = 7;
67+
static const uint16_t TIMESTAMP_LIMIT = 0xEA60;
68+
69+
bool _timestampEnabled = false;
70+
bool _listenOnly = false;
71+
bool _loopback = false;
72+
uint8_t _cs;
73+
MCP_CAN *mcp2515;
74+
CAN_SPEED bitrate;
75+
bool _isConnected = false;
76+
Stream *_stream;
77+
Stream *_debugStream;
78+
79+
enum /*class*/ COMMAND : char {
80+
COMMAND_SET_BITRATE = 'S', // set CAN bit rate
81+
COMMAND_SET_BTR = 's', // set CAN bit rate via
82+
COMMAND_OPEN_CAN_CHAN = 'O', // open CAN channel
83+
COMMAND_CLOSE_CAN_CHAN = 'C', // close CAN channel
84+
COMMAND_SEND_11BIT_ID = 't', // send CAN message with 11bit ID
85+
COMMAND_SEND_29BIT_ID = 'T', // send CAN message with 29bit ID
86+
COMMAND_SEND_R11BIT_ID = 'r', // send CAN remote message with 11bit ID
87+
COMMAND_SEND_R29BIT_ID = 'R', // send CAN remote message with 29bit ID
88+
COMMAND_READ_STATUS = 'F', // read status flag byte
89+
COMMAND_SET_ACR = 'M', // set Acceptance Code Register
90+
COMMAND_SET_AMR = 'm', // set Acceptance Mask Register
91+
COMMAND_GET_VERSION = 'V', // get hardware and software version
92+
COMMAND_GET_SW_VERSION = 'v', // get software version only
93+
COMMAND_GET_SERIAL = 'N', // get device serial number
94+
COMMAND_TIME_STAMP = 'Z', // toggle time stamp setting
95+
COMMAND_READ_ECR = 'E', // read Error Capture Register
96+
COMMAND_READ_ALCR = 'A', // read Arbritation Lost Capture Register
97+
COMMAND_READ_REG = 'G', // read register conten from SJA1000
98+
COMMAND_WRITE_REG = 'W', // write register content to SJA1000
99+
COMMAND_LISTEN_ONLY = 'L' // switch to listen only mode
100+
};
101+
102+
ERROR parseTransmit(const char *buffer, int length, struct can_frame *frame);
103+
ERROR createTransmit(const struct can_frame *frame, char *buffer, const int length);
104+
105+
uint16_t getTimestamp();
106+
ERROR setFilter(const uint32_t filter);
107+
ERROR setFilterMask(const uint32_t mask);
108+
109+
ERROR connectCan();
110+
ERROR disconnectCan();
111+
bool isConnected();
112+
ERROR writeCan(const struct can_frame *);
113+
ERROR writeStream(const char character);
114+
ERROR writeStream(const char *buffer);
115+
ERROR writeDebugStream(const char character);
116+
ERROR writeDebugStream(const char *buffer);
117+
ERROR writeDebugStream(const int buffer);
118+
ERROR writeDebugStream(const uint8_t *buffer, size_t size);
119+
120+
ERROR receiveSetBitrateCommand(const char *buffer, const int length);
121+
ERROR receiveTransmitCommand(const char *buffer, const int length);
122+
ERROR receiveTimestampCommand(const char *buffer, const int length);
123+
ERROR receiveCloseCommand(const char *buffer, const int length);
124+
ERROR receiveOpenCommand(const char *buffer, const int length);
125+
ERROR receiveListenOnlyCommand(const char *buffer, const int length);
126+
ERROR receiveSetAcrCommand(const char *buffer, const int length);
127+
ERROR receiveSetAmrCommand(const char *buffer, const int length);
128+
};
129+
130+
#endif /* CANHACKER_H_ */

CanHackerLineReader.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "CanHackerLineReader.h"
2+
3+
CanHackerLineReader::CanHackerLineReader(CanHacker *vCanHacker) {
4+
_canHacker = vCanHacker;
5+
index = 0;
6+
memset(buffer, 0, sizeof(buffer));
7+
}
8+
9+
CanHacker::ERROR CanHackerLineReader::processChar(char rxChar) {
10+
switch (rxChar) {
11+
case '\r':
12+
case '\n':
13+
if (index > 0) {
14+
buffer[index] = '\0';
15+
16+
CanHacker::ERROR error = _canHacker->receiveCommand(buffer, index);
17+
index = 0;
18+
if (error != CanHacker::ERROR_OK) {
19+
return error;
20+
}
21+
}
22+
break;
23+
24+
case '\0':
25+
break;
26+
27+
default:
28+
if (index < COMMAND_MAX_LENGTH) {
29+
buffer[index++] = rxChar;
30+
} else {
31+
index = 0;
32+
return CanHacker::ERROR_INVALID_COMMAND;
33+
}
34+
break;
35+
}
36+
return CanHacker::ERROR_OK;
37+
}
38+
39+
CanHacker::ERROR CanHackerLineReader::process() {
40+
Stream *stream = _canHacker->getInterfaceStream();
41+
while (stream->available()) {
42+
CanHacker::ERROR error = processChar(stream->read());
43+
if (error != CanHacker::ERROR_OK) {
44+
return error;
45+
}
46+
}
47+
48+
return CanHacker::ERROR_OK;
49+
}

CanHackerLineReader.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef CANHACKERLINEREADER_H_
2+
#define CANHACKERLINEREADER_H_
3+
4+
#include "CanHacker.h"
5+
6+
class CanHackerLineReader {
7+
private:
8+
static const int COMMAND_MAX_LENGTH = 30; // not including \r\0
9+
10+
CanHacker *_canHacker;
11+
char buffer[COMMAND_MAX_LENGTH + 2];
12+
int index;
13+
Stream *_stream;
14+
public:
15+
CanHackerLineReader(CanHacker *vCanHacker);
16+
CanHacker::ERROR processChar(char rxChar);
17+
CanHacker::ERROR process();
18+
};
19+
20+
#endif /* CANHACKERLINEREADER_H_ */

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Dmitry
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# CanHacker (lawicel) CAN adapter on Arduino + MCP2515
2+
3+
Use that [Library](https://github.com/autowp/CAN_BUS_Shield) to communicate with MCP2515
4+
5+
Testes with Arduino Nano.
6+
On Arduino Uno have problem with too long boot period and losing first command

lib.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "lib.h"
2+
3+
unsigned char hexCharToByte(char hex)
4+
{
5+
unsigned char result = 0;
6+
if (hex >= 0x30 && hex <= 0x39) {
7+
result = hex - 0x30;
8+
} else if (hex >= 0x41 && hex <= 0x46) {
9+
result = hex - 0x41 + 0x0A;
10+
} else if (hex >= 0x61 && hex <= 0x66) {
11+
result = hex - 0x61 + 0x0A;
12+
}
13+
return result;
14+
}
15+
16+
uint8_t ascii2byte (uint8_t *val) {
17+
uint8_t temp = *val;
18+
if (temp > 0x60) temp -= 0x27; // convert chars a-f
19+
else if (temp > 0x40) temp -= 0x07; // convert chars A-F
20+
temp -= 0x30; // convert chars 0-9
21+
return temp & 0x0F;
22+
}
23+
24+
uint8_t nibble2ascii(uint8_t nibble) {
25+
uint8_t tmp = nibble & 0x0f;
26+
return tmp < 10 ? tmp + 48 : tmp + 55;
27+
}

lib.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef LIB_H_
2+
#define LIB_H_
3+
4+
#include <stdint.h>
5+
6+
unsigned char hexCharToByte(char hex);
7+
8+
uint8_t ascii2byte (uint8_t * val);
9+
10+
uint8_t nibble2ascii(uint8_t nibble);
11+
12+
#endif /* LIB_H_ */

0 commit comments

Comments
 (0)