CAN / CANFD Arduino Library for Teensy 4.0

CAN Library for Teensy 4.0 / 4.1

It handles Controller Area Network (CAN) for CAN1, CAN2 and CAN3, and Controller Area Network with Flexible Data (CANFD) for CAN3.

Compatibility with the ACAN2515 and ACAN2517 libraries

This library is fully compatible with the MCP2515 CAN Controller ACAN2515 library https://github.com/pierremolinaro/acan2515 and the MCP2517FD, MCP2518FD CAN Controllers ACAN2517 library https://github.com/pierremolinaro/acan2517, it uses a very similar API and the same CANMessage class for handling messages.

Compatibility with the ACAN2517FD library

This library is fully compatible with the MCP2517FD, MCP2518FD CAN Controllers ACAN2517FD library https://github.com/pierremolinaro/acan2517FD, it uses a very similar API and the same CANFDMessage class for handling messages.

ACAN library description

ACAN is a driver for the three FlexCAN modules of the Teensy 4.0 / 4.1 microcontroller. It supports alternate pins for CAN1.

The driver supports many bit rates, as standard 62.5 kbit/s, 125 kbit/s, 250 kbit/s, 500 kbit/s, and 1 Mbit/s. An efficient CAN bit timing calculator finds settings for them, but also for exotic bit rates as 833 kbit/s. If the wished bit rate cannot be achieved, the begin method does not configure the hardware and returns an error code.

Driver API is fully described by the PDF file in the ACAN_T4/extras directory.

Demo Sketches

The demo sketches are in the ACAN_T4/examples directory.

The LoopBackDemoCAN1 demo sketch shows how configure the CAN1module, and how to send and revceive frames.

Configuration is a four-step operation.

  1. Instanciation of the settings object : the constructor has one parameter: the wished CAN bit rate. The settings is fully initialized.
  2. You can override default settings. Here, we set the mLoopBackMode and mSelfReceptionMode properties to true, enabling to run demo code without any additional hardware (no CAN transceiver needed). We can also for example change the receive buffer size by setting the mReceiveBufferSize property.
  3. Calling the begin method configures the driver and starts CAN bus participation. Any message can be sent, any frame on the bus is received. No default filter to provide.
  4. You check the errorCode value to detect configuration error(s).
void setup () {
  pinMode (LED_BUILTIN, OUTPUT) ;
  Serial.begin (9600) ;
  while (!Serial) {
    delay (50) ;
    digitalWrite (LED_BUILTIN, !digitalRead (LED_BUILTIN)) ;
  }
  Serial.println ("CAN1 loopback test") ;
  ACAN_T4_Settings settings (125 * 1000) ; // 125 kbit/s
  settings.mLoopBackMode = true ;
  settings.mSelfReceptionMode = true ;
  const uint32_t errorCode = ACAN_T4::can1.begin (settings) ;
  if (0 == errorCode) {
    Serial.println ("can1 ok") ;
  }else{
    Serial.print ("Error can1: 0x") ;
    Serial.println (errorCode, HEX) ;
  }
}

Now, an example of the loop function. As we have selected loop back mode, every sent frame is received.

static uint32_t gBlinkDate = 0 ;
static uint32_t gSendDate = 0 ;
static uint32_t gSentCount = 0 ;
static uint32_t gReceivedCount = 0 ;

void loop () {
  if (gBlinkDate <= millis ()) {
    gBlinkDate += 500 ;
    digitalWrite (LED_BUILTIN, !digitalRead (LED_BUILTIN)) ;
  }
  CANMessage message ;
  if (gSendDate <= millis ()) {
    message.id = 0x542 ;
    const bool ok = ACAN_T4::can1.tryToSend (message) ;
    if (ok) {
      gSendDate += 2000 ;
      gSentCount += 1 ;
      Serial.print ("Sent: ") ;
      Serial.println (gSentCount) ;
    }
  }
  if (ACAN_T4::can1.receive (message)) {
    gReceivedCount += 1 ;
    Serial.print ("Received: ") ;
    Serial.println (gReceivedCount) ;
  }
}

CANMessage is the class that defines a CAN message. The message object is fully initialized by the default constructor. Here, we set the id to 0x542 for sending a standard data frame, without data, with this identifier.

The ACAN_T4::can1.tryToSend tries to send the message. It returns true if the message has been sucessfully added to the driver transmit buffer.

The gSendDate variable handles sending a CAN message every 2000 ms.

ACAN_T4::can1.receive returns true if a message has been received, and assigned to the messageargument.

Use of Optional Reception Filtering

The hardware defines two kinds of filters: primary and secondary filters. You can define up to 32 primary filters and 96 secondary filters.

This an setup example:

  ACANSettings settings (125 * 1000) ;
  ...
   const ACANPrimaryFilter primaryFilters [] = {
    ACANPrimaryFilter (kData, kExtended, 0x123456, handle_myMessage_0)
  } ;
  const ACANSecondaryFilter secondaryFilters [] = {
    ACANSecondaryFilter (kData, kStandard, 0x234, handle_myMessage_1),
    ACANSecondaryFilter (kRemote, kStandard, 0x542, handle_myMessage_2)
  } ;
  const uint32_t errorCode = ACAN_T4::can1.begin (settings,
                                               primaryFilters, 
                                               1, // Primary filter array size
                                               secondaryFilters,
                                               2) ; // Secondary filter array size

For example, the first filter catches extended data frames, with an identifier equal to 0x123456. When a such frame is received, the handle_myMessage_0 function is called. In order to achieve this by-filter dispatching, you should call ACAN_T4::can1.dispatchReceivedMessage instead of ACAN_T4::can1.receive in the loopfunction:

void loop () {
  ACAN_T4::can1.dispatchReceivedMessage () ; // Do not use ACAN_T4::can1.receive any more
  ...
}
Similar Resources

An ESP32 CAN 2.0B library

CAN Library for ESP32 ACAN_ESP32 library description ACAN_ESP32 is a driver for the CAN module built into the ESP32 microcontroller. The driver suppor

Dec 9, 2022

Arduino library for interfacing with any GPS, GLONASS, Galileo or GNSS module and interpreting its NMEA messages.

Arduino library for interfacing with any GPS, GLONASS, Galileo or GNSS module and interpreting its NMEA messages.

107-Arduino-NMEA-Parser Arduino library for interfacing with any GPS, GLONASS, Galileo or GNSS module and interpreting its NMEA messages. This library

Jan 1, 2023

Arduino library for providing a convenient C++ interface for accessing UAVCAN.

Arduino library for providing a convenient C++ interface for accessing UAVCAN.

107-Arduino-UAVCAN Arduino library for providing a convenient C++ interface for accessing UAVCAN (v1.0-beta) utilizing libcanard. This library works f

Jan 2, 2023

Arduino web server library.

aWOT Arduino web server library. Documentation 1. Getting started Hello World Basic routing Application generator Serving static files 2. Guide Routin

Jan 4, 2023

Arduino, esp32 and esp8266 library for ABB (ex PowerOne) Aurora Inverter, implement a full methods to retrieve data from the Inverter via RS-485

Arduino, esp32 and esp8266 library for ABB (ex PowerOne) Aurora Inverter, implement a full methods to retrieve data from the Inverter via RS-485

ABB Aurora protocol You can refer the complete documentation on my site ABB Aurora PV inverter library for Arduino, esp8266 and esp32 I create this li

Nov 22, 2022

Analog Devices Analog Digital Converter AD7173 Arduino library

AD7173-Arduino Analog Devices AD7173 analog digital converter Arduino library Mostly tested setup for this library: 1007 data rate external crystal co

Nov 20, 2022

Arduino library for nRF51822-based Adafruit Bluefruit LE modules

This library is for all nRF51 based Adafruit Bluefruit LE modules that use SPI or UART. Current nRF51 based Bluefruit LE products include: Bluefruit L

Nov 6, 2022

Arduino library for the Adafruit FONA

Adafruit FONA Library This library requires Arduino v1.0.6 or higher This is a library for the Adafruit FONA Cellular GSM Breakouts etc Designed speci

Dec 15, 2022

Arduino library to access Adafruit IO from WiFi, cellular, and ethernet modules.

Arduino library to access Adafruit IO from WiFi, cellular, and ethernet modules.

Adafruit IO Arduino Library This library provides a simple device independent interface for interacting with Adafruit IO using Arduino. It allows you

Dec 23, 2022
Comments
  • PIO library dependencies include error

    PIO library dependencies include error

    Hi, I hope you can help me with an issue concerning including the ACAN_T4 library in PlatformIO. Long story short, I am working on a project with two boards, a Teensy 3.6 and 4.0. I am therefore using both your ACAN.h library and your ACAN_T4.h library. My issue is that PIO doesn't seem to find the ACAN_T4. I've worked in a while with the 3.6 board and ACAN.h with no problems at all, but with ACAN_T4, I am asked to update my include path, even though I have the library in my library dependencies. I've tried to make a seperate project with only the 4.0 environment in my .ini file, and only including the pierremolinaro/ACAN_T4 @ ^1.1.1 in my library dependencies, but PIO fails to recognize it.

    I'm aware this might be a long shot, but none the less, I was wondering if you know a way to fix this issue? Thanks in advance.

  • Fixed wrong assignment of `FLEXCAN_FDCBT_PROPSEG`

    Fixed wrong assignment of `FLEXCAN_FDCBT_PROPSEG`

    According to the processor manual, the prop seg timeout is calculated from the FDCBT_PROPSEG value by multiplying it with the sclock directly. The value is not increased by one beforehand¹.

    In the reference manual the calculation of the segment time is stated as: Propagation Segment Time = FPROPSEG × Time-Quanta

    This is indeed different to the programming of the CAN2.0 Interface (CBT_PROPSEG)², where the calculation is: Propagation Segment Time = (EPROPSEG + 1) × Time-Quanta.

    Without this commit the actual frequency, with which the BRS packages are send, is to high.

    Sources: ¹: i.MX RT1060 Processor Reference Manual, Rev. 2, 12/2019 / Chapter 45.6.2.22.4 / Page 2701 ²: i.MX RT1060 Processor Reference Manual, Rev. 2, 12/2019 / Chapter 45.6.2.19.4 / Page 2695

  • Use of Libraries

    Use of Libraries

    Pierre, I am not writing this to bring up an issue, but rather to get in contact with you about giving credit. My name is Harlan Johnson. I am the Chief Technical Officer of the Formula Electric team at the Missouri University of Science & Technology. This year we have switched over to using the Teensy 4.0/4.1 as our main microcontroller for different projects. We have found your ACAN_T4 libraries to be extremely helpful and useful in several ways. Obviously, we can use them for communication on car, but the main love I the libraries is the approachability for someone who is new to programming and electrical engineering, and is just learning how CAN works. The libraries handle a lot of functions we were writing from scratch when using TI architecture. Doing things at such a low level is very difficult to understand for a new member. This has been great for new members to more easily understand and implement the technology. Since we are using your libraries for almost all of our electronics projects, we would love to give you credit for this by putting a logo of your choice on the car next to our sponsors. The sponsors have contributed money to the team, but we consider your libraries to have equal value to the team. Please let me know if you are interested, or just to give permission for us to put your name and library name on the car. You can contact me at the following email and please check out our website at the URL below:

    Email: [email protected]

    Website: https://formulaelectric.mst.edu/

    Thank you!

  • Sample Point CAN FD

    Sample Point CAN FD

    Hello I have used you CAN1CAN2CAN3FD example. I wounder how you will set the parameters to 500/500 kbit/sec and samplepoint abbrivation 80% and Data samplepoint 60%. // Arbitration segments 80% settingsFD.mArbitrationPropagationSegment = 23; settingsFD.mArbitrationPhaseSegment1 = 8; settingsFD.mArbitrationPhaseSegment2 = 8; settingsFD.mArbitrationRJW = 4;

    // Data segments 80%, Works settingsFD.mDataPropagationSegment = 23; //Tobbe settingsFD.mDataPhaseSegment1 = 8; //Tobbe settingsFD.mDataPhaseSegment2 = 8; //Tobbe settingsFD.mDataRJW = 2; //Tobbe

    // Data segments 60%, dont work // settingsFD.mDataPropagationSegment = 13; //Tobbe // settingsFD.mDataPhaseSegment1 = 10; //Tobbe // settingsFD.mDataPhaseSegment2 = 16; //Tobbe // settingsFD.mDataRJW = 2; //Tobbe

    Best regards Torbjörn Styrenius

    [email protected] +46703739887

Related tags
Arduino CAN driver for MCP2517FD CAN Controller (in CAN 2.0B mode)

MCP2517FD CAN Controller Library for Arduino (in CAN 2.0B mode) Compatibility with the other ACAN libraries This library is fully compatible with the

Dec 22, 2022
CAN Driver for Teensy 3.1 / 3.2, 3.5 and 3.6

CAN Library for Teensy 3.1 / 3.2, 3.5, 3.6 Compatibility with the ACANxxxx libraries This library is fully compatible with the MCP2515 CAN Controller

Dec 9, 2022
Arduino polyphonic synthesizer project (not a Moog) for ESP32 - STM32 - Teensy and more
Arduino polyphonic synthesizer project (not a Moog) for ESP32 - STM32 - Teensy and more

ml_synth_basic_example Arduino polyphonic synthesizer project (not a Moog) for ESP32 - STM32 - Teensy and more link to the video My intention was to m

Dec 7, 2022
Arduino Arduino library for the CloudStorage server project. The library provides easy access to server-stored values and operations.

Arduino-CloudStorage Arduino/ESP8266 library that allows you to easly store and retreive data from a remote (cloud) storage in a key/value fashion. Cl

Jan 30, 2022
Arduino library for controlling the MCP2515 in order to receive/transmit CAN frames.
Arduino library for controlling the MCP2515 in order to receive/transmit CAN frames.

107-Arduino-MCP2515 Arduino library for controlling the MCP2515 in order to receive/transmit CAN frames. This library is prepared to interface easily

Nov 16, 2022
Arduino library for the MCP2515 CAN Controller

MCP2515 CAN Controller Library for Arduino Compatibility with the ACAN library This library is fully compatible with the Teensy 3.x ACAN library https

Dec 18, 2022
Arduino library for making an IHC in or output module using an Arduino

Introduction This is an Arduino library for making an IHC in or output module using an Arduino. (IHC controller is a home automation controller made b

Mar 26, 2020
ArduinoIoTCloud library is the central element of the firmware enabling certain Arduino boards to connect to the Arduino IoT Cloud

ArduinoIoTCloud What? The ArduinoIoTCloud library is the central element of the firmware enabling certain Arduino boards to connect to the Arduino IoT

Dec 16, 2022
MCP2515 CAN Controller Driver for Arduino

MCP2515 CAN Controller Library for Arduino Compatibility with the ACAN library This library is fully compatible with the Teensy 3.x ACAN library https

Dec 13, 2022
The Approximate Library is a WiFi Arduino library for building proximate interactions between your Internet of Things and the ESP8266 or ESP32
The Approximate Library is a WiFi Arduino library for building proximate interactions between your Internet of Things and the ESP8266 or ESP32

The Approximate Library The Approximate library is a WiFi Arduino Library for building proximate interactions between your Internet of Things and the

Dec 7, 2022