PikeAero::CCommunicationPacket Class Reference

#include <ccommunicationpacket.h>

Inheritance diagram for PikeAero::CCommunicationPacket:

Inheritance graph
[legend]
Collaboration diagram for PikeAero::CCommunicationPacket:

Collaboration graph
[legend]

Public Types

enum  tRXState
enum  tTXState { txIdle = 0, txTransmitting, txACKWaiting }

Public Member Functions

 CCommunicationPacket (CMachine::InterruptVector vector, UART_TypeDef *UARTx, CMachine::ubit32 baud=9600, bool packetMode=false)
void event (CEvent *e)
 The CEvent listener [ inherits pointer ownership ].
virtual CMachine::ubit8 rx ()
 Receive a sinlge byte.
virtual void rxFlush ()
 Flush receiver.
virtual CMachine::ubit8 tx (CMachine::ubit8 ch)
 transmit a single byte - may yield CPU time if transmitter ring buffer is full.
virtual void txFlush ()
 Flush transmitter.
virtual ~CCommunicationPacket ()

Private Member Functions

void appendData (CByteArray *buffer, CMachine::ubit8 data)
 Convinience packet access method.
CMachine::ubit8 calculateChecksum (CByteArray *buffer)
 Calculate the checksum from a buffer.
CMachine::ubit8 checksum (CByteArray *buffer)
 Convinience packet access method.
CMachine::ubit8 control (CByteArray *buffer)
 Convinience packet access method.
CMachine::ubit8 data (CByteArray *buffer, CMachine::ubit8 offset)
 Convinience packet access method.
bool full (CByteArray *buffer)
 Convinience packet access method.
CMachine::ubit8 length (CByteArray *buffer)
 Convinience packet access method.
CMachine::ubit8 sequence (CByteArray *buffer)
 Convinience packet access method.
void setChecksum (CByteArray *buffer, CMachine::ubit8 checksum)
 Convinience packet access method.
void setData (CByteArray *buffer, CMachine::ubit8 data, CMachine::ubit8 offset)
 Convinience packet access method.
void setLength (CByteArray *buffer, CMachine::ubit8 length)
 Convinience packet access method.
void setSequence (CByteArray *buffer, CMachine::ubit8 sequence)
 Convinience packet access method.
void setType (CByteArray *buffer, CMachine::ubit8 control)
 Convinience packet access method.
CMachine::ubit8 type (CByteArray *buffer)
 Convinience packet access method.

Private Attributes

bool mPacketMode
tRXState mRXState
CMachine::ubit32 mRXTimeout
tTXState mTXState
CMachine::ubit32 mTXTimeout
CByteArray rxBuffer
CByteArray txBuffer

Detailed Description

base class for communications which require a ring buffer for tx and rx.
Author:
Michael Sharkey <mike@pikeaero.com>

Definition at line 36 of file ccommunicationpacket.h.


Member Enumeration Documentation

Definition at line 40 of file ccommunicationpacket.h.

00040                      {
00041             
00042         } tRXState;

Enumerator:
txIdle 
txTransmitting 
txACKWaiting 

Definition at line 44 of file ccommunicationpacket.h.

00044                      {
00045             txIdle=0,               /* Nothing happening */
00046             txTransmitting,         /* At least one byte in transmit buffer -- tx timeout started */
00047             txACKWaiting            /* Waiting for ACK (i.e trasmission received) */
00048         } tTXState;


Constructor & Destructor Documentation

PikeAero::CCommunicationPacket::CCommunicationPacket ( CMachine::InterruptVector  vector,
UART_TypeDef UARTx,
CMachine::ubit32  baud = 9600,
bool  packetMode = false 
)

Definition at line 69 of file ccommunicationpacket.cpp.

References PikeAero::CByteArray::data(), PikeAero::CEventQueue::installListener(), memset, PACKET_SIZE, PikeAero::CByteArray::resize(), rxBuffer, PikeAero::CByteArray::size(), PikeAero::CEvent::Tick, and txBuffer.

00070  : inherited(vector,UARTx,baud)
00071  , mPacketMode(packetMode)
00072  , mTXState(txTransmitting)
00073  , mTXTimeout(0)
00074 {
00075     txBuffer.resize(PACKET_SIZE);
00076     memset(txBuffer.data(),0,rxBuffer.size());
00077 
00078     rxBuffer.resize(PACKET_SIZE);
00079     memset(rxBuffer.data(),0,rxBuffer.size());
00080 
00081     CEventQueue::installListener(this,CEvent::Tick);
00082 }

Here is the call graph for this function:

PikeAero::CCommunicationPacket::~CCommunicationPacket (  )  [virtual]

Definition at line 85 of file ccommunicationpacket.cpp.

References PikeAero::CEventQueue::removeListener(), and PikeAero::CEvent::Tick.

00086 {
00087     CEventQueue::removeListener(this,CEvent::Tick);
00088 }

Here is the call graph for this function:


Member Function Documentation

void PikeAero::CCommunicationPacket::appendData ( CByteArray buffer,
CMachine::ubit8  data 
) [private]

Convinience packet access method.

Definition at line 135 of file ccommunicationpacket.cpp.

References length(), PACKET_OFFSET_DATA, PikeAero::CByteArray::set(), and setLength().

Referenced by tx().

00136 {
00137     buffer->set( PACKET_OFFSET_DATA+length(buffer), data );
00138     setLength(buffer,length(buffer)+1);
00139 }

Here is the call graph for this function:

Here is the caller graph for this function:

CMachine::ubit8 PikeAero::CCommunicationPacket::calculateChecksum ( CByteArray buffer  )  [private]

Calculate the checksum from a buffer.

Definition at line 200 of file ccommunicationpacket.cpp.

References control(), data(), length(), and sequence().

Referenced by txFlush().

00201 {
00202     CMachine::ubit8 sum = 0;
00203 
00204     sum += control(buffer);
00205     sum += sequence(buffer);
00206     for( CMachine::ubit8 i=0; i < length(buffer); i++ )
00207     {
00208         sum += data(buffer,i);
00209     }
00210     return sum;
00211 }

Here is the call graph for this function:

Here is the caller graph for this function:

CMachine::ubit8 PikeAero::CCommunicationPacket::checksum ( CByteArray buffer  )  [private]

Convinience packet access method.

Definition at line 176 of file ccommunicationpacket.cpp.

References PikeAero::CByteArray::at(), and PACKET_OFFSET_CHECKSUM.

00177 {
00178     return buffer->at( PACKET_OFFSET_CHECKSUM );
00179 }

Here is the call graph for this function:

CMachine::ubit8 PikeAero::CCommunicationPacket::control ( CByteArray buffer  )  [private]

Convinience packet access method.

Definition at line 144 of file ccommunicationpacket.cpp.

References PikeAero::CByteArray::at(), and PACKET_OFFSET_CONTROL.

Referenced by calculateChecksum().

00145 {
00146     return buffer->at( PACKET_OFFSET_CONTROL );
00147 }

Here is the call graph for this function:

Here is the caller graph for this function:

CMachine::ubit8 PikeAero::CCommunicationPacket::data ( CByteArray buffer,
CMachine::ubit8  offset 
) [private]

Convinience packet access method.

Definition at line 184 of file ccommunicationpacket.cpp.

References PikeAero::CByteArray::at(), and PACKET_OFFSET_DATA.

Referenced by calculateChecksum().

00185 {
00186     return buffer->at( PACKET_OFFSET_DATA+offset );
00187 }

Here is the call graph for this function:

Here is the caller graph for this function:

void PikeAero::CCommunicationPacket::event ( CEvent e  )  [virtual]

The CEvent listener [ inherits pointer ownership ].

The CEvent listener.

Reimplemented from PikeAero::CObject.

Definition at line 313 of file ccommunicationpacket.cpp.

References PikeAero::CObject::dispatch(), mTXState, mTXTimeout, PikeAero::CEvent::Tick, PikeAero::CEventTimeout::TransmitBufferFilling, txACKWaiting, txIdle, txTransmitting, and PikeAero::CEvent::type().

00314 {
00315     switch( e->type() )
00316     {
00317         case CEvent::Tick:
00318             {
00319                 switch( mTXState )
00320                 {
00321                     case txIdle:
00322                         mTXTimeout = 0;
00323                         break;
00324                     case txTransmitting:
00325                         if ( ++mTXTimeout >= 2 )
00326                         {
00327                             dispatch( new CEventTimeout( this, CEventTimeout::TransmitBufferFilling) );
00328                         }
00329                         break;
00330                     case txACKWaiting:
00331                         mTXTimeout = 0;
00332                         break;
00333                 }
00334             }
00335             break;
00336         default:
00337             break;
00338     }
00339     inherited::event(e);
00340 }

Here is the call graph for this function:

bool PikeAero::CCommunicationPacket::full ( CByteArray buffer  )  [private]

Convinience packet access method.

Definition at line 192 of file ccommunicationpacket.cpp.

References length(), and PACKET_DATA_SIZE.

Referenced by tx().

00193 {
00194     return length(buffer) == PACKET_DATA_SIZE;
00195 }

Here is the call graph for this function:

Here is the caller graph for this function:

CMachine::ubit8 PikeAero::CCommunicationPacket::length ( CByteArray buffer  )  [private]

Convinience packet access method.

Definition at line 160 of file ccommunicationpacket.cpp.

References PikeAero::CByteArray::at(), PACKET_LENGTH_MASK, and PACKET_OFFSET_CONTROL.

Referenced by appendData(), calculateChecksum(), and full().

00161 {
00162     return (buffer->at( PACKET_OFFSET_CONTROL ) & PACKET_LENGTH_MASK);
00163 }

Here is the call graph for this function:

Here is the caller graph for this function:

CMachine::ubit8 PikeAero::CCommunicationPacket::rx (  )  [virtual]

Receive a sinlge byte.

Returns:
Next byte in queue of '' if queue is empty.

FIXME - do stuff here

Reimplemented from PikeAero::CCommunication.

Definition at line 294 of file ccommunicationpacket.cpp.

References mPacketMode.

Referenced by tx().

00295 {
00296     if ( mPacketMode )
00297     {
00298         if ( inherited::rxReady() )
00299         {
00300             CMachine::ubit8 ch = inherited::rx();
00301             /** FIXME - do stuff here */
00302         }
00303     }
00304     else
00305     {
00306         return inherited::rx();
00307     }
00308 }

Here is the caller graph for this function:

void PikeAero::CCommunicationPacket::rxFlush (  )  [virtual]

Flush receiver.

Flush the receiver buffer.

FIXME - do stuff here.

Reimplemented from PikeAero::CCommunication.

Definition at line 236 of file ccommunicationpacket.cpp.

00237 {
00238     /** FIXME - do stuff here. */
00239     inherited::rxFlush();
00240 }

CMachine::ubit8 PikeAero::CCommunicationPacket::sequence ( CByteArray buffer  )  [private]

Convinience packet access method.

Definition at line 168 of file ccommunicationpacket.cpp.

References PikeAero::CByteArray::at(), and PACKET_OFFSET_SEQUENCE.

Referenced by calculateChecksum(), and tx().

00169 {
00170     return buffer->at( PACKET_OFFSET_SEQUENCE );
00171 }

Here is the call graph for this function:

Here is the caller graph for this function:

void PikeAero::CCommunicationPacket::setChecksum ( CByteArray buffer,
CMachine::ubit8  checksum 
) [private]

Convinience packet access method.

Definition at line 119 of file ccommunicationpacket.cpp.

References PACKET_OFFSET_CHECKSUM, and PikeAero::CByteArray::set().

Referenced by txFlush().

00120 {
00121     buffer->set( PACKET_OFFSET_CHECKSUM, checksum );
00122 }

Here is the call graph for this function:

Here is the caller graph for this function:

void PikeAero::CCommunicationPacket::setData ( CByteArray buffer,
CMachine::ubit8  data,
CMachine::ubit8  offset 
) [private]

Convinience packet access method.

Definition at line 127 of file ccommunicationpacket.cpp.

References PACKET_OFFSET_DATA, and PikeAero::CByteArray::set().

00128 {
00129     buffer->set( PACKET_OFFSET_DATA+offset, data );
00130 }

Here is the call graph for this function:

void PikeAero::CCommunicationPacket::setLength ( CByteArray buffer,
CMachine::ubit8  length 
) [private]

Convinience packet access method.

Definition at line 102 of file ccommunicationpacket.cpp.

References PikeAero::CByteArray::at(), PACKET_LENGTH_MASK, PACKET_OFFSET_CONTROL, and PikeAero::CByteArray::set().

Referenced by appendData(), and tx().

00103 {
00104     buffer->set( PACKET_OFFSET_CONTROL, buffer->at( PACKET_OFFSET_CONTROL ) & ~PACKET_LENGTH_MASK );
00105     buffer->set( PACKET_OFFSET_CONTROL, buffer->at( PACKET_OFFSET_CONTROL ) | (length & PACKET_LENGTH_MASK) );
00106 }

Here is the call graph for this function:

Here is the caller graph for this function:

void PikeAero::CCommunicationPacket::setSequence ( CByteArray buffer,
CMachine::ubit8  sequence 
) [private]

Convinience packet access method.

Definition at line 111 of file ccommunicationpacket.cpp.

References PACKET_OFFSET_SEQUENCE, and PikeAero::CByteArray::set().

Referenced by tx().

00112 {
00113     buffer->set( PACKET_OFFSET_SEQUENCE, sequence );
00114 }

Here is the call graph for this function:

Here is the caller graph for this function:

void PikeAero::CCommunicationPacket::setType ( CByteArray buffer,
CMachine::ubit8  control 
) [private]

Convinience packet access method.

Definition at line 93 of file ccommunicationpacket.cpp.

References PikeAero::CByteArray::at(), PACKET_OFFSET_CONTROL, PACKET_TYPE_MASK, and PikeAero::CByteArray::set().

Referenced by tx().

00094 {
00095     buffer->set( PACKET_OFFSET_CONTROL, buffer->at( PACKET_OFFSET_CONTROL ) & ~PACKET_TYPE_MASK );
00096     buffer->set( PACKET_OFFSET_CONTROL, buffer->at( PACKET_OFFSET_CONTROL ) | (type & PACKET_TYPE_MASK) );
00097 }

Here is the call graph for this function:

Here is the caller graph for this function:

CMachine::ubit8 PikeAero::CCommunicationPacket::tx ( CMachine::ubit8  ch  )  [virtual]

transmit a single byte - may yield CPU time if transmitter ring buffer is full.

Returns:
The same byte.

append the char to the packet buffer data field

wait to go back to transmitting mode (i.e. got acknowledgement)

increment sequence number

reset data length

Reimplemented from PikeAero::CCommunicationUART.

Definition at line 246 of file ccommunicationpacket.cpp.

References appendData(), full(), mPacketMode, mTXState, mTXTimeout, PACKET_TYPE_DATA, rx(), sequence(), setLength(), setSequence(), setType(), txACKWaiting, txBuffer, txFlush(), txIdle, txTransmitting, and PikeAero::CTaskScheduler::yield().

Referenced by txFlush().

00247 {
00248     if ( mPacketMode )
00249     {
00250         switch( mTXState )
00251         {
00252             case txIdle:
00253                 mTXTimeout=0;
00254                 mTXState = txTransmitting;
00255             case txTransmitting:
00256                 {
00257                     /** append the char to the packet buffer data field */
00258                     appendData( &txBuffer, ch );
00259                     if ( full( &txBuffer ) )
00260                     {
00261                         mTXState = txACKWaiting;
00262                         setType( &txBuffer, PACKET_TYPE_DATA );
00263                         txFlush();
00264                     }
00265                 }
00266                 break;
00267             case txACKWaiting:
00268                 {
00269                     /** wait to go back to transmitting mode (i.e. got acknowledgement) */
00270                     while( mTXState == txACKWaiting )
00271                     {
00272                         CTaskScheduler::yield();
00273                         rx();
00274                     }
00275                     /** increment sequence number */
00276                     setSequence( &txBuffer, sequence( &txBuffer ) + 1 );
00277                     /** reset data length */
00278                     setLength( &txBuffer, 0 );
00279                 }
00280                 break;
00281         }
00282         return ch;
00283     }
00284     else
00285     {
00286         return inherited::tx( ch );
00287     }
00288 }

Here is the call graph for this function:

Here is the caller graph for this function:

void PikeAero::CCommunicationPacket::txFlush (  )  [virtual]

Flush transmitter.

Flush the transmitter buffer.

Reimplemented from PikeAero::CCommunication.

Definition at line 216 of file ccommunicationpacket.cpp.

References PikeAero::CByteArray::at(), calculateChecksum(), mPacketMode, PACKET_SIZE, setChecksum(), tx(), and txBuffer.

Referenced by tx().

00217 {
00218     if ( mPacketMode )
00219     {
00220         CByteArray* buffer = &txBuffer;
00221         setChecksum( buffer, calculateChecksum( buffer ) );
00222         for( CMachine::ubit32 i=0; i < PACKET_SIZE; i++ )
00223         {
00224             inherited::tx( buffer->at(i) );
00225         }
00226     }
00227     else
00228     {
00229         inherited::txFlush();
00230     }
00231 }

Here is the call graph for this function:

Here is the caller graph for this function:

CMachine::ubit8 PikeAero::CCommunicationPacket::type ( CByteArray buffer  )  [private]

Convinience packet access method.

Definition at line 152 of file ccommunicationpacket.cpp.

References PikeAero::CByteArray::at(), PACKET_OFFSET_CONTROL, and PACKET_TYPE_MASK.

00153 {
00154     return (buffer->at( PACKET_OFFSET_CONTROL ) & PACKET_TYPE_MASK);
00155 }

Here is the call graph for this function:


Field Documentation

Definition at line 97 of file ccommunicationpacket.h.

Referenced by rx(), tx(), and txFlush().

Definition at line 102 of file ccommunicationpacket.h.

Definition at line 103 of file ccommunicationpacket.h.

Definition at line 99 of file ccommunicationpacket.h.

Referenced by event(), and tx().

Definition at line 100 of file ccommunicationpacket.h.

Referenced by event(), and tx().

reciever

Definition at line 109 of file ccommunicationpacket.h.

Referenced by CCommunicationPacket().

transmitter

Definition at line 114 of file ccommunicationpacket.h.

Referenced by CCommunicationPacket(), tx(), and txFlush().


The documentation for this class was generated from the following files:

Generated on Sun Oct 25 13:59:54 2009 for stingray3 by  doxygen 1.5.8