Skip to content

Commit f118ddf

Browse files
committed
first commit
0 parents  commit f118ddf

File tree

7 files changed

+1282
-0
lines changed

7 files changed

+1282
-0
lines changed

LICENSE.md

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

README.md

+217
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
Arduino MCP2515 CAN interface library
2+
---------------------------------------------------------
3+
4+
5+
<br>
6+
CAN-BUS is a common industrial bus because of its long travel distance, medium communication speed and high reliability. It is commonly found on modern machine tools and as an automotive diagnostic bus. This CAN-BUS Shield adopts MCP2515 CAN Bus controller with SPI interface and MCP2551 CAN transceiver to give your Arduino/Seeeduino CAN-BUS capibility. With an OBD-II converter cable added on and the OBD-II library imported, you are ready to build an onboard diagnostic device or data logger.
7+
8+
- Implements CAN V2.0B at up to 1 Mb/s
9+
- SPI Interface up to 10 MHz
10+
- Standard (11 bit) and extended (29 bit) data and remote frames
11+
- Two receive buffers with prioritized message storage
12+
13+
14+
<br>
15+
# Usage:
16+
17+
18+
19+
## 1. Initialization
20+
21+
To create connection with MCP2515 provide pin number where SPI CS is connected (10 by default), baudrate and mode
22+
23+
The available modes are listed as follows:
24+
```C++
25+
mcp2515.setNormalMode();
26+
mcp2515.setLoopbackMode();
27+
mcp2515.setListenOnlyMode();
28+
```
29+
The available baudrates are listed as follows:
30+
```C++
31+
enum CAN_SPEED {
32+
CAN_5KBPS,
33+
CAN_10KBPS,
34+
CAN_20KBPS,
35+
CAN_31K25BPS,
36+
CAN_33KBPS,
37+
CAN_40KBPS,
38+
CAN_50KBPS,
39+
CAN_80KBPS,
40+
CAN_83K3BPS,
41+
CAN_95KBPS,
42+
CAN_100KBPS,
43+
CAN_125KBPS,
44+
CAN_200KBPS,
45+
CAN_250KBPS,
46+
CAN_500KBPS,
47+
CAN_1000KBPS
48+
};
49+
```
50+
51+
<br>
52+
Example of initialization
53+
```C++
54+
MCP_CAN mcp2515(10);
55+
mcp2515.reset();
56+
mcp2515.setBitrate(CAN_125KBPS);
57+
mcp2515.setLoopbackMode();
58+
```
59+
<br>
60+
61+
Note: To transfer data on high speed of CAN interface via UART dont forget to update UART baudrate as necessary.
62+
63+
##2. Frame data format
64+
65+
Library uses Linux-like structure to store can frames;
66+
```C++
67+
struct can_frame {
68+
uint32_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
69+
uint8_t can_dlc;
70+
uint8_t data[8];
71+
};
72+
```
73+
For additional information see [SocketCAN](https://www.kernel.org/doc/Documentation/networking/can.txt)
74+
75+
## 3. Send Data
76+
```C++
77+
MCP_CAN::ERROR sendMessage(const MCP_CAN::TXBn txbn, const struct can_frame *frame);
78+
MCP_CAN::ERROR sendMessage(const struct can_frame *frame);
79+
```
80+
81+
This is a function to send data onto the bus.
82+
83+
For example, In the 'send' example, we have:
84+
85+
```C++
86+
struct can_frame frame;
87+
frame.can_id = 0x000;
88+
frame.can_dlc = 4;
89+
frame.data[0] = 0xFF;
90+
frame.data[1] = 0xFF;
91+
frame.data[2] = 0xFF;
92+
frame.data[3] = 0xFF;
93+
94+
/* send out the message to the bus and
95+
tell other devices this is a standard frame from 0x00. */
96+
mcp2515.sendMessage(&frame);
97+
```
98+
99+
```C++
100+
struct can_frame frame;
101+
frame.can_id = 0x12345678 | CAN_EFF_MASK;
102+
frame.can_dlc = 2;
103+
frame.data[0] = 0xFF;
104+
frame.data[1] = 0xFF;
105+
106+
/* send out the message to the bus using second TX buffer and
107+
tell other devices this is a extended frame from 0x12345678. */
108+
mcp2515.sendMessage(MCP_CAN::TXB1, &frame);
109+
```
110+
111+
112+
<br>
113+
## 4. Receive Data
114+
115+
The following function is used to receive data on the 'receive' node:
116+
117+
```C++
118+
MCP_CAN::ERROR readMessage(const MCP_CAN::RXBn rxbn, struct can_frame *frame);
119+
MCP_CAN::ERROR readMessage(struct can_frame *frame);
120+
```
121+
122+
In conditions that masks and filters have been set. This function can only get frames that meet the requirements of masks and filters.
123+
124+
You can choise one of two method to receive: interrup-based and polling
125+
126+
Example of poll read
127+
128+
```C++
129+
struct can_frame frame;
130+
131+
void loop() {
132+
if (mcp2515.readMessage(&frame) == MCP_CAN::ERROR_OK) {
133+
// frame contains received message
134+
}
135+
}
136+
```
137+
138+
Example of interrupt based read
139+
```C++
140+
bool interrupt = false;
141+
struct can_frame frame;
142+
143+
void irqHandler() {
144+
interrupt = true;
145+
}
146+
147+
void setup() {
148+
...
149+
attachInterrupt(0, irqHandler, FALLING);
150+
}
151+
152+
void loop() {
153+
if (interrupt) {
154+
interrupt = false;
155+
156+
uint8_t irq = mcp2515.getInterrupts();
157+
158+
if (irq & MCP_CAN::CANINTF_RX0IF) {
159+
if (mcp2515.readMessage(MCP_CAN::RXB0, &frame) == MCP_CAN::ERROR_OK) {
160+
// frame contains received from RXB0 message
161+
}
162+
}
163+
164+
if (irq & MCP_CAN::CANINTF_RX1IF) {
165+
if (mcp2515.readMessage(MCP_CAN::RXB1, &frame) == MCP_CAN::ERROR_OK) {
166+
// frame contains received from RXB1 message
167+
}
168+
}
169+
}
170+
}
171+
```
172+
<br>
173+
##5. Set Receive Mask and Filter
174+
175+
There are 2 receive mask registers and 5 filter registers on the controller chip that guarantee you get data from the target device. They are useful especially in a large network consisting of numerous nodes.
176+
177+
We provide two functions for you to utilize these mask and filter registers. They are:
178+
179+
```C++
180+
MCP_CAN::ERROR setFilterMask(const MASK mask, const bool ext, const uint32_t ulData)
181+
MCP_CAN::ERROR setFilter(const RXF num, const bool ext, const uint32_t ulData)
182+
```
183+
184+
**MASK mask** represents one of two mask **MCP_CAN::MASK0** or **MCP_CAN::MASK1**
185+
186+
**RXF num** represents one of six acceptance filters registers from **MCP_CAN::RXF0** to **MCP_CAN::RXF5**
187+
188+
**ext** represents the status of the frame. **false** means it's a mask or filter for a standard frame. **true** means it's for a extended frame.
189+
190+
**ulData** represents the content of the mask of filter.
191+
192+
193+
194+
<br>
195+
## 6. Examples
196+
197+
Example implementation of CanHacker (lawicel) protocol based device: [https://github.com/autowp/can-usb](https://github.com/autowp/can-usb)
198+
199+
<br>
200+
For more information, please refer to [wiki page](http://www.seeedstudio.com/wiki/CAN-BUS_Shield).
201+
202+
203+
----
204+
205+
This software is written by loovee ([[email protected]]([email protected] "[email protected]")) for seeed studio,<br>
206+
Updated by Dmitry ([https://github.com/autowp](https://github.com/autowp "https://github.com/autowp"))<br>
207+
and is licensed under [The MIT License](http://opensource.org/licenses/mit-license.php). Check [LICENSE.md](LICENSE.md) for more information.<br>
208+
209+
Contributing to this software is warmly welcomed. You can do this basically by<br>
210+
[forking](https://help.github.com/articles/fork-a-repo), committing modifications and then [pulling requests](https://help.github.com/articles/using-pull-requests) (follow the links above<br>
211+
for operating guide). Adding change log and your contact into file header is encouraged.<br>
212+
Thanks for your contribution.
213+
214+
Seeed Studio is an open hardware facilitation company based in Shenzhen, China. <br>
215+
Benefiting from local manufacture power and convenient global logistic system, <br>
216+
we integrate resources to serve new era of innovation. Seeed also works with <br>
217+
global distributors and partners to push open hardware movement.<br>

can.h

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef CAN_H_
2+
#define CAN_H_
3+
4+
#include <stdint.h>
5+
6+
7+
typedef unsigned char __u8;
8+
typedef unsigned short __u16;
9+
typedef unsigned long __u32;
10+
11+
12+
/* special address description flags for the CAN_ID */
13+
#define CAN_EFF_FLAG 0x80000000UL /* EFF/SFF is set in the MSB */
14+
#define CAN_RTR_FLAG 0x40000000UL /* remote transmission request */
15+
#define CAN_ERR_FLAG 0x20000000UL /* error message frame */
16+
17+
/* valid bits in CAN ID for frame formats */
18+
#define CAN_SFF_MASK 0x000007FFUL /* standard frame format (SFF) */
19+
#define CAN_EFF_MASK 0x1FFFFFFFUL /* extended frame format (EFF) */
20+
#define CAN_ERR_MASK 0x1FFFFFFFUL /* omit EFF, RTR, ERR flags */
21+
22+
/*
23+
* Controller Area Network Identifier structure
24+
*
25+
* bit 0-28 : CAN identifier (11/29 bit)
26+
* bit 29 : error message frame flag (0 = data frame, 1 = error message)
27+
* bit 30 : remote transmission request flag (1 = rtr frame)
28+
* bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit)
29+
*/
30+
typedef __u32 canid_t;
31+
32+
#define CAN_SFF_ID_BITS 11
33+
#define CAN_EFF_ID_BITS 29
34+
35+
/* CAN payload length and DLC definitions according to ISO 11898-1 */
36+
#define CAN_MAX_DLC 8
37+
#define CAN_MAX_DLEN 8
38+
39+
struct can_frame {
40+
canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
41+
__u8 can_dlc; /* frame payload length in byte (0 .. CAN_MAX_DLEN) */
42+
__u8 data[CAN_MAX_DLEN] __attribute__((aligned(8)));
43+
};
44+
45+
#endif /* CAN_H_ */

examples/CAN_read/CAN_read.ino

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <SPI.h>
2+
#include <mcp_can.h>
3+
4+
struct can_frame canMsg;
5+
MCP_CAN mcp2515(10);
6+
7+
8+
void setup() {
9+
Serial.begin(115200);
10+
SPI.begin();
11+
12+
mcp2515.reset();
13+
mcp2515.setBitrate(CAN_125KBPS);
14+
mcp2515.setNormalMode();
15+
16+
Serial.println("------- CAN Read ----------");
17+
Serial.println("ID DLC DATA");
18+
}
19+
20+
void loop() {
21+
22+
if (mcp2515.readMessage(&canMsg) == MCP_CAN::ERROR_OK) {
23+
24+
Serial.print(canMsg.can_id, HEX); // print ID
25+
Serial.print(" ");
26+
Serial.print(canMsg.can_dlc, HEX); // print DLC
27+
Serial.print(" ");
28+
29+
for (int i = 0; i<canMsg.can_dlc; i++) { // print the data
30+
31+
Serial.print(canMsg.data[i],HEX);
32+
Serial.print(" ");
33+
34+
}
35+
36+
Serial.println();
37+
}
38+
39+
}

keywords.txt

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#######################################
2+
# Syntax Coloring Map For debug_lvc
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
MCP_CAN KEYWORD1
9+
mcp_can_dfs KEYWORD1
10+
mcp_can KEYWORD1
11+
12+
#######################################
13+
# Methods and Functions (KEYWORD2)
14+
#######################################
15+
begin KEYWORD2
16+
init_Mask KEYWORD2
17+
init_Filt KEYWORD2
18+
sendMsgBuf KEYWORD2
19+
readMsgBuf KEYWORD2
20+
checkReceive KEYWORD2
21+
checkError KEYWORD2
22+
getCanId KEYWORD2
23+
24+
#######################################
25+
# Constants (LITERAL1)
26+
#######################################
27+
CAN_5KBPS LITERAL1
28+
CAN_10KBPS LITERAL1
29+
CAN_20KBPS LITERAL1
30+
CAN_40KBPS LITERAL1
31+
CAN_50KBPS LITERAL1
32+
CAN_80KBPS LITERAL1
33+
CAN_100KBPS LITERAL1
34+
CAN_125KBPS LITERAL1
35+
CAN_200KBPS LITERAL1
36+
CAN_250KBPS LITERAL1
37+
CAN_500KBPS LITERAL1
38+
CAN_1000KBPS LITERAL1
39+
CAN_OK LITERAL1
40+
CAN_FAILINIT LITERAL1
41+
CAN_FAILTX LITERAL1
42+
CAN_MSGAVAIL LITERAL1
43+
CAN_NOMSG LITERAL1
44+
CAN_CTRLERROR LITERAL1
45+
CAN_GETTXBFTIMEOUT LITERAL1
46+
CAN_SENDMSGTIMEOUT LITERAL1
47+
CAN_FAIL LITERAL1

0 commit comments

Comments
 (0)