Recent Posts
App Note: Arduino Due 2-Channel CAN Bus Driver Software
Posted by
onThe availability of Controller Area Network (CAN) interfaces in combination with other interface technologies explains the vast popularity of the ARM Cortex-M3 processor in the CAN and SAE J1939 industry. The processor provides the means to easily and quickly create applications like CAN/J1939 gateways, CAN Bridges, J1939 ECUs, J1939 Data Logger, and many more.
The Arduino Due is the first ARM-based Arduino development board. The programming of the microcontroller is accomplished through the familiar Arduino IDE (Windows, Mac), keeping the programming as backward compatible to other Arduino systems as possible, thus allowing a smooth migration between processor systems.
Just like any other ARM M3 processor, the Due supports two CAN ports. The downside, though, is the mere fact that the Due's CAN capabilities are basically useless without additional hardware coponents. In order to save costs, but at the same time enabling the user to pay only for the interfaces needed for the project, the board design is limited to providing the processor with the bare necessities, while routing all interfacing signals “as is” to onboard connectors.
For instance, the serial interfaces such as Controller Area Network (CAN) and UART (RS-232/422/485) provide only TTL levels, and you will need additional hardware (transceivers) to make them hardware-compliant with their respective standards. For more detailed information, see my post ARM Cortex M3 Development Boards Require External CAN Bus Transceiver.
There are several options to add the necessary CAN transceivers to the Due board:
- Arduino DUE CANShield With 2 CAN Ports...
- CAN Bus Breakout Board 3.3 VDC...
- CAN Bus Breakout Board 5 VDC...
- CAN Bus Mini Breakout Board...
CAN Bus Driver Software
The software situation turned out to be worse than that of the hardware. Yes, there are several options available that help you program CAN applications for the Arduino Due, but pretty much all of them ignore some quality standards that are important to me, with readability and user-friendliness claiming the top spots. Unfortunately, the vast majority of available source code packages do not meet either criteria. To make things worse, the programs, merely developed under the aspect of proving the concept, come with numerous bugs and inconsistencies.
The best package I found was the due_can package by Collin Kidder as published on GitHub. I used his source code for a quick head start, but also modified it slightly and added a "wrapper" around it, i.e. I created more user-friendly CAN Bus access functions.
These functions are:
byte canInit(byte cPort, long lBaudrate)
The canInit function initializes the specified CAN port (cPort = 0/1) using the specified baud rate (as defined in due_can.h).
Return code: CAN_OK / CAN_ERROR
byte canTx(byte cPort, long lMsgID, bool bExtendedFormat, byte* cData, byte cDataLen)
The canTx function transmits a CAN data frame through the specified CAN port (cPort = 0/1). Further parameters are the message ID, indicator for extended format (false = 11-bit ID, true = 29-bit ID), the actual data, and the number of data bytes.
Return code: CAN_OK / CAN_ERROR
byte canRx(byte cPort, long* lMsgID, bool* bExtendedFormat, byte* cData, byte* cDataLen)
The canRx function checks for a received CAN data frame at the specified CAN port (cPort = 0/1).
Return code: CAN_OK indicates that a message was received. CAN_ERROR indicates that no message was received. If a message was received, the function also returns the message ID, an indicator for extended format (false = 11-bit ID, true = 29-bit ID), the actual data, and the number of data bytes.
And yes, this is pretty much it. Naturally, there may be a need for further functionality, such as message filtering, but for a quick head start, these functions will serve the majority of CAN Bus applications with the Arduino Due.
Download the Software
Disclaimer: All Arduino sketches and other code samples and projects as introduced here are free software; you can redistribute and/or modify them. The programs are introduced in the hope that they will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. With downloading these programs, you confirm that these code samples and projects were created for demonstration and educational purpose only.
=> Download the Due_CAN_Test Arduino Sketch (zip file)...
Last, but not least, a few tips...
- Make sure you have the latest Arduino IDE installed (check at arduino.cc). The Arduino people have this absolutely annoying habit of providing updates that are far from backwards-compatibility, meaning compiling a perfectly good program may result in an enormous list of inexplicable error messages.
- Make sure, your IDE is set up for the Arduino Due. Go to Tools->Boards->Boards Manager to install Due capabilities.
Programming Arduino Getting Started with Sketches
by Simon Monk
Clear, easy-to-follow examples show you how to program Arduino with ease! "Programming Arduino: Getting Started with Sketches" helps you understand the software side of Arduino and explains how to write well-crafted Sketches (the name given to Arduino programs) using the C language of Arduino. This practical guide offers an unintimidating, concise approach for non-programmers that will get you up and running right away.
Programming Arduino: Getting Started with Sketches explains basic concepts and syntax of C with simple language and clear examples designed for absolute beginners - no prior knowledge of programming is required. It leads you from basic through to advanced C programming concepts and features dozens of specific examples that illustrate concepts and can be used as-is or modified to suit your purposes.
- All code from the book is available for download.
- Helps you develop working Sketches quickly.
Coverage includes: C Language Basics; Functions; Arrays, Strings; Input / Output; Standard Library Goodies; Storage; LCD Displays; Programming for the Web; Program Design; C++ and Library Writing