Building a CAN API for Arduino DUE

You're absolutely correct, it is empty. That's not where you get the can library from. You have two options:

Download the git version of the Arduino IDE and use the "can" branch.

Download my copy of the library and place it into your existing 1.5.2 directory where libraries for SAM go (hardware/arduino/sam/libraries).

The official Arduino IDE git repo is GitHub - arduino/Arduino: Arduino IDE 1.x
Mine is https://github.com/collin80/due_can

Pick your poison and download from one of those two places. It is probably easier to use my library both because the download will be a lot shorter and because it is the official version plus extra stuff I did thus it has more work done on it.

Thanks alot. I'll test it the next days.

Hello,

First, thank you all for your job, especially Palliser and AdderD !

I checked this topic from time to time and today I started to test the CAN on my Arduino DUE ! :slight_smile:

So I make here a first feedback of my first tests, in order to give some information and ask more questions...

First remark, I'm not a Github user, and I don't know how to download the CAN part of the official version, so I used the AdderD's version, which is available in a zip format (thanks).

So I just put the librairy in my directory and started to compile : OK.

About the hardware part, expecially the transceiver. I will also order the same SN65HVD234 as you, but at the right time I have one another kind of transceiver : MCP2551. It is a 5V transceiver, but I wanted to test it. its advantages : it is cheap and available in the "old fashion soldering", I don't know the word ("traversant" in french), I mean that you can easily solder it with you big figers and basic iron.

As it si 5V, of course it's better to put a Zener diode on the CanRx pin. And for the Tx, we hope that the 3.3v signal will be enough.

And the answer is.. yes, it seems to work fine.
With an oscilloscope I checked that 3.3v signal coming out of the Due is well transformed in differential tension.

Now, About first functionnal test.
My first test is the "Example1" delivered with AdderD librairy.
To understand a little bit more what I was sending I set CAN_MSG_DUMMY_DATA at 0.
So then I sent for example "250" from the serial port, and I got a communication with the result :
"CAN message received= 2500000000"

So display may be strange but it seems to work.

But I tried more values and I noticed that it works well only for value under 255... ?
Here is some examples of what I get :
250 => 2500000000
256 => 01000000
257 => 11000000
258 => 21000000
3000 = > 18411000000
65535 => 255255000000
65536=> 00100000

Do you have the same ? isn't it juste a problem of data interpretation or display?
I haven't yet looked insided the code, please give me your opinion about this.

Just a quick note to say I got my setup hooked up to the car today and it was working a treat. I've even got a nice interface in .NET at the other end to display the data. I'll try and get some pictures taken on Friday when I work on it again to share. :slight_smile:

Thanks for the hard work put in so far, and I look forward to seeing where this library goes in future!

Hello, new guy to the world of arduino and CAN. I apologize for the simple question, but using the example 1 from the zip file that Adder gave a link for a while back, I get "Type CAN message to send", but whatever I type nothing else happens. Apparently I never get into

if (Serial.read() == '\n') {
Serial.print("Sent value= ");
Serial.println(CAN_MSG_1);
}

Doesn't '\n' just indicate that the Return key was pressed? And actually with more testing, I realize the program gets to

while (!(CAN2.mailbox_get_status(0) & CAN_MSR_MRDY)) {
}

and then just waits forever. So I skip that if statement and go through everything till that while loop and just sit there. Could there be something wrong with the transceivers? I couldn't find a way to test that all initializing was actually taking place.

Thanks

Hello jspobuk,

I assume you are using Arduino IDE 1.5.2. and the wiring schematic shown in reply #33. If so,

  1. Make sure that the variant files (.c, .h) located in
    ...\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\variants\arduino_due_x\ contains the CAN0 and CAN1 pin definitions.
  2. Make sure you connected transceivers pins Rs to ground and EN pins to 3.3V.
  3. Make sure you connected CAN0_H with CAN1_H and CAN0_L with CAN1_L.

Please, keep us posted about it.

Wilfredo

Hello dammien,

I have not tried the CAN version you are using from AdderD.

I just tried the example 1 of my original library (published in github) with CAN_MSG_DUMMY_DATA equal to zero (as you did) and it runs OK, which means, if you send ABC you should receive ABC, or if you send 250 you should receive 250, or send 65536 should received 65536, etc...

It looks to me that what you are getting is the value you send but coded in HEX or so. Surely AdderD could explain better.

Anyway, I have attached for you my library below. If you can try it, let me know about how it goes.

-Wilfredo

CAN_API.zip (36.5 KB)

jspobuk: I don't want to sound like I'm stating the obvious, but since you say you're new to it then it may have been overlooked.... make sure the serial bitrate in your serial console is the same as the one in the serial.begin line. Without the code to hand I can't be certain, but I don't think there's any "halts" in the code before the first bit of code you post, and mismatched speeds could be a prime culprit there.

Sherlock3112:
jspobuk: I don't want to sound like I'm stating the obvious, but since you say you're new to it then it may have been overlooked.... make sure the serial bitrate in your serial console is the same as the one in the serial.begin line. Without the code to hand I can't be certain, but I don't think there's any "halts" in the code before the first bit of code you post, and mismatched speeds could be a prime culprit there.

Hi Sherlock
Do you have a project thread or some place you post about what your working on

I would like to follow your progress and see how you got it all working
What else do you have planned ?

Thanks
Allen

[General]
Hi,
I just found the CAN-bus example for the Atmel SAM3X-EK eval board in the Atmel ASF (similar uC, same CAN-module). There are four examples for sending data to the mailboxes toggling a led. Maybe this will help the starters to help for easier understanding.

DarkKnightWong:
Do you have a project thread or some place you post about what your working on

Nope - it's just a simple read CAN, write serial, let computer software make it pretty. Pretty much all the code for the first two parts can be found in the CAN examples palliser and adderd put out with their libs. The only thing left for you to do is put the CAN data into a format you'll be able to extract it at the other end of the serial interface.

There is a german project name microcopter, all parts are communicating via I2C. The copter community is so hot for CAN, I'm pretty sure if a copter with CAN-Aerospace on top is built the geeks will greedy snap for it.

Hi guys,

at the moment I'm testing several CAN examples (Adder + modified ASF example which is very similar). Does it work by simply crossing the lines?
CAN0 TX <-> CAN1 RX
CAN0 RX <-> CAN1 TX
Unfortunately I don't have a transceiver here.

I'm trying to listen to the data stream with a led (have no scope available at the moment), but even with lower speeds it seems the modules are still disabled. Wonder what's going on here.

Unfortunately I don't have a transceiver here.

It's a balanced, multi-drop, open drain link, I doubt it will work without transceivers.


Rob

Graynomad:

Unfortunately I don't have a transceiver here.

It's a balanced, multi-drop, open drain link, I doubt it will work without transceivers.


Rob

You are correct. It won't work without either transceivers or some extra EE magic to make the signals compatible. It's not worth it to try to doctor something up because transceivers are pretty cheap. It's all around best to just get the transceiver chips and use them.

Right, but after the physical layer with its differential lines we have plain 3.3V to the GPIO - which seems not to work at all. So I simply believe none of the TX lines is fire-breathing at the moment... bummer.

I've been loosely watching this forum but I don't see that anyone has built a "proper" CAN shield yet that comes populated with transceivers? Is that correct?

Hello tbit,

I have built a couple of DIY pseudo CAN shields that work 'properly'. You can find pictures of them in replies #34 and #61.
I am happy you asked the question because I would also like to see more shields and projects from other users. The people from Arduino, Atmel, the user AdderD and me have invested considerable time to generate a workable CAN API that must be exploited to the full. Arduino Due is now a good platform for automotive application developments. Hope you can joins us.

Regards,
Wilfredo

Palliser:

  1. Make sure that the variant files (.c, .h) located in
    ...\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\variants\arduino_due_x\ contains the CAN0 and CAN1 pin definitions.

Palliser,
Number 2 and 3 are an affirmitive, but can you explain in detail where these pin definitions should be at? I only found DAC0 and DAC1 pins along with the CANRX and CANTX in variant.h

Sherlock3112:
jspobuk: I don't want to sound like I'm stating the obvious, but since you say you're new to it then it may have been overlooked.... make sure the serial bitrate in your serial console is the same as the one in the serial.begin line. Without the code to hand I can't be certain, but I don't think there's any "halts" in the code before the first bit of code you post, and mismatched speeds could be a prime culprit there.

I checked, and both are running at 9600. So I believe that should be good. Thanks for the suggestion though, I am new so it's not that obvious. :slight_smile:

Thanks

Hello jspobuk,

Search for the following:

variant.c CAN Pin Descriptions

-Lines 234 and 235 contain CANRX0 (68) and CANTX0 (69)
-Lines 281 and 282 contain CANRX1 (88) and CANTX1 (89)
-Line 286 contains function call of all pins in CAN0 (90)
-Line 288 contains function call of all pins in CAN1 (91)

variant.h CAN Pin Definitions

  • Line 157 defines CANRX (68)
  • Line 158 defines CANTX (69)
  • Line 164 defines CAN1RX (88)
  • Line 165 defines CAN1TX (89)
  • Line 168 defines PINS_CAN0 (90)
  • Line 170 defines PINS_CAN1 (91)

There are other descriptions/definitions for the CAN transceiver pins but if your variant files contain the information above, you have the right variant files.