A CAN Bus Primer Part 2: Implementation

Introduction

In part one of this series we took a dive into CAN bus itself; including what it is, the nitty-gritty of the physical layer, how collisions are handled and various common applications. In this article we’ll be taking a look at various devices and development kits that can be used for interacting with a CAN bus, considerations when selecting parts and then an example application.

Interfacing with a CAN Bus

It is uncommon to see computing devices (PCs, laptops etc) with native CAN bus interfaces provided — not to say that they do not exist such as in some Dell embedded computers — so typically some form of USB adapter has to be used. Various development kits and boards exist that allow microcontrollers to connect to a network, which are far more common.

USB Interfaces

USB-CAN interfaces are ubiquitous and vary in price from as low as the cost of a small development board and PHY combination, through to professional devices manufactured by companies such as Kvaser.

The Lowest Cost Option: An Inexpensive Development Board with CAN PHY

 

Chances are, if you are a “hacker” or “maker” type, you will have one or more development boards such as an Arduino or STM32 “Blue pill” to hand. These boards combined with a low-cost CAN PHY breakout can make for a quick and dirty adapter to sniff and interact with a CAN bus.

In the case of an Arduino (or other microcontroller that does not integrate a CAN controller) an external controller will be required to handle the bus interactions, as well as the PHY to provide the electrical interface. Seeed Studio have produced a CAN bus shield (174-3227)

at a low cost of £29 that includes CAN controller, PHY and an additional microSD card socket that can be used.

The chosen CAN controller is the ubiquitous MCP2515 from Microchip that supports CAN 2.0A and B at up to 1Mbps. Five buffers are present within the chip, with three dedicated to transmit and two for receive. Two acceptance masks and six filters are also provided, which can be used to select only certain message identifiers, to reduce the workload on the connected microcontroller.

This interface is not isolated, meaning that there is a galvanic connection between the bus itself and the connected microcontroller — in particularly electrically harsh environments this could cause issues, but given the price point this is not to be expected.

A Budget Turnkey USB Interface: The Innomaker MS123

 

If rolling your own CAN bus interface is too much and a plug-and-play solution is required, Innomaker have released a small USB to CAN interface (230-5617)

that features isolation all for the low sum of £39.

Support for Linux SocketCAN is included out-of-the-box, which makes getting started with common tools very quick and easy. The supported CAN specifications include 2.0A (standard 11-bit frames), 2.0B (extended 29-bit frames) and ISO 11898-2, which is High Speed CAN up to 1Mbps. Given the low price, the lack of CAN-FD support is unsurprising.

Isolation on the MS123 is rated at 1.5kV DC for one minute, or 3kV DC for one second, which will handle all but the worst electrical noise on a bus. An on-board termination resistor is also provided, selectable by a pin jumper.

A Mid-Range Option: The Innomaker MS125

 

With a feature set similar to the Innomaker MS123, the MS125 (252-6693)

is priced at £132 and includes two CAN channels and improved isolation and ESD protection. Innomaker have kept support for SocketCAN, as well as introducing a DLL suitable for building Windows applications in C

The improved isolation and ESD voltage ratings mean that up to 3kV DC isolation and 15kV ESD protection is offered. Again, much like the MS123, switchable 120 ohm termination resistors are provided on each port.

The High End Option: Ixxat USB-to-CAN V2 Compact

 

Coming in at £372, the Ixxat USB-CAN adapter has a number of features not found in the lower-cost options discussed previously. Supported CAN bus standards include 2.0A and 2.0B, plus ISO 11898-2 for bus speeds up to 1Mbps.

ESD protection is once again offered, along with galvanic isolation rated to 1kV DC for one second, and 500V AC for one minute. A feature not seen on the other adapters is high resolution timestamping of CAN messages — the stated resolution is 150-250 microseconds.

Software support includes a Windows VCI driver, SocketCAN or ECI for Linux, as well as support for embedded real-time operating systems QNX and VxWorks.

Considerations When Selecting CAN Interface Devices

A number of considerations should be made when selecting appropriate parts to interface with a CAN bus, such as galvanic isolation, bus operation modes and speeds, and the microcontroller interface.

Galvanic Isolation

Given the nature of the environments in which CAN bus is usually deployed, for example in engine bays around spark plugs and alternators, galvanic isolation may be required to minimise the impact of ESD events on the connected device.

Isolation can be achieved in a number of ways, ranging from using a discrete digital isolator and standard CAN transceiver, through to a dedicated isolated transceiver that integrates both parts in one chip. One such example is the Texas Instruments ISO1044 transceiver that supports CAN 2.0A/B, ISO 11898-2 and CAN FD all in a low-cost, compact eight-pin package.

Bus Modes

Another important consideration is bus operation mode, which could be as simple as CAN 2.0A or 2.0B which are 11- and 29-bit extended frames respectively, through to CAN-FD support which gives data rates up to 5Mbps, or even ISO 11898-3 which provides fault tolerance.

Fault tolerance often requires greater care when choosing transceivers due to the lack of widespread usage, whereas more “traditional” CAN buses are more widely used and supported.

Microcontroller Interface

It is increasingly common to find microcontrollers with one or more on-die CAN controllers that only require an external transceiver to work. However, many smaller eight-bit microcontrollers do not have embedded controllers.

Typically an external controller is interfaced using SPI at quite a high speed, depending on the CAN bus speed. For example, the Microchip MCP2518FD (a replacement for the now-Not Recommended For New Designs MCP2515) features classical CAN and CAN-FD support at up to 8Mbps, requiring an SPI bus clock speed of 17MHz.

An embedded controller will typically only require a TXRX signal to an external transceiver, although some also support a standbyshutdown signal to reduce power consumption when not in use.

High-Level Example

STMicroelectronics have a well documented API with examples for the on-chip “BxCAN” controllers.

 

The controller itself features support for CAN 2.0A and 2.0B with a bit rate up to 1Mbps. Three transmit queues are provided with a configurable priority, ensuring the most important messages get sent to the bus first. Two receive buffers are provided, each with three stages and fourteen filter banks which allow only messages matching certain CAN IDs to be passed to the microcontroller — reducing CPU load and time spent processing messages.

Four dedicated interrupt vectors are provided to help manage the CAN subsystem, including an interrupt on transmission, both FIFO buffers and status changes.

 

Various modes of operation are available, including a mode with three different tests that can be used to perform a self-test as well as observing bus traffic.

 

As the CAN controllers across different STM32 series can change in terms of features, the HAL device driver documentation is slightly different. The documentation is thorough and details what each method, type and function does.

Various examples of using the CAN controllers are available on GitHub, from Keil and bundled within the STM32Cube microcontroller package files, typically with multiple examples demonstrating the various different controller features. Conclusion

In this article, we’ve taken a look at various different devices and parts for interfacing with a CAN bus and the features they offer. We’ve also explored important points to remember when selecting a suitable interface that will affect performance of a device.

We’ve also taken a look at the documentation and some examples provided by STMicroelectronics for use with their “bxCAN” on-chip CAN controllers.