Skip to content

Latest commit

 

History

History
 
 

CAN

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

This is an Arduino library for XMC boards using CAN bus to send and receive data.

Based on Arduino CAN library/ Github Repo

Hardware Setup

XMC boards with CAN support (CAN transceiver on board and CAN controller module): XMC4200 Platform2GO, XMC4400 Platform2GO, XMC4700 Relax Kit and XMC1400 XMC2GO.

⚠️ There is a 120 ohm terminal resistor on the XMC4200 Platform2GO and XMC1400 XMC2Go boards, not on the XMC4400 Platform2GO and XMC4700 Relax Kit. Please remove or add resistors as needed.

All CAN nodes should be connected using the (twisted) pair cable for the CAN_H and CAN_L

Software Usage

There are examples. See the API definition below for more details.

CAN API

Here the API definition is based on:

⚠️ XMC specification

Due to the different behavior of xmc4 series and xmc1 series, the library and provided examples was tested primarily on xmc1400 2go.

The known difference is that the XMC4 is unable to receive CAN messages for all IDs. Therefore, it is necessary to define the device ID using the following function:

Set device id

  CAN.setDeviceId(id);
  • id - 11 bits standard id of the device. (default: 0xFF)

Include Library

#include <CAN.h>

Setup

Begin

Initialize the library with the specified bit rate.

CAN.begin(bitrate);
  • bitrate - bit rate in bits per seconds (bps) (1000E3, 500E3(default), 250E3, 200E3, 125E3, 100E3, 80E3, 50E3, 40E3, 20E3, 10E3, 5E3)

Returns 1 on success, 0 on failure.

Set pins

The RX and TX pins are determined.

End

Stop the library

CAN.end()

Sending data

Begin packet

Start the sequence of sending a packet.

CAN.beginPacket(id);
CAN.beginPacket(id, dlc);
CAN.beginPacket(id, dlc, rtr);

CAN.beginExtendedPacket(id);
CAN.beginExtendedPacket(id, dlc);
CAN.beginExtendedPacket(id, dlc, rtr);
  • id - 11-bit id (standard packet) or 29-bit packet id (extended packet)
  • dlc - (optional) value of Data Length Code (DLC) field of packet, default is size of data written in packet
  • rtr - (optional) value of Remote Transmission Request (RTR) field of packet (false or true), defaults to false. RTR packets contain no data, the DLC field of the packet represents the requested length.

Returns 1 on success, 0 on failure.

Writing

Write data to the packet. Each packet can contain up to 8 bytes.

CAN.write(byte);

CAN.write(buffer, length);
  • byte - single byte to write to packet

or

  • buffer - data to write to packet
  • length - size of data to write

Returns the number of bytes written.

Note: Other Arduino Print API's can also be used to write data into the packet

End packet

End the sequence of sending a packet.

CAN.endPacket()

Returns 1 on success, 0 on failure.

Receiving data

Parsing packet

Check if a packet has been received.

int packetSize = CAN.parsePacket(id);
  • id - 11-bit id (standard packet) or 29-bit packet id (extended packet)

Returns the packet size in bytes or 0 if no packet was received. For RTR packets the size reflects the DLC field of the packet.

Register callback

Register a callback function for when a packet is received.

CAN.onReceive(onReceive);

void onReceive(int packetSize) {
 // ...
}
  • onReceive - function to call when a packet is received.

Packet ID

long id = CAN.packetId();

Returns the id (11-bit or 29 bit) of the received packet. Standard packets have an 11-bit id, extended packets have an 29-bit id.

Packet Extended

bool extended = CAN.packetExtended();

Returns true if the received packet is extended, false otherwise.

Packet RTR

bool rtr = CAN.packetRtr();

Returns the value of the Remote Transmission Request (RTR) field of the packet true/false. RTR packets contain no data, the DLC field is the requested data length.

Packet DLC

int DLC = CAN.packetDlc();

Returns the value of the Data Length Code (DLC) field of the packet.

Available

int availableBytes = CAN.available()

Returns number of bytes available for reading.

Peeking

Peek at the next byte in the packet.

int b = CAN.peek();

Returns the next byte in the packet or -1 if no bytes are available.

Reading

Read the next byte from the packet.

int b = CAN.read();

Returns the next byte in the packet or -1 if no bytes are available.

Note: Other Arduino Stream API's can also be used to read data from the packet

Filtering

Filter packets that meet the desired criteria.

CAN.filter(id);
CAN.filter(id, mask);

CAN.filterExtended(id);
CAN.filterExtended(id, mask);
  • id - 11-bit id (standard packet) or 29-bit packet id (extended packet)
  • mask - (optional) 11-bit mask (standard packet) or 29-bit mask (extended packet), defaults to 0x7ff or 0x1fffffff (extended)

Only packets that meet the following criteria are acknowleged and received, other packets are ignored:

if ((packetId & mask) == id) {
  // acknowleged and received
} else {
  // ignored
}

Returns 1 on success, 0 on failure.

Other modes

Loopback mode

Put the CAN controller in loopback mode, any outgoing packets will also be received.

CAN.loopback();

Sleep mode

Put the CAN contoller in sleep mode.

CAN.sleep();

Wake up the CAN contoller if it was previously in sleep mode.

CAN.wakeup();