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__) 98 void setTX(uint8_t transmitPin);
103 void setRX(uint8_t receivePin);
108 static inline void tunedDelay(uint16_t delay);
117 SoftwareSerial(uint8_t receivePin, uint8_t transmitPin,
bool inverse_logic =
false);
128 void configure(uint8_t receivePin, uint8_t transmitPin,
bool inverse_logic =
false);
151 void begin(
long speed);
158 inline void end(
void);
180 virtual size_t write(uint8_t byte);
185 virtual int read(
void);
194 virtual void flush(
void);
208 bool SoftwareSerial::isListening(
void) {
209 return this == active_object;
212 bool SoftwareSerial::overflow(
void) {
213 bool ret = _buffer_overflow;
214 _buffer_overflow =
false;
218 #if EBOARD_DEBUG_MODE > 0x0 220 #define _DEBUG_PIN1 11 221 #define _DEBUG_PIN2 13 224 typedef struct _DELAY_TABLE {
226 unsigned short rx_delay_centering;
227 unsigned short rx_delay_intrabit;
228 unsigned short rx_delay_stopbit;
229 unsigned short tx_delay;
232 #if F_CPU == 16000000 234 static const DELAY_TABLE PROGMEM table[] = {
236 { 115200, 1, 17, 17, 12, },
237 { 57600, 10, 37, 37, 33, },
238 { 38400, 25, 57, 57, 54, },
239 { 31250, 31, 70, 70, 68, },
240 { 28800, 34, 77, 77, 74, },
241 { 19200, 54, 117, 117, 114, },
242 { 14400, 74, 156, 156, 153, },
243 { 9600, 114, 236, 236, 233, },
244 { 4800, 233, 474, 474, 471, },
245 { 2400, 471, 950, 950, 947, },
246 { 1200, 947, 1902, 1902, 1899, },
247 { 600, 1902, 3804, 3804, 3800, },
248 { 300, 3804, 7617, 7617, 7614, },
251 const int XMIT_START_ADJUSTMENT = 5;
253 #elif F_CPU == 8000000 255 static const DELAY_TABLE table[] PROGMEM = {
257 { 115200, 1, 5, 5, 3, },
258 { 57600, 1, 15, 15, 13, },
259 { 38400, 2, 25, 26, 23, },
260 { 31250, 7, 32, 33, 29, },
261 { 28800, 11, 35, 35, 32, },
262 { 19200, 20, 55, 55, 52, },
263 { 14400, 30, 75, 75, 72, },
264 { 9600, 50, 114, 114, 112, },
265 { 4800, 110, 233, 233, 230, },
266 { 2400, 229, 472, 472, 469, },
267 { 1200, 467, 948, 948, 945, },
268 { 600, 948, 1895, 1895, 1890, },
269 { 300, 1895, 3805, 3805, 3802, },
272 const int XMIT_START_ADJUSTMENT = 4;
274 #elif F_CPU == 20000000 276 static const DELAY_TABLE PROGMEM table[] = {
278 { 115200, 3, 21, 21, 18, },
279 { 57600, 20, 43, 43, 41, },
280 { 38400, 37, 73, 73, 70, },
281 { 31250, 45, 89, 89, 88, },
282 { 28800, 46, 98, 98, 95, },
283 { 19200, 71, 148, 148, 145, },
284 { 14400, 96, 197, 197, 194, },
285 { 9600, 146, 297, 297, 294, },
286 { 4800, 296, 595, 595, 592, },
287 { 2400, 592, 1189, 1189, 1186, },
288 { 1200, 1187, 2379, 2379, 2376, },
289 { 600, 2379, 4759, 4759, 4755, },
290 { 300, 4759, 9523, 9523, 9520, },
293 const int XMIT_START_ADJUSTMENT = 6;
296 #error This version of SoftwareSerial supports only 20, 16 and 8MHz processors 299 inline void SoftwareSerial::tunedDelay(uint16_t delay) {
302 asm volatile(
"sbiw %0, 0x01 \n\t" 307 :
"+r" (delay),
"+a" (tmp)
312 void SoftwareSerial::tx_pin_write(uint8_t pin_state) {
313 if (pin_state == LOW)
314 *_transmitPortRegister &= ~_transmitBitMask;
316 *_transmitPortRegister |= _transmitBitMask;
319 uint8_t SoftwareSerial::rx_pin_read() {
320 return *_receivePortRegister & _receiveBitMask;
323 inline void SoftwareSerial::handle_interrupt() {
325 active_object->recv();
329 #if defined(PCINT0_vect) 331 SoftwareSerial::handle_interrupt();
335 #if defined(PCINT1_vect) 337 SoftwareSerial::handle_interrupt();
341 #if defined(PCINT2_vect) 343 SoftwareSerial::handle_interrupt();
347 #if defined(PCINT3_vect) 349 SoftwareSerial::handle_interrupt();
355 void SoftwareSerial::end() {
356 if (digitalPinToPCMSK(_receivePin))
357 *digitalPinToPCMSK(_receivePin) &= ~_BV(digitalPinToPCMSKbit(_receivePin));
361 SoftwareSerial *SoftwareSerial::active_object = 0;
363 volatile uint8_t SoftwareSerial::_receive_buffer_tail = 0;
364 volatile uint8_t SoftwareSerial::_receive_buffer_head = 0;
365 #if EBOARD_DEBUG_MODE > 0x0 366 inline void DebugPulse(uint8_t pin, uint8_t count) {
368 volatile uint8_t *pport = portOutputRegister(digitalPinToPort(pin));
370 uint8_t val = *pport;
372 *pport = val | digitalPinToBitMask(pin);
380 bool SoftwareSerial::listen() {
381 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) {
419 tunedDelay(_rx_delay_intrabit);
420 #if EBOARD_DEBUG_MODE > 0x0 421 DebugPulse(_DEBUG_PIN2, 1);
431 tunedDelay(_rx_delay_stopbit);
432 #if EBOARD_DEBUG_MODE > 0x0 433 DebugPulse(_DEBUG_PIN2, 1);
438 if ((_receive_buffer_tail + 1) %
_SS_MAX_RX_BUFF != _receive_buffer_head) {
439 _receive_buffer[_receive_buffer_tail] = d;
442 #if EBOARD_DEBUG_MODE > 0x0 443 #if _DEBUG // for scope: pulse pin as overflow indictator 444 DebugPulse(_DEBUG_PIN1, 1);
447 _buffer_overflow =
true;
451 #if GCC_VERSION < 40302 468 _rx_delay_centering = 0;
469 _rx_delay_intrabit = 0;
470 _rx_delay_stopbit = 0;
472 _buffer_overflow =
false;
473 _inverse_logic = inverse_logic;
480 _rx_delay_centering(0),
481 _rx_delay_intrabit(0),
482 _rx_delay_stopbit(0),
484 _buffer_overflow(false),
485 _inverse_logic(inverse_logic) {
496 digitalWrite(tx, HIGH);
504 digitalWrite(rx, HIGH);
513 for (
unsigned i=0; i<
sizeof(table)/
sizeof(table[0]); ++i) {
514 long baud = pgm_read_dword(&table[i].baud);
519 _tx_delay = pgm_read_word(&table[i].tx_delay);
533 pinMode(_DEBUG_PIN1, OUTPUT);
534 pinMode(_DEBUG_PIN2, OUTPUT);
565 uint8_t oldSREG = SREG;
572 for (byte mask = 0x01; mask; mask <<= 1) {
583 for (byte mask = 0x01; mask; mask <<= 1) {
603 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
SoftwareSerial(void)
this enables eBoard to use the SoftwareSerial-interface
static volatile uint8_t _receive_buffer_head
current location in rxBuffer
void configure(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic=false)
this allows eBoard to configure the object
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 ;)
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...