Arduino library for controlling the MCP2515 in order to receive/transmit CAN frames.

107-Arduino-MCP2515

Arduino Library Badge Compile Examples Check Arduino Check keywords.txt General Formatting Checks Spell Check

Arduino library for controlling the MCP2515 in order to receive/transmit CAN frames. This library is prepared to interface easily with libcanard for using UAVCAN on Arduino via 107-Arduino-UAVCAN.

This library works for

Example

#include <SPI.h>
#include <ArduinoMCP2515.h>
/* ... */
static int const MKRCAN_MCP2515_CS_PIN  = 3;
static int const MKRCAN_MCP2515_INT_PIN = 7;
/* ... */
void onReceiveBufferFull(uint32_t const timestamp_us, uint32_t const id, uint8_t const * data, uint8_t const len)
{
  Serial.println(id, HEX);
}
void onTransmitBufferEmpty(ArduinoMCP2515 * this_ptr)
{
  /* You can use this callback to refill the transmit buffer via this_ptr->transmit(...) */
}
/* ... */
ArduinoMCP2515 mcp2515([](){ digitalWrite(MKRCAN_MCP2515_CS_PIN, LOW); },
                       [](){ digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH); },
                       [](uint8_t const d) -> uint8_t { return SPI.transfer(d); },
                       micros,
                       onReceiveBufferFull,
                       onTransmitBufferEmpty);
/* ... */
void setup()
{
  Serial.begin(9600);
  while(!Serial) { }

  SPI.begin();
  pinMode(MKRCAN_MCP2515_CS_PIN, OUTPUT);
  digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH);

  pinMode(MKRCAN_MCP2515_INT_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(MKRCAN_MCP2515_INT_PIN), [](){ mcp2515.onExternalEventHandler(); }, FALLING);

  mcp2515.begin();
  mcp2515.setBitRate(CanBitRate::BR_250kBPS_16MHZ);
  mcp2515.setNormalMode();
}

void loop()
{
  uint8_t const data[8] = {0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF};
  mcp2515.transmit(1 /* id */, data, 8 /* len */);
  delay(100);
}
Comments
  • Nano 33 BLE, Example locks up

    Nano 33 BLE, Example locks up

    :bug: Bug Report

    On a Nano 33 BLE with a MPC2515, the UAVCAN-Heartbeat-Publish example locks up. The problem appears to be related to locking and/or interrupts, as it hangs when trying to aquire a lock. I managed to get it work by commenting out the locking code and disabling the interrupt.

    I'm not entirely certain if this is truely a software bug. Perhaps this could also be caused by a wiring issue or misconfiguration?

    // CritSec-mbed.cpp
    extern "C" void crit_sec_enter() {}
    extern "C" void crit_sec_leave() {}
    
    // UAVCAN-Heartbeat-Publish.ino in void setup()
    pinMode(MKRCAN_MCP2515_INT_PIN, INPUT_PULLUP);
    // attachInterrupt(digitalPinToInterrupt(MKRCAN_MCP2515_INT_PIN), onExternalEvent, FALLING);
    

    Minimum configuration to replicate the bug

    • Build Toolchain: 1.8.15
    • Arduino Core: Mbed Nano 2.5.2
    • Library Version: 107-Arduino-UAVCAN 1.4.0, 107-Arduino-MCP2515 1.3.2
  • "Stale" interrupt situation using sniffer example. ESP32

    I am curious if anyone has observed something like this...

    When running the sniffer example I get a few transmissions and then it stops and I find that the MCP2515 Interrupt pin is LOW. It appears to be a missed interrupt which stops the library. I added some test code to check for the stale interrupt pin and run the handler to get things going again and it works fine.

    Note that it is possible some other hardware event is the root cause. I just found it an interesting observation.

    Setup: Custom Board, ESP32 with MCP2515 and MCP2551 to an automotive canbus. Using HSPI on the ESP32 for the MCP2515 Interface.

  • Added a TXBnCTRL bit checker before sending the next CAN frame

    Added a TXBnCTRL bit checker before sending the next CAN frame

    This PR addresses the use of the library while transmitting UAVCAN frames at high speed (single and multi frame messages).

    The issue was found when tryint to transmit IMU data at +50Hz and it was observed that many frames where propped on the transport layer. Investigation with candump showed that the error was in the message itself. by adding a small delay between frame transmission, they number of error layers dropeed significantly.

    I did this the simplest way I could think of, so if there are any mods that are needed for more compatibility, or better ways to acheive this, please let me know and I am happy to make the changes.

    Before the fix 1152 Transport Layer errors in less than a minute of up time image

    After applying the fix image

  • use 29-bit CAN-IDs

    use 29-bit CAN-IDs

    at the moment the library uses 11-bit CAN-IDs but UAVCAN uses extended 29-bit CAN-IDs. See this forum post: https://forum.uavcan.org/t/receiving-heartbeat-message-with-pyuavcan-cli/1035/4?u=generationmake

  • Interrupt always low

    Interrupt always low

    Hi,

    I've been debugging this for way too long and need help. I am able to send CAN frames out onto the CAN bus, however I am not able to receive frames. That is because the interrupt pin on the MCP2515 is constant low. The low state is triggered if I send a CAN frame or if there is any traffic on the bus. When the interrupt pin is in its low state the arduino stalls and i have to reset both the arduino and the MCP2515.

    If I instead disconnect the interrupt pin from the arduino 33 BLE I can send multiple frames no problem. However the interrupt pin on the MCP2515 is still low which it shouldn't be. I am using the example code and have tried both the sniffer and loopback examples with giving me the same behavior.

    I'm at a loss at how to proceed. Any suggestions are very much appreciated!

  • Support libcanard defined types for receiving/transmitting

    Support libcanard defined types for receiving/transmitting

    This PR adds the feature of automatically supporting the libcanard defined CanardFrame type for receiving/transmitting of CAN frames when libcanard is present in the build project. Therefore this PR prepares the repository being included in UAVCAN/platform_specific_components as dicusssed in https://github.com/UAVCAN/platform_specific_components/issues/15 .

    What do you think @pavel-kirienko ?

  • Fix: Only enable receive interrupts when a onReceive callback has been provided.

    Fix: Only enable receive interrupts when a onReceive callback has been provided.

    This reduces the amount of SPI transactions in a network with heavy CAN traffic where no reception by a CAN node is required.

    @generationmake can you please test?

  • [Feature Request] Documentation on how to use

    [Feature Request] Documentation on how to use "CanBitRate" enumeration

    ⚡ Feature Request

    Please Describe The Problem To Be Solved I apologize if my google-fu is just failing me and this is documented somewhere, but I'm not sure what to select for the "CanBitRate" during initialization. For example, "BR_500kBPS_16MHZ"... I imagine that the 500kBPS is the can bus speed, but the "_16MHZ"... what does that refer to? I am wondering if this pertains to the oscillator that is used on the can bus interface chip. For example, my MCP2515 boards have an 8MHz oscillator on them. Should I change this enumeration to use the "_8MHZ" variants? Or is this frequency something else related to the canbus that I don't understand?

    (Optional): Suggest A Solution Update the example programs in "examples" directory to have a comment around the lines suggesting how users set the CanBitRate enumeration. Also the example in the README.md file.

  • is this compile error due to missing support?

    is this compile error due to missing support?

    Hi

    I am getting this error:

    sketch\ArduinoMCP2515.cpp.o:(.text._ZN7MCP251514MCP2515_Config13enableIntFlagENS_7CANINTEE[MCP2515::MCP2515_Config::enableIntFlag(MCP2515::CANINTE)]+0x0): undefined reference to MCP2515::MCP2515_Io::setBit(MCP2515::Register, unsigned char)

    Could this be because I am trying to build for ESP8266 which may not be supported? Or did I do something dumb?

  • Feature Request #60: Add inline documentation for CanBitRate

    Feature Request #60: Add inline documentation for CanBitRate

    added some small inline comments to provoke developers to make sure they adjust these lines and also explain that the suffix frequency is related to the oscillator/clock rate of the MCP2515 chip they are using.

  • Cannot connect to MCP215 with the Rasbpery Pi pico board

    Cannot connect to MCP215 with the Rasbpery Pi pico board

    Hello, I have been trying to use this library along with UAV CAN to make some proof of concepts for a project. I have not been able to use this library along with the ArduinoCore-mbed (using the RP2040). I just dont manage to get any communications via CAN even with the loopback example. The is no error message, just there is no communications. My current wiring is:

    static const byte MCP2515_SCK   = 18;
    static const byte MCP2515_MOSI  = 19;
    static const byte MCP2515_MISO  = 16;
    static const byte MCP2515_CS    = 17;
    static const byte MCP2515_INT   = 6;
    

    I have been able to have both examples working with the standard arduino-pico library with the same wiring mentioned before, the issue is that it seems that that library is not compatible withe the UAVCAN library as when I try to compile the UAVCAN examples I get the following errors:

    Error while detecting libraries included by /home/pepe/Arduino/libraries/107-Arduino-UAVCAN/src/libcanard/canard.c
    
    Error while detecting libraries included by /home/pepe/Arduino/libraries/107-Arduino-UAVCAN/src/libcanard/canard_dsdl.c
    
    Error while detecting libraries included by /home/pepe/Arduino/libraries/107-Arduino-UAVCAN/src/o1heap/o1heap.c
    
    home/pepe/.arduino15/packages/rp2040/tools/pqt-gcc/1.3.1-a-7855b0c/bin/../lib/gcc/arm-none-eabi/10.3.0/../../../../arm-none-eabi/bin/ld: /tmp/arduino_build_632379/sketch/UAVCAN-Heartbeat-Publish.ino.cpp.o: in function `_ZN9LockGuardC4Ev':
    /home/pepe/Arduino/libraries/107-Arduino-UAVCAN/src/utility/LockGuard.h:24: undefined reference to `crit_sec_enter'
    /home/pepe/.arduino15/packages/rp2040/tools/pqt-gcc/1.3.1-a-7855b0c/bin/../lib/gcc/arm-none-eabi/10.3.0/../../../../arm-none-eabi/bin/ld: /tmp/arduino_build_632379/sketch/UAVCAN-Heartbeat-Publish.ino.cpp.o: in function `_ZN9LockGuardD4Ev':
    /home/pepe/Arduino/libraries/107-Arduino-UAVCAN/src/utility/LockGuard.h:25: undefined reference to `crit_sec_leave'
    /home/pepe/.arduino15/packages/rp2040/tools/pqt-gcc/1.3.1-a-7855b0c/bin/../lib/gcc/arm-none-eabi/10.3.0/../../../../arm-none-eabi/bin/ld: /tmp/arduino_build_632379/libraries/107-Arduino-UAVCAN/ArduinoUAVCAN.cpp.o: in function `_ZN9LockGuardC4Ev':
    /home/pepe/Arduino/libraries/107-Arduino-UAVCAN/src/utility/LockGuard.h:24: undefined reference to `crit_sec_enter'
    /home/pepe/.arduino15/packages/rp2040/tools/pqt-gcc/1.3.1-a-7855b0c/bin/../lib/gcc/arm-none-eabi/10.3.0/../../../../arm-none-eabi/bin/ld: /tmp/arduino_build_632379/libraries/107-Arduino-UAVCAN/ArduinoUAVCAN.cpp.o: in function `_ZN9LockGuardD4Ev':
    /home/pepe/Arduino/libraries/107-Arduino-UAVCAN/src/utility/LockGuard.h:25: undefined reference to `crit_sec_leave'
    collect2: error: ld returned 1 exit status
    
    

    Any pointers would be greatly appreciated

  • [Bug Report]Try to use it on esp32c3 but not work

    [Bug Report]Try to use it on esp32c3 but not work

    :bug: Bug Report

    Describe the bug

    Datasheet: Esp32c3 datasheet Hi, I am trying use this lib on esp32c3 on arduino which has 3 spi.spi 2 is for normal use. By compare de loopback example. I am define cs pin 10, int pin 3. sck pin 6, mosi pin 7, miso pin 2. replace SPI.begin() to SPI.begin(6,2,7,-1); Everything looks working but cannot call int function onReceiveBufferFull.

    I am adding this code and it shows ok

    if(!mcp2515.transmit(frame.id, frame.data, frame.len)) {
                              Serial.println("ERROR TX");
                          }else{
                              Serial.println("OK TX");
                          }
    

    I tried other mcp2515 library and get the same result. I am also tried anoter pin define. becuz esp32c3 said spi2 could be any pins. But nothing works.

    Minimum example code to replicate the bug

    void setup()
    {
        pinMode (12, OUTPUT) ;
        digitalWrite (12, HIGH) ;
    //--- Start serial
        Serial.begin (115200) ;
        while (!Serial);
    
        /* Setup SPI access */
        SPI.begin(6,2,7,-1);
        pinMode(MKRCAN_MCP2515_CS_PIN, OUTPUT);
        digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH);
    
        /* Attach interrupt handler to register MCP2515 signaled by taking INT low */
        pinMode(MKRCAN_MCP2515_INT_PIN, INPUT_PULLUP);
        attachInterrupt(digitalPinToInterrupt(MKRCAN_MCP2515_INT_PIN), [](){ mcp2515.onExternalEventHandler(); }, LOW);
    
        mcp2515.begin();
        mcp2515.setBitRate(CanBitRate::BR_250kBPS_16MHZ); // CAN bit rate and MCP2515 clock speed
        mcp2515.setLoopbackMode();
    
        std::for_each(CAN_TEST_FRAME_ARRAY.cbegin(),
                      CAN_TEST_FRAME_ARRAY.cend(),
                      [](sCanTestFrame const frame)
                      {
                          if(!mcp2515.transmit(frame.id, frame.data, frame.len)) {
                              Serial.println("ERROR TX");
                          }else{
                              Serial.println("OK TX");
                          }
                          delay(1000);
                      });
    }
    

    the terminal just shows: OK TX OK TX OK TX OK TX OK TX OK TX OK TX

    Minimum configuration to replicate the bug Please provide the configuration data used to build Snowfox:

    • Build Toolchain:Arduino IDE 2.0 and PlatformIO+CLion, both tried.
    • Arduino Core: framework-arduinoespressif32 @ 3.20005.220925 (2.0.5)
    • Library Version (of all involved libraries): 1.3.5
  • Portenta with MKR CAN shield

    Portenta with MKR CAN shield

    Hello! I'm trying to receive CAN frames with the Portenta and the MKR CAN shield using this library. However, I'm not able to receive anything. I'm trying the CAN sniffer example. The code compiles, but I do not see any data in the serial terminal. I visualize the CAN frame in an oscilloscope and it seems to be fine. Any idea why it might not be working? Update: I noticed that once the CAN frame starts to arrive to the Portenta, it enters in some sort of error mode (the RGB LED starts to blink in red). I even tried the loopback example without any physical CAN bus connected to the Portenta, and it keeps crashing.

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
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
Send and receive MIDI messages over Ethernet (rtpMIDI or AppleMIDI)
Send and receive MIDI messages over Ethernet (rtpMIDI or AppleMIDI)

AppleMIDI (aka rtpMIDI) for Arduino Enables an Arduino with IP/UDP capabilities (Ethernet shield, ESP8266, ESP32, ...) to participate in an AppleMIDI

Dec 29, 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
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 (CAN

Dec 9, 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
Distribution of Arduino driver for MCP2517FD CAN controller (CANFD mode)

MCP2517FD and MCP2518FD CAN Controller Library for Arduino (in CAN FD mode) Compatibility with the other ACAN libraries This library is fully compatib

Dec 21, 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
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
Arduino library for MQTT support

Adafruit MQTT Library Arduino library for MQTT support, including access to Adafruit IO. Works with the Adafruit FONA, Arduino Yun, ESP8266 Arduino pl

Jan 6, 2023