David Smart / CANPort2
Embed: (wiki syntax)

« Back to documentation index

CANPort Class Reference

CANPort Class Reference

This is the CANPort, which is the physical interface to CAN. More...

#include <CANPort.h>

Public Member Functions

 CANPort (CANCHANNEL_T chNum, PinName rd, PinName td, PinName activityPin=NC, PinName slopePin=NC, CANSlopeControl_T slope=NORMALSPEED)
 The advanced form of the constructure to create a CANPort, name an activity indicator, and name a slope control pin.
virtual ~CANPort ()
 Destroys the CANPort.
bool TransmitMsg (CANmsg msg)
 Transmit a message to the bus.
bool ReceiveMsg (CANmsg &msg)
 Read a message from the bus into a buffer.
void Flash (CANDIR_T tx)
 This flashes the activity indicator associated with this CANPort.
void Extinguish (void)
 This extinguishes the activity indicator associated with this CANPort.
void Attach (void(*fptr)(void))
 Attach a callback whenever a CAN receive interrupt is generated.
bool SetAutoReset (bool enable)
 Attach a callback whenever a CAN receive interrupt is generated.
bool GetAutoReset ()
 This returns the current state of the AutoReset feature.
bool SetBusMode (CANBusMode_T mode)
 This provides control to set the bus mode as active or listener only.
CANBusMode_T GetBusMode ()
 This returns the current state of the bus mode.
bool SetSlopeControl (CANSlopeControl_T slope)
 This provides control to set the transceiver slope control mode.
CANSlopeControl_T GetSlopeControl ()
 This returns the current state of the slope control.
bool SetBitRate (uint32_t rate)
 This sets the bitrate for the CAN channel.
uint32_t GetBitRate ()
 This returns the current desired bitrate for the CAN channel.
int GetTxCounter ()
 This returns the number of messages that were sent by this CAN channel.
int GetRxCounter ()
 This returns the number of messages that were received by this CAN channel.
int GetTxErrorCounter ()
 This returns the number of transmit errors.
int GetRxErrorCounter ()
 This returns the number of receive errors.
bool ResetChip ()
 This resets the CAN interface.
void PrintInfo (Serial *stream)
 This writes various CAN info to the selected serial channel.

Detailed Description

This is the CANPort, which is the physical interface to CAN.

This derived class has a number of additional capabilities:

  • activity indicator to show receive and transmit activity per port
  • slope control pin to permit controlling performance and power modes
  • counters, to keep track of received and transmitted messages
  • and more...

Definition at line 39 of file CANPort.h.


Constructor & Destructor Documentation

CANPort ( CANCHANNEL_T  chNum,
PinName  rd,
PinName  td,
PinName  activityPin = NC,
PinName  slopePin = NC,
CANSlopeControl_T  slope = NORMALSPEED 
)

The advanced form of the constructure to create a CANPort, name an activity indicator, and name a slope control pin.

This version lets you specify the receive and transmit pins for a CANPort, indicate a pin which is used to indicate CAN activity, both receive and transmit, and identify a slope control pin.

Parameters:
chNumis the user assigned channel number given to this port
rdis the receive pin
tdis the transmit pin
activityPinis the PWM pin used to indicate CAN traffic
slopePinis the DigitalInOut pin used to control slope
slopeis the initial slope setting

Definition at line 31 of file CANPort.cpp.

~CANPort (  ) [virtual]

Destroys the CANPort.

Definition at line 52 of file CANPort.cpp.


Member Function Documentation

void Attach ( void(*)(void)  fptr )

Attach a callback whenever a CAN receive interrupt is generated.

This attaches a simple callback to the CAN ISR

Parameters:
fptrpointer to the function to be called
void Extinguish ( void   )

This extinguishes the activity indicator associated with this CANPort.

If the activity indicator is configured.

Definition at line 101 of file CANPort.cpp.

void Flash ( CANDIR_T  tx )

This flashes the activity indicator associated with this CANPort.

If the activity indicator is configured.

Parameters:
txindiciates if the flash is to be a sign of xmt or rcv it will flash dimly for a transmit and bright for a receive

Definition at line 108 of file CANPort.cpp.

bool GetAutoReset (  )

This returns the current state of the AutoReset feature.

Returns a value indicating the current state

Returns:
true if AutoReset is enabled
false if AutoReset is disabled

Definition at line 144 of file CANPort.h.

uint32_t GetBitRate (  )

This returns the current desired bitrate for the CAN channel.

This returns the previously set bitrate. Note that the actual bitrate may be different, due to the internal calculations of the mbed library.

Returns:
the bitrate in bits per second

Definition at line 188 of file CANPort.cpp.

CANPort::CANBusMode_T GetBusMode (  )

This returns the current state of the bus mode.

Returns:
MONITOR if the chip is in the monitor (listen / promiscuous) mode
ACTIVE if the chip is in the active mode

Definition at line 143 of file CANPort.cpp.

int GetRxCounter (  )

This returns the number of messages that were received by this CAN channel.

The counter is never reset.

Returns:
the number of messages received by this CAN channel

Definition at line 198 of file CANPort.cpp.

int GetRxErrorCounter (  )

This returns the number of receive errors.

As counted by the hardware

Returns:
the number of receive errors

Definition at line 208 of file CANPort.cpp.

CANPort::CANSlopeControl_T GetSlopeControl (  )

This returns the current state of the slope control.

The state is controlled by the SetSlopeControl command.

Returns:
the current state; one of HIGHSPEED, NORMALSPEED, or STANDBY

Definition at line 173 of file CANPort.cpp.

int GetTxCounter (  )

This returns the number of messages that were sent by this CAN channel.

The counter is never reset.

Returns:
the number of messages sent by this CAN channel

Definition at line 193 of file CANPort.cpp.

int GetTxErrorCounter (  )

This returns the number of transmit errors.

As counted by the hardware

Returns:
the number of transmit errors

Definition at line 203 of file CANPort.cpp.

void PrintInfo ( Serial *  stream )

This writes various CAN info to the selected serial channel.

When supplied with a Serial port channel, this will output some possibly interesting information about the CAN configuration. It does not output details for only the active objects channel, rather it outputs information about the CAN subsystem.

Parameters:
streamis the handle of a serial channel
Returns:
nothing

Definition at line 219 of file CANPort.cpp.

bool ReceiveMsg ( CANmsg &  msg )

Read a message from the bus into a buffer.

This method will extract a message from the bus and put it in the message space provided

Parameters:
msgis a reference to a CANmsg into which the msg is placed
Returns:
true if a message was received and processed

This looks like a very inefficient method, but it works.

Definition at line 80 of file CANPort.cpp.

bool ResetChip (  )

This resets the CAN interface.

Unconditionally.

Returns:
true if success

Definition at line 213 of file CANPort.cpp.

bool SetAutoReset ( bool  enable )

Attach a callback whenever a CAN receive interrupt is generated.

This attaches a callback to the CAN ISR

Parameters:
tptrpointer to the object to call the member function on
mptrpointer to the member function to be called This provides control of the AutoReset feature

AutoReset is a feature that permits automatically resetting the CAN chip when it is trapped in an error state (e.g. bus off). The default is disabled.

Parameters:
enableis used to enable or disable the auto reset feature
Returns:
true if the command was accepted

Definition at line 120 of file CANPort.cpp.

bool SetBitRate ( uint32_t  rate )

This sets the bitrate for the CAN channel.

This sets the bitrate for the CAN channel. The rate is in bits per second. The actual bitrate and the sample point is automagically determined from the internal algorithms provided by the mbed library.

This API appears redundant to the frequency api, however this one will retain the rate setting and then permits the query of the bitrate.

Parameters:
rateis the desired bitrate in bits per second.
Returns:
true if the command succeeded

Definition at line 178 of file CANPort.cpp.

bool SetBusMode ( CANBusMode_T  mode )

This provides control to set the bus mode as active or listener only.

The CAN adapter can be placed into a monitor(LISTENER) mode (sometimes called promiscuous mode) or into an active mode, in which case it will send hardware acknowledge.

Parameters:
modeis either MONITOR or ACTIVE
Returns:
true if the command was accepted

Definition at line 126 of file CANPort.cpp.

bool SetSlopeControl ( CANSlopeControl_T  slope )

This provides control to set the transceiver slope control mode.

Many CAN transceivers can be operated in one of several modes -

  • HIGHSPEED - which supports the highest frequency of communication, but does tend to generate more EMI unless the cable is properly shielded
  • NORMALSPEED - which wave-shapes the rising and falling edge, and significantly reduces the EMI concerns, but may trade off the highest performance.
  • STANDBY - which puts the chip into the lowest power state, and prevents transmission of a message. It can typically still receive messages, but this mode may also be useful for other purposes (wake on CAN, autobaud w/o disrupting bus traffic, etc.)
Parameters:
slopesets the slope control to one of the states HIGHSPEED, NORMALSPEED, or STANDBY
Returns:
true if the command succeeded
false if the command failed, which may be due to not having defined a slope control pin.

Definition at line 148 of file CANPort.cpp.

bool TransmitMsg ( CANmsg  msg )

Transmit a message to the bus.

This method transmits a message to the bus.

Parameters:
msgis a CANmsg
Returns:
true if the message was successfully written to CAN

Definition at line 65 of file CANPort.cpp.