Building a CAN API for Arduino DUE

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.

Palliser - Thank you for the response. I completely agree, I have a bit of background in the automotive industry and see some real potential here with the DUE where the little 8 bit guys didn't quite cut it (especially without an internal controller and DMA).....i hope to contribute. I've just taken a first stab at a DUE protoshield and I plan on spinning a rev to include transceivers. I just ordered some 233's from TI.

Hi guys,
I adapted the ASF to the SAM8XE (I'm not a big afficionado of the Arduino IDE since it even doesn't has the modest capabilities of c&p-ing stuff from the output window so I use AS 6.0 instead). Still have no transceivers but at least a oscilloscope here...

=> Palliser:
Do you remember you changed some 'special' RX code from the 3X-EK lib for the Due platform? I did some debug stuff but at the moment I'm in a dead end.

At next CAN1_Handler(void) must bring g_ul_recv_status to log. 1 but no go.

Test1: CAN0 Mailbox 0 transmitting to CAN1 Mailbox 0

All registers in hex:

CAN0->CAN_MR: 1
CAN0->CAN_SR: 610001
CAN0->CAN_BR: 63233
CAN0->CAN_ECR: 0
CAN0->CAN_TCR: 0
CAN0->CAN_TIM: 93C0
CAN0->CAN_WPMR: 0
CAN0->CAN_WPSR: 0

Box 0:
CAN0->CAN_MB[0].CAN_MDL: 11223344
CAN0->CAN_MB[0].CAN_MDH: 55AAAA5
CAN0->CAN_MB[0].CAN_MCR: 0

CAN1->CAN_MR: 1
CAN1->CAN_SR: 610000
CAN1->CAN_BR: 63233
CAN1->CAN_ECR: 0
CAN1->CAN_TCR: 0
CAN1->CAN_TIM: E8B6
CAN1->CAN_WPMR: 0
CAN1->CAN_WPSR: 0

Box0:
CAN1->CAN_MB[0].CAN_MDL: 0
CAN1->CAN_MB[0].CAN_MDH: 0
CAN1->CAN_MB[0].CAN_MCR: 0

DEBUG: (1) g_ul_recv_status = 0 // => no receiving

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.
[/quote]

I apparently did not have the right variant files. I looked on github and just copied and pasted from the CAN branch updated by cmaglie. Is there other files I should be worried about? It's still only showing the initial "Type CAN message to send" with no response to what I type and send.

Thanks

Hello jspobuk,

Copy the variant files attached below (they are the same files in github CAN branch):

Then paste them to your Arduino files under the 'arduino_due_x' folder:

...\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\variants\arduino_due_x\

Let me know.

EDIT: These variant files should replace the current ones in your computer.

variant.cpp (22 KB)

variant.h (5.77 KB)