Arduino USB/Serial to CAN Interface
Arduino C
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
usbcan Add sendStatus() function for troubleshooting and testing Aug 28, 2014
CANShield.sch
COPYING
README

README

This is an Arduino sketch and circuit description that allows you to use an Arduino
as a generic USB/Serial to CAN converter.

There is a schemtic of the CAN Shield that can easily be built on a proto shield
as well as the Arduino sketch.  To install the Arduino sketch just copy the whole
'usbcan' directory into your Sketchbook directory.  Check the preferences of your
Arduino software to determine where that is.  A better way to install it would be
a symbolic link to this repository, that way any changes that you make will be
reflected in both places.

This device doesn't include all of the capabilities of CAN.  Since the primary
focus of this effort is to lower the cost of entry for somebody to start doing
CAN-FIX development we only included the features that were necessary for
that purpose.  Remote and Extended frames were deliberately left out since they
are not utilized in CAN-FIX at this point.

The software is licensed under the GPL so if you use it for what you like.

If you are curious what CAN-FIX is you can get all the details on the 
MakerPlane forum here...

http://makerplane.org/forum/viewforum.php?f=24

To use the module you need to have a virtual serial port connection set up to your
Arduino.  If you were able to load the sketch is likely that you have that working
The general form of the command strings to the module are a single character
command code followed by the data (if any) that is required by that command and
then terminated with a linefeed <LF> (ASCII code 10 or 0x0A).

The response from the interface will either be the lower case version of the command
code or an '*' followed by an error code.

* Command Codes

Command            Code
-------------------------
Reset               K
Set Bitrate         B
Send Frame          W
Open Port           O
Close Port          C
Set Mask            M
Set Filter          F
Status              S
MCP2515 Raw Message Z
Help                H


* Error Codes

1 Malformed command string
2 Unknown bitrate
3 Transmit buffers full
4 Bad CAN identifier
5 Bad mask or filter index
6 Port must be open to send frames


* Reset Command
  host      K<lf>
  response  k<lf>

Sends a reset message to the MCP2515

* Set Bitrate Command
  host      Bxxxx<lf>
  response  b<lf>

x = the bitrate desired in kbps.  Currently the following are the only
allowed recognized commands

B125<lf>
B250<lf>
B500<lf>
B1000<lf>

* Send Frame Command
  host (standard)  Wiii:dddddddddddddddd<lf>
  host (extended)  Wiiiiiiii:dddddddddddddddd<lf>
  response   w<lf>

i..i = the CAN identifier in hexidecimal format.  Each dd is a byte of data to be
sent also in hexidecimal format if no data is to be sent the : should be 
followed by the <lf>.  The response is sent when the data is loaded into the 
transmit buffer.  If all the transmit buffers are full an error will be returned.

* Open Port Command
  host       O<lf>
  response   o<lf>

Puts the MCP2515 into Normal mode

* Close Port Command
  host       C<lf>
  response   c<lf>

Puts the MCP2515 into Configuration mode

* Set Mask Command
  host (standard)  Mx:iii<lf>
  host (extended)  Mx:iiiiiiii<lf>
  response   m<lf>

Sets the mask given by 'x' in the MCP2515 to the value given by i..i. 
All in hexidecimal format.  There are only two masks 0 and 1.  See the
MCP2515 datasheet for more information.

* Set Filter Command
  host (standard)  Fx:iii<lf>
  host (extended)  Fx:iiiiiiii<lf>
  response   f<lf>

Sets the filter given by 'x' in the MCP2515 to the value given by i..i.  All in
hexidecimal format.  There are six filters.  0 and 1 are associated with
mask 0 and 2-5 are associated with mask 1.  See the MCP2515 datasheet for more
information.

* MCP2515 Raw SPI Message Command
  host       Zdddddddddddd<lf>
  response   zdddddddddddd<lf>

Sends the data given over the SPI port and responds with the data that is read from
the SPI port.  No assumptions, validation or other logic is applied to this data.
This is for advanced users only and you should be familiar with how the MCP2515
SPI communication works.

* Status Command
  host        S[x]<lf>
  response    sdd...dd<lf> or a screenfull of information.

Retrieve status from the interface.  The 'x' in the request determines the status
entry to be read.  If it is absent then all the status is sent with human readable
descriptions.

The status codes are...
  1    Frames Sent      32 bits
  2    Frames Recieved  32 bits
  3    Buffer Overflows 16 bits
  4    Transmit Errors  8 bits
  5    Receive Errors   8 bits
  6    Error Flags      8 bits

The first three are calculated in the sketch by the Arduino.  The first two are
obvious, the buffer overflows represents the number of messages that have been
missed because they came in on the CAN-BUS faster then we could get them out
on the serial port.  The last three are read directly from the MCP2515 at the 
time the command is recieved.

* Help Command
  host        H<lf>
  response    <screen full of helpful stuff>

* CAN Frame Reception

When the device receives a message that passes through the acceptance filters and is
received it is sent to the host with the following format...

  (standard) riii:dddddddddddddddd<lf>
  (extended) riiiiiiii:dddddddddddddddd<lf>

...where i..i is the identifier of the CAN frame and each dd represents a byte of
data.  If there was no data the : would be followed by the <lf>

No indication is given of which filter match occured.  See the MCP2515 datasheet for
more information on how the acceptance filters work.