CAN

Description

This features is present on OI-Core/OI-CoreLite (x1).

This is a standard CAN 2.0B bus.

A termination resistor of 120Ω is built into the module between CAN_H and CAN_L.

The CAN controller adheres to the standard ISO 11989-1, while the CAN transceiver complies with standards ISO-11898-2 and ISO-11898-5. The bus speed can be set up to 1Mbit/sec (user-selectable in code). The CAN functionality is managed internally via SPI, but there’s no need to worry as a ready-to-use API is already implemented to assist you.

Code examples

The example code below demonstrates how to use CAN (standard frame).

#include "OpenIndus.h"
#include "Arduino.h"

Core core;

CAN_Message_t rxMsg;
CAN_Message_t txMsg;

void setup(void)
{
    Serial.begin(115200);
    
    core.can.begin(); // Initialize CAN peripheral without args :  default speed is 1 Mbps, normal mode, ONE-SHOT enabled

    txMsg.id = 11;
    txMsg.size = 8;
    txMsg.msg[0] = 'A';
    txMsg.msg[1] = 'U';
    txMsg.msg[2] = 'R';
    txMsg.msg[3] = 'E';
    txMsg.msg[4] = 'L';
    txMsg.msg[5] = 'I';
    txMsg.msg[6] = 'E';
    txMsg.msg[7] = 'N';
    txMsg.IDE = false; // Standard ID
    txMsg.RTR = false; // No remote request frame
    
    core.can.write(txMsg);
    core.can.setStandardFilter(0xFF0, 0x0AA);
}

void loop(void)
{
    if(core.can.available()) {
        rxMsg = core.can.read();
        Serial.printf("ID: %lx Size: %d Data: ", rxMsg.id, rxMsg.size);
        for(int i =0; i <rxMsg.size; i++) {
            Serial.printf("%x", rxMsg.msg[i]);
        }
        Serial.printf("\n");    
    }
    delay(100);
}

Note

To use CAN Extended mode, simply modify the message identifier and the relevant flags in the example above to use an extended identifier (29 bits) and enable the extended frame flag in the CAN API. Refer to the API documentation for the exact parameters to adjust.

Software API

class CAN

CAN bus class.

Public Functions

CAN(spi_host_device_t host, gpio_num_t cs, gpio_num_t intr)

Construct a new CAN object.

Parameters:
  • host – SPI host device

  • cs – Chip select pin

  • intr – Interrupt pin

void begin(unsigned long baudrate = 1000000, bool one_shot_mode = true, bool extended_mode = false)

Initialize the CAN communication.

Note

Protocol recommendations:

  • CANopen/DeviceNet: one_shot_mode=false, extended_mode=false (Standard IDs + retransmissions)

  • J1939: one_shot_mode=false, extended_mode=true (Extended IDs + retransmissions)

  • Raw CAN/Testing: one_shot_mode=true (avoid infinite loops on bus errors)

Parameters:
  • baudrateCAN baudrate in bits/s (default: 1000000) Common values: 125000, 250000, 500000, 1000000

  • one_shot_mode – Enable ONE-SHOT transmission mode (default: true)

    • true: No automatic retransmission (recommended for testing/debug) Message sent only once, no retry on error or missing ACK

    • false: Automatic retransmission (required for CANopen/J1939/DeviceNet) Message retransmitted until ACK or bus-off state

  • extended_modeCAN identifier mode (default: false)

    • false: Standard 11-bit IDs only (forces msg.IDE=false in TX)

    • true: Extended 29-bit IDs only (forces msg.IDE=true in TX)

void end(void)

Delete CAN instance.

void write(CAN_Message_t msg)

Write a CAN message to the bus.

Parameters:

msgCAN message structure

int available(void)

Get the number of messages available for reading.

Returns:

Number of available messages

CAN_Message_t read(void)

Read a CAN message from the bus.

Returns:

CAN message structure

void setStandardFilter(uint16_t mask, uint16_t filter)

Set CAN bus standard IDs filter.

Parameters:
  • maskCAN mask for ID filter

  • filterCAN IDs filter

void setExtendedFilter(uint32_t mask, uint32_t filter)

Set CAN bus extended IDs filter.

Parameters:
  • maskCAN mask for ID filter

  • filterCAN IDs filter

void setResetControl(ioex_device_t *ioex, ioex_num_t reset_pin)

Configure hardware reset control via GPIO expander.

Note

This must be called before begin() if hardware reset is desired

Parameters:
  • ioex – IO expander device handle

  • reset_pin – Reset pin number on IO expander

void reset(void)

Perform hardware reset of MCP25625.

Note

Requires setResetControl() to be called first Reset sequence: LOW (10ms) -> HIGH (release)

struct CAN_Message_t

CAN message structure.