Arduino Forum

Products => Arduino Due => Topic started by: Palliser on Nov 07, 2012, 12:08 am

Title: Building a CAN API for Arduino DUE
Post by: Palliser on Nov 07, 2012, 12:08 am
I am starting this post upon recommendation of Massimo Banzi for channeling the efforts in this forum to contribute to complete the building of the CAN communication protocol interface of the Arduino DUE fully suported by Arduino APIs.

Just as most of you in this forum, I am new with Arduino DUE and with some background in integration, programming, electronics, computing, etc...   .
For the last three years I have been involved in a couple of projects interfacing CAN protocol between PCs and PLCs through gateways. The processors that handled the CAN code were all the same: ATMEL ARM AT91SAM7X256. In the same way as AT91SAM7X256, Arduino DUE's ATSAM3X8E contains a highly integrated peripheral set for connectivity and communication that includes dual CAN. So, I have planned, between sips of coffee, to to my best building the aforementioned interface.

I will start using the following hardware/software tools:

Arduino DUE
Atmel SAM3X-EK (ATSAM3X-EK)
SAM3X-EK CAN Demo Sources
SAM3X-EK CAN Demo Binaries
SAM3X-EK Demo Release Notes
AT91SAM CAN Bootloader User Notes
AT91SAM7X256_CAN_tes (666.73 kB) Sourcecodes download (if necessary)

Regards!

EDIT [01-08-15]:

Link of latest version of Arduino DUE CAN library and examples:

https://github.com/collin80/due_can (https://github.com/collin80/due_can)

(https://ibb.co/ejut6b)

Electric schematic Arduino Due - OBD 2 connector (https://ibb.co/ejut6b)
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: mbanzi on Nov 07, 2012, 11:06 am
Palliser

great job. I can create a repository for that library in our github and give you access.
Would that work for you?

m
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Nov 07, 2012, 01:17 pm
Hi Massimo.
Sure thing. Just PM me details. And you are welcome. That's why I love Open Source technology.
Palliser
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: superda on Nov 08, 2012, 01:45 pm
ciao! scusate ma io e l'inglese...non andiamo proprio d'accordo xD dopo traduco con Google ;)

...è da tempo che ho in mente di creare un computer di bordo in macchina, ma avendo l'Arduino UNO ho trovato molta confusione su shield che permettevano la connessione al CAN-BUS della macchina...vorrei sapere...dato che nell'Arduino DUE la connessione CAN è gia implementata all'origine...sarà fattibile come progetto??

Grazie a tutti anticipatamente =)

GOOGLE TRADUTTORE xD

hello! sorry but I and English ... we do not go quite agree xD after I translate with Google;)

has long ... I have in mind to create an on-board computer in the car, but having the Arduino UNO I found a lot of confusion shield that allowed the connection to the CAN bus of the car ... I want to know ... since CAN nell'Arduino DUE that the connection is already implemented as a project source ... it will be feasible?

Thank you all in advance =)
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: mbanzi on Nov 08, 2012, 01:59 pm
The Arduino Due has a can interface. at the moment the software API is not done yet.

Continue the discussion on the Italian forum.

----- Italiano ----

Ciao

L'Arduino Due ha l'interfaccia CAN ma mancano le API software. ci stiamo lavorando sopra.

Ti consiglio di postare la domanda sul forum italiano, questo è in inglese e diventa complesso gestire più lingue contemporaneamente

m
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: LROBBINS on Nov 09, 2012, 02:36 pm
Dear Palliser,

I am extremely, extremely enthusiastic about your building a CAN-bus interface.  I do have two suggestions that you might want to keep in mind while you are doing this:

(1) The most common use of CAN by Arduino-community members has been for reading on-board automotive systems, so the header files for libraries I've seen define those messages.  OBDC is only one use for CAN, and to my mind the least interesting use for those who want to do something new.  I think that it would be very useful for many, and certainly for me, if your interface provided a "friendly" implementation of CANopen.  (My particular interest in this is that recent Roboteq motor controllers are CAN capable and aside from raw CAN and a mini CAN subset, they implement CANopen (though only with 11 bit ids).

(2) The Due has two CAN controllers in its processor, but it still needs external transceivers.  The ATmega Arduinos need both external controllers and transceivers, but aside from the need to use SPI the CAN protocol is the same.  Perhaps your interface could be written so that it would work for both.  For example, I've just started working on a simple Arduino NANO CAN shield that mounts only a MCP 2515 and 2551 to use in several embedded CAN nodes.  Your interface will surely be much, much better than anything I will come up by hardcoding the frames I need into a modified version of an existing library.

Grazie,
Lenni
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Nov 09, 2012, 03:59 pm
Hello Lenni. Thank you very much for your encouraging words concerning this initiative to complete the newly created avenue to make Arduino Due to get seriously involved with the CAN standard.

In reference to your first suggestion, as too many of us know, CANopen is a higher level CAN bus protocol. Even thou both use the CAN 2.0B standard for the physical and data link layer, that's it. They differ from the other five layers in the OSI model. I am completely agree with you about implementing CANopen but we are in the incipient phase in building this interface, so, one step at a time. I believe the help from people like you will be needed very soon. I have planned to take advantage of what is inside the ATSAM3X8E, two can controllers that provide all the features required to implement the serial communication protocol CAN as referred by ISO/11898A that includes 1.0 Part A and 2.0 Part B.

In reference to your second suggestion, yes, it will be necessary a couple of transceiver like SN65HVD234 to provide the required transmission capabilities between the differential CAN bus and the CAN controller. I am taking notes of your comments and other comments from this community to keep in mind. Thank you again.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Markus_L811 on Nov 09, 2012, 04:44 pm

Hello Lenni. Thank you very much for your encouraging words concerning this initiative to complete the newly created avenue to make Arduino Due to get seriously involved with the CAN standard.

In reference to your first suggestion, as too many of us know, CANopen is a higher level CAN bus protocol. Even thou both use the CAN 2.0B standard for the physical and data link layer, that's it. They differ from the other five layers in the OSI model. I am completely agree with you about implementing CANopen but we are in the incipient phase in building this interface, so, one step at a time. I believe the help from people like you will be needed very soon. I have planned to take advantage of what is inside the ATSAM3X8E, two can controllers that provide all the features required to implement the serial communication protocol CAN as referred by ISO/11898A that includes 1.0 Part A and 2.0 Part B.

In reference to your second suggestion, yes, it will be necessary a couple of transceiver like SN65HVD234 to provide the required transmission capabilities between the differential CAN bus and the CAN controller. I am taking notes of your comments and other comments from this community to keep in mind. Thank you again.


I have interrests to, I would prefer the SN65HVD235 with the possibility off using the Autobaudfunktion without making trouble on the bus. The firm I worked for parttime has many CAN-Bus equipment so I can test.

Implementation of LIN and CAN Arduino Style for the Due is on my ToDo list to get "warm" with the Due.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Nov 09, 2012, 05:37 pm
Hello Markus,

I feel happier today because more people want to get involve with me in this journey. I just mentioned the SN65HVD234 as a known option, but agree with you about the autobaud loop-back and standby mode capabilities of the 235. I have also worked with SN55HVD251 and SN65HVD251, but at the end, its about a community-consensus with the permission of Arduino. So, I encourage you to move to the top of your ToDo list to find the best CAN transceiver option for the transmission between the Arduino Due processor (ATSAM3X8E) and the outer CAN world. Please, keep us posted about your tests and progress. Thank you.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: cyclegadget on Nov 09, 2012, 07:41 pm

I hope this will be helpful, it is a thread at LeafLabs about getting CAN to work with STM chips.

http://forums.leaflabs.com/topic.php?id=802
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Nov 10, 2012, 08:32 pm
Building a CAN communication protocol interface for Arduino Due (Scope)

Arduino introduces the Due board as a heavyweight bridge between mature Arduino tinkerers and advanced new cool open source applications to come. Focused more on expanding its elderly relative's platform's features and also focused in the outer world, Arduino Due with its 84 MHz clock, 512 KB of flash memory, 12 analog inputs (ADC), high-resolution analog outputs (DAC), 4 UARTs among other new features, it is capable of host our USB devices like mobile phones, cameras, printers, playback our wav and ogg music, and very soon…interact with our vehicles. And it is with this latter universe, that Arduino Due will make easier for us to materialize custom ideas using the communication Controller Area Network (CAN) protocol which rests inside, not only the most of modern vehicles, but everywhere.

This tutorial-post will take us to develop the working source code for the CAN protocol interface of Arduino Due supported by the Arduino API.

With your help and the help of Atmel and the Arduino team, I have planned to create a software foundation for the CAN platform using Arduino and Atmel development tools to design the C++ project to communicate with different intelligent CAN devices. This is made possible thanks to the dual CAN processors inside the Arduino Due platform based on Atmel ARM SAM3X processor.

I will start running and debugging sample CAN codes provided by Atmel that send/receive messages over CAN bus. In the meantime, we will use easy-to-read schematics and illustrations to explain in the most simple way, the structure of the CAN protocol and its several parts like configuration registers, message objects (mailboxes), etc.

Something very important: All the software we will be using is open and free to download (some free registrations may apply).

The CAN specification to be implemented in Arduino Due is defined by ISO/11898A (2.0 Part A and 2.0 Part B) standards for high speeds and ISO/11519-2 for low speeds.

Now, before start our first line of code, let's talk a bit about the Controller Area Network and a short walk through the history behind it. 

"Controller Area Network (CAN) is a network protocol that allows multiple processors in a system to communicate efficiently with each other. In the early 1980s microprocessors became small enough and powerful enough to start appearing everywhere, not just inside personal computers. The typical automobile built since 1985 has dozens of embedded processors. Electronic fuel injection systems have replaced carburetors. Anti-lock braking systems use fast microprocessors to make split second decisions, as do safety systems like air bags and seat belts. Other systems have built-in processors to work as sensors, keeping track of and reporting on temperature and pressure changes for the coolant system, transmission, and oil. Still others monitor emissions, steering performance, and brake fluid levels. As more and more electronic systems were added to automobiles and other machines to make them safer, more efficient, more reliable, and easier to maintain, it became increasingly important for these systems to communicate with each other. To this end the Controller Area Network protocol was introduced in 1983. Today Controller Area Network is the standard for high-speed, mission critical, real-time control networks in many machines. CAN chips are reliable, simple, and cheap. They last a long time and are able to endure temperature and pressure extremes".[1] www.parallax.com/dl/docs/prod/comm/cantechovew.pdf (http://www.parallax.com/dl/docs/prod/comm/cantechovew.pdf)

"Today, electronic components and systems account for over 20% of the cost of a high end passenger car. In-vehicle networks are already successfully applied to body and powertrain control systems. This figure is expected to grow significantly over the next ten years with the introduction increasingly more complex control systems such as Drive-by-Wire and multimedia systems giving access to the internet".[2] www.atmel.com/Images/doc7548.pdf (http://www.atmel.com/Images/doc7548.pdf)

"In February 1986 at SAE Conference in Detroit, Controller Area Network (CAN) was officially introduced by Robert Bosch GmbH in anticipation of future advances in onboard electronics".[3] http://www.aa1car.com/library/can_systems.htm (http://www.aa1car.com/library/can_systems.htm)   

"It was called the "Automotive Serial Controller Area Network" because CAN systems were
developed first for the automotive industry. It quickly became obvious, however, that the protocol would work extremely well in a wide variety of embedded systems applications. In 1990 CAN technology was introduced for weaving machines, and today the textile industry makes heavy use of CAN systems. CAN chips are found in elevator systems in large buildings, ships of all kinds, trains, and aircraft, in X-Ray machines and other medical equipment, logging equipment, tractors and combines, coffee makers, and major appliances. Today almost every
new car built in Europe has at least one CAN system, and CAN has become the industry standard for communications systems in vehicles. Industry leaders expect steady growth in CAN systems for all types of machines and equipment." [4] www.automationmag.com/.../network-evolution-25-years-of-connect... (http://www.automationmag.com/.../network-evolution-25-years-of-connect...)

Let me now turn to the two CAN Controllers contained in the SAM3X8E. Each CAN (Controller Area Network) is a stand-alone controller for the Controller Area Network widely used in automotive and industrial applications. Core has simple CPU interface (8/16/32 bit configurable data width) with little or big endian addressing scheme. CAN support both standard (11 bit identifier) and extended (29 bit identifier) frames. Hardware message filtering and 64 byte receive FIFO enables back-to-back message reception with minimum CPU load. This two CAN Controllers are able to handle all types of frames (Data, Remote, Error and Overload) and achieves a bit rate of 1 Mbit/sec. As regards CAN protocol, Arduino Due contain just two pins: CANTX and CANRX equivalent to the CAN_H and CAN_L known twisted wires.

Now, turning to substance, I have planned to implement three feasible approaches to build the CAN interface supported by the Arduino API. The first one is based on using the Atmel development kit board SAM3X-EK ($180) with Atmel SAM-BA software and a debugger like J-Link or SAM-ICE. Once defined and tested the sample CAN project, we will move to the second approach using Atmel Studio 6 (powered by Visual Studio) with Atmel Software Framework (ASF). Finally, having a proven CAN code, we will create the Arduino Due sketches in Atmel Studio 6.

So our first step will be with to port a very simple CAN example project for the SAM3X-EK board. I will test the original sample project connecting in loop the two CAN ports available in the SAM3X-EK board. Then, I will remove the 'jumper' between CAN ports and connect one of them with a real CAN device. As this project progresses we can find anything on the way. I mean, errors, bugs or something worst: nothing. And it is then that your full participation will take place. Anyone is free to participate with ideas and questions. Like the most of you, I have my work and set aside time to do this for free. Why? Because I love open source!

First approach:
To run the sample CAN project using the SAM3X-EK board, it will be required to load the CAN_CAN_Example1. Please, refer to the following replays on this post for further details.

Once this test is successfully finished, I will upload all the files in a dedicated repository in github.

Second approach:
We not only will run the same CAN_CAN_Example1 project using Atmel Studio 6 but also customized that sample to communicate with a real CAN device. For this, will be required to add some services/drivers/components from the ASF using ASF Wizard (By cross checking with the existing CAN_CAN_Example1 project for SAM3X-EK). Another changes will be necessary in the conf_uart_serial.h file (UART interface settings) and arduino_Due_x.h file (adding define CAN pins) before compile the project.

Similarly, once this test is successfully finished, I will upload all the files in a dedicated repository in github.

I have decided to explain in more detail the third approach a little later. As I quoted at the beginning of this scope, we will create the Arduino Due sketches in Atmel Studio 6.

In my next reply, I will present the hardware/software tools to be used to start with the first approach. regards!
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: that_kid on Nov 10, 2012, 09:32 pm
This is very exciting, I'm still learning about CAN and have just barely gotten my feet wet so I'm not sure how much I can contribute but I'm available. 
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: keija on Nov 12, 2012, 05:26 pm
Hi Palliser,

I'm very interested in your efforts of getting a CAN-Library working on the DUE.
I have been doing a few things with the CAN-Shield and an Arduino UNO and I know a lot about the CAN-Bus, so I will try to help you getting it done! Sad thing is:  I currently have other urgent projects/assignments that i need to address first! (And i need to get a DUE as well ;) )

I'm looking forward to your CAN-Bus library and I'll try to get involved, as soon as i have the time.
... Sam
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: mbanzi on Nov 12, 2012, 05:34 pm
Palliser

thanks for the exhaustive introduction. I would keep ASF out of this project, no need to add a million lines of code to access the CAN peripheral. We can extract the info from the relevant files.

Please leave Atmel studio 6 out of the development process, making the code available through the IDE is what we need in order to get many people to help out, test and use the code.

Let me know if we need to design a can shield.

keija: the library will be developed and made available here https://github.com/arduino-libraries/arduino-due-can join github if you want to be part.

m
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Markus_L811 on Nov 12, 2012, 07:29 pm

Palliser

thanks for the exhaustive introduction. I would keep ASF out of this project, no need to add a million lines of code to access the CAN peripheral. We can extract the info from the relevant files.

Please leave Atmel studio 6 out of the development process, making the code available through the IDE is what we need in order to get many people to help out, test and use the code.

Let me know if we need to design a can shield.

keija: the library will be developed and made available here https://github.com/arduino-libraries/arduino-due-can join github if you want to be part.

m


Hello Massimo,

you talk about the ASF, yes the CAN_CAN_EXAMPLE1 is heavy but all what's needed is there all files maybe some more than needed but the component_"what ever".h are from the AFS or not?

About the files where are the comments?

An Shield is absolutely nessesary an transciever chip is needed to get access to the CAN-Bus.

Markus
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Markus_L811 on Nov 12, 2012, 07:49 pm

Hello Markus,

I feel happier today because more people want to get involve with me in this journey. I just mentioned the SN65HVD234 as a known option, but agree with you about the autobaud loop-back and standby mode capabilities of the 235. I have also worked with SN55HVD251 and SN65HVD251, but at the end, its about a community-consensus with the permission of Arduino. So, I encourage you to move to the top of your ToDo list to find the best CAN transceiver option for the transmission between the Arduino Due processor (ATSAM3X8E) and the outer CAN world. Please, keep us posted about your tests and progress. Thank you.


Uhh Palliser you trapped in the same hole I was, the Series SN65HVD23x are in some way special.

SN65HVD233 has the Loopback funktion

SN65HVD234 has the Standby mode and the

SN65HVD235 has the Autobaud option,

you need to choose the Chipfunction you want, but the SAM supports Autobaud and Sleepmode only the Loopback funktion is missing.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Nov 12, 2012, 08:42 pm
hello Markus,
Let's go by parts:

About the CAN_CAN_EXAMPLE1, I would say don't worry.It is a generic CAN example project for SAM3X-EK board. This same sample was included with the latest ASF of Atmel Studio 6 and in the ASF standalone zip package (GCC & IAR). I decided to use it because I already have the kit board.

As I stated in my scope:

Quote
For this, will be required to add some services/drivers/components from the ASF using ASF Wizard (By cross checking with the existing CAN_CAN_Example1 project for SAM3X-EK).


I said some because that's what we need, just few libraries like can.h stdio.h, sysclk.h etc.These libraries were built by Atmel for the SAM3X platform, not for AS6.



About the github files, I haven't uploaded anything yet. I still running tests. And I agreed like the shield. Necessarily, a shield containing (among other things) similar transceivers should be considered for the Arduino Due. Which ones? I don't know yet but we'll find out soon. regards.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: mbanzi on Nov 13, 2012, 01:24 am
By bringing in the ASF you end up duplicating a lot of the functionality already present in the Arduino API.
in the end you need only 4 files  plus the example. replace the missing function with the corresponding arduino API and you're all set

m
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: fiddler on Nov 13, 2012, 08:05 am
I think its important that the Library be made so it can run as a background task in a scheduler.
Also that the auto transmit messaging be selected to be optional as sometimes you don't want it to keep transmitting.

K
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: keija on Nov 13, 2012, 11:31 pm
Hi,

i agree with the notion of making it work within the Arduino IDE without needing too many additional libraries.
I've been using the sparkfun shield and the accompanying library, and it provides the basic functionality of sending and receiving a CAN-Message. It gives you the message-ID and the data Bytes of a received message and requires the same for sending one. Additionally you can set the ID-Filters (although this isn't very intuitive ... and should definitely be improved, as it's the best way to take load off an Arduino UNO ;) )

I think this should be the basic foundation of the DUE-CAN library. Any additional features can be built on top of that in additional library files/layers, but should be modular (imho).


Let me know if we need to design a can shield.


That would be great! I don't know if its possible with the current pinout, but if you could implement a shield with two CAN-controllers, to interface with two independent CAN-Busses or to act as a gateway, it would be fantastic!


I'm excited about this development and I can't wait to see the first implementations!
... Sam
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Nov 14, 2012, 12:18 am
Thank you again keija for you willingness to help with this CAN interface for the Arduino Due. Remember that the ARM Cortex M3 is different from the 8-bit/16-bit RISC architecture  we are accustomed from Atmel and it confines any Due development to it. Right now I am finishing some CAN sample tests and minimizing/trimming all non-necesary drivers/services/components to make this project easier. Regards.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Nov 14, 2012, 06:55 pm
Ok. Change of plans. Henceforth I will stay working with the CAN code available through the Arduino IDE. At the end, a CAN library and a couple of sample sketches will be added to the Arduino IDE as tools to send and receive standard (ver.2.0A - 11 bit) and extended (ver.2.0B - 29 bits) messages, but before this, as I stated in previous posts, I have been running some tests of the sample CAN code provided by Atmel in AS6, SAM_BA 2.12 and my SAM3X-EK board. The purpose of these tests is to dissect and obtain a proven minimum CAN code for the SAM3X8E. Finally, once the necessary source code has been chosen (libraries, .h, .c), we will proceed to program the SAM3X8E in Arduino due through the Arduino IDE.

I know the majority of the CAN protocol fans in this forum look forward to have access to the code but for the moment let me show you a picture my SAM3X-EK and a CAN device (sorry for the wiring mess) and also a pseudo-code and an idealized sketch.

(http://img5.imageshack.us/img5/4373/cantest1.jpg)

For understand a bit better the following pseudo -code, it is healthy to explain briefly about the CAN platform embedded inside the SAM3X8E core.
The SAM3X8H has two CAN Controllers. We will call these controllers CAN0 and CAN1. The SAM3X8H read/writes from/to them through the PIO A and PIO B respectively. Each of these controllers connects one respective bus. Each bus should comprehends only two transmission lines (CANTX/CANRX) but due to the energy efficiency design of the SAM3X8E, it is required the use of two CAN transceivers (one per bus) between the SAM3X8E and the outer world. Also, we will discuss latter about these transceiver and the future shield.
Here the pseudo-code:
Initialization code (setup)

1.   Define CAN0 and CAN1 transfer mailbox structure
2.   Enable the module clock for CAN0 and CAN1
3.   Initialize CAN0 and CAN1, baud rate (baud/s)
4.   Reset all CAN0 and CAN1 mailboxes
5.   Initialize CAN1 mailbox 0 as receiver, frame ID
6.   Initialize CAN0 mailbox 0 as transmitter, frame ID
7.   Prepare transmit ID, data and data length in CAN0 mailbox 0

Main code (loop)

1.   Send out data in CAN0 mailbox 0
2.   Wait for CAN1 mailbox 0 to receive the data
3.   Read the received data from CAN1 mailbox 0

For a user-friendly interface, I will encapsulate the initialization code in a couple of steps that only include baud rate, frame ID, priority, type of message frame (2.0A or 2.0B) and mailbox configuration(transmitter/receiver).

The main code will use the following functions: send, wait and read.
An idelized Arduino Due CAN sketch will look like:

Code: [Select]

// Idealized CAN sketch for Arduino Due board
// By Palliser 2012


// Required CAN library
#include <can.h>

// Initialize CAN Controllers (defines structure, enable clock)
CANInit CAN0;
CANInit CAN1;

// Mailboxes frame ID 7 and 8
int MB_ID7 = 7;
int MB_ID8 = 8;

// CAN message to turn on Arduino Due pin 13
int CAN_MSG_HIGH_PIN_13 = 0x11223344
// Arduino Due pin 13
int led13 = 13;

void setup()
{
  //CAN baud rate 1Mb/s
  Can.begin(1000000);
  //Reset all CAN0 and CAN1 mailboxes
  Can.MBReset(CAN0);
  Can.MBReset(CAN1);
  //Init CAN0 mailbox 7 as receiver
  Mailbox.mode(CAN0, MB_ID7, RX_Mode);
  // Init CAN1 mailbox 8 as transmitter
  Mailbox.mode(CAN1, MB_ID8, TX_Mode);
  // Prepare transmit information into CAN1 mailbox 8
  CAN_MB_WRITE(CAN1, MB_ID8, CAN_MSG_HIGH_PIN_13);
  // initialize the digital pin as an output.
  pinMode(led13, OUTPUT);
}

void loop()
{
 
  // Send out information in CAN1 mailbox 8
  CAN_MB_SEND(CAN1, MB_ID8);
 
  while(!recv_status){
  }
  if(CAN0_MB_DATA == CAN_MSG_HIGH_PIN_13){
  Serial.println("Test passed");
  digitalWrite(led13, HIGH);}
  else{
  Serial.println("Test ERROR"); 
  }
}


Regards.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Markus_L811 on Nov 14, 2012, 07:13 pm
I like the wiring, sounds good looking forward to it
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Nov 17, 2012, 02:43 am
I'm almost done with my minimize raw CAN 2.0A (11 bit) running in the Arduino Due with a sample sketch. I'm expecting to receive the transceivers in a couple of days (TI SN65HVD234 & SN65HVD235) and build a shield. In the meantime I've hooked CANRX0<->CANRX1 and CANTX0<->CANTX1 for looping tests. I hope by mid-next week to upload in github the raw CAN code for revision and comments. Thank you for your patience.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: retrolefty on Nov 17, 2012, 03:30 am
Quote
I think this should be the basic foundation of the DUE-CAN library.


Oh please, don't miss the opportunity of calling it the CAN-DUE library, we need more nerd humour around here. LOL  :D

Lefty
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: keija on Nov 18, 2012, 12:59 am
"No CAN due-sville, babydol" ?

Wow Palliser - you seem to be really focused on this :O thanks for spearheading!
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: giants_fan3 on Nov 18, 2012, 04:58 am
I haven't heard of CAN protocol until reading this thread, so I am wondering (just in case I'm missing out on learning some interesting things):
What is "CAN protocol" used for? Automotive applications were mentioned (so I assume car odometer, engine status, etc.), but what other applications can it be or is commonly used for?
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Nov 18, 2012, 07:14 am
Hello giantsfan3. In my case, I started getting interested in the CAN protocol after been involved for over a year integrating a German Battery Manager System (CAN) with a PLC (Modbus RTU). Then, I understood why CAN is one of the most reliable communication protocols I ever seen. In the particular case of the microcontroller inside of the Arduino Due (SAM3X8E), there are about one hundred configurable registers! (status, interruptions, errors, etc.). The CAN protocol has been around for more than 25 years. Just two wires, it offers high-speed communication rate up to 1 Mbits/sec thus allows real-time control. CAN can theoretically link up to 2032 devices (assuming one node with one identifier), excellent error handling and fine fault confinement. Used to operate in robust, electromechanical (noisy) environments like in automotive, also in medical apparatus, textile machines, elevators, agricultural and nautical machinery. There are other CAN-based higher layer protocols like CAN Application Layer (CAL), CAN Kingdom, CANopen and DeviceNet.

Just Google it or Wikipedia. You can check the following compendium of the CAN protocol and uses in the automation industry.
http://www.diakom.com.ru/el/communication/can/can_org.pdf (http://www.diakom.com.ru/el/communication/can/can_org.pdf)  

We hope soon, see Arduino Due fans contributing with new cool CAN applications. Regards.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Nov 25, 2012, 03:16 pm
Implementing the CAN physical layer.  

For the implementation of the physical layer of the Arduino Due CAN interface, we need a mean to translate the CAN messages to/from differential signals across a physical medium such as a twisted pair cable.

CAN transceivers provide that differential physical layer interface between the data link layer (CAN controller inside the SAM3X8E) and the physical wiring of the CAN bus.

CAN has only two bus voltage states; recessive (driver outputs are high impedance) and dominant (one bus line, CANH, is high and the other, CANL, is low), unlike the traditional differential data transmission where a logic 1 is transmitted as a voltage level high on one noninverting transmission line and low on the inverting line. Correspondingly, Logic 0 is transmitted as low on the noninverting line and high on the inverting line.

For obvious space reasons, we need a shield to mount the transceivers, some pull resistors, capacitors and the CANL CANH connectors.

I have chosen two SN65HVD234 CAN transceivers for my first shield (I am considering to use 235's later). Because I am prototyping, for the board I will use a SchmartBoard (RadioShack 2760259). The shield will provide 2 can ports (CAN0 and CAN1).

Here a picture of the transceivers circuit. It's very simple, just a  straight connection with the arduino CAN ports. Later on, I have planned to use some features of these transceivers like high-speed and low power modes that will required 4 more Arduino Due pins (2 EN and 2 RS).

(http://img811.imageshack.us/img811/4198/wrong1.jpg)

Regarding the code, I already have my Arduino Due talking CAN 2.0A but a library optimization is required before upload the files in github. Thank you again for your patience and keep looking out my post.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: roundhouselabs on Nov 26, 2012, 09:04 pm
Hello,
"Long-time listener/first-time caller" here...  I just wanted to point out an interesting discussion regarding use of an Arduino-compatible 32-Bit board (the ChipKIT) and a CAN Communication Interface for the Ford Motor Company called OpenXC (http://openxcplatform.com/) (http://openxcplatform.com/)).  The discussion forum can be found at https://groups.google.com/forum/?hl=en&fromgroups#!forum/openxc (https://groups.google.com/forum/?hl=en&fromgroups#!forum/openxc)
  I have no idea if there's any way these two efforts can be of use to each other, but I figured I'd mention it...
Keep up the exciting work!
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Nov 26, 2012, 10:03 pm
Hello roundhouselabs and welcome to the forum. Thank you for the information. So far, my Arduino Due can transmit/receive CAN 2.0A messages within the Arduino API. I used an Atmel sample code for the SAM3X series. Right now, I am organizing the library and replacing missing functions presents in Atmel but in the Arduino API and hope to release them with a couple of sample sketches in github in the coming days for revision and comments. As you may have noticed, a CAN shield has been required. Regards!
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Nov 27, 2012, 12:06 am
Here a picture of my Arduino Due CAN shield (prototype) during loop tests. I will mount the resistors in the shield (less power of course!)

(http://img39.imageshack.us/img39/4830/candue1.jpg)
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: peplin on Nov 27, 2012, 03:53 am
I'm excited to see work with CAN on the Arduino Due - I'm one of the developers of the OpenXC platform that roundhouselabs mentioned (openxcplatform.com) and while we started prototyping with the Arduino at Ford, we had to move to the chipKIT to get a little bit more performance and CAN controllers on the MCU. I'd love to get the OpenXC translator working with the Due; all we really need is the library API - I read through this thread but I can't quite tell if there is progress happening somewhere else on the software side.

If there's a work-in-progress repository somewhere, I'd love to help out. Looking for examples of simple CAN APIs that would exist at about the same level of abstraction as the other core Arduino libraries, the one provided by the mbed library may be a good start: http://mbed.org/handbook/CAN
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Nov 27, 2012, 05:20 pm
CAN Shield - Due electrical schematic.

(http://s2.postimg.org/n7rzwqurd/Transceivers_Due_CAN.jpg)
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Nov 28, 2012, 06:20 pm
Arduino Due and CAN shield with termination resistors (120 ohm) mounted. The jumpers are selecting high-speed (0V-black) and disabling low-power (3V3-red) modes in the SN65HVD234 transceivers. The CANH (orange) and CANL (purple) bus terminals hooked for a loop test.

(http://img33.imageshack.us/img33/3963/duecanshield2.jpg)
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: keija on Nov 29, 2012, 02:50 pm
I was able to order a DUE today, so I'll join the ranks soon :)

I really appreciate it, that you use both CAN-Transceivers! This will be great for using it as a Gateway!
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: that_kid on Dec 03, 2012, 12:58 am
I have my Due and now it's time to order the transceivers.  I'm going to get both the 234 and 235's.  Which ever ones I don't use in the car I'll use on the work bench.  Thanks for tackling this.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Dec 03, 2012, 01:49 am
Great for you keija and that_kid. Do as I did: Request a couple of samples of each transceiver to Atmel. I have some sample sketches running OK on my Due, using a combination of Atmel and Arduino library files. The goal is to occupy as much as Arduino files and as less as Atmel files as possible. I also hope to run my first real test these next days interfacing Due with a battery manager system (with XC167CI - by Infineon and AT91SAM7).
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Dec 04, 2012, 06:07 pm
I wanted to share another of the tests I am working now with Arduino Due using the CAN shield. The test consists of making Due read CAN data from a commercial OBD II code reader. This will prepare the ground for more tests like convert Due as a OBD2 simulator system generating some DTC codes to be read by the code reader and finally to make Arduino Due read some real DTC codes from my car (Honda-Oddysey 2002) or any other car under OBD2.

As some of you know, ODB II stands for On Board Diagnostics, level 2. OBD II is a federally mandated engine & emissions management standard for all passenger cars sold in 1996 and beyond. ODB II uses CAN bus Dual Wire like Arduino Due (ISO 11898-1/SAE J2284).

For my first test I used an Actron CP9125 pocket scanner (with a 89C71CC01CA microcontroller and TJA1050 CAN transceiver).

Here a the schematic of interconnection between the code reader and Arduino Due and a picture of both systems. So far, Due can read from the reader ID 0x07DF the following data: 02 01 00 99 99 99 99 99 which is a request data to start reading DTC codes. I am very excited exploring CAN protocol in Arduino Due and OBD2 systems. I'd recommend to get ready if you want to embark on this journey.

(http://img24.imageshack.us/img24/5060/canobd22.jpg)

(http://img600.imageshack.us/img600/167/duecanobd2scanner.jpg)
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: that_kid on Dec 04, 2012, 08:07 pm
Excellent news, I'm just waiting on my parts to arrive from mouser.com  I was going to order the engineering samples but I needed some parts so I just went ahead and get it all.  Next stop is to see if my local radio shack has the shield that you are using. 
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: downtown_sausage on Dec 05, 2012, 03:58 pm
Hey guys. I'm interested in getting on board with the CAN protocol as well. I've done a lot of low level CAN drivers on the pic24, dspic, and pic32 along with the MCP2551 transceiver at work. I have a DUE coming in next week and just put in my order for TI samples of the two transceivers mentioned earlier in this thread (234 and 235).
What is the scope of this CAN library? I'd be more than willing to work the low level driver stuff and different implementations of it (interrupt based, polling based, different modes).
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Dec 05, 2012, 04:57 pm
Hello downtown_sausage and welcome aboard! We are going to need your CAN skills soon. So far I have Due running CAN 2.0A but with a mixed Arduino-Atmel library. I hope to release a couple of simple sample sketches and a tentative CAN library in github very soon. I am following guidelines/advices from the Arduino team and Atmel but everybody is welcome here. The scope: once released the CAN library, we'll need as much people as possible to be involved and contribute with ideas, applications, improvements, fixing bugs, etc... remember, this is the first Arduino board with embedded CAN controllers: just transceivers between the board and real CAN device, so, a new Arduino Art is oncoming and a lot of work to do. Thanks!
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: peplin on Dec 08, 2012, 08:20 pm
Palliser, you might consider doing your development in the open on GitHub, even before you think it's at a good draft stage. No reason we can't start helping you out already!
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Dec 08, 2012, 10:10 pm
Thank you peplin again for your interest in help with the CAN API development. Please, have just a little bit of patience. I hope to upload the CAN library and sample sketches on the next days. I am waiting on answers/revisions by Atmel/Arduino. And as I have said, try to build or have ready you own CAN transceivers shield; otherwise, it would be virtually impossible and even dangerous to try any communication test without it.

Like you and other Arduino users, I am more than willing to see a CAN API ready and all the discussion to come.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: downtown_sausage on Dec 13, 2012, 06:08 pm
Palliser, are you developing the base CAN drivers based on Atmel example code at the moment? My plan was to write the CAN drivers from scratch, with different configurations options. After that is up and working, integrate that code into an Arduino library.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Dec 13, 2012, 07:35 pm
Hello Downtown_sausage, Yes. I wrote two sample sketches based on an Atmel sample for the SAM3X-EK. Currently, I am receiving direct support from Atmel/Arduino. They are adjusting some path needs and setting the correct dependencies before releasing it on github (Arduino libraries). At the end, all the drivers (including the transceiver one) should be into libsam of future IDE releases. Regarding your excellent initiative to build the driver, I don't know how easy could be to start from scratch a can driver for the SAM3X8E. The Atmel CAN controllers are hard-to-tame for me if compared with other (remember that there are a lot of MCUs out there with Cortex-M3 core and embedded CAN controller like STM32F10XX). It requires an orchestrated initialization between PMC, AIC, CLK and CAN controllers. The main idea is to build a consistent CAN library to take advantage as much as possible of features like interrupt/message handling, timing, wakeup/sleep, producer/consumer modes, etc... It's about a short time to have available the library but....it's up to you, feel free to contribute with your skills. And thank you!
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: downtown_sausage on Dec 13, 2012, 11:20 pm
I see. I haven't looked at the Atmel CAN example drivers yet, but from my experience, chip manufacturer's code is generally pretty rubbish. If we want legitimate CAN applications for the Arduino, we will need to have an extremely solid base driver interface to work off. Don't want to build a house on a weak (inefficient) foundation.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: downtown_sausage on Dec 13, 2012, 11:36 pm
To clarify my last post, I suggest we don't use Atmel's CAN drivers and instead work them from scratch. But if they are really good, then no need to mess with them. In reality, it will probably be a task of looking what they did and seeing if we can improve upon their driver implementation. If it's anything like the CAN processing on the PIC24/PIC32, you can probably offload much of the CAN receiving messages to a DMA channel, which will be key for efficiency. This could be done with TX too, but may not benefit too many applications. I should probably start reading the Atmel uC manual to see how they implement this stuff.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: mbanzi on Dec 14, 2012, 11:18 am
downtown_sausage we're working directly with the ATMEL engineer who wrote the examples in the first place. I suggest you coordinate with Palliser who we have put in touch with the engineer. there is a good chance to get the info straight from the source.

m
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Dec 16, 2012, 11:26 pm
For a better understanding of the Atmel CAN controllers inside the SAM3X8E, I have dissected the initialization procedure required to let the

CAN controller interact (synchronize/listen) with the real world (CANH/CANL).

Document Sources:
- Atmel doc 11057.pdf
- Can Library from Atmel ASF.

System controllers used:
- Power Management Controller (PMC)
- Nested Vectored Interrupt Controller (NVIC)
- CAN Controller (CAN)

CAN initialization procedure:

1. CAN controller -> DISABLED after power-up reset.
2. CAN controller clock -> ENABLED by PMC (via Low-Power Peripheral Bridge) for CAN peripheral IDs 43 (CAN0) and 44 (CAN1).
3. CAN controller interrupt line -> ENABLED for mailbox X (API definition - CAN_IER_MBX) through CMSIS definitions (CANx_IRQn) in the embedded NVIC (NVIC_EnableIRQ).
4. CAN controller initialized
  4.1. CAN_BR register (baudrate) -> SET. The following steps are performed:
       4.1.1 Check whether the baudrate prescale will be greater than the max divide value.
       4.1.2 Check whether the input MCK is too small.
       4.1.3 Initialize it as the minimum Time Quantum.
       4.1.4 Initialize the remainder as the max value. When the remainder is 0, get the right TQ number.
       4.1.5 Find out the approximate Time Quantum according to the baudrate.
       4.1.6 Calculate the baudrate prescale value.
       4.1.7 Get the right CAN BIT Timing group.
       4.1.8 Before modifying the CANBR register, disable the CAN controller.
       4.1.9 Write into the CAN baudrate register.

  4.2. 8-CAN message mailboxes -> RESET. (CAN_MB_DISABLE_MODE) The following steps are performed:
       4.2.1 Check the object type of the mailbox. If it's used to disable the mailbox, reset the whole mailbox.
       4.2.2 Set the priority in Transmit mode.
       4.2.3 Set the message ID and message acceptance mask for the mailbox in other modes.
       4.2.4 Set up mailbox in one of the five different modes.
  4.3. Configure a mailbox in reception (or consumer) mode  
  4.4. CAN autobaud listen mode -> SET in CAN_MR register (CAN_MR_ABM == 1)
  4.5. CAN controller -> SET CANEN in CAN_MR register (CAN_MR_CANEN == 1)
  At this stage, the internal CAN controller state machine is reset, error counters are reset to 0, error flags are reset to 0 and starts bus synchronization automatically by
  scanning eleven recessive bits an then start listening to the network (real world).

I want to clarify that, even though the above initial procedure it might look over-elaborated, the nature of the CAN controller so requires. We, mortal thinkerers are used to a higher level language. My aim has been to bring here part of what's cooking regarding the CAN API, now in progress. But don't worry; at the end, a simpler solution to this whole approach will emerge.  

Note: The CAN interruptions in SAM3X8E are implemented through CMSIS definitions in the embedded NVIC inside Cortex-M3 (Not from not existent AIC controller). I got confused because it would seem that some of the CAN controller sections in the SAM3X8E manual (doc11057) were from copy&paste sections of the SAM7X manual. The explanation could be that CORTEX-M3 inherited part of its functionality from the venerable ARM7.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: that_kid on Jan 04, 2013, 04:58 pm
Hi Palliser, any progress on this?
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Jan 04, 2013, 05:06 pm
hello that_kid and thank you for ask.

For all those who have followed this post, I would like to inform you that we still working very hard building the API and runing tests, with the valuable help of Atmel. For obvious reasons (Christmas and New Year) we had few latencies but I hope that, as soon as possible, we will publish the library and sample sketches in github. On the other hand, I think it is timely and appropriate, to see from those that already have built their DIY pseudo CAN shields, to show us their progress. Thank y'all again for your patience.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: that_kid on Jan 04, 2013, 05:24 pm
Thanks for the update, I have my samples in but now I need to get a board to solder them to.  My goal is to make my own board but that won't happen until I recover from my broken foot.  Meanwhile I'm getting everything that I can ready to help with the testing.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Jan 05, 2013, 06:14 pm
Hi that_kid. I am sorry to know about your broken foot. Just take it easy. Even thou I have my shield working OK, I still looking for a better out-of-the-shelf board that fits better the design. I am also working on some eagle files to build, at some time, my own design. Remember that for the two transceiver we are going to need 8 pins of the Arduino DUE (CANRX0, CANTX0, CANEN0, CANRS0, CANRX1, CANTX1, CANEN1, CANRS1). For my tests, I didn't use the EN and RS pins. I just connected them to +3.3 and ground respectively.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: zabaat on Jan 07, 2013, 11:22 am

Hi that_kid. I am sorry to know about your broken foot. Just take it easy. Even thou I have my shield working OK, I still looking for a better out-of-the-shelf board that fits better the design. I am also working on some eagle files to build, at some time, my own design. Remember that for the two transceiver we are going to need 8 pins of the Arduino DUE (CANRX0, CANTX0, CANEN0, CANRS0, CANRX1, CANTX1, CANEN1, CANRS1). For my tests, I didn't use the EN and RS pins. I just connected them to +3.3 and ground respectively.


I've been lurking on this thread since you started it. I would be happy to collaborate on the Eagle files and even fab a board as this fits with my plans. I would be happy to produce a run and share the rate with the good people here. My board would integrate modbus via MAX485 as well with very little extra cost. I also would love even a very beta copy of your sketch if possible.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Jan 08, 2013, 07:59 pm
Hi zabaat. I am very happy for your interest to contribute on the CAN shield development. At this very moment, even thou I have done successful tests with the TI SN65HVD234 transceivers, we don't know precisely if they are going to be the best and final choice. I hope to get soon a feedback from Atmel about their tests (they are building their own DIY pseudo CAN shield). I also expect to receive next week a couple of DUEs I ordered almost two months ago to do some networking tests. Regarding the sketch, as I mentioned before here, it should be available in github on the next days.  You could start ordering the parts and building your own shield to be prepared to run tests once the API is released to public. Finally your idea of the gateway is very good knowing the need to integrate CAN with other protocols. Just hold it until the time comes. Regards!
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: that_kid on Jan 09, 2013, 03:39 pm

Hi that_kid. I am sorry to know about your broken foot. Just take it easy. Even thou I have my shield working OK, I still looking for a better out-of-the-shelf board that fits better the design. I am also working on some eagle files to build, at some time, my own design. Remember that for the two transceiver we are going to need 8 pins of the Arduino DUE (CANRX0, CANTX0, CANEN0, CANRS0, CANRX1, CANTX1, CANEN1, CANRS1). For my tests, I didn't use the EN and RS pins. I just connected them to +3.3 and ground respectively.


Thanks Palliser, I was also thinking of building my own board as well.  I have some other parts I want to add for other functions from the DUE.  In the meantime I'll just get a small board that I can mount the transceivers to for testing.  The good thing about not being able to move around much is that I can think more about my designs.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: zad80 on Jan 12, 2013, 10:34 am
Hi All,
I'm very excited about this project and I'm constantly watching this thread for news and source.
BTW what we did here http://arduino.cc/forum/index.php/topic,135403.msg1064848.html#msg1064848 may be very helpful for the project  Have a look.
I'm planning to use a debugger too with the arduino , but that is still a working in progress.
Happy new year at everyone
  Zad
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Jan 18, 2013, 05:04 am
For those that already have in mind an automotive application for the Arduino Due and the almost ready for release CAN API, while it seems to be too early, I believe is timely for them to start seeking out as much information as possible of the CAN protocol of their vehicles and get familiar with it. For example, the specific type of CAN standard and some specifications like CAN identifiers, Bit rate, message type (default 11-bit identifier?), data format, range definition, etc... I must say, during some tests, often, I had to stop my test and spend a considerable amount of time searching for needed CAN info. Oh, I forgot: try to get an OBD-II male connector (plug) and where is the OBD-II connector located in your car (Most are located under the steering column).
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Jan 18, 2013, 11:18 pm
I wanted to show, briefly, how I started building my second DIY pseudo CAN shield. I decided to use two SN65HVD235 transceivers in order to compare them with the two SN65HVD234 I used in my first shield. For these 235s I also used a different SchmartBoard: The model 206-0004-01, and instead of keep it intact, I did cut it. Here a picture of the two 235s soldered in the SchmartBoard, with pin headers and a pinout.

***********WARNING: This pinout corresponds to the SN65HVD234 transceivers. Not the 235 ones**********

I wanted to present it here, for those that have not started yet their shield, as an example to be followed using the previous schematic I published in this post. Even thou 234 and 235 transceivers are very similar, differ in the pin 5. For the 234 this pin corresponds to EN (enable) for an ultralow-current (50-nA) sleep mode and for the 235 this pin 5 corresponds to AB that implements a bus listen-only loopback which allows baud rate synchronization.

Notice that this second CAN shield, more than a shield, will work as a side car (alone or inserted in a bread board) unlike the first one which is a true shield. I made it that way to avoid soldering work. I hope to be uploading more pictures of my progress with it.

(http://img6.imageshack.us/img6/1741/transceiver222.jpg)
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: that_kid on Jan 19, 2013, 01:19 am
Nice, I'm going to try my hand at making my own surface mount board.  I need to get my hands on a laser printer first.  For testing I'll start with making a board just to hold the tranceiver.  If all goes well I can have the trancievers plus the other components I need for my project. 
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Jan 20, 2013, 12:19 am
Hello that_kid. I hope your foot is doing better. Here another picture of my second DIY pseudo CAN shield and a brief comparison between the two shields:

SHIELD       ADVANTAGE             DISADVANTAGE
  #1         Compact               Prone to hot spots (soldering)
  #2         Easy-to-assemble      Bulky and messy

(http://img203.imageshack.us/img203/1803/shield2.jpg)
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: that_kid on Jan 20, 2013, 02:56 am
Hello Palliser the foot is coming along.  I'm now able to walk on it but I still have the boot on my foot, still it feels good to be on both feet again.  I see you got your second DUE, while gathering the things I need for my board I'm also working on learning more about Arduino programming.  I should be good and ready by the time I'm able to move around more. 
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: pagoda on Jan 27, 2013, 02:48 pm

Hello that_kid. I hope your foot is doing better. Here another picture of my second DIY pseudo CAN shield and a brief comparison between the two shields:

SHIELD       ADVANTAGE             DISADVANTAGE
  #1         Compact               Prone to hot spots (soldering)
  #2         Easy-to-assemble      Bulky and messy

(http://img203.imageshack.us/img203/1803/shield2.jpg)


I think  I'll be building an interface in the near future -so will be following this thread closely.  I've messed about with CAN decoding in the past - using USB can interfaces  ( Lawicwl CanUSB), and  various monitoring / message filtering software.  I hate being locked out of my car's software, so using DUE as a smart interface makes sense.
The mixture of 11 & 29 bit messages (and several different bus speeds) makes life pretty complex, and given that vehicle manufacturers like to hide useful features.. lets hope the energy & problem solving in the Arduino community can cut through the BS  :)
Lawicel do an AVR/CAN adapter, with some sample code available.
Also worth looking at :  http://rbei-etas.github.com/busmaster/
With some  contributions from Bosch - who started it all...  open source code on github.
looking forward to see what libraries develop!

Graeme
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: lufegimenez on Jan 30, 2013, 12:12 pm
Hi everybody!

I`m quite new to the Arduino world, but not to the uC world. I'd like to collaborate as much as possible with the development of the CAN API for the Arduino DUE. I actually have several DUE and other Arduino boards (with the necessary shields) and USB CAN interfaces, as well as other applications that are extensively using CAN BUS.

I'm very sorry of not having been able to start collaborating with you earlier, but I really would like to help as much as possible in any task you give me.

Cheers!

Lufe
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: zabaat on Jan 30, 2013, 06:05 pm

Hi everybody!

I`m quite new to the Arduino world, but not to the uC world. I'd like to collaborate as much as possible with the development of the CAN API for the Arduino DUE. I actually have several DUE and other Arduino boards (with the necessary shields) and USB CAN interfaces, as well as other applications that are extensively using CAN BUS.

I'm very sorry of not having been able to start collaborating with you earlier, but I really would like to help as much as possible in any task you give me.

Cheers!

Lufe




Except we can't contribute or do anything, it's really fun this way...
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Jan 30, 2013, 07:27 pm
zabaat,
What you're saying is partly true, until the new CAN library is published. The Atmel engineer that developed a prior CAN API for the SAM3X8H, is supporting Arduino with this CAN interface for Due. Once the library is on github, everyone should start to test/discuss/contribute. And I say partly because at this very moment people in this forum could be contributing, let's say, with the design of the shield. Electric schematics have been published here. I have also shown a couple of my shield approaches. What I would like to hear in this post is: My pseudo CAN shield is ready. Here a picture of it, but I have yet to see any of them here. Does that really matter? Yes, because otherwise, you can't do any test or discuss or contribute just with the sketch or the classes. Am I right?
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: zabaat on Jan 30, 2013, 08:39 pm

zabaat,
What you're saying is partly true, until the new CAN library is published. The Atmel engineer that developed a prior CAN API for the SAM3X8H, is supporting Arduino with this CAN interface for Due. Once the library is on github, everyone should start to test/discuss/contribute. And I say partly because at this very moment people in this forum could be contributing, let's say, with the design of the shield. Electric schematics have been published here. I have also shown a couple of my shield approaches. What I would like to hear in this post is: My pseudo CAN shield is ready. Here a picture of it, but I have yet to see any of them here. Does that really matter? Yes, because otherwise, you can't do any test or discuss or contribute just with the sketch or the classes. Am I right?



Oh, I guess I was waiting the same way, thinking what use is a can shield in my efforts to contribute to the testing when I have  no sketch or classes. Since it's now clear that is what I need to do to get access. I will build the shield as you have listed and post it's picture. I have had 20 of the chips and 4 Dues for a couple weeks now, definitely would have built this had I known.

I already posses a good understanding of the protocol, USB-CAN adapters and debugging software tools to tackle this. I even have a project that needs it.

I would suggest making a closed project at least for people that understand it is in beta stage so you can control the feedback you get.

Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: lufegimenez on Jan 31, 2013, 12:34 pm
I'm in the same position as Zabaat.

I'll build the Pseude shield ASAP and post pics. Maybe I try out another HW solution from the IC I have right here. I would also like to be collaborating with a beta release and feedback you directly.

Cheers

Lufe
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: zabaat on Jan 31, 2013, 10:11 pm

I'm in the same position as Zabaat.

I'll build the Pseude shield ASAP and post pics. Maybe I try out another HW solution from the IC I have right here. I would also like to be collaborating with a beta release and feedback you directly.

Cheers

Lufe


Lufe - I'm working to build a better "industrial protocol" arduino board, integrating modbus / rs232 (12v) and CAN and 2x Due chips. I just saw Palliser's ethernet addin and am debating adding that in by exposing those pins off of the dues for future expansion for profinet if possible. Is this something you would be interest in collaborating in?
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Feb 01, 2013, 03:26 am
Hola Lufe.

Your CAN experience will surely be very useful. As I mentioned in my first CAN post, my humble role is to channel the efforts to let the CAN API be completed. So far, I must say, it has been more a behind-the-forum work between Arduino, Atmel and me. Tests are in progress; the CAN library is working using the IDE directly (no makefile) and we are close.

From all of this, we have learned a big lesson: DO NOT (I repeat) DO NOT try to build something from Atmel Studio ASF and introduce it into Arduino. Both software architectures are entirely different and it shall be a pain to remove unneeded references. This experience is helping us to understand better how to create a Due library. The updated libsam will be solid ground for future developments for Due.  Saludos!
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Markus_L811 on Feb 01, 2013, 08:18 am

Hola Lufe.

Your CAN experience will surely be very useful. As I mentioned in my first CAN post, my humble role is to channel the efforts to let the CAN API be completed. So far, I must say, it has been more a behind-the-forum work between Arduino, Atmel and me. Tests are in progress; the CAN library is working using the IDE directly (no makefile) and we are close.

From all of this, we have learned a big lesson: DO NOT (I repeat) DO NOT try to build something from Atmel Studio ASF and introduce it into Arduino. Both software architectures are entirely different and it shall be a pain to remove unneeded references. This experience is helping us to understand better how to create a Due library. The updated libsam will be solid ground for future developments for Due.  Saludos!

Some good news some bad news, good to hear but can you explain the part from "Not trying to port Atmel Studio ASF to Arduino Software architecture a little bit more for me for better understanding.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: lufegimenez on Feb 01, 2013, 10:10 am


Lufe - I'm working to build a better "industrial protocol" arduino board, integrating modbus / rs232 (12v) and CAN and 2x Due chips. I just saw Palliser's ethernet addin and am debating adding that in by exposing those pins off of the dues for future expansion for profinet if possible. Is this something you would be interest in collaborating in?


Zabaat (and everybody else too, of course)

I'm developing for several projects I'm involved in a multifunctional shield with RS485, TTL UART, CAN BUS, TTL digital GPIO, discrete open-ground outputs, relay module and Analog Input port, for the MEGA board.
CAN BUS is based on MCP2515 IC, but I would absolutely prefer to use an internal CAN peripheral, therefore I'm following very closely the evolution of the Due board. Testings of this board will start middle February and if everything goes ok, I'll be presenting it soon.

As I told in my introduction, I'm not an Arduino expert nor a programmer guru, but I really would like to collaborate with any interesting project like the one you are presenting.

Estimado Palliser
I'm looking forward to test as soon as possible this CAN library. Being able to have direct access to CAN from a CortexM3 processor... :smiley-yell: this sounds sooooo good!!!
I'll get today all my 3.3V CAN transceivers, I'll mount them tomorrow or Monday and I will have my DUE CAN test bench available for you!

Cheers

Lufe
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Feb 01, 2013, 03:53 pm
Lufe,
Thank you. I will be waiting to see your shield!

Markus,
I am being the spokesperson of the Atmel engineer who is supporting us, and Massimo (of course). As you may know, I ported the CAN files from ASF to Arduino IDE
but, because of the AFS structure, it took over 20 files to make the interface run. This is not under the Laws of Arduino. Arduino should be simple, beautiful. All the time, in this post. Massimo's retort has been: use few files.
For this project, Atmel is using some non-ASF drivers they had before the ASF era. They explained to me that if we include any ASF based file in the project, there will be needed to add thousand others. I know you did a great job with your RTC Library but in the case of the CAN class, more references need to be considered.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: robertspark on Feb 01, 2013, 10:08 pm
Hello all, firstly I am new to Arduino, but am eager with the anticipation of Canbus for the Due as it will serve my application well.

While I can understand why some of you may require a shield to provide easy connectivity with a Canbus interface and the arduino due, could I ask what would be wrong with using something such as the below link for a canbus  development module that appears to be quite cost effective to me + my application?  I presume it would still work with the upcoming Due library?

SN65HVD230 CAN Board Network Transceiver Evaluation Development Module Kit 3.3V
http://dx.com/p/sn65hvd230-can-board-network-transceiver-evaluation-development-module-150782
http://www.wvshare.com/product/SN65HVD230-CAN-Board.htm

To those developing the canbus library, keep up the good work, your efforts will very much be appreciated I'm sure as it will bring the simplicity of the other numerous other libraries to this very capable processor.

(not to be pushy, but do we have a timeline of when we may see a beta library, or an initial issue?)

Thanks in advance
Rob
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Feb 01, 2013, 11:55 pm
Hello Rob and thank you for your encouraging words!

I checked the electric schematic of the SN65HVD230 based board you mentioned. It can work with the CAN library but in a direct mode. I mean, pin 8 (Rs) is been underused and fixed to ground (high-speed mode). The CAN library contains the SN65HVD234 component data conforming to Arduino API that will be sub-utilized too (I think you don't want to port the manufacture ones).

I would recommend it only for tests purposes, but for your application, you will surely want to migrate to 234 to take advantage of the Due libraries for ultra-low current sleep mode. Additionally, the missing filter and buffer capacitors should be needed if distance between Due and CAN device increases.

In case you decide to get it, I would also recommend to get two for loop tests of the CAN controllers. Notice that the manufacturer's (Wave Share) price differs considerably from the vendor's (DX) (Russian?). By the way, I don't see the 120 ohm terminal resistor in the photos. Only the 10K, I think. Regards!

Here the schematic of the SN65HVD230 based board you mentioned.

(http://img594.imageshack.us/img594/9004/sn65hvd2301.jpg)
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: robertspark on Feb 02, 2013, 01:17 am
Palliser,

the 120k resistor is positioned to the left hand side of the end of bus jumper pins (between the CANL & CANH markings on the Waveshare board).

(http://www.wvshare.com/img/devkit/accBoard/SN65HVD230-CAN-Board/SN65HVD230-CAN-Board-2.jpg)

Thanks very much for your points regarding ultra-low sleep current mode, and the filter & buffer capacitors, i'll defiantly keep it in mind and can see the advantages of the 234 over the 230.

With regards to "pin 8 (Rs) is been underused and fixed to ground (high-speed mode)", looking at the datasheet for the 230 (link below), I don't think that this module / interface board is actually set for full high speed mode, as it has a 10k resistor before the ground connection, and reading the datasheet, this seems to imply that it is actually set up in slope control mode (pg 20 of datasheet) as ~ 15V/usec, does this not imply that it is actually not in full high speed mode?  But I may have misunderstood the datasheet (is slew rate not related to the bus frequency?)

http://www.ti.com/lit/ds/symlink/sn65hvd230.pdf (http://www.ti.com/lit/ds/symlink/sn65hvd230.pdf)

Many thanks again,

regards Rob
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Feb 02, 2013, 03:36 am
Rob,
You are right. Sorry for overlooking the 120 resistor and the slope mode. According to the specs, the high-speed mode of operation is selected by connecting pin 8 to ground or lowering of V(Rs) to 0.0v < 1.0v (p.2,3). So, no high-speed. I believe the manufacturer sets the board to 'slope' as a typical mode that helps to improve EMC behavior (spikes, harmonics) in case we use an unshielded bus cable in order to save some money, but, the trade-off is a loop propagation delay around 100ns with 10K (p.22,23). Anyway, you may reduce the baud-rate to 250 or 500K if necessary. regards.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Markus_L811 on Feb 02, 2013, 03:10 pm

They explained to me that if we include any ASF based file in the project, there will be needed to add thousand others. I know you did a great job with your RTC Library but in the case of the CAN class, more references need to be considered.

Thanks for the laurels, but the RTC stuff isn't as half as big as the CAN stuff you did and btw. there was an "component_rtc.h" file that was waiting for me and begged me to make an userfriendly (under arduino law-standing) Library. For you there was nothing from arduino to work above it.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: AdderD on Feb 06, 2013, 04:54 pm
Is there hope of the code seeing the light of day soon? There seem to be no fewer than 3 people (4 including me) who could be working on creating a library but instead it's languishing behind the scenes and no code has been posted. This is kind of depressing since there have been mentions of posting code for around 2 months. It's all well and good to want people to create shields but some people are programmers and some people are electrical engineers. The two don't always overlap so asking people to build something for the transceiver interface isn't always realistic. The four pin adapters posted in this thread would be fine to use for testing if we had code. It doesn't even have to be nice code or complete. Something that works is sufficient to get started. It seems like somebody has code that at least partially works since I've seen mention of testing for a long time. What is really stopping the placement of that code on github? There seem to be no shortage of volunteers to work on it. Just saying...
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: downtown_sausage on Feb 06, 2013, 11:21 pm
I would have to agree with AdderD. Why can't preliminary code be posted up for people to start digesting/optimizing/learning/testing/developing? Also, this API that's being 'developed'. There hasn't even been any discussion on how this API will utilize the CAN peripheral and other necessary peripheral features on board the micro.

Let me elaborate: From my experience with micros and peripherals, there are many ways to configure the drivers. Sometimes you can use DMA, sometimes there is dedicated RAM on board for the specifc peripheral, or you might not use either. There's all kinds of Low Power Mode Options just for CAN. What events will be generating CAN interrupts to the uC?
What exactly is this API establishing? A bare bones CAN driver or are there more layers of software queuing on top?

So why are driver configuration options not up for discussion? The driver is the foundation upon which everything else will be running. It seems as though there should be discussion on this, unless Palliser wants only his CAN driver ideas implemented..
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: peplin on Feb 07, 2013, 03:35 am
Another "let us code" post here - I don't particularly care how the CAN shield turns out, and it's not going to have any effect on what the CAN API looks like. It's getting to the point that someone is going to fork off and make their own CAN library, but if the efforts happening behind the scenes of this thread are the "officially blessed" efforts, it would be a shame to have conflict in the future.

A GitHub repo with a "This code does not work." line in the README should suffice.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Feb 07, 2013, 05:41 am
I think, at this moment, an overview of this project would help to clarify and provide some answers for the latest posts:

My original goal was to find a tinker way to communicate the two little "CANRX" and "CANTX" of the new Arduino Due. I did read in the Arduino site that there was no API available for that. I ran through this forum and found nothing in progress. I asked Massimo about it and he invited me to contribute doing so. I started to look for prior arts regarding the integration of SAM family cores and the CAN protocol and found SAM3X-EK development board and Atmel ASF CAN library. This forced me to re-align my original goal with Atmel's goal which is: to show how to configure the two CAN controllers and how to manage CAN message transfers. Then I started to port the Atmel sample to Arduino IDE. I worked alone for a month, posting my progress here in the forum. During this exercise, I didn't imagine that this way was the hardest road. Finally, I made Due to read/write CAN messages, but at a cost: ~40 files (50% Atmel, 50% Arduino). Even though I followed Massimo recommendations trying to minimize the library, I couldn't reduced them too much. So, I asked him and Cristian for help. They did put me in contact with the Atmel engineer that developed the CAN sample. I gave him the files and he started to redo pretty much everything. As I mentioned before, he already built the shield and is doing now tests. He is a leader, an expert with CORTEX-SAM and a hard worker.

Like you guys, I am waiting for the final CAN API. In the meantime, my role these days has been to motivate the making of the shield, which is a must.

All I can say is that Arduino want to release a solid CAN API, providing to this community a good start in working with the CAN protocol. It is a fact that not everyone knows CAN, like some of us. In this way, it will be easier to digest/optimize/learn/test/develop the protocol. And that's when you will be very valuable with new ideas/applications and supporting others that want to learn. Thanks.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: downtown_sausage on Feb 07, 2013, 03:43 pm
Palliser, I did  not mean to be critical. People are ready to go on this, and I think it's time we start working towards a goal. It sounds like the Atmel engineer may have higher priorities or slowed down on progress, maybe we should start working on our own CAN API. What would we need outside of the datasheet to do this?
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: AdderD on Feb 07, 2013, 04:34 pm
Yes, I don't mean to sound like a jerk but that point it really looks like whatever is happening behind the scenes is not working. I really seriously am considering starting up an effort to make a new library from the ground up with just Arduino sanctioned code and libraries. It seems like this Atmel person isn't working very quickly. It is true that it appears as if the canbus stuff in the cortex chip isn't the easiest to work with but the Arduino 1.5.2 IDE seems to already have plenty of libsam and CMSIS stuff in it so access to the relevant hardware features to enable canbus should be possible. At first a fresh effort like this would just basically be the ability to send/receive canbus frames without a lot of hand holding but eventually it could be built up. We'll see. I should get my Due soon and transceivers some day and once I get my hardware I'm going to be itching to make things. If this Atmel guy hasn't produced anything by then I might take matters into my own hands. It's OK if my effort doesn't get sanctioned or if I end up abandoning it once/if the sanctioned one sees the light of day. I'll still have learned a lot about the architecture and that's important too.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Palliser on Feb 08, 2013, 09:24 pm
I have better news!. Today I got the CAN class from the Atmel engineer. It is a raw class to start with. He really did a great job. Now, instead of ~40 files, we have 4 files. The CAN class (.c,.h) and the transceivers class (.c,.h). It compiles OK (0 errors/0 warnings) with the new IDE 1.5.2. I will be running some tests during this weekend and consulting with Arduino for the releasing in github next week. Thanks you for your patience.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: AdderD on Feb 13, 2013, 08:42 pm
That is good news. I know I'm willing to test. I'm sure plenty of other people are itching to do so as well. No need for too much testing before initial commit to git. The whole point in something like git is to disseminate code. Nobody expects a first commit to be perfect. We're just after something to look at. The sooner we see it, the sooner we get started with testing and modifications.
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Sherlock3112 on Feb 15, 2013, 02:02 pm
Another week passes and still nothing in GIT. Hardly the spirit of open source, is it?
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: cmaglie on Feb 15, 2013, 06:16 pm

The "can" branch is now published on github, currently it's in sync with the upstream "ide-1.5.x" branch.
I would like to thanks Palliser and aethaniel for the work done so far, and all the other that have contributed to this discussion.

https://github.com/arduino/Arduino/tree/can
https://github.com/arduino/Arduino/tree/can/hardware/arduino/sam/libraries/CAN

Who is interested in development must checkout the "can" branch since its likely to diverge while the CAN library gets developed and the API evolves.
The CAN library is still far from its final shape, a lot of work needs to be done, BTW its public now and I'm excited to see what the community can get out of it.

Finally: thanks you all for your patience, I know that more than two months of wait seems a lot but, believe me, time flies and sometime is very hard to keep up with the amount of inputs coming from this amazing community.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: giants_fan3 on Feb 15, 2013, 09:18 pm
Very exciting, thank you @cmaglie. I'm looking to check out the repo.
Thanks for starting this whole effort and the thread, @Palliser
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Sherlock3112 on Feb 16, 2013, 12:50 am
Thanks for the time and effort from all those who have worked on it so far. It'll be great to have something to get our teeth in to. :)
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Feb 16, 2013, 02:14 am
A big thank you to Palliser and CMaglie! This is really cool! Thanks again!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: that_kid on Feb 16, 2013, 04:06 am
Excellent work guys, thanks again
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Feb 16, 2013, 06:39 pm
Ok, I've forked the Arduino project and checked out the CAN branch so I'm pretty well good to go there. I looked at the code for the canbus library. It's a good start. Here is what I think should be done in order from first to last:

1. The normal arduino way to instantiate a library seems to be to store everything in the class and instantiate it globally. Right now the can library is constantly needing class references passed to its functions. The Can class should be internal to the CANRaw. This was already done in the code but commented out. I'm guessing that whoever wrote the code just hasn't gotten to it yet. The canbus transceiver class is already a member of the CANRaw function so that's good.
2. It would be nice to make it interrupt driven. Right now it appears to be polling driven. That's OK for testing but you aren't going to be able to put it on a bus with a new message every 600us and have it work right.
3. It would probably be good to be able to use DMA transfers to automatically write canbus frames to a software ring buffer.
4. Filtering should eventually be implemented
5. Then higher level protocols should be supported as well.

I'll try to work on some of this. I won't have any transceiver hardware to test with until Monday.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Feb 18, 2013, 08:01 pm
Thank you AdderD for your notes and insights about the library.

A second CAN sample sketch has been added into the "can" branch. In this example, the two CAN modules work in PRODUCER mode (CAN0 mailbox 0) and CONSUMER mode (CAN1 mailbox 0) and use CAN interrupt handler to check whether the communication has been completed (interrupt is triggered).

https://github.com/arduino/Arduino/tree/can/hardware/arduino/sam/libraries/CAN/examples (https://github.com/arduino/Arduino/tree/can/hardware/arduino/sam/libraries/CAN/examples)

Quote
Producer Mode:
In this mode, when a remote frame is received, the mailbox data are sent automatically. By enabling this mode, a producer can be done using only one mailbox instead of two: one to detect the remote frame and one to send the answer.

Consumer Mode:
In this mode, after each transfer request, a remote frame is automatically sent. The first answer received is stored in the corresponding mailbox data registers.

Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Feb 18, 2013, 10:09 pm
Awesome! I just tried both example sketches and have both working on a cobble-job dual canbus shield I made from a schmartshield 1.27mm arduino shield and two sn65hvd234 chips. My shield sucks and I had to enable slope control with a 10k resistor from Rs to ground but now it seems to work reliably enough. This will allow me to actually test code now. :)
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: haha2k on Feb 19, 2013, 04:45 am
Thanks again for providing the sample sketches I have tried both and either worked. By any chance you could please provide the wiring diagrams? I doubt you can make anything out of the pile of spaghetti but please post the diagram. Thank you :)
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Feb 19, 2013, 05:01 am
Please, check replay #33.

http://arduino.cc/forum/index.php?PHPSESSID=891604776329b7f55faf3f9510fb1ace&topic=131096.30 (http://arduino.cc/forum/index.php?PHPSESSID=891604776329b7f55faf3f9510fb1ace&topic=131096.30).

Regards.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: haha2k on Feb 21, 2013, 05:55 am
Thanks again Palliser. After exhausting all possibilities and redid the circuit multiple times I finally figured out the work around. I am suspecting it has something to do with the Serial Monitor on Mac. I inserted a line (cannot be a blank line) right before the while loop and both examples worked.

void loop()
{
  Serial.println("-----");
  while (Serial.available() > 0) {
     CAN_MSG_1 = Serial.parseInt();
      if (Serial.read() == '\n') {     
      Serial.print("Sent value= ");
      Serial.println(CAN_MSG_1);
      //delay(1000);
    }
  }
....................
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Feb 22, 2013, 05:52 pm
I have done some updates to the canbus library previously posted. I've made it a lot simpler to work with, more object oriented, and more "Arduino" like. I forked the Arduino project and my changes are pushed to my own fork. So, if anyone is interested they'd have to get it from my github account:
https://github.com/collin80/Arduino.git

I'm working on making it interrupt driven as well. Also, I will try to keep encapsulating functionality into easier to work with classes. I think the next order of business is to work on the mailbox settings. It's not fun to work with all that currently.

I wish there were a better way to share the canbus library. Perhaps I should create a repo for just the library on my account and push just the canbus files there? It would make things a lot smaller and easier to get.
Title: Re: [OBD2 ?] Building a CAN API for Arduino DUE
Post by: haha2k on Feb 25, 2013, 03:00 am
Has anyone gotten this to work with OBD2? I isolated the consumer and producer from the interrupt example and loaded onto 2 DUE and was able to get consume message from the producer. Then tried hooking up the consumer to the OBD2 and nothing. I haven't tried debug through the code just thought I ask. Thanks.
Title: Re: [OBD2 ?] Building a CAN API for Arduino DUE
Post by: AdderD on Feb 25, 2013, 04:21 am

Has anyone gotten this to work with OBD2? I isolated the consumer and producer from the interrupt example and loaded onto 2 DUE and was able to get consume message from the producer. Then tried hooking up the consumer to the OBD2 and nothing. I haven't tried debug through the code just thought I ask. Thanks.


I think you are misunderstanding how OBDII works with canbus. You don't use consumer or producer mode for mailboxes. Really, remote frames have been depreciated for years. I really haven't ever seen that capability used in anything. Anyway, wikipedia has a decent enough reference: http://en.wikipedia.org/wiki/OBD-II_PIDs

Basically, you want to use two mailboxes; one for transmitting, one for receiving. You transmit a PID request on a certain ID and the ECU responds on a different ID which is 8 higher than the ID you sent. If you do this you should see it working.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Feb 25, 2013, 04:34 am
Hello haha2k.
I am happy you already want to deals with the 'heart of the matter'.

Thanks AddeD for your very pertinent comments.

I would like to add a couple of notes:

- Check the default baud rate in the specifications of your OBD?. Remember, the example default baud rate is 1Mbps. Depending on the OBD factory settings, this value could change to 500K, 250K, etc. If so, you have to change it. I.e. if you baud rate is 125K, then you have to do this:

Code: [Select]

CAN.init(CAN0, SystemCoreClock, CAN_BPS_125K);
CAN.init(CAN1, SystemCoreClock, CAN_BPS_125K);


- Try changing the transfer ID to 0x07DF. this is a standard CAN functional request. Do it as follows:

Code: [Select]
#define TEST1_CAN_TRANSFER_ID    0x07DF

Thank you and keep us posted.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: haha2k on Feb 25, 2013, 07:58 am
Thank you Palliser and AddeD for the pointer and insight. I think I am truely confuse with the way this is set up. What I am going after at the moment is to just listen/spy on the bus for the traffic. Whether it be OBD2 or vehicle's internal bus I have been able to plug into it via an OBD2 shield in conjunction with UNO or simply a CAN232 without participating as a node and see data flow through. Yes, I am aware of how OBD2 work :) on top of the request and respond the power train CAN in my vehicle contains hundreds of frames per seconds of other propriatary information aside from RPM, speed, temperature, etc. Again, thanks Palliaer I'll give it shot. The OBD2 is at 500 and I had tried them all. Here is another question, how come 100K isn't part of the constants list? Thanks,
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jeancarlo19 on Feb 25, 2013, 02:05 pm
Hi guys! Congrats! You've done an excellent job!!!

I wanted to try the CAN library so I've downloaded them, compile it and tried but I get stuck in some compiling issues.

I've tried both examples. The first one fails due to not finding sysclk.h file and the second one fails showing the following errors:

Quote

In file included from can_example.ino:2:
C:\Users\jordi\Documents\arduino-can\hardware\arduino\sam\libraries\CAN/sn65hvd234.h:46: error: ISO C++ forbids declaration of 'Pio' with no type
C:\Users\jordi\Documents\arduino-can\hardware\arduino\sam\libraries\CAN/sn65hvd234.h:46: error: expected ';' before '*' token
C:\Users\jordi\Documents\arduino-can\hardware\arduino\sam\libraries\CAN/sn65hvd234.h:48: error: 'uint32_t' does not name a type
C:\Users\jordi\Documents\arduino-can\hardware\arduino\sam\libraries\CAN/sn65hvd234.h:51: error: ISO C++ forbids declaration of 'Pio' with no type
C:\Users\jordi\Documents\arduino-can\hardware\arduino\sam\libraries\CAN/sn65hvd234.h:51: error: expected ';' before '*' token
C:\Users\jordi\Documents\arduino-can\hardware\arduino\sam\libraries\CAN/sn65hvd234.h:53: error: 'uint32_t' does not name a type
C:\Users\jordi\Documents\arduino-can\hardware\arduino\sam\libraries\CAN/sn65hvd234.h:56: error: 'uint32_t' does not name a type
C:\Users\jordi\Documents\arduino-can\hardware\arduino\sam\libraries\CAN/sn65hvd234.h:57: error: 'uint32_t' does not name a type
C:\Users\jordi\Documents\arduino-can\hardware\arduino\sam\libraries\CAN/sn65hvd234.h:58: error: 'uint32_t' does not name a type
C:\Users\jordi\Documents\arduino-can\hardware\arduino\sam\libraries\CAN/sn65hvd234.h:60: error: 'uint32_t' does not name a type
C:\Users\jordi\Documents\arduino-can\hardware\arduino\sam\libraries\CAN/sn65hvd234.h:61: error: 'uint32_t' does not name a type
C:\Users\jordi\Documents\arduino-can\hardware\arduino\sam\libraries\CAN/sn65hvd234.h:63: error: 'uint32_t' does not name a type
C:\Users\jordi\Documents\arduino-can\hardware\arduino\sam\libraries\CAN/sn65hvd234.h:64: error: 'uint32_t' does not name a type
can_example:23: error: 'CANRaw' does not name a type
can_example.ino: In function 'void loop()':
can_example:56: error: 'SN65HVD234_Init' was not declared in this scope
can_example:57: error: 'SN65HVD234_SetRs' was not declared in this scope
can_example:58: error: 'SN65HVD234_SetEN' was not declared in this scope
can_example:60: error: 'SN65HVD234_DisableLowPower' was not declared in this scope
can_example:61: error: 'SN65HVD234_Enable' was not declared in this scope
can_example:76: error: 'CAN' was not declared in this scope
can_example:118: error: 'SN65HVD234_EnableLowPower' was not declared in this scope
can_example:119: error: 'SN65HVD234_Disable' was not declared in this scope


I guess there is something related with the header files from Atmel microprocessor but I'm not sure about it and I haven't been able to download them and try.

Can anybody give me a hint? Thx in advance!!!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Feb 25, 2013, 02:30 pm

Thank you Palliser and AddeD for the pointer and insight. I think I am truely confuse with the way this is set up. What I am going after at the moment is to just listen/spy on the bus for the traffic. Whether it be OBD2 or vehicle's internal bus I have been able to plug into it via an OBD2 shield in conjunction with UNO or simply a CAN232 without participating as a node and see data flow through. Yes, I am aware of how OBD2 work :) on top of the request and respond the power train CAN in my vehicle contains hundreds of frames per seconds of other propriatary information aside from RPM, speed, temperature, etc. Again, thanks Palliaer I'll give it shot. The OBD2 is at 500 and I had tried them all. Here is another question, how come 100K isn't part of the constants list? Thanks,


If that's the case then I think what you want is to set one or more mailboxes to receive mode, not consumer mode. In consumer mode it sends a remote frame request on a certain ID and expects a reply back on that ID. I'm not sure how that'd work with OBDII. You merely need to set up a receive box and let traffic flow to it.

As for 100K, I'd guess it's not on the list because it just isn't that common. You should be able to add it though.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Feb 25, 2013, 04:12 pm
Hello jeancarlo19,

Here a procedure that should make the CAN sketches compile OK. I know the following is going to look drastic but it is the easiest way to do it.

Before proceed with the following steps, MAKE SURE to BACKUP your current Arduino 1.5.2 files.

Step 1: copy and replace ALL the folders/files from ...\Arduino-can\Arduino-can\*.*
to ...\arduino-1.5.2-windows\arduino-1.5.2\*.*

I want to clarify that ...\Arduino-can\Arduino-can\ refers to the CAN github stuff.
and ...\arduino-1.5.2-windows\arduino-1.5.2\ refers to your current arduino 1.5.2 stuff.

Step 2: Open the Arduino IDE 1.5.2

Step 3: Open any CAN sketch (now under ...\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\libraries\CAN\).

Step 4: Import the CAN library (don't forget to set the Arduino Due (Programming Port) board)

And that's it. Now, you should be able to compile the sketch without errors/warnings.

The reason for the above procedure is because the CAN library was built as Arduino staging using in a prior Ardunio stuff.
Besides the CAN library itself, there are few files that have to be updated in the current 1.5.2 version.  I will make time
to find and tell the only necessary files to copy/replace. Regards! 
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Feb 25, 2013, 06:31 pm
Attached to this post is the newest version of my copy of the canbus library. I've changed a lot but the examples are updated to reflect how it works now. It would be good to get feedback on whether this is a good direction to take the library or not. I'm not totally done with my modifications yet but it is in a usable state currently.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: haha2k on Feb 26, 2013, 04:00 am
Success!!! AdderD & Palliser thank you both again the priceless HINTS. After an hour of tweaking the code this evening I was able to tap into both the internal and OBD2 bus, the data flowed through into the serial monitor as if a flood gate was opened. Sorry TI for doubting the 3.3V compatibility to 5V (after the disappointment last night I ordered 10pcs of TJA1055/c thinking 3.3V is not what TI claimed to be). Next stop, double back to the consumer mode, set the ACK bit and make DUE as a node on the bus. It's exciting. Again without the inputs from both of you I would still be going in circles. The IRQ worked like charm, it's better than having to loop and loop to get the serial stream from CAN232 and miss 5-10% of the frames. I am SOLD!!!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Feb 26, 2013, 04:24 am
Hello AdderD,

You are doing an amazing job. Thank you. I haven't get through all the improvements (just a glance) but looks/works very pretty now. Thank you also for adding a third sketch. This is how value is added here.

haha2k
I am happy you are on the move with the CAN library. Keep us posted about your progress.

I invite the CAN bus fans in the forum to join us and let us know how the library is working, picture of the CAN shield, etc. Regards!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jeancarlo19 on Feb 26, 2013, 10:19 am
Hi Palliser!

Thx for your help. Now it's working!!! I guess I had downloaded a wrong repository because I've already did without good results but I downloaded the repo again and now everything is compiling.

Gracias Pana!!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Feb 26, 2013, 08:08 pm
Here is another zip. I made a mistake in setting the ID for canbus frames and so basically all frames were being sent as ID 0. It works now. Whoops...
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: lufegimenez on Feb 27, 2013, 04:11 pm
Hi guys!

I've been trying out the CAN API and it works all right.

My sincere congrats to all developers. You have done an excellent work!

I have been testing the CAN api and everything works smooth at all speeds up to 1Mbps.

Although I have some comments:

The Can mailbox config structure "can_mb_conf_t" contains only two possible data, datah and datal. Is there any direct way to address individual bytes (B0 - B7) of the CAN message? Obviously the DLC param should has to be correctly set.

I'm using Line Driver SN65HVD232 (without any kind of low power mode, or anytingh else than pure CAN line driving) and I think that it is a little bit unconfortable to start up the transceiver as SSN65HVD234_Data can0_transceiver; instead of just using a generic CAN pin enable. Other options could also be using any 3V-5V line transceiver and using after any standard 5V Can driver.


Thank you again folks!

Lufe
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Feb 27, 2013, 06:12 pm

Although I have some comments:

The Can mailbox config structure "can_mb_conf_t" contains only two possible data, datah and datal. Is there any direct way to address individual bytes (B0 - B7) of the CAN message? Obviously the DLC param should has to be correctly set.


You must not have downloaded the version I uploaded yesterday. In my new version there is an RX_CAN_FRAME structure which does expose all 8 data bytes.

Quote

I'm using Line Driver SN65HVD232 (without any kind of low power mode, or anytingh else than pure CAN line driving) and I think that it is a little bit unconfortable to start up the transceiver as SSN65HVD234_Data can0_transceiver; instead of just using a generic CAN pin enable. Other options could also be using any 3V-5V line transceiver and using after any standard 5V Can driver.


Yes, right now the code basically assumes you will be using a 234 chip. If you use my new version you might be able to set both Rs and En pins to be the same pin number. That would probably work alright for your use. Eventually we should support disabling the use of certain pins so that chips like 232 can be supported without wasting pins.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: lufegimenez on Feb 28, 2013, 09:37 am
Just starting to try out!

Thanks!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jeancarlo19 on Mar 01, 2013, 12:11 pm
Hi guys,

After being successful in the test between CAN bus 0 and 1 in Arduino Due, I've tried to receive messages from an ECU. This ECU sends 13 CAN frames of 64 bytes each one. I'm able to read the first frame but I have no idea about how to read the rest of them. By reading, I guess that the ECU is working with extended frame so I've been trying to mask the whole ID (CAN_ID and frame_ID), setting uc_id_ver=1 and some other options but nothing worked... Am I right with the extended frame CAN bus version? Has anybody experience on that? Is this library working with this kind of CAN bus?

Thanks!!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Mar 01, 2013, 03:09 pm

After being successful in the test between CAN bus 0 and 1 in Arduino Due, I've tried to receive messages from an ECU. This ECU sends 13 CAN frames of 64 bytes each one.


Something is not right with this statement. You cannot send more than 8 bytes per frame. It is totally against the spec. This is not to mention that it is impossible to even set a DLC of more than 15 since it only uses 4 bits. So, you either mean 64 bits (8 bytes) or you mean that the ECU uses some form of higher level protocol to send messages as a series of frames. In the latter case there is no support for higher level protocols so that might be a problem.

Quote

I'm able to read the first frame but I have no idea about how to read the rest of them. By reading, I guess that the ECU is working with extended frame so I've been trying to mask the whole ID (CAN_ID and frame_ID), setting uc_id_ver=1 and some other options but nothing worked... Am I right with the extended frame CAN bus version? Has anybody experience on that? Is this library working with this kind of CAN bus?
Thanks!!


Yes, if you are using the official version of the library then uc_id_ver being set to 1 means extended frame. To tell you the truth, I have not tried the library (either the official one or the newer one I've worked on) with extended frames. It *should* work but I just haven't tested.

If you use my newer/modified version of the canbus library you will get hardware interrupts and a software buffer so you can just keep reading new frames out of the software buffer and not worry about messing with mailboxes. If you are using the official version then you need to check for whether a given mailbox has a new message and grab it if so. The act of grabbing the message clears the box to accept another message. So, that part is handled for you. Below are the relevant lines necessary to grab a message. If they're put in a loop you should be able to keep grabbing new messages as they come in.

Code: [Select]

  // Wait for CAN1 mailbox 0 to receive the data
  while (!(CAN.mailbox_get_status(CAN1, 0) & CAN_MSR_MRDY)) {
  }

  // Read the received data from CAN1 mailbox 0
  CAN.mailbox_read(CAN1, &can1_mailbox);


Of course, I'm going to suggest that you download the version of the library I posted a few messages back and use that. To me it's easier to work with. It will require quite a bit of changing your code around though since I changed so much. As luck would have it, I did update the examples so it should be pretty easy to get up to speed with the different way of doing things.

I will try to do some testing with extended frames to make sure that the library is working with them as well.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Mar 01, 2013, 04:02 pm
Hello chamo jeancarlo19,

please, confirm if your ECU is working with extended frames.

By default, all the CAN sample sketches work in the standard frame mode. For extended frames you will have to change the Mailbox ID register to CAN_MID_MIDvB instead of CAN_MID_MIDvA. This register CAN_MID_MIDvB needs to be SET for the mailbox to handle the extended format. Don't worry about the acceptance masking (MAM). The samples ORit depending on the format type.

In the same way as AdderD, I will be revising the extended format and try to bring an example using it. Regards!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Mar 01, 2013, 04:19 pm
Ok, I made a 4th example sketch in my copy of the canbus library. It turns out that extended frames do work fine already with the library. The sketch is a ping/pong type where the two canbus devices are set up to fire and receive frames pointing at each other so one sends, the other receives,then that one sends and the first receives, back and forth. Currently I have 100uS delay between each sending to slow things down a bit. I have tested this to be pretty stable. Right now I am looking at a serial monitor showing 5 million ping/pongs in a row. If I take the 100us delay out it will work VERY quickly but usually lock up after around 1 million ping/pongs. I don't know why quite yet, I assume the speed is causing it to sometimes miss setting things right and even one failed send will stop the sequence from happening.

Word to the wise, I test with a canbus to usb dongle also connected so I can see the traffic. I don't have a dongle that can handle 100,000 frames per second and you probably don't either. But, it appears the Due can do it. So, you kind of have to trust that the sent and received counters are accurate because it's hard to find another piece of hardware to verify.

I've attached the newest version of the library with the above 4th example.

Edit: I tested up to 100 million successful ping/pongs and quit. If it's stable to that much it seems stable enough. It would be good if someone wanted to try testing how little delay you can add and still have the traffic stay stable.

Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jeancarlo19 on Mar 01, 2013, 04:30 pm
Hi AllerD,

You're right... it's out of specs but frame format is 64 bytes and I'm supposed to received 13 frames of 64 bytes each one. In fact, by changing MAX_CAN_FRAME_DATA_LEN to 64 I'm received all bytes and reading the first frame. I guess ECU is using some higher level of protocol... I'll have to investigate. In any case, I'll download your library to test the extended version.

Hi Palliser,

I've already tested to change the Mailbox ID register to CAN_MID_MIDvB and everything worked the same way, I mean, I received the first frame as before but I don't know how to get the other 12 frames...

Whatever, I'll have to investigate further before continuing.

Thanks for your help!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: haha2k on Mar 04, 2013, 01:05 am
perhaps the while loop at the very end of loop method? I sometimes overlook the simplest things and over complicate them (life of a code monkey!!!)
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: lufegimenez on Mar 05, 2013, 10:01 am
Hello friends

I've been programming for a while with the API and there are some questions that I don't get very well.

These are my questions for the gurus:

1) I wasn't able to find the structure of "can_mb_conf_t" and the correspondence of the different elements with the CAN Register mapping (Section 41.9 of SAM3XA of Atmel documentation) so I get a little lost...
2) I also would appreciate a list with all defined parameters.

Thats all by this time.

Thank you a lot!!!

Lufe
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Mar 05, 2013, 04:19 pm
No problem Lufe,

Code: [Select]

typedef struct {
uint32_t ul_mb_idx;
uint8_t uc_obj_type;  //! Mailbox object type, one of the six different objects.
uint8_t uc_id_ver;    //! 0 stands for standard frame, 1 stands for extended frame.
uint8_t uc_length;    //! Received data length or transmitted data length.
uint8_t uc_tx_prio;   //! Mailbox priority, no effect in receive mode.
uint32_t ul_status;   //! Mailbox status register value.
uint32_t ul_id_msk;   //! No effect in transmit mode.
uint32_t ul_id;       //! Received frame ID or the frame ID to be transmitted.
uint32_t ul_fid;      //! Family ID.
uint32_t ul_datal;
uint32_t ul_datah;
} can_mb_conf_t;


This can be found within the can.h file in the libsam files.

Here are some other relevant structures which are found in component_can.h in the CMSIS files:
Code: [Select]

typedef struct {
  RwReg  CAN_MMR;       // (CanMb Offset: 0x0) Mailbox Mode Register
  RwReg  CAN_MAM;       // (CanMb Offset: 0x4) Mailbox Acceptance Mask Register
  RwReg  CAN_MID;       /**< \brief (CanMb Offset: 0x8) Mailbox ID Register
  RwReg  CAN_MFID;      /**< \brief (CanMb Offset: 0xC) Mailbox Family ID Register
  RwReg  CAN_MSR;       /**< \brief (CanMb Offset: 0x10) Mailbox Status Register
  RwReg  CAN_MDL;       /**< \brief (CanMb Offset: 0x14) Mailbox Data Low Register
  RwReg  CAN_MDH;       /**< \brief (CanMb Offset: 0x18) Mailbox Data High Register
  RwReg  CAN_MCR;       /**< \brief (CanMb Offset: 0x1C) Mailbox Control Register
} CanMb;
/** \brief Can hardware registers
#define CANMB_NUMBER 8
typedef struct {
  RwReg  CAN_MR;        /**< \brief (Can Offset: 0x0000) Mode Register
  WoReg  CAN_IER;       /**< \brief (Can Offset: 0x0004) Interrupt Enable Register
  WoReg  CAN_IDR;       /**< \brief (Can Offset: 0x0008) Interrupt Disable Register
  RoReg  CAN_IMR;       /**< \brief (Can Offset: 0x000C) Interrupt Mask Register
  RoReg  CAN_SR;        /**< \brief (Can Offset: 0x0010) Status Register
  RwReg  CAN_BR;        /**< \brief (Can Offset: 0x0014) Baudrate Register
  RoReg  CAN_TIM;       /**< \brief (Can Offset: 0x0018) Timer Register
  RoReg  CAN_TIMESTP;   /**< \brief (Can Offset: 0x001C) Timestamp Register
  RoReg  CAN_ECR;       /**< \brief (Can Offset: 0x0020) Error Counter Register
  WoReg  CAN_TCR;       /**< \brief (Can Offset: 0x0024) Transfer Command Register
  WoReg  CAN_ACR;       /**< \brief (Can Offset: 0x0028) Abort Command Register
  RoReg  Reserved1[46];
  RwReg  CAN_WPMR;      /**< \brief (Can Offset: 0x00E4) Write Protect Mode Register
  RoReg  CAN_WPSR;      /**< \brief (Can Offset: 0x00E8) Write Protect Status Register
  RoReg  Reserved2[69];
  CanMb  CAN_MB[CANMB_NUMBER]; /**< \brief (Can Offset: 0x200) MB = 0 .. 7
} Can;


If you look in the files I referenced you'll find a whole bunch of definitions. You can reference back and forth between those things and section 41 of the SAM3XA docs to figure things out.

However, the whole point of the canbus library is to make it so that you don't have to worry about all of these details. Palliser, I, and others have already been working on the library so that you don't have to worry about all these low-level details. My hope is that soon nobody will really wonder about can_mb_conf_t or any of the other low level structures. In my version of the lib I already basically removed all references and uses of that struct since it seems overly tied to hardware and complex to work with.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: lufegimenez on Mar 05, 2013, 06:08 pm
Thank you a lot!

I appreciate very much your work. I know how difficult it is to write libraries that match the needs of everybody. I will give a good use of this info!

I grow up in engineering through pages and pages of datasheets (when you had to get them in paper, long long time ago...) so although it is not the Arduino Spirit, sometimes I prefer go to a low level...

In any case THANK YOU!!!!! You guys have my full respect!

Lufe
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: robertspark on Mar 10, 2013, 02:13 am
Hello all, firstly may I please apologise if this appears to be a daft question, I am new to Arduino Can Bus on the Due.

I've had a look at the examples given and the library and the SAM manual section 41.7.2.1, but I'm at a loss to understand how to setup the Due to receive ALL messages regardless of id.

Or if it was setup to receive SAE J1939 messages (29 bit messages) that uses a 3 bit priority, then a 18 bit Parameter Group Number (PGN), and then an 8 bit source address for the message.  The PGN allows for destination specific communication and broadcast communication.  How to set it up to receive a range or all PGN messages or a bank of PGN messages.

Thanks for your time & advice in advance.

Rob
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Mar 11, 2013, 02:53 pm
Hello Rob,

Above all, I would like to know if you are referring to AdderD last example updates in the CAN library.
If not, I would recommend you to download his zip file (see reply #118 in this thread).

What I understand from you question is that you need a kind of CAN traffic reader or so.
I believe AdderD can answer better your question but what I know from a CAN node as receiver independent from ID
is that, at least, you have to set the following parameters:

1. Controller: You have to chose controller 0 or 1 as receiver.
2. Protocol: You have to chose Standard or Extended.
3. Baud rate: You have to chose the kBit/s of the emitter.

Examples 1-3 work in standard mode and example 4 in extended.

In reference to the ISO definition: Our CAN library implemented for Arduino Due is defined under ISO/11898A (2.0 Part A and 2.0 Part B) standards for high speeds and ISO/11519-2 for low speeds. The standard you mentioned (SAE J1939) is equivalent to ISO 11992, thus, I need to check if that definition works. I.E. in my personal case, I have a Honda Odyssey that uses ISO 14230-4, ISO 9141-2 (also called K-line protocol) that sadly doesn't work with the library.

Just one note to think. Even though SAE J1939 includes CAN 2.0b, also defines higher layers (above the Physical and Data Link layers), that in my opinion, lie beyond the reach of the CAN here. 


Hello AdderD,
Any ideas? Thank you.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Mar 11, 2013, 07:30 pm

Hello all, firstly may I please apologise if this appears to be a daft question, I am new to Arduino Can Bus on the Due.

I've had a look at the examples given and the library and the SAM manual section 41.7.2.1, but I'm at a loss to understand how to setup the Due to receive ALL messages regardless of id.

If you want to receive all messages then one easy way to do that is to just set your mask to all zeros. In example 4 you can see lines like this:
Code: [Select]
  CAN.mailbox_set_accept_mask(0, 0x1FFFFFFF, true);.
Well, setting the accept mask to zero makes it totally accepting.

Quote

Or if it was setup to receive SAE J1939 messages (29 bit messages) that uses a 3 bit priority, then a 18 bit Parameter Group Number (PGN), and then an 8 bit source address for the message.  The PGN allows for destination specific communication and broadcast communication.  How to set it up to receive a range or all PGN messages or a bank of PGN messages.


Once again, you would set the acceptance mask. However, for these examples you would also need to set an ID to match against. I know it will probably be easier if I give some examples. I don't remember J1939 off hand but lets say the top three bits are the priority, the next 18 PGN and the lowest 8 the source. So, with that in mind lets say you want to accept only messages with PGN of 0x3946B. You don't care about the priority and you don't care about the source. So, anything you don't care about should be a 0 in the bit mask and everything you do care about should be a 1. You want to match only one PGN so you want to match every bit in the PGN. This makes your acceptance mask: 0b00000011111111111111111100000000 = 0x3FFFF000. You then set the ID for the mailbox to be the ID you want to accept. You have to keep in mind that the PGN is shifted up 8 bits in the canbus ID so you take your desired ID of 0x3946B and shift it up 8 bits to yield 0x3946B00. This value goes in the ID field. The result looks like this:
Code: [Select]

CAN.mailbox_set_accept_mask(0, 0x3FFFF000, true);
CAN.mailbox_set_id(0, 0x3946B00, true);


In that code the true's at the end mean to use extended addresses. False would have meant standard (11 bit) IDs.

Now, what if you know that there a group of PGNs you want to receive. Let's say you need to accept PGN 0x39460 - 0x3946F (16 PGN codes) and further that you only want to accept from a source address that ranges from 0x50 - 0x57. So, you want to match everything but the last 4 bits of the PGN which you'll allow to be whatever (this gives you the 16 PGN codes listed above.) You also want to match everything but the last 3 bits of the source. So, this makes your acceptance mask 0b00000011111111111111000011111000 which is 0x3FFF0F8. You make your ID that you set be the lower bounds of the two values (PGN = 0x49460 and source = 0x50). This makes your ID 0x4946050. So, the relevant code would be:
Code: [Select]

CAN.mailbox_set_accept_mask(0, 0x3FFF0F8, true);
CAN.mailbox_set_id(0, 0x3946050, true);


If you need to accept ranges that aren't possible with a single bit pattern then you need to set up multiple mailboxes, each with the separate settings to accept each discrete pattern.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: robertspark on Mar 12, 2013, 01:10 am
AdderD, thank you very much, perfect explanation, just what I was looking for, I'm a whole lot clearer on the MAMx, and MIDx registers.  The SAM manual appears to have errors in it.

Looking at table 41.9.15, Can Message Acceptance Mask Register (CAN_MAMx), the table below refers to MIDvA & MIDvB.... which is actually the table for 41.9.16, Can Message ID Register (CAN_MIDx).

Palliser, thanks for your help, I'm basically looking to use SAE J1939 as the PGN arrangement seems to offer everything I'm looking for to be able to transmit quite a bit of multiple sensor / input data in a single CAN message.

The following file may be of interest to someone : http://gurtam.com/files/ftp/CAN/j1939-71.pdf (http://gurtam.com/files/ftp/CAN/j1939-71.pdf).

Many thanks to both of you again,
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: keithspg on Mar 15, 2013, 02:13 am
This is a very interesting topic to me, but I cannot see a way to get this library so I can start playing with it. I can figure out how to download the whole fork, but not just the CAN library. Do I need all of it? I have the latest 1.5.x and was hoping to just add this library so I could start investigating.

I want to create a version of this:
http://mbed.org/users/Just4pLeisure/code/Just4Trionic/

but using Due as a basis instead of the LPC chip.

Looking for help to get started with this.

Keith
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Mar 15, 2013, 02:41 am
I have since broken out my version of the canbus library to its own github repo. Here it is: git://github.com/collin80/due_can.git

It is renamed so as to not conflict with the official Arduino Canbus library. Make no mistake, mine is not official, just the one I use. It's more object oriented and, I think, easier to use. But, it's your choice. There is an officially sanctioned library but you have to check out the can branch of the Arduino IDE on github (the whole thing).

But, and this is a big but, you probably have to download the official canbus library branch anyway. There are changes to some of the libsam files that are necessary for both canbus libraries to properly compile. I suppose I really should figure out what those changes are and import them into my library so that stock 1.5.2 will work. I'll try to do that within a few days. So, it might be best to wait a few days.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: keithspg on Mar 15, 2013, 04:33 am
AdderD

Thanks! I'll wait and see what happens. Please post back to this thread when you get it resolved.

Regards,

Keith
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Mar 17, 2013, 01:15 am
Ok, I just checked into GIT a fix to allow the due_can library to work with stock 1.5.2. So, you don't need to check out the Arduino code from git to use it anymore.

Heres the link again: git://github.com/collin80/due_can.git

If you don't want to use git you can just download the zip of the master at this link: https://github.com/collin80/due_can/archive/master.zip
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Sherlock3112 on Mar 23, 2013, 03:28 am
Just want to say thanks for the work you're putting in AdderD. Dropped the delay to 10 microseconds and made it report to serial every 50,000 instead and it's whirring along at over 30 million packets after a few minutes.

Started putting together my own code using the samples as a base, and I've come across something that has me stumped although that could be as much to do with the time as anything else.

The second sample says it uses interrupts rather than polling, but as far as I see in the code it runs the 'test1()' function after enabling the interrupt. The test1() function sends data from CAN2 then hits a while loop waiting for CAN to have received data - isn't this still polling rather than using interrupts? I was hoping to be able to do something else whilst waiting for data to be received...
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Mar 23, 2013, 05:19 pm

The second sample says it uses interrupts rather than polling, but as far as I see in the code it runs the 'test1()' function after enabling the interrupt. The test1() function sends data from CAN2 then hits a while loop waiting for CAN to have received data - isn't this still polling rather than using interrupts? I was hoping to be able to do something else whilst waiting for data to be received...


It is using interrupts. The interrupt puts the frame into a software buffer. Then you poll for new messages when you're ready to receive them. This is better than normal polling in that multiple messages can come in and you don't have to worry about checking frequently enough to prevent overruns. So, I suppose you could say that both interrupts and polling are taking place. The alternative is callbacks. This breaks into your code and won't always happen at an opportune time.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Sherlock3112 on Mar 24, 2013, 03:32 pm
Great, thanks for the clarification. I was concerned that if I'm doing other things before checking for the message then I might miss some off the bus. :)
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Mar 24, 2013, 06:43 pm
By default the canbus library (well, my version) has a 32 entry rolling receive buffer so you can afford to have that many messages come in while you are busy. Changes are you won't be getting that many behind your back unless the bus is *really* busy.

Also, FWIW, I just updated the library. I fixed the examples to call the proper header and added keywords.txt so things are actually highlighted/colored properly.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: DarkKnightWong on Mar 25, 2013, 04:00 pm
Hi Everyone

Ive been reading up on this thread for a while now. 
I tinker around with old cars, and have been wanting to figure out how to put a gauge cluster out of a 09 Corvette into my current project.
Im a software developer by day and junk yard mechanic by night :-)

I would like to get started with the Arduino DUE to tinker with the can bus on the cluster.
Its GMLAN which I read that they have a Highspeed and low speed can bus in there system.

Im about to order the DUE, but what other hardware should I order to get me started using the CAN API.
I come from a OO background so I probably will look into AdderD branch of the CAN API, since he mention its a little more structured for OO

I saw earlier post that people are making a shield with SN65HVD243..  I search online a bit and havnt found a place to order these...
What other fun stuff will I need to order ?  And possibly from where if its allowed to be posted ..

Thanks in advance!  Cant wait to get started
Allen
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Mar 25, 2013, 06:13 pm
I ordered most of my stuff from Mouser. The transceiver chip ends in 234 which is maybe why you can't find it. Here it is at Mouser: http://www.mouser.com/_/?Keyword=SN65HVD234&FS=True

You will need one of those chips for each canbus port (so, two per Due) and some misc other goodies (60 or 120 ohm resistors, small value capacitors, etc). Also, use a schmartboard 1.27mm arduino board which is part # 206-0004-02 (also available at mouser). This board has a place to put the transceiver chips as well as having pins that go into a normal arduino board. It is not the proper size for a Due but you can run jumper wires to the proper pins of the Due. It's sort of a cobble job at the moment but it does work.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: DarkKnightWong on Mar 25, 2013, 06:33 pm

I ordered most of my stuff from Mouser. The transceiver chip ends in 234 which is maybe why you can't find it. Here it is at Mouser: http://www.mouser.com/_/?Keyword=SN65HVD234&FS=True

You will need one of those chips for each canbus port (so, two per Due) and some misc other goodies (60 or 120 ohm resistors, small value capacitors, etc). Also, use a schmartboard 1.27mm arduino board which is part # 206-0004-02 (also available at mouser). This board has a place to put the transceiver chips as well as having pins that go into a normal arduino board. It is not the proper size for a Due but you can run jumper wires to the proper pins of the Due. It's sort of a cobble job at the moment but it does work.


Hi AdderD

Thank you for the links and info.
The page with the transceiver chip has different ones.
Which one do I need and whats the difference between these ?
SN65HVD234D
SN65HVD234DG4

Can you recommend a Arduino bread board start kit also ?

Thanks Again
Allen
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Sherlock3112 on Mar 25, 2013, 09:05 pm
Thanks for your earlier support AdderD, and the continued development. I've got the Due happily taking data off a 1MHz bus with a DTA ECU and accelerometer spamming it.

Code: [Select]
CAN message received:
13036 0x2000 0x1EFF Ext 8b 0 0 5C 0 14 0 12 0 
13042 0x498 0x1EFF Std 8b 7F CD 7F 4F 88 34 80 7 
13049 0x2001 0x1EFF Ext 8b 64 0 3 2 0 0 11 0 
13055 0x2002 0x1EFF Ext 8b 2C 1 3C 0 79 0 0 0 
13061 0x2003 0x1EFF Ext 8b 0 0 0 0 0 0 0 0 
13067 0x2004 0x1EFF Ext 8b CD E 0 0 0 0 0 0 
13073 0x2005 0x1EFF Ext 8b 0 0 0 0 1F 1 42 3 


Working a treat. I don't suppose anyone knows if there's some industry standard for converting data put out by the ECU to something useable? I know our ECU has 6 data frames (0x2000-0x2005), each 8-byte with 2 bytes for each value (engine rpm, throttle position etc) but not sure if there's a standard way to turn this into something useable (0x5C00 doesn't mean much as an idle throttle position!) or if I'm going to have to ask the ECU manufacturer nicely to give us some help converting.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Mar 25, 2013, 10:13 pm
Hello Sherlock3112,

I am very happy seen your progress with Due and CAN.

Based on my experience, most manufacturers of devices/equipment with CAN capability, should have available what is called "CAN matrix" of the correspondent device/equipment. Such CAN matrix should contain, among other things, CAN identifiers, Length, Byte and Bit descriptions, access (read or write only), range, resolution, units, etc.. I believe, a good step for you is to contact the ECU manufacturer. We could make also time to help you but we need from your device at least the Brand, Model and firmware version/revision (if apply). Good luck!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Sherlock3112 on Mar 25, 2013, 11:29 pm
Thanks Palliser. I had a feeling that would be the case, but thought it was worth checking if there was a standard before I call them and look silly. ;)

It's a less-than-common ECU - a DTA fast S series. I'll get on to them tomorrow and try to get a copy of the matrix. All they give us on their site is a basic list identifying which byte(s) of each frame relate to which sensors.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Mar 26, 2013, 12:55 am

Thank you for the links and info.
The page with the transceiver chip has different ones.
Which one do I need and whats the difference between these ?
SN65HVD234D
SN65HVD234DG4

Can you recommend a Arduino bread board start kit also ?


I don't have any idea why places like Mouser and Digikey do that... They're the same part. Sometimes they have funny stuff like that where you have the same part with very slightly different part numbers varying only in suffix. Perhaps there is a slight difference of some sort but I don't know what it is. Chances are either would work fine.

As for an Arduino bread board, I did recommend one. The schmartboard is a breadboard with 1.27mm spots for chips that fits on an arduino.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AJK101 on Mar 28, 2013, 06:25 am
Hey, I bought a due a week ago and tried using can with a device I have.
I checked the family id due was sending yesterday, and saw it to be huge. The docs of the device say it is expecting a 11bit cob id.

Googling it ,I found out  about can v2 having 29 bit ids and can v1 having 11 bit ids. ...  my current assumption is that the device is expecting can v1 while due is using v2
So, any idea how I can make due use v1?
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: graynomad on Mar 28, 2013, 06:30 am
Quote
any idea how I can make due use v2?

Do you mean v1?

______
Rob
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AJK101 on Mar 28, 2013, 06:48 am

Do you mean v1?

______
Rob


Yeah, sorry, I meant v1.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Sherlock3112 on Mar 28, 2013, 12:54 pm
Only example 4 in AdderD's code uses extended (29-bit) identifiers. All the other examples use the old standard identifiers. Make sure you're checking against the right variable? The family ID isn't the frame standard/extended id...

The difference is when setting up the mailbox, there's a "true" or "false" at the end of the parameter list - true is extended, false is standard.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Mar 28, 2013, 01:51 pm

Hey, I bought a due a week ago and tried using can with a device I have.
I checked the family id due was sending yesterday, and saw it to be huge. The docs of the device say it is expecting a 11bit cob id.

Googling it ,I found out  about can v2 having 29 bit ids and can v1 having 11 bit ids. ...  my current assumption is that the device is expecting can v1 while due is using v2
So, any idea how I can make due use v1?



Family ID is not what the Due sends. Family ID is a special field that allows you to track which sort of frames a receive mailbox is supposed to accept. When sending what matters is the ID you set. As Sherlock3112 said, the end parameter to several of the commands is either true or false. True means use extended addressing, false means don't (use standard addressing.)

For reference, family ID is there just to help you to do fast branching decisions on messages coming in on receive mailboxes. The docs for the processor go into the gritty details of how that works. But, you should be able to pretty much ignore it.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AJK101 on Mar 29, 2013, 07:51 am
Thanks for the replies, AdderD, Sherlock.

I assumed family id to be three id being sent as it started with 1100, which I read somewhere is supposed to be for sdo, which is what I was trying out ...

Anyway, back to my issue, I directly used the example 1 from CAN0 to CAN2, and it worked (with 2 transceivers )

And as I already mentioned, with the device I'm using, it doesn't work

In the docs of the device, only the bits for the frame are, given, and this is the first time I'm using can ...
What I tried was to check all the values of the registers on the can and mailbox structures.

Could you help me out with this? Basically how do I find out exactly what it's sending, what mask is being used while receiving, what is the mall being applied on (full frame from id, or only data, etc) and finally what it's receiving ...

If you'll could help me out, it'd be awesome ... been trying to get the thing work for a week now ..
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Mar 29, 2013, 05:29 pm
Hello AJK101,
Could you give us more details of your CAN device? Brand, model, firmware version/revision?

Thanks!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AJK101 on Mar 29, 2013, 06:01 pm
Maxon epos 2 controllers 70/10.

The product id according to the website is 375711

Thanks !
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Mar 29, 2013, 07:53 pm
I made a brief review on Maxonmotor website and it looks like your device speaks Canopen. If that is the case, you can't communicate it with Due.
Canopen is a high-level protocol for CAN-bus. I would recommend you to contact them to confirm it.

Edit: Other devices like EPOS 2 P 24/5 (378308) have CAN bus functionality but not the one you mentioned (375711).
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Mar 29, 2013, 08:21 pm
It does appear that the controller mentioned utilizes CANOpen only. I would not say that this makes it impossible to talk to with the Due. However, it would require someone to code up support for CANOpen on top of the existing canbus library. That someone has not yet materialized. I have been thinking about adding support for some higher level protocols but the thing is, well, I just don't have any need for them myself. Pretty much everything I'd interface with uses vanilla canbus. So, the options are, take the library and add on support for CANOpen, find some existing code to do that (though I don't know of anything that really fits the bill), or somehow motivate someone other than you to do it. The joy of open source is that you've got a head start. The downside is that people only work on stuff they want/need.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AJK101 on Mar 30, 2013, 04:10 am
What exactly is the difference between can open and can ?
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Mar 30, 2013, 04:47 am

What exactly is the difference between can open and can ?


CanOpen is a protocol built on top of CAN (usually). It is a higher level protocol. This is much like how Zigbee is built upon 802.15.4 or WiFi is built upon 802.11 or how TCP/IP can be run over ethernet. Imagine CANOpen is TCP/IP and CAN is ethernet. You run CANOpen over CAN. You can easily get all the information you ever wanted on CANOpen by using Google.

CANOpen is a kind of large thing to implement. You don't want to have to reinvent the wheel on that one. So, it pays to start with something existing and make it work with what you have. Here's a promising link:

http://sourceforge.net/projects/canopennode/

Any takers? ;)
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AJK101 on Mar 30, 2013, 07:23 am
ah, this seems much more complicated than I had wagered on ...

I don't have much knowledge about this, so doubt I'd be able to make a whole library for it on my own ...

Could I make some haphazard venture to just "make it work" ?

I mean from the documentation from maxon, i got that it is expecting to receive :
> 1 bit Start of Frame
> 11 bit COB-ID (first 4 bits frame identifier, last 7 the ID of the receiving device)
> 1 bit RTR which should always be 1
> 6 bit control field (1 bit for identifier Extension which is to be dominant (1 ... i assume), 1 reserved bit which should also be dominant, 4 bits for Data Length Code which holds number of bytes being transfered)
> The data bytes that need to be transferred
> 16 bit CRC. To generate the CRC, they have given a function
> 2 bit Acknowledge which should always be 11
> 7 bit End of Frame
> 3 bit with no data transfer (intermission)

Also, they've given an example for this also, with what the COB-ID should be like (0x600 + NODE_ID) and the data bytes should be...

... now As i had mentioned in my earlier post, if I knew exactly what Due sends via can, i.e. like which element of the structure is the ID, which element is data, etc ... I think i could achieve basic communication.

What I mean is like this :  irrespective of the comm protocol, as long as i have a register somewhere that if i set it to : 1100000000111000111111111.. etc it will just put a start of frame, send the data, put end of frame and wait for the intermission, i can make it work

(this is something like UART, where i just put Serial.write('w'), it sends startframe + 'w' + endframe ... assuming no parity etc was enabled, now the 'w' may be according to some higher protocol, but for the transmitting_buffer, it doesn't really matter)

And also, for receiving, there would again be a receiving buffer (register) which i can just read in a continuous while loop ... like : i'll just put ...
if( CAN_RX_FLAG == true) {
    my_msg = CAN_RX_BUF;
    if(my_msg == 0x6d64f74e28.. )
      Serial.print("comm was successful, motor is running ! ");
}

So ... any idea if some buffer like this are there ? or if I can use those structures in libsam for these ? Or maybe even instead of buffers, can I use mailboxes like this (as you said CanOpen is a higher protocol of Can, mailboxes would be there also ... i assume)
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Mar 31, 2013, 03:55 am

Could I make some haphazard venture to just "make it work" ?

I mean from the documentation from maxon, i got that it is expecting to receive :
> 1 bit Start of Frame
> 11 bit COB-ID (first 4 bits frame identifier, last 7 the ID of the receiving device)
> 1 bit RTR which should always be 1
> 6 bit control field (1 bit for identifier Extension which is to be dominant (1 ... i assume), 1 reserved bit which should also be dominant, 4 bits for Data Length Code which holds number of bytes being transfered)
> The data bytes that need to be transferred
> 16 bit CRC. To generate the CRC, they have given a function
> 2 bit Acknowledge which should always be 11
> 7 bit End of Frame
> 3 bit with no data transfer (intermission)


The arrangement you listed above is just how CAN is sent over the wires. That's just vanilla CAN.

Quote

Also, they've given an example for this also, with what the COB-ID should be like (0x600 + NODE_ID) and the data bytes should be...


Yeah, now you're getting into CANOpen related stuff.

Quote

... now As i had mentioned in my earlier post, if I knew exactly what Due sends via can, i.e. like which element of the structure is the ID, which element is data, etc ... I think i could achieve basic communication.

What I mean is like this :  irrespective of the comm protocol, as long as i have a register somewhere that if i set it to : 1100000000111000111111111.. etc it will just put a start of frame, send the data, put end of frame and wait for the intermission, i can make it work

(this is something like UART, where i just put Serial.write('w'), it sends startframe + 'w' + endframe ... assuming no parity etc was enabled, now the 'w' may be according to some higher protocol, but for the transmitting_buffer, it doesn't really matter)

And also, for receiving, there would again be a receiving buffer (register) which i can just read in a continuous while loop ... like : i'll just put ...
if( CAN_RX_FLAG == true) {
    my_msg = CAN_RX_BUF;
    if(my_msg == 0x6d64f74e28.. )
      Serial.print("comm was successful, motor is running ! ");
}

So ... any idea if some buffer like this are there ? or if I can use those structures in libsam for these ? Or maybe even instead of buffers, can I use mailboxes like this (as you said CanOpen is a higher protocol of Can, mailboxes would be there also ... i assume)


Have you even downloaded my version of the library and looked at the four examples? The answers to your questions are pretty clear when you look at the examples.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Transistorfips on Apr 01, 2013, 07:58 pm
Hi guys,
I tried to download
https://github.com/arduino-libraries/arduino-due-can
but it seems it is completely empty. I fiddled with the github command line tool but also I had no success. All empty.
What is wrong?


Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Apr 01, 2013, 09:45 pm
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 https://github.com/arduino/Arduino
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.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Transistorfips on Apr 02, 2013, 07:10 am
Thanks alot. I'll test it the next days.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: dammien on Apr 02, 2013, 12:01 pm
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 !  :)

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.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Sherlock3112 on Apr 04, 2013, 12:06 am
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. :)

Thanks for the hard work put in so far, and I look forward to seeing where this library goes in future!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jspobuk on Apr 06, 2013, 12:10 am
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

Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Apr 06, 2013, 05:25 am
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
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Apr 06, 2013, 05:54 am
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
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Sherlock3112 on Apr 06, 2013, 08:09 pm
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.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: DarkKnightWong on Apr 06, 2013, 09:36 pm

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
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Transistorfips on Apr 07, 2013, 07:22 am
[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.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Sherlock3112 on Apr 07, 2013, 02:00 pm

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.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Transistorfips on Apr 07, 2013, 08:30 pm
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.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Transistorfips on Apr 08, 2013, 02:34 pm
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.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: graynomad on Apr 08, 2013, 03:40 pm
Quote
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
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Apr 08, 2013, 04:43 pm

Quote
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.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Transistorfips on Apr 08, 2013, 04:57 pm
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.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: tbit on Apr 08, 2013, 09:57 pm
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?
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Apr 08, 2013, 10:40 pm
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
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jspobuk on Apr 09, 2013, 03:51 am


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


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. :)

Thanks
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Apr 09, 2013, 04:24 am
Hello jspobuk,

Search for the following:

Quote
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.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: tbit on Apr 09, 2013, 05:30 am
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.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Transistorfips on Apr 10, 2013, 12:46 pm
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




Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jspobuk on Apr 10, 2013, 08:26 pm
Quote
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
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Apr 10, 2013, 08:55 pm
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:

Quote
...\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.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Sherlock3112 on Apr 13, 2013, 03:33 am
A few pictures from today's testing. Seems to be going well so far...

(http://i.imgur.com/4nLCNHvs.jpg) (http://imgur.com/4nLCNHv.jpg) (http://i.imgur.com/NDPeinds.jpg) (http://imgur.com/NDPeind.jpg) (http://i.imgur.com/dczp5SHs.jpg) (http://imgur.com/dczp5SH.jpg) (http://i.imgur.com/Z9UyQNzs.jpg) (http://imgur.com/Z9UyQNz.jpg)

Just need to get a suitable power supply for the arduino plumbed into the car and decide what other features I want to get in.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Apr 13, 2013, 06:41 pm
Hello Sherlock3112.

Thank you for posting pictures of your project! A kind of CAN/XBEE/Ethernet/PC system I assume.
About the PC software, it looks interesting. Formula Student 2013. Could you tell us a bit about your interface .NET?
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Sherlock3112 on Apr 13, 2013, 06:44 pm
Yeah, using VB .NET with the NI measurement studio to make the gauges/graphing a bit easier. The ethernet shield is only in there for the SD card slot atm until I get hold of one of the newer wireless shields.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: cmaglie on Apr 19, 2013, 10:08 am
Pallise, AdderD

I see big progress on CAN library, nice work!
I see that there is also some confusion on how to install the library: can I do some "cleaning"
work on the Arduino repository? Do you want me to merge something?

Or maybe its better to remove the CAN library from the main repository, so you can work autonomously?

Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Apr 20, 2013, 06:22 pm
Hello Cristian,

Above all, thank you very much for your words and interest to consolidate the Arduino Due CAN API library.
So far, I have been testing both sets of CAN files (AdderD's and mine) and since you have asked, I think it is time for an update. AdderD files should replace my files on Arduino github given that his files are compiling/running OK and they are more high leveled, more automotive-oriented and more user friendly, even though few people have been downloading/working with them. I want to mention that some updates need to be done in the new Arduino IDE 1.5.3 (AdderD and me could PM you about it) that will help making easier the installation of the files. Finally I would like to thank AdderD and you for all the time, effort and support here with the CAN library.

AdderD,
What do you think?

Regards!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Apr 20, 2013, 08:32 pm
Yeah, go ahead and merge my code in. It seems to be working for a number of people. I'll keep at it when I've got time. There's still plenty to do but as it sits it pretty much works.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: graynomad on Apr 21, 2013, 02:32 am
Quote
even though few people have been downloading/working with them

Doesn't mean we're not interested though, the board Rockwallaby an I are designing will have one (maybe two) CAN transceivers as standard equipment.

So we're interested in how this goes, just nowhere near ready to use any code yet.

The reason there may only be one xceiver is that one of the CANRX signals clashes with DAC1 so to have both is a pain.

______
Rob
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: aethaniel on Apr 23, 2013, 04:19 pm
Hello,

13 pages on this topic isn't enough :-D

Here is more CAN stuff, as I just discovered this while taking news of OpenXC project: https://github.com/openxc/cantranslator

Maybe this can help you going further.

I don't remember who posted about CanOpenNode project hosted on Sourceforge (http://sourceforge.net/projects/canopennode), but it seems also really interesting for people wanting to move on higher industrial bus (interoperability).
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Apr 23, 2013, 05:34 pm
The code within the OpenXC project seems reasonably decent. There might be some potential to use some of it. I think I'm the one who mentioned canopennode before. However, it seems a bit hard to understand. My goal is to end up with a canbus library that has legible source code that people can easily read and/or modify to suit their needs. Canopennode isn't terrible in this regard but it isn't quite up to the level I'd like to get to. Still, it is a good reference for an implementation of canopen.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: zabaat on Apr 23, 2013, 08:10 pm
Thank you all again for your wonderful work - just vouching for the awesomeness of Adder's work

I really liked this sparkfun shield
(http://i.imgur.com/G4AU6wH.jpg)
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: zabaat on Apr 24, 2013, 10:57 am
Maybe a little help here though. I would like to send more than 8 bytes, the following excerpt of code was my best guess but it sits there with a blank stare.

What do I need to change if I want to send this other data?

Thanks!


Code: [Select]
#define TEST1_CAN_COMM_MB_IDX    0
#define TEST0_CAN_TRANSFER_ID    0x07
#define TEST1_CAN_TRANSFER_ID    0x08
#define TEST2_CAN_TRANSFER_ID    0x08
#define TEST3_CAN_TRANSFER_ID    0x09
#define TEST4_CAN_TRANSFER_ID    0x0A
#define TEST1_CAN0_TX_PRIO       15
#define CAN_MSG_DUMMY_DATA       0x55AAAA55


      CAN.init(SystemCoreClock, CAN_BPS_250K);
  CAN.reset_all_mailbox();
  // Initialize CAN0 mailbox 0 as transmitter, transmit priority is 15
  CAN.mailbox_init(0);
  CAN.mailbox_init(1);
  CAN.mailbox_init(2); 
  CAN.mailbox_init(3);
  CAN.mailbox_init(4); 
  CAN.mailbox_set_mode(0, CAN_MB_TX_MODE);
  CAN.mailbox_set_mode(1, CAN_MB_TX_MODE);
  CAN.mailbox_set_mode(2, CAN_MB_TX_MODE);
  CAN.mailbox_set_mode(3, CAN_MB_TX_MODE);
  CAN.mailbox_set_mode(4, CAN_MB_TX_MODE); 
  CAN.mailbox_set_priority(0, TEST1_CAN0_TX_PRIO);
  CAN.mailbox_set_priority(1, TEST1_CAN0_TX_PRIO);
  CAN.mailbox_set_priority(2, TEST1_CAN0_TX_PRIO);
  CAN.mailbox_set_priority(3, TEST1_CAN0_TX_PRIO);
  CAN.mailbox_set_priority(4, TEST1_CAN0_TX_PRIO);
 
  CAN.mailbox_set_accept_mask(0, 0, false);
  // Prepare transmit ID, data and data length in CAN0 mailbox 0
  CAN.mailbox_set_id(0, TEST0_CAN_TRANSFER_ID, false);
  CAN.mailbox_set_datal(0, CAN_MSG_1);
  CAN.mailbox_set_datah(0, CAN_MSG_2);
  CAN.mailbox_set_datalen(0, MAX_CAN_FRAME_DATA_LEN);

  CAN.mailbox_set_id(1, TEST1_CAN_TRANSFER_ID, false);
  CAN.mailbox_set_datal(1, CAN_MSG_3);
  CAN.mailbox_set_datah(1, CAN_MSG_4);
  CAN.mailbox_set_datalen(1, MAX_CAN_FRAME_DATA_LEN);

  CAN.mailbox_set_id(2, TEST2_CAN_TRANSFER_ID, false);
  CAN.mailbox_set_datal(2, CAN_MSG_5);
  CAN.mailbox_set_datah(2, CAN_MSG_6);
  CAN.mailbox_set_datalen(2, MAX_CAN_FRAME_DATA_LEN);

  CAN.mailbox_set_id(3, TEST3_CAN_TRANSFER_ID, false);
  CAN.mailbox_set_datal(3, CAN_MSG_7);
  CAN.mailbox_set_datah(3, CAN_MSG_8);
  CAN.mailbox_set_datalen(3, MAX_CAN_FRAME_DATA_LEN);

  CAN.mailbox_set_id(4, TEST4_CAN_TRANSFER_ID, false);
  CAN.mailbox_set_datal(4, CAN_MSG_9);
  CAN.mailbox_set_datah(4, CAN_MSG_DUMMY_DATA);
  CAN.mailbox_set_datalen(4, MAX_CAN_FRAME_DATA_LEN);
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: graynomad on Apr 24, 2013, 12:08 pm
I'm doing much of the hardware design for the Due board Rockwallaby and I are doing, I'm at the point of choosing CAN transceivers for the board. Some quotes from the previous 13 pages.

Quote
SN65HVD233 has the Loopback funktion
SN65HVD234 has the Standby mode and the
SN65HVD235 has the Autobaud option,

Quote
I have chosen two SN65HVD234 CAN transceivers for my first shield (I am considering to use 235's later).

Quote
even thou I have done successful tests with the TI SN65HVD234 transceivers, we don't know precisely if they are going to be the best and final choice.

Quote
I decided to use two SN65HVD235 transceivers in order to compare them with the two SN65HVD234

Quote
I just ordered some 233's from TI.

Quote
surely want to migrate to 234 to take advantage of the Due libraries for ultra-low current sleep mode.


It seems that either the 233 or 234 are becoming the transceivers of choice. As they are physically compatible with the only difference being the function of pin 5 which can be controlled by the library it may not affect me at this point.

But does anyone have an opinion on the transceiver choice?

______
Rob
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Apr 24, 2013, 03:29 pm

I'm doing much of the hardware design for the Due board Rockwallaby and I are doing, I'm at the point of choosing CAN transceivers for the board.

It seems that either the 233 or 234 are becoming the transceivers of choice. As they are physically compatible with the only difference being the function of pin 5 which can be controlled by the library it may not affect me at this point.

But does anyone have an opinion on the transceiver choice?


I used 234 transceivers on my protoboard and also they're being used for another project I'm involved with. But, for the most part it really doesn't matter. The 233 support loopback but you can loopback anyway when you have two transceivers by just connecting them together. I'm not a huge fan of autobaud but maybe it'd be kind of nice if you or whoever us using your Rockwallaby doesn't know the proper baud of the bus. 234 is nice because it allows you to basically disconnect the transceiver from the bus and turn off like it isn't even there at all. It really depends on your requirements. I'd say 234 or 235 would be the best choice but it depends on whether you value the ability to turn off the transceiver more than you value autobaud.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Apr 24, 2013, 03:41 pm

Maybe a little help here though. I would like to send more than 8 bytes, the following excerpt of code was my best guess but it sits there with a blank stare.

What do I need to change if I want to send this other data?

Thanks!


It seems to me that the only thing you're really lacking is that you forgot to pull the trigger at the end. You have to tell it to send those mailboxes once you load them. You can do this one at a time, stagger them, or however you wish. If you want to try to tell it to send all of the mailboxes as fast as it can then do this:

Code: [Select]

 CAN.global_send_transfer_cmd(CAN_TCR_MB0 | CAN_TCR_MB1 | CAN_TCR_MB2 | CAN_TCR_MB3 | CAN_TCR_MB4);  //send frame in mailbox 0-4


I should probably abstract that a bit. It looks kind of ugly as-is.

Edit: BTW... I just changed my displayed user name, still the same person, different ID. This matches my github account (where the due_can library is hosted.)
Title: Re: Building a CAN communication protocol interface for Arduino DUE
Post by: Markus_L811 on Apr 24, 2013, 03:42 pm
Quote my self...


SN65HVD233 has the Loopback funktion

SN65HVD234 has the Standby mode and the

SN65HVD235 has the Autobaud option,

you need to choose the Chipfunction you want, but the SAM supports Autobaud and Sleepmode only the Loopback funktion is missing.


I did not know if the functions allready implemented in the library, but compared to the functions of the SAM and the TI Transciever I would choose the the SN65HVD233 to make it complete without having some option 2 times
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: graynomad on Apr 24, 2013, 05:27 pm
I think the Atmel designers have made the choice for me. I was leaning towards the 234 anyway because of the EN function that allows me to save power, then I remembered that CANRX1 shares a pin with DAC0 so it's important to be able to tri-state CANRX1 if you are using the DAC.

So it's the 234 for me, no choice unless I add a FET switch or something.

If the library doesn't support controlling the EN pin (it should, we all want to save power :)) then I'll cross that bridge when I come to it.

______
Rob
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Apr 24, 2013, 06:24 pm

If the library doesn't support controlling the EN pin (it should, we all want to save power :)) then I'll cross that bridge when I come to it.


It does *use* the enable pin. But, right now as soon as CAN and CAN2 are instantiated they immediately enable the transceiver and leave it enabled. So, the library basically assumes you're using a 234 chip. This has been accurate for the most part. Since they're instantiated automatically for you this means that the current library forces the relevant pins to be CAN and enables the transceiver forever. Of course, not everyone wants this behavior so in the future it should be possible to turn off the canbus hardware if required. This is not hard to do at all. I think that the default behavior of automatically turning everything on is still the way to go. If you don't want that you can not include the library in which case it won't do that. ;) But, yes, on the to-do list is being able to disable and re-enable the hardware.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Apr 24, 2013, 11:39 pm
In the github CAN samples 1 & 2, I used four pins to control the two transceivers as follows:

CAN0 Transceiver
- pin A7 (D61) to enable/disable the lowpower mode.
- pin A8 (D62) to enable/disable the transceiver.

CAN1 Transceiver
- pin A9 (D63) to enable/disable the lowpower mode.
- pin A10 (D64) to enable/disable the transceiver.

But you can choose the pins that best suits your needs. For example, let's say you want to use pin 46 (D46) to enable the lowpower mode in the CAN1 transceiver, you have to use the following code:

Code: [Select]
SN65HVD234_SetRs(&can1_transceiver, 46);
SN65HVD234_EnableLowPower(&can1_transceiver);


Or, if you want to use pin A4 (D58) to restore normal lowpower mode (disable) in the CAN0 transceiver, you have to use the following code:

Code: [Select]
SN65HVD234_SetRs(&can0_transceiver, 58);
SN65HVD234_DisableLowPower(&can0_transceiver);


Similar case to enable or disable the transceivers. For example, you want to use pin 14 (D14) to disable the CAN0 transceiver, you have to use the following code:

Code: [Select]
SN65HVD234_SetEN(&can0_transceiver, 14);
SN65HVD234_Disable(&can0_transceiver);


I hope that the foregoing clarify a bit more the use of the control pins for the CAN transceivers.

EDIT: Of course, the four pins you choose and add to your sketch shall be connected to the transceivers pins EN ans Rs accordingly.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: graynomad on Apr 25, 2013, 05:19 pm
Sounds like it's covered. You didn't use a resistor on RS in the post #33 schems, I think I'll add one, value TBD.

______
Rob
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Apr 25, 2013, 06:49 pm

Sounds like it's covered. You didn't use a resistor on RS in the post #33 schems, I think I'll add one, value TBD.

______
Rob


Hello Rob,

At this time, this is a very good point. Thanks for mention it because it allows me to clarify something:

Knowing that the most important thing in the first stages of this project was to establish a good communication between nodes, I forced a 'direct mode' in the transceivers connecting pins 5(EN) to 3V3 and pins 8(Rs) to GND but now that some users out there have achieved this, their next step should be to deal with the connection/control of those pins if they are interested to exploit the low power mode an other features of these transceivers, thus, a 10K pullup resistor must be connected on EN and a 10K pulldown resistor on Rs according to Atmel's original design (doc11156).
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: zabaat on Apr 25, 2013, 07:02 pm

It seems to me that the only thing you're really lacking is that you forgot to pull the trigger at the end. You have to tell it to send those mailboxes once you load them. You can do this one at a time, stagger them, or however you wish. If you want to try to tell it to send all of the mailboxes as fast as it can then do this:

Code: [Select]

 CAN.global_send_transfer_cmd(CAN_TCR_MB0 | CAN_TCR_MB1 | CAN_TCR_MB2 | CAN_TCR_MB3 | CAN_TCR_MB4);  //send frame in mailbox 0-4


I should probably abstract that a bit. It looks kind of ugly as-is.



I made the change - here is my code again. I still am only getting out on the first TEST1_CAN_TRANSFER_ID :(

Thank you!

Quote

#include <Wire.h>
#include "variant.h"
#include <due_can.h>

#define TEST1_CAN_COMM_MB_IDX    0
#define TEST1_CAN_TRANSFER_ID    0x07
#define TEST2_CAN_TRANSFER_ID    0x08
#define TEST3_CAN_TRANSFER_ID    0x08
#define TEST4_CAN_TRANSFER_ID    0x09
#define TEST5_CAN_TRANSFER_ID    0x0A
#define TEST1_CAN0_TX_PRIO       15
#define CAN_MSG_DUMMY_DATA       0x55AAAA55

// CAN frame max data length
#define MAX_CAN_FRAME_DATA_LEN   8

// Message variable to be send
uint32_t CAN_MSG_1 = 0;
uint32_t CAN_MSG_2 = 0;
uint32_t CAN_MSG_3 = 0;
uint32_t CAN_MSG_4 = 0;
uint32_t CAN_MSG_5 = 0;
uint32_t CAN_MSG_6 = 0;
uint32_t CAN_MSG_7 = 0;
uint32_t CAN_MSG_8 = 0;
uint32_t CAN_MSG_9 = 0;
uint32_t CAN_MSG_10 = 0;
uint32_t CAN_MSG_11 = 0;
uint32_t CAN_MSG_12 = 0;

  // Initialize CAN1 mailbox 0 as receiver, frame ID is 0x07
  CAN.init(SystemCoreClock, CAN_BPS_250K);
  CAN.reset_all_mailbox();
  // Initialize CAN0 mailbox 0 as transmitter, transmit priority is 15
  CAN.mailbox_init(0);
  CAN.mailbox_init(1);
  CAN.mailbox_init(2); 
  CAN.mailbox_init(3);
  CAN.mailbox_init(4); 
  CAN.mailbox_set_mode(0, CAN_MB_TX_MODE);
  CAN.mailbox_set_mode(1, CAN_MB_TX_MODE);
  CAN.mailbox_set_mode(2, CAN_MB_TX_MODE);
  CAN.mailbox_set_mode(3, CAN_MB_TX_MODE);
  CAN.mailbox_set_mode(4, CAN_MB_TX_MODE); 
  CAN.mailbox_set_priority(0, TEST1_CAN0_TX_PRIO);
  CAN.mailbox_set_priority(1, TEST1_CAN0_TX_PRIO);
  CAN.mailbox_set_priority(2, TEST1_CAN0_TX_PRIO);
  CAN.mailbox_set_priority(3, TEST1_CAN0_TX_PRIO);
  CAN.mailbox_set_priority(4, TEST1_CAN0_TX_PRIO);
  
  CAN.mailbox_set_accept_mask(0, 0, false);
  // Prepare transmit ID, data and data length in CAN0 mailbox 0
  CAN.mailbox_set_id(0, TEST1_CAN_TRANSFER_ID, false);
  CAN.mailbox_set_datal(0, CAN_MSG_1);
  CAN.mailbox_set_datah(0, CAN_MSG_2);
  CAN.mailbox_set_datalen(0, MAX_CAN_FRAME_DATA_LEN);

  CAN.mailbox_set_id(1, TEST2_CAN_TRANSFER_ID, false);
  CAN.mailbox_set_datal(1, CAN_MSG_3);
  CAN.mailbox_set_datah(1, CAN_MSG_DUMMY_DATA);
  CAN.mailbox_set_datalen(1, MAX_CAN_FRAME_DATA_LEN);

  CAN.mailbox_set_id(2, TEST3_CAN_TRANSFER_ID, false);
  CAN.mailbox_set_datal(2, CAN_MSG_5);
  CAN.mailbox_set_datah(2, CAN_MSG_6);
  CAN.mailbox_set_datalen(2, MAX_CAN_FRAME_DATA_LEN);

  CAN.mailbox_set_id(3, TEST4_CAN_TRANSFER_ID, false);
  CAN.mailbox_set_datal(3, CAN_MSG_7);
  CAN.mailbox_set_datah(3, CAN_MSG_8);
  CAN.mailbox_set_datalen(3, MAX_CAN_FRAME_DATA_LEN);

  CAN.mailbox_set_id(4, TEST5_CAN_TRANSFER_ID, false);
  CAN.mailbox_set_datal(4, CAN_MSG_9);
  CAN.mailbox_set_datah(4, CAN_MSG_DUMMY_DATA);
  CAN.mailbox_set_datalen(4, MAX_CAN_FRAME_DATA_LEN);



  // Send out the information in the mailbox
  CAN.global_send_transfer_cmd(CAN_TCR_MB0);
  CAN.global_send_transfer_cmd(CAN_TCR_MB1);
  CAN.global_send_transfer_cmd(CAN_TCR_MB2);
  CAN.global_send_transfer_cmd(CAN_TCR_MB3);
  CAN.global_send_transfer_cmd(CAN_TCR_MB4); 

  CAN.disable();

Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Apr 25, 2013, 08:47 pm


I made the change - here is my code again. I still am only getting out on the first TEST1_CAN_TRANSFER_ID :(

Thank you!

Quote

  // Send out the information in the mailbox
  CAN.global_send_transfer_cmd(CAN_TCR_MB0);
  CAN.global_send_transfer_cmd(CAN_TCR_MB1);
  CAN.global_send_transfer_cmd(CAN_TCR_MB2);
  CAN.global_send_transfer_cmd(CAN_TCR_MB3);
  CAN.global_send_transfer_cmd(CAN_TCR_MB4); 

  CAN.disable();




Why are you disabling the canbus device as soon as you try to send? Those sending commands are just flags that tell the canbus hardware to send out that mailbox when it gets a chance. Likely what is happening is that you send the commands, the bus is free so it starts sending the first frame. Then you disable the hardware so the other four never get a chance to go out. You've got to give it some time to send the frames before you disable the canbus hardware. Figure that a frame could be about 118 bits and then use your baud rate to see how long the minimum time to wait is. At 250k baud this is about .5ms per frame.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: cmaglie on Apr 27, 2013, 11:18 am

I've updated the Arduino "can" branch with https://github.com/collin80/due_can
PM me if there are other changes needed.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: tbit on Apr 29, 2013, 04:48 pm
Got my prototype pcb's in and built one up.....seems to have passed ping-pong and skew testing (thanks for writing those tests btw) although i was using a short piece of cable and doing nothing else on the board. I used 233's with jumpers for 120ohm terminations, RS pin (10k on board), and loopback. I like the 233's w/loopback an example use would be if you are doing a project in which you have 2 CAN ports operating at two different baud rates, loopback lets you do some code testing...just my preference. Looking to do some OBD testing next, Feedback appreciated.

http://tinypic.com/r/1z1tdw9/5 (http://tinypic.com/r/1z1tdw9/5)

Found a painful issue putting the SPI stacking header on....because the DUE has a male header on it the SPI sits higher when stacked than the rest of the headers. This caused some pins to only have partial...of course because i was doing a CAN project in this occurred on the CAN pins! Not sure what i'm going to do about that, maybe for SPI use i have to recommend removal and swapping the DUE to a female stacking header as well.  Feedback appreciated.  
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: sanzoghenzo on May 02, 2013, 11:58 am
Hi all, CAN and arduino noob here...
Fisrt, thank you for the great job done, it always makes me happy seeing so much people involved in open projects :)
I try to make up my mind for a project (http://arduino.cc/forum/index.php/topic,163778.0.html) that uses CAN to talk to pioneer IP-Bus.
Am I restricted to SN65HVD234 transceivers in order to use this library? Or can I use a MCP2551-I/P (to be able to use a breadboard and eventually simple soldering)? Should I write a driver for it?
Thanks for the attention.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on May 02, 2013, 02:42 pm

Am I restricted to SN65HVD234 transceivers in order to use this library? Or can I use a MCP2551-I/P (to be able to use a breadboard and eventually simple soldering)? Should I write a driver for it?
Thanks for the attention.


You can use a tuning fork, a ball of twine, and a pair of jumper cables if you want. The transceiver takes a TTL signal and turns it into a differential signal for canbus. Use whichever transceiver you want. The reason there is a driver file for the 234 transceiver is that it supports being turned on/off under MCU control. Otherwise the canbus code doesn't need to worry about what the transceiver does at all.

Having said that, the MCP2551 is a 5V device. The Due is a 3.3V device. This is not a match made in Heaven. The MCP2551 will probably accept the TX signal coming from the Due as it will be close to 3.3V and that should be enough to trigger the MCP chip. However, the MCP will be sending back a 5V RX signal and that's bad for your Due's health. You can fix that with a voltage divider but it's extra parts. So, yes, you can use it if you know what you're doing.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: LROBBINS on May 02, 2013, 03:59 pm
The 2551 can be put to sleep from the MCU (pin 8 = RS pulled HIGH), but I think you would need voltage shifting to make it work reliably with the due.  Ciao, Lenny
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: sanzoghenzo on May 02, 2013, 04:04 pm
thank you so much for the information. better stick with the SN65HVD234 then!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: neuweiler on May 08, 2013, 11:12 am
Hi folks,

Great work and thanks a lot to all involved! Just wanted to let you know that I finished my build on a proto-board and (surprise !) it worked from the beginning (at least the local loop-back tests).

Here's a pic of my version. It's going to be used in a car conversion (see http://s80ev.blogspot.com)

Note: Although it says 5V on the print, I'm running it on 3.3V only. :)
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: zabaat on May 11, 2013, 11:22 am
(http://i.imgur.com/lHeEtCs.jpg)

So far so good :)

Thanks for the tip about being able to do this Adder
CAN.global_send_transfer_cmd(CAN_TCR_MB0 | CAN_TCR_MB1 | CAN_TCR_MB2 | CAN_TCR_MB3 | CAN_TCR_MB4);

Worked perfectly!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: LROBBINS on May 11, 2013, 12:24 pm
Zabaat,

What are those nice robust-looking connectors on your last photo?

Ciao,
Lenny
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: flyinguy18 on May 15, 2013, 03:47 pm
Hi guys, thanks for all the effort in making this work!
I have been playing with CAN on the Due and have the basics figured out. I am struggling with one thing though... I can send and receive just fine but I need to receive a CAN message and assign its value (in HEX obviously) to a variable. I have been able to use incoming.data[] with some success but assume there is a better way to do this. I would like to assign all 8 bytes to a single variable but understand some limitations to that idea. Sorry for asking a noob question here but its only been a few weeks since I saw the arduino for the first time so I'm in a bit over my head  :~   Just for fun I attached a pic of my breadboard setup and am also working toward manufacturing shields for this...
Thanks in advance for any help!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: maxwest on May 18, 2013, 12:53 am
Hi there,

I did some research for CANopen OpenSource libraries and found the canfestival.org. That seams to have a very active community. canfestival also supports already AVR chips as noted on their "supported devices" page under http://www.canfestival.org/doc.

I saw all the work you have already done for supporting CAN on the due. Great work, amazing.

Do you think it is feasible to integrate the canfestival into the Arduino IDE for Arduino Due and possibly also in Arduino Uno with the seeedstudio or sparkfun can shield?

Thx

Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: tbit on May 18, 2013, 04:27 am
Zabaat - is that a deutsch dtm series pcb mount connectors for use with the oem sealed enclosure they offer?
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on May 19, 2013, 06:11 pm

Do you think it is feasible to integrate the canfestival into the Arduino IDE for Arduino Due and possibly also in Arduino Uno with the seeedstudio or sparkfun can shield?


Hello maxwest,

As an integrator I could say Yes. Integrate canfestival (or any other third-party CANopen protocol like CanOpenNode, etc.) to Due or UNO should be feasible. The problem lies that between CAN bus and CANopen there is an in-construction bridge of missing layers to be built. So far, I'm not aware of any software (code) implementation that fills the gap. Again, it can be done and collin (reply #152) and me have made comments about this matter but let me explain in a dossier this thing.

CAN bus and CANopen (an other CANs) are OSI bus standards to communicate embedded control systems like MCUs between them without an intermediate host.

CAN bus is the lowest level link in the chain and it comprehends basically three layers or profiles.

1. Physical layer. Implemented with a transceiver (SN65HVD234 in my design)
2. Data Link layer. Implemented with a controller (inside SAM3X8E)
3. Application Layer. Implemented with software (our CAN library)

Layers 1 and 2 are known as CAN itself.

CAN open is a higher level protocol. It comprehends the first two layers of CAN bus plus other layers above, as follows:

3. Application Layer (non existent for DUE) - communication - I/O - drives -motion control - programmable devices
4. Transport Layer (software-reliability, segmentation/desegmentation, error control).

Thus, it all about to generate the layers 3 and 4.     

Having said this, now you know, broadly speaking, what it is required to complete the bridge and let the train goes.

There is another thing: Time. How much time can you afford to reconcile canfestival and Due? Right now I am trying to make our new Arduino CAN and EMAC libraries to run in a third-party board based on a SAM3X8C. This MCU has only 100 pins unlike the MCU of the Due SAM3X8E with 144 pins and even though they belong to the same family "X", I have to so some tweak on the current variant files (.cpp and .h) and pin_arduino.h. See that my case is your opposite case, I am integrating a third-party board with CAN library (you want integrate a third-party library with DUE or UNO). My integration is not a big deal given that the differences between both MCUs is minimal. In your case, I would recommend to visit the canfestival community and ask questions. I would recommend also that you start with UNO (AVR) and a CAN shield. I hope this helps you. Good luck!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: reinaldoaf on Jun 12, 2013, 07:15 pm
hi,

I have on hands the tja1042t(High-speed CAN transceiver with Standby mode) http://www.nxp.com/documents/data_sheet/TJA1042.pdf (http://)

I like know if can I use this library to work with this uC, I see both datasheet (SN65HVD234  and TJA1042T -T/3) and both are the transceiver uC to CAN BUS.. the TJA1042T is only 5v and it can burn I/O on Due, for this I will use one circuit to make division, maybe 10k-10k get me 2.5v and this is OK to work.

I see on github the library name is SN56HVD234.c and .h is because this my question about use library on TJA1042.

tks all
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Jun 12, 2013, 07:30 pm
You can try it. The transceiver is really fairly invisible to the way the library works. There is just a small amount of code to support an SN65HVD234. What happens is that the enable pin is set to enabled and the RS pin is controlled to manipulate the slope rate. But, you can easily make a board that does its own enabling and slope control and just not hook that stuff up to the Due at all. So, that brings me to whether you can use your transceiver. I suppose you could. It would probably accept the CANTX signal from the Due just fine. Then, as you said, you have to use a voltage divider or buffer chip to turn the 5V CANRX signal from the transceiver into a 3.3V signal to the Due. Chances are it would accept 2.5v. It isn't ideal but if you're just making something for yourself it makes sense to use whatever you happen to have on the bench.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: dammien on Jun 16, 2013, 11:45 am

Hi All,

Just for information, I made 2 shields : one is based on SN56HVD234, the other uses MCP2551.
I have run some basic tests and it seems to work fine for both.
Concerning the 5V aspect, I use Zener diode (3.3 or 3.6v) on the Rx pin, and it seems to work fine.


Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Transistorfips on Jun 16, 2013, 07:38 pm
Hi guys,
I'm a bit in trouble.

First I started the CAN project with the original ASF example and did very minor changings. Unfortunately the CAN1 rcv interrupt routine doesn't get touched at all. I checked all my hardware with a oscilloscope and it seems it works fine. But no message reveiving, not even a error. Hrrmpfh.

Now I tried to attack with the functional Arduino example. I downloaded https://github.com/collin80/due_can, unzipped it, and copied it to C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries
Since the IDE did not accept the original name "due-can-master" while starting I named it DueCanMaster instead. When I try to compile I get a shipload of errors. Here are a few:


C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2/hardware/tools/g++_arm_none_eabi/bin/arm-none-eabi-g++ -c -g -Os -w -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -Dprintf=iprintf -mcpu=cortex-m3 -DF_CPU=84000000L -DARDUINO=152 -D__SAM3X8E__ -mthumb -DUSB_PID=0x003e -DUSB_VID=0x2341 -DUSBCON -IC:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/libsam -IC:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/CMSIS/CMSIS/Include/ -IC:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/CMSIS/Device/ATMEL/ -IC:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\cores\arduino -IC:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\variants\arduino_due_x -IC:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\libraries\DueCanMaster C:\Users\Joachim\AppData\Local\Temp\build386121661864746555.tmp\Arduino_Due_CAN_Sample_1.cpp -o C:\Users\Joachim\AppData\Local\Temp\build386121661864746555.tmp\Arduino_Due_CAN_Sample_1.cpp.o
In file included from C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/CMSIS/Device/ATMEL/sam3xa/include/sam3x8e.h:260,
                 from C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/CMSIS/Device/ATMEL/sam3xa/include/sam3xa.h:44,
                 from C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/CMSIS/Device/ATMEL/sam3.h:59,
                 from C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/CMSIS/Device/ATMEL/sam.h:189,
                 from C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/libsam/chip.h:25,
                 from C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\cores\arduino/Arduino.h:34,
                 from C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\variants\arduino_due_x/variant.h:26,
                 from Arduino_Due_CAN_Sample_1.ino:7:
C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/CMSIS/CMSIS/Include/core_cm3.h:355: error: #elif without #if
C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/CMSIS/CMSIS/Include/core_cm3.h:655: error: #elif without #if
C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/CMSIS/CMSIS/Include/core_cm3.h:955: error: #elif without #if
C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/CMSIS/CMSIS/Include/core_cm3.h:1066: error: #endif without #if
C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/CMSIS/CMSIS/Include/core_cm3.h:1070: error: #endif without #if
C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/CMSIS/CMSIS/Include/core_cm3.h:1071: error: stray '\265' in program
C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/CMSIS/CMSIS/Include/core_cm3.h:1071: error: stray '\265' in program
C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/CMSIS/CMSIS/Include/core_cm3.h:1073: error: stray '\2' in program
C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/CMSIS/CMSIS/Include/core_cm3.h:1073: error: stray '\270' in program
C:\ARM_ARDUINO\1_5_2\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system/CMSIS/CMSIS/Include/core_cm3.h:1073: error: stray '\210' in program




Why is that? Do I have to change some settings? I have no clue about github so I downloaded the zip file and unzipped it. Something wrong with this way?
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Jun 17, 2013, 01:04 am
Name the directory "due_can" just like the project name.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Transistorfips on Jun 17, 2013, 04:03 pm
Hi Collin, renaming got me in the ballpark (is this yankee phrase suitable here? :-) and after additionally cleaning up the directories it compiles fine. I simply have to little experience with the IDE since all stuff I do with Atmel Studio. Thanks!



Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Transistorfips on Jun 23, 2013, 10:11 pm
Here's a photo of my test hardware. DUE + 2x PIC 18F458.
http://imageshack.us/photo/my-images/24/6urm.jpg/

I'm still researching the deeper sense of some registers since especially the PICs have a behaviour I don't always understand ;-)
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: reinaldoaf on Jun 25, 2013, 02:31 pm
hello, today came the SN65, TJA and the MCP CAN TRANSCEIVERS,

Before I make one board to use with this uC because I still having problens with breadboard assembly,
the picture foto(1) and Image(17) show this board... the top of image(17) show the SN65, I use Schematic of this post (page 3)

I try on with SN65 chip and work fine with DUE_CAN API, Thank You very much for this API

but I'm a very newbie on CAN Interface, I want talk with the board of the foto(2) but I don't know how,

I still reading forum and informations about CAN, and I conect the board direct on the can wire, the bard baud rate is 125KHZ (i change it on start ports) but no answare,

can anybody show one documentation to help me make this comunication.

thank you so much.

and thank you again for the CAN API,
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Transistorfips on Jun 25, 2013, 06:06 pm
Don't you have a schematic of the board you want to connect to?
Usually to connect to the CAN Transceivers is pretty straightforward. Maybe even they are Pin compatible to others too, so simply watch the footprints and follow the lines. Some are high-speed, some aren't. The best expierince I made was with TI's SN65 series which are avavilable for 5V, too. They can be used for both speed ranges. The High-Speed TJA1050 have a strange behaviour when testing with a multimeter, mine lost the signals after some usec and reacted like a monoflop.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jspobuk on Jul 03, 2013, 01:39 am
Hello, sorry to ask a simple question, but I am trying to re-download the files again after reinstalling windows.  I don't see anywhere on this page that I can download the header and .cpp files and examples and such. 
https://github.com/arduino/Arduino/tree/can/hardware/arduino/sam/libraries/CAN#due_can
thanks!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Jul 03, 2013, 02:31 am
Well, I can't seem to find a way to do that either. But, you can do it at my repo: https://github.com/collin80/due_can
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jspobuk on Jul 03, 2013, 02:41 am
Thanks! that's what I was looking for.  Boy that was getting frustrating. :)
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jspobuk on Jul 03, 2013, 03:16 am
Why am I getting these errors?

In file included from C:\Users\johnp.PEM\Documents\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\libraries\CAN\sn65hvd234.c:26:
C:\Users\johnp.PEM\Documents\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\libraries\CAN\/sn65hvd234.h:30: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'SSN65HVD234'
C:\Users\johnp.PEM\Documents\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\libraries\CAN\sn65hvd234.c:37: error: expected ')' before '*' token
C:\Users\johnp.PEM\Documents\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\libraries\CAN\sn65hvd234.c:54: error: expected ')' before '*' token
C:\Users\johnp.PEM\Documents\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\libraries\CAN\sn65hvd234.c:72: error: expected ')' before '*' token
C:\Users\johnp.PEM\Documents\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\libraries\CAN\sn65hvd234.c:88: error: expected ')' before '*' token
C:\Users\johnp.PEM\Documents\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\libraries\CAN\sn65hvd234.c:103: error: expected ')' before '*' token
C:\Users\johnp.PEM\Documents\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\libraries\CAN\sn65hvd234.c:118: error: expected ')' before '*' token
C:\Users\johnp.PEM\Documents\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\libraries\CAN\sn65hvd234.c:135: error: expected ')' before '*' token
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: graynomad on Jul 03, 2013, 03:29 am
Maybe sn65hvd234.c should have a CPP extension to invoke the C++ parser/compiler/whatever

______
Rob
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jspobuk on Jul 03, 2013, 03:37 am
Oh duh, I see, there was a random sn65hvd234.c file.... so i just deleted it and it works fine.  There already was a sn65hvd234.cpp file. 

Thanks!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jspobuk on Jul 03, 2013, 03:42 am
When I press "Send" in the following screen, I get nothing.  The input box goes blank and the message is the same.  Just a TX blinking, but that's it.  Any ideas as to what is wrong? Here is my code

// Arduino Due - CAN Sample 1
// Brief CAN example for Arduino Due
// Test the transmission from CAN0 Mailbox 0 to CAN1 Mailbox 0
// By Thibaut Viard/Wilfredo Molina/Collin Kidder 2013

// Required libraries
#include "variant.h"
#include <due_can.h>

#define TEST1_CAN_COMM_MB_IDX    0
#define TEST1_CAN_TRANSFER_ID    0x07
#define TEST1_CAN0_TX_PRIO       15
#define CAN_MSG_DUMMY_DATA       0x55AAAA55

// CAN frame max data length
#define MAX_CAN_FRAME_DATA_LEN   8

// Message variable to be send
uint32_t CAN_MSG_1 = 0;

void setup()
{
 // start serial port at 9600 bps:
 Serial.begin(9600);
 Serial.println("Type CAN message to send");
 while (Serial.available() == 0);
}
void loop(){

 while (Serial.available() > 0) {
   CAN_MSG_1 = Serial.parseInt();
   if (Serial.read() == '\n') {
     Serial.print("Sent value= ");
     Serial.println(CAN_MSG_1);
   }
 }

 // Initialize CAN0 and CAN1, baudrate is 1Mb/s
 CAN.init(SystemCoreClock, CAN_BPS_1000K);
 CAN2.init(SystemCoreClock, CAN_BPS_1000K);

 // Initialize CAN1 mailbox 0 as receiver, frame ID is 0x07
 CAN2.mailbox_init(0);
 CAN2.mailbox_set_mode(0, CAN_MB_RX_MODE);
 CAN2.mailbox_set_accept_mask(0, 0x7FF, false);
 CAN2.mailbox_set_id(0, TEST1_CAN_TRANSFER_ID, false);

 // Initialize CAN0 mailbox 0 as transmitter, transmit priority is 15
 CAN.mailbox_init(0);
 CAN.mailbox_set_mode(0, CAN_MB_TX_MODE);
 CAN.mailbox_set_priority(0, TEST1_CAN0_TX_PRIO);
 CAN.mailbox_set_accept_mask(0, 0, false);
 // Prepare transmit ID, data and data length in CAN0 mailbox 0
 CAN.mailbox_set_id(0, TEST1_CAN_TRANSFER_ID, false);
 CAN.mailbox_set_datal(0, CAN_MSG_1);
 CAN.mailbox_set_datah(0, CAN_MSG_DUMMY_DATA);
 CAN.mailbox_set_datalen(0, MAX_CAN_FRAME_DATA_LEN);

 // Send out the information in the mailbox
 CAN.global_send_transfer_cmd(CAN_TCR_MB0);

 // Wait for CAN1 mailbox 0 to receive the data
 while (!(CAN2.mailbox_get_status(0) & CAN_MSR_MRDY)) {
 }

 RX_CAN_FRAME incoming;
 // Read the received data from CAN1 mailbox 0
 CAN2.mailbox_read(0, &incoming);
 Serial.print("CAN message received= ");
 Serial.print(incoming.data[0]);
 Serial.print(incoming.data[1]);
 Serial.print(incoming.data[2]);
 Serial.print(incoming.data[3]);
 Serial.print(incoming.data[4]);
 Serial.print(incoming.data[5]);
 Serial.print(incoming.data[6]);
 Serial.println(incoming.data[7]);
 
 // Disable CAN0 Controller
 CAN.disable();

 // Disable CAN1 Controller
 CAN2.disable();

 Serial.print("End of test");

 while (1) {
 }
}


Thanks!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: graynomad on Jul 03, 2013, 04:06 am
To test I would forget about the UI stuff and just send a hard-coded value. Then test the UI by itself and finally put them together.

Where are CAN and CAN2 defined? Are they created in the library?

_____
Rob
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Jul 03, 2013, 01:37 pm
Your code looks suspiciously like one of the included examples. I don't see any obvious changes so I'm going to assume you're basically trying to run one of the examples. The most obvious reasons it might not be working:

1. Have you hooked the high and low lines together for the two can outputs? CANH1 to CAN2H, CAN1L to CAN2L? The tests assume you've done this.
2. It is possible that you don't have the proper pins specified. In due_can.h you'll find these defines:
#define CAN0_RS  61
#define CAN0_EN  62
#define CAN1_RS  63
#define CAN1_EN  64

They should match your board. You could hardwire your canbus shield to always be enabled and handle its own RS or you can set these defines and the library will do it.
3. Maybe the canbus shield doesn't work?

It can be hard to say. If you have an oscilloscope or logic analyzer you could try scoping the H and L pins at the output of the transceivers or scope the CANTX and CANRX pins at the Due to see if any form of signal is getting out. What you'd expect to see is a momentary signal as the frame is transmitted from one canbus device to the other. If there is as problem but it is trying to send you should see the frame very rapidly resending forever. This is pretty easy to see on a scope. If you are getting constant resends then either you aren't looped back, the signal is corrupt, or the receiver is not responding.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: newhobby on Jul 03, 2013, 07:42 pm
Is there such a thing as a CAN I/O expander chips?
One that would work similar to I2C I/O expanders?
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: graynomad on Jul 04, 2013, 02:13 am
Not AFAIK, if you're keen you could DIY one by using an LPC11C24 processor.

_____
Rob
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Transistorfips on Jul 04, 2013, 06:26 am
>Is there such a thing as a CAN I/O expander chips?
Microchip MCP2502 CAN I/O Expander Family

http://ww1.microchip.com/downloads/en/devicedoc/21664d.pdf



Hope this helps,

Joachim
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: graynomad on Jul 04, 2013, 06:51 am
Ooo nice Joachim, I didn't know about them. Thanks.

_____
Rob
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: newhobby on Jul 04, 2013, 07:37 am

>Is there such a thing as a CAN I/O expander chips?
Microchip MCP2502 CAN I/O Expander Family

http://ww1.microchip.com/downloads/en/devicedoc/21664d.pdf



Hope this helps,

Joachim

Have you used them yet?
I heard it's a pain to program them.
It seems to be a one shot deal and the dev kit has some issues burning it and you end up with bricked chips.
Just wanted to have anyone else's experience with them.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Transistorfips on Jul 04, 2013, 10:20 am
No, I haven't. I'm (as a hobbyist) using PIC 18F for several years now and I haven't bricked not even only one. But if the CAN expanders base on the same rationale I do not know. At least for CAN it seems they have the same buffers, protocol engine and control as the 18F458. I actually have two of them on my perf board http://imageshack.us/photo/my-images/24/6urm.jpg/ (http://imageshack.us/photo/my-images/24/6urm.jpg/). True, the PICs sometimes are the worst imps of satan in the business. For example the CAN timing on my PICs are 7.7us instead of 8.0us at 125kbaud. I tried several hour to fix but without success. But they do their job with zero in the error counter.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: djkrugger on Jul 06, 2013, 10:07 am
Hi guys, i've been following this project since its inception and i want to thank everybody for such a great work.
I just got my DUE so i started to experiment, My goal is to use the DUE in a GMlan network which is a single wire CAN bus i got the transceivers so thats fine, my only problem is that the GMlan low speed is just 33.333Kbps, is there any way to incorporate that or any other custom baud rate in  the duo_can.h? i tried adding #define CAN_BPS_33k    33.333 but something is missing.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Jul 06, 2013, 02:18 pm
There are/were a couple of problems with trying to get 33.333K baud.

1. There are no fractions. The math is all done on integers so 33.333 is just 33
2. This is partly because baud is defined in thousands. If it were defined as the total baud then you could put in 33333 as the baud.

For proper results you need to be able to divide your system clock by some value to get the canbus baud rate. You will notice that the normal baud rates evenly divide millions of hertz because they are multiples of 5 (25,50,100,250,500,1000) 84MHz / 33333 = 2520.0252. The fractional part will go away leaving you with a divisor of 2520. 84MHz / 2520 = 33,333.333 baud. This is probably close even and maybe even what gmlan has in mind. Additionally, 84MHz / 126 (which is a valid divisor value) = 20 (and a very small fractional portion). 20 is a valid # of TQ. So, the library should be able to set the baud fairly accurately.

However, the only way you're going to get that baud rate is for you, me, or someone to modify the library to input the actual baud to the set_baudrate function. It's not hard to do. Just take out all of the "* 1000" spots and switch up the defines to be 1000 times what they are now. Then you can set any baud you want. I can't promise it will work but that's the way to try. I have attached a test copy of the due_can source code where I made the changes. I have not tested them. Try it out and let me know if it works. If it does I'll commit the changes to git and we won't have this problem any more.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: OV1A on Jul 08, 2013, 11:11 am
Hi.

Now I have my CAN test running, thanks to your fine work:
Hardware is Iduino DUE with homemade CAN interface (MCP2551 salvaged from some junk)
https://dl.dropboxusercontent.com/u/5062915/Duino/CAN1.jpg (https://dl.dropboxusercontent.com/u/5062915/Duino/CAN1.jpg)

Running Arduino_Due_CAN_Sample_1:
https://dl.dropboxusercontent.com/u/5062915/Duino/CAN2.jpg (https://dl.dropboxusercontent.com/u/5062915/Duino/CAN2.jpg)

Watching transmission with USB-CAN A1+:
https://dl.dropboxusercontent.com/u/5062915/Duino/CAN3.jpg (https://dl.dropboxusercontent.com/u/5062915/Duino/CAN3.jpg)
http://www.ebay.com/itm/USB-CAN-USB-to-CAN-bus-Converter-Adapter-with-High-quality-/151077099849?pt=LH_DefaultDomain_0&hash=item232ce59949 (http://www.ebay.com/itm/USB-CAN-USB-to-CAN-bus-Converter-Adapter-with-High-quality-/151077099849?pt=LH_DefaultDomain_0&hash=item232ce59949)
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Transistorfips on Jul 10, 2013, 05:05 pm
Hi guys,
without reading the 16 pages of this thread before, does anyone has already made a real-world-application for the CAN?

I found CODE BY DEBRAJ DEB FOR OBD READER, https://sites.google.com/site/hobbydebraj/home/can-bus-based-obd-reader (https://sites.google.com/site/hobbydebraj/home/can-bus-based-obd-reader) made for a Microchip PIC24 CAN device. This code is quite simple to port since all of the data handling is very similar to the data structures used by the Atmel/Arduino example. I fly to Spain for holiday on tomorrow but as a real nerd I of course have my Due with me. Hopefully while with my big toe drilling holes in the sand I have enough time to port the code under the parasol.
Aloha.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Jul 10, 2013, 09:23 pm
Yeah, I eat my own dog food (common US expression... I use my own products). I'm using the library in an open source vehicle control unit: https://github.com/collin80/GEVCU

So, I do know that it works well enough, at least with standard frames. I've been occupied with hardware design recently so neither this library nor GEVCU have gotten much love recently but all of it does work.

Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jspobuk on Jul 19, 2013, 11:35 pm
Hello, so i found time to go back to my arduino due.  Still same problem as before.  I did hook up an oscilloscope as you suggested Collin80 in a previous post:




It can be hard to say. If you have an oscilloscope or logic analyzer you could try scoping the H and L pins at the output of the transceivers or scope the CANTX and CANRX pins at the Due to see if any form of signal is getting out. What you'd expect to see is a momentary signal as the frame is transmitted from one canbus device to the other. If there is as problem but it is trying to send you should see the frame very rapidly resending forever. This is pretty easy to see on a scope. If you are getting constant resends then either you aren't looped back, the signal is corrupt, or the receiver is not responding.


I hooked up just the due CANTX and GND. When i start the serial monitor I get a solid 3.2 V.  When i press "Send", I see the following screen.  Is that the resending forever you were mentioning?

Thanks!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Jul 20, 2013, 05:03 am
It sort of seems that way. The default sending rate of the canbus examples is 1M baud so you really need to be closer to 1-10uS divs to see the actual signal itself but your larger div screen shows it trying to resend around 1.5ms between attempts. This makes it appear as if your Due is working perfectly fine. If things are still not working then perhaps the transceiver is not operating properly. You do have to enable the transceiver to make it work. You should probably try scoping the CANH and CANL lines to see if they're responding to the signal coming out of the Due.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jspobuk on Jul 22, 2013, 11:00 pm
So I got the signal to go all the way to the CANRX in to DAC0.  But the serial monitor still does nothing when I press send.  I can't get past the line:

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


Any help is greatly appreciated.
Thanks!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Jul 23, 2013, 03:40 am
Hello jspobuk,
I don't remember if you followed the directions I gave you in my (reply #181). Remember, if you have the wrong variant files, the communication can't be established.

http://forum.arduino.cc/index.php?topic=131096.181 (http://forum.arduino.cc/index.php?topic=131096.181)

Please, keep us posted. Regards,
-Wilfredo

Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jspobuk on Jul 23, 2013, 03:46 am
Yes, actually I was just re reading that.  this is what i have in my variant.h

/*
* Complementary CAN pins
*/
static const uint8_t CAN1RX = 88;
static const uint8_t CAN1TX = 89;

// CAN0
#define PINS_CAN0            (90u)
// CAN1
#define PINS_CAN1            (91u)

I believe that is correct, and my variant.cpp

// 88/89 - CANRX1/CANTX1 (same physical pin for 66/53)
  { PIOB, PIO_PB15A_CANRX1,     ID_PIOB, PIO_PERIPH_A, PIO_DEFAULT, PIN_ATTR_DIGITAL,                  NO_ADC, NO_ADC, NOT_ON_PWM,  NOT_ON_TIMER }, // CANRX1
  { PIOB, PIO_PB14A_CANTX1,     ID_PIOB, PIO_PERIPH_A, PIO_DEFAULT, PIN_ATTR_DIGITAL,                  NO_ADC, NO_ADC, NOT_ON_PWM,  NOT_ON_TIMER }, // CANTX1

  // 90 .. 91 - "All CAN pins" masks
  // 90 - CAN0 all pins
  { PIOA, PIO_PA1A_CANRX0|PIO_PA0A_CANTX0, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT, (PIN_ATTR_DIGITAL|PIN_ATTR_COMBO), NO_ADC,  NO_ADC, NOT_ON_PWM,  NOT_ON_TIMER },
  // 91 - CAN1 all pins
  { PIOB, PIO_PB15A_CANRX1|PIO_PB14A_CANTX1, ID_PIOB, PIO_PERIPH_A, PIO_DEFAULT, (PIN_ATTR_DIGITAL|PIN_ATTR_COMBO), NO_ADC, NO_ADC, NOT_ON_PWM,  NOT_ON_TIMER },


which i also believe is correct.  I tried all the examples, but to no avail.  If i comment out the while loop in the example one that i alluded to above, it prints out 00000000.

Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Jul 23, 2013, 04:16 am
I was double checking that. I also believe you forgot to mention the following remaining lines:

In variant.h:
Code: [Select]
static const uint8_t CANRX = 68;
static const uint8_t CANTX = 69;


In variant.cpp
Code: [Select]
// 68/69 - CANRX0/CANTX0
 { PIOA, PIO_PA1A_CANRX0,   ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT, PIN_ATTR_DIGITAL,                  ADC14,  NO_ADC, NOT_ON_PWM,  NOT_ON_TIMER }, // CANRX
 { PIOA, PIO_PA0A_CANTX0,   ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT, PIN_ATTR_DIGITAL,                  ADC15,  NO_ADC, NOT_ON_PWM,  NOT_ON_TIMER }, // CANTX


I have a check-out list somewhere. I will try to find it but in the meantime remember:

1. CANH0<->CANH1    /    CANL0<->CANL1
2. 'direct mode' in the transceivers connecting pins 5(EN) to 3V3 and pins 8(Rs) to GND.
3. Serial monitor of the Arduino IDE 1.5.2 with autoscroll and newline modes activated.

EDIT: Terminal resistors (120 ohm) between CANL and CANH lines.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: bilbolodz on Jul 24, 2013, 10:52 am
I know that it's totally wrong place but I don't know where I can get it, maybe someone from forum can help me.
I've bought these interface:
http://www.ebay.com/itm/360610597258 (http://www.ebay.com/itm/360610597258)
http://item.taobao.com/item.htm?spm=a1z10.1.w4004-2663162030.6.PtpJVg&id=18286480388 (http://item.taobao.com/item.htm?spm=a1z10.1.w4004-2663162030.6.PtpJVg&id=18286480388)

Looks fine but unfortunately CD with software was broken. I'm looking for any of these programs:


Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jspobuk on Jul 24, 2013, 11:37 pm
Thanks Palliser,

I didn't have newline mode on, which caused the "Sent Value" to not show up.  And I have at least one bad connection which is what kept the while loop going. (i am starting to dislike surface mount chips).  After I finish with these simple connection issues I want to be able to communicate J1939.  Anyone else know about J1939?  I want to be able to read and maybe write to diesel engines that are J1939 compatible. However, its pretty murky waters to me right now.  If anyone else is also doing stuff, any help is greatly appreciated.

Thanks!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Jul 25, 2013, 04:32 pm
Hello jspobuk,

As I mentioned in my reply #125, I am not sure if the CAN library support J1939. Definitively the one that have gasped more with it is Collin80. Please, read his reply 126 about the masking. Anyway, if you have the specific brand and model of your J1939 device, please, let us know. May someone else is working with it or we could make time to read about it and try to help you more.

http://forum.arduino.cc/index.php?topic=131096.126 (http://forum.arduino.cc/index.php?topic=131096.126)

Regards.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jspobuk on Jul 26, 2013, 01:50 am
Yes, I believe J1939 is a higher level than just the signals, but it does utilize the extended id.  I guess what I am trying to do first is just get the raw data, even though I might not know exactly what it means.  The only CAN thing I have is a dynaGen GSC 400.  There is a J1939 plug, and the manual is not very helpful about... well... anything.  Right now I am just trying to get my arduino to send extended id messages to itself.  So I tweaked the first example.  Here is my code:

Code: [Select]
// Required libraries
#include "variant.h"
#include <due_can.h>
#include <sn65hvd234.h>

#define TEST1_CAN_COMM_MB_IDX    0
#define TEST1_CAN_TRANSFER_ID    0xFEEE00
#define TEST1_CAN0_TX_PRIO       0
#define CAN_MSG_DUMMY_DATA       0x55AAAA55

// CAN frame max data length
#define MAX_CAN_FRAME_DATA_LEN   8

// Message variable to be send
uint32_t CAN_MSG_1 = 0;

void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
  Serial.println("Type CAN message to send");
  while (Serial.available() == 0);
}
void loop(){

  while (Serial.available() > 0) {
    CAN_MSG_1 = Serial.parseInt();
    if (Serial.read() == '\n') {
      Serial.print("Sent value= ");
      Serial.println(CAN_MSG_1);
    }
  }
 
  CAN.reset_all_mailbox();
  CAN2.reset_all_mailbox();

  // Initialize CAN0 and CAN1, baudrate is 1Mb/s
  CAN.init(SystemCoreClock, CAN_BPS_1000K);
  CAN2.init(SystemCoreClock, CAN_BPS_1000K);

  // Initialize CAN1 mailbox 0 as receiver, frame ID is 0x07
  CAN2.mailbox_init(0);
  CAN2.mailbox_set_mode(0, CAN_MB_RX_MODE);
  CAN2.mailbox_set_accept_mask(0, 0, true);
  CAN2.mailbox_set_id(0, 0, true);

  // Initialize CAN0 mailbox 0 as transmitter, transmit priority is 15
  CAN.mailbox_init(0);
  CAN.mailbox_set_mode(0, CAN_MB_TX_MODE);
  CAN.mailbox_set_priority(0, TEST1_CAN0_TX_PRIO);
  CAN.mailbox_set_accept_mask(0, 0, true);
  // Prepare transmit ID, data and data length in CAN0 mailbox 0
  CAN.mailbox_set_id(0, TEST1_CAN_TRANSFER_ID, true);
  CAN.mailbox_set_datal(0, CAN_MSG_1);
  //CAN.mailbox_set_datah(0, CAN_MSG_DUMMY_DATA);
  CAN.mailbox_set_datalen(0, MAX_CAN_FRAME_DATA_LEN);

  // Send out the information in the mailbox
  CAN.mailbox_send_transfer_cmd(0);

  // Wait for CAN1 mailbox 0 to receive the data
  while (!(CAN2.mailbox_get_status(0) & CAN_MSR_MRDY)) {

  }

  RX_CAN_FRAME incoming;
  // Read the received data from CAN1 mailbox 0
  CAN2.mailbox_read(0, &incoming);
  Serial.println("CAN message received= ");
  Serial.print("ID:");
  Serial.println(incoming.id);
  Serial.print("FID:");
  Serial.println(incoming.fid);
  Serial.print("RTR:");
  Serial.println(incoming.rtr);
  Serial.print("IDE:");
  Serial.println(incoming.ide);
  Serial.print("DLC:");
  Serial.println(incoming.dlc);
  Serial.print("DATA:");
  Serial.print(incoming.data[0]);
  Serial.print(incoming.data[1]);
  Serial.print(incoming.data[2]);
  Serial.print(incoming.data[3]);
  Serial.print(incoming.data[4]);
  Serial.print(incoming.data[5]);
  Serial.print(incoming.data[6]);
  Serial.println(incoming.data[7]);
 
  // Disable CAN0 Controller
  CAN.disable();

  // Disable CAN1 Controller
  CAN2.disable();

  Serial.print("End of test");

  while (1) {
  }
}


and my output on the serial monitor below.  Notice the Data is all zeroes.  This the case no matter what i put in.  Any ideas of why I can't get the data through?

Thanks!

Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jspobuk on Jul 26, 2013, 11:24 pm
So I figured out my problem.  I needed to put a
CAN.global_send_transfer_cmd(CAN_TCR_MB0);

instead of
CAN.mailbox_send_transfer_cmd(0);

Success! Now I can actually try to do something with this.  I'm going to try to send some info to my DynaGen GSC 400.  If anyone has any knowledge of doing such things hit me up, otherwise I'll keep ya'll posted.   
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Rich_P on Jul 29, 2013, 05:35 pm
Hi All.

First off, Fantastic Effort.

I am having real trouble getting the example 1 to compile,

Getting a lot of errors that i think stem from incorrect Variant file, does anyone have the variant file i need? as i cant find it on Collins Git page

Cheers

Rich
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Jul 29, 2013, 06:36 pm
Hello Rich,

Please see my reply #181.

http://forum.arduino.cc/index.php?topic=131096.181

Regards,
Wilfredo
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jspobuk on Jul 29, 2013, 07:34 pm
Hey Rich,

Check out Replies 229-231 also.  I was getting a bunch of errors too at first.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: jspobuk on Jul 30, 2013, 12:34 am
So just an update on the J1939 endeavor.  I was able to find several parameters and PGN's to test.  However, most of them are just to read information not necessarily change throttle speed or even send stop/start signals.  In the picture you can see the Eng. Temp and Hours as N/A and 0.0 respectively.  I was able to send fake data on the bus and have the Dyna Gen pick it up and display it.  Pretty much made my day.

All in all this due is pretty awesome.  Thanks again to all those who worked on putting together the library! Now if only SAE and manufacturers would give access to all the PGN's and detailed descriptions of what each block of data does.  I could do a lot more.  But I'm just glad it worked.  So that's the scoops, and the next step is actually using the Due in a real application.

Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Rich_P on Jul 30, 2013, 09:21 am
Hi Thanks for the VAriant Files,

I replaced the standard arduino Variant Files with these and i still cant compile it,

The first error is that variant.h doesnt exist. i have checked spelling of the file and location (in the variants folder i assuem?) seems ok

See below, i am completely confused, i dont usually have issues like this but i have not used the due much as of yet,

Code: [Select]

Arduino_Due_CAN_Sample_1.ino:7:21: error: variant.h: No such file or directory
In file included from \\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:72,
                from Arduino_Due_CAN_Sample_1.ino:8:
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/sn65hvd234.h:34: error: 'uint32_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/sn65hvd234.h:37: error: 'uint32_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/sn65hvd234.h:39: error: 'uint32_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/sn65hvd234.h:40: error: 'uint32_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/sn65hvd234.h:41: error: 'uint32_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/sn65hvd234.h:43: error: 'uint32_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/sn65hvd234.h:44: error: 'uint32_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/sn65hvd234.h:46: error: 'uint32_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/sn65hvd234.h:47: error: 'uint32_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/sn65hvd234.h:48: error: expected `)' before 'Rs'
In file included from Arduino_Due_CAN_Sample_1.ino:8:
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:76: error: 'uint8_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:77: error: 'uint8_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:129: error: 'uint32_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:130: error: 'uint32_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:131: error: 'uint8_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:132: error: 'uint8_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:144: error: 'uint8_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:151: error: ISO C++ forbids declaration of 'Can' with no type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:151: error: expected ';' before '*' token
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:159: error: 'uint16_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:160: error: 'uint16_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:161: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:161: error: 'uint32_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:167: error: expected `)' before '*' token
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:213: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:213: error: 'uint16_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:214: error: 'uint32_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:215: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:216: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:217: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:218: error: 'uint32_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:219: error: 'uint32_t' does not name a type
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:220: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:220: error: 'uint32_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:221: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:221: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:222: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:222: error: 'uint32_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:223: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:223: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:224: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:224: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:224: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:225: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:225: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:226: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:226: error: 'uint32_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:227: error: 'uint8_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:227: error: 'uint32_t' has not been declared
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\due_can/due_can.h:232: error: 'uint32_t' does not name a type
Arduino_Due_CAN_Sample_1.ino: In function 'void loop()':
Arduino_Due_CAN_Sample_1:39: error: 'class CANRaw' has no member named 'init'
Arduino_Due_CAN_Sample_1:39: error: 'SystemCoreClock' was not declared in this scope
Arduino_Due_CAN_Sample_1:40: error: 'class CANRaw' has no member named 'init'
Arduino_Due_CAN_Sample_1:60: error: 'CAN_TCR_MB0' was not declared in this scope
Arduino_Due_CAN_Sample_1:63: error: 'class CANRaw' has no member named 'mailbox_get_status'
Arduino_Due_CAN_Sample_1:63: error: 'CAN_MSR_MRDY' was not declared in this scope
Arduino_Due_CAN_Sample_1:68: error: 'class CANRaw' has no member named 'mailbox_read'
Arduino_Due_CAN_Sample_1:70: error: 'struct RX_CAN_FRAME' has no member named 'data'
Arduino_Due_CAN_Sample_1:71: error: 'struct RX_CAN_FRAME' has no member named 'data'
Arduino_Due_CAN_Sample_1:72: error: 'struct RX_CAN_FRAME' has no member named 'data'
Arduino_Due_CAN_Sample_1:73: error: 'struct RX_CAN_FRAME' has no member named 'data'
Arduino_Due_CAN_Sample_1:74: error: 'struct RX_CAN_FRAME' has no member named 'data'
Arduino_Due_CAN_Sample_1:75: error: 'struct RX_CAN_FRAME' has no member named 'data'
Arduino_Due_CAN_Sample_1:76: error: 'struct RX_CAN_FRAME' has no member named 'data'
Arduino_Due_CAN_Sample_1:77: error: 'struct RX_CAN_FRAME' has no member named 'data'
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Jul 30, 2013, 03:15 pm
Hello Rich,
I would recommend you to move (cut and paste) the CAN library folder 'due_can' from

Quote
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\libraries\


to

Quote
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\libraries\


Please, let me know.

EDIT:
Remember also that the variant files should be located at:

Quote
\\fs02\users\richardpel\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\variants\arduino_due_x
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: djusthof on Aug 01, 2013, 04:27 pm
I just downloaded arduino 1.5.2, downloaded CAN library from GitHub, also updated the variant file just like the last posts indicated but still getting all kinds of errors when I try to run a sketch. Are there more files that I need to be updating within arduino 1.5.2?

Thanks
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Aug 01, 2013, 07:03 pm
Hello djusthof,
Could you show us part of the errors you are getting? Thanks.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: djusthof on Aug 01, 2013, 08:09 pm
when i try to compile this
Code: [Select]
#include <due_can.h>

void setup() {
 // put your setup code here, to run once:

}

void loop() {
 // put your main code here, to run repeatedly:
 
}


Here is part of the error,

Code: [Select]
In file included from sketch_jul31a.ino:1:
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:9: error: stray '\302' in program
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:9: error: stray '\267' in program
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:9: error: stray '\302' in program
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:9: error: stray '\267' in program
In file included from sketch_jul31a.ino:1:
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:314: error: invalid suffix "b4c26c6e36e6baaed686d0d5c3666b" on integer constant
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:315: error: invalid suffix "b4c26c6e36e6baaed686d0d5c3666b" on integer constant
In file included from sketch_jul31a.ino:1:
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:369: error: too many decimal points in number
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:373: error: too many decimal points in number
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:377: error: too many decimal points in number
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:426: error: invalid digit "9" in octal constant
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:430: error: invalid digit "8" in octal constant
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:466: error: invalid digit "9" in octal constant
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:470: error: invalid digit "8" in octal constant
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:498: error: too many decimal points in number
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:502: error: too many decimal points in number
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:522: error: too many decimal points in number
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:526: error: too many decimal points in number
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:530: error: too many decimal points in number
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:534: error: too many decimal points in number
C:\Users\Derek\Desktop\Arduino 1.5.2\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:538: error: too many decimal points in number


Thanks
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Aug 01, 2013, 09:08 pm
Hello djusthof,
I don't know which due_can.h file you pulled but the one from collin80 here: https://github.com/collin80/due_can (https://github.com/collin80/due_can) only has 241 lines and I see you are getting error in line 538?. Thus, I would recommend you to download again the library and try it again. Regards.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: djusthof on Aug 01, 2013, 09:54 pm
Thanks Palliser, works great
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: zabaat on Aug 03, 2013, 12:14 am
Is there any way at all to get more than 8 total mailboxes?

Code: [Select]
CAN.global_send_transfer_cmd(CAN_TCR_MB0 | CAN_TCR_MB1 | CAN_TCR_MB2 | CAN_TCR_MB3 | CAN_TCR_MB4 | CAN_TCR_MB5 | CAN_TCR_MB6 | CAN_TCR_MB7 | CAN_TCR_MB8 | CAN_TCR_MB9 | CAN_TCR_MB10 | CAN_TCR_MB11 | CAN_TCR_MB12);

Code: [Select]
dueCAN4.ino: In function 'void loop()':
dueCAN4:475: error: 'CAN_TCR_MB8' was not declared in this scope
dueCAN4:475: error: 'CAN_TCR_MB9' was not declared in this scope
dueCAN4:475: error: 'CAN_TCR_MB10' was not declared in this scope
dueCAN4:475: error: 'CAN_TCR_MB11' was not declared in this scope
dueCAN4:475: error: 'CAN_TCR_MB12' was not declared in this scope



I tried this in the library... but not sure what is going on


due_can.cpp
Code: [Select]
void CANRaw::interruptHandler() {

uint32_t ul_status = m_pCan->CAN_SR; //get status of interrupts

if (ul_status & CAN_SR_MB0) { //mailbox 0 event
mailbox_int_handler(0, ul_status);
}
if (ul_status & CAN_SR_MB1) { //mailbox 1 event
mailbox_int_handler(1, ul_status);
}
if (ul_status & CAN_SR_MB2) { //mailbox 2 event
mailbox_int_handler(2, ul_status);
}
if (ul_status & CAN_SR_MB3) { //mailbox 3 event
mailbox_int_handler(3, ul_status);
}
if (ul_status & CAN_SR_MB4) { //mailbox 4 event
mailbox_int_handler(4, ul_status);
}
if (ul_status & CAN_SR_MB5) { //mailbox 5 event
mailbox_int_handler(5, ul_status);
}
if (ul_status & CAN_SR_MB6) { //mailbox 6 event
mailbox_int_handler(6, ul_status);
}
if (ul_status & CAN_SR_MB7) { //mailbox 7 event
mailbox_int_handler(7, ul_status);
}
if (ul_status & CAN_SR_MB8) { //mailbox 7 event
mailbox_int_handler(8, ul_status);
}
if (ul_status & CAN_SR_MB9) { //mailbox 7 event
mailbox_int_handler(9, ul_status);
}
if (ul_status & CAN_SR_MB10) { //mailbox 7 event
mailbox_int_handler(10, ul_status);
}
if (ul_status & CAN_SR_MB11) { //mailbox 7 event
mailbox_int_handler(11, ul_status);
}
if (ul_status & CAN_SR_MB12) { //mailbox 7 event
mailbox_int_handler(12, ul_status);
}


keywords.txt
Code: [Select]
CAN_TCR_MB0 LITERAL1
CAN_TCR_MB1 LITERAL1
CAN_TCR_MB2 LITERAL1
CAN_TCR_MB3 LITERAL1
CAN_TCR_MB4 LITERAL1
CAN_TCR_MB5 LITERAL1
CAN_TCR_MB6 LITERAL1
CAN_TCR_MB7 LITERAL1
CAN_TCR_MB8 LITERAL1
CAN_TCR_MB9 LITERAL1
CAN_TCR_MB10 LITERAL1
CAN_TCR_MB11 LITERAL1
CAN_TCR_MB12 LITERAL1


I didn't know I was so limited in this respect and am almost ready for this project and just hit an enormous wall!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Palliser on Aug 03, 2013, 02:46 am
Hello zabaat.

According to Atmel doc11057 (page 1207) http://www.atmel.com/Images/doc11057.pdf (http://www.atmel.com/Images/doc11057.pdf), each of the two CAN controllers inside SAM3X8E has 8 mailboxes implemented. Thus, you have 16 mailboxes available which is more than enough for a general purpose CAN project. It is a hardware limitation of the MCU. Notice also that the component_can.h file from the CMSIS library on the Arduino Due...
Quote
(...\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system\CMSIS\Device\ATMEL\sam3xa\include\component)

... there are only 8 defined mailboxes per CAN controller (MB0->MB7). On the other hand, you could optimize the use of the mailboxes implementing the Producer<->Consumer mode where you need half of the amount of mailboxes as compared to the clasical Transmit<->Receive mode. Can you tell us more about you application?. Why more than 16 mailboxes? Regards!
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: zabaat on Aug 03, 2013, 11:51 am
Thank you for the great response! Would it be possible for me to roll through different can ids and data locations as I looped? so in other words I could still have 8 mailboxes per loop but have toggle that redirected the mailboxes after each send? I'll try this tomorrow
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: AdderD on Aug 03, 2013, 04:42 pm
Yes, you can reconfigure the mailboxes any time you want. You can change what a mailbox does every loop if you want to.
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: Transistorfips on Aug 31, 2013, 06:34 pm
Hi guys,
here's a quick update of my CAN OBD analyzer:
http://imageshack.us/photo/my-images/707/glhn.jpg/ (http://imageshack.us/photo/my-images/707/glhn.jpg/)
The software is still under construction, I use a simulator from Ozen Elektronik (a turkish brand) instead of my car. The Display is a Touch-LCD from lcd-module.de.
Speed, rpm, coolant temp and vehicle frame number are readable (multi-frame transfer took me a while to fiddle with).
I only use the DUE hardware, software is a modified can example for the SAM3X-EK board.

DUE rocks!

Title: Re: Building a CAN API for Arduino DUE
Post by: Owais on Sep 10, 2013, 02:03 pm
Hello, Can anyone please explain the function of mailbox_set_accept_mask(); both in case of transmitter as well as receiver mailbox.

Thanks,

Owais.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Sep 10, 2013, 03:00 pm

Hello, Can anyone please explain the function of mailbox_set_accept_mask(); both in case of transmitter as well as receiver mailbox.

Thanks,

Owais.


Sure. For transmission mailboxes it means absolutely nothing. The examples might call that function but it is just for completeness and not because you have to. Transmissions will go out no matter how you have the accept mask set.

Now, such is not the case for receiving mail boxes. That's where the accept mask comes into play. It works like this:

1. A frame is sent over the wire and picked up by the Due.
2. The ID of the frame is logical AND with the accept mask
3. This new value is then compared to the id you set for the RX mailbox (mailbox_set_id). If the IDs match then the frame is accepted and available for your program to inspect.

Here is an example. Let's say you set your accept mask to 0x7F0 and your ID to 0x230 (assume only standard frames). Now, a frame with ID 0x232 comes in. So, take 0x232 AND 0x7F0 = 0x230. Compare this to the mailbox ID you set (0x230) and find out that it matches. So, the frame is accepted by your program. Now, if a frame with ID 0x23A comes in then the same thing will happen. If 0x240 comes in then it will be rejected.

In a nutshell: the accept mask allows you to accept a range of frame IDs with a single mailbox. In the example above it allows a range of 16 IDs through 0x230 - 0x23F. The traditional use for this is to tailor acceptance to only frames you care about. Canbus traffic can get busy and you don't want to have to deal with every frame on the bus.
Title: Re: Building a CAN API for Arduino DUE
Post by: Owais on Sep 10, 2013, 03:20 pm
Thank you very much Collin80 its crystal now.
Title: Re: Building a CAN API for Arduino DUE
Post by: Transistorfips on Sep 16, 2013, 03:42 pm
Hi guys,
today I compiled the due_can project the first time with Visual Micro + AS6.1. The second example from Collin brought:

Compiling 'Arduino_Due_CAN_Sample_2' for 'Arduino Due (Programming Port)'
Binary sketch size: 73052 bytes (used 14% of a 524288 byte maximum) (11,29 secs)

which is a huge amount of code for this little example. Even my complete CAN OBD-II project (with a small GUI and an estimated far more than thousand lines of code) written in pure C is only 43k in size. I wonder what's going on here. Also the Visual Micro hides all compiler warnings which yield additional confusings when debugging. I haven't found any settings for tweaking the IDE yet. Don't know if it is really that useful for developing CAN stuff...
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Sep 16, 2013, 06:37 pm
Arduino code for the Due seems to be gigantic. It takes 10,076 for the blink sketch. AnalogInOutSerial takes 26,712. The size you got for the canbus example is pretty typical for the examples. They just seem to come out that large. However, it doesn't seem to enlarge a whole lot thereafter. My whole VCU project takes up less than 100K of flash space. It has a whole lot more code than the canbus library but only takes up about 30K extra. So, I think that the arduino core library can just be a bit large. I've wondered if there is something that could be done about this but have not actually taken the time to try to figure it out. I'm being lazy because there is still all sorts of available flash space. If my sketches were getting toward 450K in size then I'd worry about how to save space.
Title: Re: Building a CAN API for Arduino DUE
Post by: Transistorfips on Sep 24, 2013, 09:27 am
Hi guys,
I think I need a idea.

I try to communicate with my OBD simulator with 29-bit EX-ID frame. 11-bit mode work fine but in 29-bit mode the sim doesn't resond. I looked on my scope and I saw the exact time for 29bit frame with 8byte payload which I calculated before. But the sim plays possum.

Here is a chunk of my code which derived from the Atmel Code example. It works fine for 11bit frame mode (I send/receive with CAN_0). Did I forget something?

Code: [Select]

void getECUData(can_mb_conf_t *p_sc_mailbox_rx, can_mb_conf_t *p_sc_mailbox_tx) {
can_reset_all_mailbox(CAN0);
reset_mailbox_conf(p_sc_mailbox_rx);

/* Init CAN0 Mailbox 1 to Reception Mailbox. */
p_sc_mailbox_rx->ul_mb_idx = TEST1_CAN_COMM_MB_IDX + 1;  //mailbox 1
p_sc_mailbox_rx->uc_obj_type = CAN_MB_RX_MODE;
p_sc_mailbox_rx->ul_id_msk = CAN_MAM_MIDvA_Msk | CAN_MAM_MIDvB_Msk; //CAN 2.0A + B
if (!eepromData[EEPROM_CAN_11_29_BIT_ID]){
p_sc_mailbox_rx->ul_id = CAN_MID_MIDvA(0x7E8);  //get 7E8 from engine control module
p_sc_mailbox_rx->uc_id_ver = 0; // 0/1: standard/extended frame
}
else  {
p_sc_mailbox_rx->ul_id = CAN_MID_MIDvB(0x7E8);  //get 7E8 from engine control module
p_sc_mailbox_rx->uc_id_ver = 1; // Ver. A/B 0/1: standard/extended frame
}
can_mailbox_init(CAN0, p_sc_mailbox_rx);

// problem starts here ---------------------------------------

/* Init CAN0 Mailbox 0 to Transmit Mailbox. */
p_sc_mailbox_tx->ul_mb_idx = TEST1_CAN_COMM_MB_IDX; //mailbox 0
p_sc_mailbox_tx->uc_obj_type = CAN_MB_TX_MODE;
p_sc_mailbox_tx->uc_tx_prio = TEST1_CAN0_TX_PRIO;
if (!eepromData[EEPROM_CAN_11_29_BIT_ID]){
p_sc_mailbox_tx->uc_id_ver = 0; // 0/1: standard/extended frame
}
else {
p_sc_mailbox_tx->uc_id_ver = 1; // Ver. A/B 0/1: standard/extended frame
printf ("DEBUG: in getECUData(); 29bit frame mode\r\n");
}
p_sc_mailbox_tx->ul_id_msk = CAN_MAM_MIDvA_Msk | CAN_MAM_MIDvB_Msk; //CAN 2.0A + B
can_mailbox_init(CAN0, p_sc_mailbox_tx);

/* Write transmit information into mailbox. */
if (!eepromData[EEPROM_CAN_11_29_BIT_ID]){  //EEPROM_CAN_11_29_BIT_ID, bit0:  0: 11bit, 1: 29bit
p_sc_mailbox_tx->ul_id = CAN_MID_MIDvA(0x7DF);  // engine control module
}
else {
p_sc_mailbox_tx->ul_id = CAN_MID_MIDvB(0x7DF);  // engine control module
printf ("DEBUG: in getECUData(); 29bit frame mode\r\n");
}
//p_sc_mailbox_tx->ul_datal = ...; set by caller
//p_sc_mailbox_tx->ul_datah = ...;
p_sc_mailbox_tx->uc_length = MAX_CAN_FRAME_DATA_LEN; // 8bytes
can_mailbox_write(CAN0, p_sc_mailbox_tx);

// problem ends here ---------------------------------------

can_enable_interrupt(CAN0, CAN_IER_MB1);

/* Send out the information in the mailbox. */
can_global_send_transfer_cmd(CAN0, CAN_TCR_MB0);

/* Wait until TX Mailbox empty. */
while (!(CAN_MSR_MRDY == (CAN_MSR_MRDY &
can_mailbox_get_status(CAN0, TEST1_CAN_COMM_MB_IDX))));
}
Title: Re: Building a CAN API for Arduino DUE
Post by: M2319 on Sep 25, 2013, 03:26 am
Hi,
I'm so glad I found this thread!  Thanks for all the hard work you guys have done.  It's a little bit confusing as to which repository is the best/latest API.  Can someone point me to the place where I can find it.  I see this link https://github.com/arduino/Arduino/tree/can/hardware/arduino/sam/libraries/CAN (https://github.com/arduino/Arduino/tree/can/hardware/arduino/sam/libraries/CAN) but it seems a little bit out of date. 

Thanks!  :smiley-mr-green: :smiley-mr-green:
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Sep 25, 2013, 04:12 am
Hello M2319,
The updated version is here:
https://github.com/collin80/due_can (https://github.com/collin80/due_can)

Regards!
Title: Re: Building a CAN API for Arduino DUE
Post by: Owais on Oct 09, 2013, 03:51 pm
Hello,
           I am in the process of understanding the CAN library created for DUE. I am having trouble in figuring out this exact declaration :
Can*  m_pCan;

           I have seen the typ def Can in the beginning of " due_can.h " but it is only as a reference as to how the struct is defined besides that there is no other definition available of this type. Can someone please clear this up for me.

Regards.
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Oct 09, 2013, 08:37 pm

Hello,
          I am in the process of understanding the CAN library created for DUE. I am having trouble in figuring out this exact declaration :
Can*  m_pCan;

          I have seen the typ def Can in the beginning of " due_can.h " but it is only as a reference as to how the struct is defined besides that there is no other definition available of this type. Can someone please clear this up for me.

Regards.


Hello Owais,

Can* m_pCan is a parameter of a Can function. More specifically, a defined address.
For the two Can controllers inside Arduino Due's SAM3X8E, the current CAN library (originally ported from Atmel) has defined two CAN addresses: CAN0 (0x400B4000U) and CAN1 (0x400B8000U). See sam3x8e.h file at

Quote
...\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system\CMSIS\Device\ATMEL\sam3xa\include


Let's dissect a bit the Can* m_pCan parameter:

Can: is a structure that contains the Can hardware registers, and it is part of the component_can.h file

Quote
...\arduino-1.5.2-windows\arduino-1.5.2\hardware\arduino\sam\system\CMSIS\Device\ATMEL\sam3xa\include\component


m_pCan: is the Pointer to a CAN peripheral instance.

When we declare Can*  m_pCan, m_pCan is a variable that stores the address of a variable of the structure Can.
If for example, we want to enable the CAN controller 0. Then in the main, the function call looks like:

Quote
CAN.enable();


And that's it! but... to get a better understanding (as you requested) of the parameter, let's take a look at the original function (before the CANRaw class was created).

The call of the old function looks like:
 
Quote
can_enable(CAN0);


And the old function itself:

Code: [Select]
void can_enable(Can*  m_pCan)
{
m_pCan->CAN_MR |= CAN_MR_CANEN;
}


Where...

CAN_MR_CANEN is the CAN controller enable register. Bit 0 (0x1u << 0)
CAN_MR is the CAN mode hardware register inside the Can structure.

Thus, when we type can_enable(CAN0); we are declaring the call of the can enable function that points to the CAN controller 0 that SET the bit 0 of the enable register in the can mode hardware register inside the can structure.

Notice that all of this is transparent for the user given that all the Can functions are handle for the CANRaw class.

I hope this helps. Regards!
Title: Re: Building a CAN API for Arduino DUE
Post by: Owais on Oct 10, 2013, 09:38 am
Thank you Palliser for taking the time to write a detailed response to my question it has helped me clear my understanding.

Just one more thing, as you have mentioned that the Can structure is from the file " component_can.h ", how/where do link this to our "due_can.h" so that the compiler knows where to look for Can structure?
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Oct 10, 2013, 04:04 pm

Just one more thing, as you have mentioned that the Can structure is from the file " component_can.h ", how/where do link this to our "due_can.h" so that the compiler knows where to look for Can structure?


Hello Owais,
I don't know which version of the IDE 1.5.x you are using but if you do a simple excercise of changing the name of the component_can.h file located at

Quote
...\arduino-1.5.x-windows\arduino-1.5.x\hardware\arduino\sam\system\CMSIS\Device\ATMEL\sam3xa\include\component


Let's say i.e. renaming as componen_can1.h, you will find the debugging chain showing the error. Here the simplify chain:

Quote
due_can.h->sn65hvd234.h->variant.h->Arduino.h->chip.h->sam.h->sam3.h->sam3xa.h->sam3x8e.h->component_can.h


Notice that it starts with due_can.h and then through the transceiver driver points to the variant file and so on until reach the component_can.h file that contains the Can structure.

Regards!
Title: Re: Building a CAN API for Arduino DUE
Post by: Owais on Oct 10, 2013, 05:14 pm
Thank you once again and excuse me if I am missing the obvious. I have now gone through the files and have somewhat basic understanding of the library. Moreover, I have read about Time Triggered CAN and gone through the Atmel datasheet for SAM3X, I have seen the function " void enable_time_triggered_mode(); "  in the " due_can.h " and would like to implement a test code for it. Since it is Time Triggered I figure that I will have to implement the code out of the " void Loop() ". Any suggestions as to how I should implement the Time Triggered mode?

Regards.
Title: Re: Building a CAN API for Arduino DUE
Post by: Transistorfips on Oct 10, 2013, 10:05 pm
Hi,
there are a lot of possibillities to do trigger a time event on the CAN-bus.
You can use the rtc,  one of the timers or simply use the standard Cortex system timer - which is by far the most easy-to-use timer as it is always scintillating in the background of the core. Have you installed the Atmel Software framework yet? There are plenty of examples and many of them use this particular system timer, for example in the "Getting-Started Application on SAM - Arduino Due/X" - which by the way use TC0 additionally (at least I think so), too.


Hope this helps,

Joachim
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Oct 10, 2013, 11:57 pm
I am not sure if the Time Trigger Mode mentioned in the Atmel doc11057 is related to the FTTC (Flexible Time-Triggered communication) high level layer extension to the CAN protocol (also known as TTCAN) specified in the ISO 11898-4 in 2004 for ECU devices. If so, I haven't implemented so far a test code solution, although it would be a good question for the Atmel AT91SAM support.
Title: Re: Building a CAN API for Arduino DUE
Post by: arduhanno on Oct 23, 2013, 04:22 pm
Hello,
I tried to run the Sample_1 only with one sn65hvd234 on CAN0 and connected the transceiver with an CAN-USB-Interface. I only want wo test the TX connection from Arduino -> PC. Until now without success. Does this sample work for my test application

On Pc I run a CAN diagnostic tool, which display each incoming message.
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Oct 23, 2013, 04:53 pm
Hello arduhanno,

Even though the current CAN bus library and examples were ported considering the Arduino DUE architecture and the Arduino IDE, the data that DUE spits or bites is pure CAN, thus, It shouldn't be so hard to make it work via USB.
I have done what you are trying to do but only via serial(DUE -> CAN Transceiver -> CAN-to-Serial converter -> PC).
I used a CAN Analyzer from an Italian brand (ADFweb). As a reference, for the serial communication I had to consider the following default parameters:
-Serial COM port.
-CAN bus protocol type: 11 bit
-CAN Baudrate: 1000K BPS

If you let me know the brand/model of the interface you are using, I could go through the documentation and see how they implemented it using USB. Regards!
Title: Re: Building a CAN API for Arduino DUE
Post by: arduhanno on Oct 23, 2013, 09:41 pm
Hello Palliser,

I am not sure but I really think you misunderstood me. The USB-Interface is an external device with an USB connector and an SUB-D 9 pol. connector. It is for connecting a CAN Bus on PC with a diagnostic software and API. So I know from other application that this device is working and will display every Frame received via CAN. I also can send any Frames from PC to can.

My problem is that I build my own PCB with transceiver on CAN0 and it does not work. I am not sure whether it is a hard- or software problem. Therefore I want to use a Sample only for sending data that I can see Frames on PC. Can I use the Sample 1?

Other question: After this will work, I want to send and receive CAN data both over CAN0. Is this a problem?
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Oct 23, 2013, 10:26 pm
You shouldn't have problems with the CAN examples (1,2,3 or 4). The idea behind the ported CAN project from Atmel to Arduino was to test the two CAN controllers (CAN0 and CAN1) doing the loop as you may have seen in this post. Assuming that your CANBUS side is OK, in the CAN side you should verify the following for the CAN0 transceiver (SN65HVD234):

1. Verify that pin 1 of the transceiver is connected to the CANTX pin of the DUE.
2. Verify that pin 4 of the transceiver is connected to the CANRX pin of the DUE.
3. Verify that pins 2 and 8 of the transceiver are connected to GROUND.
4. Verify that pins 3 and 5 of the transceiver are connected to +3V3.
5. Verify that the terminal resistor (120 ohm) is connected between CANH and CANL terminals.

See picture on reply #33
http://forum.arduino.cc//index.php?PHPSESSID=sd97s9v75d5d3d2r0fc23mcqu2&topic=131096.30 (http://forum.arduino.cc//index.php?PHPSESSID=sd97s9v75d5d3d2r0fc23mcqu2&topic=131096.30)

One more thing. Some CAN converters are aimed for low speed CAN networks, thus, you could try also to change the default CAN speed of 1Mbit to a lower value. For example to change the speed to 250Kbit, replace the following lines of the samples:

Code: [Select]
CAN.init(SystemCoreClock, CAN_BPS_1000K);
CAN2.init(SystemCoreClock, CAN_BPS_1000K);


with

Code: [Select]
CAN.init(SystemCoreClock, CAN_BPS_250K);
CAN2.init(SystemCoreClock, CAN_BPS_250K);


I hope this help. regards!
Title: Re: Building a CAN API for Arduino DUE
Post by: arduhanno on Oct 24, 2013, 10:10 am
Hellp Palliser,

tanks a lot for helping. Now, it works fine. One soldering at transceiver was fault.....  ;)

The test app works and I can see transmitted data on PC. Now, I want to use the lib from my program. Therefore I want to use this functionality and do not know if I have to use mailboxes or how I can realize it with the lib.

One function should be something like
Code: [Select]
send_frame(int ID, int len, int d1, ....., int d8)

In an other loop part I want to get all received Frames in the same structure.
How can I do this with the lib? Is there a documention for the lib?

I have to do both receive and send over CAN0.

regards
Title: Re: Building a CAN API for Arduino DUE
Post by: arduhanno on Oct 30, 2013, 08:53 am
I don’t know whether this is the right use of lib but the send function works:
Code: [Select]

int can_senden(int id, int len, int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7)
{
CAN.mailbox_set_id(1, id, false);
CAN.mailbox_set_datalen(1, len);
CAN.mailbox_set_databyte(1, 0, d0);
CAN.mailbox_set_databyte(1, 1, d1);
CAN.mailbox_set_databyte(1, 2, d2);
CAN.mailbox_set_databyte(1, 3, d3);
CAN.mailbox_set_databyte(1, 4, d4);
CAN.mailbox_set_databyte(1, 5, d5);
CAN.mailbox_set_databyte(1, 6, d6);
CAN.mailbox_set_databyte(1, 7, d7);
CAN.global_send_transfer_cmd(CAN_TCR_MB1);
return 0;
}

This is my setup code:
Code: [Select]


CAN.reset_all_mailbox();
//Rx-Setup
CAN.mailbox_set_mode(0, CAN_MB_RX_MODE);
CAN.mailbox_set_id(0, TEST1_CAN_TRANSFER_ID, false);
//Tx-Setup
CAN.mailbox_set_mode(1, CAN_MB_TX_MODE);
CAN.mailbox_set_priority(1, TEST1_CAN0_TX_PRIO);
CAN.mailbox_set_accept_mask(1, 0x1FFFFFFF, false);

can_senden(TEST1_CAN_TRANSFER_ID + 0x200, 7, 'A', 'R', 'D', 'U', 'I', 'N', 'O', 0);

CAN.enable_interrupt(CAN_IER_MB0);

And this is Code for incoming frame interpretation (only test structure, I will implement bitwise interpretation). How can I receive all incoming frames with every ID or ID from 100-200? I tried to set accept mask without success. It only works with ID set in mailbox_set_id.
Code: [Select]


if (CAN.rx_avail())
{
CAN.get_rx_buff(&inFrame);
Serial.print("CAN message received= ");
Serial.print(inFrame.data[0]);
Serial.print(inFrame.data[1]);
Serial.print(inFrame.data[2]);
Serial.print(inFrame.data[3]);
Serial.print(inFrame.data[4]);
Serial.print(inFrame.data[5]);
Serial.print(inFrame.data[6]);
Serial.println(inFrame.data[7]);

if (inFrame.data[0] == 0x11)
{
LED_on();
can_senden(TEST1_CAN_TRANSFER_ID + 0x200, 6, 'L', 'E', 'D', ' ', 'O', 'N', 0, 0);
}

if (inFrame.data[0] == 0x22)
{
LED_off();
can_senden(TEST1_CAN_TRANSFER_ID + 0x200, 7, 'L', 'E', 'D', ' ', 'O', 'F', 'F', 0);
}
}


Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Oct 30, 2013, 03:56 pm

This is my setup code:
Code: [Select]


CAN.reset_all_mailbox();
//Rx-Setup
CAN.mailbox_set_mode(0, CAN_MB_RX_MODE);
CAN.mailbox_set_id(0, TEST1_CAN_TRANSFER_ID, false);
//Tx-Setup
CAN.mailbox_set_mode(1, CAN_MB_TX_MODE);
CAN.mailbox_set_priority(1, TEST1_CAN0_TX_PRIO);
CAN.mailbox_set_accept_mask(1, 0x1FFFFFFF, false);

can_senden(TEST1_CAN_TRANSFER_ID + 0x200, 7, 'A', 'R', 'D', 'U', 'I', 'N', 'O', 0);

CAN.enable_interrupt(CAN_IER_MB0);

And this is Code for incoming frame interpretation (only test structure, I will implement bitwise interpretation). How can I receive all incoming frames with every ID or ID from 100-200? I tried to set accept mask without success. It only works with ID set in mailbox_set_id.


Overall your code looked pretty decent. It should work fine. But, you wanted to know how you might accept every frame 100 to 200. I assume you mean in hex (0x100 to 0x200) and not decimal. The basic process works the same anyway.

To accept a range of message ids you need to figure out which bits they always have in common and which they do not. So, turn 0x100 and 0x200 into binary:

0x100 = 1 0000 0000
0x200 = 10 0000 0000

In between these two the bottom 8 bits will vary from 0000 0000 to 1111 1111. There is a predicament here. It would be easy to accept all frames from 0x100 to 0x1FF but 0x200 is in a different range. If it would be OK to just accept 0x100 through 0x1FF then you'd set your mask to 0x700 and your ID to match against as 0x100. This way any incoming frames are AND with the mask then compared to the match ID. Any frame 0x100 to 0x1FF will pass this test. Any other frame will not. Try it: 0x253 AND 0x700 = 0x200 (doesn't match), 0x070 AND 700 = 0 (doesn't match). 0x1B5 AND 700 = 0x100 (matches!)
Title: Re: Building a CAN API for Arduino DUE
Post by: semerson on Oct 31, 2013, 10:20 pm
I sent a message to Palliser but I figured I'd post here as well:

I'm working on using the due can library to read the can bus of a battery management system and motor controllers for an electric racecar. I've built two shields with transceivers which appear to be functional. My setup is two dues, each using CAN0, or the labeled cantx and canrx lines. One is set up to transmit over the bus and the other is set up to receive. The setup looks like this:

https://www.dropbox.com/s/lrf63pz3mfl6s85/2013-10-26%2017.03.02.jpg
https://www.dropbox.com/s/ma2ak5gbajj547n/2013-10-26%2017.03.16.jpg

When running my code, it would seem to me from the oscilloscope that the bus is only clocking or syncing and not actually sending any data.

When probes are hooked to the pins on the arduino (cantx on transmitter, canrx on receiver) the output looks like so:

https://www.dropbox.com/s/nmzru0i89r6r11t/2013-10-26%2017.03.40.jpg

And canh and canl lines look like so:

https://www.dropbox.com/s/vic7v8wugrr4gwc/2013-10-26%2017.08.17.jpg

At this point, I'm stuck. I can't figure out what is wrong with my code or if there is something wrong with my physical setup. Can anyone see anything wrong here?

Thanks for your help,
Shep
Title: Re: Building a CAN API for Arduino DUE
Post by: semerson on Nov 07, 2013, 05:12 pm
Anybody?

Here's my CAN_RX code if that helps (can't find the CAN_TX at the moment, I will have to search around for that if it's needed):

CAN_RX.ino:
Code: [Select]
// CAN_RX.ino
// Arduino Due - CAN Test based on CAN Sample 3
// RX Side

// Required libraries
#include "variant.h"
#include <due_can.h>

#define TEST1_CAN_COMM_MB_IDX    0
#define TEST1_CAN_TRANSFER_ID    0x07
#define TEST1_CAN0_TX_PRIO       15
#define CAN_MSG_DUMMY_DATA       0x55AAAA55

// CAN frame max data length
#define MAX_CAN_FRAME_DATA_LEN   8

uint32_t receivedFrames;

void setup() {

// start serial port at 115200 bps:
  Serial.begin(115200);

  // Verify CAN0 initialization, baudrate is 1Mb/s:
  if (CAN.init(SystemCoreClock, CAN_BPS_1000K)) {

    // Disable all CAN0 interrupts
    CAN.disable_interrupt(CAN_DISABLE_ALL_INTERRUPT_MASK);

    // Configure and enable interrupt of CAN0, as the tests will use receiver interrupt
    NVIC_EnableIRQ(CAN0_IRQn);
  }
  else {
    Serial.println("CAN initialization (sync) ERROR");
  }

}

// Test the transmission from CAN0 Mailbox 0 to CAN1 Mailbox 0
static void test_1(void)
{
  //Reset all CAN0 and CAN1 mailboxes:
  CAN.reset_all_mailbox();

  //setup four receive mailboxes that each accept a different frame id
  for (uint8_t count = 0; count < 4; count++)
  {
    CAN.mailbox_set_mode(count, CAN_MB_RX_MODE);
    CAN.mailbox_set_accept_mask(count, 0x7FF, false); //only accept the exact ID
    CAN.mailbox_set_id(count, TEST1_CAN_TRANSFER_ID + count * 4, false);
    Serial.print("ID: ");
    Serial.print(TEST1_CAN_TRANSFER_ID + count * 4);
    Serial.print("\n");
   
  }

  // Enable interrupt for mailboxes 0-3 on first canbus
  CAN.enable_interrupt(CAN_IER_MB0 | CAN_IER_MB1 | CAN_IER_MB2 | CAN_IER_MB3);

  // Wait for the communication to come in.
  Serial.print("Waiting for a frame...\n");
  Serial.print(CAN.rx_avail());
  while (!CAN.rx_avail()) { //while no frame is received
  }

  RX_CAN_FRAME inFrame;
  while (CAN.rx_avail()) {
      CAN.get_rx_buff(&inFrame);
      receivedFrames++;
  }
}

// can_example application entry point
void loop()
{

// Run test
while (Serial.available() == 0)
{
  Serial.print("Testing...\n");
  test_1();
  delay(8);
  Serial.print(" R: ");
  Serial.println(receivedFrames);
  Serial.print("\n");
}

// Disable CAN0 Controller
CAN.disable();

while (1) {
        }
}
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Nov 08, 2013, 08:48 pm
Your CANH/L signals look awful. There is way too much slope on those signals. Are you terminating each end with 120 ohm resistors? Your pictures look to have something like 100k and 22k resistors (it's hard for me to make out all the colors) which doesn't seem to make sense unless I'm looking at the wrong thing.
Title: Re: Building a CAN API for Arduino DUE
Post by: semerson on Nov 10, 2013, 08:03 pm

Your CANH/L signals look awful. There is way too much slope on those signals. Are you terminating each end with 120 ohm resistors? Your pictures look to have something like 100k and 22k resistors (it's hard for me to make out all the colors) which doesn't seem to make sense unless I'm looking at the wrong thing.


Looks like that's the error... accidentally read that off the schematic as a 120k (didn't have any in stock which is why I had the 120k+22k series combo). I'll have to solder in a 120ohm and see if it works.

Thanks for your help,
Shep
Title: Re: Building a CAN API for Arduino DUE
Post by: pjrc on Nov 17, 2013, 04:04 pm
This may be a silly question, but I'd like to ask about the status of this library's API....

Specifically, is this really the final "official" API for CAN on Arduino?

The reason I ask is because this API seems to disregard many of the common conventions and the overall style of Arduino APIs.  Most communication libraries inherit Stream (http://arduino.cc/en/Reference/Stream) or mimic Stream.  The init(), rx_avail(), get_rx_buff() and sendFrame() are pretty close, but they do require users to deal with structs, which is not commonly done with Arduino APIs.  The mailbox API seems to be particularly complex, and tied to Due's specific hardware, rather than attempting to provide a simplified abstraction that most Arduino users expect, with just a few easy-to-learn functions which cover most common needs.

Another common design feature of many Arduino APIs is the use of hardware neutral base classes.  Stream doesn't neatly fit CAN.  The closest is probably Udp.h, which Arduino 1.0 added to the core library.  The idea is you can write a UDP-based library or sketch using the base class functions, and then it will work when used with different libraries (eg, both Ethernet and Wifi).  Perhaps CAN needs its own abstract base class?  This sort of abstraction seems particularly valuable for developing the higher level protocols, like ODB-II.

So my question is whether this API is now forever fixed, or if it's still in significant development, and if it is, would a migration towards a more Arduino-like API be welcome?
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Nov 18, 2013, 01:00 am
I'd call the current state of the canbus library "just barely operational enough to be useable." And, I say that being one of the people responsible for it.  It does include a total mish-mash of API formats. I suppose the proper answer to your question is that it's still in a state of significant development and, yes, migrating to a more Arduino friendly format would probably be a good thing. The library is used in a project of mine and I think we added a couple of convenience functions that maybe didn't make their way back into the library but either way the library is a mess. I had always meant to go back and clean it up but you know how that goes. It works so nobody ever quite gets back around to making it pretty. I do still want to fix it up though.
Title: Re: Building a CAN API for Arduino DUE
Post by: pjrc on Nov 18, 2013, 12:39 pm
Thanks Collin, for such an honest appraisal.

Within the next few months, I plan to work on a new CAN library for different hardware.  I had hoped to work towards compatibility with a well-defined official Arduino CAN API.  Seems such an API is still some distance in the future?

When talking about APIs for Arduino, it's usually good to refer to Arduino's style guide.

http://arduino.cc/en/Reference/APIStyleGuide

Bringing up new hardware AND designing an official API (implementing and testing on Due as well) might be more work that I can realistically take on?  Then again, I'll need to implement something for an API.  I really do care deeply about compatibility.  I know from experience that starting without an API plan usually results with an API deeply rooted in the hardware's specific details.  From what I can see, it looks like the Atmel guy did this, then you've been adding the higher level stuff?

My hope at this point, if you and others are willing, would be to discuss what the final, official Arduino CAN API might be, in a perfect universe where we all had plenty of time to actually work on the code?

I've been reviewing the existing CAN APIs.  So far, I've found 3 Arduino libraries: this one, Seeed's for their MCP2515-based shield, and ChipKit.  Seeed's API is fairly simple, just 8 public functions.  ChipKit's API is horribly complex.  The other API I've seen which seems the most Arduino-like, is mbed's:

http://mbed.org/handbook/CAN#api

I'd like to ask a couple specific API questions...

#1: How do you feel about using a special typedef or object for message data, versus primitive types?  Currently Due, Chipkit and mbed use these.  It does allow passing more info together, like the ID, length, timestamp.  But it also doubles the API, from just one object that communicates to 2 objects, with more stuff to learn to create and extract the data.

#2: Does providing mailboxes in the API add more benefit for users than the increase in complexity and overall size of the API they require?  For many Arduino users, the ease of learning is key.  An API with fewer than 10 functions, where all have simple names, seems approachable.  Seeed and mbed don't expose mailbox functionality.  Due and ChipKit have APIs with dozens of functions and long lists of constants.  So my question is whether mailbox access (rather than simple read & write) is really beneficial for some uses, and what those applications might be?
Title: Re: Building a CAN API for Arduino DUE
Post by: pjrc on Nov 20, 2013, 02:25 pm
I made a quick PCB layout for the schematic in message #33.  Here the shared design, if anyone's interested.

http://www.oshpark.com/shared_projects/e2BC6FkW
Title: Re: Building a CAN API for Arduino DUE
Post by: bvernham on Dec 22, 2013, 01:52 am
Any progress on this effort?

What can be done to help?
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Dec 23, 2013, 03:27 pm

Any progress on this effort?

What can be done to help?


Sorry, I haven't gotten around to working on the library in some time. I do hope to get back to it soon.

At this point what needs to happen is for someone (me, you, someone) to create a sensible API that we would then follow. The way it works currently is very convoluted and silly. You shouldn't need to know so much about the hardware to use the library. Paul had wanted to do all of this a while back and things just sort of fell by the wayside - probably my fault.  He had mentioned looking at the APIs for things like mbed. That's probably a good idea. Ideally we'd have an API where you initialize a canbus port by giving the speed and then you can set the filters in an easy way and send frames just by giving an ID, whether it is extended or standard, and the data bytes. There should be a structure or class that we use for canbus frames. It probably would be easiest if we used the same structure/class for both RX and TX frames even though they've got slightly different info (RX frames have no priority while TX frames do).
Title: Re: Building a CAN API for Arduino DUE
Post by: pjrc on Dec 23, 2013, 06:02 pm
I am still planning to work on this.  Since mid-November, a number of things have happened.

PJRC released Teensy 3.1 on December 9.  Obviously my main interest is developing a library to support CAN on Teensy.  I do want to work towards a really good, hardware-neutral API, even if that means getting somewhat involved in redesigning Due's library.

So far, the only real "work" I've done on CAN is buying and building hardware.  I built up that little circuit board for Due, and a CAN interface board for Teensy 3.1, and I purchased a couple MCP2515-based modules and connected them to the other Teensy boards.  As you can see, I've been putting together the hardware side for working on a CAN library.....

(http://www.pjrc.com/teensy/beta/can_hardware_test.jpg)
Title: Re: Building a CAN API for Arduino DUE
Post by: MrCanBus on Dec 23, 2013, 07:53 pm
Paul. I'm watching this intently, but helping would be way beyond my skill level.  My main interest in this is automotive, and specifically motorbike at that.  I already have an Uno with Sparkfun CAN shield which I've used to monitor and subsequently decode much of the data on my Ducati Multistrada. An easy to DIY teensy solution will address my current space issues. 

P.s. Excuse the slightly arrogant user name, it up was one that I used on a Ducati forum so dealers wouldn't link the posts back to me and then deny and future (unrelated) warranty claims!
Title: Re: Building a CAN API for Arduino DUE
Post by: bvernham on Dec 23, 2013, 11:35 pm
I have some experience with the MCP2515 with both the ISO and the J1939.

I built logger and gateways using the UNO and the 1284P.

I also have access to buss analysis tools and would be willing to help out as needed.

Thanks

Bruce
Title: Re: Building a CAN API for Arduino DUE
Post by: pjrc on Dec 24, 2013, 12:04 am
What bus analyzer do you use?  I've been considering getting one of these, but mainly because I have their USB analyzer that works quite well.

http://www.totalphase.com/products/komodo_cansolo/

Truth is, I have pretty much zero experience with CAN bus.  But I do have 20+ years experience developing with microcontrollers and several years on Arduino libraries and code.

I could use a little advice about what to do on the CAN side of things.....
Title: Re: Building a CAN API for Arduino DUE
Post by: bvernham on Dec 24, 2013, 01:12 am
I use the Vector CANALYZER PRO with J1939 option.

These are pretty expensive and I have tried to replace this with some small micros.  I have 4 gateways running with UNO's right now.  $80 bucks ~$2000.
Title: Re: Building a CAN API for Arduino DUE
Post by: noop on Dec 24, 2013, 07:41 pm
Any help is greatly appreciated, you guys are the greatest. I just want to send and receive one message in 11 bit and 29 bit. When I change the example DUMMY_DATA to 0x1234 and 0x5678, I get some kind of endian format  with number pairs reversed. And the 29 bit message header is always 0. I can't get the message header for 29 bit. I have a seeeduino shield to monitor also. The seeeduino receives the right bit header, but not the Due. It also does not seem to send the 11 bit correctly. I tried to clear codes on my car but was not successful. The message never got interpreted by the ecu correctly.
Would someone be able to post simple code that I can analyse? I only want one mailbox to send, and then receive for 11 and 29 bit - all masks will do.
I too am watching this forum closely. I agree that the code should be simplified, like seeeduino with less concern for the Due internals.
Title: Re: Building a CAN API for Arduino DUE
Post by: Arbies on Dec 26, 2013, 09:01 pm
I put in an order for the OSH Park Arduino Due CAN Test Board.
Does anyone have a parts list or link to a shopping cart (jamco, mouser, digikey) I can use for the can transceiver, resistor, caps, and DB9 header.
I am fairly new to surface mount components and am not sure what physical size components were used in the design.
I already have a DB9 to OBD adapter from the UNO can shield.
Title: Re: Building a CAN API for Arduino DUE
Post by: tbit on Dec 26, 2013, 09:19 pm
Yes pm me. Fwiw I finally have a production run of shields on order and I can share some of my prototyping stuff and parts list.
Title: Re: Building a CAN API for Arduino DUE
Post by: pjrc on Dec 26, 2013, 09:44 pm

I put in an order for the OSH Park Arduino Due CAN Test Board.
Does anyone have a parts list or link to a shopping cart (jamco, mouser, digikey) I can use for the can transceiver, resistor, caps, and DB9 header.


Yes, here you go.  I also updated the description at OSH Park (http://www.oshpark.com/shared_projects/e2BC6FkW).

Code: [Select]

    Qty   Digikey P/N       Description
    ---   -----------       -----------
     2    296-27991-1-ND    CAN transceiver
     2    445-7660-1-ND     Capacitor, 10uF, 805
     2    399-1170-1-ND     Capacitor, 0.1uF, 805
     2    609-4003-ND       DB9M connector
     2    CF14JT120RCT-ND   Resistor, 120 ohm
     2    3M9447-ND         Header, 2 pin
     2    3M9580-ND         Jumper

Title: Re: Building a CAN API for Arduino DUE
Post by: noop on Dec 27, 2013, 04:29 pm
Ok, it seems I had to send three times to the ecu before it responded.
id = 0x7DF
databyte[0]=0x2
databyte[1]=0x1
databyte[2]=0x0
databyte[3 to 7] = 99
global send three times in succession did the trick. I guess my jury-rigged shield needs help.
Title: Re: Building a CAN API for Arduino DUE
Post by: noop on Jan 04, 2014, 04:29 pm
Well I finally got my shield to work reliably, bad soldering was the problem.
Now I am faced with a 29 bit extended frame problem.
I modified the Sample 4 to Serial print some data

#define TEST1_CAN_TRANSFER_ID    0x11AE756A //random 29 bits

          CAN.get_rx_buff(&inFrame);  //read the message
          //display the message
          Serial.print(" fid ");Serial.println(inFrame.fid);
          Serial.print(" ide ");Serial.println(inFrame.ide);
          Serial.print(" inFrame id = "); Serial.println(inFrame.id, HEX);
          Serial.print("  id len = "); Serial.println(sizeof(inFrame.id), HEX);         
         
What I don't see correctly is the inFrame.id.
This is what I get:   
     inFrame id = 2756A
     id len = 4
     fid 40000000
     ide 1

The due_can.cpp file has the following:
        ul_id = m_pCan->CAN_MB[uc_index].CAN_MID;
   if ((ul_id & CAN_MID_MIDE) == CAN_MID_MIDE) { //extended id
      rxframe->id = ul_id & 0x3ffffu;
      //rxframe->id = (ul_id);     // I wanted to look at the id - it always returns a 3 in the leftmost position
      rxframe->ide = 1;
   }
   else { //standard ID
        rxframe->id = (ul_id  >> CAN_MID_MIDvA_Pos) & 0x7ffu;
      rxframe->ide = 0;
   }

        inFrame id = 2756A  //should be  11AE756A that was set as the id
        id len = 4
        fid 40000000
        ide 1
Here is the modified cpp file result:
inFrame id = 31AE756A

I modified the file to look at the id and it returns 31AE756A (always a 3 in front)

I am hoping someone will show me just how dumb I am that I am overlooking an obvious parameter.




Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jan 05, 2014, 10:06 pm
I'm fairly sure that extended frames are messed up in a variety of ways currently. I'm going back through this library once again and fixing it up so the problems should be corrected within a week or so.  Also, I'm vastly simplifying the interface for most uses so it should be a lot easier to use too.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jan 08, 2014, 04:25 am
I have just pushed a commit to github. There are two examples now (as opposed to the previous four) but they show how to use the library now. Word of warning: If you update to this version of the library you will very likely need to update your sketch. Some things have changed. The function to get frames (get_rx_buff) now takes a reference parameter instead of a pointer. This should be easier for most people to use. There are now easy functions for sending frames, receiving frames, and setting filters. There is now less need to directly call low level functions or have to mess with the interrupts. A lot more things are handled within the library now. Additionally, all can frames now use CAN_FRAME instead of having different versions for RX and TX. CAN_FRAME now uses a union for the data bytes so you can directly set things 1 byte, two bytes, 4 bytes, or all 8 bytes all at once.

I have done various tests and this version of the library seems to properly send frames in both standard and extended mode. I have tested reception of standard frames but haven't tested reception of extended frames yet. Both included examples have been verified by connecting a Kvaser Leaf Light to a dual canbus board to scope the traffic.

More changes are coming soon. I'll also create a document that details how to use the new functions.
Title: Re: Building a CAN API for Arduino DUE
Post by: jwhsiehum on Jan 10, 2014, 03:06 pm
Hi all,

I recently started following this thread and I was trying to see if I could get it to work on my own. I cant, however , seem to get the examples to compile and I keep getting the following errors.

CAN_EchoTest.ino:7:21: error: variant.h: No such file or directory
In file included from C:\Users\jhsieh\Documents\Arduino\libraries\due_can/due_can.h:72,
                 from CAN_EchoTest.ino:8:
C:\Users\jhsieh\Documents\Arduino\libraries\due_can/sn65hvd234.h:34: error: 'uint32_t' does not name a type
C:\Users\jhsieh\Documents\Arduino\libraries\due_can/sn65hvd234.h:37: error: 'uint32_t' does not name a type
C:\Users\jhsieh\Documents\Arduino\libraries\due_can/sn65hvd234.h:39: error: 'uint32_t' does not name a type
etc...

I'm sure I have done something obviously wrong. Can anyone point it out?
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jan 10, 2014, 03:08 pm

Well I finally got my shield to work reliably, bad soldering was the problem.
Now I am faced with a 29 bit extended frame problem.

The due_can.cpp file has the following:
        ul_id = m_pCan->CAN_MB[uc_index].CAN_MID;
   if ((ul_id & CAN_MID_MIDE) == CAN_MID_MIDE) { //extended id
      rxframe->id = ul_id & 0x3ffffu;
      //rxframe->id = (ul_id);     // I wanted to look at the id - it always returns a 3 in the leftmost position
      rxframe->ide = 1;
   }
   else { //standard ID
        rxframe->id = (ul_id  >> CAN_MID_MIDvA_Pos) & 0x7ffu;
      rxframe->ide = 0;
   }


Your problem with the extended ID was not your fault at all. You can see in the snippet you posted this line:

rxframe->id = ul_id & 0x3ffffu;

I'm not sure what sort of brain malfunction beset me or whoever wrote that but the proper mask for extended frames is 0x1FFFFFFF not 0x3FFFF. I just fixed that in the git repo. Extended IDs are stored in the bottom 29 bits of the register so all you have to do is mask off the extended bit which is what that line was supposed to do. Try your sketch again and I think you'll see it work better now. Sorry about that.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jan 10, 2014, 03:11 pm

Hi all,

I recently started following this thread and I was trying to see if I could get it to work on my own. I cant, however , seem to get the examples to compile and I keep getting the following errors.

CAN_EchoTest.ino:7:21: error: variant.h: No such file or directory
In file included from C:\Users\jhsieh\Documents\Arduino\libraries\due_can/due_can.h:72,
                 from CAN_EchoTest.ino:8:
C:\Users\jhsieh\Documents\Arduino\libraries\due_can/sn65hvd234.h:34: error: 'uint32_t' does not name a type
C:\Users\jhsieh\Documents\Arduino\libraries\due_can/sn65hvd234.h:37: error: 'uint32_t' does not name a type
C:\Users\jhsieh\Documents\Arduino\libraries\due_can/sn65hvd234.h:39: error: 'uint32_t' does not name a type
etc...

I'm sure I have done something obviously wrong. Can anyone point it out?


Hmm... variant.h does exist. My guess is maybe you aren't using the proper Arduino IDE (you need 1.5.2 or newer) or that you forgot to set the board to Arduino Due (programming or native port).  Otherwise, you do have the library installed to the proper place so it should work.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jan 10, 2014, 03:13 pm
I posted another example (a traffic snooper). But, does anyone have any other requests or suggestions? Maybe an example of how to do PID requests and parse responses for OBDII? I'd like at least some of the examples to be useful right off the bat for various things instead of being pointless demonstrations of features.
Title: Re: Building a CAN API for Arduino DUE
Post by: jwhsiehum on Jan 10, 2014, 03:29 pm
So originally I noticed I needed to update arduino and I did, but my stupid self forgot to switch the board back after updating. Thanks for the help!
Title: Re: Building a CAN API for Arduino DUE
Post by: RPieter on Jan 14, 2014, 11:30 pm
Hello Collin,

Respect for doing this. I will read this topic in more detail ASAP, because I am now using a UNO with a CAN-bus shield. To use the DUE will be a big inprovement.

Thanks! I will give you feedback and I am willing to help further develop this. You can always PM me if you need help with something. I am not an expert, but  maybe there is something I can do.

Pieter


I have just pushed a commit to github. There are two examples now (as opposed to the previous four) but they show how to use the library now. Word of warning: If you update to this version of the library you will very likely need to update your sketch. Some things have changed. The function to get frames (get_rx_buff) now takes a reference parameter instead of a pointer. This should be easier for most people to use. There are now easy functions for sending frames, receiving frames, and setting filters. There is now less need to directly call low level functions or have to mess with the interrupts. A lot more things are handled within the library now. Additionally, all can frames now use CAN_FRAME instead of having different versions for RX and TX. CAN_FRAME now uses a union for the data bytes so you can directly set things 1 byte, two bytes, 4 bytes, or all 8 bytes all at once.

I have done various tests and this version of the library seems to properly send frames in both standard and extended mode. I have tested reception of standard frames but haven't tested reception of extended frames yet. Both included examples have been verified by connecting a Kvaser Leaf Light to a dual canbus board to scope the traffic.

More changes are coming soon. I'll also create a document that details how to use the new functions.
Title: Re: Building a CAN API for Arduino DUE
Post by: coryjfowler on Jan 18, 2014, 05:57 pm
I've read most of this topic, then started skimming though it as troubleshooting questions took up most of the posts.  I wanted to clarify some things and also add to the discussion about the MCP2515 CAN protocol controller that has been used on the ATmega328 among other legacy Arduinos as I have experience with it.  At some point I, if someone else does not try to first, want to make it interoperable with the Due's CAN library so more than two transceivers could be used with the Due.  I suspect after the modifications I recently made to the MCP2515 library that it might work now, but I do not have a Due, yet, to test with.

As for J1939 and other High-Level "CAN" protocols.  Those should be ENTIRELY supported with the CAN controllers on the Due.  I've used the ATmega328 with the MCP2515 to interface directly with J1939 systems.  J1939 really is just a specification to make smart engine sensors and components work between manufactures.  It transmits extended CAN IDs and 8 data bytes.  The only catch is, you have to figure out the CAN ID from the PGN and source address which is not that hard.  If you have access to a list of PGNs and SPNs, you can figure out a lot of what goes on in a J1939 bus.  I used J1939 at my employer, and I have become very familiar with it.

I also want to say that support for higher-level protocols should be made in an additional library that takes use of the CAN library.  There is no reason to add high-level bloat to the low-level library that not everyone will want or need, but that is my opinion.

As for what IC should be implemented on the Due CAN Shield, I would really like to see the 235 used because the ability for auto-baud selection without sending errors to the bus would be very useful to me.  I would also suggest that the standard CAN pin-out be used if a DE9 is put on the shield.
Title: Re: Building a CAN API for Arduino DUE
Post by: tbit on Jan 20, 2014, 03:11 am
FWIW - we have a production run of protoshields with CAN support now available. http://togglebit.net/product/arduino-due-can-shield/ (http://togglebit.net/product/arduino-due-can-shield/). Also found on Ebay. Thank you to everyone who helped and provided feedback!
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Jan 21, 2014, 03:54 pm
Nice product! Thank you Dan for your contribution to make the Arduino DUE CAN interface easy to access using your protoshield. Please, keep us posted about the feedback from your curstomers. Regards, Palliser.
Title: Re: Building a CAN API for Arduino DUE
Post by: RPieter on Jan 25, 2014, 12:42 pm
Hey guys,

I also ordered a prototype shield from Dan. Looking forward to working with it :)
Title: Re: Building a CAN API for Arduino DUE
Post by: Lukas_ on Jan 27, 2014, 03:48 pm
Hi,

is this junit fully working with J1939 CAN-Bus on both channels?
Thought about buying a chipkit max32, but arduino convinced me more if it is working.
Title: Re: Building a CAN API for Arduino DUE
Post by: tbit on Jan 27, 2014, 04:14 pm
The unit is "working", in that you can communicate via CAN bus using the libraries published here. Understand J1939 is a higher level protocol that would need to be developed to sit "on top of" the CAN libraries. J1939 is implemented over CAN bus as it's "physical layer". So yes, the hardware works but there would be some software work involved. Perhaps someone on the board has done 1939 work with the DUE and can respond. Feel free to contact me directly as well.
Title: Re: Building a CAN API for Arduino DUE
Post by: Lukas_ on Jan 27, 2014, 05:42 pm
Thanks,

already using a UNO with mcp2515 on J1939 Bus.
But its loosing frames...

Wan't to log can traffic in car, especially for programming over bus.
There are 2 separate buses with 125 and 250 kbits.
It would be fine to log both buses at the same time and output over serial bus or on SD-Card.

This unit seems to be powerful enough, but a working library makes the difference...
Title: Re: Building a CAN API for Arduino DUE
Post by: RPieter on Jan 27, 2014, 06:04 pm
Hi Lukas and Dan,

I also would like to have an upper level library for both J1939 and OBDII. I ordered an Arduino DUE CAN-Shield from Dan to test the CAN functionalities from the current CAN Library. When I understand how this is done I hope there is a way to work together here on the Forum to develop an OBDII and J1939 upper level Library. That would be so great. For OBDII there is this library from SK PANG (distributed by Sparkfun too) which is used on an UNO: http://skpang.co.uk/catalog/arduino-canbus-shield-with-usd-card-holder-p-706.html (http://skpang.co.uk/catalog/arduino-canbus-shield-with-usd-card-holder-p-706.html)

I am not sure how good this library is, but maybe it is straight forward to port it on top of the standard Arduino DUE library.

Somebody experienced with this SK Pang shield and the OBDII library?

Pieter
Title: Re: Building a CAN API for Arduino DUE
Post by: Lukas_ on Jan 27, 2014, 09:11 pm
On UNO i use that:
http://www.nunoalves.com/open_source/?p=475 (http://www.nunoalves.com/open_source/?p=475)
but it's loosing frames...

If it makes sense, i will order one of this powerful units too?
Working together and bring this unit on a useful and practical background sounds like a good idea :)

Also need the K-Line to unlock both can channels in car on the obd socket,
this should be not so hard with a transistor...

Collins library looks good, has anyone tested it successful?
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jan 28, 2014, 01:56 am
I have looked at both of the above mentioned canbus libraries (SK-PANG and nunoalves). In either case the actual PID/OBDII processing is pretty basic and straight forward. There is no reason that such routines could not easily be constructed for the Due canbus library as well. The actual sending and receiving of OBDII frames over canbus is pretty simple. You generally send to a broadcast address a simple frame with 8 data bytes and you get back a frame from the ECU that chose to respond. This frame also has 8 bytes. The format is explained at: http://en.wikipedia.org/wiki/OBD-II_PIDs

As for testing my library successfully: A previous version of it has been used in an open source vehicle control unit for around a year now. The most recent version has been tested by me with various other devices and seems to function. I actually haven't gotten a lot of feedback on the newest version so buyer beware - it works for me though.

That ECU project actually has OBDII/PID code in it already in the WIP branch. I haven't gotten a chance to test it out yet but the basic idea is there. I think that someone suggested that it'd be good to build higher protocol layers into their own libraries and not muddy up the canbus library with all sorts of protocols. That's a good idea. The PID library would be really simple but that's OK. I'll try to create such a thing in the next week or so.
Title: Re: Building a CAN API for Arduino DUE
Post by: tbit on Jan 28, 2014, 02:14 am
I have a periodic acquisition scheduler for CAN messages and an obd2 lib based upon Collins previous lib I've been working with as well. I will try to do a bit more testing with it and post it new
Title: Re: Building a CAN API for Arduino DUE
Post by: jspobuk on Jan 29, 2014, 12:37 am
Wow, looks like a lot has been going on since I last looked on this forum.  That's awesome.  I see some people have mentioned J1939.  My immediate application involves J1939 and I have successfully sent messages and received them.  I have even changed the runtime hours and engine temperature on a J1939 display device.  However, in our water pumping schemes we need to vary the speed of the engines and I don't know what the PGN is for that or if it is proprietary data or whatever.  So I am looking forward to higher protocol layers being added by someone more knowledgeable about J1939 than me.  Keep up the good work guys!
Title: Re: Building a CAN API for Arduino DUE
Post by: coryjfowler on Jan 29, 2014, 02:46 am

On UNO i use that:
http://www.nunoalves.com/open_source/?p=475 (http://www.nunoalves.com/open_source/?p=475)
but it's loosing frames...

If it makes sense, i will order one of this powerful units too?
Working together and bring this unit on a useful and practical background sounds like a good idea :)

Also need the K-Line to unlock both can channels in car on the obd socket,
this should be not so hard with a transistor...

Collins library looks good, has anyone tested it successful?


Try the MCP2515 library on my GitHub...
Title: Re: Building a CAN API for Arduino DUE
Post by: MarkOne on Feb 05, 2014, 03:51 pm
Hi,
I'm new to the forum and Arduino. I do know CAN however. Looking forward to getting started with Due CAN.

I don't quite understand the capacitor types specified in the diagram in reply #33. I'm not familiar with the symbols used. Is it a 10uF electrolytic and a 100nF Ceramic ?

Thanks,
Mark
Title: Re: Building a CAN API for Arduino DUE
Post by: graynomad on Feb 05, 2014, 04:06 pm
You cannot tell the type of cap from that schematic, if it's important it should be documented somewhere, either directly on the schem or in a BOM or other documentation.

However as they are just for decoupling I'd say the 100n is a ceramic and the 10u can be a tant or electro or whatever you have in the bottom of a drawer.

______
Rob
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Feb 06, 2014, 02:41 am
Graynomad is right!

From Atmel's BOM:

ID          VALUE       DESIGNATION                                          REFERENCE                          MANUFR.         FOOTPRINT
---        -------         --------------                                       ------------                         ----------         -----------
C1/C3      10uF       LOW ESR TANTALUM CAPACITOR     TPSA106K010R1800                  AVX                        A 
C2/C4    100nF     Ceramic Capacitor                                  C1608X7R1C104KT                  TDK                       603

Regards,
Palliser
Title: Re: Building a CAN API for Arduino DUE
Post by: MarkOne on Feb 09, 2014, 12:32 am
Thanks, I now have both CAN channels working.

I've written a very simple gateway (below) and tested it using CANalyzer. I used two messages for each CAN channel - each message containing 2 x 32 bit signals. I used a sine wave generator to provide a moving number. I varied the frequency of frames sent by CANalyzer in order to control bus load.

I was able to post process the CANalyzer log to get an idea of the gateway performance: (all 500kBaud)
55.25% Busload - CAN0 Rx, CAN1 Tx 0.07% dropped,   CAN1 Rx, CAN0 Tx 0.10% dropped
69.0%  Busload - CAN0 Rx, CAN1 Tx 0.04% dropped,   CAN1 Rx, CAN0 Tx 0.06% dropped
91.5%  Busload - CAN0 Rx, CAN1 Tx 0.61% dropped,   CAN1 Rx, CAN0 Tx 2.61% dropped

This is pretty good, a typical vehicle bus would be less than 50% load at 500kBaud

To improve on this, I can think of two things:
1) CAN mailboxes - how are these accessed individually? Does CAN.get_rx_buff() get the last received frame? Is there any way to get all frames from all mailboxes, then clear all mailboxes?
2) Event based code - Is there anyway to set up the code to trigger functions on receiving a new frame?


Code: [Select]

/*
V1.0 - 08-Feb-2014 - First version. Total Pass-through CAN gateway


V1.0 Test results as follows:
55.25% Busload - CAN0 Rx, CAN1 Tx 0.07% dropped, CAN1 Rx, CAN0 Tx 0.10% dropped
69.0%  Busload - CAN0 Rx, CAN1 Tx 0.04% dropped, CAN1 Rx, CAN0 Tx 0.06% dropped
91.5%  Busload - CAN0 Rx, CAN1 Tx 0.61% dropped, CAN1 Rx, CAN0 Tx 2.61% dropped //see code ordering to explain
*/

// Required libraries
#include "variant.h"
#include <due_can.h>
CAN_FRAME frameRx0, frameRx1; //Gateway frames received by CAN0 and CAN1
boolean extdCAN = false;

void setup() {

  // start serial port at 9600 bps for debug
  Serial.begin(9600);

  // Verify CAN0 and CAN1 initialization, set baudrate below:
  if (CAN.init(CAN_BPS_500K)) {
    Serial.println("CAN ONE initialization (sync) OK");
  }
  else {
    Serial.println("CAN ONE initialization (sync) ERROR");
  }

  if (CAN2.init(CAN_BPS_500K)) {
    Serial.println("CAN TWO initialization (sync) OK");
  }
  else {
    Serial.println("CAN TWO initialization (sync) ERROR");
  }

//Copied from echo example
  //By default there are 7 mailboxes for each device that are RX boxes
  //This sets each mailbox to have an open filter that will accept all frames
  int filter;
  for (int filter = 0; filter < 7; filter++) {
    CAN.setRXFilter(filter, 0, 0, extdCAN);
    CAN2.setRXFilter(filter, 0, 0, extdCAN);
  }
  LoopFuncn();
}

// Gateway frames between CAN 0 and CAN 1
static void LoopFuncn(void)
{
  Serial.print("Begin gateway loop");
  Serial.println();

  while (1 == 1) {
    CAN_FRAME frameRx0;
    CAN_FRAME frameRx1;

    if (CAN.rx_avail()) {
      CAN.get_rx_buff(frameRx0);
      CAN2.sendFrame(frameRx0);
    }
    if (CAN2.rx_avail()) {
      CAN2.get_rx_buff(frameRx1);
      CAN.sendFrame(frameRx1);
    }
  }
}

void loop()
{
}

Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Feb 11, 2014, 09:05 pm
Hello Mark and thanks for your contribution with the gateway.  Looks promising as a base for future applications.
Are you using the CAN analyzer from Microchip or  ADFweb? It would be great also if you show us pictures of your DUE and transceiver with the analyzer.

Regards!
Palliser
Title: Re: Building a CAN API for Arduino DUE
Post by: MarkOne on Feb 12, 2014, 09:33 pm
Hi Palliser,
Canalyzer is a Vector product. I'm afraid I was borrowing the Canalyser hardware so can't take any pics now.

Has anyone got any tips on either of my two questions?

Quote

To improve on this, I can think of two things:
1) CAN mailboxes - how are these accessed individually? Does CAN.get_rx_buff() get the last received frame? Is there any way to get all frames from all mailboxes, then clear all mailboxes?
2) Event based code - Is there anyway to set up the code to trigger functions on receiving a new frame?
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Feb 12, 2014, 09:45 pm
Sorry Mark, I should have answered those questions before.

1. CAN.get_rx_buff() grabs the first canbus frame in the receive buffer. The receive buffer is filled as frames come in to the RX mailboxes. There is currently no way to easily figure out which mailbox the frame came from.  The mailboxes are automatically cleared as soon as their frame is placed into the receive buffer. If a frame is being dropped then one of these things happened: all mailboxes that could accept the given frame were still full and the frame could not be received or the software did something dumb and lost the frame. The first possibility shouldn't actually drop the frame because canbus resends until somebody acks the frame. So, the only way it would be "dropped" at the Due is if the Due could not accept it but something else on the bus did set acknowledge on the frame. The second possibility could be a race condition in the interrupt code. It might be harder to find.
2. Not currently. The frames are automatically stuck into the receive buffer for you to check later on. There isn't a way to register a callback right now.

I have a kvaser leaf light so I might be able to do some testing to see if I can get the Due to drop frames. I know it does sometimes because I've tried to capture a firmware update between an ECU programmer and a motor controller and the Due could not keep up with the traffic. The Kvaser leaf does.
Title: Re: Building a CAN API for Arduino DUE
Post by: AlexHighTower on Feb 13, 2014, 11:13 am
Hello,

can I use SN65HVD230 based can boards on arduino nano/pro micro?
Title: Re: Building a CAN API for Arduino DUE
Post by: pjrc on Feb 13, 2014, 03:47 pm

can I use SN65HVD230 based can boards on arduino nano/pro micro?


No, not in any way that might actually work.  Arduino Nano and Sparkfun's Pro Micro do not have CAN bus support, so connecting only a transceiver chip would be pointless.
Title: Re: Building a CAN API for Arduino DUE
Post by: AlexHighTower on Feb 13, 2014, 05:58 pm

No, not in any way that might actually work.  Arduino Nano and Sparkfun's Pro Micro do not have CAN bus support, so connecting only a transceiver chip would be pointless.

not good for me....
can you prove list of arduino which has CAN bus support? (DUE comparable)
thanks in advance.
Title: Re: Building a CAN API for Arduino DUE
Post by: pjrc on Feb 13, 2014, 06:39 pm

can you prove list of arduino which has CAN bus support? (DUE comparable)


I believe Due is the only official Arduino board that has CAN bus.  Clones of Due, like Udoo and DigiX, should have CAN bus, assuming they've copied Due closely.

On 3rd party boards, ChipKit Max32 has dual CAN bus.  As far as I know, it's currently the only other board that attempts some form of Arduino compatibility and has a published CAN library.  I looked at their library some time ago, and my initial impression was the API was even more complex and convoluted that the older version of Due's CAN library!

Teensy 3.1 has a single CAN bus, but there is currently no CAN library for Teensy 3.1 (full disclosure: I'm the creator of Teensy).  Some Maple boards might have CAN bus, but as far as I know, no easy-to-use libraries are available and there may be interrupt conflicts with USB.  I've seen conversation about people using Keil's example code.
Title: Re: Building a CAN API for Arduino DUE
Post by: MarkOne on Feb 15, 2014, 08:33 pm
Thanks Collin,

Are you sure about this statement (below). I've never heard of this and I don't know how it could work. I know that diagnostic CAN devices work very differently to normal CAN communication around a vehicle - is this what you mean?
A diagnostic device will send an incomplete (no data) frame that the target device (vehicle ECU, body control module etc) should complete and reply with. Under normal circumstances however, vehicle modules communicate by sending repeated frames at a (nearly) constant rate - for instance, engine speed from ECU to Instrument panel, Traction control torque requests from ABS module to ECU etc etc. The frames are 'broadcast' on the network and I don't think there is anything in the physical layer that could confirm frame receipt.


....The first possibility shouldn't actually drop the frame because canbus resends until somebody acks the frame. ....


Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Feb 16, 2014, 01:26 am
Yes, I am sure. Here is the reasonably quick version:

Canbus is transferred such that bits are transferred by either leaving the bus alone (recessive state) or pulling H and L lines apart (dominant state). This is actually one of the reasons for the terminating resistors - they pull H and L together when nothing is asserting the dominant state. So, recessive bits are transferred when no one is doing anything to the bus and dominant bits are transferred when at least one device is asserting dominance on the bus. Back to the point at hand - ack bit. Every frame has a single bit designated as the acknowledge bit - it is basically the last bit transferred. This bit is transferred as recessive by the transmitting device. If another device properly receives the frame (has properly gotten every bit before the ack bit) then it will assert dominance on the bus and the bit will switch to dominant. The transmitting device can detect this and will know that the frame was received. If nobody sets ACK then the transmitting node will normally try to resend. It has the option of never resending, trying to resend a couple of times, or resend forever.  You can see this behavior very easily. Put an oscilloscope on an isolated device (still use a terminating resistor) and try to send a frame. You'll see the oscilloscope light up constantly without ceasing as the device tries to resend the same frame over and over looking for acknowledgement.

Thus, usually frames won't drop on the network because the transmitting node should automatically resend. The catch is that you can turn this behavior off so there isn't a guarantee that the transmitting node is attempting resends even though it could.


Thanks Collin,

Are you sure about this statement (below). I've never heard of this and I don't know how it could work. I know that diagnostic CAN devices work very differently to normal CAN communication around a vehicle - is this what you mean?
A diagnostic device will send an incomplete (no data) frame that the target device (vehicle ECU, body control module etc) should complete and reply with. Under normal circumstances however, vehicle modules communicate by sending repeated frames at a (nearly) constant rate - for instance, engine speed from ECU to Instrument panel, Traction control torque requests from ABS module to ECU etc etc. The frames are 'broadcast' on the network and I don't think there is anything in the physical layer that could confirm frame receipt.


....The first possibility shouldn't actually drop the frame because canbus resends until somebody acks the frame. ....

Title: Re: Building a CAN API for Arduino DUE
Post by: MarkOne on Feb 18, 2014, 10:23 pm
Thanks Colin, I'll look out for that.
Title: Re: Building a CAN API for Arduino DUE
Post by: MarkOne on Feb 18, 2014, 11:17 pm

1. CAN.get_rx_buff() grabs the first canbus frame in the receive buffer. The receive buffer is filled as frames come in to the RX mailboxes. There is currently no way to easily figure out which mailbox the frame came from.  The mailboxes are automatically cleared as soon as their frame is placed into the receive buffer. 


can I use CAN.mailbox_read() to get a frame from a specific mailbox? I can't work out the syntax CAN2.mailbox_read(0,frameRx0);

won't compile:

" error: no matching function for call to 'CANRaw::mailbox_read(int, CAN_FRAME&)'
C:\Program Files (x86)\Arduino\hardware\arduino\sam\libraries\due_can/due_can.h:236: note: candidates are: uint32_t CANRaw::mailbox_read(uint8_t, volatile CAN_FRAME*) "

Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Feb 19, 2014, 05:31 pm

can I use CAN.mailbox_read() to get a frame from a specific mailbox? I can't work out the syntax CAN2.mailbox_read(0,frameRx0);

won't compile:

" error: no matching function for call to 'CANRaw::mailbox_read(int, CAN_FRAME&)'
C:\Program Files (x86)\Arduino\hardware\arduino\sam\libraries\due_can/due_can.h:236: note: candidates are: uint32_t CANRaw::mailbox_read(uint8_t, volatile CAN_FRAME*) "


Yes, you can directly call mailbox_read in order to grab frames from any mailbox. However, the library automatically sets itself up for interrupt driven reception so you will have to disable that or you'll never have anything in mailboxes (the interrupt will snag it first). Clarification: Interrupts are not enabled for RX until you use SetRXFilter to create a filter. This sets up the interrupt for the mailbox that the filter is placed on. So, you can avoid this function and directly use mailbox_set_accept_mask and mailbox_set_id or you can call disable_interrupt(getMailboxIer(mailboxnum));

Then, the call to mailbox_read takes the mailbox number (0-7) and a pointer to the structure to fill. So, you'd probably want to make the call like this: CAN.mailbox_read(0, &myStruct);
Note that the function call expects the structure to already be in memory - it doesn't allocate one. So, your best bet is to allocate the structure like this: CAN_FRAME myStruct;
Title: Re: Building a CAN API for Arduino DUE
Post by: pjrc on Feb 19, 2014, 06:15 pm
I know this might be nitpicking and it's probably far too late to change now, but...

A common convention for names in all caps and with underscores (eg, "CAN_FRAME") is for constants from #define.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Feb 19, 2014, 08:31 pm

I know this might be nitpicking and it's probably far too late to change now, but...

A common convention for names in all caps and with underscores (eg, "CAN_FRAME") is for constants from #define.


Yes, you're right. It's not a particularly great thing that the structure is named like a constant. I don't remember the how or why of how this happened. Maybe there was some insanely good reason but likely it is a mistake that never got corrected. I suppose in the interim I could create a typedef or #define to remap it to a more correct convention like CanFrame and then eventually make the actual switch.
Title: Re: Building a CAN API for Arduino DUE
Post by: 3400tZ on Feb 23, 2014, 02:01 am
Hi,

I've downloaded the library from https://github.com/collin80/due_can and trying to compile the "CAN_EchoTest" example with Arduino 1.5.6rc2 IDE without much luck. Here is the compile log, any idea ? How should this library be installed ?

Thank you!

Code: [Select]

sketch_feb22a.ino:7:21: error: variant.h: No such file or directory
In file included from C:\Users\Seb\Documents\Arduino\libraries\DueCAN/due_can.h:72,
                from sketch_feb22a.ino:8:
C:\Users\Seb\Documents\Arduino\libraries\DueCAN/sn65hvd234.h:34: error: 'uint32_t' does not name a type
C:\Users\Seb\Documents\Arduino\libraries\DueCAN/sn65hvd234.h:37: error: 'uint32_t' does not name a type
C:\Users\Seb\Documents\Arduino\libraries\DueCAN/sn65hvd234.h:39: error: 'uint32_t' does not name a type
C:\Users\Seb\Documents\Arduino\libraries\DueCAN/sn65hvd234.h:40: error: 'uint32_t' does not name a type
C:\Users\Seb\Documents\Arduino\libraries\DueCAN/sn65hvd234.h:41: error: 'uint32_t' does not name a type
C:\Users\Seb\Documents\Arduino\libraries\DueCAN/sn65hvd234.h:43: error: 'uint32_t' does not name a type
C:\Users\Seb\Documents\Arduino\libraries\DueCAN/sn65hvd234.h:44: error: 'uint32_t' does not name a type
C:\Users\Seb\Documents\Arduino\libraries\DueCAN/sn65hvd234.h:46: error: 'uint32_t' does not name a type
C:\Users\Seb\Documents\Arduino\libraries\DueCAN/sn65hvd234.h:47: error: 'uint32_t' does not name a type
C:\Users\Seb\Documents\Arduino\libraries\DueCAN/sn65hvd234.h:48: error: expected `)' before 'Rs'
In file included from sketch_feb22a.ino:8:
C:\Users\Seb\Documents\Arduino\libraries\DueCAN/due_can.h:76: error: 'uint8_t' does not name a type
C:\Users\Seb\Documents\Arduino\libraries\DueCAN/due_can.h:77: error: 'uint8_t' does not name a type
C:\Users\Seb\Documents\Arduino\libraries\DueCAN/due_can.h:131: error: 'uint64_t' does not name a type
C:\Users\Seb\Documents\Arduino\libraries\DueCAN/due_can.h:133: error: 'uint32_t' does not name a type
...
sketch_feb22a.ino: In function 'void setup()':
sketch_feb22a.ino:29: error: 'SerialUSB' was not declared in this scope
sketch_feb22a.ino: In function 'void loop()':
sketch_feb22a.ino:36: error: 'SerialUSB' was not declared in this scope
sketch_feb22a.ino:45: error: 'class CANRaw' has no member named 'init'
sketch_feb22a.ino:46: error: 'class CANRaw' has no member named 'init'
sketch_feb22a.ino:53: error: 'struct CAN_FRAME' has no member named 'id'
sketch_feb22a.ino:54: error: 'struct CAN_FRAME' has no member named 'length'
sketch_feb22a.ino:56: error: 'union BytesUnion' has no member named 'low'
sketch_feb22a.ino:58: error: 'union BytesUnion' has no member named 'high'
sketch_feb22a.ino:69: error: 'class CANRaw' has no member named 'get_rx_buff'
sketch_feb22a.ino:71: error: 'SerialUSB' was not declared in this scope
sketch_feb22a.ino:72: error: 'union BytesUnion' has no member named 'low'
sketch_feb22a.ino:73: error: 'union BytesUnion' has no member named 'high'
Title: Re: Building a CAN API for Arduino DUE
Post by: pjrc on Feb 23, 2014, 02:18 am
Did you select Arduino Due in the Tools > Boards menu?

Those errors look suspiciously like the kind that occur when compiling Due-specific code for an AVR-only board.
Title: Re: Building a CAN API for Arduino DUE
Post by: 3400tZ on Feb 23, 2014, 02:20 am
That was it. Thank you Paul!
Title: Re: Building a CAN API for Arduino DUE
Post by: MarkOne on Mar 03, 2014, 12:05 am

Yes, you can directly call mailbox_read in order to grab frames from any mailbox. However, the library automatically sets itself up for interrupt driven reception so you will have to disable that or you'll never have anything in mailboxes (the interrupt will snag it first). Clarification: Interrupts are not enabled for RX until you use SetRXFilter to create a filter. This sets up the interrupt for the mailbox that the filter is placed on. So, you can avoid this function and directly use mailbox_set_accept_mask and mailbox_set_id or you can call disable_interrupt(getMailboxIer(mailboxnum));

Then, the call to mailbox_read takes the mailbox number (0-7) and a pointer to the structure to fill. So, you'd probably want to make the call like this: CAN.mailbox_read(0, &myStruct);
Note that the function call expects the structure to already be in memory - it doesn't allocate one. So, your best bet is to allocate the structure like this: CAN_FRAME myStruct;


This worked well, thankyou. I'll post some code soon

I don't quite understand how Rx'd frames are distributing into the mailboxes yet, bit more experimentation to do there.

One crucial question for me: is there any quick way of determining if the frame in a mailbox has already been read? CAN.mailbox_read(0, &canframe) doesn't seem to clear the mailbox.

Thanks so much for the help, much appreciated
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Mar 04, 2014, 04:38 am

One crucial question for me: is there any quick way of determining if the frame in a mailbox has already been read? CAN.mailbox_read(0, &canframe) doesn't seem to clear the mailbox.


Hello MarkOne,

The mailbox_get_status function checks the status of the mailbox and it can be used in conjunction with a mailbox status register (CAN_MSR_MRDY) to determine whether a mailbox has received a data frame. For example, the mailbox 4 of the CAN controller 2 receives data when the following condition is TRUE:

Quote
CAN2.mailbox_get_status(4) & CAN_MSR_MRDY


I borrowed the above condition from the CANX_Handler of the original Atmel ASF CAN library.

Also, the function mailbox_int_handler will handle a mailbox interrupt but, I've not try that yet.

Regards, Palliser
Title: Re: Building a CAN API for Arduino DUE
Post by: jspobuk on Mar 07, 2014, 02:21 am
Finally finished my shield.  The through holes are to give access to the header pins in the middle of the board.  I think the next version will just have the pins go through.  The header sockets were a little annoying to solder.  Solder paste does amazing things with surface mount chips.  No more frustrating solder joint problems.   Now just waiting for the engine to arrive.  :D

Title: Re: Building a CAN API for Arduino DUE
Post by: noop on Mar 10, 2014, 01:34 pm
Love what you guys are doing here!
I have a very newbie question. I wonder if someone can show me how to filter for just the ECU?
It seems that what I am doing is not the right thing.

I want to read trouble codes and I keep getting lost somewhere.
I can TX 0x7DF with 03 parameter but RX is my downfall.

CAN.setRXFilter(filter, 0, 0, false); //gets way too much of everything

I try the following two lines and that's where I go wrong:

CAN.setRXFilter(filter, 0x7E8, 0, false);

CAN.setRXFilter(filter, 0, 0x7E8, false);

I just don't get how to set the filter.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Mar 10, 2014, 04:12 pm
setRXFilter has two forms:

int setRXFilter(id, mask, extended);

and

int setRXFilter(mailbox, id, mask, extended);

You seem to be using the second type that allows you to explicitly set which mailbox you are talking about.

In order to properly filter a frame you need to set reasonable values for both id and mask. The incoming frames are AND with the mask and then compared to the id. So, if you need to accept only frames with 0x7E8 then you can set the mask to 0x7FF which forces all frames to exactly match the id. Thus, your call would be:

CAN.setRXFilter(filter, 0x7E8, 0x7FF, false);
Title: Re: Building a CAN API for Arduino DUE
Post by: noop on Mar 10, 2014, 05:36 pm
Thank you for that answer. Filtering is little clearer for me now.

I will test this in a couple of days.
Title: Re: Building a CAN API for Arduino DUE
Post by: Mike M on Mar 12, 2014, 10:31 pm
Hello,

I'm trying to send CAN frames from one Due compatible board to another one using Collin80's library (https://github.com/collin80/due_can).  The sending part works fine and I can see byte traffic with a scope all the way up to the RX TX side of the transceiver on the receiving board.  However, rx_avail() never returns true on the receiving side.  I'm using the CAN_TrafficSnooper example sketch that should not filter any messages and print out everything to the serial console.

Any debugging hints or ideas about what might be going wrong?

As a side question: what is the
Code: [Select]
#define TEST1_CAN0_TX_PRIO       15
line in the example sketches?  I don't see it used anywhere.
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Mar 13, 2014, 01:12 am
Hello Mike,
Have you tried the two can modules (CAN0/CAN1) in the same DUE as initially intended in the examples? I tested the EchoTest, ExtendedPingPong and TrafficSnooper and they worked OK on the same original DUE (also with Taijiuino). I haven't tried with two DUEs but Collin's examples should work. It is just about to be careful with the interconnection of the modules. I.e. CAN0 on Due1 with CAN1 on Due2 or CAN1 on Due1 with CAN0 on Due2...or depending on your code, therefore, it would be good if you post your code if differs from the original Collin's examples.

Regards, Palliser   
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Mar 13, 2014, 03:38 am
I need to get the most obvious two points out of the way first:

1. Are you using some sort of CANBus shield which has transceivers on it? You really do need transceivers. If you do have a board then it would help to know if it was something you made or something you bought (and if so, which board did you buy?)

2. Are the canbus wires terminated? A lot of shields are going to have at least selectable termination but you need there to be 60 ohms between CANH and CANL. This should take the form of a 120 ohm resistor at each end of the bus.

Even with the proper transceivers you will not get a signal through between two devices without some termination. A lot of times you can get away with just one 120 or 100 ohm resistor on one end if the link is short. But, 60 ohms is proper.
Title: Re: Building a CAN API for Arduino DUE
Post by: Mike M on Mar 13, 2014, 04:27 pm
Thanks for the replies.

I've made two AT91SAM3X8E based Arduino-compatible boards working off the Due schematics.  I'm using two TI ISO1050 isolated CAN transceivers (datasheet (http://www.ti.com/lit/ds/symlink/iso1050.pdf)).  Since these transceivers need to be powered from both sides I have one board powering the transceiver of the other board.  I've attached a partial schematic below.

I do own an official Due.  I'll try wiring up transceivers from CAN0 to CAN1 on the Due and see if the PingPong example works.

UPDATE: The breadboarded version using two of the ISO1050 transceivers and one Arduino Due runs the CAN_EchoTest and CAN_ExtendedPingPong examples correctly.  I'll keep looking through my hardware, but let me know if there's something I could be missing.
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Mar 15, 2014, 02:51 am
Hello Mike,

It might be considered a note on page 4 of the TI ISO1050 specs doc:
http://www.ti.com/lit/ds/symlink/iso1050.pdf (http://www.ti.com/lit/ds/symlink/iso1050.pdf)

Quote
"TXD is very weakly internally pulled up to VCC1. An external pull up resistor should be used to make sure that TXD is biased to recessive (high) level to avoid issues on the bus if the microprocessor doesn't control the pin and TXD floats. TXD pullup strength and CAN bit timing require special consideration when the device is used with an open-drain TXD output on the microprocessor's CAN controller. An adequate external pullup resistor must be used to ensure that the TXD output of the microprocessor maintains adequate bit timing input to the input on the transceiver".
Title: Re: Building a CAN API for Arduino DUE
Post by: tbit on Mar 19, 2014, 03:01 am
Long overDUE and probably rushed, here is an OBD2 /free-running CAN library I've been picking away at.

I'll apologize in advance for:
-developing with the old due_can libraries Collin80 developed and not porting it over to the newer yet
- it's probably slow and I haven't taken full advantage of the HW (only using one CAN mailbox for example)
- any code conventions I probably don't follow, I did put as much green as a plane flight would allow for
- it's only been tested on one vehicle (see logs), toyota w/11-bit ID's

See the readme on github and the examples for OBD2 implementaitons and free-running CAN
https://github.com/togglebit/ArduinoDUE_OBD_FreeRunningCAN  (https://github.com/togglebit/ArduinoDUE_OBD_FreeRunningCAN)

Hope this moves the needle a bit. I should have some upcoming practical uses for this code that will force me to work on it a bit more.
-Dan
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Mar 21, 2014, 03:58 am
Hello Dan and thank you very much for share your OBD library for Arduino DUE. Nice job (an a lot of work). I think it is one of the first practical automotive applications based in the due_can library. It really moves the needle (up to 99 KPH!)
I have one question: did you use the pins 6 (CANH) and 14(CANL) in the OBD connectors like I showed in my reply #38?
http://forum.arduino.cc/index.php?PHPSESSID=6fjbgmq82640nklcdr2psed920&topic=131096.30 (http://forum.arduino.cc/index.php?PHPSESSID=6fjbgmq82640nklcdr2psed920&topic=131096.30)
Regards. -P

Title: Re: Building a CAN API for Arduino DUE
Post by: tbit on Mar 22, 2014, 12:48 pm
I have a pre-made OBD cable for CAN ( i think from obd2cables.com)....While i didn't check it would have to be 6 and 14 in order to work (CANH, CANL) .
Title: Re: [ now published on github ] Building a CAN API for Arduino DUE
Post by: OBD-II on Mar 26, 2014, 11:04 pm
answer to question.
Customers can also look on the box label for the 2-character Environment Category (E-Cat) Code. If it starts with "G" (G3, G4, etc), the box contains Green compatible parts (E-Cat Codes were added to boxes beginning on June 1st, 2004.





Thank you for the links and info.
The page with the transceiver chip has different ones.
Which one do I need and whats the difference between these ?
SN65HVD234D
SN65HVD234DG4  (G4 means super GREEN part, for sure, above may not be.. )   R suffix are reel parts.

Can you recommend a Arduino bread board start kit also ?


I don't have any idea why places like Mouser and Digikey do that... They're the same part. Sometimes they have funny stuff like that where you have the same part with very slightly different part numbers varying only in suffix. Perhaps there is a slight difference of some sort but I don't know what it is. Chances are either would work fine.

As for an Arduino bread board, I did recommend one. The schmartboard is a breadboard with 1.27mm spots for chips that fits on an arduino.
Title: Re: Building a CAN API for Arduino DUE
Post by: MorgKrig on Apr 02, 2014, 04:19 pm
I'd like to build up a CAN Receiver with the Arduino DUE just to receive data.
Is there anywhere a solution or a project like that?

Is the CAN API for the DUE rdy for use?

Thx for your help!
Title: Re: Building a CAN API for Arduino DUE
Post by: tbit on Apr 02, 2014, 05:16 pm
MorgKrig you may want check out my acquisition lib on github I have an rx example in there for free running 11bit messages. Could certainly use more testing.
Title: Re: Building a CAN API for Arduino DUE
Post by: OBD-II on Apr 02, 2014, 11:28 pm
hello, Dan.
i bought your can shield, it's put together now. what a NICE JOB Y0U did there, nice quality. Thanks !!
im doing the same , doing CAN-BUS monitoring on Jeep 2008 bus. (for diagnosing com issues,,etc) I have full scan tools but need
to see if there are errors say from BCM out bound with  switches turned, like blinkers, HL, or other critical functions.

I saw that one guy here, used a filter to stop the repeats. (from low priority task outbound commands)   
but first i want to get  this working below.
id like to do the loop back test.  port 1 to 2  loop jumper-ed. (first)
does your software library support that?
I see the arduino lib does, but will their (here) application ino, run with your installed lib?
i followed your on line instructions, as to putting your unzipped files. 
my plan is.
loop back , app.
then monitor , app.
then monitor with filters.

thanks again and with any hints. 


Title: Re: Building a CAN API for Arduino DUE
Post by: tbit on Apr 03, 2014, 03:04 am
Thank you for the kind words and the order! ( flattery will get you everywhere).  Here is a quick mod on the CANexample2 where port 0 is transmitting message ID 0x100 and port 1 is receiving  message ID 0x100.

This builds but i didn't test it, so let us know how it goes.To test: connect the buses (don't forget your termination!), load the code and watch the serial monitor. You should see the println value count upward and roll over. Disconnect the buses (or perhaps just pull termination) and the value should stop increasing. (if i don't have any bugs)

Hope this helps and thanks again!
Dan

Code: [Select]

#include <OBD2.h>
#include <DueTimer.h>

//create the CANport acqisition schedulers
cAcquireCAN CANport0(CAN_PORT_0);
cAcquireCAN CANport1(CAN_PORT_1);


/***** DEFINITIONS FOR RAW CAN FRAME *****/      
cCANFrame  RAW_CAN_Frame1;
cCANFrame  RAW_CAN_Frame2;

void setup()
{
//start serial port
Serial.begin(115200);

//debugging message for monitor to indicate CPU resets are occuring
Serial.println("System Reset");

//start CAN ports, set the baud rate here
CANport0.initialize(_500K);
CANport1.initialize(_500K);

//initialize the items needed to TX/RX raw CAN mesasges
RAW_CAN_Frame1.ID = 0x100;
RAW_CAN_Frame1.rate  = _1Hz_Rate;
 
RAW_CAN_Frame2.ID = 0x100;
RAW_CAN_Frame2.rate  = _1Hz_Rate;
   
//add our raw messages to the scheduler 1
CANport0.addMessage(&RAW_CAN_Frame1, TRANSMIT);
CANport1.addMessage(&RAW_CAN_Frame2, RECEIVE);


//set up the transmission/reception of messages to occur at 500Hz (2mS) timer interrupt
Timer3.attachInterrupt(CAN_RxTx).setFrequency(500).start();

//output pin that can be used for debugging purposes
pinMode(13, OUTPUT);      
}


UINT8 i;

void loop()
{
// this single byte should continously change in our raw CAN transmissions
RAW_CAN_Frame1.U.b[0] = i++;

//print byte from the CAN message we are receiving
Serial.println(RAW_CAN_Frame2.U.b[0],HEX);

//pass control to other task
delay(1000);
}

//this is our timer interrupt handler, called at XmS interval
void CAN_RxTx()
{
//run CAN acquisition schedulers on both ports including OBD and RAW CAN mesages (RX/TX)
CANport0.run(TIMER_2mS);
CANport1.run(TIMER_2mS);
}
Title: Re: Building a CAN API for Arduino DUE
Post by: OBD-II on Apr 03, 2014, 05:09 pm
thanks so very much.
and the code.  ill report back soon.   
I will add a web page showing , my jeep data. 


i will check all that, out... thanks for the test code, very very much.
Title: Re: Building a CAN API for Arduino DUE
Post by: yazen on Apr 05, 2014, 01:04 pm
I am using Arduino Due with external transceiver MCP2551. It worked and I can send and receive messages from the node, but when I put output.rtr = 0 to send a remote request message it doesn't work.
here is my code:

#include <due_can.h>
#include <sn65hvd234.h>
int ch=0;
int id;
void setup() {
  Serial.begin(9600);
CAN.init(CAN_BPS_1000K);
int filter;
//standard
  for (int filter = 3; filter < 7; filter++) {
   CAN.setRXFilter(filter, 0, 0, false);
  }
}

void loop() {
  CAN_FRAME incoming;
  if (CAN.rx_avail()) {
    CAN.get_rx_buff(incoming);
   printFrame(incoming);
  }
  if(Serial.available()>0){
    id=Serial.parseInt();
CAN_FRAME output;
output.id = id;
output.length = 8;
output.data.low = 0x0005f000;
output.data.high = 0x12345678;
output.extended = 0;
output.rtr = 1;
  CAN.sendFrame(output);
    }
}

void printFrame(CAN_FRAME &frame) {
   Serial.print("ID: 0x");
   Serial.print(frame.id, HEX);
   Serial.print(" Len: ");
   Serial.print(frame.length);
   Serial.print(" Data: 0x");
   for (int count = 0; count < frame.length; count++) {
       Serial.print(frame.data.bytes[count], HEX);
       Serial.print(" ");
   }
   Serial.print("\r\n");
}

I have tried putting output.rtr = 1, it gives me the same thing.
any solution for this.
Title: Re: Building a CAN API for Arduino DUE
Post by: OBD-II on Apr 05, 2014, 06:32 pm
hello dan,  (per your echo code above, and thanks)
i get this error. at compile time.
quote:
Quote
Arduino: 1.5.6-r2 (Windows 7), Board: "Arduino Due (Programming Port)"

Build options changed, rebuilding all

K:\Program Files (x86)\Arduino/hardware/tools/g++_arm_none_eabi/bin/arm-none-eabi-g++ -c -g -Os -w -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -Dprintf=iprintf -mcpu=cortex-m3 -DF_CPU=84000000L -DARDUINO=156 -DARDUINO_SAM_DUE -DARDUINO_ARCH_SAM -D__SAM3X8E__ -mthumb -DUSB_VID=0x2341 -DUSB_PID=0x003e -DUSBCON -DUSB_MANUFACTURER="Unknown" -DUSB_PRODUCT="Arduino Due" -IK:\Program Files (x86)\Arduino\hardware\arduino\sam\system/libsam -IK:\Program Files (x86)\Arduino\hardware\arduino\sam\system/CMSIS/CMSIS/Include/ -IK:\Program Files (x86)\Arduino\hardware\arduino\sam\system/CMSIS/Device/ATMEL/ -IK:\Program Files (x86)\Arduino\hardware\arduino\sam\cores\arduino -IK:\Program Files (x86)\Arduino\hardware\arduino\sam\variants\arduino_due_x C:\Users\xxxxx\AppData\Local\Temp\build5265564629386830732.tmp\echotest.cpp -o C:\Users\xxxxx\AppData\Local\Temp\build5265564629386830732.tmp\echotest.cpp.o

In file included from K:\Program Files (x86)\Arduino\hardware\arduino\sam\cores\arduino/OBD2.h:17,
                 from echotest.ino:1:
K:\Program Files (x86)\Arduino\hardware\arduino\sam\cores\arduino/due_can.h:72: fatal error: sn65hvd234.h: No such file or directory
compilation terminated.


i look in that folder for due_can.h and it's there.
my K: drives is HDD, my C: drive is SSD, i never install apps to the SDD.

i guess im missing this,  but where it and what folder does it belong? 
sn65hvd234.h

thanks for any help. !!!
Title: Re: Building a CAN API for Arduino DUE
Post by: tbit on Apr 06, 2014, 04:11 am
sorry left these of the first commit, now included on github. They go with the rest of the files in "C:\Program Files (x86)\Arduino\hardware\arduino\sam\cores\arduino"
Title: Re: Building a CAN API for Arduino DUE
Post by: OBD-II on Apr 06, 2014, 03:30 pm
thank you Dan !
ok that is done. and good, but ..... now....
now missing duetimer.h

what ive done so far.  (i dont know prerequisites, at all. )
installed newest IDE.
then installed your library parts , dans CAN  zip from your  github , and followed, your instructions page.
i have no other extra libs. installed.
does your lib, build off base IDE package or do i need other libs installed.?
thanks for the help , greatly appreciated.

does your code, Dan , build off this? (or part?)   
https://github.com/arduino/Arduino/tree/can
Title: Re: Building a CAN API for Arduino DUE
Post by: tbit on Apr 07, 2014, 02:37 am
Sorry, these are the files from ivan seidel's DUE Timer library. I put these up, they need to also go into:
C:\Program Files (x86)\Arduino\hardware\arduino\sam\cores\arduino
Thanks for testing all of this! sorry for my assumptions and omissions.
Title: Re: Building a CAN API for Arduino DUE
Post by: yazen on Apr 07, 2014, 12:13 pm
Has anyone worked with the remote request using the due_can library.
according to the library you have to put the frame name with (.rtr) and set it to one for example:

output.rtr = 1;

I have tried this but it didn't work.
should I use another library or is there an alternative solution.
Title: Re: Building a CAN API for Arduino DUE
Post by: OBD-II on Apr 07, 2014, 01:15 pm
thanks dan
i know how hard it is to keep track of what one has in ones library, after a long time and many projects.
i started with a clean install, so im a good tester in this case.
thanks again.

i followed the instructions from mr. ivan
modify the Folder name to "DueTimer", done. all works.
and it compiles now,!,  and uploads. ok.
now to run tests...
loop test passes,
the counts roll from 00 to FF(hex)  Thanks Dan.
[font=Verdana]hoo-hooh[/font]

next stop, a traffic snooper, does anyone have an example for snoop monitor, no transmissons. ( car can snoop)

(http://www.fixkick.com/hacking/candue-loop1w.JPG)


Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Apr 08, 2014, 03:15 am
My library (https://github.com/collin80/due_can) has a traffic snooper example. The library you're using I believe was based off of my version of the DueCan library so you might be able to retrofit the example to your library. The library you found a while back in the official Arduino tree is an older copy of what my library is based off of. I don't remember if it was an earlier version of my library or what I based my code on. Either way it's fairly old. But, the CAN_TrafficSnooper example from my current library should point you in the right direction.
Title: Re: Building a CAN API for Arduino DUE
Post by: OBD-II on Apr 08, 2014, 03:23 pm
thanks , C80
ill give that as spin.


Title: Re: Building a CAN API for Arduino DUE
Post by: MorgKrig on Apr 16, 2014, 02:07 pm
Somebody cann tell me if there is a Way to use the CAN on the DUE?
I must works well and it must work guaranteed. :-)

Would be great if someone with Arduino/CAN/DUE experience can talk to me in a pm. Thx a lot.
Title: Re: Building a CAN API for Arduino DUE
Post by: pjrc on Apr 16, 2014, 02:13 pm

I must works well and it must work guaranteed. :-)


Do you realize you're asking for a guarantee on free software written by unpaid volunteers?

What if it doesn't work?  Will you need a refund?
Title: Re: Building a CAN API for Arduino DUE
Post by: OBD-II on Apr 16, 2014, 03:34 pm
Morg.
yes it will work , the hardware works.
the software is being developed here. if you read all posts, you can see that (no pun)
there is no guarantee, you are the engineer you do that.  (you build it, test it and debug it, )
this IDE, is just that.
why not post your Goal?, to do what? with what other system? (mine is CAR CAN)?
Post your goal.
Post your code. if it fails
State what works and what don't with examples.
does the loop back test work? ;like mine above.?
many here are pleased to help you.   



Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Apr 16, 2014, 04:03 pm

Somebody cann tell me if there is a Way to use the CAN on the DUE?
I must works well and it must work guaranteed. :-)

Would be great if someone with Arduino/CAN/DUE experience can talk to me in a pm. Thx a lot.


Yes, there is a way. You're in the right thread for that. It does work, the library has been used in several projects including an electric vehicle ECU that has sales in excess of 30 at this point (yes, that's still small but large enough that you can be sure we've tested it and there are many other people using it on a regular basis). Will I guarantee it? Of course not. It's software and its free. If it works for you, great! If it kills your cat, runs over your grandma, slaps your wife, and gives you diarrhea then I apologize.

FWIW, I recently posted a link to my version of the library. There is also another version which I believe is a bit more specialized to work in automotive uses. Pick your poison and we'll try to help.
Title: Re: Building a CAN API for Arduino DUE
Post by: ArtificialDemon on Apr 27, 2014, 05:15 pm
Hey guys,

I bought the DUE somewhere in 2013, since it supports CAN. Then I found out it also needed a tranceiver and the libraries were not finished at all.
Therefore I´ve been monitoring this thread for a long time and when the library/libraries finally were kinda finished, I started to gather the stuff I needed to build the hardware.
Firstly, thanks for all the effort! I really appreciate it and without this effort my initial plans would have been destroyed.
In short, I'm connecting a TFT monitor and CAN to the same DUE in order to communicate between my car's ECU and the TFT. No OBD stuff here. I only need CAN for stable communication since I should be able to control the messages sent by the ECU (non OEM, hence no OBD)

Some bumps I encountered is the fact that this thread lacks a bit of overview. It's not immediately clear where to find the circuit that needs to be built and with multiple libraries, several versions etcetera I was not quite sure if I actually have the most recent and best fitting library for my purpose.
Don't take this in a negative way ;) It's just meant as some feedback from a "noob".

Today I built the circuit on my breadboard and used Collin's library and was very surprised to see actually something happening in the serial monitor.

-Example 1 and 2 do show what message has been received, but it does not print which message was sent.
-At example 3 I got weird ASCII symbols in my serial monitor, and a quick search forced me to modify the baud rate to 9600; apparently when this is not the same for the monitor and the arduino, it puts out weird symbols.
Changing the baud rate fixed it.
-Example 4 (ping/pong); also changed the baud rate, but I found out that the communication stops after a few times.
Can't really figure out why this happens. This is the output on my monitor:
Code: [Select]
S: 5002 R: 5001
S: 10004 R: 10003
S: 15006 R: 15005
S: 20008 R: 20007
S: 25010 R: 25009
S: 30012 R: 30011
S: 35014 R: 35013
S: 40016 R: 40015

After that, it just stops communicating. Any ideas?

Transmitting works, so my next challenge is understanding what actually happens. I'm trying to understand everything by the documentation mentioned in this thread, but for now some things are not completely clear.
I probably will ask a lot of dumb questions in the near future, so forgive me for that ;)

Thanks again!

edit: first question already
I've left out the terminator jumpers. I assume these jumpers are necessary to leave out the terminator resistors (120ohm).
Is this correct? When should I make use of this? Is it when I have 3 units on the same network? Only two should be terminated, right? So in my case I would use one terminated arduino channel (CAN0), and terminate at the ECU side. If I would connect another Arduino with CAN, or would also make use of CAN1, I should not terminate this connection with the resistor, is this correct?

Title: Re: Building a CAN API for Arduino DUE
Post by: OBD-II on Apr 27, 2014, 06:37 pm
It forms a transmission line.
http://www.ti.com/lit/an/slla270/slla270.pdf

at the ends.
and the can-bus is a twister, pair, which has  a characteristic impedance of 120 ohms
if the line is not terminated ,you get reflections.  (noise)
there is also a split method.
the lines are balanced or they  would never work this fast.
in fact they are called, balanced transmission lines, in electronics.

the DUE has no drivers, you must add the drivers.    the current is very high, way more that a processor can muster.
if you read all posts, you see everyone, making up their own drivers.

http://www.ti.com/general/docs/datasheetdiagram.tsp?genericPartNumber=SN65HVD233&diagramId=SLLS557F

the driver can source 50 milliamp min.  nothing trivial that.
http://www.ti.com/lit/ds/symlink/sn65hvd233.pdf






Title: Re: Building a CAN API for Arduino DUE
Post by: ArtificialDemon on Apr 27, 2014, 10:13 pm

It forms a transmission line.
http://www.ti.com/lit/an/slla270/slla270.pdf

at the ends.
and the can-bus is a twister, pair, which has  a characteristic impedance of 120 ohms
if the line is not terminated ,you get reflections.  (noise)
there is also a split method.
the lines are balanced or they  would never work this fast.
in fact they are called, balanced transmission lines, in electronics.

the DUE has no drivers, you must add the drivers.    the current is very high, way more that a processor can muster.
if you read all posts, you see everyone, making up their own drivers.

http://www.ti.com/general/docs/datasheetdiagram.tsp?genericPartNumber=SN65HVD233&diagramId=SLLS557F

the driver can source 50 milliamp min.  nothing trivial that.
http://www.ti.com/lit/ds/symlink/sn65hvd233.pdf


I understand. The functional diagram learns me that in my whole CAN system, I need exactly 2 terminator resistors. No more, no less.
So, to get back at my original question, the jumpers need to be removed if I don't want to terminate at the DUE, but let's say somewhere else.

Your mentioning of "twisted pair" triggers another question.. I was planning to use RJ45 connectors on my application, where normally I see DB9(ish) connections. Any particular reasons not to use RJ45 with regular twisted pair (ethernet) cables?


Somehow the problem with CAN Sample 4 magically solved itself when I restart my monitor.
Thanks for the documents btw!

edit: one of the documents answered my question already.
Quote
These include the 9-pin DSUB shown in Figure 11 , Multipole, RJ10, RJ45, M12, the 5-pin mini-style in Figure 12 and more micro-style connectors in the CiA specification document DR 303-1, V1-3

So the RJ45 should work fine.
Title: Re: Building a CAN API for Arduino DUE
Post by: OBD-II on Apr 28, 2014, 05:19 pm
the jumpers are on pro grade driver boards, like mine, they are PCB layed out nice , with jumpers for term.

the DUE has no CAN drivers. so has no term jumpers, it's just the CAN raw, lines. 3v logic, lines. not CAN drive.
the balanced transmission lines, are all different. (ever work with Computer SCSI HDD systems, they too are balanced and terminated and will fail if done wrong, big time)

it depends on the wire type, and insulation, type. the 120 ohms is just the typical impedance of twisted pair.
and COAX cable ( like my instructor said) is really like infinite turns of twisted pair, see?   think about that, see that?
the equations for  the wires is easy, but id no worry it,   (wire center to center distance being major player)

on my car , im jacking into the radio harness CAN-Bus. (as others, taught me) Google "jeep Can-bus"
I'm attempting to learn all can-codes for all majors, in car. for sure all switches, turned on, etc.
on cars, today, lets say , you turn on the blinkers.?
did the switch fail, or did the can-bus command fail to go out or id it not bet repeated to the lamp controls. or what?
that is my goal is to be able to see all  that., and diagnose that.

good luck with you project,
im very slow do to home projects, to do before air temps hit 100F. 

what TFT did you  use, that is my next step is built in display, I might just use and old VGA screen.(flat)
Title: Re: Building a CAN API for Arduino DUE
Post by: ArtificialDemon on May 11, 2014, 11:26 pm
must keep this thread alive...

I'm running into problems since I don't have any documentation and have to understand the code by reading it. This is not fail safe and takes a lot of time.

My ECU is sending messages over CAN. I'm not sure what they look like, but I see voltages changing compared to when it is not broadcasting CAN messages. What I know is:
- My arduino can talk to itself. As mentioned, the examples worked fine.
- I disconnected CAN0 and connected that side to my ECU. So CAN1 is connected (correctly) to my ECU. I also used a common ground.
- I'm using Sample_1 as base, in which I removed the "sending stuff" from CAN0. In short.. CAN1 mailbox 0 has ID 7 and has a mask of 0x7FF (2047 in dec).

The ECU uses:
- 11 bit header.
- some message ID in the message
- Not sure if it has a specific address in the header, since it is broadcasting.

Should I change anything  to deal with the 11-bit header? I vaguely recall something about a bit in the header which contains information about header length, so it might automatically be resolved?

How does the mask work? Can I configure the code to accept any message? I'm sorry if this has already been answered somewhere...
Will there be some documentation at some point to give additional information on the examples? Like "we set this to xxxx because ..." "change this to xxxxx if you want yyyy"

Everything is going pretty well though... :) haven't wrecked anything yet. I'm looking forward to my euforia when I actually see a reception in my serial monitor  8)

edit: ok I received something :P
I changed the bitrate to 500Mb/sec, on which the ecu apparently sends.
Still confused about message Id's and masks, so please answer those questions if you're able... but at least I got something and I'm really really really happy.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on May 12, 2014, 02:56 am
As you found, the first order of business is to set the CANbus speed to the same speed that everything else on the bus was set to. If you don't do this you'll mess up all canbus comms and bad thing might happen. A common side effect is that your dashboard either quits working or goes nuts.

Now, the next issue is canbus mask and filter. You should use the most recent canbus library of mine and find the CAN_TrafficSnooper sketch. It sets the hardware to accept all frames and displays when it sees. This illustrates something about the mask and filter but I'll quickly go over it here.

Let's say a frame with id 0x211 comes in to the Due. The first thing that happens is that the Due hardware takes that ID and does a bitwise AND with the mask. So, let's say you have a receive mailbox with a mask of 0x7F0. 0x211 AND 0x7F0 is 0x210. The next thing is that it compares the new value with the value in the filter. If the filter value is 0x210 then they match and the frame is accepted. It's that simple. This same thing happens for all receive mailboxes. AND with mask, compare to filter. So, if you set your mask and filter to zero then it will always match because anything AND 0 is 0. You can use this capability to filter based on ranges. If you set a mask of 0x7F0 as in my example then it will accept any frame that matches the filter where the bottom nibble is ignored for matching purposes. This is quite common.

But, you're right, some documentation should probably be written.
Title: Re: Building a CAN API for Arduino DUE
Post by: ArtificialDemon on May 12, 2014, 10:14 pm
Downloaded everything from:
https://github.com/collin80/due_can

That's the correct version, right?
Thanks for the clear repsonse! I'm getting some data when I set  the mask to 0x5F1, but it appears to read the values from 0x5F3. Perhaps this is due to my misunderstanding of the mask or something.

Can you tell me what the boolan "ext" does in method mailbox_set_accept_mask (edit: I figured it's extended headers..)
I'll try to see what happens when I accept everything. Since my ECU works with offsets from a base identifier, I won't be able to know what it actually means.

Is my assumption correct that I should initialize a mailbox for every "offset"? So the first 8 bytes sent by my ECU should be at the base identifier, these will be dropped in mailbox 0. The next 8 bytes in mailbox 1 etc etc... is that the way I should implement it?

edit2:
oh boy your traffic  snooper works awesome. This is perfect for debugging and the code is easy to understand. Great job :)
Title: Re: Building a CAN API for Arduino DUE
Post by: ArtificialDemon on May 17, 2014, 11:16 am
Hey guys,

I only need one CAN channel, and need the pins used for CAN1 for something else. The pins DAC0 and D53 won't be used for CAN.
Do I have to change the header files?

The header in the library used contains the following code:
Code: [Select]
#ifndef PINS_CAN0
static const uint8_t CAN1RX = 88;
static const uint8_t CAN1TX = 89;

// CAN0
#define PINS_CAN0            (90u)
// CAN1
#define PINS_CAN1            (91u)
#define ARDUINO152
#endif


This is unclear to me. I know which physical pins are used, but can't figure out how they correspond to the numbers I see.
I use the diagram in the following thread for reference:
http://forum.arduino.cc/index.php?PHPSESSID=f6jl678uej29utsg9oarq26983&topic=132130.0

88 and 89 are used for CAN1? How do these relate to the diagram in the above thread?

Since I'm using other libraries that use the pins, the pins will be referenced more than once. This will probably cause no problems when the code is not called, but I'd like things to be safe.
So if I won't initialize CAN1, there should be no problem, correct?

Could arduino/compiler notice these 'conflicts' and pose problems?

tl;dr what's the safest way to use one CAN channel and use the pins used for CAN1 for other functionalities.
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on May 17, 2014, 02:27 pm
Hello ArtificialDemon,

You shouldn't have any problem using the CAN1 pins for other purposes ( like read Analog channel 0, etc...).
Notice that the CAN1 pins were added later to the variant files.
https://github.com/arduino/Arduino/commit/fabc658a942de659d44f22f196701d04b857f094 (https://github.com/arduino/Arduino/commit/fabc658a942de659d44f22f196701d04b857f094)

Those pin numbers are 'consecutive numbers' of the Due PinDescription. 88 and 89 correspond to the CAN1 port itself. The other numbers (90 and 91) correspond to a COMBO which is an attribute telling the higher level layer that the description is multi-pin oriented. Notice that that was also done for the USART, TWI, SPI, etc.. in order to make easier the init understanding. You will surely see new number in the future every time the Arduino team add new libraries/functionality to Due like for example the EMAC peripheral, etc. Regards,

p
Title: Re: Building a CAN API for Arduino DUE
Post by: tbit on May 18, 2014, 03:12 am
ArtD- if what you are looking to do is just talk to an ECU with 11 bit ID, you could probably use my free running library that sits on top of collins original code yet below the obd layer, it handles the filter settings for you. Only takes a few lines to implement, I think gone has done some work with it and posted feedback.Let me know if I can help. As for a working hardware I put my schematics up on my page as well...which is really just based upon information posted in this thread. Good luck!
Title: Re: Building a CAN API for Arduino DUE
Post by: christinaoy on May 18, 2014, 08:33 pm
Hello everyone, I am new to the Arduino Due . I have brought the CAN transceiver mcp2551. I don't know if I can use the mcp2551 to connect Arduino Due. And can I use the Due_can library code? I see that includes sn65hvd234.h ,therefore I am not sure if it is suitable for the mcp2551.

Thanks in advance.
christina
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on May 18, 2014, 09:50 pm
It actually isn't that good of an idea to try to use the MCP2551. That chip requires 5V while the Due is 3.3V. This difference will make using the chip difficult. You really do want the proper transceiver or another 3.3V compatible transceiver. There are lots of choices out there. The transceiver you use isn't really that important but you do need to match it to the proper voltage level.
Title: Re: Building a CAN API for Arduino DUE
Post by: christinaoy on May 18, 2014, 10:28 pm
thank you ,Collin80 .

It's no wonder  that the Can initial  is completed, but the message can not sent or received. The key problem is the  voltage difference between the transceiver and Due. I will solve it and hope it will work later.
Title: Re: Building a CAN API for Arduino DUE
Post by: onion on May 19, 2014, 12:47 pm

Hello everyone, I am new to the Arduino Due . I have brought the CAN transceiver mcp2551. I don't know if I can use the mcp2551 to

The MCP2562 looks to be a much nicer choice, voltages for IO and CAN are separete so you should be able to plug it into your Due without problems. I'm going to order some for my Due+CAN project.
Title: Re: Building a CAN API for Arduino DUE
Post by: djkrugger on May 26, 2014, 05:37 am
For those looking for an easy solution for the transceiver, i'm using a pair of SN65HVD230 eveluation boards (Amazon) (http://www.amazon.com/Communication-SN65HVD230-connecting-Transceiver-Development/dp/B00DU4WSVW/ref=sr_1_1?ie=UTF8&qid=1401074293&sr=8-1&keywords=SN65HVD230) probably you can find them cheaper.

  I could get some commands to be recognized by some car units in the bench, but i need to send timed messages, is there any timing function in the library itself or i just need to stick to "millis"?
Title: Re: Building a CAN API for Arduino DUE
Post by: McNeight on Jun 08, 2014, 09:13 am
Is anyone still working on integrating the Due CAN libraries into the mainstream Arduino release?

I've been working on a "unified" CAN library that works for both the SAM3X8E and MCP2515. The library syntax follows Arduino conventions (begin(), available(), read(), write() and end()), and right now I've gotten it down to a pair of #if defined() statements needed in the main .ino to separate the two hardware implementations.

I'm wondering if it's possible to combine the two hardware implementations even further to reduce confusion to new CAN users? Possibly having just a single CAN() class that covers multiple hardware implementations?

Thanks!

-Neil
Title: Re: Building a CAN API for Arduino DUE
Post by: Jasoncha on Jun 16, 2014, 07:06 pm
Is there any working samples that simply receives data from ecu such as  motec?
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Jun 16, 2014, 09:47 pm
Hello Jason,
You can try the CAN_TrafficSnooper example from Collin's library. Regards.
https://github.com/collin80/due_can (https://github.com/collin80/due_can)

p
Title: Re: Building a CAN API for Arduino DUE
Post by: rtfh on Jun 16, 2014, 11:45 pm
So I have a CAN network already established with multiple nodes fully functional. I have just aquired a Due and would like be able to use the Due to see the traffic on the CAN bus and also be able to send messages. Most/all of these posts deal with using CAN0 and CAN1 to communicate back and forth. I want to be able to see the traffic from an already existing CAN bus, and no this is not in my car...

I have the WaveShare CAN board as I was told these would be good to use for this kind of application because they function at 3.3V. These are the boards: http://www.wvshare.com/product/SN65HVD230-CAN-Board.htm

Can any of you guys provide some help with this?
Title: Re: Building a CAN API for Arduino DUE
Post by: McNeight on Jun 19, 2014, 08:59 am
rtfh,

It looks like you are off to a good start with that board. Connect CAN TX on the Due to CAN TX on the board, and the same for CAN RX. Hook up power and ground, and check your termination, and you should be good to go.

And since nobody else had mentioned it, I went ahead and implemented a "unified" CAN library. One API gets you functionality for three different types of CAN controller; Microchip MCP2515, Atmel SAM3X (Due), and Freescale K2x (Teensy 3.1). It's not quite finished yet, but if you are interested in using it, take a look at https://github.com/McNeight/CAN-Library/

-Neil
Title: Re: Building a CAN API for Arduino DUE
Post by: Jasoncha on Jun 20, 2014, 08:33 am
Hey Palliser,

I having many problems running the traffic snooper file



CAN_TrafficSnooper:15: error: variable or field 'printFrame' declared void
CAN_TrafficSnooper:15: error: 'CAN_FRAME' was not declared in this scope
CAN_TrafficSnooper:15: error: 'frame' was not declared in this scope
CAN_TrafficSnooper.ino: In function 'void setup()':
CAN_TrafficSnooper:19: error: no matching function for call to 'CANRaw::init(int)'
C:\Users\jj\Desktop\arduino\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:185: note: candidates are: uint32_t CANRaw::init(uint32_t, uint32_t)
CAN_TrafficSnooper:20: error: no matching function for call to 'CANRaw::init(int)'
C:\Users\jj\Desktop\arduino\arduino-1.5.2\hardware\arduino\sam\libraries\CAN/due_can.h:185: note: candidates are: uint32_t CANRaw::init(uint32_t, uint32_t)
CAN_TrafficSnooper:28: error: 'class CANRaw' has no member named 'setRXFilter'
CAN_TrafficSnooper:29: error: 'class CANRaw' has no member named 'setRXFilter'
CAN_TrafficSnooper:33: error: 'class CANRaw' has no member named 'setRXFilter'
CAN_TrafficSnooper:34: error: 'class CANRaw' has no member named 'setRXFilter'
CAN_TrafficSnooper.ino: At global scope:
CAN_TrafficSnooper:39: error: variable or field 'printFrame' declared void
CAN_TrafficSnooper:39: error: 'CAN_FRAME' was not declared in this scope
CAN_TrafficSnooper:39: error: 'frame' was not declared in this scope


any advise?
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jun 20, 2014, 09:41 pm
Try to make sure that you really are compiling for the Arduino Due, using the 1.5.4 IDE or newer, and that you don't have any older copies of the library around. I've had people accidentally have two copies with an older one in the first place Arduino looks.
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Jun 22, 2014, 05:41 pm

Hey Palliser,

I having many problems running the traffic snooper file

[code errors]
any advise?



Hello Jason. It sounds like you have selected the wrong board in your IDE 1.5.X.

p
Title: Re: Building a CAN API for Arduino DUE
Post by: gambit1244 on Jul 12, 2014, 01:46 am
Hello guys,
i have a so many mcp2551 transceiver chip. and i dont want to wait for work (china ship time 15 days  :smiley-roll: )
so they are 5v level, due is 3.3v level
after the level harmonize (5v>3.3v)
can i use them together with this can api?
Thank you so much
Tufan.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jul 12, 2014, 05:21 am

Hello guys,
i have a so many mcp2551 transceiver chip. and i dont want to wait for work (china ship time 15 days  :smiley-roll: )
so they are 5v level, due is 3.3v level
after the level harmonize (5v>3.3v)
can i use them together with this can api?
Thank you so much
Tufan.


Sure, I don't see why not. We had standardized on a different chip just because it is 3.3v, reasonably respected, and easy to obtain. But, within reason you can use any canbus transceiver chip you want. The transceiver is almost completely transparent as far as the library goes. The only reason we even have code for the transceiver at all is because the SN65HVD chip we're using has an enable pin so you can turn the transceiver on and off. If you don't care about this you can just hard wire it to on or use a chip that doesn't have an enable pin.

As you mentioned, using a 5V chip will necessitate a level shift. You will, at the least, need to turn the 5V CAN_RX output from the MCP2551 into 3.3V for the Due. I'll bet the 3.3V CAN_TX signal from the Due will trigger the MCP just fine but you'll have to try it.
Title: Re: Building a CAN API for Arduino DUE
Post by: gambit1244 on Jul 14, 2014, 10:03 pm
thank you so much collin80,

i see a howtouse.txt file in new github

but examples are very hard for me
i need to send simple can message
how can i do that? can you show me please a very very simple code like this
because this is doesnt work :(


Code: [Select]


#include "variant.h"
#include <due_can.h>

void setup()
{
CAN.init(250000);
}

void loop()
{
uint32_t id ;
uint8_t length;
uint8_t data[8];

CAN_FRAME myFrame;
myFrame.id = 7E0;
myFrame.length = 1
myFrame.data[0] = 0xFF;
CAN.sendFrame(myFrame);
delay(100);
}



i have a peak system can bus interface  for watch
i want to test my mcp2551  with simple code
thank you so much
Title: Re: Building a CAN API for Arduino DUE
Post by: gambit1244 on Jul 16, 2014, 02:26 pm
anybody can help me please    :~
i need a very simple code for send can message

warm regards.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jul 17, 2014, 05:31 pm
Sorry, I wanted to help but but I've been unable to log into this forum for the past few days. It seems to be fixed now.

Anyway, your code looked right except that you were missing a semi-colon and a hexadecimal prefix. The below two lines are what would have to change:

myFrame.id = 0x7E0;
myFrame.length = 1;

Those are the correct lines.
Title: Re: Building a CAN API for Arduino DUE
Post by: gambit1244 on Jul 18, 2014, 04:42 am
Thank you so much Collin,
but i cant compile this code where is the bug :(
i am watcihng the can1-can2 tx rx pins of arduino with my saleae logic analyzer

this is the screenshot
thank you for your help
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jul 18, 2014, 02:47 pm
Oops, I did miss that bug in your code. The data line should be:

myFrame.data.bytes[0] = 0xFF;

The data member of the structure is a union full of different types of access patterns so you can access it by byte, by 16 bit int, by 32 bit int, or by 64 bits all at once.
Title: Re: Building a CAN API for Arduino DUE
Post by: gambit1244 on Jul 19, 2014, 12:15 pm
thank you Collin,
i compile and run this code and im watching the arduino's can ports via my logic analyzer.
after the reset i can see the initalize logic levels, after init, tx ports high , rx ports staying low positions and nothing more :(
i didnt connect my transceiver chips yet. logic analyzer directly connected to can tx -rx pins directly
why i cant see the id and data
when i connect my logic analyzer to my can bus data logger's ports i can see the all id and data but arduino doesnt work :(


i think you might be busy but i need really help collin :( thank you again
warm regards
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jul 21, 2014, 08:20 pm
To help narrow it down you should try two things:

1. Compile and run one of the example sketches that sends data. The pingpong sketch should be fine for this. It should immediately saturate the bus with attempts to deliver frames.
2. You don't happen to have another Due do you? (I'm doctor Suess now)
Title: Re: Building a CAN API for Arduino DUE
Post by: Manewc on Aug 06, 2014, 05:08 pm
Hi

Thanks a million for this thread makes everything much easier, i have a question which is of the options below is viable with due arduino and which one would be best with an arduino ethernet shield.

http://www.ti.com/product/sn65hvd233

http://www.wvshare.com/product/SN65HVD230-CAN-Board.htm

http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en010405

Thanks for your help this will help me with my project

manewc
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Aug 06, 2014, 08:36 pm
Manewc,

The first two use the SN65HVD23X transceiver (where X is 0,3,4, etc). That transceiver will work fine. I really wouldn't recommend using the MCP2551 as it assumes a 5V tolerant MCU which the Due is not. If you are building a one-off prototype then the second link with a breakout board would probably be ideal. But, if you're going to use another board like an ethernet shield, then you might be able to add the bare SN65HVD chip on a prototyping section. All the ethernet shields I found don't have such an area though.
Title: Re: Building a CAN API for Arduino DUE
Post by: zabaat on Aug 15, 2014, 08:31 pm
My thanks again to Collin and Palliser!

I've built a number of these boards for a project but placed my clamping diode incorrectly and will have to run them again. They are perfectly fine for someone that won't be going over the 36V input range ever.  I think they would be quite nice for someone who is looking to tinker and wants a stable platform to do so.

It has placements for a 9-36V input, a pheonix contact push in connector for powre and CAN, a multi dip switch and all the caps. I've used it with the arduino due and the DigiX due clone with great success. Also dual ethernet.

I've soldered it all by hand so you shouldn't need to be too scared of it.

If someone is interested let me know. dimensions are 4.4" square.

(http://i.imgur.com/Cwtc6vAl.jpg)
Title: Re: Building a CAN API for Arduino DUE
Post by: TT_ZX on Aug 25, 2014, 04:40 am
Many thanks to Collin, Palliser and everyone else that has contributed to this library.  I am working on a project that will have up to 22 nodes on a single bus.  Without this library I don't think I could attempt this with my limited knowledge.

I have a couple of questions that hopefully someone could comment on.

1. I want to isolate each node on the bus.  This is because the bus length will be up to 100m and each node will have it's own power supply (230VAC at each node).  So far my testing has been using the SN65HVD234 transceivers and they work very well.  But to isolate each node I'm thinking that I'd need to either us some optocouplers between the transceiver and micro controller or change to a different transceiver.  I quick Google turns up the ISO1050 which is available locally.  Here's the datasheet:
http://www.ti.com/lit/ds/symlink/iso1050.pdf (http://www.ti.com/lit/ds/symlink/iso1050.pdf)
Am I on the right track?  Has anyone else done something similar?

2. Can the due_can library be used with the AT90CAN series of micro controllers?
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Aug 25, 2014, 04:00 pm

1. I want to isolate each node on the bus.  This is because the bus length will be up to 100m and each node will have it's own power supply (230VAC at each node).  So far my testing has been using the SN65HVD234 transceivers and they work very well.  But to isolate each node I'm thinking that I'd need to either us some optocouplers between the transceiver and micro controller or change to a different transceiver.  I quick Google turns up the ISO1050 which is available locally.  Here's the datasheet:
http://www.ti.com/lit/ds/symlink/iso1050.pdf (http://www.ti.com/lit/ds/symlink/iso1050.pdf)
Am I on the right track?  Has anyone else done something similar?


Good thinking. The ISO1050 works fine with the Due and with the due_can library. I do have experience with this. The GEVCU project has several (> 50) boards out in the field with that exact transceiver. We haven't blown up the transceiver or board yet.

Quote

2. Can the due_can library be used with the AT90CAN series of micro controllers?


No. The AT90CAN series of processors are 8 bit AVR processors and this library is meant specifically for the 32 bit ARM Cortex M3 processor in the Due.
Title: Re: Building a CAN API for Arduino DUE
Post by: g4rb4g3 on Sep 07, 2014, 06:49 pm
Hi there,

I'm trying to get an Arduino Due to work with my car but init functions always returns 0 when the transcreceiver is connected to the cars bus system.
I'm using a transcreceiver board with the SN65HVD230 chip (http://www.wvshare.com/product/SN65HVD230-CAN-Board.htm). As far as I understood this thread it should work with the library here.

I guess the main problem is that the cars bus is working at 100Kbit/s while this library doesn't seem to support it.
At least in the due_can.h there are only higher or lower values, so I added a line for 100Kbit/s.
Code: [Select]
#define CAN_BPS_100K                  100000
I found out that the CANRaw::set_baudrate functions seems to work out and it does return 1. But I'm not realy sure if the can_bit_time array has the right values for the desired speed.
I understand some of the code but as a java and .net programmer c(++) code is pretty hard to understand for me (especialy the bit-shifting ... :smiley-eek:).

So maybe someone here could help me out and tell me if the code should work with 100Kbit/s or if it needs some modification. If it does it would be great to get some hints for the right direction  :).

Edit: I forgot to mention that I removed the 120 Ohm resistor on the board already since this is not the start/end point of the bus.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Sep 08, 2014, 02:14 am
Yes, all of that should work fine. There is an array for bit timing but it isn't directly tied to the actual canbus speed. In theory you should be able to set any canbus speed. But, some speeds will not be possible to exactly reach. I'd imagine that 100k baud wouldn't be a problem. But, if you try to set it to 46,247 baud you'll probably have trouble.
Title: Re: Building a CAN API for Arduino DUE
Post by: that_kid on Sep 11, 2014, 06:59 pm
I can confirm that it does work fine at 100k, I tested this in my car on the infotainment bus which runs at that speed.
Title: Re: Building a CAN API for Arduino DUE
Post by: g4rb4g3 on Sep 20, 2014, 06:00 pm

I can confirm that it does work fine at 100k, I tested this in my car on the infotainment bus which runs at that speed.


Could you please provide me some more infos about your setup?
I'm trying to get access to the K-Can on a BMW E91, maybe your experience could help me a bit.
Title: Re: Building a CAN API for Arduino DUE
Post by: that_kid on Sep 21, 2014, 02:36 pm
I added a #define for 100k as you did and was able to connect to the 100k infotainment and comfort bus in my VW Touareg.  I'm grabbing the can bus lines from the radio/nav socket in the dash.  I also was able to connect from the bus lines connected to the digital amp in the back.  I didn't need a terminating resistor on the Arduino side either.
Title: Re: Building a CAN API for Arduino DUE
Post by: agrawalk on Sep 23, 2014, 07:42 pm
Hey Guys,

so I need some help. I am trying to implement a CAN Bus interface using the arduino due. I am using your library as a template but was having some issues.

Currently I am trying to send Can messages from by due to my computer ( I have industrial level software that shows me can messages).  Specifically the software I am using is called CANalyzer provided by Vector.

I have in various parts of my code instrumented more code to check whether all the register values are being read correctly and are in fact valid.  I have include images of  the register values( printed via serial port) along with my code so you can cross check.

The issue I am having is although the CANalyzer software detects a message on the line it detects a "error frame".  Furthermore if you see then message breakdown (CANaylzer allows you to see the error frame breakdown) you will notice that all relevant values i.e. ID & Data are correct.

Obviously as per Can protocol at 128 messages failed to send ( as the due gets no acknowledgement) the Can timeouts.

Additional info:
I am using (2 * 120 || 120 ) terminating resistors.  


Please let me know if you need anything more.


###Here is the ino code ###



// Required libraries
#include "variant.h"
#include "due_can.h"

#define TEST1_CAN_TRANSFER_ID   0x1FFFFFFF //11 bits Standard //random 29 bits Extended
#define TEST1_CAN0_TX_PRIO       1
#define CAN_MSG_DUMMY_DATA       0xABC

// CAN frame max data length
#define MAX_CAN_FRAME_DATA_LEN   8

uint32_t sentFrames, receivedFrames;

//Leave this defined if you use the native port or comment it out if you use the programming port
//#define Serial SerialUSB

CAN_FRAME frame1, frame2, incoming;

void setup() {

   // start serial port at 115200 bps:
   Serial.begin(115200);

   // Serial port intializied
   Serial.print("Serial Port Initialized\n");

   // Verify CAN0 and CAN1 initialization, baudrate is 0.25Mb/s:
   if (CAN.init(CAN_BPS_250K) &&
      CAN2.init(CAN_BPS_250K)) {
   }
   else {
      Serial.println("CAN initialization (sync) ERROR");
   }

   //Initialize the definitions for the frames we'll be sending.
   //This can be done here because the frame never changes
   frame1.id = TEST1_CAN_TRANSFER_ID;
   //frame1.fid = 0x000000A3;
   frame1.length = MAX_CAN_FRAME_DATA_LEN;
   //Priority
   frame1.priority = 0;
   frame1.rtr = 1;
   //Below we set the 8 data bytes in 32 bit (4 byte) chunks
   //Bytes can be set individually with frame1.data.bytes[which] = something
   frame1.data.low = 0xA;
   frame1.data.high = CAN_MSG_DUMMY_DATA;
   //We are using extended frames so mark that here. Otherwise it will just use
   //the first 11 bits of the ID set
   frame1.extended = 1;

   //Lines create a filter on the corresponding CAN device that allows
   //just the one ID we're interested in to get through.
   //The syntax is (mailbox #, ID, mask, extended)
   //You can also leave off the mailbox number: (ID, mask, extended)
   CAN2.setRXFilter(0, TEST1_CAN_TRANSFER_ID + 0x200, 0x1FFFFFFF, true);
}

// Test rapid fire ping/pong of extended frames
static void test_1(void)
{

   Serial.println("******START OF LOOP ******");
   CAN_FRAME inFrame;
   uint32_t counter = 0;
   // Send out the first frame
   CAN.sendFrame(frame1);
   sentFrames++;
   Serial.print("No of Sent frames: ");
   Serial.println(sentFrames);
   /*
   CAN.printCAN_MR();
   Serial.println("############");
   CAN.printCAN_IER();
   Serial.println("############");
   CAN.printCAN_IDR();
   Serial.println("############");
   CAN.printCAN_IMR();
   Serial.println("############");
   CAN.printCAN_SR();
   Serial.println("############");
   CAN.printCAN_MID7();
   Serial.println("############");
   CAN.printCAN_ECR();
   Serial.println("############");
   CAN.printCAN_ECR();
   delay(5000);
   */
   delay(2000);
   Serial.println("<><><><><><><><><>><><><><>");
   CAN.printCAN_ECR();
   Serial.println("<><><><><><><><><>><><><><>");
   Serial.println("#####END OF LOOP ######");

}


// can_example application entry point
void loop()
{
   test_1();
}

Thanks in advance.
Kamil
Title: Re: Building a CAN API for Arduino DUE
Post by: g4rb4g3 on Sep 23, 2014, 09:02 pm

Yes, all of that should work fine. There is an array for bit timing but it isn't directly tied to the actual canbus speed. In theory you should be able to set any canbus speed. But, some speeds will not be possible to exactly reach. I'd imagine that 100k baud wouldn't be a problem. But, if you try to set it to 46,247 baud you'll probably have trouble.


Thank you, I've tried it again today and now it works. :smiley-eek:
I'm not sure why, I know for sure that I didn't change anything on the software side and that I didn't change anyhting on the cables between the Arduino and the transreceiver board. Maybe I just connected the can bus the wrong way around last time... I guess so.
I just added a comment to my sketch so I know how to connect it the right way now for sure. ;)
Title: Re: Building a CAN API for Arduino DUE
Post by: jspobuk on Oct 02, 2014, 01:24 am
Does anybody know if a 2003 Ford Ranger uses CAN?  Or where I can find that information?  Google just gives me confusing results. 
It's the only vehicle I have to work with.  Thanks in advance!
Title: Re: Building a CAN API for Arduino DUE
Post by: lol_lol_2512 on Oct 12, 2014, 03:43 pm
Hello everyone, I have been following this thread for a while, I must say thanks to everyone that has contributed to this library.

I am currently using this library with a Motec M800 pnp with standard data set and it works so well. I am logging 5 frames which means 20 channels (rpm, throttle position etc) and I get about 40-50 updates per second!!!
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Oct 27, 2014, 11:19 pm
Does anybody know if a 2003 Ford Ranger uses CAN?  Or where I can find that information?  Google just gives me confusing results. 
It's the only vehicle I have to work with.  Thanks in advance!
Hello jspobuk,

According to a contributor in the Ford Ranger forum (see #4 of the link)...

http://www.fordrangerforum.com/general-ford-ranger-discussion/93279-can-bus.html (http://www.fordrangerforum.com/general-ford-ranger-discussion/93279-can-bus.html)

Quote
... Ford Rangers don't use a true CAN-bus backbone system in regards to the vehicles overall wiring system like many modern vehicles now do.
So other than the generic OBD2 system - it doesn't have a vehicle wide CAN-bus wiring system that every device connects to using only a single CAN-Bus data line and power. In our Rangers, every switch & control point is still hard wired to its corresponding device via 'old school' wiring looms.

So, in practical terms - our trucks don't have a CAN-bus delivery system.
Regards,

p
Title: Re: Building a CAN API for Arduino DUE
Post by: jspobuk on Oct 28, 2014, 02:14 am
Thanks for looking that up for me Palliser!  A little sad, but at least it answers my questions. 

I might be able to get a hold of a Mini Cooper '12.  Does anyone know if Mini supports CAN as well?  Any help is greatly appreciated. 

Thank you,
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Oct 28, 2014, 02:02 pm
Thanks for looking that up for me Palliser!  A little sad, but at least it answers my questions. 

I might be able to get a hold of a Mini Cooper '12.  Does anyone know if Mini supports CAN as well?  Any help is greatly appreciated. 

Thank you,

Any 2008 or newer vehicle made for the US market must support CANBus for OBDII. Many vehicles already did before that year. So, a 2012 Mini Cooper should most certainly support CANBus on the OBDII port. I have no idea if it uses canbus elsewhere but most modern vehicles do.
Title: Re: Building a CAN API for Arduino DUE
Post by: jspobuk on Oct 28, 2014, 09:26 pm
So the trick is to either find a pinout for the obdII on a Mini or find an unused CANbus connector to tap into the data?
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Oct 28, 2014, 10:13 pm
So the trick is to either find a pinout for the obdII on a Mini or find an unused CANbus connector to tap into the data?
The OBD-II is a standard. Look at my post#38 for the pinout.
http://forum.arduino.cc/index.php?topic=131096.30 (http://forum.arduino.cc/index.php?topic=131096.30)

p
Title: Re: Building a CAN API for Arduino DUE
Post by: jspobuk on Oct 28, 2014, 10:36 pm
Thanks for the quick response.  So hypothetical situation (maybe its already a reality i dont know), if you had a wifi shield on your DUE and it was reading the CANbus in your car, can you have your phone automatically connect to it every time you got in your car and download a report and email it to you/dealer/person who fixes your car? And even if it is possible/available, what kind of information can you pull from the CANbus that would be useful apart from the lights on your dashboard?  Kind of an open ended question, any comments are welcome. 

Thanks,
J
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Oct 29, 2014, 07:34 pm
I suppose your theoretical situation could work. You could set up hardware that your phone could connect to and use for logging and diagnostics. This is going to require some custom work and two shields - one for wifi and one for canbus.

As for the sort of info you might be able to grab
1. OBDII is a diagnostics port so you can grab pretty much full diagnostics for the car.
2. It also tends to have performance data such as the current temperature at various points, the voltage, tire pressure, etc.
3. Who knows what else? The car is free to place more/proprietary data on this port. Very likely it has such things which are used by the dealer to get a better idea of what is going on with the car. Generally you only get the specification for this if you pay. You seem to be at an EV business. They can probably contact BMW and arrange to get this information if it would be lucrative for your business. It will probably be a few thousand dollars and an NDA away.
Title: Re: Building a CAN API for Arduino DUE
Post by: jspobuk on Oct 31, 2014, 02:54 am
Thanks for the comments Collin, I actually don't work at an EV business, just thinking out loud.  What would be the advantages over something like this:

http://www.amazon.com/BAFX-Products-Bluetooth-diagnostics-Android/dp/B005NLQAHS#productDetails

It sounds like it does everything I described, or am I missing something?

Thanks again in advance.

J
Title: Re: Building a CAN API for Arduino DUE
Post by: lol_lol_2512 on Nov 02, 2014, 06:41 pm
Thanks for the quick response.  So hypothetical situation (maybe its already a reality i dont know), if you had a wifi shield on your DUE and it was reading the CANbus in your car, can you have your phone automatically connect to it every time you got in your car and download a report and email it to you/dealer/person who fixes your car? And even if it is possible/available, what kind of information can you pull from the CANbus that would be useful apart from the lights on your dashboard?  Kind of an open ended question, any comments are welcome.  

Thanks,
J
I am actually using a bluetooth adapter that connects to the Arduino's serial port. I then use my android phone to connect and grab all sensor data. The great thing about this is that you can write an android app that can basically do anything. My app currently displays the sensor data like rpm, engine temp. on the screen and it also sends it over wifi/3g where anyone with a url can livestream my car's sensor data.
Title: Re: Building a CAN API for Arduino DUE
Post by: jspobuk on Nov 03, 2014, 07:28 pm
That is pretty sweet lol_lol, I would like to do the same but with these tier IV engines that run some pumps for our customers.  That way we can monitor them off-site (some are located at remote wells).  I don't know about continuous data usage, how much data does it eat up with ? 
Title: Re: Building a CAN API for Arduino DUE
Post by: nicnac on Nov 04, 2014, 05:11 pm
Hi, I've got a project, where we try to move a linear motor with CAN Bus. It's some Bosch linear motor.
Since I am totally new to CAN and this whole bus thing, I wanted to ask if you think communicating with a motor on a simple manner is realistic and how tedious it will be?
I basically want to get info of rpm and I want to control the same quantity on the motor.
What, except for a DUE do I need? I don't care about error notices or fancy features, I just need to be sure, that I can tell a motor via CAN to go faster.

Thanks, and thanks again for all the work you did for everyone here!
Title: Re: Building a CAN API for Arduino DUE
Post by: melodic on Nov 04, 2014, 05:31 pm
I am actually using a bluetooth adapter that connects to the Arduino's serial port. I then use my android phone to connect and grab all sensor data. The great thing about this is that you can write an android app that can basically do anything. My app currently displays the sensor data like rpm, engine temp. on the screen and it also sends it over wifi/3g where anyone with a url can livestream my car's sensor data.
can you share your sketch?
Title: Re: Building a CAN API for Arduino DUE
Post by: lol_lol_2512 on Nov 04, 2014, 10:37 pm
That is pretty sweet lol_lol, I would like to do the same but with these tier IV engines that run some pumps for our customers.  That way we can monitor them off-site (some are located at remote wells).  I don't know about continuous data usage, how much data does it eat up with ? 
I was also wondering how much data it would use but never properly measured it (I have a pretty nice phone plan so I don't really have to worry about data usage  :) ). I think its about 1mb/min, not exactly sure. That's with sending 8 channels about 4 times a second.

can you share your sketch?
I am very new to the world of programming so my sketch is rather terrible, pm me if you still want to see.
Title: Re: Building a CAN API for Arduino DUE
Post by: kiko_zizo88 on Dec 02, 2014, 03:35 pm
 i'm trying to build a canbus converter/modifier that has 2 canbuses like arduino due for example that can listen to the bus from CAN1 then edit only the msgs i choose before sending it back to CAN2 and all the msgs that i dont choose to change just forwarded as is from CAN1 directly to CAN2
my programing skills is not that good so i need help on how i can implement this idea into a code

this project will be used in a VW jetta where i want to install VW DSG gearbox to a manual vichle so i need to modify the canbus for the jetta to make the DSG understan it
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Dec 02, 2014, 05:25 pm
Kiko, that's an understandable goal. I'm sure many people would be interested in that same sort of thing. It's quite common to want this functionality for the very reason you want it. So, I'll create another example sketch that shows how to do this. Sometime in the next 24-48 hours it should be ready.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Dec 04, 2014, 04:01 am
OK, the due_can library now has a new example: TrafficModifier. This sketch can be used to cut a canbus line and forward frames from one side to the other. But, it also will modify specific frames based on the ID such that you could do things like edit torque commands. I should probably mention that you are solely responsible for what happens when you do this. If you command too much torque and snap a half shaft that's your problem, not mine. If you fault your ECU you did it. My point is that using this sketch to modify traffic is potentially unsafe so be careful. It could be used for practically anything.
Title: Re: Building a CAN API for Arduino DUE
Post by: rockwallaby on Jan 06, 2015, 03:34 pm
I'm looking into a project that could use CAN bus and recalled that there was a thread in the DUE section that Palliser started back in 2012.

So, I re-found this thread, great, but where to find the code, the library?

I first thought, any good forum edicate makes it so that the latest code is easily found usually at post #1 on the first page, but no, nothing there.

So, then I thought, well, I'll work backwards from the newest post and see if there is any reference to where I can find the library.

So, then I start reading backwards, nothing.

So, then I start again from the beginning, surely at some point I will find where the heck this code is located.

Then I come across a post where it is first announced, but no, that git repo no longer exists, giving me a big 'Page Not Found'.

I proceed further, reading and reading, until, finally Collin makes a fork after making suggestions to improvements to the code as recieved.

That was at post #99, and I will quote it here:
Quote
23 February 2013, 03:52:08
I have done some updates to the canbus library previously posted. I've made it a lot simpler to work with, more object oriented, and more "Arduino" like. I forked the Arduino project and my changes are pushed to my own fork. So, if anyone is interested they'd have to get it from my github account:
https://github.com/collin80/Arduino.git

I'm working on making it interrupt driven as well. Also, I will try to keep encapsulating functionality into easier to work with classes. I think the next order of business is to work on the mailbox settings. It's not fun to work with all that currently.

I wish there were a better way to share the canbus library. Perhaps I should create a repo for just the library on my account and push just the canbus files there? It would make things a lot smaller and easier to get.
Collin, I believe you saved me from total madness, thank you.

Oh, yes, finally I see the repo with what I hope is the latest and greatest.

So, can I check and ask, is this site the latest code and where the development is taking place?
https://github.com/collin80/Arduino.git (https://github.com/collin80/Arduino.git)

What I can say is all that reading gave me further insight into CAN bus.
____
Paul

Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jan 06, 2015, 05:53 pm
That was at post #99, and I will quote it here:Collin, I believe you saved me from total madness, thank you.

Oh, yes, finally I see the repo with what I hope is the latest and greatest.

So, can I check and ask, is this site the latest code and where the development is taking place?
https://github.com/collin80/Arduino.git (https://github.com/collin80/Arduino.git)

What I can say is all that reading gave me further insight into CAN bus.

No, use https://github.com/collin80/due_can

I forked the Arduino IDE for an unrelated reason and it doesn't have the canbus library in it.
Title: Re: Building a CAN API for Arduino DUE
Post by: rockwallaby on Jan 06, 2015, 10:46 pm
Hi Collin, yes, I guess I did leave off the '/due_can' from the location.

You say 'it doesn't have the canbus library in it', but from the readme it says:
Quote
due_can

Object oriented canbus library for Arduino Due compatible boards

Download the archived 1.0 release if you are compiling against existing code bases such as the EVTV GEVCU project. New projects will probably find that the current "master" branch version of the project is easier to use.
That to me suggests it is a CAN bus library, can you explain it then to me what you mean?

Also, what is 'archived 1.0 release' and how does it relate and where does one find this?

What is the EVTV GEVCU project?

It's a lot of guess work, trying to work out with so little explanations.
____
Paul

EDIT: I see my error, I gave the wrong link altogether, please ignore most of what I say above then.

The link https://github.com/collin80/due_can is where I can find a working library for the DUE then?
Collin, are you now the main developer for this library?
____
Paul
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jan 07, 2015, 10:17 pm
The GEVCU project uses custom hardware (still Due compatible) and software to create a vehicle control unit for electric cars. Think of it as the central brain box of the car. It's for DIY people who are building an electric car out of an existing gasoline/diesel car.

Don't use the archived version. Just pull the master branch of due_can and you're all set. I am the main developer for the library - at least the version at my github account. There is no canbus library by default in the Arduino IDE. It was discussed a while back but I keep making changes and they at least want that sort of thing to settle down before including it.
Title: Re: Building a CAN API for Arduino DUE
Post by: rockwallaby on Jan 08, 2015, 12:24 am
Collin, thank you for your clarification, that helps clear up a few things for me now.

I since found an article on EVTV while I was searching for DUE based CAN bus transceiver boards.
This one here, where I see you are mentioned http://evtv.me/2014/09/nerd-thing/ (http://evtv.me/2014/09/nerd-thing/)
I is an interesting read and there certainly is an interest in my mind to want to have an electric vehicle.

But my current need for using CAN bus is to connect and communicate with a LiFePO4 battery management system for my up and coming upgrade to using Lithiums over lead acid for my off-grid home.

The BMS is made by an EV company here in Australia, this is the board http://zeva.com.au/index.php?product=112 (http://zeva.com.au/index.php?product=112)
Would you suggest another option, I only need it for eight cells, 24 volt system?

My intention is to use one of the DUEs I have here to replace a Ether-Mega for my main controller used for all the IO and comms to sensors and SSR's.

I use a Cubieboard connected to this to then provide the main SCADA functions and HTTP server.

So, I am on the lookout for a suitable CAN transceiver board that I can use on the DUE, do you have any suggestions?

Would the board mentioned on the EVTV link be worth my while?
I noticed there was a bunch of boards made up before that where the connectors were placed on the wrong side of the board, and now sit in a box, maybe I could ask for one or two of those?

Another was where a component was misplaced and the board is only ok for systems where the voltage was below 36 volts, and again, those boards sit in a box unused, is that also an option?

I would like to organise for one or two of these boards, who do I need to contact?

Again, many thanks for your input.
____
Paul
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Jan 08, 2015, 04:31 pm
Hello Paul/Collin,

I've added Collin's link at page 1 post#0.

Regards,

p
Title: Re: Building a CAN API for Arduino DUE
Post by: sdourlens on Jan 25, 2015, 10:30 am
Hello Collin, Paul, Palliser,

It seems there is a "bug" in the CAN DUE library I detected in a specific case:
In any cases of your example, you recieve a CAN message (for example from an ECU) and then you answer. ANd yes, it works very well. I think because the CAN bus is already well synchronized and well set by ECU or others. I don't know why it works in between CAN0 and CAN1 of ATMEL SAM3X but I think it's because they should work and has been tested or have the same configuration by default.

In my case, I only have a sniffer like IXXAT and my board is only CAN message emitter on CAN1.
I set the two devices at the 500Kb/s baudrate CAN2.0A = 11 bits ID, no error until now.
When I send a CAN message with my board, the first message (only one sent) is received billions times by IXXAT, I have a 128 transmit error (get_tx_error_cnt()) and I saturate IXXAT software. why ? (In my application, I also use CAN0 at 250Kb/s but I don't use it when looking for the bug).

I investigated and I did the same with others devices on the can bus just waiting for messages (not in sniffer mode) and the same situation appears. I verified the call to global_send_transfer_cmd() (see below), only one call is made, so no bug in the library to send a message, the message stays in the mailbox because of the error).  I also tried "disable_tx_repeat()", no change.

void CANRaw::global_send_transfer_cmd(uint8_t uc_mask)
{
   uint32_t ul_reg;

   debug_nbsent++; // I get 1 when I call sendFrame(), so no error here !
   ul_reg = m_pCan->CAN_TCR & ((uint32_t)~GLOBAL_MAILBOX_MASK);
   m_pCan->CAN_TCR = ul_reg | uc_mask;
}


I do the same but with one other device on the can bus (a ELM327) first sending a request before any response of my board and no error at all when I send the response. get_tx_error_cnt()=0 ! It seems ELM327 well initialized the bus.

So my conclusion is that there is a initialization problem or a bit error problem of the CAN of the ATMEL processor.

Please tell me what do you think about it and if you can give some solutions to try or a working example in my case. Any more ideas are welcomed.

Thank you a lot for your time.

Title: Re: Building a CAN API for Arduino DUE
Post by: adar0n on Feb 04, 2015, 06:32 pm
Hello team,

I have an issue tring to compile CAN_TrafficSnooper, the error is :
ue_can.cpp.o: In function `CANRaw::read(CAN_FRAME&)':
/Applications/Due-Arduino.app/Contents/Java/hardware/arduino/sam/variants/arduino_due_x/due_can.cpp:32: multiple definition of `CANRaw::CANRaw(Can*, unsigned long)'
due_can.cpp.o:/Applications/Due-Arduino.app/Contents/Java/hardware/arduino/sam/variants/arduino_due_x/due_can.cpp:32: first defined here
due_can.cpp.o: In function `CANRaw::read(CAN_FRAME&)':
/Applications/Due-Arduino.app/Contents/Java/hardware/arduino/sam/variants/arduino_due_x/due_can.cpp:32: multiple definition of `CANRaw::CANRaw(Can*, unsigned long)'
due_can.cpp.o:/Applications/Due-Arduino.app/Contents/Java/hardware/arduino/sam/variants/arduino_due_x/due_can.cpp:32: first defined here
due_can.cpp.o: In function `CANRaw::mailbox_set_datah(unsigned char, unsigned long)':
/Applications/Due-Arduino.app/Contents/Java/hardware/arduino/sam/variants/arduino_due_x/due_can.cpp:52: multiple definition of `CANRaw::set_baudrate(unsigned long)'
due_can.cpp.o:/Applications/Due-Arduino.app/Contents/Java/hardware/arduino/sam/variants/arduino_due_x/due_can.cpp:52: first defined here
due_can.cpp.o: In function `CANRaw::sendFrame(CAN_FRAME&)':
/Applications/Due-Arduino.app/Contents/Java/hardware/arduino/sam/variants/arduino_due_x/due_can.cpp:225: multiple definition of `CANRaw::setCallback(int, void (*)(CAN_FRAME*))'
due_can.cpp.o:/Applications/Due-Arduino.app/Contents/Java/hardware/arduino/sam/variants/arduino_due_x/due_can.cpp:225: first defined here
due_can.cpp.o: In function `CANRaw::setGeneralCallback(void (*)(CAN_FRAME*))':
/Applications/Due-Arduino.app/Contents/Java/hardware/arduino/sam/variants/arduino_due_x/due_can.cpp:238: multiple definition of `CANRaw::setGeneralCallback(void (*)(CAN_FRAME*))'
due_can.cpp.o:/Applications/Due-Arduino.app/Contents/Java/hardware/arduino/sam/variants/arduino_due_x/due_can.cpp:238: first defined here
due_can.cpp.o: In function `CANRaw::attachCANInterrupt(void (*)(CAN_FRAME*))':
Title: Re: Building a CAN API for Arduino DUE
Post by: rockwallaby on Feb 04, 2015, 11:37 pm
adarOn: please learn to read and understand your compile errors.
From the errors, I see this;
Code: [Select]
multiple definitionThis tells you a lot, it tells me that you possibly are including the same header file twice in your application somewhere, or you are including not the header file, but the source file.


What IDE are you using, the basic Arduino IDE or something better like Eclipse?
____
Paul
Title: Re: Building a CAN API for Arduino DUE
Post by: adar0n on Feb 05, 2015, 11:42 am
Thanks a lot, I have deleted the header files from the scketch directory and everything was fine. I considered that invoking #include <due_can.h> will not add the files in the local directory.

I am using Arduino IDE 1.5.8 for due.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Feb 05, 2015, 07:09 pm
sdourlens, sorry, didn't see your post until now.

What it sounds like is happening is that there is either a baud rate mismatch or you don't have enough bus termination (should be 120 ohms on both ends of the bus for a total bus impedance of 60 ohms). The library itself has been tested quite thoroughly in real usage within the GEVCU project and probably by other people. If the library were broken I think it likely (though not 100% certain) that we'd have known about it by now.

So, you should probably check your wiring and make sure that nothing is wrong. One way to check might be to try having the IXXAT send something. Does it also create tons of errors on the bus? This will help to narrow down the trouble.
Title: Re: Building a CAN API for Arduino DUE
Post by: adar0n on Feb 08, 2015, 10:05 pm
hello again,

Has anyone tried to use DUE with SN65HVD231D instead of SN65HVD234 ?
I find a lot of 230/231/232 in the stores instead of 234.

thanks a lot
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Feb 12, 2015, 06:06 pm
Hello adar0n,

I don't see any problem to use SN65HVD230/231/232. SN65HVD233, 234 and 235 are needed in case you need to use some of their special features like loopback (LBK-pin 5 in 233), ultra-low current sleep mode (EN-pin 5 in 234) and listen-only loopback (AB-pin 5 in 235).

Connect 230/231 as follows:

1-CANTX1 (DUE)
2-GND
3- +3V3
4-CANRX1 (DUE)
5- +3V3
6-CANL
7-CANH
8-10K resistor to GND

Remember to connect the 120 ohm resistor between CANL and CANH.
Also use 100nf and 10uf caps between +3V3 and GND to attenuate HF/RF noise and transients.

For 232, do not connect pins 5 and 8.

Regards,

p
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Feb 14, 2015, 01:15 pm
Hello all. First post. Allow me a short introduction and how I ended up here.

I am a software-guy-with-hardware roots by trade. Done quite a few things with Arduino Nano's and Mini's, such as controlling lights in my house using 433 MHz switches, impulse counting from the enery meter, etc. I am familiar with low-level interfacing, OS-less programming and building basic hardware. While I have read up quite substantially on CAN and OBD-II, I am new to the territory, but I do "get the basics".

I own a Renault Zoë full electric vehicle and I want to have some data available from the car itself (not the charger) while it is feeding, specifically SOC (State Of Charge), but once I get that, I know I will want more ;-)

One possibility (the best looking one so far) is to build a Due, CAN tranceiver, and hook up an ESP8226 Wifi board, and work from there. I would appreciate the forum members opinion on the following:

The Zoë does not talk with my run-off-the-mill (probably Chinese V1.0 bootleg, hard to figure that out) ELM327 <-> blue tooth dongle at all. I have searched, but not found hints on how "special" the Zoë's CAN implementation is. If there is anyone who has heard of a successful implementation I would be very grateful. Also, if you have any pointers in terms of "build xxx first, check yyy, give up if you see zzz (i.e. CanOpen?), I would appreciate that too. Well actually, I'd appreciate any pointers! While fun too, I'd like to avoid beginners mistakes. I am willing to go to the bit nitty gritty, but it would be far more effective if I can start "higher up the stack", read, rely on what you guys have already done.

Thank you all for your replies and thank you for what seems to be an awesome project. I admit I am still ploughing through the thread.

Jeroen


ps: I know of (only) one (commercial) product, not being Renault's CLIP system, that does succesfully communicate to the Zoë. It is called an "iCarsoft i907 for Renault Zoe". Google is your friend here. Problem with that one is that is is an entirely self-contained unit which cannot communicate to the outside world, and therefor not suitable for what I would like to do. I can probably borrow one for a day or two, so once I have some basic sniffing set up, I would guess I might be able to emulate some of it's functions. It does an awesome job, down to i.e. reporting the cell voltage of each individual cell in the car (there are about 100 in the pack)

pps: an alternative approach would be to use the latest greatest ELM327, and a simpler Arduino. However, I will only have a look at that if I can be sure the latest versions do communicate with the car and have not found anything so far.

ppps: There are definately two CAN busses on the OBD2 connector of the Zoë. One on the pin 6/14 pair (which is considered standard), and one on the pin 12/13 pair (which are basically manufacturer's discretion pins). A hint is this page: http://pinoutsguide.com/CarElectronics/renault_obd2_diag_pinout.shtml which I found after my measurements. They seem to suggest a secondary CAN bus indeed on pins 12/13, mainly for multimedia. Note that the page is specific for Renault, but not for the Zoë. I think the next step is to hook up a oscilloscope to both busses to see the data rate and what is happening.
Title: Re: Building a CAN API for Arduino DUE
Post by: adar0n on Feb 16, 2015, 08:19 am
Hey Pallister,
Thank you a lot for the reply. I made the circuit acording to your guidance with 231, loaded the CAN sniffer program, but .. no captured frames. I red carefully again the posts and see that you mention CANTX1/CANRX1 towards DUE, these are pins D69/D68 ? or D53/D66 ? I connected the transceiver to D69/D68 while the CAN sniffer program should read both CAN ports.

 I am tring to read the CAN messages between 2 industrial devices. I am not sure that is the bus speed, so I tried sequentially the init speeds to see what I can find. So far I noticed that below 250kbps, the CAN bus comes up between the devices, no messages on DUE. On 250kbps, the CAN bus flips so DUE interracts with the bus. On 500kbps, the CAN bus doesn't come up anymore, even the CAN on DUE should only sniff.

 Any ideeas are highly appreciated.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Feb 16, 2015, 03:39 pm
One possibility (the best looking one so far) is to build a Due, CAN tranceiver, and hook up an ESP8226 Wifi board, and work from there. I would appreciate the forum members opinion on the following:

The Zoë does not talk with my run-off-the-mill (probably Chinese V1.0 bootleg, hard to figure that out) ELM327 <-> blue tooth dongle at all. I have searched, but not found hints on how "special" the Zoë's CAN implementation is. If there is anyone who has heard of a successful implementation I would be very grateful. Also, if you have any pointers in terms of "build xxx first, check yyy, give up if you see zzz (i.e. CanOpen?), I would appreciate that too. Well actually, I'd appreciate any pointers! While fun too, I'd like to avoid beginners mistakes. I am willing to go to the bit nitty gritty, but it would be far more effective if I can start "higher up the stack", read, rely on what you guys have already done.
The chinese bootleg ELM327 clones quite often do not actually implement canbus. So, that could be your problem. If your adapter had canbus then it should work since OBDII is mandated in the US and Europe and so you should be able to get some standard data from OBDII queries. But, what you're looking for (SOC) might not be available via OBDII since that was meant for combustion diagnostics. So, you will probably need to gain access to things not available over OBDII. ELM327 chips aren't really that great for non-OBDII uses though the official units *can* do it. So, shameless plug - I am affiliated with EVTV and they have produced a canbus shield for the Due that works very well and is supported by my due_can library that we talk about here on this thread. But, the shield also has an SDCard slot and will do logging. It is also dual canbus so you could monitor two buses at once. It comes with an OBDII cable but the cable is only wired for the first canbus link (the one that OBDII runs on). Here is a link to it: http://store.evtv.me/proddetail.php?prod=ArduinoDueCANBUS&cat=23 it comes with the due and the cabling and everything. Yes, it is not the cheapest option but it's well made and well supported. The canbus shield could also be stacked onto something else if you wanted to use a wifi shield.

Most vehicles don't do anything funny enough to really mess up your attempts at getting the data. Maybe the Zoe uses canopen but canopen is mostly a trick of the canbus IDs and for your purposes you can mostly ignore the other details about it. J1939 likewise has additional protocol but mostly you can ignore that. You aren't trying to talk to the bus, just listen. If you're listening and everyone else is talking then all you need to do is find the relevant data among the chatter. The protocol is just inane chatter as far as you're concerned.

Quote
ps: I know of (only) one (commercial) product, not being Renault's CLIP system, that does succesfully communicate to the Zoë. It is called an "iCarsoft i907 for Renault Zoe". Google is your friend here. Problem with that one is that is is an entirely self-contained unit which cannot communicate to the outside world, and therefor not suitable for what I would like to do. I can probably borrow one for a day or two, so once I have some basic sniffing set up, I would guess I might be able to emulate some of it's functions. It does an awesome job, down to i.e. reporting the cell voltage of each individual cell in the car (there are about 100 in the pack)

ppps: There are definately two CAN busses on the OBD2 connector of the Zoë. One on the pin 6/14 pair (which is considered standard), and one on the pin 12/13 pair (which are basically manufacturer's discretion pins). A hint is this page: http://pinoutsguide.com/CarElectronics/renault_obd2_diag_pinout.shtml which I found after my measurements. They seem to suggest a secondary CAN bus indeed on pins 12/13, mainly for multimedia. Note that the page is specific for Renault, but not for the Zoë. I think the next step is to hook up a oscilloscope to both busses to see the data rate and what is happening.
The fact that something exists which can monitor the car suggests that the data are being sent over the OBDII connector which is a good sign.

BTW, my entire interest in canbus is centered on electric vehicles and reverse engineering so this all is right up my alley!
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Feb 16, 2015, 03:45 pm
Hey Pallister,
Thank you a lot for the reply. I made the circuit acording to your guidance with 231, loaded the CAN sniffer program, but .. no captured frames. I red carefully again the posts and see that you mention CANTX1/CANRX1 towards DUE, these are pins D69/D68 ? or D53/D66 ? I connected the transceiver to D69/D68 while the CAN sniffer program should read both CAN ports.

 I am tring to read the CAN messages between 2 industrial devices. I am not sure that is the bus speed, so I tried sequentially the init speeds to see what I can find. So far I noticed that below 250kbps, the CAN bus comes up between the devices, no messages on DUE. On 250kbps, the CAN bus flips so DUE interracts with the bus. On 500kbps, the CAN bus doesn't come up anymore, even the CAN on DUE should only sniff.

 Any ideas are highly appreciated.
You want to connect to pins 68 and 69. The other pins are for the second canbus. The sniffer program does not use listening mode and so it *does* interact with the canbus. The sniffer is fully capable of signaling reception of canbus frames. Because of this it'll cause havoc on the bus if you have the wrong speed set. From your description it seems as if 250k is the proper speed. But, I'm not sure why you would not then start receiving the messages.

Unfortunately, wiring up your own circuit is a recipe for a fun time. Maybe you did something wrong, maybe not. It's hard for me to say. If you have an oscilloscope you could scope the CANRX and CANTX pins going to the Due to see if you're getting nice squarewave signals there. If the Due is sending ACKs to can frames then you'll see one quick pulse on the CANTX line for every frame it is acknowledging.
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Feb 16, 2015, 04:31 pm
You want to connect to pins 68 and 69. The other pins are for the second canbus. The sniffer program does not use listening mode and so it *does* interact with the canbus. The sniffer is fully capable of signaling reception of canbus frames. Because of this it'll cause havoc on the bus if you have the wrong speed set. From your description it seems as if 250k is the proper speed. But, I'm not sure why you would not then start receiving the messages.

Unfortunately, wiring up your own circuit is a recipe for a fun time. Maybe you did something wrong, maybe not. It's hard for me to say. If you have an oscilloscope you could scope the CANRX and CANTX pins going to the Due to see if you're getting nice squarewave signals there. If the Due is sending ACKs to can frames then you'll see one quick pulse on the CANTX line for every frame it is acknowledging.
Also, which USB port are you using? Native or Programming? In the latter case, remember to
comment #define Serial SerialUSB.

p
Title: Re: Building a CAN API for Arduino DUE
Post by: adar0n on Feb 16, 2015, 04:42 pm
Hey Collin, Pallister, I am using the programming port and yes, I commented the SerialUSB;
I will add this evening another CAN transceiver for the second CAN bus port on DUE, this will eliminate the circuit issues I probably made on the first circuit and hopefully I will be able to pinpoint the issue. I don't have an oscilloscope.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Feb 16, 2015, 07:07 pm
Hey Collin, Pallister, I am using the programming port and yes, I commented the SerialUSB;
I will add this evening another CAN transceiver for the second CAN bus port on DUE, this will eliminate the circuit issues I probably made on the first circuit and hopefully I will be able to pinpoint the issue. I don't have an oscilloscope.
I know this is going to sound elitist but if you don't have an oscilloscope or a logic analyzer then you probably shouldn't be wiring up your own communications circuits. There are too many things that can go wrong. And, an oscilloscope would tell you what's going on in 2 minutes without having to try all sorts of things. You don't need a good scope. A cheapo 10MHz scope is sufficient for viewing canbus, I2C, TTL serial, all sorts of things. Old, cheap, doesn't matter. CANBus is pretty slow. Just something to consider. Having the proper equipment is really helpful and can save hours of time. There are various chinese options if you can't afford something name brand. There are even little portable units for like $100 new. Just get something that is at least several MHz or you'll have trouble with the faster canbus speeds.

Do you have a multimeter that measures frequency? You could try that on the CANRX pin and see if you get some frequency there.
Title: Re: Building a CAN API for Arduino DUE
Post by: adar0n on Feb 16, 2015, 07:50 pm
Hey Collin, first of all, you don't sound elitist at all :) , it makes perfect sense to have an oscilloscope, I actually had one and gave it away some years ago. I was hoping to strike lucky, get all working with DUE and focus on the programing part of CAN :).
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Feb 16, 2015, 10:00 pm
Dear Colin,


Thank you very much for your answer! Seems like we share the same interest. Much appreciated.

I have plowed trough the ELM docs and if it's a real thing it should be able to talk CAN, but who knows what's truly in that plug? The Due option seems more preferable to me as I can do what I want to really do with the data on the same processor. And yes, the data is most definitely on the OBD2 connector. I have watched the Renault mechanic do the same with the CLIP adapter to the OBD2 connector only, no other stuff needed, and the i907 does the same. I am 100% sure of that. I am also 99% sure (by simply measuring voltages and impedances) the Zoë's OBD2 only has said two CAN buses and nothing more. So yes, at this moment I am totally confident I should concentrate on CAN only. The only thing that worries me is that Renault might have decided to do some "weird" things on the higher OSI levels.

Actually, I probably do need to talk to the bus to ask the battery management system for the SOC. Or maybe it's in the chatter, who knows. As I understand it the "shameless plug" (good for you!) can do that off the bat, at least from a hardware perspective. If I read the image OK, CAN0 is wired to the DB9, and CAN1 to the green header. That is a neat design! Am I right to assume the entire software side of it is what we are discussing here in this thread?


Here is my short term "strategy":

1. Dig a bit deeper in the nitty gritty. Borrow & hook up scope, find out data rates, just to get a feeling;

2. Play a bit with the i907. Figure out if it even uses the second CAN bus. Figure out if I am happy with the data it provides (Of course I am in contact with the owner now, he seems very happy and impressed).

3. Depending on where we are then, I will put my teeth in the hardware. Yes, that board looks very nice and could save me a lot of time. If the second bus is needed, I could rewire de buses to a DB-15 compatible with the breakout cable.

4. Assuming reasonable success: scan, program, scan more, program more.

Thank you for the heads up Colin, and thank you for the "alley" remark: I will probably need to pick your brain a bit here and there. The Zoë is a reasonably successful full EV in the European market (I really enjoy mine) and will of course share everything I hopefully figure out.

Jeroen
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Feb 17, 2015, 04:07 pm
Update about the ELM327 (the official one). Here is their reply:

"No, sorry. We have not heard positively or negatively, about the ELM327's ability to support your vehicle. Being all electric, it likely does not use a standard OBDII protocol."

That is quite the statement isn't it? Anyway I have removed ELM from my shortlist for now.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Feb 17, 2015, 05:03 pm
The software for the hardware over at EVTV is all open source an available at my github repos. (Same place as the due_can library)

Yes, the ELM327 probably isn't quite what you want.
Title: Re: Building a CAN API for Arduino DUE
Post by: criis99 on Feb 20, 2015, 10:19 pm
How can see if send a frame? Help me please
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Feb 21, 2015, 04:44 pm
How can see if send a frame? Help me please
Hello Criis99,

Unless you have a scope, the easy way to know that a frame was sent is to receive a confirmation that it was read. In some of the CAN library examples this is done using the CAN function mailbox_get_status that in combination with CAN_MSR_MRDY (mailbox ready) gets the status of the mailbox if a response is received.

Code: [Select]
  // Wait for CAN1 mailbox 0 to receive the data
  while (!(CAN.mailbox_get_status(CAN1, 0) & CAN_MSR_MRDY)) {
 }


p
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Feb 22, 2015, 08:20 pm
Update: First bus (pin 6-14) on the Zoë runs on 500 kb/s. Second bus (pin 12-13) on 250. More or less as expected. I'll get myself some hardware now. Might take a few so I will probably be silent here for a while. But I will be back ;-)
Title: Re: Building a CAN API for Arduino DUE
Post by: criis99 on Feb 27, 2015, 05:34 pm


¿ SAM3X -EK must be programmed with the DB9 serial connector ?





Ok. Change of plans. Henceforth I will stay working with the CAN code available through the Arduino IDE. At the end, a CAN library and a couple of sample sketches will be added to the Arduino IDE as tools to send and receive standard (ver.2.0A - 11 bit) and extended (ver.2.0B - 29 bits) messages, but before this, as I stated in previous posts, I have been running some tests of the sample CAN code provided by Atmel in AS6, SAM_BA 2.12 and my SAM3X-EK board. The purpose of these tests is to dissect and obtain a proven minimum CAN code for the SAM3X8E. Finally, once the necessary source code has been chosen (libraries, .h, .c), we will proceed to program the SAM3X8E in Arduino due through the Arduino IDE.

I know the majority of the CAN protocol fans in this forum look forward to have access to the code but for the moment let me show you a picture my SAM3X-EK and a CAN device (sorry for the wiring mess) and also a pseudo-code and an idealized sketch.

(http://img5.imageshack.us/img5/4373/cantest1.jpg)

For understand a bit better the following pseudo -code, it is healthy to explain briefly about the CAN platform embedded inside the SAM3X8E core.
The SAM3X8H has two CAN Controllers. We will call these controllers CAN0 and CAN1. The SAM3X8H read/writes from/to them through the PIO A and PIO B respectively. Each of these controllers connects one respective bus. Each bus should comprehends only two transmission lines (CANTX/CANRX) but due to the energy efficiency design of the SAM3X8E, it is required the use of two CAN transceivers (one per bus) between the SAM3X8E and the outer world. Also, we will discuss latter about these transceiver and the future shield.
 Here the pseudo-code:
Initialization code (setup)

1. Define CAN0 and CAN1 transfer mailbox structure
2. Enable the module clock for CAN0 and CAN1
3. Initialize CAN0 and CAN1, baud rate (baud/s)
4. Reset all CAN0 and CAN1 mailboxes
5. Initialize CAN1 mailbox 0 as receiver, frame ID
6. Initialize CAN0 mailbox 0 as transmitter, frame ID
7. Prepare transmit ID, data and data length in CAN0 mailbox 0

Main code (loop)

1. Send out data in CAN0 mailbox 0
2. Wait for CAN1 mailbox 0 to receive the data
3. Read the received data from CAN1 mailbox 0

For a user-friendly interface, I will encapsulate the initialization code in a couple of steps that only include baud rate, frame ID, priority, type of message frame (2.0A or 2.0B) and mailbox configuration(transmitter/receiver).

The main code will use the following functions: send, wait and read.
An idelized Arduino Due CAN sketch will look like:

Code: [Select]

// Idealized CAN sketch for Arduino Due board
// By Palliser 2012


// Required CAN library
#include <can.h>

// Initialize CAN Controllers (defines structure, enable clock)
CANInit CAN0;
CANInit CAN1;

// Mailboxes frame ID 7 and 8
int MB_ID7 = 7;
int MB_ID8 = 8;

// CAN message to turn on Arduino Due pin 13
int CAN_MSG_HIGH_PIN_13 = 0x11223344
// Arduino Due pin 13
int led13 = 13;

void setup()
{
  //CAN baud rate 1Mb/s
  Can.begin(1000000);
  //Reset all CAN0 and CAN1 mailboxes
  Can.MBReset(CAN0);
  Can.MBReset(CAN1);
  //Init CAN0 mailbox 7 as receiver
  Mailbox.mode(CAN0, MB_ID7, RX_Mode);
  // Init CAN1 mailbox 8 as transmitter
  Mailbox.mode(CAN1, MB_ID8, TX_Mode);
  // Prepare transmit information into CAN1 mailbox 8
  CAN_MB_WRITE(CAN1, MB_ID8, CAN_MSG_HIGH_PIN_13);
  // initialize the digital pin as an output.
  pinMode(led13, OUTPUT);
}

void loop()
{
  
  // Send out information in CAN1 mailbox 8
  CAN_MB_SEND(CAN1, MB_ID8);
  
  while(!recv_status){
  }
  if(CAN0_MB_DATA == CAN_MSG_HIGH_PIN_13){
  Serial.println("Test passed");
  digitalWrite(led13, HIGH);}
  else{
  Serial.println("Test ERROR");  
  }
}


Regards.
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Feb 27, 2015, 09:13 pm
Nope. I programmed SAM3X-EK using SAM-ICE with ATMEL STUDIO 6.

p
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Mar 01, 2015, 12:37 am
Here a raw schematic of the photo.

(http://s21.postimg.org/cokhaoj5j/SAM3_X_EK_CAN005_z.jpg)

p
Title: Re: Building a CAN API for Arduino DUE
Post by: criis99 on Mar 07, 2015, 01:46 am
Here a raw schematic of the photo.

(http://s21.postimg.org/cokhaoj5j/SAM3_X_EK_CAN005_z.jpg)

p
Here a raw schematic of the photo.

(http://s21.postimg.org/cokhaoj5j/SAM3_X_EK_CAN005_z.jpg)

p
good afternoon
hello, thank you very much for helping me.

I have a question , I am working with atsam3xH8 with can bus using an example,
but I want to use extended mode 29 bits

Do you know how ?


and try this

    gCanTransfer0.acceptance_mask_reg = CAN_MAM_MIDvB_Msk;
    gCanTransfer0.identifier = CAN_MAM_MIDvB (0x0f0007);
Title: Re: Building a CAN API for Arduino DUE
Post by: Athlon on Mar 10, 2015, 12:52 am
Hello everybody,

first thank a lot for your work on the API.


I was interested in canbus since years but never did anything because is not my field.

Now I have some spare time and some DIY project and canbus fit perfectly.


My testbench is a canbus analyzer (Peak-system http://www.peak-system.com/PCAN-USB.199.0.html?&L=1)  and a few devices ( mostly battery BMS)  that generate canbus trafic.

My goal is to use an Arduino Due to log canbus messages on a sd card , to filter and modify part of the packets and to show some data on a Ldc.


Right now I have the Due in front of me , installed and I loaded some basic sketch just to chek if everything is working (compiling, analog read , digital out , serial monitor).

I also installed Collin's library but now I'm not sure how to go forward.


I was trying to do the EchoTest.ino and I can get to the "Type CAN message to send" point but no more.


my question is about can port .. in the examples there is always a Can0 and a Can1 port, but on the board I only see 2 pin CANRX CANTX ( I assue this is port0) , the other port witch pin uses?


I think I will not be able to run the EchoTest until I connect the two port and terminate the bus.





Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Mar 10, 2015, 01:08 am
Hello Athlon,

Check the schematic in post #33 for CAN port's pin location.

http://forum.arduino.cc/index.php?topic=131096.30 (http://forum.arduino.cc/index.php?topic=131096.30)

Regards,

p
Title: Re: Building a CAN API for Arduino DUE
Post by: ik2ihz on Apr 06, 2015, 09:35 pm
Hello everybody,
I approached theCAN BUS just few days ago and, therefore, I'm sure there is plenty of ideas around but... I want just to offer you a little, very simple idea to test the "due" as soon as you get it from the box. I did; it works;may be someone could be interested.... ;)

Connect a 470 ohm resistor to 3.3 pin on the DUE, connect the opposite side to:
CAN0RX and CAN1RX (DAC0). Nowconnect two diodes with the Anode to the resistor and receivers and the two cathodes to CAN0TX and CAN1TX (pin 53).

DONE !!!

I run the pingpong example from the examples available on https://github.com/collin80/due_can

I also designed this very simple isolated sniffer
   
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Jun 28, 2015, 06:56 pm
I have been away for quite a while for which I apologize. Real life got in the way. Anyway, today I assembled the hardware and configured the IDE for the Due, and...... Success! The Zoe is talking to me, though I don't understand what she's saying to me, yet :-)

This is a major step forward. If anyone has pointers to interpreters, I would appreciate it very much!

AN_TrafficSnooper
ID: 0x646 Len: 8 Data: 0x40 91 5 91 81 9A 19 50
ID: 0x130 Len: 8 Data: 0x0 C8 6F FE 0 9F FE 2D
ID: 0x17A Len: 8 Data: 0xFF FF FF AA 0 C0 31 A3
ID: 0x186 Len: 7 Data: 0x0 0 32 3 20 0 20
ID: 0x65B Len: 6 Data: 0x0 0 0 FC 41 34
ID: 0x1F6 Len: 8 Data: 0xDE 4 0 3B 0 FF 0 FF
ID: 0x12E Len: 8 Data: 0xCB 7F FC 7F E0 FF FF 0
ID: 0x242 Len: 7 Data: 0x2 0 FF EF FE 0 C
ID: 0x29C Len: 8 Data: 0x0 0 0 0 FF FF 0 0
ID: 0x352 Len: 4 Data: 0x5C 0 0 0
ID: 0x212 Len: 6 Data: 0xFE 3D C0 50 0 0
ID: 0x218 Len: 1 Data: 0x9C
ID: 0x4FA Len: 2 Data: 0x0 0
ID: 0x671 Len: 2 Data: 0x0 0
ID: 0x6F8 Len: 5 Data: 0x44 0 D5 AA FF
ID: 0x1F8 Len: 8 Data: 0xF8 4 FF EF FE 0 A D
ID: 0x17E Len: 8 Data: 0xFF FF FF 0 FF 40 0 FF
ID: 0x18A Len: 6 Data: 0xFF F0 0 6 40 0
ID: 0x432 Len: 8 Data: 0x50 40 2 B EC 80 0 58
ID: 0x130 Len: 8 Data: 0x0 E8 6F FE 0 9F FE D
ID: 0x17A Len: 8 Data: 0xFF FF FF AA 0 C0 31 A3
ID: 0x186 Len: 7 Data: 0x0 0 32 3 20 0 20
ID: 0x1F6 Len: 8 Data: 0xDE 4 0 3B 0 FF 0 FF
ID: 0x12E Len: 8 Data: 0xCC 7F FC 7F E0 FF FF 0
ID: 0x29A Len: 8 Data: 0x0 0 0 0 0 0 9 F6
ID: 0x2B7 Len: 5 Data: 0x0 E0 FF FE 94
ID: 0x1F8 Len: 8 Data: 0xF6 4 FF EF FE 0 C D
ID: 0x17E Len: 8 Data: 0xFF FF FF 0 FF 40 0 FF
ID: 0x18A Len: 6 Data: 0xFF F0 0 6 40 0
ID: 0x638 Len: 6 Data: 0x50 13 89 C0 0 0
ID: 0x130 Len: 8 Data: 0x0 8 6F FE 0 9F FE ED
ID: 0x17A Len: 8 Data: 0xFF FF FF AA 0 C0 31 A3
ID: 0x186 Len: 7 Data: 0x0 0 32 3 20 0 20
ID: 0x1FD Len: 8 Data: 0x8 40 7 FF 7F C8 50 0
ID: 0x1F6 Len: 8 Data: 0xDE 4 0 3B 0 FF 0 FF
ID: 0x12E Len: 8 Data: 0xCC 7F FC 7F E0 FF FF 0
ID: 0x242 Len: 7 Data: 0x2 0 FF EF FE 0 C
ID: 0x29C Len: 8 Data: 0x0 0 0 0 FF FF 0 0
ID: 0x354 Len: 8 Data: 0x0 0 0 0 0 0 0 0
ID: 0x212 Len: 6 Data: 0xFE 3D C0 50 0 0
ID: 0x1F8 Len: 8 Data: 0xF4 4 FF EF FE 0 E D
ID: 0x17E Len: 8 Data: 0xFF FF FF 0 FF 40 0 FF
ID: 0x18A Len: 6 Data: 0xFF F0 0 6 40 0
ID: 0x439 Len: 2 Data: 0x3E 80
ID: 0x3F7 Len: 2 Data: 0x8 0
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Jun 29, 2015, 11:53 pm
Hello yoh-there,

I don't know about Renault but some vehicle/engine manufacturers such as Caterpillar keep their CAN IDs matrices for themselves (proprietary) in order to sell their (or authorized OEM) diagnostic hardware/software tools. Even when most of the parameters comply with the SAE standards, manufacturers tend to add software layers (like DataLink) and protocols with additional features embedded to the raw Can data, making more difficult its decoding.
It is a different case for example, with Toyota, that most of their IDs are open/public and can be found everywhere.
I googled a bit and found a small Fluence ZE PID's table that includes some of the PIDs that you got. I hope this can help you as a start.  Here the link:

http://www.qsl.net/ct1gvn/fluenceze.htm (http://www.qsl.net/ct1gvn/fluenceze.htm)

For those interested in OBD2 PIDs (used by Toyota) here the Wiki link:

https://en.wikipedia.org/wiki/OBD-II_PIDs (https://en.wikipedia.org/wiki/OBD-II_PIDs)

Regards,

p
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Jun 30, 2015, 01:02 am
Thank you Palliser for your links. And all the hard work on the library of course! I am already in contact with Alexandre who mad that Matrix and I can already tell you some data I sniffed from my Zoe looks unsurprisingly familiar to what he found on his Fluenze.

What also will be helpful that the hardware I build has a ODB2 T-splitter cable, so I can actually sniff the bus while I can also hook up an iCarsoft 970. I will try to get that done soon.
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Jun 30, 2015, 12:36 pm
Allow me a quick update and some "bootstrap" help for beginners.

Hardware: The Due (or the Taijiuino that I have) is a wonderful thing. You only need to add a CAN tranceiver (something like this http://www.ebay.com.au/itm/131537907713 ) and hardware-wise, you're done. The 13.8 volt from any ODB2 can go directly into VIN and the board will supply the 3.3V for the tranceiver(s). I added a picture of my quick-build.

CAN interface: I got a CAN breakout, tossed the box and used the T-cable, to give me super flexibility, i.e. log while the pro's are working on my car! Something like this http://www.aliexpress.com/item/OBDSHOP-OBDII-Breakout-Box-Tester-Pin-Out-Diagnostic-Pinout-OBD2-Pin-Out-BOX-Auto-Diagnostic/32267320483.html

IDE: You have to tweak the 1.6 IDE a bit to make it understand the DUE board. Here is a good generic guide https://www.arduino.cc/en/Guide/ArduinoDue and this is to install the support https://www.arduino.cc/en/Guide/Cores

Ports: If you are used to AVR arduino's, I would advice to stick to using the native port only. Make sure you select Arduino Due (Native USB Port) as board. All examples  in the CAN library use the native port by default anyway (see the "#define Serial" line).

Logging: My hardware does not log to i.e. SD yet (might add that later), so I simply push all data to the Serial (USB) port. I use my laptop as a logger. On Ubuntu, may I suggest to use minicom ( minicom -D /dev/ttyACM0 ). If you've never used it, just remember it uses Ctrl-A to escape. Then Z for help, or X to quit and, very important L for loging (capture).

I will do a full trip log on the Zoe today, and match that with the field Alexandre figured out for the Fluence. More to follow.
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Jun 30, 2015, 12:40 pm
Photo was to big, retry
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Jul 05, 2015, 07:49 pm
Major progress! Logging data from the Zoe to the laptop and interpreting it later, or, as the Due is powerful enough, interpret it right away in real time. The database, defining which bits in which bytes in which frame IDs means what is growing rapidly. There are way over 50 datastreams! But I have already decoded about 60 fields. It is daunting but fun. Most credits go to you guys for building the CAN library and Alexandre for his tedious work on the Renault Fluence.

It seems there are indeed a few (but not many) higher layer protocols, as I see see some frames changing all the times very, very rapidly. It might have to do with TCP/IP encapsulation, not sure.

If anyone finds this thread because he's looking on finding what's going on in the Renault Zoe's CANbus, just chime in. Also, I am happy to share the code that runs on the Due (Arduino IDE) and my laptop (gcc on Ubuntu), which you can use as a kick-start for logging other vehicles.

Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Jul 06, 2015, 03:31 pm
Hello yoh-there,
I am very happy for your progress with your Due/Zoë project using the CAN library. I also hope that at some point of the time you can find/get the CAN matrix of the Zoë in order to save effort/time. I think that it is easier for you you grasp more information there than us here. Anyway, in the meantime let me give you one tip. Try to get (may be through ebay) a cheap version or a used Renault CAN Clip Diagnostic Interface V139. Then, using Due, you can read all the traffic between the clip and Zoe. The idea is to compare data so you can figure out more parameters. It's just a though. Regards.

p
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Jul 07, 2015, 12:06 am
Thank you Paliser!

A "real", as in , official matrix, well, I don't see it happening to be honest. But I hope some people will chime and and find more fields.


As far as I know, the original CLIP tools only work with the Zoe when they can "phone home". However, I have already planned to do what you suggest, but only slightly different. Next Saturday I will visit a guy who has bought a Carsoft i907, it is a diagnostic tool with specific firmware for the Zoe and Fluence (another EV of Renault).

And since I build the Due with a CAN T-cable I can indeed sniff what is going on on the bus(ses) when we interrogate i.e. the individual cell voltage. So yes, great advice. More next week!
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jul 07, 2015, 02:54 pm
Well, if you want to create a database of the signals you are finding then a program I'm writing will soon be of use to you. I have some programs at my github repos (https://github.com/collin80) that could be useful. GVRET is an Arduino sketch that uses due_can and is *much* more advanced than the bundled example sketches that come with the due_can library. I have tested GVRET up to around 4000 frames per second without any dropped frames. The PC side complement to that is SavvyCAN which is a QT program you can compile on Windows, Linux, or OSX. That program is soon to support full DBC file creation so that you can create a database of signals in an industry standard format and then use that database to automatically interpret the incoming frames (or frames you loaded from a file). So, if any of that is useful to you then have at it - the source is all open. I'd love to see people making DBC files of their work since that way lots of people can collaborate and make use of the work.
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Jul 07, 2015, 05:44 pm
OY! I should have spotted that earlier Collin! I already did a logger and formatter and a kinda-sorta database myself. :-/

I will dive into what you made, THANK YOU!

And how the DBC format works, as I already (mostly nicked from Alexandre BTW) have about 60 parameters. Learning every day :-)
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Jul 08, 2015, 07:46 pm
@Colin80, I am really sorry to sidetrack a bit here, but the SavvyCAN.... it is my first delve into QT and I have some problems building it. It complains about seriaport. Can you please tell me what version of QT you are using? I installed 5.0.2 and that might be the culprit, as I read somewhere it is only included starting 5.1. But before I mess up my system further...... Thank you so much!

Edit: Ehm, with 5.5 (yes I did) the qmake is ok but on the make I get issues on #include <QtGui/QAction>.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jul 08, 2015, 08:17 pm
I build it with QT 5.4. But, as luck would have it, I routinely publish binaries of it too:

www.kk-ev.com/SavvyCAN.zip         (Windows 64 bit)

www.kk-ev.com/SavvyCAN.tar.gz     (Linux - Does not include any QT libraries so you'd have to install them from your linux repo)

www.kk-ev.com/SavvyCAN.dmg        (OSX - I believe version 10.7 or newer)
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Jul 09, 2015, 09:51 pm
Thank you! Still fighting with that, but I'll get there! I will let you know.
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Jul 10, 2015, 03:37 pm
Dear @Collin80,

I got it working, and that surely looks good. Very impressed with what you did. And I had a quick start with your item on evtv. Can I ask a few questions here?

GVRET: I added the SD card to the DUE. GVRET now stops complaining abot not being able to log. Is it supposed to create a log when run without anything hooked up to the USB? If so, how does it deal with closing the file? I mean, at one moment or the other I have to pull the plug!

My own logger/display/database (a faint blip compared with what you did) I rewrote slightly to use the same packet format as GVRET both for logging as well as displaying. Mine, though simply terminal based, might draw the eye a tad more to any bit somewhere changing. If I still think that's true after fiddling a bit, I will try to add some code at your discretion of course. But let me learn SavvyCan more first before I pound my chest :-)

As for defining fields, what do you recommend while DBC editing is not available yet? The tool that Victor distributes? Notepad-style? In the latter, do you have maybe a link to a quick-start guide? In essence I already have ID, bitx-to-bity (from 0-64), divisor, multiplier and value offset for roughly 60 fields. Which node is sending I don't know, but I can throw that in the "catch all" bin.

Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jul 10, 2015, 08:17 pm
GVRET: I added the SD card to the DUE. GVRET now stops complaining abot not being able to log. Is it supposed to create a log when run without anything hooked up to the USB? If so, how does it deal with closing the file? I mean, at one moment or the other I have to pull the plug!
It periodically flushes the file so when you're done logging you can just power off. At the worse your file will be missing the end few frames.

Quote
As for defining fields, what do you recommend while DBC editing is not available yet? The tool that Victor distributes? Notepad-style? In the latter, do you have maybe a link to a quick-start guide? In essence I already have ID, bitx-to-bity (from 0-64), divisor, multiplier and value offset for roughly 60 fields. Which node is sending I don't know, but I can throw that in the "catch all" bin.
Well, you can use Vector's DBC editor. It should be essentially free to use - you just have to download a trial of one of their products. The trial will expire but the DBC editor never should.
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Jul 10, 2015, 09:09 pm
Thank you Collin. I noticed no file at all, but it might have to do with some defaults. I am a bit lost on how to update the EEPROM settings. I will be rude and for the moment just update the defaults in my source copy.

DBC editor: Got, will give that a spin.
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Jul 12, 2015, 06:53 pm
More success. The T-cable proved invaluable and we sniffed what was going on between the Zoë and an iCarsoft 907. As expected, some data was being actively queried by the tool. With partly my own software and partly SavvyCan we were able to see what was going on and map what the tool was reporting on some raw data. I can now query the LBC for it's 12 temperature sensors in the battery compartment, and the EVC for i.e. current drawn/fed and the maximum charging power it will accept given the SOC and temperature of the battery pack.

Some data span multiple packets and thatwas, erm, interesting!
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Jul 31, 2015, 11:11 am
Here is a final update for all interested.

The Due proved a really good choice as this car is producing chatter in excess of 1600 frames per second on the main CANbus only and there is no way a smaller micro-controller can handle that, let alone push it out over real UART serial 115200.

On the application level we (yes, we, the Zoe "hacking" team is growing) seem to be moving to using GVRET as capturing tool and CRTD as logging format. I am very happy with this, as it allows us to concentrate on the real unknowns better.

On the analysing side we use SavvyCan and loads of small programs to figure out problems and challenges as they arise. Here, individual skills are more leading. Java. C. C++. QT. Swing. And even VT100 emulation for quick and dirty stuff. Mostly, we are climbing up the tree of the higher level protocols, such as ISO-TP and UDP. I hope I can contribute a bit to SavvyCan on that as some sort of repayment, though my QT skills are, well, non-existent. Let me work on that!

As most of these things though are both pretty car-specific, as well as beyond the realm of this thread, I think posting here would not be appropriate. For those interested in "hacking" into the Renault Zoë (and Fluence) and ending up here, feel absolutely free to PM me.

Things work absolutely excellent with the due_can library and once again my sincere gratitude goes out to all people contributing to the tools mentioned for making this all happen. Thank you!!
Title: Re: Building a CAN API for Arduino DUE
Post by: marooned on Sep 23, 2015, 10:42 pm
I'm working on a project where I need to capture the timestamp for the incoming CAN frames. I'll only be monitoring about 15-20 ID's at a time. Each ID could (potentially) have a period of 10ms. Any suggestions on where to get started?
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Sep 29, 2015, 04:04 pm
@marooned: I think you're looking at work on top of this library.

gtithub.com/collin80/GVRET and gtithub.com/collin80/SavvyCan
Title: Re: Building a CAN API for Arduino DUE
Post by: Rich_P on Nov 23, 2015, 05:50 pm
Good evening All,

I have been using the library from Git with no issues until I updated to IDE 1.6.4, now I seem to get the following error :


arm-none-eabi-gcc: error: C:\Users\RICHAR~1\AppData\Local\Temp\build8906646598289652614.tmp/core/syscalls_sam3.c.o: No such file or directory
arm-none-eabi-gcc: error: C:\Users\RICHAR~1\AppData\Local\Temp\build8906646598289652614.tmp/core.a: No such file or directory
Error compiling.

I have installed the due core in the IDE which improved errors, but im stumped.

Any help would be great

Thanks
Rich
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Nov 23, 2015, 09:50 pm
Rich, that doesn't appear to be related to the due_can library itself. It looks like it isn't properly copying over the Arduino core and a Atmel support library. This leads me to believe that perhaps something went wrong with the installation of the Due core files. You might try erasing them and redownloading. Or, maybe upgrade to 1.6.6 which installs files in a different place anyway and might fix the issue.
Title: Re: Building a CAN API for Arduino DUE
Post by: yamakasi on Jan 12, 2016, 11:40 am
can somebody explain me this schematic please?

http://img24.imageshack.us/img24/5060/canobd22.jpg

Can i take this schematic if i want to read CAN-Data in the car?
Title: Re: Building a CAN API for Arduino DUE
Post by: rockwallaby on Jan 12, 2016, 12:18 pm
yamakasi,
Your link is 404 Not Found
Please ensure your links work before posting, and test after posting and also, use the 'insert link' tags to post links on the forum.
____
Paul
Title: Re: Building a CAN API for Arduino DUE
Post by: rockwallaby on Jan 12, 2016, 12:23 pm
The click-able link is;
https://img24.imageshack.us/img24/5060/canobd22.jpg (http://img24.imageshack.us/img24/5060/canobd22.jpg)

What specifically do you want explained ?
That circuit is very clear and concise to me.

The circuit shows the connection to an ODB2 connector for J2284, is that what you want ?
____
Paul
Title: Re: Building a CAN API for Arduino DUE
Post by: yamakasi on Jan 12, 2016, 03:31 pm
Sorry for the not working link.

I'm new in the µC-world and that circuit ist not clear to me. I'm not sure why we need the jumpers, resistors and capacitors after the transceivers.

Does this circuit (the part till CAN0 and CAN1, of course without the connector) depend on my connector and my CAN-Signal? Can I use this circuit also If want to connect the DUE to a D-Sub connector (9 pins)? do I have to take other values for the resistors and the capacitors?

I hope you understand my question. sorry for my english.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jan 12, 2016, 03:59 pm
None of the links offered seem to work for me. I had to google for canobd22.jpg and found it elsewhere. Anyway, the resistors and caps are correct for basically any canbus connection. Here is the quick version:

CAN requires what are called "termination resistors". On each far end of the bus you have 120 ohm resistors. This gives the bus 60 ohms of resistance. If your bus already has two resistors then you don't need a third. That is why the jumpers are there. You close the jumpers if you need the resistor and open them if you don't. The capacitors have nothing to do with canbus. They are there for noise suppression on the incoming power wire and are thus found on all sorts of circuits.

So, trust the schematic. It's correct and how things should be hooked up.
Title: Re: Building a CAN API for Arduino DUE
Post by: yamakasi on Jan 12, 2016, 04:18 pm
Thank you very much collin80!!!

How are the values of the capacitors calculated? And can I take any 120ohm resistor and 10uF/100nF capacitor or do I have to take special types?
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jan 12, 2016, 08:45 pm
For the resistor basically any resistor that can handle the wattage will work. It's around 4v or so max between CANH and CANL so that would require about a 4 milliwatt resistor. Thus, a little 1/10 watt resistor would have plenty of headroom. CAN isn't that terribly fast or anything so I've never seen anyone be too overly careful picking the resistor. Except, sometimes people use two 60 ohm resistors with a 0.01uF cap between them and ground. It looks like this:

CANH---R------R---CANL
                 |
                 C
                 |
                GND

This has better noise immunity and you get a bit better waveform. It's up to you - doesn't really matter a lot but pretty well any professional design will go with two 60 ohm resistors and a cap.

The 10uF cap and 100nF caps are just run of the mill caps. There are references on the net about how to pick the right cap but basically just find something that can handle a bit more than the voltage you're using. So, anything rated for more than 5V would probably work fine. This has nothing to do with canbus so really you just have to find something on the net that explains power decoupling. The general rule of thumb for power decoupling caps is that you use a 1-10uf cap in parallel to a 0.01-0.1uf cap. The exact capacitance values are generally not that super important. Having said that, chip makers will generally tell you the approximate values they would use within a schematic you'll find in their datasheet. If you don't know any better then just use what they tell you to use.
Title: Re: Building a CAN API for Arduino DUE
Post by: yamakasi on Jan 19, 2016, 04:34 pm
thank you collin80!

if i want to use the SN65HVD235 can i also take this schematic and the uploaded can library?
Title: Re: Building a CAN API for Arduino DUE
Post by: djkrugger on Jan 19, 2016, 09:09 pm
Thanks everyone for the great work, i was using my own software but i tried SavvyCAN and it's great! now my problem is that i'm using a Win32 machine so i have it running inside QTcreator which i have no idea how to use it to create a windows executable.
 Does anyone have done it and would be so kind to share it?
THANKS!!
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jan 20, 2016, 04:55 pm
thank you collin80!

if i want to use the SN65HVD235 can i also take this schematic and the uploaded can library?
Yes, it should work fine. I actually haven't used that specific version of the SN65HVD chip. You might need to do something to the pins to make sure it doesn't go into auto loopback or standby mode. But, otherwise that transceiver will be fine.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jan 20, 2016, 04:56 pm
Thanks everyone for the great work, i was using my own software but i tried SavvyCAN and it's great! now my problem is that i'm using a Win32 machine so i have it running inside QTcreator which i have no idea how to use it to create a windows executable.
 Does anyone have done it and would be so kind to share it?
THANKS!!
www.savvycan.com has compiled executables for three platforms (windows, osx, linux).
Title: Re: Building a CAN API for Arduino DUE
Post by: djkrugger on Jan 21, 2016, 04:30 pm
www.savvycan.com has compiled executables for three platforms (windows, osx, linux).
Thank you Collin, i got that but it seems to be for Win64.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jan 21, 2016, 10:01 pm
Oh, I'm sorry... I failed to read your post properly... You're right, I never compiled it for anything other than 64 bit operating systems. I suppose that maybe there isn't any reason I couldn't make a Win32 executable as well. I'll try to get such a build for you.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Jan 22, 2016, 04:33 am
Ok, there is a 32 bit windows executable now: www.savvycan.com/SavvyCAN-32.zip
Title: Re: Building a CAN API for Arduino DUE
Post by: djkrugger on Jan 22, 2016, 07:50 pm
Ok, there is a 32 bit windows executable now: www.savvycan.com/SavvyCAN-32.zip
Thank you so so much!!
Title: Re: Building a CAN API for Arduino DUE
Post by: yamakasi on Jan 27, 2016, 03:55 pm
Yes, it should work fine. I actually haven't used that specific version of the SN65HVD chip. You might need to do something to the pins to make sure it doesn't go into auto loopback or standby mode. But, otherwise that transceiver will be fine.
To which pin of the DUE do i have to connect pin5 of the SN65HVD235/SN65HVD234?
And can i also connect pin1 of the transceiver to PC13 instead of PB14?
Title: Re: Building a CAN API for Arduino DUE
Post by: briantee on Jan 28, 2016, 04:24 pm
Has anyone tried hooking this up and using it with LabView? I'm still waiting for the Canbus shield and Due to arrive but thought i would ask ahead of time.

Thank you.
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Jan 29, 2016, 09:01 pm
To which pin of the DUE do i have to connect pin5 of the SN65HVD235/SN65HVD234?
And can i also connect pin1 of the transceiver to PC13 instead of PB14?
Hello yamakasi,

Here a schematic of the Due and CAN transceiver connection.

(http://s22.postimg.org/mkk3v1y5t/Transceivers_Due_CAN.jpg)

PC13 is not related at all with CAN controller.

-p
Title: Re: Building a CAN API for Arduino DUE
Post by: yamakasi on Feb 01, 2016, 05:51 pm
Hello yamakasi,


PC13 is not related at all with CAN controller.

-p
Hello Palliser,

sorry for that stupid question :( i didn't think about the can controller...

But to which pin do i have to connect pin5 of the SN65HVD235/SN65HVD234 transceiver if i maybe want to use Autobaud Loopback/Sleep mode later?
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Feb 02, 2016, 05:55 pm
Hello Palliser,

sorry for that stupid question :( i didn't think about the can controller...

But to which pin do i have to connect pin5 of the SN65HVD235/SN65HVD234 transceiver if i maybe want to use Autobaud Loopback/Sleep mode later?
Hello yamasaki,

No need to apologize.

Regarding the further use of pin 5, sleep mode (234) and autobaud loopback mode (235), you can use any digital pin available but you have to create your own control code for that pin 5.

Remember also to consider the pin 8 that provides extra features like high speed/low power/and slope control mode using external resistor.

I can't tell you from the top of my head if Collin's libraries include pins 5 and 8 as my original library did.

Here a link to the mfr specs with more information about the transceivers:
http://www.ti.com/lit/ds/symlink/sn65hvd234.pdf (http://www.ti.com/lit/ds/symlink/sn65hvd234.pdf)

Good luck,

-p
Title: Re: Building a CAN API for Arduino DUE
Post by: yamakasi on Feb 04, 2016, 03:58 pm
Regarding the further use of pin 5, sleep mode (234) and autobaud loopback mode (235), you can use any digital pin available
Can I take PC12 and PC13?

...but you have to create your own control code for that pin 5.
If I don't want to use sleep mode/loopback can I take for example your libraries and just set PC12 and PC13 to 3.3V? or is it better to take a jumper to choose?

Remember also to consider the pin 8 that provides extra features like high speed/low power/and slope control mode using external resistor.
I know, I want to build my shield with a jumper where I can choose between connecting pin 8 to GND, VCC(3.3V) or to a 10k Ohm resistor and then to GND.
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Feb 04, 2016, 05:34 pm
Yep. You can use PC12 and PC13.

Regarding the use of jumper, I rather prefer to use code.

I have attached for you the sn65hvd234 library with several functions. It work OK for 234 but I haven't use it for 235. You can set your pins depending on your need. For example, if you want to set PC12 as Rs (pin 8 of 234) to 3.3V and set PC13 as EN (pin 5 of 234) to 3.3V do as follows:

In the setup()

Add -> Init() to initialize to GND (default) your control pins.
Add -> SSN65HVD234(51, 50) to set your control pins PC12 and PC13

In the loop()

Use -> Enable() to enable the transceiver.
Use -> SetRs(51) to initialize the transceiver control pin 8 to 3.3V.
Use -> SetEN(50)  to initialize the transceiver control pin 5 to 3.3V.

If you use Disable() you set loe LOW the EN pin, etc.

For the rest of the functions (enable/disable low power) take a look at the library. If you understand it, you can even change it to accommodate it to 235.

I really haven't touch this transceiver library (I ported from Atmel to Due) lately, thus, try to play with it ans see how it goes. 

-p
Title: Re: Building a CAN API for Arduino DUE
Post by: Mplex43 on Feb 08, 2016, 05:44 pm
www.savvycan.com has compiled executables for three platforms (windows, osx, linux).
SavvyCan is a beautifull program , inpressive
Very handy for searching CAN signals.
Only thing is ,I wonder how to get the .DBC working,
When try met GVRET logfile and bms.DBC   , I do not see a way to load the dbc ,
it never state "dbc datafile loaded"
Title: Re: Building a CAN API for Arduino DUE
Post by: XAIN on Feb 18, 2016, 08:41 pm
I am new to arduino and i have been given a task to read the can data on arduino using sn65hvd234. can any one please guide me what are the necessary things which i need to have to complete the task. sn65hvd234 is a transceiver can i access the can data with out using any other ic with the help of a link layer program onboard arduino. please help. thank you.
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Feb 19, 2016, 02:02 am
I am new to arduino and i have been given a task to read the can data on arduino using sn65hvd234. can any one please guide me what are the necessary things which i need to have to complete the task. sn65hvd234 is a transceiver can i access the can data with out using any other ic with the help of a link layer program onboard arduino. please help. thank you.
Hello XAIN:

Software:
Use the following library with examples.

https://github.com/collin80/due_can (https://github.com/collin80/due_can)

Hardware:
Use the schematic shown in post #526 that only uses the transceiver, caps and resistors.

-p


Title: Re: Building a CAN API for Arduino DUE
Post by: yamakasi on Feb 24, 2016, 06:18 pm
thank you so much for your help Palliser :)

Regarding the use of jumper, I rather prefer to use code.

ok for high speed mode and low power mode i will use code, but for choosing between high speed/low power and slope control i have to use a jumper because of the resistor or am i wrong?
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Feb 26, 2016, 03:45 am
thank you so much for your help Palliser :)

ok for high speed mode and low power mode i will use code, but for choosing between high speed/low power and slope control i have to use a jumper because of the resistor or am i wrong?
Haven't implemented the slope mode yet but yes, in order to add the resistor and/or potentiometer to control the slope time response you have to implement it using a jumper as a straight solution.

-p
Title: Something different - ported CAN API for Arduino DUE to support ATmega64M1
Post by: thomasow on Mar 06, 2016, 05:37 pm
Hello.

Sorry to side track this forum a bit, but I wanted to make it known that I have taken the due_can lib and ported its API to support other Atmel CPUs with built in CAN controllers - specifically the AVR line. The target CPUs include:




The port is posted on GitHub at: https://github.com/thomasonw/avr_can (https://github.com/thomasonw/avr_can)

During this effort I did trim out several of the API calls, those which appeared to be very chip specific (e.g., ones that returned the internal content of a CPU register) - but I am hoping the ones left are truly the viable ones.

I would appreciate any comments from folks on this, and specifically around the APIs which have been ported over vs. ones left behind.   To date I have only tested using the ATmega64M1, and rather limited testing at that; using the examples in combination with a Due on the other end.  As issues are found I will be updating this library.

Title: Re: Building a CAN API for Arduino DUE
Post by: yamakasi on Apr 20, 2016, 07:31 pm
sorry but can someone help me please??? im reading the howtouse file and the exaples since days  :'(
i can't find out how to use the library...

in my project i have to get data from the can-bus every 0,005s (cycletime of the frames are between 1s and 0,01s) and do some calculation...

my question is how can i get data from 5 different can-frames (5 different can-ids) at the same time? and how do i know if these data are actual (or atleast the newest) and not some buffered data from the past???

it would be so great if someone could help me. thank you!!!
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Apr 21, 2016, 02:17 am
Your best is to start simple and work up to more complicated things. Start with the CAN_TrafficSniffer example that comes with the library. Change the CAN speed(s) to match your bus. See if you can see traffic coming through. If you do then your hardware is working and you can proceed to complicate the sketch a bit. If not then you need to start with getting the hardware to work first.

Now, assuming you do have working hardware, the frames are received automatically and saved for you. the available() function will tell you if the library has some recieved frames for you. You never get old frames, only new frames so you needn't worry about that. If you try to read frames and there aren't any you'll just get null data back.

See in that example sketch where there are calls to setRXFilter? You use that function to tell the library which IDs you want to know about. Setting up filters can be complicated but if you only have 5 messages you need you can do this:

Code: [Select]

Can0.setRXFilter(0, 0x7FF, ID1, false);
Can0.setRXFilter(1, 0x7FF, ID2, false);
Can0.setRXFilter(2, 0x7FF, ID3, false);
Can0.setRXFilter(3, 0x7FF, ID4, false);
Can0.setRXFilter(4, 0x7FF, ID5, false);


Where obviously ID1 through ID5 are the actual frame IDs you care about. Then only those will get through (so long as you don't let it run the code that originally used the setRXFilter function. That code sets them all to accept all frames)
Title: Re: Building a CAN API for Arduino DUE
Post by: yamakasi on Apr 21, 2016, 10:54 am
thank you again collin!!!

i have already tested the hardware with just one id with watchFor() and it does work.

now i wanted to try this test this:

Code: [Select]

Can0.setRXFilter(0, ID1, ID1, false);
Can0.setRXFilter(1, ID2, ID2, false);
Can0.setRXFilter(2, ID3, ID3, false);
Can0.setRXFilter(3, ID4, ID4, false);
Can0.setRXFilter(4, ID5, ID5, false);


is this wrong? i thought this would get the IDs i want through...

and if i want to save something from the IDs in a variable i have to write a function like this

Code: [Select]

void gotFrameMB0(CAN_FRAME *frame) {
    variableX = frame->data.byte[0] - 40;
  };


and everytime i want to get the data (actual value of variableX) in my code i have to make a callback like this?

Code: [Select]

Can0.setCallback(0, gotFrameMB0);
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Apr 21, 2016, 02:32 pm
Yes, technically using Can0.setRXFilter(0, ID1, ID1, false); would work but it has the potential to allow more frame IDs through than you really want. That second parameter is a mask and if you don't set the mask to 0x7FF you could get multiple frame IDs through the filter. But, it might not depending on the IDs you are actually seeing.

The rest of your post is one potential way to write the code. That way causes you to be immediately notified the instant a frame that matches filter 0 is received. You'd then get a call to your function and could process the frame.

The other way is to use Can0.available() to see how many frames are available and then Can0.read(frame) to grab the next available frame. This has both advantages and disadvantages. The advantage is that you are then able to process traffic when you're ready to do so. It'll wait for you until you get around to checking. This has the side effect of not interrupting what you were doing. But, you then have to manually figure out what to do with the frame based on its ID. The way you proposed in your message instead immediately interrupts what your sketch was doing to process the frame. But, it also is keyed to the filter so you presumably already know exactly which message causes the filter to be satisfied and thus you don't have to worry so much about figuring out which frame you just got - you already know.

It's up to you which way you use. A word of warning though - you can use both at once but any frames that were dispatched with callbacks will not be available with available() or read(). They're dispatched via the callback and then discarded.
Title: Re: Building a CAN API for Arduino DUE
Post by: yamakasi on Apr 21, 2016, 05:27 pm
sorry i am a little confused, maybe because off my bad english....

in my project i am using an extended kalman filter with a sampletime of 5 ms. therfore i have to get exactly every 5 ms actual data (engine speed and torque, engaged gear, temperature,...) from the can-bus. i dont want to wait until a frame comes in. if ther is no data available i have to take the last available data again until the next time step, and so on... But i also dont want to be interrupted in the middle of my calculation. i have for example two functions: void getCAN() and void kalmann(). i want to have a routine of these to functions. every 5ms i want to begin with getCan() which saves the actual data i need from the can-bus into variables and then the function kalmann() which do something with this variables (of course the function kalmann() is faster then 5ms).

so i think i have to go the other way with Can0.available() and Can0.read(frame). am i right?
and if it is right how would my code look like?
first
Code: [Select]

Can0.setRXFilter(0, 0x7FF, ID1, false);
Can0.setRXFilter(1, 0x7FF, ID2, false);
Can0.setRXFilter(2, 0x7FF, ID3, false);
Can0.setRXFilter(3, 0x7FF, ID4, false);
Can0.setRXFilter(4, 0x7FF, ID5, false);

and then?
do i have to use Can0.read(frame) in my function getCan()? i dont understand how i can use Can0.read(frame) for 5 different frames.

i am sorry for all these stupid questions, the problem is that my project is for the university and dont have much time left and it is difficult to get access to the car for testing my code...
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Apr 21, 2016, 08:08 pm
The new filter code is correct. That will allow just those 5 messages through. Probably you do want to use available and read so that the callback doesn't happen in the middle of your kalmann function. Otherwise what could happen is that your kalmann function would start processing with the old data then midway through a frame comes in and the callback changes the data so that the rest of your kalmann function starts to use the new data. That is not normally a very desirable thing to have happen.

So, available and read it is. You'd have a getCAN function that looks something like this:

Code: [Select]

void getCAN()
{
   CAN_FRAME incoming;
   while (Can0.available() > 0)
   {
       Can0.read(incoming);
       switch (incoming.id)
       {
           case ID1:
           //do stuff
           somevar = incoming.data.byte[1]; //grab some data from this frame
           break;
           case ID2:
           //do stuff
           break;
           case ID3:
           //do stuff
           break;
           case ID4:
           //do stuff
           break;
           case ID5:
           //do stuff
           break;
       }
   }
}


The while loop will cause it to grab any waiting frames. If there are none you don't wait. You just plain keep going. If there are some then the switch will figure out which frame it was and you can process based on which ID it had. Then, you'd have variables that you update in this getCAN function and the kalmann function would use those variables. Perhaps they're global variables, perhaps you use an object and make this C++, doesn't matter.

Not to confuse you worse, but an alternative approach is to still use callbacks, one for each filter, and update the variables in there. Then, in your kalmann function you'd copy the value of all of those variables as quickly as you can and then use the copies. That provides limited time for the data to change while you're copying. It's not totally safe but pretty close. You can even make that safe by turning off interrupts while you copy and then turning them back on after you copy. But, if this is confusing just ignore this paragraph.
Title: Re: Building a CAN API for Arduino DUE
Post by: yamakasi on Apr 21, 2016, 09:50 pm
that means, that after saving the frames in a variable the frames will be cleared? i mean if not i would never get out of the while loop  :D

yes, they are global variables.

i think i will first try it without a callback...

and a general question: what do you think is the best way to ensure that my code repeats every 5 ms? ISR?
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Apr 22, 2016, 04:51 am
To ensure an even 5ms use a timer library. I suggest this:

https://github.com/ivanseidel/DueTimer
Title: Re: Building a CAN API for Arduino DUE
Post by: CharlieHdz on Apr 27, 2016, 07:49 pm
Hi everyone, Thank yall for your enthusiames and great work, I have been following this thread since a long time, but I'm sorry to say it, it makes me hard to understand some issues i found working with CAN and your CAN library. Let me explain myself.

I have a Arduino Due with the um SAM3XBE Actually I'm trying to achieve a working with the examples published in your library (more specifically the CAN_SendingTest) but actually I dont get a successful job.


1.- I have a MCP2562 transceiver...Is it will work?

2.-In the example of CAN_SendingTest I just put some println and It seems that the function Can0.available() isn't working (return 0 all the time), the sending frames works (returning 1), and I don't have any idea why is available() not working...Is there some trick?? Are there some connections missed??

3.- I wanna to know the raw/hexadecimal values inputs/outputs of the pines CANTX and CANRX (there are DIgital or Analog, what's the values sizes in bytes and if there is a specific type, but I dont get it already how to do it, Would you like to illustrate me a little?

Thank you for your attention  and I hope yall are having a good day.

Carlos Hdz
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Apr 28, 2016, 05:05 am
Hi everyone, Thank yall for your enthusiames and great work, I have been following this thread since a long time, but I'm sorry to say it, it makes me hard to understand some issues i found working with CAN and your CAN library. Let me explain myself.

I have a Arduino Due with the um SAM3XBE Actually I'm trying to achieve a working with the examples published in your library (more specifically the CAN_SendingTest) but actually I dont get a successful job.


1.- I have a MCP2562 transceiver...Is it will work?

2.-In the example of CAN_SendingTest I just put some println and It seems that the function Can0.available() isn't working (return 0 all the time), the sending frames works (returning 1), and I don't have any idea why is available() not working...Is there some trick?? Are there some connections missed??

3.- I wanna to know the raw/hexadecimal values inputs/outputs of the pines CANTX and CANRX (there are DIgital or Analog, what's the values sizes in bytes and if there is a specific type, but I dont get it already how to do it, Would you like to illustrate me a little?

Thank you for your attention  and I hope yall are having a good day.

Carlos Hdz
Hello CharlieHdz,

I think that your problem is your transceiver MCP2562 given that its operating voltage range is from 4.5 to 5.5V. Arduino Due works with 3.3V, thus, I'd recommend you to use a suitable transceiver like the Texas Instrument SN65HVD23X series. best regards,

-p
Title: Re: Building a CAN API for Arduino DUE
Post by: djkrugger on May 09, 2016, 11:09 pm
Collin, is there any way to add 83.3Kbps or "custom baudrates" to SavvyCan?
 Again thanks for your great work.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on May 10, 2016, 02:10 am
hmmm, probably. I'll add it to the list of things to do. I can't promise that any custom baud rates will work though. Some data rates will probably be tough to match. To get a certain baud rate you have to have some way to divide the processor clock by a certain divisor and then come up with a set of bit times for stuff like propagation time, segment 1, segment 2, SJW, etc. The due_can library will try to come up with good values here but it's always sort of a crapshoot with new speeds. But, 83.333kbps is pretty close to an even divisor on a 12mhz clock so it should be OK.

I should probably mention that you can set any baud rate from 1 to 1 million at the gvret console though. So long as you don't then try to set baud rates in savvycan you'll retain whatever oddball speed you had set.
Title: Re: Building a CAN API for Arduino DUE
Post by: MoncefM on May 11, 2016, 12:04 pm
Hello everyone,

I'm using the Arduino Due to read and send some CAN messages using this library https://github.com/collin80/due_can (https://github.com/collin80/due_can) via 2 SN65HVD235 transceivers for each CAN channel, but I'm currently having two weird issues and can't make things work correctly.

My project uses a Windows application that communicates with a test bench by CAN messages (using MUXy Interface from USB to CAN). So the testbench send periodically (each 10ms) different frames (with same data but different checksum) and I can see that using the software CANalyzer.

The problem is that when I try to catch these frames with my Arduino it keeps receiving the same frame, with same checksum. And when I plug the CANalyzer in the bus and start recording it shows correctly the periodic different frames in the Arduino !!  :o  (I just can't figure out what's happening)

My second problem is that I can't send any CAN message using the library. I tried the code bellow and others but none of them worked for me.


Code: [Select]
#include "variant.h"
#include <due_can.h>


CAN_FRAME myFrame;


void setup()
{
  Serial.begin(115200);
  if (Can0.begin(CAN_BPS_500K) != 0 )
     Serial.println("CAN Communication initialized succussfuly");
 

  myFrame.id = 0x31;
  myFrame.length = 6;
  myFrame.extended = false;
  myFrame.data.bytes[0] = 0;
  myFrame.data.bytes[1] = 0;
  myFrame.data.bytes[2] = 0x05;
  myFrame.data.bytes[3] = 0;
  myFrame.data.bytes[4] = 0;
  myFrame.data.bytes[5] = 0;
 
}

void loop()
{
  if(Can0.sendFrame(myFrame))
     Serial.println("Executed");
  delay(1000);
}


And when I plug an oscilloscope to see what's happening I see that CAN_L and CAN_H pins of the transceivers aren't showing any wave form (only around a 2.3V signal) and the CANRX and CANTX pins of the Arduino they're showing the same waveform as the image attached.

Please let me know if you need anything more.

Thanks in advance.
Moncef.



Title: Re: Building a CAN API for Arduino DUE
Post by: duramaxhd on May 11, 2016, 02:05 pm
Hello CharlieHdz,

I think that your problem is your transceiver MCP2562 given that its operating voltage range is from 4.5 to 5.5V. Arduino Due works with 3.3V, thus, I'd recommend you to use a suitable transceiver like the Texas Instrument SN65HVD23X series. best regards,

-p
The MCP2562 will work fine. Yes, it is a 5v transceiver, BUT it does have an extra "reference" Vio supply pin specifically to allow it to work with microcontrollers that run on less than 5v.

So you hook up the Vdd/Vss pins on the MCP2562 to the +5v supply and ground of the Due. Then tie the Vio pin of the MCP2562 to the +3.3v supply of the Due.

Basically that Vio pin is just a signal/reference to the MCP2562 to tell it to level-shift the CAN Rx and Tx logic input/output pins to whatever reference voltage is supplied on that Vio pin.

Ive used MCP2562's with the Due and they work perfectly. They're about half the cost of the "dedicated 3.3v" SN65HVD23X transceiver too... :)

Ben
Title: Re: Building a CAN API for Arduino DUE
Post by: duramaxhd on May 11, 2016, 02:33 pm
Hi everyone, Thank yall for your enthusiames and great work, I have been following this thread since a long 1.- I have a MCP2562 transceiver...Is it will work?
Yes, tie the Vdd pin of the MCP2562 to +5v on the Due board, and then tie the Vio pin of the MCP2562 to the +3.3v on the Due board.

Ben
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on May 11, 2016, 07:35 pm
The MCP2562 will work fine. Yes, it is a 5v transceiver, BUT it does have an extra "reference" Vio supply pin specifically to allow it to work with microcontrollers that run on less than 5v.

So you hook up the Vdd/Vss pins on the MCP2562 to the +5v supply and ground of the Due. Then tie the Vio pin of the MCP2562 to the +3.3v supply of the Due.

Basically that Vio pin is just a signal/reference to the MCP2562 to tell it to level-shift the CAN Rx and Tx logic input/output pins to whatever reference voltage is supplied on that Vio pin.

Ive used MCP2562's with the Due and they work perfectly. They're about half the cost of the "dedicated 3.3v" SN65HVD23X transceiver too... :)

Ben
I continue adopting the idea of spend a little bit on a pure 3.3Vdd proven transceiver for $US1.15 (like ti SN65XX) than to save cents buying cheap +5Vdd devices. If your MCP2562 fails (or gets burnt) you easily could lose your expensive Due (at least the CAN controller section).

-p
Title: Re: Building a CAN API for Arduino DUE
Post by: duramaxhd on May 16, 2016, 03:20 pm
I continue adopting the idea of spend a little bit on a pure 3.3Vdd proven transceiver for $US1.15 (like ti SN65XX) than to save cents buying cheap +5Vdd devices. If your MCP2562 fails (or gets burnt) you easily could lose your expensive Due (at least the CAN controller section).

-p
Good point, I had not thought of that...if something shorts internally on the MCP2562 I guess it could send 5v back to the Due.

However the cheapest SN65xx I can find is SN65HVD232 (no sleep, no slope control) which is $1.85 at Mouser. The MCP2562 is almost a dollar cheaper at $0.98?? This is both for quantity(1).
Title: Re: Building a CAN API for Arduino DUE
Post by: duramaxhd on May 16, 2016, 06:15 pm
Had a quick question...

My project actually needs two high speed CAN busses, and a third single-wire CAN bus.

I need the two high speed busses to mirror each other, basically just act like a gateway to modify some packets.

The Due's two native CAN controllers are doing the high speed mirroring.

To add the 33.33k SWCAN, I added an MCP2515 interfaced via SPI. Based on some data picked out from the high speed CAN mirroring, Im having the MCP2515 then send out some data onto the SWCAN bus.

I have the whole thing working properly.

But now I want to have the MCP2515 also do some simple receiving, and Im having trouble getting it to receive the entire SWCAN message properly.

In the end, I want to have the MCP2515 send a message on SWCAN, listen for a response, and then take a few bytes of the received message, format it into a high speed CAN message, and then send it out on high speed. The tricky part is all of this has to take place while the high speed mirroring continues in the background.

To start from the beginning as simple as possible, I just have the high speed mirroring going in the background (with no message modification, removed to simplify for now) and also have the MCP2515 looking out only for SWCAN ID $647.

When it sees a message coming in on SWCAN $647, it prints it to the serial window.

However, I cant seem to get the MCP2515 to receive anything past the first byte of the message? It just displays the 1st byte and thats it.

For example, if the $647 module on SWCAN sends out "04 03 01 FF 33 00 00 12", the only thing that actually prints to the serial window is


ID: 647  DATA: 
04



Code: [Select]
// Required libraries
#include "variant.h"
#include <due_can.h>
#include <mcp_can.h>
#include <SPI.h>

const int SPI_CS_PIN = 52;
MCP_CAN SWCAN(SPI_CS_PIN);

//Leave defined if you use native port, comment if using programming port
//#define Serial SerialUSB  //KEEP AS NATIVE USB to prevent any potential slowdowns or speed issues with programming port

  unsigned char len ;
  unsigned char rxBuf[8];  // place to store received SWCAN 11-bit message
  unsigned char i;
  long unsigned int rxId;

void setup(){

  Serial.begin(115200);
 
  // Initialize CAN0 and CAN1, Set the proper baud rates here
  Can0.begin(CAN_BPS_500K);
  Can1.begin(CAN_BPS_500K);
 
  //By default there are 7 mailboxes for each device that are RX boxes
  //This sets each mailbox to have an open filter that will accept extended
  //or standard frames
  int filter;
  //extended...ONLY USED FOR 29-bit
  //Can0.setRXFilter(filter, 0, 0, true);
  //Can1.setRXFilter(filter, 0, 0, true);
  //standard...ONLY USED FOR 11-bit
 for (int filter = 3; filter < 7; filter++) {
  Can0.setRXFilter(filter, 0, 0, false);// can0 hooked to the BCM. 
  Can1.setRXFilter(filter, 0, 0, false);// can1 hooked to the truck. 
  } 


    SWCAN.begin(CAN_33KBPS);            // init can bus : baudrate = 33k
        Serial.println("CAN BUS Shield init ok!");   // MCP2515 SWCAN init ok

    SWCAN.init_Mask(0, 0, 0x6FF);                         // SWCAN mask 1 set to $6FF...set all filters and masks the same to avoid any glitches or issues
    SWCAN.init_Mask(1, 0, 0x6FF);                         // SWCAN mask 2 set to $6FF

   
//     set filter, we can receive id from 0x47 ~ 0x47...SET ALL THE SAME to avoide any glitches or issues
//     The only SWCAN message we care about listening for is 0x647
     
    SWCAN.init_Filt(0, 0, 0x647);                          // there are 6 filter in mcp2515, set to $47 so it only receives SWCAN ID $647
    SWCAN.init_Filt(1, 0, 0x647);                          // there are 6 filter in mcp2515, set to $47 so it only receives SWCAN ID $647

    SWCAN.init_Filt(2, 0, 0x647);                          // there are 6 filter in mcp2515, set to $47 so it only receives SWCAN ID $647
    SWCAN.init_Filt(3, 0, 0x647);                          // there are 6 filter in mcp2515, set to $47 so it only receives SWCAN ID $647
    SWCAN.init_Filt(4, 0, 0x647);                          // there are 6 filter in mcp2515, set to $47 so it only receives SWCAN ID $647
    SWCAN.init_Filt(5, 0, 0x647);                          // there are 6 filter in mcp2515, set to $47 so it only receives SWCAN ID $647
   
 
 
}

//================================================================
//================================================================

void SWCANRX(){
Serial.println
("SWCANCHK");

if(CAN_MSGAVAIL == SWCAN.checkReceive())            // check if data coming
    {
        SWCAN.readMsgBuf(&len, rxBuf);    // read data,  len: data length, buf: data buf

        rxId = SWCAN.getCanId();
        Serial.print("ID: ");
        Serial.print(rxId, HEX);
        Serial.println("  DATA:  ");
        for(int i = 0; i<len; i++); 
     {
      if(rxBuf[i] < 0x10)
     {
      Serial.print("0");
     }
        Serial.print(rxBuf[i], HEX);
        Serial.print(" ");   
    }
    Serial.println();
     }
}

//================================================================
//================================================================

void loop(){

CAN_FRAME incoming; 
  if (Can0.available() > 0) { //if message is coming from BCM.
  Can0.read(incoming); //read in can information from BCM
  int canid = incoming.id;  //reads in the decimal id from the can message.
  if ( canid == 481){   // look for flag on high speed CAN to go check SWCAN for $647
    SWCANRX();
  }
  Can1.sendFrame(incoming); //sets message to can1 to be sent out to the truck 
  }
  {
  if (Can1.available() > 0) { // if message is coming from truck to bcm.  we want this to be a clean mirror. 
  Can1.read(incoming);
 
   
  Can0.sendFrame(incoming);
  }
}
}


Title: Re: Building a CAN API for Arduino DUE
Post by: ardler_dan on May 18, 2016, 08:22 pm
Try using the line:
unsigned char len = 8;

I'm looking at doing a similar project utilising a MCP2515 based shield [run at 3.3v] to input a 3rd CANbus channel to the board. I'm currently utilising the 2 on-board CAN channels successfully.

I'm currently utilising a Seeedstudio shield to help prototype and the MCP_CAN_lib library very successfully with an Uno, if I utilise the same library on the Due the shield fails to Rx or Tx any data. The serial display reports "CAN init OK!" everytime but I'm not seeing any data leaving the shield.

Even if I just upload an example style sketch with very basic commands it does not prove fruitful. Does anyone have any ideas?!

Daniel

Title: Re: Building a CAN API for Arduino DUE
Post by: Succubus on Jun 13, 2016, 11:22 am
Hello Daniel,

I have this setup running here at my desk (Arduino DUE [3,3V], SEEEDStudio CAN Shield [5V] and Proto Shield [3,3V & 5V]). On the Proto shield are two CAN transceiver for the two internal CANs of the DUE and also levelshifters for the ISP interface  the 6 pin connector. For Interrupts  I used another pin with a 100 Ohm resistor and the CS to the CAN shield is working without changes. In my opinion are these all changes which are necessary.
Works fine here, too inject some highspeed CAN frames into the lowspeed gateway for what I used the internal Due CANs.

Succubus
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Jun 21, 2016, 05:54 pm
Try using the line:
unsigned char len = 8;

I'm looking at doing a similar project utilising a MCP2515 based shield [run at 3.3v] to input a 3rd CANbus channel to the board. I'm currently utilising the 2 on-board CAN channels successfully.

I'm currently utilising a Seeedstudio shield to help prototype and the MCP_CAN_lib library very successfully with an Uno, if I utilise the same library on the Due the shield fails to Rx or Tx any data. The serial display reports "CAN init OK!" everytime but I'm not seeing any data leaving the shield.

Even if I just upload an example style sketch with very basic commands it does not prove fruitful. Does anyone have any ideas?!

Daniel


Hello Daniel,

As Succubus mentioned, I believe the MCP_CAN_lib should work but remember that the SPI pin connections in the UNO are different in the Due. You have to use the correspondent MOSI, MISO, SCK pins in the Due 2x3 connector instead of the default in the shield (digital 11, 12, 13).

-p
Title: Re: Building a CAN API for Arduino DUE
Post by: yamakasi on Jun 30, 2016, 05:42 pm
Hello everyone,

I have tried to write an ISR with this timer library:
https://github.com/ivanseidel/DueTimer

and my code looks like this:

// global variables
int16_t var0 = 0;
uint16_t var1 = 0;
int16_t var2 = 0;
int16_t var3 = 0;
uint8_t var4 = 0;
double var5 = 0;

// get data from CAN and copy them in globale variables
void getCAN() {
 int16_t var0_CAN = 0;
 uint16_t var1_CAN = 0;
 int16_t var2_CAN = 0;
 int16_t var3_CAN = 0;
 uint8_t var4_CAN = 0;
 double var5_CAN = 0;

 CAN_FRAME incoming;
 
 if (Can1.available() > 0) {
    Can1.read(incoming);
    switch (incoming.id) {
    case ID_0:
      var0_CAN = (incoming.data.byte[0] - 40);
    break;
    case ID_1:
      var1_CAN = (incoming.data.byte[0]&0xF);
    break;
    case ID_2:
      var2_CAN = ((incoming.data.byte[5]<<8 | incoming.data.byte[4]) * 0.25);
    break;
    case ID_3:
      var3_CAN = ((incoming.data.byte[2]<<4 | incoming.data.byte[1] & 0xF0) * 0.5);
    break;
    case ID_4:
      var4_CAN = ((incoming.data.byte[7]<<8 | incoming.data.byte[6]) * 0.0625);
    break;
    case ID_5:
      var5_CAN = (incoming.data.byte[4]<<32 | incoming.data.byte[3]<<16 | incoming.data.byte[2]<<8 | incoming.data.byte[1]);
    break;
    }
 }

 var0 = 0.5 * (var0_CAN + var3_CAN);
 var1 = var1_CAN;
 var2 = var2_CAN;
 var3 = var3_CAN;
 var4 = var4_CAN;
 var5 = var5_CAN - var2_CAN;

}

void ISR() {
 Timer1.detachInterrupt();
 interrupts();
 getCAN();
 noInterrupts();
 Timer1.attachInterrupt(ISR);
}


void setup() {
 Can0.disable();
 Can1.begin(CAN_BPS_500K, 65);  

 Can1.setRXFilter(0, 0x7FF, ID_0, false);
 Can1.setRXFilter(1, 0x7FF, ID_1, false);
 Can1.setRXFilter(2, 0x7FF, ID_2, false);
 Can1.setRXFilter(3, 0x7FF, ID_3, false);
 Can1.setRXFilter(4, 0x7FF, ID_4, false);
 Can1.setRXFilter(5, 0x7FF, ID_5, false);

 
 Serial.begin(250000);
 

 Timer1.attachInterrupt(ISR);
 Timer1.start(20000);
}

void loop() {
 Serial.print("var0: ");
 Serial.println(var0);
 Serial.print("var1: ");
 Serial.println(var1);
 Serial.print("var2: ");
 Serial.println(var2);
 Serial.print("var3: ");
 Serial.println(var3);
 Serial.print("var4: ");
 Serial.println(var4);
 Serial.print("var5: ");
 Serial.println(var5);
}


I don't know why this does not work :( It seems that getCAN() does not work because the values of the global variables do not change and after few cycles of loop() even the prints to the serial monitor stop. Do somebody see what i did wrong? Thanks!!!
Title: Re: Building a CAN API for Arduino DUE
Post by: yamakasi on Jul 02, 2016, 12:01 pm
or can somebody tell me how i can write a ISR with this can-library?
Title: Re: Building a CAN API for Arduino DUE
Post by: Succubus on Jul 05, 2016, 03:52 pm
Hello,

I attached the interrupt to a hardware pin from the CAN shield (not the original one in my opinion used a wire) not to a timer as you did, than I added

  attachInterrupt(2, ISR_function , FALLING);

to my DUE initialization.

This is my ISR:
void ISR_function()
{
  flagRecv = 1;
}
Do as less as necessary in the ISR

works fine for me

Succubus
Title: Re: Building a CAN API for Arduino DUE
Post by: joeblogs on Jul 11, 2016, 01:23 am
Nice work Palliser.

Ive just come across this post, have read through a fair few pages.  Can anyone up to date with this post tell me,
Is there a shield for the Due, or just use the chips in your schematic. (post 526)
In your schematic, it shows 3.3v as can high, how would this work with 5v can.

Sorry if questions are stupid, my experince with can is 0, electronics not much better.

Cheers.
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Jul 11, 2016, 04:08 am
Nice work Palliser.

Ive just come across this post, have read through a fair few pages.  Can anyone up to date with this post tell me,
Is there a shield for the Due, or just use the chips in your schematic. (post 526)
In your schematic, it shows 3.3v as can high, how would this work with 5v can.

Sorry if questions are stupid, my experince with can is 0, electronics not much better.

Cheers.
Hello joeblogs. I'm happy that you liked this post.

Regarding the CAN shield for Due, I'd recommend you the following one that I've been using for some time now.
http://togglebit.net/product/arduino-due-can-shield/ (http://togglebit.net/product/arduino-due-can-shield/)

Due I/O is not tolerant to 5VDC, thus, for interfacing any external device that works with 5V, I'd recommend you to use a voltage converter like,
http://www.kr4.us/Logic-Level-Converter-Bi-Directional.html?gclid=CP2swb6p6s0CFQcKaQodrRoGug (http://www.kr4.us/Logic-Level-Converter-Bi-Directional.html?gclid=CP2swb6p6s0CFQcKaQodrRoGug)

Regards,
-p
Title: Re: Building a CAN API for Arduino DUE
Post by: joeblogs on Jul 11, 2016, 10:04 am
Thanks Mate
Title: Re: Building a CAN API for Arduino DUE
Post by: Succubus on Jul 11, 2016, 12:35 pm
Hello,

for SPI you should use in any case an level shifter they are really cheap (here I used a Chinese one sorry the SMD are on the other side). But for some other lines (RX or INT) is an resistor quite enough.

I attached an Prototype CAN shield to this post with 2 CAN transceivers and also a level shifter for the SPI interface, this allows me to plug in a Seeed Studion CAN Shield which runs at 5V with nearly no modification at this shield. I just changed the Interrupt line to a different pin.

Also the transceivers are 5V types which work with 3.3V signals the RX back to the DUE has an 100 Ohm resistor in the line. You can see this below the bl/grn CAN wires below above the reset btn.

Succubus
Title: Re: Building a CAN API for Arduino DUE
Post by: ikbalort on Jul 14, 2016, 03:13 pm
Hi,

I am using due_can library for one of my projects. I log all the CAN bus data for a car and for this i built a due shield.

I am logging all the data succesfully to SD card and view it but i am having trouble with CAN_FRAME data structure.

When i record the data as the bytes from less significant to most significant, Vector's CANalyzer interprates it right. But when i use data as value(64 bit) or low(32)-high(32), these formats keep data differently.

For example;

A CAN bus message with some ID contains the data like A0107000A2000000. When i take it and records it like:

                 data.bytes[0] data.bytes[1] data.bytes[2] ...

it gives me the data below which is true:

                 A0 10 70 00 A2 00 00 00

But when i use data.value, it gives me:

                 7010A0A2 (00 70 10 A0 00 00 00 A2)

I am trying to get some bits of this 64 bit integer. It shoul be in right order. Otherwise I will not get the right bits.

My question is this, can i change the library to get first data i wrote or what do you suggest?

I took have a look at the due_can library. It takes the CAN message from a pointer like:

                 /* CAN peripheral, set by constructor */
            Can* m_pCan ;

However I could not find the structure of 'Can'.

If someone helps me, I would be appreciated.




Title: Re: Building a CAN API for Arduino DUE
Post by: Succubus on Jul 20, 2016, 05:11 pm
Hello,

in the due_can.h I found this:

Code: [Select]

//This is architecture specific. DO NOT USE THIS UNION ON ANYTHING OTHER THAN THE CORTEX M3 / Arduino Due
//UNLESS YOU DOUBLE CHECK THINGS!
typedef union {
    uint64_t value;
struct {
uint32_t low;
uint32_t high;
};
struct {
uint16_t s0;
uint16_t s1;
uint16_t s2;
uint16_t s3;
    };
uint8_t bytes[8];
uint8_t byte[8]; //alternate name so you can omit the s if you feel it makes more sense
} BytesUnion;

typedef struct
{
uint32_t id; // EID if ide set, SID otherwise
uint32_t fid; // family ID
uint8_t rtr; // Remote Transmission Request
uint8_t priority; // Priority but only important for TX frames and then only for special uses.
uint8_t extended; // Extended ID flag
uint8_t length; // Number of data bytes
BytesUnion data; // 64 bits - lots of ways to access it.
} CAN_FRAME;



I think your problem is based of the union construct.
Title: Re: Building a CAN API for Arduino DUE
Post by: duramaxhd on Sep 15, 2016, 11:38 pm
Trying to stumble through a problem here that is driving me crazy.

What I want to do is have the Due listen for a certain ID/message on the bus...then take that message, and spit it back out onto the bus, except with a different ID assigned to it.


For example:

Due is setup to only listen to messages from $123 ID...so then it receives the message:

($123) AA CC 12 31 DE FF 87 43

=====================================

I want it to take that "AA CC 12 31 DE FF 87 43"
and spit it back out onto the bus, except I want it to spit it back out with the ID $456 attached to it.

so the Due would output

($456)  AA CC 12 31 DE FF 87 43

onto the CAN bus....if that makes sense?


Ive already got the Due filtering so it receives just the ID (ID $123) that I want it to receive, and it prints to the serial window perfectly.

But Im just having trouble getting it to save those 8 bytes to global variables, so then it can re-send that 8 byte message back out, with the different header.

Thanks
Ben
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Oct 03, 2016, 05:54 pm
Trying to stumble through a problem here that is driving me crazy...

Hello Ben,

Have you made progress in solving your problem?
If not, can you show us the code or a snippet of your filtering (receive)? Thanks.

-p
Title: Re: Building a CAN API for Arduino DUE
Post by: anpaza on Oct 08, 2016, 06:27 pm
Hello adar0n,

I don't see any problem to use SN65HVD230/231/232. SN65HVD233, 234 and 235 are needed in case you need to use some of their special features like loopback (LBK-pin 5 in 233), ultra-low current sleep mode (EN-pin 5 in 234) and listen-only loopback (AB-pin 5 in 235).

[...]
5- +3V3
Hello!

Could you explain, why you decided to connect the VREF pin to +3.3V?
I'm using SN65HVD230 right now, according to datashet, VREF is connected to some sort of internal voltage regulator which maintains VCC/2 (e.g. around 1.65V) on VREF. But when microcontroller is sending data, I've connected my oscilloscope to VREF and it kind of follows the input on the TX pin, with a large amplitude - around 1-2V. Because of this (I suppose) the outputs on CANL and CANH is very distorted, and is not correctly decoded by receiver.

But if I connect VREF to VCC, it starts working, pure magic! But this contradicts the datasheet, which says to leave VREF unconnected (for simple cases). So, I wonder, how you came to this solution - to connect VREF to VCC?
Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Oct 08, 2016, 10:41 pm
Hello!

Could you explain, why you decided to connect the VREF pin to +3.3V?
I'm using SN65HVD230 right now, according to datashet, VREF is connected to some sort of internal voltage regulator which maintains VCC/2 (e.g. around 1.65V) on VREF. But when microcontroller is sending data, I've connected my oscilloscope to VREF and it kind of follows the input on the TX pin, with a large amplitude - around 1-2V. Because of this (I suppose) the outputs on CANL and CANH is very distorted, and is not correctly decoded by receiver.

But if I connect VREF to VCC, it starts working, pure magic! But this contradicts the datasheet, which says to leave VREF unconnected (for simple cases). So, I wonder, how you came to this solution - to connect VREF to VCC?

Hello anpaza,

Pin 5 in the SN65HVDx 3.3-V CAN transceiver series has different functions depending on the specific model of transceiver. For example, in the model that I used SN65HVD234, the pin 5 corresponds to the EN or ultra-low current sleep mode Input pin for the device. I powered it with 3.3V to disable this mode (transceiver asleep when pin is LOW). In your model, SN65HVD230 pin 5 is an Output (not an Input), a Vcc/2 voltage reference. As the specs indicate in 10.3.1., this Vref pin can be connected to the common mode point of the split termination resistance in order to help with further stabilization of the common mode voltage of the bus (showed in figure 44 of page 34). If the Vref pin is not used it may be left floating.

Here the link of the Texas Instruments Inc. specification to which I refers.

http://www.ti.com/lit/ds/symlink/sn65hvd231.pdf (http://www.ti.com/lit/ds/symlink/sn65hvd231.pdf)

-p
Title: Re: Building a CAN API for Arduino DUE
Post by: anpaza on Oct 10, 2016, 10:40 pm
I did more investigations.

If I disconnect VREF from VCC, the device can receive, but cannot send. In particular, with a two-device network (other device sending and my device receiving) this leads to receiver being unable to set the ACK flag at the end of datagram, to signal that the datagram was correctly received. Thus, sender automatically sends the datagram again and again to infinity.

But in a more-than-two-devices network in a listen-only configuration that could work. Bus contents are deciphered correctly, only can't send correctly.

For full functionality, however, you have to connect VREF to VCC. I don't understand why it is this way, but that's a fact. My ICs are marked "VP230 61M", I think 61M is batch number (maybe it's a failed batch, I don't know).

Here's the oscillogram from SN65HVD230 sending some data at 1MBit speed with pin VREF connected to VCC.
https://s12.postimg.org/vpvwwhjt9/image.png
Red and yellow graphs are CANL/CANH, and green graph is math (CANH-CANL).
Looks ok to me but, however, I can't compare with other transceivers as I have only VP230s here.
Title: Re: Building a CAN API for Arduino DUE
Post by: duramaxhd on Oct 16, 2016, 07:40 pm
Hello Ben,

Have you made progress in solving your problem?
If not, can you show us the code or a snippet of your filtering (receive)? Thanks.

-p
Hi- Yes, I figured it out!

Here is what I did:

Code: [Select]
// Required libraries
#include "variant.h"
#include <due_can.h>

//Leave defined if you use native port, comment if using programming port
#define Serial SerialUSB


char MSG[8];

void setup()
{

  Serial.begin(115200);
 
  // Initialize CAN0, Set the proper baud rates here
  Can0.begin(CAN_BPS_500K);

 
Can0.setRXFilter(0x4A1, 0x7ff, false);// can0 to only receive $4A1 CAN ID
  } 

//============================================================================================
//============================================================================================


void printFrame(CAN_FRAME &frame) {
   Serial.print("ID: 0x");
   Serial.print(frame.id, HEX);
   Serial.print(" Len: ");
   Serial.print(frame.length);
   Serial.print(" Data: 0x");
   for (int g = 0; g < frame.length; g++) {
       Serial.print(frame.data.bytes[g], HEX);
       Serial.print(" ");
   }
   Serial.print("\r\n");


  frame.id = 677;                  // assign new CAN ID $2A5 to original message received on $4A1
  frame.extended = false;
  frame.length = 8;
   frame.data.byte[8] = int (MSG);
   Can0.sendFrame(frame);
}
//============================================================================================
//============================================================================================


void loop(){
  CAN_FRAME incoming;
  if (Can0.available() > 0) {
Can0.read(incoming); //read in can information
  printFrame(incoming);
}
}
Title: Re: Building a CAN API for Arduino DUE
Post by: duramaxhd on Oct 16, 2016, 10:14 pm
Hello Ben,

Have you made progress in solving your problem?
If not, can you show us the code or a snippet of your filtering (receive)? Thanks.

-p
One other quick question...

Have you had any success using the 100-pin SAM3X8C? Or do you have ideas of what needs to be changed in the libraries to reconfigure things?

It would be awesome to be able to get this CAN stuff working on the smaller Cortex M3 chip....
Title: Re: Building a CAN API for Arduino DUE
Post by: Barre on Feb 02, 2017, 08:56 pm
Hi Gentleman's !
Please need help !!!

trying to send some can bus frames from SD to can bus, but "SD read" and "can send" make some collision
SD works ,can works -but not together
the failure is empty data information reading can bus id and ext works
any suggestions ? more description in code


code;
void setup()
{
    //***********************  SD  **********************
  Serial1.begin(9600);
  Can0.begin(CAN_BPS_125K);
  Can0.watchFor(); 
  mydisp.clearScreen();
}

void sendData()
{
  SysCall::yield();
  if (!sd.begin(chipSelect, SPI_SIXTEENTH_SPEED)) {
    sd.initErrorHalt();
  }
 openfile:
   mydisp.clearScreen();
  if (! printFile.open("Run.txt", O_READ))
  { sd.errorHalt("opening test.txt for read failed");
  mydisp.println("card failure ");
  }
   while (printFile.available()) {
 
   
   // buffer = printFile.readStringUntil('\n');                      //  this row failure funktion wile reading file
   buffer = "03C3F7FC;F9;00;00;19;40;27;78;15;0000064C;;";           // this string from file work, if before read is blocked "//"

     
     
     CAN_FRAME outgoing;

 outgoing.id = strtol(buffer.substring(0,8).c_str(), NULL, 16);
  outgoing.extended = true;
  outgoing.priority = 4; //0-15 lower is higher priority
 
 outgoing.data.byte[0] =  strtol(buffer.substring(9,11).c_str(), NULL, 16);
  outgoing.data.byte[1] =  strtol(buffer.substring(12,14).c_str(), NULL, 16);
   outgoing.data.byte[2] =  strtol(buffer.substring(15,17).c_str(), NULL, 16);
    outgoing.data.byte[3] =  strtol(buffer.substring(18,20).c_str(), NULL, 16);
     outgoing.data.byte[4] =  strtol(buffer.substring(21,23).c_str(), NULL, 16);
      outgoing.data.byte[5] =  strtol(buffer.substring(24,26).c_str(), NULL, 16);
       outgoing.data.byte[6] =  strtol(buffer.substring(27,29).c_str(), NULL, 16);
        outgoing.data.byte[7] =  strtol(buffer.substring(30,32).c_str(), NULL, 16);

      delay (1000);
      mydisp.print(outgoing.id,HEX);mydisp.print(" "); mydisp.print(outgoing.data.low,HEX);mydisp.println(outgoing.data.high,HEX);// print works
Can0.sendFrame(outgoing);

     delay (1000);

  printFile.close();


}
void loop()
{

  sendData();
    delay (1000);
  }
 
Title: Re: Building a CAN API for Arduino DUE
Post by: kindred7 on Apr 17, 2017, 04:04 am
Code: [Select]
// Required libraries
  #include "variant.h"
  #include "due_can.h"
  #include <HardwareSerial.h>
  //#include "Time.h"
  int checker = 1;
  unsigned long currentMillis = 0;
  unsigned long previousMillis = 0;
  static const int interval = 5000;
  //Leave defined if you use native port, comment if using programming port
 // #define Serial SerialUSB

  CAN_FRAME incoming0, incoming1; // might not need, canframe defined in function
  
  //Define DCDC messages
  #define dcdc_1  0x12231930
  #define dcdc_hi 0x00000000

void setup() {
  Serial.begin(115200);
  Can0.begin(CAN_BPS_500K); // Initialize CAN0 and CAN1, Set the proper baud rates here
  Can1.begin(CAN_BPS_500K);  
  //Can0.set_baudrate(33333);
  //Can0.set_baudrate(1000000);
  //Can0.enable();
  Can0.watchFor(0x102);
  Can1.watchFor(0x103);
}

void sendData()
{
  CAN_FRAME outgoing;
  outgoing.id = 0x400;
  outgoing.extended = false;
  outgoing.priority = 4; //0-15 lower is higher priority
  outgoing.data.s0 = 0xFEED;
  outgoing.data.byte[2] = 0xDD;
  outgoing.data.byte[3] = 0x55;
  outgoing.data.high = 0xDEADBEEF;
  Can0.sendFrame(outgoing);
}

void sendDCDC(){
  CAN_FRAME dcdc;
  dcdc.id = 0x112;
  dcdc.extended = true; //what is exteneded frames?
  dcdc.priority = 4;
  dcdc.length = 8;
  dcdc.data.low = dcdc_1;
  dcdc.data.high = dcdc_hi;
  Can0.sendFrame(dcdc);
  //Serial.println(Can0.sendFrame(dcdc),HEX);
  //Serial.println(dcdc.data.low,HEX);
}

void loop() {
  //CAN_FRAME incoming;
  
//  if (Can0.available() > 0)  {//This meanss there is data existing on CAN0 RX.
//  //Serial.println("CAN0 data recieved and read");
//  Can0.read(incoming);
//  Can1.sendFrame(incoming);
//   }
//  if (Can1.available() > 0) {//This means there is data existing on CAN1 RX.
//  Serial.println("CAN1 data recieved and read");
//  Can1.read(incoming);
//  Can0.sendFrame(incoming);
//  }
  currentMillis = millis();
  if (currentMillis - previousMillis >= interval){
    Serial.print("CurrentMillis:");
    Serial.println(currentMillis);
    Serial.print("PreviousMillis:");
    Serial.println(previousMillis);
    Serial.println("");
    Serial.print(interval);
    Serial.println("");
    Can0.read(incoming0);
    previousMillis = currentMillis;
      if (checker == 1){
         Serial.println("Checker is on");
         sendDCDC();
         delayMicroseconds(100);
         checker = 0;
      }
   }
}


Hi everyone,

I am fairly new to arduino but not programming in general. Above you can see the code I am using for testing transmission of a CAN frame I need to test.

The problem I am having right now is an odd one I think. Currently I cannot stop the transmission of my CAN frame. I have placed an interval to delay the sending of the CAN frame. It is supposed to send one frame every 5 seconds. This portion works and is confirmed by my print statements. I even placed a checker variable near the end to block the "sendDCDC()" function call just to make sure I am not somehow looping the send command for the frame.

I feel like I am losing my mind, can anyone lend a hand? Thank for your time!
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Apr 17, 2017, 04:19 am
The problem I am having right now is an odd one I think. Currently I cannot stop the transmission of my CAN frame. I have placed an interval to delay the sending of the CAN frame. It is supposed to send one frame every 5 seconds. This portion works and is confirmed by my print statements. I even placed a checker variable near the end to block the "sendDCDC()" function call just to make sure I am not somehow looping the send command for the frame.

I feel like I am losing my mind, can anyone lend a hand? Thank for your time!
You're not losing your mind. It is very likely that the problem is that nothing else on the bus is acknowledging the frame. If nobody claims it then the sending node will attempt to resend on the assumption that transmission had failed. It will do this rapid fire forever until someone says they're listening. So, you need something on the other end that isn't in listen-only mode.
Title: Re: Building a CAN API for Arduino DUE
Post by: kindred7 on Apr 18, 2017, 05:47 am
You're not losing your mind. It is very likely that the problem is that nothing else on the bus is acknowledging the frame. If nobody claims it then the sending node will attempt to resend on the assumption that transmission had failed. It will do this rapid fire forever until someone says they're listening. So, you need something on the other end that isn't in listen-only mode.
Thanks Collin80 for clearing the murky waters a bit! Also thanks for all the work you put into this CAN back end, it's awesome. Cheers!
Title: Re: Building a CAN API for Arduino DUE
Post by: SergeyPP on Aug 06, 2017, 12:59 pm
Thanks for the library. It helps me to understand how to use CAN.


I had a problem with sending messages in CAN.  All examples I tried sent nothing.
I found the default tx mailboxes is 0.

Can0.setNumTXBoxes(1); helped me.


Like this:

Code: [Select]
void setup()
{
 pinMode(13,OUTPUT);
 Serial.begin(115200);
 
 Serial.print("\n Go ...");

 Can0.begin(CAN_BPS_1000K);
 Can0.setNumTXBoxes(1);

 Can0.watchFor();
}



Hardware is Arduino Due + SN65HVD230
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Aug 06, 2017, 10:32 pm
Woah, thanks for reporting that! I can't believe I missed that. You're right, a recent batch of changes accidentally caused the number of TX boxes to be set to zero by default. That is corrected now.
Title: Re: Building a CAN API for Arduino DUE
Post by: methods on Aug 11, 2017, 08:01 am
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
Title: Re: Building a CAN API for Arduino DUE
Post by: SergeyPP on Sep 01, 2017, 10:55 am
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.

Code: [Select]

bool CANRaw::sendFrame(CAN_FRAME& txFrame)
{
...
    for (uint8_t mbox = 0; mbox < 8; mbox++) {
...
          result=true; //we've sent it. mission accomplished.
          !!!!
        }
...
}
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Sep 03, 2017, 11:04 pm
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.
Title: Re: Building a CAN API for Arduino DUE
Post by: ofir060 on Sep 28, 2017, 11:06 am
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?
Title: Re: Building a CAN API for Arduino DUE
Post by: ard_newbie on Sep 28, 2017, 02:20 pm
Are you using transceivers  or diodes ?  Are you using the programming port or the native USB port ?
What is exactly your wiring ?

Title: Re: Building a CAN API for Arduino DUE
Post by: ofir060 on Sep 28, 2017, 03:13 pm
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)
Title: Re: Building a CAN API for Arduino DUE
Post by: ard_newbie on Sep 28, 2017, 03:32 pm
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 (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
Title: Re: Building a CAN API for Arduino DUE
Post by: ofir060 on Sep 28, 2017, 04:35 pm
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 (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?)
Title: Re: Building a CAN API for Arduino DUE
Post by: tom32500 on Oct 16, 2017, 07:24 pm
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
Title: Re: Building a CAN API for Arduino DUE
Post by: jrickard on Oct 20, 2017, 11:27 am
Here's a Due board with built-in CAN port.

http://store.evtv.me/proddetail.php?prod=EVTVDue2 (http://store.evtv.me/proddetail.php?prod=EVTVDue2)
Title: Re: Building a CAN API for Arduino DUE
Post by: amin_mdn on Oct 24, 2017, 05:35 pm
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 :( . I had used MCP2551 for many times, has my CAN controller been failed because of this? :(((
Title: Re: Building a CAN API for Arduino DUE
Post by: tbit on Oct 29, 2017, 03:59 pm
Fyi - new how-to for message gateway/spoofing with the DUE. http://togglebit.net/how-to-gateway-messages/ (http://togglebit.net/how-to-gateway-messages/)
Title: Re: Building a CAN API for Arduino DUE
Post by: alexogeo03 on Nov 14, 2017, 07:56 pm
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
Title: Re: Building a CAN API for Arduino DUE
Post by: amin_mdn on Nov 14, 2017, 08:59 pm
Hello!
I need to learn about "filter( , , , )" command. Can you explain s'th or upload a pdf about that?
Title: Re: Building a CAN API for Arduino DUE
Post by: tbit on Nov 15, 2017, 03:09 am
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.
Title: Re: Building a CAN API for Arduino DUE
Post by: amin_mdn on Nov 17, 2017, 03:21 pm
tbit thanks a lot.
Title: Re: Building a CAN API for Arduino DUE
Post by: amin_mdn on Dec 12, 2017, 03:43 pm
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 :( . 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?
Title: Re: Building a CAN API for Arduino DUE
Post by: AllenDC on Dec 31, 2017, 07:08 pm
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


Title: Re: Building a CAN API for Arduino DUE
Post by: Palliser on Jan 01, 2018, 08:54 pm
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:
https://github.com/collin80/can_common (https://github.com/collin80/can_common)
Title: Re: Building a CAN API for Arduino DUE
Post by: gaskraank on Apr 24, 2018, 03:17 pm
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
Title: Re: Building a CAN API for Arduino DUE
Post by: tbit on Apr 24, 2018, 03:21 pm
http://togglebit.net/how-to-gateway-messages/
Title: Re: Building a CAN API for Arduino DUE
Post by: gaskraank on Apr 24, 2018, 04:00 pm
Hey Thank you for the fast answer.

My researches brought me to your can Shield and Im gonna buy it.

But my actual question was about, is the Clock Speed sufficient to forward 200 Messages and not only like 7.
I just want to Manipulate one of these messages.

Thank you so much for your support.

Greetings
Title: Re: Building a CAN API for Arduino DUE
Post by: tbit on Apr 24, 2018, 04:07 pm
Clock speed is only a part of a more complex problem. I suggest having a read through the datasheet for the processor and inspect the underlying code you'll see there are message filters and different and more efficient ways of handling ISR's with hardware pointers, and then you have the overhead of your code, message buffer sizes, baud rate, the transmit rate of messages that are transmitted from your devices etc.

Maybe a valid first step is to simply just test the code thats written and see how far you get and optimize from there....i've not tested that particular bit of code with 200 messages.
Title: Re: Building a CAN API for Arduino DUE
Post by: gaskraank on Apr 24, 2018, 05:30 pm
Okey thank you so far.

I don't have a due here to try it so I have to order both.

Im just stucking if I should just go ahead with a Raspberry PI 3 where the performance would be much higher.
I don't want to waste my really small budget.

Greetings.
Title: Re: Building a CAN API for Arduino DUE
Post by: tbit on Apr 24, 2018, 06:08 pm
I don't know that a PI3  has a built-in CAN controller, so you will be stuck with what you can get through SPI just something to look out for....
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on Apr 24, 2018, 06:14 pm
Okey thank you so far.

I don't have a due here to try it so I have to order both.

Im just stucking if I should just go ahead with a Raspberry PI 3 where the performance would be much higher.
I don't want to waste my really small budget.

Greetings.
For what it's worth, I've used my library (due_can) to capture over 4000 frames per second with the Due. I can send that quickly too. I really don't think you're liable to have any trouble in forwarding 300 messages per second. I can see no reason this wouldn't work with ToggleBit's library too. The Due is plenty fast for keeping up with two CAN buses.
Title: Re: Building a CAN API for Arduino DUE
Post by: yoh-there on Apr 24, 2018, 08:44 pm
^What AdderD said^. Don't worry. It's lightning fast. I am receiving 1000 fps on the Due. And even doing some silly slow sprintfs on the data.
Title: Re: Building a CAN API for Arduino DUE
Post by: tbit on Apr 25, 2018, 01:43 am
There you go, good feedback.
Title: Re: Building a CAN API for Arduino DUE
Post by: gaskraank on Apr 25, 2018, 06:56 pm
Hey Guys,

thank you so much for the reputation!

As soon as everything arrives I will report if it works or not.
The source code to implement this should be really easy.

If have the time and opportunity I will do some measurements how fast a message gets forwarded.

Thank you all!

Greetings
Title: Re: Building a CAN API for Arduino DUE
Post by: gaskraank on May 01, 2018, 02:33 pm
Hey Guys,

my Arduino just arrived.

I wired the attached circuit together and tried to receive some messages.
Everything worked well, but when I try to use the PingPong, Echo Example nothing happens.
This means the sendFrame function is not working.

Is there anything special to be aware of?
The circuit how I done is in the attachment.
I tried to use this circuit :
(https://www.mikrocontroller.net/attachment/280466/canobd22.jpg)

I'm currently using the SN65HVD230 from Texas Instruments.

Greetings.

Title: Re: Building a CAN API for Arduino DUE
Post by: ard_newbie on May 01, 2018, 04:18 pm
You need 120 Ohms resistors at each end of the bus.

If you have issues with your transceivers, to test your CAN software, make a CAN bus without transceivers (it works up to a few meters long):

https://www.mikrocontroller.net/attachment/28831/siemens_AP2921.pdf (https://www.mikrocontroller.net/attachment/28831/siemens_AP2921.pdf)

Note that you would have to connect the resistor to 3.3V (and not 5V). Choose the resistor so that the current be lower than 1.6 mA.
Title: Re: Building a CAN API for Arduino DUE
Post by: gaskraank on May 01, 2018, 06:55 pm
Hey Thank you for your answer.
Okey I added the 120 Ohm and I also have a lot of traffic on the can RX side already.

If I want to send a frame this won't work.
Do you think that's because of the transceiver.
Greetings
Title: Re: Building a CAN API for Arduino DUE
Post by: ard_newbie on May 01, 2018, 07:34 pm

Do you think that's because of the transceiver ?.

The most obvous answer is NO, but there must be something wrong in your wiring.

Check all contacts on your breadboard.

The bus is supposed to be a twisted pair, althought it should not be an issue on such a short distance.
Title: Re: Building a CAN API for Arduino DUE
Post by: gaskraank on May 01, 2018, 08:05 pm
Hey,

I just breaked out CANTX on the Oscilloscope  and I have a Square Signal on that. If Break-Out CANH or CANH there's only noise.

Do I have to activate Receive and Send on the transceiver could that be possible?

Greetings.
Title: Re: Building a CAN API for Arduino DUE
Post by: AdderD on May 02, 2018, 02:34 am
That particular transceiver has no enable pin so it is essentially always on. If you have signals on CANRX and CANTX but the CANL and CANH lines are garbage then there is likely a wiring fault on the H/L side (the actual CAN bus). What you should get is a square wave on H/L when you use L for ground on your scope and H at the scope input. Or connect to your local board ground with the scope and L will sink then return to center and H will rise and return to center. They ought to rest at essentially the exact same voltage. At rest H = L so you will measure 0 volts. When active they pull away from each other as a squarewave that ought to look like an exact mirror image of each other. The more it differs from that ideal the worse off you are.
Title: Re: Building a CAN API for Arduino DUE
Post by: weird_dave on May 02, 2018, 10:21 am
Be careful here, if you connect the scope ground to 'L' and the Due is powered from USB, you may be shorting 'L' via mains earth (if your local mains supply has this). You're better off 'scoping both the 'L' and 'H' simultaneously.

Can (no pun intended) we have 'scope traces of the noise?

I assume you've checked that there is in fact 3v3 across the modules supply pins. (I had a board with a 5v part fitted instead of 3v3 and all I got was noise).

If you are using the 230 part instead of the 234, note that pin 5 is not supposed to be connected to 3v3, it's a reference output, forcing this to 3v3 may be why it doesn't work as expected (and could damage it).
Title: Re: Building a CAN API for Arduino DUE
Post by: gaskraank on May 02, 2018, 02:20 pm
Be careful here, if you connect the scope ground to 'L' and the Due is powered from USB, you may be shorting 'L' via mains earth (if your local mains supply has this). You're better off 'scoping both the 'L' and 'H' simultaneously.

Can (no pun intended) we have 'scope traces of the noise?

I assume you've checked that there is in fact 3v3 across the modules supply pins. (I had a board with a 5v part fitted instead of 3v3 and all I got was noise).

If you are using the 230 part instead of the 234, note that pin 5 is not supposed to be connected to 3v3, it's a reference output, forcing this to 3v3 may be why it doesn't work as expected (and could damage it).
Hey thank you for the helpful information.

Currently PIN 5 of the transceiver is connected to 3v3 you think it has to be connected with 5V instead?

I could do some scope traces of the noise but I will try to use the 5V first.
Do you mean the modules supply pins the pins of the arduino board or of the transceiver?

That particular transceiver has no enable pin so it is essentially always on. If you have signals on CANRX and CANTX but the CANL and CANH lines are garbage then there is likely a wiring fault on the H/L side (the actual CAN bus). What you should get is a square wave on H/L when you use L for ground on your scope and H at the scope input. Or connect to your local board ground with the scope and L will sink then return to center and H will rise and return to center. They ought to rest at essentially the exact same voltage. At rest H = L so you will measure 0 volts. When active they pull away from each other as a squarewave that ought to look like an exact mirror image of each other. The more it differs from that ideal the worse off you are.
Yes so I'm gonna do some measurements on CANTX and CANRX on the Oscillator today again and post the scope here.

I'm going to check if CANL and CANH has 0 Volts when there is nothing send. But I already checked with the multimeter and if I was Sending Frames there was still 0 Volts. If I receive Messages via CANalzyer its about 190 mV. Receiving Messages is working fantastic, do you think this could be still a wiring fault on the H/L side?

Thank you so much for all the answers.
Title: Re: Building a CAN API for Arduino DUE
Post by: gaskraank on May 02, 2018, 02:39 pm
Hey Again,

maybe this is also important to know.

I used this CAN Transceiver Board
KNACRO SN65HVD230
Bought them here https://www.amazon.com/KNACRO-SN65HVD230-Communication-transceiver-Arduino/dp/B01ILU2WP6/ref=cm_cr_arp_d_pdt_img_top?ie=UTF8

So the  SN65HVD230  is already wired on a breakout board an I only added the capacitors in between.

Greetings
Title: Re: Building a CAN API for Arduino DUE
Post by: ard_newbie on May 03, 2018, 05:59 am
It seems that these transceivers have already 120 Ohms resistors, no need to add them. Remove capacitors and see what happens.
Title: Re: Building a CAN API for Arduino DUE
Post by: weird_dave on May 03, 2018, 12:54 pm
Stupid website logged me out while I was replying, I'm not going through all that again

Suffice to say, don't connect anything to pin 5, it's an output!
CANH and CANL should never be both 0v or both 3v3.

disconnect everything on CANL and CANH at the module and attach 2 channels of a scope to them. I would also disconnect Rx and Tx from the module, get this working then attach the Rx and TX and see if it still works, then attach to CANL and CANH, at one of these stages it will fail, indicating where the problem is. I also advise taking a look at the datasheet for the IC.
Title: Re: Building a CAN API for Arduino DUE
Post by: gaskraank on May 09, 2018, 10:37 pm
Hey Guys,

So I just bought the Copperhiltech CAN Due Dual Channel Shield.

This worked perfect for me uploaded the code I already had and it worked instantly.

So I thank you all so much and this due is insane its just firing out the messages.

I was even printing all the frames on the serial and this was still getting all the messages forwarded.


Thank you so much.

Greetings