Skip to content

Commit 5c15968

Browse files
committed
Add driver
1 parent be2e3ba commit 5c15968

File tree

17 files changed

+3730
-0
lines changed

17 files changed

+3730
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
3+
4+
# Custom for Visual Studio
5+
*.cs diff=csharp
6+
*.sln merge=union
7+
*.csproj merge=union
8+
*.vbproj merge=union
9+
*.fsproj merge=union
10+
*.dbproj merge=union
11+
12+
# Standard to msysgit
13+
*.doc diff=astextplain
14+
*.DOC diff=astextplain
15+
*.docx diff=astextplain
16+
*.DOCX diff=astextplain
17+
*.dot diff=astextplain
18+
*.DOT diff=astextplain
19+
*.pdf diff=astextplain
20+
*.PDF diff=astextplain
21+
*.rtf diff=astextplain
22+
*.RTF diff=astextplain

hal/transport/CAN/driver/.gitignore

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#################
2+
## Eclipse
3+
#################
4+
5+
*.pydevproject
6+
.project
7+
.metadata
8+
bin/
9+
tmp/
10+
*.tmp
11+
*.bak
12+
*.swp
13+
*~.nib
14+
local.properties
15+
.classpath
16+
.settings/
17+
.loadpath
18+
19+
# External tool builders
20+
.externalToolBuilders/
21+
22+
# Locally stored "Eclipse launch configurations"
23+
*.launch
24+
25+
# CDT-specific
26+
.cproject
27+
28+
# PDT-specific
29+
.buildpath
30+
31+
32+
#################
33+
## Visual Studio
34+
#################
35+
36+
## Ignore Visual Studio temporary files, build results, and
37+
## files generated by popular Visual Studio add-ons.
38+
39+
# User-specific files
40+
*.suo
41+
*.user
42+
*.sln.docstates
43+
44+
# Build results
45+
[Dd]ebug/
46+
[Rr]elease/
47+
*_i.c
48+
*_p.c
49+
*.ilk
50+
*.meta
51+
*.obj
52+
*.pch
53+
*.pdb
54+
*.pgc
55+
*.pgd
56+
*.rsp
57+
*.sbr
58+
*.tlb
59+
*.tli
60+
*.tlh
61+
*.tmp
62+
*.vspscc
63+
.builds
64+
*.dotCover
65+
66+
## TODO: If you have NuGet Package Restore enabled, uncomment this
67+
#packages/
68+
69+
# Visual C++ cache files
70+
ipch/
71+
*.aps
72+
*.ncb
73+
*.opensdf
74+
*.sdf
75+
76+
# Visual Studio profiler
77+
*.psess
78+
*.vsp
79+
80+
# ReSharper is a .NET coding add-in
81+
_ReSharper*
82+
83+
# Installshield output folder
84+
[Ee]xpress
85+
86+
# DocProject is a documentation generator add-in
87+
DocProject/buildhelp/
88+
DocProject/Help/*.HxT
89+
DocProject/Help/*.HxC
90+
DocProject/Help/*.hhc
91+
DocProject/Help/*.hhk
92+
DocProject/Help/*.hhp
93+
DocProject/Help/Html2
94+
DocProject/Help/html
95+
96+
# Click-Once directory
97+
publish
98+
99+
# Others
100+
[Bb]in
101+
[Oo]bj
102+
sql
103+
TestResults
104+
*.Cache
105+
ClientBin
106+
stylecop.*
107+
~$*
108+
*.dbmdl
109+
Generated_Code #added for RIA/Silverlight projects
110+
111+
# Backup & report files from converting an old project file to a newer
112+
# Visual Studio version. Backup files are not needed, because we have git ;-)
113+
_UpgradeReport_Files/
114+
Backup*/
115+
UpgradeLog*.XML
116+
117+
118+
119+
############
120+
## Windows
121+
############
122+
123+
# Windows image file caches
124+
Thumbs.db
125+
126+
# Folder config file
127+
Desktop.ini
128+
129+
130+
#############
131+
## Python
132+
#############
133+
134+
*.py[co]
135+
136+
# Packages
137+
*.egg
138+
*.egg-info
139+
dist
140+
build
141+
eggs
142+
parts
143+
bin
144+
var
145+
sdist
146+
develop-eggs
147+
.installed.cfg
148+
149+
# Installer logs
150+
pip-log.txt
151+
152+
# Unit test / coverage reports
153+
.coverage
154+
.tox
155+
156+
#Translations
157+
*.mo
158+
159+
#Mr Developer
160+
.mr.developer.cfg
161+
162+
# Mac crap
163+
.DS_Store
13.1 KB
Binary file not shown.

hal/transport/CAN/driver/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
MCP_CAN Library for Arduino
2+
==============
3+
MCP_CAN library v1.5
4+
This library is compatible with any shield or board that uses the MCP2515 or MCP25625 CAN protocol controller.
5+
6+
This version supports setting the ID filter mode of the protocol controller, the BAUD rate with clock speed with the begin() function. Baudrates 5k, 10k, 20k, 50k, 100k, 125k, 250k, 500k, & 1000k using 16MHz clock on the MCP2515 are confirmed to work using a Peak-System PCAN-USB dongle as a reference. Baudrates for 8MHz and 20MHz crystals are yet to be confirmed but were calculated appropiately.
7+
8+
**The readMsgBuf() functions bring in the message ID. The getCanId() function is obsolete and no longer exists, don't use it.**
9+
10+
The readMsgBuf(*ID, *DLC, *DATA) function will return the ID type (extended or standard) and it will bring back the remote request status bit.
11+
If the ID AND 0x80000000 EQUALS 0x80000000, the ID is of the Extended type, otherwise it is standard.
12+
If the ID AND 0x40000000 EQUALS 0x40000000, the message is a remote request.
13+
14+
The readMsgBuf(*ID, *EXT, *DLC, *DATA) function will return the ID unaltered and doesn't inform us of a remote request.
15+
If EXT is true, the ID is extended.
16+
17+
The sendMsgBuf(ID, DLC, DATA) function can send extended or standard IDs.
18+
To mark an ID as extended, OR the ID with 0x80000000.
19+
To send a remote request, OR the ID with 0x40000000.
20+
21+
The sendMsgBuf(ID, EXT, DLC, DATA) has not changed other than fixing return values.
22+
23+
Using the setMode() function the sketch can now put the protocol controller into sleep, loop-back, or listen-only modes as well as normal operation. Right now the code defaults to loop-back mode after the begin() function runs. I have found this to increase the stability of filtering when the controller is initialized while connected to an active bus.
24+
25+
User can enable and disable (default) One-Shot transmission mode from the sketch using enOneShotTX() or disOneShotTX() respectively.
26+
27+
Installation
28+
==============
29+
Copy this into the "[.../MySketches/]libraries/" folder and restart the Arduino editor.
30+
31+
NOTE: If an older version of the library exists (e.g. CAN_BUS_Shield) be sure to remove it from the libraries folder or replace the files with those in this library to avoid conflicts.
32+
33+
34+
Help and Support
35+
==============
36+
This is primarily for non-bug related issues: Please start a *new thread* in an appropriate area at Seeedstudio forums or Arduino.cc forums and then send me (coryjfowler) a link through the PM system, my user name is the same as it is here. I will receive an email about the PM and generally get to it with-in a week or less. Keep in mind, I do this in my spare time.
37+
38+
39+
*Happy Coding!*
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* CAN Loopback Example
2+
* This example sends a message once a second and receives that message
3+
* no CAN bus is required. This example will test the functionality
4+
* of the protocol controller, and connections to it.
5+
*
6+
* Written By: Cory J. Fowler - October 5th 2016
7+
*/
8+
9+
#include <mcp_can.h>
10+
#include <SPI.h>
11+
12+
// CAN TX Variables
13+
unsigned long prevTX = 0; // Variable to store last execution time
14+
const unsigned int invlTX = 1000; // One second interval constant
15+
byte data[] = {0xAA, 0x55, 0x01, 0x10, 0xFF, 0x12, 0x34, 0x56}; // Generic CAN data to send
16+
17+
// CAN RX Variables
18+
long unsigned int rxId;
19+
unsigned char len;
20+
unsigned char rxBuf[8];
21+
22+
// Serial Output String Buffer
23+
char msgString[128];
24+
25+
// CAN0 INT and CS
26+
#define CAN0_INT 2 // Set INT to pin 2
27+
MCP_CAN CAN0(10); // Set CS to pin 10
28+
29+
30+
void setup()
31+
{
32+
Serial.begin(115200); // CAN is running at 500,000BPS; 115,200BPS is SLOW, not FAST, thus 9600 is crippling.
33+
34+
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
35+
if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
36+
Serial.println("MCP2515 Initialized Successfully!");
37+
else
38+
Serial.println("Error Initializing MCP2515...");
39+
40+
// Since we do not set NORMAL mode, we are in loopback mode by default.
41+
//CAN0.setMode(MCP_NORMAL);
42+
43+
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
44+
45+
Serial.println("MCP2515 Library Loopback Example...");
46+
}
47+
48+
void loop()
49+
{
50+
if(!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
51+
{
52+
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
53+
54+
if((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
55+
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
56+
else
57+
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
58+
59+
Serial.print(msgString);
60+
61+
if((rxId & 0x40000000) == 0x40000000){ // Determine if message is a remote request frame.
62+
sprintf(msgString, " REMOTE REQUEST FRAME");
63+
Serial.print(msgString);
64+
} else {
65+
for(byte i = 0; i<len; i++){
66+
sprintf(msgString, " 0x%.2X", rxBuf[i]);
67+
Serial.print(msgString);
68+
}
69+
}
70+
71+
Serial.println();
72+
}
73+
74+
if(millis() - prevTX >= invlTX){ // Send this at a one second interval.
75+
prevTX = millis();
76+
byte sndStat = CAN0.sendMsgBuf(0x100, 8, data);
77+
78+
if(sndStat == CAN_OK)
79+
Serial.println("Message Sent Successfully!");
80+
else
81+
Serial.println("Error Sending Message...");
82+
83+
}
84+
}
85+
86+
/*********************************************************************************************************
87+
END FILE
88+
*********************************************************************************************************/
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// CAN Receive Example
2+
//
3+
4+
#include <mcp_can.h>
5+
#include <SPI.h>
6+
7+
long unsigned int rxId;
8+
unsigned char len = 0;
9+
unsigned char rxBuf[8];
10+
char msgString[128]; // Array to store serial string
11+
12+
#define CAN0_INT 2 // Set INT to pin 2
13+
MCP_CAN CAN0(10); // Set CS to pin 10
14+
15+
16+
void setup()
17+
{
18+
Serial.begin(115200);
19+
20+
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
21+
if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
22+
Serial.println("MCP2515 Initialized Successfully!");
23+
else
24+
Serial.println("Error Initializing MCP2515...");
25+
26+
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
27+
28+
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
29+
30+
Serial.println("MCP2515 Library Receive Example...");
31+
}
32+
33+
void loop()
34+
{
35+
if(!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
36+
{
37+
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
38+
39+
if((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
40+
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
41+
else
42+
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
43+
44+
Serial.print(msgString);
45+
46+
if((rxId & 0x40000000) == 0x40000000){ // Determine if message is a remote request frame.
47+
sprintf(msgString, " REMOTE REQUEST FRAME");
48+
Serial.print(msgString);
49+
} else {
50+
for(byte i = 0; i<len; i++){
51+
sprintf(msgString, " 0x%.2X", rxBuf[i]);
52+
Serial.print(msgString);
53+
}
54+
}
55+
56+
Serial.println();
57+
}
58+
}
59+
60+
/*********************************************************************************************************
61+
END FILE
62+
*********************************************************************************************************/

0 commit comments

Comments
 (0)