This is an Arduino library for XMC boards using CAN bus to send and receive data.
Based on Arduino CAN library/ Github Repo
XMC boards with CAN support (CAN transceiver on board and CAN controller module): XMC4200 Platform2GO, XMC4400 Platform2GO, XMC4700 Relax Kit and XMC1400 XMC2GO.
All CAN nodes should be connected using the (twisted) pair cable for the CAN_H and CAN_L
There are examples. See the API definition below for more details.
Here the API definition is based on:
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:
CAN.setDeviceId(id);
id
- 11 bits standard id of the device. (default:0xFF
)
#include <CAN.h>
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.
The RX and TX pins are determined.
Stop the library
CAN.end()
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 packetrtr
- (optional) value of Remote Transmission Request (RTR) field of packet (false
ortrue
), defaults tofalse
. RTR packets contain no data, the DLC field of the packet represents the requested length.
Returns 1
on success, 0
on failure.
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 packetlength
- 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 the sequence of sending a packet.
CAN.endPacket()
Returns 1
on success, 0
on failure.
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 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.
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.
bool extended = CAN.packetExtended();
Returns true
if the received packet is extended, false
otherwise.
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.
int DLC = CAN.packetDlc();
Returns the value of the Data Length Code (DLC) field of the packet.
int availableBytes = CAN.available()
Returns number of bytes available for reading.
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.
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
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 to0x7ff
or0x1fffffff
(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.
Put the CAN controller in loopback mode, any outgoing packets will also be received.
CAN.loopback();
Put the CAN contoller in sleep mode.
CAN.sleep();
Wake up the CAN contoller if it was previously in sleep mode.
CAN.wakeup();