1 #ifndef EAGLE_EBOARD_HELPLIB_SOFTWARESERIAL 2 #define EAGLE_EBOARD_HELPLIB_SOFTWARESERIAL 16 #define _SS_MAX_RX_BUFF 64 // RX buffer size 18 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) 99 void setTX(uint8_t transmitPin);
104 void setRX(uint8_t receivePin);
109 static inline void tunedDelay(uint16_t delay);
119 SoftwareSerial(uint8_t receivePin, uint8_t transmitPin,
bool inverse_logic =
false);
141 void begin(
long speed);
148 inline void end(
void);
170 virtual size_t write(uint8_t byte);
175 virtual int read(
void);
184 virtual void flush(
void);
198 bool SoftwareSerial::isListening(
void) {
199 return this == active_object;
202 bool SoftwareSerial::overflow(
void) {
203 bool ret = _buffer_overflow;
204 _buffer_overflow =
false;
216 #if EBOARD_DEBUG_MODE > 0x0 218 #define _DEBUG_PIN1 11 219 #define _DEBUG_PIN2 13 221 typedef struct _DELAY_TABLE {
223 unsigned short rx_delay_centering;
224 unsigned short rx_delay_intrabit;
225 unsigned short rx_delay_stopbit;
226 unsigned short tx_delay;
228 #if F_CPU == 16000000 230 static const DELAY_TABLE PROGMEM table[] = {
232 { 115200, 1, 17, 17, 12, },
233 { 57600, 10, 37, 37, 33, },
234 { 38400, 25, 57, 57, 54, },
235 { 31250, 31, 70, 70, 68, },
236 { 28800, 34, 77, 77, 74, },
237 { 19200, 54, 117, 117, 114, },
238 { 14400, 74, 156, 156, 153, },
239 { 9600, 114, 236, 236, 233, },
240 { 4800, 233, 474, 474, 471, },
241 { 2400, 471, 950, 950, 947, },
242 { 1200, 947, 1902, 1902, 1899, },
243 { 600, 1902, 3804, 3804, 3800, },
244 { 300, 3804, 7617, 7617, 7614, },
247 const int XMIT_START_ADJUSTMENT = 5;
249 #elif F_CPU == 8000000 251 static const DELAY_TABLE table[] PROGMEM = {
253 { 115200, 1, 5, 5, 3, },
254 { 57600, 1, 15, 15, 13, },
255 { 38400, 2, 25, 26, 23, },
256 { 31250, 7, 32, 33, 29, },
257 { 28800, 11, 35, 35, 32, },
258 { 19200, 20, 55, 55, 52, },
259 { 14400, 30, 75, 75, 72, },
260 { 9600, 50, 114, 114, 112, },
261 { 4800, 110, 233, 233, 230, },
262 { 2400, 229, 472, 472, 469, },
263 { 1200, 467, 948, 948, 945, },
264 { 600, 948, 1895, 1895, 1890, },
265 { 300, 1895, 3805, 3805, 3802, },
268 const int XMIT_START_ADJUSTMENT = 4;
270 #elif F_CPU == 20000000 272 static const DELAY_TABLE PROGMEM table[] = {
274 { 115200, 3, 21, 21, 18, },
275 { 57600, 20, 43, 43, 41, },
276 { 38400, 37, 73, 73, 70, },
277 { 31250, 45, 89, 89, 88, },
278 { 28800, 46, 98, 98, 95, },
279 { 19200, 71, 148, 148, 145, },
280 { 14400, 96, 197, 197, 194, },
281 { 9600, 146, 297, 297, 294, },
282 { 4800, 296, 595, 595, 592, },
283 { 2400, 592, 1189, 1189, 1186, },
284 { 1200, 1187, 2379, 2379, 2376, },
285 { 600, 2379, 4759, 4759, 4755, },
286 { 300, 4759, 9523, 9523, 9520, },
289 const int XMIT_START_ADJUSTMENT = 6;
292 #error This version of SoftwareSerial supports only 20, 16 and 8MHz processors 295 inline void SoftwareSerial::tunedDelay(uint16_t delay) {
298 asm volatile(
"sbiw %0, 0x01 \n\t" 303 :
"+r" (delay),
"+a" (tmp)
310 void SoftwareSerial::tx_pin_write(uint8_t pin_state) {
311 if (pin_state == LOW)
312 *_transmitPortRegister &= ~_transmitBitMask;
314 *_transmitPortRegister |= _transmitBitMask;
317 uint8_t SoftwareSerial::rx_pin_read() {
318 return *_receivePortRegister & _receiveBitMask;
321 inline void SoftwareSerial::handle_interrupt() {
323 active_object->recv();
327 #if defined(PCINT0_vect) 329 SoftwareSerial::handle_interrupt();
333 #if defined(PCINT1_vect) 335 SoftwareSerial::handle_interrupt();
339 #if defined(PCINT2_vect) 341 SoftwareSerial::handle_interrupt();
345 #if defined(PCINT3_vect) 347 SoftwareSerial::handle_interrupt();
353 void SoftwareSerial::end() {
354 if (digitalPinToPCMSK(_receivePin))
355 *digitalPinToPCMSK(_receivePin) &= ~_BV(digitalPinToPCMSKbit(_receivePin));
359 SoftwareSerial *SoftwareSerial::active_object = 0;
361 volatile uint8_t SoftwareSerial::_receive_buffer_tail = 0;
362 volatile uint8_t SoftwareSerial::_receive_buffer_head = 0;
363 #if EBOARD_DEBUG_MODE > 0x0 364 inline void DebugPulse(uint8_t pin, uint8_t count) {
366 volatile uint8_t *pport = portOutputRegister(digitalPinToPort(pin));
368 uint8_t val = *pport;
371 *pport = val | digitalPinToBitMask(pin);
379 bool SoftwareSerial::listen() {
380 if (active_object !=
this)
382 _buffer_overflow =
false;
383 uint8_t oldSREG = SREG;
385 _receive_buffer_head = _receive_buffer_tail = 0;
386 active_object =
this;
394 void SoftwareSerial::recv() {
396 #if GCC_VERSION < 40302 411 if (_inverse_logic ? rx_pin_read() : !rx_pin_read()) {
413 tunedDelay(_rx_delay_centering);
414 #if EBOARD_DEBUG_MODE > 0x0 415 DebugPulse(_DEBUG_PIN2, 1);
418 for (uint8_t i=0x1; i; i <<= 1)
420 tunedDelay(_rx_delay_intrabit);
421 #if EBOARD_DEBUG_MODE > 0x0 422 DebugPulse(_DEBUG_PIN2, 1);
432 tunedDelay(_rx_delay_stopbit);
433 #if EBOARD_DEBUG_MODE > 0x0 434 DebugPulse(_DEBUG_PIN2, 1);
439 if ((_receive_buffer_tail + 1) %
_SS_MAX_RX_BUFF != _receive_buffer_head) {
440 _receive_buffer[_receive_buffer_tail] = d;
444 #if EBOARD_DEBUG_MODE > 0x0 445 #if _DEBUG // for scope: pulse pin as overflow indictator 446 DebugPulse(_DEBUG_PIN1, 1);
449 _buffer_overflow =
true;
453 #if GCC_VERSION < 40302 468 SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin,
bool inverse_logic ) :
469 _rx_delay_centering(0),
470 _rx_delay_intrabit(0),
471 _rx_delay_stopbit(0),
473 _buffer_overflow(false),
474 _inverse_logic(inverse_logic) {
485 digitalWrite(tx, HIGH);
487 uint8_t port = digitalPinToPort(tx);
494 digitalWrite(rx, HIGH);
497 uint8_t port = digitalPinToPort(rx);
504 for (
unsigned i=0; i<
sizeof(table)/
sizeof(table[0]); ++i) {
505 long baud = pgm_read_dword(&table[i].baud);
510 _tx_delay = pgm_read_word(&table[i].tx_delay);
524 pinMode(_DEBUG_PIN1, OUTPUT);
525 pinMode(_DEBUG_PIN2, OUTPUT);
556 uint8_t oldSREG = SREG;
563 for (byte mask = 0x01; mask; mask <<= 1) {
575 for (byte mask = 0x01; mask; mask <<= 1) {
596 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
[328p] This is used to avoid path resolving issues and defines the common known Arduino SoftwareSeria...