1 #ifndef EAGLE_EBOARD_HELPLIB_SOFTWARESERIAL 2 #define EAGLE_EBOARD_HELPLIB_SOFTWARESERIAL 4 #define _SS_MAX_RX_BUFF 64 // RX buffer size 6 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) 81 void setTX(uint8_t transmitPin);
86 void setRX(uint8_t receivePin);
101 SoftwareSerial(uint8_t receivePin, uint8_t transmitPin,
bool inverse_logic =
false);
123 void begin(
long speed);
130 inline void end(
void);
152 virtual size_t write(uint8_t byte);
157 virtual int read(
void);
166 virtual void flush(
void);
174 bool SoftwareSerial::isListening(
void) {
175 return this == active_object;
178 bool SoftwareSerial::overflow(
void) {
179 bool ret = _buffer_overflow;
180 _buffer_overflow =
false;
192 #if EBOARD_DEBUG_MODE > 0x0 194 #define _DEBUG_PIN1 11 195 #define _DEBUG_PIN2 13 197 typedef struct _DELAY_TABLE {
199 unsigned short rx_delay_centering;
200 unsigned short rx_delay_intrabit;
201 unsigned short rx_delay_stopbit;
202 unsigned short tx_delay;
204 #if F_CPU == 16000000 206 static const DELAY_TABLE PROGMEM table[] = {
208 { 115200, 1, 17, 17, 12, },
209 { 57600, 10, 37, 37, 33, },
210 { 38400, 25, 57, 57, 54, },
211 { 31250, 31, 70, 70, 68, },
212 { 28800, 34, 77, 77, 74, },
213 { 19200, 54, 117, 117, 114, },
214 { 14400, 74, 156, 156, 153, },
215 { 9600, 114, 236, 236, 233, },
216 { 4800, 233, 474, 474, 471, },
217 { 2400, 471, 950, 950, 947, },
218 { 1200, 947, 1902, 1902, 1899, },
219 { 600, 1902, 3804, 3804, 3800, },
220 { 300, 3804, 7617, 7617, 7614, },
223 const int XMIT_START_ADJUSTMENT = 5;
225 #elif F_CPU == 8000000 227 static const DELAY_TABLE table[] PROGMEM = {
229 { 115200, 1, 5, 5, 3, },
230 { 57600, 1, 15, 15, 13, },
231 { 38400, 2, 25, 26, 23, },
232 { 31250, 7, 32, 33, 29, },
233 { 28800, 11, 35, 35, 32, },
234 { 19200, 20, 55, 55, 52, },
235 { 14400, 30, 75, 75, 72, },
236 { 9600, 50, 114, 114, 112, },
237 { 4800, 110, 233, 233, 230, },
238 { 2400, 229, 472, 472, 469, },
239 { 1200, 467, 948, 948, 945, },
240 { 600, 948, 1895, 1895, 1890, },
241 { 300, 1895, 3805, 3805, 3802, },
244 const int XMIT_START_ADJUSTMENT = 4;
246 #elif F_CPU == 20000000 248 static const DELAY_TABLE PROGMEM table[] = {
250 { 115200, 3, 21, 21, 18, },
251 { 57600, 20, 43, 43, 41, },
252 { 38400, 37, 73, 73, 70, },
253 { 31250, 45, 89, 89, 88, },
254 { 28800, 46, 98, 98, 95, },
255 { 19200, 71, 148, 148, 145, },
256 { 14400, 96, 197, 197, 194, },
257 { 9600, 146, 297, 297, 294, },
258 { 4800, 296, 595, 595, 592, },
259 { 2400, 592, 1189, 1189, 1186, },
260 { 1200, 1187, 2379, 2379, 2376, },
261 { 600, 2379, 4759, 4759, 4755, },
262 { 300, 4759, 9523, 9523, 9520, },
265 const int XMIT_START_ADJUSTMENT = 6;
268 #error This version of SoftwareSerial supports only 20, 16 and 8MHz processors 271 SoftwareSerial *SoftwareSerial::active_object = 0;
273 volatile uint8_t SoftwareSerial::_receive_buffer_tail = 0;
274 volatile uint8_t SoftwareSerial::_receive_buffer_head = 0;
275 #if EBOARD_DEBUG_MODE > 0x0 276 inline void DebugPulse(uint8_t pin, uint8_t count) {
278 volatile uint8_t *pport = portOutputRegister(digitalPinToPort(pin));
280 uint8_t val = *pport;
283 *pport = val | digitalPinToBitMask(pin);
289 inline void SoftwareSerial::tunedDelay(uint16_t delay) {
292 asm volatile(
"sbiw %0, 0x01 \n\t" 297 :
"+r" (delay),
"+a" (tmp)
302 bool SoftwareSerial::listen() {
303 if (active_object !=
this)
305 _buffer_overflow =
false;
306 uint8_t oldSREG = SREG;
308 _receive_buffer_head = _receive_buffer_tail = 0;
309 active_object =
this;
317 void SoftwareSerial::recv() {
319 #if GCC_VERSION < 40302 334 if (_inverse_logic ? rx_pin_read() : !rx_pin_read()) {
336 tunedDelay(_rx_delay_centering);
337 #if EBOARD_DEBUG_MODE > 0x0 338 DebugPulse(_DEBUG_PIN2, 1);
341 for (uint8_t i=0x1; i; i <<= 1)
343 tunedDelay(_rx_delay_intrabit);
344 #if EBOARD_DEBUG_MODE > 0x0 345 DebugPulse(_DEBUG_PIN2, 1);
355 tunedDelay(_rx_delay_stopbit);
356 #if EBOARD_DEBUG_MODE > 0x0 357 DebugPulse(_DEBUG_PIN2, 1);
362 if ((_receive_buffer_tail + 1) %
_SS_MAX_RX_BUFF != _receive_buffer_head) {
363 _receive_buffer[_receive_buffer_tail] = d;
367 #if EBOARD_DEBUG_MODE > 0x0 368 #if _DEBUG // for scope: pulse pin as overflow indictator 369 DebugPulse(_DEBUG_PIN1, 1);
372 _buffer_overflow =
true;
376 #if GCC_VERSION < 40302 390 void SoftwareSerial::tx_pin_write(uint8_t pin_state) {
391 if (pin_state == LOW)
392 *_transmitPortRegister &= ~_transmitBitMask;
394 *_transmitPortRegister |= _transmitBitMask;
397 uint8_t SoftwareSerial::rx_pin_read() {
398 return *_receivePortRegister & _receiveBitMask;
401 inline void SoftwareSerial::handle_interrupt() {
403 active_object->recv();
407 #if defined(PCINT0_vect) 409 SoftwareSerial::handle_interrupt();
413 #if defined(PCINT1_vect) 415 SoftwareSerial::handle_interrupt();
419 #if defined(PCINT2_vect) 421 SoftwareSerial::handle_interrupt();
425 #if defined(PCINT3_vect) 427 SoftwareSerial::handle_interrupt();
431 SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin,
bool inverse_logic ) :
432 _rx_delay_centering(0),
433 _rx_delay_intrabit(0),
434 _rx_delay_stopbit(0),
436 _buffer_overflow(false),
437 _inverse_logic(inverse_logic) {
448 digitalWrite(tx, HIGH);
450 uint8_t port = digitalPinToPort(tx);
457 digitalWrite(rx, HIGH);
460 uint8_t port = digitalPinToPort(rx);
467 for (
unsigned i=0; i<
sizeof(table)/
sizeof(table[0]); ++i) {
468 long baud = pgm_read_dword(&table[i].baud);
473 _tx_delay = pgm_read_word(&table[i].tx_delay);
487 pinMode(_DEBUG_PIN1, OUTPUT);
488 pinMode(_DEBUG_PIN2, OUTPUT);
525 uint8_t oldSREG = SREG;
532 for (byte mask = 0x01; mask; mask <<= 1) {
544 for (byte mask = 0x01; mask; mask <<= 1) {
565 uint8_t oldSREG = SREG;
uint16_t _rx_delay_intrabit
the rx startbit delay
void recv(void)
private receive routine called each time interrupt handler gets triggered
void setTX(uint8_t transmitPin)
sets a specific pin to be 'the chosen one' as a txPin
void begin(long speed)
the start function to setup delay_values etc.
uint8_t _transmitBitMask
the pin mask to address the tx pin
static volatile uint8_t _receive_buffer_head
current location in rxBuffer
virtual size_t write(uint8_t byte)
writes a specific value to the tx register
bool isListening(void)
checks if this object is the listening object
uint16_t _rx_delay_stopbit
the rx stopbit dely
int peek(void)
reads the actual pointed rxBuffer element without dropping it
uint8_t rx_pin_read(void)
simple routine to read the rxPin by registers
bool listen(void)
sets the SoftwareSerial object to be the listening one gaining control over the buffers etc...
void tx_pin_write(uint8_t pin_state)
writes a bool value on the txPin by registers
this namespace contains all the Don't use manually classes ;)
SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic=false)
the constructor for the SoftwareSerial object
virtual void flush(void)
resets the position in buffer and the buffer itself if the object is listening
static char _receive_buffer[64]
the buffer for rxBuffer
~SoftwareSerial(void)
the destructor of the SoftwareSerial object
bool overflow(void)
returns the current overflow flag and disables it
virtual int available(void)
checks if there is data to read available
static void handle_interrupt(void)
used to handle interrupts on active listening object
volatile uint8_t * _transmitPortRegister
the register the reveice pin is located on
uint16_t _tx_delay
the (generic) tx delay
void setRX(uint8_t receivePin)
sets a specific pin to be 'the chosen one' as a rxPin
void end(void)
ends communcation on the rx pin
static SoftwareSerial * active_object
the active SoftwareSerial object to operate on
uint16_t _inverse_logic
determining if all pin reads etc whould be inverted (e.g. no pullup on rx);
uint16_t _rx_delay_centering
the rx center delay
static volatile uint8_t _receive_buffer_tail
size of rxBuffer
uint8_t _receivePin
the id of the receive pin
uint16_t _buffer_overflow
determining if an _buffer_overflow occured
uint8_t _receiveBitMask
the pin mask to directly read from register (Rx)
virtual int read(void)
reads the actual pointed rxBuffer top element
volatile uint8_t * _receivePortRegister
the register the reveice pin is located on
static void tunedDelay(uint16_t delay)
apply a specific delay to achieve higher precision
This is used to avoid path resolving issues and defines the common known Arduino SoftwareSerial inter...