com.dalsemi.comm
Class CanBus

java.lang.Object
  |
  +--com.dalsemi.comm.CanBus

public class CanBus
extends Object

Class allowing access to DS80C390 CAN Bus Controllers. Each controller can access 15 message centers numbered 1 - 15. Each message center can be programmed to either transmit or receive, except message center 15. Message center 15 can only receive (not transmit) and can also buffer two back-to-back frames.
With an 18.432MHz system clock, CAN bit rates of 20, 50, and 125Kbps can be achieved.

         Prescaler  tqu      tseg1   tseg2
 20Kbps  71         3.852us  5       7
 50Kbps  41         2.224us  3       5
 125Kbps 7          3.798us  13      7
 

See Also:
CanFrame

Field Summary
static byte CANBUS0
          Passed to constructor to create object to access CAN Bus 0
static byte CANBUS1
          Passed to constructor to create object to access CAN Bus 1
 
Constructor Summary
CanBus(byte portnum)
          Creates a new CanBus instance on the specified port.
 
Method Summary
 void autoAnswerRemoteFrameRequest(int messageCenter, int ID, byte[] data)
          Sets up a message center to auto reply to remote frame requests.
 void close()
          Closes this CAN Bus Controller.
 void disableController()
          Shuts down this CAN controller.
 void disableMessageCenter(int messageCenter)
          Shuts down this message center for changes.
 void enableController()
          Starts up this CAN controller, and connects to the bus.
 void enableControllerPassive()
          Starts up this CAN controller, but doesn't connect CAN transmit to the bus.
 void enableMessageCenter(int messageCenter)
          Starts up this message center.
 int getRXErrorCount()
          Gets the receiver error count.
 int getTXErrorCount()
          Gets the transmitter error count.
 void open()
          Opens this CAN Bus Controller.
 void receive(CanFrame frame)
          Receives a CAN data frame.
 int receiveFramesAvailable()
          Gets number of frames in the receive queue.
 boolean receivePoll(CanFrame frame)
          Receives a CAN data frame.
 void resetController()
          Resets this CAN controller.
 void sendDataFrame(int ID, boolean extendedID, byte[] data)
          Sends a CAN Data Frame from first available message center without using an explicit CanFrame.
 void sendFrame(CanFrame frame)
          Sends a CAN frame from first available message center.
 void sendRemoteFrameRequest(int ID, boolean extendedID, byte[] data)
          Sends a CAN Remote Frame Request from first available message center without using an explicit CanFrame.
 void set11BitGlobalIDMask(int mask)
          Sets the 11 Bit (STANDARD) Global ID Mask.
 void set11BitMessageCenterArbitrationID(int messageCenter, int ID)
          Sets an 11 Bit Arbitration ID for a message center.
 void set29BitGlobalIDMask(int mask)
          Sets the 29 Bit (EXTENDED) Global ID Mask.
 void set29BitMessageCenter15IDMask(int mask)
          Sets the 29 Bit Message Center 15 ID Mask.
 void set29BitMessageCenterArbitrationID(int messageCenter, int ID)
          Sets a 29 Bit Arbitration ID for a message center.
 void setBaudRatePrescaler(int prescaler)
          Sets the baud rate prescaler from the CPU crystal.
 void setMessageCenterMessageIDMaskEnable(int messageCenter, boolean maskEnable)
          Enables or disables Message ID Masking for a specific message center.
 void setMessageCenterRXMode(int messageCenter)
          Sets a specific message center as receiver.
 void setMessageCenterTXMode(int messageCenter)
          Sets a specific message center as transmitter.
 void setMessageCenterWriteOverEnable(int messageCenter, boolean writeover)
          Sets the state of write over in a specific message center.
 void setReceiveQueueLimit(int numframes)
          Sets receive queue frame limit.
 void setSampleRate(int sampleRate)
          Sets SMP (Sample Rate) which is whether to use one or three samples per bit time during CAN communication.
 void setSiestaMode()
          Puts the CAN Controller in SIESTA (low power) mode.
 void setSynchronizationJumpWidth(int jumpWidth)
          Sets the SJW (Synchronization Jump Width) necessary for adjusting TSEG1 and TSEG2 to compensate for sync problems during CAN communication.
 void setTransmitQueueLimit(int numframes)
          Sets transmit queue frame limit.
 void setTSEG1(int tseg1)
          Sets TSEG1 (Timing Segment 1 = PROP_SEG + PHASE_SEG1) to a specified number of time quanta.
 void setTSEG2(int tseg2)
          Sets TSEG2 (Timing Segment 2 = PHASE_SEG2) to a specified number of time quanta.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CANBUS0

public static final byte CANBUS0
Passed to constructor to create object to access CAN Bus 0

CANBUS1

public static final byte CANBUS1
Passed to constructor to create object to access CAN Bus 1
Constructor Detail

CanBus

public CanBus(byte portnum)
       throws CanBusException
Creates a new CanBus instance on the specified port. This opens and resets the specified CAN Controller and readies it for operation. The controller will be in a disabled state. Calling enableController will place the controller on the bus. All message centers start up disabled.
Parameters:
portnum - either CANBUS0 or CANBUS1
Throws:
CanBusException - if port is already open, or the controller will not reset
Method Detail

open

public void open()
          throws CanBusException
Opens this CAN Bus Controller. Assigns ownership of the controller to the calling process. The constructor calls this automatically when a new CanBus object is created.
Throws:
CanBusException - if controller is already open

close

public void close()
           throws CanBusException
Closes this CAN Bus Controller. Reliquishes ownership of the controller and releases all resources for this CAN controller.
Throws:
CanBusException - if controller is not open

setBaudRatePrescaler

public void setBaudRatePrescaler(int prescaler)
                          throws CanBusException
Sets the baud rate prescaler from the CPU crystal. This method sets the basic time quantum (tqu) necessary for CAN communication. The divisor divides straight off the external crystal on the processor.
For instance, at 18.432MHz, a divisor of 7 will give you a tqu of 379.774ns.
tqu = 7 * 1/18.432MHz
Parameters:
prescaler - 1 to 256
Throws:
CanBusException - if controller is not open
IllegalArgumentException - if prescaler is out of range

setSynchronizationJumpWidth

public void setSynchronizationJumpWidth(int jumpWidth)
                                 throws CanBusException
Sets the SJW (Synchronization Jump Width) necessary for adjusting TSEG1 and TSEG2 to compensate for sync problems during CAN communication.
TSEG1 may be lengthened during resync.
TSEG2 may be shortened during resync.
Parameters:
jumpWidth - 1 to 4 (1tqu to 4tqu)
Throws:
CanBusException - if controller is not open
IllegalArgumentException - if SJW is out of range

setSampleRate

public void setSampleRate(int sampleRate)
                   throws CanBusException
Sets SMP (Sample Rate) which is whether to use one or three samples per bit time during CAN communication.
Parameters:
sampleRate - 1 or 3
Throws:
CanBusException - if controller is not open
IllegalArgumentException - if SMP is out of range

setTSEG1

public void setTSEG1(int tseg1)
              throws CanBusException
Sets TSEG1 (Timing Segment 1 = PROP_SEG + PHASE_SEG1) to a specified number of time quanta. This is the timing segment before the bit sample.
Parameters:
tseg1 - 2 to 16 (2tqu to 16tqu)
Throws:
CanBusException - if controller is not open
IllegalArgumentException - if TSEG1 is out of range

setTSEG2

public void setTSEG2(int tseg2)
              throws CanBusException
Sets TSEG2 (Timing Segment 2 = PHASE_SEG2) to a specified number of time quanta. This is the timing segment after the bit sample.
Parameters:
tseg2 - 2 to 8 (2tqu to 8tqu)
Throws:
CanBusException - if controller is not open
IllegalArgumentException - if TSEG2 is out of range

resetController

public void resetController()
                     throws CanBusException
Resets this CAN controller. No more transmissions, or receptions will be processed. The CAN controller is disconnected from the bus. All message centers reset to power on defaults.
ALL SETTINGS WILL BE LOST!
Throws:
CanBusException - if controller is not open

disableController

public void disableController()
                       throws CanBusException
Shuts down this CAN controller. No more transmissions, or receptions will be processed. The CAN controller is disconnected from the bus. User is free to change timing and other communication critical parameters. Message centers remain intact.
Throws:
CanBusException - if controller is not open

enableController

public void enableController()
                      throws CanBusException
Starts up this CAN controller, and connects to the bus. All critical timing parameters must already be set. Message centers remain intact.
Throws:
CanBusException - if controller is not open

enableControllerPassive

public void enableControllerPassive()
                             throws CanBusException
Starts up this CAN controller, but doesn't connect CAN transmit to the bus. Become a quiet listener on the bus. All critical timing parameters must already be set. Message centers remain intact.
Throws:
CanBusException - if controller is not open

enableMessageCenter

public void enableMessageCenter(int messageCenter)
                         throws CanBusException
Starts up this message center. All mode changes must be complete. Message center becomes operational and able to send or receive messages depending on mode.
Parameters:
messageCenter - message center to enable
Throws:
CanBusException - if controller is not open

disableMessageCenter

public void disableMessageCenter(int messageCenter)
                          throws CanBusException
Shuts down this message center for changes. No more transmissions, or receptions will be processed at this message center.
Parameters:
messageCenter - message center to enable
Throws:
CanBusException - if controller is not open

setReceiveQueueLimit

public void setReceiveQueueLimit(int numframes)
                          throws CanBusException
Sets receive queue frame limit. If the limit is set to zero, reads will block until a frame is received, or bus error. Maximum limit is 255 frames.
Parameters:
numframes - maximum number of frames to store in queue
Throws:
CanBusException - if controller is not open, or if queue could not be allocated
IllegalArgumentException - if number of frames is out of range

receiveFramesAvailable

public int receiveFramesAvailable()
                           throws CanBusException
Gets number of frames in the receive queue. If the receive queue limit is set to zero, the return value will be zero.
Returns:
number of frames pending in receive queue
Throws:
CanBusException - if controller is not open

receive

public void receive(CanFrame frame)
             throws CanBusException
Receives a CAN data frame. This method blocks until a frame is received.
Parameters:
frame - a frame in which to place the incoming data
Throws:
CanBusException - if controller is not open, or if bus error occurs

receivePoll

public boolean receivePoll(CanFrame frame)
                    throws CanBusException
Receives a CAN data frame. This method returns immediately and does not block.
Parameters:
frame - frame in which to place the incoming data
Returns:
true if frame has been received
Throws:
CanBusException - if controller is not open, or if bus error occurs

setTransmitQueueLimit

public void setTransmitQueueLimit(int numframes)
                           throws CanBusException
Sets transmit queue frame limit. If the limit is set to zero, writes will block until frame is transmitted, or bus error. Maximum limit is 255 frames.
Parameters:
numframes - maximum number of frames to store in queue
Throws:
CanBusException - if controller is not open
IllegalArgumentException - if number of frames is out of range

sendFrame

public void sendFrame(CanFrame frame)
               throws CanBusException
Sends a CAN frame from first available message center. If transmit queue size is one, this method will block until the message has been successfully transmitted, or an error has occurred. If transmit queue size is greater than one, messages will be queued for transmit and method will return immediately.
Parameters:
frame - frame to send
Throws:
CanBusException - if controller is not open, or if bus error occurs

sendDataFrame

public void sendDataFrame(int ID,
                          boolean extendedID,
                          byte[] data)
                   throws CanBusException
Sends a CAN Data Frame from first available message center without using an explicit CanFrame. If transmit queue size is one, this method will block until the message has been successfully transmitted, or an error has occurred. If transmit queue size is greater than one, messages will be queued for transmit and method will return immediately.
Parameters:
ID - 11 or 29 bit ID for frame
extendedID - true for extended ID (29 bit), or false for standard ID (11 bit)
data - to send in frame (max 8 bytes)
Throws:
CanBusException - if controller is not open, or if bus error occurs

sendRemoteFrameRequest

public void sendRemoteFrameRequest(int ID,
                                   boolean extendedID,
                                   byte[] data)
                            throws CanBusException
Sends a CAN Remote Frame Request from first available message center without using an explicit CanFrame. If transmit queue size is one, this method will block until the message has been successfully transmitted, or an error has occurred. If transmit queue size is greater than one, messages will be queued for transmit and method will return immediately.
Parameters:
ID - 11 or 29 bit ID for frame
extendedID - true for extended ID (29 bit), or false for standard ID (11 bit)
data - to send in frame (max 8 bytes)
Throws:
CanBusException - if controller is not open, or if bus error occurs

autoAnswerRemoteFrameRequest

public void autoAnswerRemoteFrameRequest(int messageCenter,
                                         int ID,
                                         byte[] data)
                                  throws CanBusException
Sets up a message center to auto reply to remote frame requests.
Note: This resets the state of a message center for this purpose only. Also, the message center becomes enabled during this method.
Parameters:
messageCenter - message center to use for this reply
ID - 11 or 29 bit ID for frame
data - to send in frame (max 8 bytes)
Throws:
CanBusException - if controller is not open, or if bus error occurs

set11BitGlobalIDMask

public void set11BitGlobalIDMask(int mask)
                          throws CanBusException
Sets the 11 Bit (STANDARD) Global ID Mask. The Global ID Mask is used to denote which bits to match in the incoming frame ID.
Parameters:
mask - 11 bit mask
Throws:
CanBusException - if controller is not open, or if bus error occurs

set29BitGlobalIDMask

public void set29BitGlobalIDMask(int mask)
                          throws CanBusException
Sets the 29 Bit (EXTENDED) Global ID Mask. The Global ID Mask is used to denote which bits to match in the incoming frame ID.
Parameters:
mask - 29 bit mask
Throws:
CanBusException - if controller is not open, or if bus error occurs

set29BitMessageCenter15IDMask

public void set29BitMessageCenter15IDMask(int mask)
                                   throws CanBusException
Sets the 29 Bit Message Center 15 ID Mask. Message Center 15 has it's own ID Mask. The Message Center 15 ID Mask is used to denote which bits to match in the Message Center 15 ID.
Parameters:
mask - 29 bit mask
Throws:
CanBusException - if controller is not open, or if bus error occurs

set11BitMessageCenterArbitrationID

public void set11BitMessageCenterArbitrationID(int messageCenter,
                                               int ID)
                                        throws CanBusException
Sets an 11 Bit Arbitration ID for a message center. When this value matches an incoming frame ID subject to the Global ID Mask or Message Center 15 Mask, the incoming frame will be received.
Note: This will change the specified message center to only respond to 11 bit messages.
Parameters:
messageCenter - message center to change
ID - 11 bit ID
Throws:
CanBusException - if controller is not open, or if bus error occurs

set29BitMessageCenterArbitrationID

public void set29BitMessageCenterArbitrationID(int messageCenter,
                                               int ID)
                                        throws CanBusException
Sets a 29 Bit Arbitration ID for a message center. When this value matches an incoming frame ID subject to the Global ID Mask or Message Center 15 Mask, the incoming frame will be received.
Note: This will change the specified message center to only respond to 29 bit messages.
Parameters:
messageCenter - message center to change
ID - 29 bit ID
Throws:
CanBusException - if controller is not open, or if bus error occurs

setMessageCenterWriteOverEnable

public void setMessageCenterWriteOverEnable(int messageCenter,
                                            boolean writeover)
                                     throws CanBusException
Sets the state of write over in a specific message center. This allows incoming frames to overwrite received frames that haven't been moved out of the message center.
Parameters:
messageCenter - message center to change
writeover - set to true to allow overwrite.
Throws:
CanBusException - if controller is not open, or if bus error occurs

setMessageCenterTXMode

public void setMessageCenterTXMode(int messageCenter)
                            throws CanBusException
Sets a specific message center as transmitter.
Note: Message Center 15 is always RECEIVE only.
Parameters:
messageCenter - message center to change
Throws:
CanBusException - if controller is not open, or if bus error occurs
IllegalArgumentException - if message center is out of range

setMessageCenterRXMode

public void setMessageCenterRXMode(int messageCenter)
                            throws CanBusException
Sets a specific message center as receiver.
Note: Message Center 15 is always RECEIVE only.
Parameters:
messageCenter - message center to change
Throws:
CanBusException - if controller is not open, or if bus error occurs

setMessageCenterMessageIDMaskEnable

public void setMessageCenterMessageIDMaskEnable(int messageCenter,
                                                boolean maskEnable)
                                         throws CanBusException
Enables or disables Message ID Masking for a specific message center.
Note: If masking is disabled, this message center ID must match ALL bits of incoming ID.
Parameters:
messageCenter - message center to change
maskEnable - true to allow Global Mask, false to require exact match of ID
Throws:
CanBusException - if controller is not open, or if bus error occurs

getTXErrorCount

public int getTXErrorCount()
                    throws CanBusException
Gets the transmitter error count.
Returns:
transmitter error count
Throws:
CanBusException - if controller is not open, or if bus error occurs

getRXErrorCount

public int getRXErrorCount()
                    throws CanBusException
Gets the receiver error count.
Returns:
receiver error count
Throws:
CanBusException - if controller is not open, or if bus error occurs

setSiestaMode

public void setSiestaMode()
                   throws CanBusException
Puts the CAN Controller in SIESTA (low power) mode. When Bus activity is detected, the controller will wake up and participate on the bus.
Throws:
CanBusException - if controller is not open, or if bus error occurs