Building a CAN API for Arduino DUE

Nice thread! First time seeing it.

5 years in development eh… Thanks for posting. That’s a big contribution.

I will share anything I learn along the way. I need to implement on a 5V chip that is low cost so we will see how it goes. I have done many Arduino based designs but usually don’t release them out of embarrassment for lack of polish - or rather they are over-specific to whatever I am doing.

This one is prototype of Master in a distributed LTC based BMS with lots of competing requirements other than CAN functionality. Low BOM cost and 5V peripheral compatibility a must - so I will get to reading and see if this is an easy chore or complicated one.

Arduino FTW… I convert people all the time.

thanks,
-methods

One more think I met in sendFrame.
If it is set CANPort->setNumTXBoxes(3) then one call Can0.sendFrame(FrameToSend) is sending the three frames to bus.

It is possible I use not the last version of the sources.

There is no brake at !!! in code below.

bool CANRaw::sendFrame(CAN_FRAME& txFrame) 
{
...
    for (uint8_t mbox = 0; mbox < 8; mbox++) {
...
          result=true; //we've sent it. mission accomplished.
          !!!!
        }
...
}

Spasibo!

Yes, you're right. There is a missing break and it seems it has caused your problem. Thank you for reporting this. I've updated the code. Recently I merged in a lot of changes from other people and apparently it didn't all get tested thoroughly enough.

Hi, I've tried testing this library by uploading CAN_ExtendedPingPong to my due, and shorting CANRX with pin 53 and CANTX with DAC0. The pingpong doesn't seem to work, Could anyone know why?

Are you using transceivers or diodes ? Are you using the programming port or the native USB port ? What is exactly your wiring ?

ard_newbie: Are you using transceivers or diodes ? Are you using the programming port or the native USB port ? What is exactly your wiring ?

I am connected to the due via the programming micro USB, The wiring I've stated above are direct.. just cables. (CANTX0 to CANRX1 , CANTX1 to CANRX0)

For a correct wiring, use CAN Transceivers and two 120 Ohms resistors at each end of the bus or, as an alternative solution, diodes and a 2K2 resistor connected to 3.3V as explained in this Application Note from Siemens: https://www.mikrocontroller.net/attachment/28831/siemens_AP2921.pdf

Diodes are a perfect choice for testing, as long as your bus is no longer than a few meters (I tested with a 1.5 meter but others have been successful with this solution up to 4 meters) although this solution may not be sufficient in case there are lots of EMI.

Use a signal diode similar to 1N914 or 1N4148 and a 2K2 (or higher) resistor connected to 3.3 V ( Not 5V as mentionned in AN AP2921). The current should not go above 1.6 ma.

Since you are using the programming port, don't forget to comment line 22 in your sketch:

define Serial SerialUSB

ard_newbie: For a correct wiring, use CAN Transceivers and two 120 Ohms resistors at each end of the bus or, as an alternative solution, diodes and a 2K2 resistor connected to 3.3V as explained in this Application Note from Siemens: https://www.mikrocontroller.net/attachment/28831/siemens_AP2921.pdf

Diodes are a perfect choice for testing, as long as your bus is no longer than a few meters (I tested with a 1.5 meter but others have been successful with this solution up to 4 meters) although this solution may not be sufficient in case there are lots of EMI.

Use a signal diode similar to 1N914 or 1N4148 and a 2K2 (or higher) resistor connected to 3.3 V ( Not 5V as mentionned in AN AP2921). The current should not go above 1.6 ma.

Since you are using the programming port, don't forget to comment line 22 in your sketch:

define Serial SerialUSB

Why do I need to use a transceiver and diodes if I only want to test read/write capabilities of the library? I don't want to create my own can bus high/low, just check the tx/rx for software. The signals will be the same because the conversion the transceiver does will yield the same results to the tx/rx pins (am i right?)

Hello
I come to you because I buy a due and I try to make the can work. I do not get there

I realize two shields with SN65HVD234 and I connect them water and nothing happens and I even try to set to plug can1 and can2 directly I got inconsistent values

Here's a Due board with built-in CAN port.

http://store.evtv.me/proddetail.php?prod=EVTVDue2

Hello!
I tested a pair of program that transmit and receive the digital data of a photocell or in the other word send a byte through the CAN to the receiver part. It’s by SN65HVD230Q transceiver but my program is failed. I guess the transmission part is valid and the receive part is wrong. I tested the Tx pin of transmission part in the oscilloscope and saw the pulses with about 500ns width. I can’t get any answer in receiver part at all :frowning: . I had used MCP2551 for many times, has my CAN controller been failed because of this? :(((

my_receive.ino (295 Bytes)

my_trans.ino (339 Bytes)

Fyi - new how-to for message gateway/spoofing with the DUE. http://togglebit.net/how-to-gateway-messages/

Hi!
I want to Express my gratitude to the author of the theme. I was able to do the CAN gateway on the Arduino Due. The time of forwarding messages is very small. After the initial offset of 15-25 milliseconds, then the displacements are almost there (less than 1 millisecond). It fully complies with the requirements for test tool safety CAN interfaces in complex systems. Besides, I understand how you can still identify the hardware and software bookmarks, of which I warned on its website (in Russian) www.external.pro.

Alex

Hello! I need to learn about "filter( , , , )" command. Can you explain s'th or upload a pdf about that?

amin_mdn - this is a masking filter for ID's that will be "seen" and received by the CAN controller hardware...see page 1189 of the SAM3X / SAM3A Series datasheet, the masking is not simply explained. You can work out in either collin's due_can.cpp and/or CAN_Acquisition.cpp and work out how the masking is set. I have had inquires about opening the filters completely which is possible however you may run into overflow or overrun situations in doing so.

tbit thanks a lot.

amin_mdn:
Hello!
I tested a pair of program that transmit and receive the digital data of a photocell or in the other word send a byte through the CAN to the receiver part. It’s by SN65HVD230Q transceiver but my program is failed. I guess the transmission part is valid and the receive part is wrong. I tested the Tx pin of transmission part in the oscilloscope and saw the pulses with about 500ns width. I can’t get any answer in receiver part at all :frowning: . I had used MCP2551 for many times, has my CAN controller been failed because of this? :(((

hello guys!
I found commands that solve this problem! :
void setup()
{

Serial.begin(9600);

Can0.begin(CAN_BPS_250K);

int filter;
//extended
for (filter = 0; filter < 3; filter++) {
Can0.setRXFilter(filter, 0, 0, true);
}
//standard
for (int filter = 3; filter < 7; filter++) {
Can0.setRXFilter(filter, 0, 0, false);
}

}

But can any one say how does it solve my problem?

I used the “Link of latest version of Arduino DUE CAN library and examples:”

In file included from C:\Users\Administrator\Documents\Arduino\libraries\due_can-master\examples\CAN_ExtendedPingPong\CAN_ExtendedPingPong.ino:10:0:

C:\Users\Administrator\Documents\Arduino\libraries\due_can-master\src/due_can.h:23:24: fatal error: can_common.h: No such file or directory

#include <can_common.h>

Where can I find “can_common.h”

David

AllenDC:
I used the “Link of latest version of Arduino DUE CAN library and examples:”

In file included from C:\Users\Administrator\Documents\Arduino\libraries\due_can-master\examples\CAN_ExtendedPingPong\CAN_ExtendedPingPong.ino:10:0:

C:\Users\Administrator\Documents\Arduino\libraries\due_can-master\src/due_can.h:23:24: fatal error: can_common.h: No such file or directory

#include <can_common.h>

Where can I find “can_common.h”

David

Hello David,

There are a couple of lines in the README.md file:

The needed can_common library is found here:

Hello Everybody,

first of all thank you so much for that awesome work. Currently Im using this library for my bachelors. In this project a little part is to control a robot hand via CAN-Bus. First I only tried to sniff on the bus. This work awesome!

Now my next question is, if I would use the Arduino DUE as a Gateway (Read all Can Messages on CAN0 and write them on CAN1) and there would be over 300 Frames coming in with 10 ms, are the 84Mhz sufficient?

Could you help me doing the maths.

Thank you so much I love this place.

Greetings