1
- /* Copyright (C) 2014
1
+ /*
2
+ Copyright © 2007-2015 Fabian Greif, David Harding, Kyle Crockett,
3
+ Nuno Alves, Stevenh, Collin Kidder, Daniel Kasamis, Cory Fowler, teachop,
4
+ Pedro Cevallos, Neil McNeight
2
5
3
- Contributors: Pedro Cevallos & Neil McNeight
6
+ This file is part of CAN_Library.
4
7
5
- This program is free software: you can redistribute it and/or modify
6
- it under the terms of the GNU General Public License as published by
7
- the Free Software Foundation, either version 3 of the License, or
8
+ CAN_Library is free software: you can redistribute it and/or modify
9
+ it under the terms of the GNU Lesser General Public License as published by
10
+ the Free Software Foundation, either version 2.1 of the License, or
8
11
(at your option) any later version.
9
12
10
- This program is distributed in the hope that it will be useful,
13
+ CAN_Library is distributed in the hope that it will be useful,
11
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- GNU General Public License for more details.
16
+ GNU Lesser General Public License for more details.
14
17
15
- You should have received a copy of the GNU General Public License
18
+ You should have received a copy of the GNU Lesser General Public License
16
19
along with this program. If not, see <http://www.gnu.org/licenses/>.
17
20
18
21
Acknowledgements:
19
- Fabian Greif for the initial libraries for MCP2515, SJA1000 and AT90CAN
20
- http://www.kreatives-chaos.com/artikel/universelle-can-bibliothek
21
- as well as his updates at https://github.com/dergraaf/avr-can-lib
22
- David Harding for his version of the MCP2515 library
23
- http://forum.arduino.cc/index.php/topic,8730.0.html
24
- Kyle Crockett CANduino library with 16Mhz oscillator
25
- http://code.google.com/p/canduino/
26
- Nuno Alves for the help on Extended ID messaging
27
- Stevenh for his work on library and all of the MCP research/work
28
- http://modelrail.otenko.com/arduino/arduino-controller-area-network-can
29
- Collin Kidder (collin80) for his work on the Arduino Due CAN interface
30
- https://github.com/collin80/due_can
31
- Daniel Kasamis (togglebit) both for his code at
32
- https://github.com/togglebit/ArduinoDUE_OBD_FreeRunningCAN as well as his
33
- DUE CANshield http://togglebit.net/product/arduino-due-can-shield/
34
- Cory Fowler (coryjfowler) for 16 MHz bitrate timing information
35
- https://github.com/coryjfowler/MCP2515_lib
36
- teachop for the FlexCAN library for the Teensy 3.1
37
- https://github.com/teachop/FlexCAN_Library
22
+ Fabian Greif for the initial libraries for MCP2515, SJA1000 and AT90CAN
23
+ http://www.kreatives-chaos.com/artikel/universelle-can-bibliothek
24
+ as well as his updates at https://github.com/dergraaf/avr-can-lib
25
+ David Harding for his version of the MCP2515 library
26
+ http://forum.arduino.cc/index.php/topic,8730.0.html
27
+ Kyle Crockett CANduino library with 16Mhz oscillator
28
+ http://code.google.com/p/canduino/
29
+ Nuno Alves for the help on Extended ID messaging
30
+ Stevenh for his work on library and all of the MCP research/work
31
+ http://modelrail.otenko.com/arduino/arduino-controller-area-network-can
32
+ Collin Kidder (collin80) for his work on the Arduino Due CAN interface
33
+ https://github.com/collin80/due_can
34
+ Daniel Kasamis (togglebit) both for his code at
35
+ https://github.com/togglebit/ArduinoDUE_OBD_FreeRunningCAN as well as his
36
+ DUE CANshield http://togglebit.net/product/arduino-due-can-shield/
37
+ Cory Fowler (coryjfowler) for 16 MHz bitrate timing information
38
+ https://github.com/coryjfowler/MCP2515_lib
39
+ teachop for the FlexCAN library for the Teensy 3.1
40
+ https://github.com/teachop/FlexCAN_Library
38
41
39
42
-------------------------------------------------------------------------------
40
43
Change Log
41
44
42
- DATE VER WHO WHAT
45
+ DATE VER WHO WHAT
43
46
07/07/13 0.1 PC Modified and merge all MCP2515 libraries found. Stripped
44
47
away most unused functions and corrected MCP2515 defs
45
48
09/12/13 0.2 PC Added selectable CS SPI for CAN controller to use 1 IC
46
49
to control several mcp2515
47
- 02/05/14 0.3 PC Added filter and mask controls
50
+ 02/05/14 0.3 PC Added filter and mask controls
48
51
05/01/14 0.4 PC Cleaned up functions, variables and added message
49
52
structures for J1939, CANopen and CAN.
50
- 05/07/14 1.0 PC Released Library to the public through GitHub
51
- 06/18/14 1.9 NEM Preparing a unified CAN library across three different
53
+ 05/07/14 1.0 PC Released Library to the public through GitHub
54
+ 06/18/14 1.5 NEM Preparing a unified CAN library across three different
52
55
CAN controllers
56
+ 06/14/15 1.6.0 NEM Code cleanup and compatibility with Arduino 1.6.*
53
57
-------------------------------------------------------------------------------
54
58
55
59
*/
@@ -113,17 +117,16 @@ DATE VER WHO WHAT
113
117
//
114
118
typedef struct __attribute__ ((__packed__))
115
119
{
116
- uint32_t id : 29 ; // if (ide == CAN_RECESSIVE) { extended ID }
117
- // else { standard ID }
118
- uint8_t valid : 1 ; // To avoid passing garbage frames around
119
- uint8_t rtr : 1 ; // Remote Transmission Request Bit (RTR)
120
+ uint32_t id : 29 ; // if (extended == CAN_RECESSIVE) { extended ID } else { standard ID }
121
+ uint8_t valid : 1 ; // To avoid passing garbage frames around
122
+ uint8_t rtr : 1 ; // Remote Transmission Request Bit (RTR)
120
123
uint8_t extended : 1 ; // Identifier Extension Bit (IDE)
121
124
uint32_t fid; // family ID
122
- uint8_t priority : 4 ; // Priority but only important for TX frames and then only for special uses.
123
- uint8_t length : 4 ; // Data Length
125
+ uint8_t priority : 4 ; // Priority but only important for TX frames and then only for special uses.
126
+ uint8_t length : 4 ; // Data Length
124
127
uint16_t timeout; // milliseconds, zero will disable waiting
125
- uint8_t data[8 ]; // Message data
126
- } CAN_Frame; // suffix of '_t' is reserved by POSIX for future use
128
+ uint8_t data[8 ]; // Message data
129
+ } CAN_Frame; // suffix of '_t' is reserved by POSIX for future use
127
130
128
131
129
132
// From http://www.cse.dmu.ac.uk/~eg/tele/CanbusIDandMask.html
@@ -172,22 +175,27 @@ typedef struct __attribute__((__packed__))
172
175
// number of different tasks.
173
176
typedef struct __attribute__ ((__packed__))
174
177
{
175
- uint32_t id : 29 ; // if (ide == CAN_RECESSIVE) { extended ID }
176
- // else { standard ID }
177
- uint8_t rtr : 1 ; // Remote Transmission Request Bit (RTR)
178
+ uint32_t id : 29 ; // if (extended == CAN_RECESSIVE) { extended ID } else { standard ID }
179
+ uint8_t rtr : 1 ; // Remote Transmission Request Bit (RTR)
178
180
uint8_t extended : 1 ; // Identifier Extension Bit (IDE)
179
181
uint8_t data[2 ]; // Filter / Mask for message data
180
- } CAN_Filter; // suffix of '_t' is reserved by POSIX for future use
182
+ } CAN_Filter; // suffix of '_t' is reserved by POSIX for future use
181
183
182
184
183
185
class CANClass // Can't inherit from Stream
184
186
{
185
187
public:
188
+ // Initializes CAN communications.
186
189
virtual void begin (uint32_t bitrate);
190
+ // Finishes CAN communications
187
191
virtual void end ();
192
+ // Check if message has been received on any of the buffers
188
193
virtual uint8_t available ();
194
+ // Receive CAN message and allows use of the message structure for easier message handling
189
195
virtual CAN_Frame read ();
196
+
190
197
virtual void flush ();
198
+ // Load and send CAN message.
191
199
virtual uint8_t write (const CAN_Frame&);
192
200
193
201
// CAN_Frame& operator=(const CAN_Frame&);
@@ -199,8 +207,11 @@ class CANClass // Can't inherit from Stream
199
207
// Unable to use extern on a base class
200
208
201
209
// It's time for #ifdef bingo!
202
- #if defined(ARDUINO_ARCH_AVR) && !defined(__MK20DX256__)
203
- //
210
+ /* *********************************************************/
211
+ /* 8 bit AVR-based boards */
212
+ /* *********************************************************/
213
+ #if defined(__AVR__)
214
+
204
215
#if defined(__AVR_AT90CAN32__) || \
205
216
defined (__AVR_AT90CAN64__) || \
206
217
defined(__AVR_AT90CAN128__)
@@ -218,21 +229,34 @@ class CANClass // Can't inherit from Stream
218
229
#define CAN_CONTROLLER_MCP2515 // SPI interface to MCP2515 chip
219
230
#include < SPI.h>
220
231
#include " CAN_MCP2515.h"
221
- // #elif defined(SJA1000) // SPI interface to SJA1000 chip
232
+ // #elif defined(SJA1000) // 8-bit parallel interface to SJA1000 chip
222
233
// #define CAN_CONTROLLER_SJA1000
223
- // #include <SPI.h>
224
234
// #include "CAN_SJA1000.h"
225
235
// #else
226
236
// #error "Your SPI CAN controller is currently unsupported."
227
237
// #endif // MCP2515 / SJA1000
228
238
#endif // AT90 / M1 / C1 / SPI
229
239
230
- #elif defined(ARDUINO_ARCH_SAM) || defined(__SAM3X8E__) // Arduino Due
240
+ /* *********************************************************/
241
+ /* 32 bit Arduino Due */
242
+ /* *********************************************************/
243
+ #elif defined(__arm__) && defined(__SAM3X8E__)
231
244
#define CAN_CONTROLLER_SAM3X
232
245
#include " CAN_SAM3X.h"
233
- #elif defined(__MK20DX256__) // Teensy 3.1
246
+ /* *********************************************************/
247
+ /* 32 bit Teensy 3.0 and 3.1 */
248
+ /* *********************************************************/
249
+ #elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISK)
234
250
#define CAN_CONTROLLER_K2X
235
251
#include " CAN_K2X.h"
252
+ /* *********************************************************/
253
+ /* 32 bit Teensy-LC */
254
+ /* *********************************************************/
255
+ #elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISL)
256
+ // Assume an SPI interface to an MCP2515
257
+ #define CAN_CONTROLLER_MCP2515 // SPI interface to MCP2515 chip
258
+ #include < SPI.h>
259
+ #include " CAN_MCP2515.h"
236
260
#else
237
261
#error "Your CPU & CAN controller are currently unsupported."
238
262
#endif // ARDUINO_ARCH_*
0 commit comments