-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12651 from mbezuidenhout/feature/can-bus
Feature/can bus
- Loading branch information
Showing
40 changed files
with
1,947 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>arduino-mcp2515</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
</buildSpec> | ||
<natures> | ||
</natures> | ||
</projectDescription> |
2 changes: 2 additions & 0 deletions
2
lib/lib_div/arduino-mcp2515-1.0.1/.settings/org.eclipse.core.resources.prefs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
eclipse.preferences.version=1 | ||
encoding/<project>=UTF-8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2013 Seeed Technology Inc. | ||
Copyright (c) 2016 Dmitry | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
Arduino MCP2515 CAN interface library | ||
--------------------------------------------------------- | ||
|
||
|
||
<br> | ||
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. | ||
|
||
- Implements CAN V2.0B at up to 1 Mb/s | ||
- SPI Interface up to 10 MHz | ||
- Standard (11 bit) and extended (29 bit) data and remote frames | ||
- Two receive buffers with prioritized message storage | ||
|
||
## Library Installation | ||
|
||
1. Download the ZIP file from https://github.com/autowp/arduino-mcp2515/archive/master.zip | ||
2. From the Arduino IDE: Sketch -> Include Library... -> Add .ZIP Library... | ||
3. Restart the Arduino IDE to see the new "mcp2515" library with examples | ||
|
||
|
||
<br> | ||
# Usage: | ||
|
||
|
||
|
||
## 1. Initialization | ||
|
||
To create connection with MCP2515 provide pin number where SPI CS is connected (10 by default), baudrate and mode | ||
|
||
The available modes are listed as follows: | ||
```C++ | ||
mcp2515.setNormalMode(); | ||
mcp2515.setLoopbackMode(); | ||
mcp2515.setListenOnlyMode(); | ||
``` | ||
The available baudrates are listed as follows: | ||
```C++ | ||
enum CAN_SPEED { | ||
CAN_5KBPS, | ||
CAN_10KBPS, | ||
CAN_20KBPS, | ||
CAN_31K25BPS, | ||
CAN_33KBPS, | ||
CAN_40KBPS, | ||
CAN_50KBPS, | ||
CAN_80KBPS, | ||
CAN_83K3BPS, | ||
CAN_95KBPS, | ||
CAN_100KBPS, | ||
CAN_125KBPS, | ||
CAN_200KBPS, | ||
CAN_250KBPS, | ||
CAN_500KBPS, | ||
CAN_1000KBPS | ||
}; | ||
``` | ||
|
||
<br> | ||
Example of initialization | ||
```C++ | ||
MCP2515 mcp2515(10); | ||
mcp2515.reset(); | ||
mcp2515.setBitrate(CAN_125KBPS); | ||
mcp2515.setLoopbackMode(); | ||
``` | ||
<br> | ||
<br> | ||
You can also set oscillator frequency for module when setting bitrate: | ||
```C++ | ||
mcp2515.setBitrate(CAN_125KBPS, MCP_8MHZ); | ||
``` | ||
<br> | ||
The available clock speeds are listed as follows: | ||
```C++ | ||
enum CAN_CLOCK { | ||
MCP_20MHZ, | ||
MCP_16MHZ, | ||
MCP_8MHZ | ||
}; | ||
``` | ||
Default value is MCP_16MHZ | ||
<br> | ||
|
||
Note: To transfer data on high speed of CAN interface via UART dont forget to update UART baudrate as necessary. | ||
|
||
##2. Frame data format | ||
|
||
Library uses Linux-like structure to store can frames; | ||
```C++ | ||
struct can_frame { | ||
uint32_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ | ||
uint8_t can_dlc; | ||
uint8_t data[8]; | ||
}; | ||
``` | ||
For additional information see [SocketCAN](https://www.kernel.org/doc/Documentation/networking/can.txt) | ||
## 3. Send Data | ||
```C++ | ||
MCP2515::ERROR sendMessage(const MCP2515::TXBn txbn, const struct can_frame *frame); | ||
MCP2515::ERROR sendMessage(const struct can_frame *frame); | ||
``` | ||
|
||
This is a function to send data onto the bus. | ||
|
||
For example, In the 'send' example, we have: | ||
|
||
```C++ | ||
struct can_frame frame; | ||
frame.can_id = 0x000; | ||
frame.can_dlc = 4; | ||
frame.data[0] = 0xFF; | ||
frame.data[1] = 0xFF; | ||
frame.data[2] = 0xFF; | ||
frame.data[3] = 0xFF; | ||
|
||
/* send out the message to the bus and | ||
tell other devices this is a standard frame from 0x00. */ | ||
mcp2515.sendMessage(&frame); | ||
``` | ||
```C++ | ||
struct can_frame frame; | ||
frame.can_id = 0x12345678 | CAN_EFF_MASK; | ||
frame.can_dlc = 2; | ||
frame.data[0] = 0xFF; | ||
frame.data[1] = 0xFF; | ||
/* send out the message to the bus using second TX buffer and | ||
tell other devices this is a extended frame from 0x12345678. */ | ||
mcp2515.sendMessage(MCP2515::TXB1, &frame); | ||
``` | ||
|
||
|
||
<br> | ||
## 4. Receive Data | ||
|
||
The following function is used to receive data on the 'receive' node: | ||
|
||
```C++ | ||
MCP2515::ERROR readMessage(const MCP2515::RXBn rxbn, struct can_frame *frame); | ||
MCP2515::ERROR readMessage(struct can_frame *frame); | ||
``` | ||
In conditions that masks and filters have been set. This function can only get frames that meet the requirements of masks and filters. | ||
You can choise one of two method to receive: interrup-based and polling | ||
Example of poll read | ||
```C++ | ||
struct can_frame frame; | ||
void loop() { | ||
if (mcp2515.readMessage(&frame) == MCP2515::ERROR_OK) { | ||
// frame contains received message | ||
} | ||
} | ||
``` | ||
|
||
Example of interrupt based read | ||
```C++ | ||
bool interrupt = false; | ||
struct can_frame frame; | ||
|
||
void irqHandler() { | ||
interrupt = true; | ||
} | ||
|
||
void setup() { | ||
... | ||
attachInterrupt(0, irqHandler, FALLING); | ||
} | ||
|
||
void loop() { | ||
if (interrupt) { | ||
interrupt = false; | ||
|
||
uint8_t irq = mcp2515.getInterrupts(); | ||
|
||
if (irq & MCP2515::CANINTF_RX0IF) { | ||
if (mcp2515.readMessage(MCP2515::RXB0, &frame) == MCP2515::ERROR_OK) { | ||
// frame contains received from RXB0 message | ||
} | ||
} | ||
|
||
if (irq & MCP2515::CANINTF_RX1IF) { | ||
if (mcp2515.readMessage(MCP2515::RXB1, &frame) == MCP2515::ERROR_OK) { | ||
// frame contains received from RXB1 message | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
<br> | ||
##5. Set Receive Mask and Filter | ||
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. | ||
We provide two functions for you to utilize these mask and filter registers. They are: | ||
```C++ | ||
MCP2515::ERROR setFilterMask(const MASK mask, const bool ext, const uint32_t ulData) | ||
MCP2515::ERROR setFilter(const RXF num, const bool ext, const uint32_t ulData) | ||
``` | ||
|
||
**MASK mask** represents one of two mask **MCP2515::MASK0** or **MCP2515::MASK1** | ||
|
||
**RXF num** represents one of six acceptance filters registers from **MCP2515::RXF0** to **MCP2515::RXF5** | ||
|
||
**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. | ||
|
||
**ulData** represents the content of the mask of filter. | ||
|
||
|
||
|
||
<br> | ||
## 6. Examples | ||
|
||
Example implementation of CanHacker (lawicel) protocol based device: [https://github.com/autowp/can-usb](https://github.com/autowp/can-usb) | ||
|
||
<br> | ||
For more information, please refer to [wiki page](http://www.seeedstudio.com/wiki/CAN-BUS_Shield). | ||
|
||
|
||
---- | ||
|
||
This software is written by loovee ([luweicong@seeed.cc](luweicong@seeed.cc "luweicong@seeed.cc")) for seeed studio,<br> | ||
Updated by Dmitry ([https://github.com/autowp](https://github.com/autowp "https://github.com/autowp"))<br> | ||
and is licensed under [The MIT License](http://opensource.org/licenses/mit-license.php). Check [LICENSE.md](LICENSE.md) for more information.<br> | ||
|
||
Contributing to this software is warmly welcomed. You can do this basically by<br> | ||
[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> | ||
for operating guide). Adding change log and your contact into file header is encouraged.<br> | ||
Thanks for your contribution. | ||
|
||
Seeed Studio is an open hardware facilitation company based in Shenzhen, China. <br> | ||
Benefiting from local manufacture power and convenient global logistic system, <br> | ||
we integrate resources to serve new era of innovation. Seeed also works with <br> | ||
global distributors and partners to push open hardware movement.<br> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#ifndef CAN_H_ | ||
#define CAN_H_ | ||
|
||
#include <stdint.h> | ||
|
||
|
||
typedef unsigned char __u8; | ||
typedef unsigned short __u16; | ||
typedef unsigned long __u32; | ||
|
||
|
||
/* special address description flags for the CAN_ID */ | ||
#define CAN_EFF_FLAG 0x80000000UL /* EFF/SFF is set in the MSB */ | ||
#define CAN_RTR_FLAG 0x40000000UL /* remote transmission request */ | ||
#define CAN_ERR_FLAG 0x20000000UL /* error message frame */ | ||
|
||
/* valid bits in CAN ID for frame formats */ | ||
#define CAN_SFF_MASK 0x000007FFUL /* standard frame format (SFF) */ | ||
#define CAN_EFF_MASK 0x1FFFFFFFUL /* extended frame format (EFF) */ | ||
#define CAN_ERR_MASK 0x1FFFFFFFUL /* omit EFF, RTR, ERR flags */ | ||
|
||
/* | ||
* Controller Area Network Identifier structure | ||
* | ||
* bit 0-28 : CAN identifier (11/29 bit) | ||
* bit 29 : error message frame flag (0 = data frame, 1 = error message) | ||
* bit 30 : remote transmission request flag (1 = rtr frame) | ||
* bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit) | ||
*/ | ||
typedef __u32 canid_t; | ||
|
||
#define CAN_SFF_ID_BITS 11 | ||
#define CAN_EFF_ID_BITS 29 | ||
|
||
/* CAN payload length and DLC definitions according to ISO 11898-1 */ | ||
#define CAN_MAX_DLC 8 | ||
#define CAN_MAX_DLEN 8 | ||
|
||
struct can_frame { | ||
canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ | ||
__u8 can_dlc; /* frame payload length in byte (0 .. CAN_MAX_DLEN) */ | ||
__u8 data[CAN_MAX_DLEN] __attribute__((aligned(8))); | ||
}; | ||
|
||
#endif /* CAN_H_ */ |
39 changes: 39 additions & 0 deletions
39
lib/lib_div/arduino-mcp2515-1.0.1/examples/CAN_read/CAN_read.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <SPI.h> | ||
#include <mcp2515.h> | ||
|
||
struct can_frame canMsg; | ||
MCP2515 mcp2515(10); | ||
|
||
|
||
void setup() { | ||
Serial.begin(115200); | ||
SPI.begin(); | ||
|
||
mcp2515.reset(); | ||
mcp2515.setBitrate(CAN_125KBPS); | ||
mcp2515.setNormalMode(); | ||
|
||
Serial.println("------- CAN Read ----------"); | ||
Serial.println("ID DLC DATA"); | ||
} | ||
|
||
void loop() { | ||
|
||
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) { | ||
|
||
Serial.print(canMsg.can_id, HEX); // print ID | ||
Serial.print(" "); | ||
Serial.print(canMsg.can_dlc, HEX); // print DLC | ||
Serial.print(" "); | ||
|
||
for (int i = 0; i<canMsg.can_dlc; i++) { // print the data | ||
|
||
Serial.print(canMsg.data[i],HEX); | ||
Serial.print(" "); | ||
|
||
} | ||
|
||
Serial.println(); | ||
} | ||
|
||
} |
Oops, something went wrong.