Working fork to test F0 application

Dependents:   ppCANOpen_Example

Fork of CANnucleo by Zoltan Hudak

Embed: (wiki syntax)

« Back to documentation index

CAN Class Reference

A can bus client, used for communicating with can devices. More...

#include <CAN.h>

Public Member Functions

 CAN (PinName rd, PinName td, FunctionalState abom=ENABLE)
 Creates an CAN interface connected to specific pins.
virtual ~CAN ()
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, int handle=0)
 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.
int mode (Mode mode)
 Change CAN operation to the specified mode.
int filter (unsigned int id, unsigned int mask, CANFormat format=CANAny, int handle=0)
 Filter out incomming messages.
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), IrqType type=RxIrq)
 Attach a function to call whenever a CAN frame received interrupt is generated.
template<typename T >
void attach (T *tptr, void(T::*mptr)(void), IrqType type=RxIrq)
 Attach a member function to call whenever a CAN frame received interrupt is generated.

Static Public Member Functions

static void _irq_handler (uint32_t id, CanIrqType type)

Detailed Description

A can bus client, used for communicating with can devices.

Definition at line 131 of file CAN.h.


Constructor & Destructor Documentation

CAN ( PinName  rxPin,
PinName  txPin,
FunctionalState  abom = ENABLE 
)

Creates an CAN interface connected to specific pins.

Constructor.

Parameters:
rdread from transmitter
tdtransmit to transmitter

Example:

 #include "mbed.h"
 #include "CAN.h"

 Ticker ticker;
 DigitalOut led1(LED1);
 CAN can(PA_11, PA_12);

 char counter = 0;

 void send() {
     if(can.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(can.read(msg)) {
             printf("Message received: %d\n\n", msg.data[0]);
             led1 = !led1;
         }
         wait(0.2);
     }
 }

Constructor

Parameters:
rdCAN receiver pin name
tdCAN transmitter pin name
abomAutomatic recovery from bus-off state (default value set to enabled)
Note:
Constructs an instance of CAN class
Parameters:
rxPin,:CAN Rx pin name
txPin,:CAN Tx pin name
abom,:Automatic recovery from bus-off state (defaults to enabled)
Return values:

Definition at line 33 of file CAN.cpp.

~CAN ( void   ) [virtual]
Note:
Parameters:
@retval

Definition at line 46 of file CAN.cpp.


Member Function Documentation

void _irq_handler ( uint32_t  id,
CanIrqType  type 
) [static]
Note:
Parameters:
@retval

Definition at line 250 of file CAN.cpp.

void attach ( T *  tptr,
void(T::*)(void)  mptr,
IrqType  type = RxIrq 
)

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

Parameters:
tptrpointer to the object to call the member function on
mptrpointer to the member function to be called
eventWhich CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)

Definition at line 296 of file CAN.h.

void attach ( void(*)(void)  fptr,
IrqType  type = RxIrq 
)

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

Attaches handler funcion to CAN1 RX0 Interrupt.

Parameters:
fptrA pointer to a void function, or 0 to set as none
eventWhich CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error)
Note:
Only CAN1 RX0 Interrupt supported
Parameters:
fptr,:pointer to a void (*)(void) function
type,:not used (only CAN1 RX0 Interrupt supported)
Return values:

Definition at line 235 of file CAN.cpp.

int filter ( unsigned int  id,
unsigned int  mask,
CANFormat  format = CANAny,
int  handle = 0 
)

Filter out incomming messages.

Sets up a CAN filter.

Parameters:
idthe id to filter on
maskthe mask applied to the id
formatformat to filter on (Default CANAny)
handlemessage filter handle (Optional)
Returns:
0 - successful 1 - error 2 - busy 3 - time out
Note:
At the present, CANnucleo supports only mask mode and 32-bit filter scale. Identifier list mode filtering and 16-bit filter scale are not supported. There are 14 filters available (0 - 13) for the application to set up. Each filter is a 32-bit filter defined by a filter ID and a filter mask. If no filter is set up then no CAN message is accepted (received)! That's why filter #0 is set up in the constructor to receive all CAN messages by default. On reception of a message it is compared with filter #0. If there is a match, the message is stored. If there is no match, the incoming identifier is then compared with the next filter. If the received identifier does not match any of the identifiers configured in the filters, the message is discarded by hardware without disturbing the software.
Parameters:
id,:'Filter ID' defines the bit values to be compared with the corresponding received bits

Mapping of 32-bits (4-bytes) : | STID[10:3] | STID[2:0] EXID[17:13] | EXID[12:5] | EXID[4:0] IDE RTR 0 |

STID - Stardard Identifier bits EXID - Extended Identifier bits [x:y]- bit range IDE - Identifier Extension bit (0 -> Standard Identifier, 1 -> Extended Identifier) RTR - Remote Transmission Request bit (0 -> Remote Transmission Request, 1 -> Standard message)

Parameters:
mask,:'Filter mask' defines which bits of the 'Filter ID' are compared with the received bits and which bits are disregarded.

Mapping of 32-bits (4-bytes) : | STID[10:3] | STID[2:0] EXID[17:13] | EXID[12:5] | EXID[4:0] IDE RTR 0 |

STID - Stardard Identifier bits EXID - Extended Identifier bits [x:y]- bit range IDE - Identifier Extension bit RTR - Remote Transmission Request bit

1 -> bit is considered 0 -> bit is disregarded

---------------------------------------- Example of filter set up and filtering: ----------------------------------------

Let's assume we would like to receive only messages with standard identifier STID = 0x0207 (STID[15:0] = 00000010 00000111)

We map the STID to filter ID by shifting the bits appropriately: Filter id = STID << (16 + (15 - 10)) = STID << 21 = 01000000 11100000 00000000 00000000 = 0x40E00000

To compare only the bits representing STID we set the filter mask adequately: Filter mask = 11111111 11100000 00000000 00000100 = 0xFFE00004 |||||||| ||| | -------- --- | | | | STID[10:3] STID[2:0] IDE

Keep in mind that filter #0 was already set up in the constructor to receive all CAN messages by default. So we have to reconfigure it. If we were set up filter #1 here then filter #0 would receive all the messages and no message would reach filter #1!

To set up filter #0 we call: can.filter(0x0207 << 21, 0xFFE00004, CANAny, 0);

Only these bits (set to 1) of filter id are compared with the corresponding bits of received message (the others are disregarded) | --------------------------------- |||||||| ||| | Filter mask = 11111111 11100000 00000000 00000100 (= 0xFFE00004) Filter id = 01000000 11100000 00000000 00000000 (= 0x40E00000) |||||||| ||| | --------------------------------- | To receive the message the values of these bits must match. Otherwise the message is passed to the next filter or discarded if this was the last active filter. | --------------------------------- |||||||| ||| | Received id = 01000000 11100000 00000000 00000010 (= 0x40E00002) ||||| |||||||| ||||| || ----------------------- | These bits (set to 0 in filter mask) are disregarded (masked). They can have arbitrary values.

NOTE: For the meaning of individual bits see the mapping of 32-bits explained above.

Parameters:
format,:This parameter must be CANAny
handle,:Selects the filter. This parameter must be a number between 0 and 13.
retval,:0 - successful 1 - error 2 - busy 3 - time out

Definition at line 224 of file CAN.cpp.

int frequency ( int  f )

Set the frequency of the CAN interface.

Parameters:
hzThe bus frequency in hertz
Returns:
1 if successful, 0 otherwise
Note:
Parameters:
@retval

Definition at line 57 of file CAN.cpp.

int mode ( Mode  mode )

Change CAN operation to the specified mode.

Parameters:
modeThe new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest)
Returns:
0 if mode change failed or unsupported, 1 if mode change was successful
Note:
Parameters:
@retval

Definition at line 127 of file CAN.cpp.

void monitor ( bool  silent )

Puts or removes the CAN interface into silent monitoring mode.

Parameters:
silentboolean indicating whether to go into silent mode or not
Note:
Parameters:
@retval

Definition at line 117 of file CAN.cpp.

unsigned char rderror ( void   )

Returns number of read errors to detect read overflow errors.

Note:
Parameters:
@retval

Definition at line 97 of file CAN.cpp.

int read ( CANMessage msg,
int  handle = 0 
)

Read a CANMessage from the bus.

Parameters:
msgA CANMessage to read to.
handlemessage filter handle (0 for any message)
Returns:
0 if no message arrived, 1 if message arrived
Note:
Parameters:
@retval

Definition at line 77 of file CAN.cpp.

void reset ( void   )

Reset CAN interface.

To use after error overflow.

Note:
Parameters:
@retval

Definition at line 87 of file CAN.cpp.

unsigned char tderror ( void   )

Returns number of write errors to detect write overflow errors.

Note:
Parameters:
@retval

Definition at line 107 of file CAN.cpp.

int write ( CANMessage  msg )

Write a CANMessage to the bus.

Parameters:
msgThe CANMessage to write.
Returns:
0 if write failed, 1 if write was successful
Note:
Parameters:
@retval

Definition at line 67 of file CAN.cpp.