00001 /* -*- mode:C++; fill-column: 100 -*- 00002 00003 SFBByteBuffer.h - Circular byte buffering between the UART interrupt and the 00004 background packet dispatching levels 00005 Copyright (C) 2009 The Regents of the University of New Mexico. All rights reserved. 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Lesser General Public 00009 License as published by the Free Software Foundation; either 00010 version 2.1 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this library; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 00020 USA 00021 00022 $Id$ 00023 */ 00024 00051 #ifndef SFBBYTEBUFFER_H 00052 #define SFBBYTEBUFFER_H 00053 00054 #include "SFBTypes.h" /* For u8 etc */ 00055 #include "SFBConstants.h" /* For BYTE_BUFFER_BYTES */ 00056 00061 class SFBByteBuffer { 00062 00063 protected: 00064 u8 (&buf)[BYTE_BUFFER_BYTES]; 00065 u16 oldIndex; 00066 u16 newIndex; 00067 00068 00069 SFBByteBuffer(u8 (&buffer)[BYTE_BUFFER_BYTES]) 00070 : buf(buffer), 00071 oldIndex(0), 00072 newIndex(0) 00073 { } 00074 00075 u16 spaceAvailable() { 00076 return ((BYTE_BUFFER_BYTES-1)-(newIndex-oldIndex))&BYTE_BUFFER_MASK; 00077 } 00078 00079 u16 bytesAvailable() { 00080 return (newIndex-oldIndex)&BYTE_BUFFER_MASK; 00081 } 00082 00083 u16 wrap(u16 index) { 00084 return index&BYTE_BUFFER_MASK; 00085 } 00086 void uncheckedStoreByte(u8 data) { 00087 buf[newIndex] = data; 00088 newIndex = wrap(newIndex+1); 00089 } 00090 u8 uncheckedFetchByte() { 00091 u8 val = buf[oldIndex]; 00092 oldIndex = wrap(oldIndex+1); 00093 return val; 00094 } 00095 00096 enum { 00097 BB_ESC = 0xA5, 00098 BB_EESC = 0x00 // BB_ESC+BB_EESC -> single BB_ESC datum 00099 // BB_ESC+anything else -> overflow and/or hw error occurred, anything else==flags 00100 }; 00101 00102 }; 00103 00114 class SFBRxByteBuffer : public SFBByteBuffer { 00115 public: 00116 00117 enum { 00118 BBFLAG_ERRORBITS= PK_OVERRUN|PK_PARITY|PK_FRAMING|PK_BREAK, 00119 BBFLAG_OVERFLOW= PK_BUFFER, 00120 BBFLAG_ZERO= PK_DELETED|PK_BAD_ESCAPE 00121 }; 00122 00123 SFBRxByteBuffer(u8 (&buffer)[BYTE_BUFFER_BYTES]) : SFBByteBuffer(buffer), flags(0) { } 00124 00128 void resetBG() ; 00129 00133 int fetchByteBG() ; 00134 00138 void storeErrorStatusIL(u8 status) { 00139 flags |= status; // Just add in the bits, will be flushed in storeByteIL 00140 } 00141 00145 void storeByteIL(u8 data) ; 00146 00147 private: 00148 u8 flags; 00149 00150 }; 00151 00162 class SFBTxByteBuffer : public SFBByteBuffer { 00163 public: 00164 SFBTxByteBuffer(u8 (&buffer)[BYTE_BUFFER_BYTES]) : SFBByteBuffer(buffer), pendingCount(0) { } 00165 00169 void resetBG() ; 00170 00175 void storeByteBG(u8 data) ; 00176 00181 bool canStoreBytesBG(u32 count) ; 00182 00192 bool shipBytesBG() ; 00193 00194 00199 int fetchByteIL() ; 00200 00201 private: 00202 int pendingCount; 00203 00204 }; 00205 00206 #endif /* SFBBYTEBUFFER_H */ 00207