Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef FastSerial_h
00043 #define FastSerial_h
00044
00045
00046 #ifdef HardwareSerial_h
00047 # error Must include FastSerial.h before the Arduino serial driver is defined.
00048 #endif
00049 #define HardwareSerial_h
00050
00051 #include <inttypes.h>
00052 #include <stdlib.h>
00053 #include <avr/io.h>
00054 #include <avr/interrupt.h>
00055
00056 #include "BetterStream.h"
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 extern class FastSerial Serial;
00085 extern class FastSerial Serial1;
00086 extern class FastSerial Serial2;
00087 extern class FastSerial Serial3;
00088
00089 class FastSerial : public BetterStream {
00090 public:
00091 FastSerial(const uint8_t portNumber,
00092 volatile uint8_t *ubrrh,
00093 volatile uint8_t *ubrrl,
00094 volatile uint8_t *ucsra,
00095 volatile uint8_t *ucsrb,
00096 const uint8_t u2x,
00097 const uint8_t portEnableBits,
00098 const uint8_t portTxBits);
00099
00100
00101 virtual void begin(long baud);
00102 virtual void begin(long baud, unsigned int rxSpace, unsigned int txSpace);
00103 virtual void end(void);
00104 virtual int available(void);
00105 virtual int read(void);
00106 virtual int peek(void);
00107 virtual void flush(void);
00108 virtual void write(uint8_t c);
00109 using BetterStream::write;
00110
00111
00112 struct Buffer {
00113 volatile uint16_t head, tail;
00114 uint16_t mask;
00115 uint8_t *bytes;
00116 };
00117
00118 private:
00119
00120 volatile uint8_t *_ubrrh;
00121 volatile uint8_t *_ubrrl;
00122 volatile uint8_t *_ucsra;
00123 volatile uint8_t *_ucsrb;
00124
00125
00126 uint8_t _portEnableBits;
00127 uint8_t _portTxBits;
00128 uint8_t _u2x;
00129
00130
00131 Buffer *_rxBuffer;
00132 Buffer *_txBuffer;
00133 bool _open;
00134
00135 static bool _allocBuffer(Buffer *buffer, unsigned int size);
00136 static void _freeBuffer(Buffer *buffer);
00137 };
00138
00139
00140 extern FastSerial::Buffer __FastSerial__rxBuffer[];
00141 extern FastSerial::Buffer __FastSerial__txBuffer[];
00142
00143
00144 #define FastSerialHandler(_PORT, _RXVECTOR, _TXVECTOR, _UDR, _UCSRB, _TXBITS) \
00145 ISR(_RXVECTOR, ISR_BLOCK) \
00146 { \
00147 uint8_t c; \
00148 int16_t i; \
00149 \
00150 \
00151 c = _UDR; \
00152 \
00153 i = (__FastSerial__rxBuffer[_PORT].head + 1) & __FastSerial__rxBuffer[_PORT].mask; \
00154 \
00155 if (i != __FastSerial__rxBuffer[_PORT].tail) { \
00156 \
00157 __FastSerial__rxBuffer[_PORT].bytes[__FastSerial__rxBuffer[_PORT].head] = c; \
00158 __FastSerial__rxBuffer[_PORT].head = i; \
00159 } \
00160 } \
00161 ISR(_TXVECTOR, ISR_BLOCK) \
00162 { \
00163 \
00164 if (__FastSerial__txBuffer[_PORT].tail != __FastSerial__txBuffer[_PORT].head) { \
00165 _UDR = __FastSerial__txBuffer[_PORT].bytes[__FastSerial__txBuffer[_PORT].tail]; \
00166 \
00167 __FastSerial__txBuffer[_PORT].tail = \
00168 (__FastSerial__txBuffer[_PORT].tail + 1) & __FastSerial__txBuffer[_PORT].mask; \
00169 } else { \
00170 \
00171 if (__FastSerial__txBuffer[_PORT].head == __FastSerial__txBuffer[_PORT].tail) \
00172 _UCSRB &= ~_TXBITS; \
00173 } \
00174 } \
00175 struct hack
00176
00177
00178
00179
00180
00181 #if !defined(USART0_RX_vect)
00182 # if defined(USART_RX_vect)
00183 # define USART0_RX_vect USART_RX_vect
00184 # define USART0_UDRE_vect USART_UDRE_vect
00185 # elif defined(UART0_RX_vect)
00186 # define USART0_RX_vect UART0_RX_vect
00187 # define USART0_UDRE_vect UART0_UDRE_vect
00188 # endif
00189 #endif
00190
00191 #if !defined(USART1_RX_vect)
00192 # if defined(UART1_RX_vect)
00193 # define USART1_RX_vect UART1_RX_vect
00194 # define USART1_UDRE_vect UART1_UDRE_vect
00195 # endif
00196 #endif
00197
00198 #if !defined(UDR0)
00199 # if defined(UDR)
00200 # define UDR0 UDR
00201 # define UBRR0H UBRRH
00202 # define UBRR0L UBRRL
00203 # define UCSR0A UCSRA
00204 # define UCSR0B UCSRB
00205 # define U2X0 U2X
00206 # define RXEN0 RXEN
00207 # define TXEN0 TXEN
00208 # define RXCIE0 RXCIE
00209 # define UDRIE0 UDRIE
00210 # endif
00211 #endif
00212
00213
00214
00215
00216 #define FastSerialPort(_name, _num) \
00217 FastSerial _name(_num, \
00218 &UBRR##_num##H, \
00219 &UBRR##_num##L, \
00220 &UCSR##_num##A, \
00221 &UCSR##_num##B, \
00222 U2X##_num, \
00223 (_BV(RXEN##_num) | _BV(TXEN##_num) | _BV(RXCIE##_num)), \
00224 (_BV(UDRIE##_num))); \
00225 FastSerialHandler(_num, \
00226 USART##_num##_RX_vect, \
00227 USART##_num##_UDRE_vect, \
00228 UDR##_num, \
00229 UCSR##_num##B, \
00230 _BV(UDRIE##_num))
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 #define FastSerialPort0(_portName) FastSerialPort(_portName, 0)
00241 #define FastSerialPort1(_portName) FastSerialPort(_portName, 1)
00242 #define FastSerialPort2(_portName) FastSerialPort(_portName, 2)
00243 #define FastSerialPort3(_portName) FastSerialPort(_portName, 3)
00244
00245 #endif // FastSerial_h