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 above demonstrates how to use CAN.

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

using namespace OI;

Core core;

CAN_Message_t rx_msg;
CAN_Message_t tx_msg;

void setup()
{
    Serial.begin(115200);
    
    /* Init CAN */
    core.can.begin();

    tx_msg.id = 11;
    tx_msg.size = 8;
    tx_msg.msg[0] = 'A';
    tx_msg.msg[1] = 'U';
    tx_msg.msg[2] = 'R';
    tx_msg.msg[3] = 'E';
    tx_msg.msg[4] = 'L';
    tx_msg.msg[5] = 'I';
    tx_msg.msg[6] = 'E';
    tx_msg.msg[7] = 'N';

    tx_msg.IDE = false; // Standard ID, not extended
    tx_msg.RTR = false; // No remote request frame
    
    core.can.write(tx_msg);
    core.can.setStandardFilter(0xFF0, 0x0AA);
}

void loop()
{
    if(core.can.available())
    {
        rx_msg = core.can.read();
        Serial.printf("NEW MESSAGE // ID : %d, length : %d MSG :  ", rx_msg.id, rx_msg.size);
        for(int i =0; i <rx_msg.size; i++)
        {
            Serial.printf("%x", rx_msg.msg[i]);
        }
        Serial.printf(" // \n");
        
    }
    delay(100);
}

The example code above demonstrates how to use CAN extended.

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

using namespace OI;

OICore core;

CAN_Message_t rx_msg;
CAN_Message_t tx_msg;

void setup()
{
    Serial.begin(115200);
    
    /* Init CAN */
    core.can.begin(1000000, true);

    tx_msg.id = 11111;
    tx_msg.size = 8;
    tx_msg.msg[0] = 'A';
    tx_msg.msg[1] = 'U';
    tx_msg.msg[2] = 'R';
    tx_msg.msg[3] = 'E';
    tx_msg.msg[4] = 'L';
    tx_msg.msg[5] = 'I';
    tx_msg.msg[6] = 'E';
    tx_msg.msg[7] = 'N';

    tx_msg.IDE = true; // Extended ID, not standard
    tx_msg.RTR = false; // No remote request frame
    
    core.can.write(tx_msg);
    core.can.setExtendedFilter(0xFFF0, 0xAAA0);
}

void loop()
{
    if(core.can.available())
    {
        rx_msg = core.can.read();
        Serial.printf("NEW MESSAGE // ID : %d, length : %d MSG :  ", rx_msg.id, rx_msg.size);
        for(int i =0; i <rx_msg.size; i++)
        {
            Serial.printf("%x", rx_msg.msg[i]);
        }
        Serial.printf(" // \n");
        
    }
    delay(100);
}

Software API

void OI::CAN::begin(unsigned long baudrate = 1000000, bool extended_mode = false)
void OI::CAN::end(void)
int OI::CAN::available(void)
CAN_Message_t OI::CAN::read(void)
void OI::CAN::write(CAN_Message_t msg)
void OI::CAN::setStandardFilter(uint16_t mask, uint16_t filter)
void OI::CAN::setExtendedFilter(uint32_t mask, uint32_t filter)