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 Types
enum  Mode {
  Reset = 0, Normal, Silent, LocalTest,
  GlobalTest, SilentTest
}
enum  IrqType {
  RxIrq = 0, TxIrq, EwIrq, DoIrq,
  WuIrq, EpIrq, AlIrq, BeIrq,
  IdIrq, IrqCnt
}
Public Member Functions
 CAN (PinName rd, PinName td)
 CAN (PinName rd, PinName td, int hz)
int frequency (int hz)
int write (CANMessage msg)
int read (CANMessage &msg, int handle=0)
void reset ()
void monitor (bool silent)
int mode (Mode mode)
int filter (unsigned int id, unsigned int mask, CANFormat format=CANAny, int handle=0)
unsigned char rderror ()
unsigned char tderror ()
void attach (Callback< void()> func, IrqType type=RxIrq)
template<typename T >
void attach (T *obj, void(T::*method)(), IrqType type=RxIrq)
template<typename T >
void attach (T *obj, void(*method)(T *), IrqType type=RxIrq)
Static Public Member Functions
static void _irq_handler (uint32_t id, CanIrqType type)
Protected Member Functions
virtual void lock ()
virtual void unlock ()
Protected Attributes
can_t _can
Callback< void()> _irq [IrqCnt]
PlatformMutex _mutex

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.

#include "mbed.h"

Ticker ticker;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
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;
        } 
        wait(0.2);
    }
}

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.