Mistake on this page?
Report an issue in GitHub or email us

CAN

CAN class hierarchy

Controller-Area Network (CAN) is a bus standard that allows microcontrollers and devices to communicate with each other without going through a host computer.

Note: You can use the CAN interface to write data words out of a CAN port. It will return the data received from another CAN device. You can configure the CAN clock frequency.

CAN class reference

Public Member Functions
 CAN (PinName rd, PinName td)
 Creates a CAN interface connected to specific pins. More...
 CAN (PinName rd, PinName td, int hz)
 Initialize CAN interface and set the frequency. More...
 CAN (const can_pinmap_t &pinmap)
 Initialize CAN interface. More...
 CAN (const can_pinmap_t &pinmap, int hz)
 Initialize CAN interface and set the frequency. More...
int frequency (int hz)
 Set the frequency of the CAN interface. More...
int write (CANMessage msg)
 Write a CANMessage to the bus. More...
int read (CANMessage &msg, int handle=0)
 Read a CANMessage from the bus. More...
void reset ()
 Reset CAN interface. More...
void monitor (bool silent)
 Puts or removes the CAN interface into silent monitoring mode. More...
int mode (Mode mode)
 Change CAN operation to the specified mode. More...
int filter (unsigned int id, unsigned int mask, CANFormat format=CANAny, int handle=0)
 Filter out incoming messages. More...
unsigned char rderror ()
 Detects read errors - Used to detect read overflow errors. More...
unsigned char tderror ()
 Detects write errors - Used to detect write overflow errors. More...
void attach (Callback< void()> func, IrqType type=IrqType::RxIrq)
 Attach a function to call whenever a CAN frame received interrupt is generated. More...

CAN hello, world

This example sends a counter from one CAN bus (can1) and listens for a packet on the other CAN bus (can2). Each bus controller should be connected to a CAN bus transceiver. These should be connected together at a CAN bus.

/*
 * Copyright (c) 2017-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */

#if !DEVICE_CAN
#error [NOT_SUPPORTED] CAN not supported for this target
#endif


#include "mbed.h"


Ticker ticker;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
/** The constructor takes in RX, and TX pin respectively.
  * These pins, for this example, are defined in mbed_app.json
  */
CAN can1(MBED_CONF_APP_CAN1_RD, MBED_CONF_APP_CAN1_TD);
CAN can2(MBED_CONF_APP_CAN2_RD, MBED_CONF_APP_CAN2_TD);
char counter = 0;

void send()
{
    printf("send()\n");
    if (can1.write(CANMessage(1337, &counter, 1))) {
        printf("wloop()\n");
        counter++;
        printf("Message sent: %d\n", counter);
    }
    led1 = !led1;
}

int main()
{
    printf("main()\n");
    ticker.attach(&send, 1);
    CANMessage msg;
    while (1) {
        printf("loop()\n");
        if (can2.read(msg)) {
            printf("Message received: %d\n", msg.data[0]);
            led2 = !led2;
        }
        ThisThread::sleep_for(200);
    }
}


Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.