api / mbed / trunk / CAN

CANMessage

class CANMessage : public CAN_Message
CANMessage
Functions
CANMessageCreates empty CAN message.
CANMessageCreates CAN message with specific content.
CANMessageCreates CAN remote message.
Variables
idThe message id.
dataSpace for 8 byte payload.
lenLength of data in bytes.
formatDefines if the message has standard or extended format.
typeDefines the type of a message.
CANA can bus client, used for communicating with can devices
Functions
CANCreates an CAN interface connected to specific pins.
frequencySet the frequency of the CAN interface
writeWrite a CANMessage to the bus.
readRead a CANMessage from the bus.
resetReset CAN interface.
monitorPuts or removes the CAN interface into silent monitoring mode
rderrorReturns number of read errors to detect read overflow errors.
tderrorReturns number of write errors to detect write overflow errors.
attachAttach a function to call whenever a CAN frame received interrupt is generated.

Functions

CANMessage

CANMessage()

Creates empty CAN message.

CANMessage

CANMessage(int _id,  
const char *_data,  
char _len =  8,
CANType _type =  CANData,
CANFormat _format =  CANStandard)

Creates CAN message with specific content.

CANMessage

CANMessage(int _id,  
CANFormat _format =  CANStandard)

Creates CAN remote message.

Variables

id

unsigned int id

The message id.

If format is CANStandard it must be an 11 bit long id If format is CANExtended it must be an 29 bit long id

data

unsigned char data[8]

Space for 8 byte payload.

If type is CANData data can store up to 8 byte data.

len

unsigned char len

Length of data in bytes.

If type is CANData data can store up to 8 byte data.

format

CANFormat format

Defines if the message has standard or extended format.

Defines the type of message id

Default is CANStandard which implies 11 bit id.  CANExtended means 29 bit message id.

type

CANType type

Defines the type of a message.

The message type can rather be CANData for a message with data (default).  Or CANRemote for a request of a specific CAN message.

CAN

class CAN : public Base

A can bus client, used for communicating with can devices

Functions
CANCreates an CAN interface connected to specific pins.
frequencySet the frequency of the CAN interface
writeWrite a CANMessage to the bus.
readRead a CANMessage from the bus.
resetReset CAN interface.
monitorPuts or removes the CAN interface into silent monitoring mode
rderrorReturns number of read errors to detect read overflow errors.
tderrorReturns number of write errors to detect write overflow errors.
attachAttach a function to call whenever a CAN frame received interrupt is generated.

Functions

CAN

CAN(PinName rd,
PinName td)

Creates an CAN interface connected to specific pins.

Example

#include "mbed.h"

Ticker ticker;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
CAN can1(p9, p10);
CAN can2(p30, p29);

char counter = 0;

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

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

Variables

rdread from transmitter
tdtransmit to transmitter

frequency

int frequency(int hz)

Set the frequency of the CAN interface

Variables

hzThe bus frequency in hertz
returns1 if successful, 0 otherwise

write

int write(CANMessage msg)

Write a CANMessage to the bus.

Variables

msgThe CANMessage to write.

Returns

0If write failed.
1If write was successful.

read

int read(CANMessage &msg)

Read a CANMessage from the bus.

Variables

msgA CANMessage to read to.

Returns

0If no message arrived.
1If message arrived.

reset

void reset()

Reset CAN interface.

To use after error overflow.

monitor

void monitor(bool silent)

Puts or removes the CAN interface into silent monitoring mode

Variables

silentboolean indicating whether to go into silent mode or not

rderror

unsigned char rderror()

Returns number of read errors to detect read overflow errors.

tderror

unsigned char tderror()

Returns number of write errors to detect write overflow errors.

attach

void attach(void (*fptr)(void))

Attach a function to call whenever a CAN frame received interrupt is generated.

Variables

fptrA pointer to a void function, or 0 to set as none
class CANMessage : public CAN_Message
CANMessage()
Creates empty CAN message.
unsigned int id
The message id.
unsigned char data[8]
Space for 8 byte payload.
unsigned char len
Length of data in bytes.
CANFormat format
Defines if the message has standard or extended format.
CANType type
Defines the type of a message.
class CAN : public Base
A can bus client, used for communicating with can devices
CAN(PinName rd,
PinName td)
Creates an CAN interface connected to specific pins.
int frequency(int hz)
Set the frequency of the CAN interface
int write(CANMessage msg)
Write a CANMessage to the bus.
int read(CANMessage &msg)
Read a CANMessage from the bus.
void reset()
Reset CAN interface.
void monitor(bool silent)
Puts or removes the CAN interface into silent monitoring mode
unsigned char rderror()
Returns number of read errors to detect read overflow errors.
unsigned char tderror()
Returns number of write errors to detect write overflow errors.
void attach(void (*fptr)(void))
Attach a function to call whenever a CAN frame received interrupt is generated.