Skip to content

Commit effd880

Browse files
bigdinotechcalvinatintel
authored andcommitted
CurieSoftwareSerial Improvements
-changed global variables into static -allow different baud rates for different instances of a SoftwareSerial object -minor code cleanup of unused code -moved source code into src directory
1 parent ef9d032 commit effd880

File tree

2 files changed

+56
-67
lines changed

2 files changed

+56
-67
lines changed

libraries/CurieSoftwareSerial/SoftwareSerial.cpp renamed to libraries/CurieSoftwareSerial/src/SoftwareSerial.cpp

Lines changed: 46 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,17 @@ char SoftwareSerial::_receive_buffer[_SS_MAX_RX_BUFF];
4242
volatile uint8_t SoftwareSerial::_receive_buffer_tail = 0;
4343
volatile uint8_t SoftwareSerial::_receive_buffer_head = 0;
4444

45-
46-
//
47-
// Globals
48-
//
49-
uint8_t rxPin;
50-
uint16_t bitDelay;
51-
uint16_t rxIntraBitDelay;
52-
int rxCenteringDelay;
53-
int initRxIntraBitDelay;
54-
int firstIntraBitDelay;
55-
int initRxCenteringDelay;
56-
bool firstStartBit = true;
57-
bool bufferOverflow = true;
58-
bool invertedLogic = false;
59-
bool isSOCGpio = false;
45+
static uint8_t _rxPin;
46+
static uint16_t bitDelay;
47+
static uint16_t rxIntraBitDelay;
48+
static int rxCenteringDelay;
49+
static int initRxIntraBitDelay;
50+
static int firstIntraBitDelay;
51+
static int initRxCenteringDelay;
52+
static bool firstStartBit = true;
53+
static bool bufferOverflow = true;
54+
static bool invertedLogic = false;
55+
static bool isSOCGpio = false;
6056

6157
//
6258
// Debugging
@@ -94,14 +90,21 @@ bool SoftwareSerial::listen()
9490
bufferOverflow = false;
9591
_receive_buffer_head = _receive_buffer_tail = 0;
9692
active_object = this;
97-
rxPin = _receivePin;
93+
_rxPin = _receivePin;
94+
rxIntraBitDelay = _rx_delay_intrabit;
95+
rxCenteringDelay = _rx_delay_centering;
96+
initRxIntraBitDelay = _rx_delay_init_intrabit;
97+
firstIntraBitDelay = _rx_delay_first_intrabit;
98+
initRxCenteringDelay = _rx_delay_init_centering;
99+
invertedLogic = _inverse_logic;
100+
isSOCGpio = _isSOCGpio;
98101
if(invertedLogic)
99102
{
100-
attachInterrupt(rxPin, recv, HIGH);
103+
attachInterrupt(_rxPin, recv, HIGH);
101104
}
102105
else
103106
{
104-
attachInterrupt(rxPin, recv, LOW);
107+
attachInterrupt(_rxPin, recv, LOW);
105108
}
106109

107110
return true;
@@ -116,7 +119,7 @@ bool SoftwareSerial::stopListening()
116119
if (active_object == this)
117120
{
118121
active_object = NULL;
119-
detachInterrupt(rxPin);
122+
detachInterrupt(_rxPin);
120123
return true;
121124
}
122125
return false;
@@ -131,7 +134,7 @@ void SoftwareSerial::recv()
131134
uint8_t d = 0;
132135
// If RX line is high, then we don't see any start bit
133136
// so interrupt is probably not for us
134-
if (invertedLogic ? digitalRead(rxPin) : !digitalRead(rxPin))
137+
if (invertedLogic ? digitalRead(_rxPin) : !digitalRead(_rxPin))
135138
{
136139
// The very first start bit the sketch receives takes about 5us longer
137140
if(firstStartBit && !isSOCGpio)
@@ -162,7 +165,7 @@ void SoftwareSerial::recv()
162165
delayTicks(rxIntraBitDelay);
163166
}
164167
d >>= 1;
165-
if (digitalRead(rxPin))
168+
if (digitalRead(_rxPin))
166169
d |= 0x80;
167170
firstStartBit = false;
168171
}
@@ -187,15 +190,15 @@ void SoftwareSerial::recv()
187190
uint8_t loopTimeout = 8;
188191
if(invertedLogic)
189192
{
190-
while(digitalRead(rxPin) && (loopTimeout >0))
193+
while(digitalRead(_rxPin) && (loopTimeout >0))
191194
{
192195
delayTicks(bitDelay >> 4);
193196
loopTimeout--;
194197
}
195198
}
196199
else
197200
{
198-
while(!digitalRead(rxPin) && (loopTimeout >0))
201+
while(!digitalRead(_rxPin) && (loopTimeout >0))
199202
{
200203
delayTicks(bitDelay >> 4);
201204
loopTimeout--;
@@ -208,21 +211,9 @@ void SoftwareSerial::recv()
208211

209212
uint32_t SoftwareSerial::rx_pin_read()
210213
{
211-
return digitalRead(rxPin);
214+
return digitalRead(_rxPin);
212215
}
213216

214-
//
215-
// Interrupt handling
216-
//
217-
218-
/* static */
219-
inline void SoftwareSerial::handle_interrupt()
220-
{
221-
if (active_object)
222-
{
223-
active_object->recv();
224-
}
225-
}
226217

227218
//
228219
// Constructor
@@ -235,7 +226,7 @@ SoftwareSerial::SoftwareSerial(uint32_t receivePin, uint32_t transmitPin, bool i
235226
_buffer_overflow(false),
236227
_inverse_logic(inverse_logic)
237228
{
238-
invertedLogic = inverse_logic;
229+
_inverse_logic = inverse_logic;
239230
setTX(transmitPin);
240231
_transmitPin = transmitPin;
241232
setRX(receivePin);
@@ -283,37 +274,37 @@ void SoftwareSerial::begin(long speed)
283274
{
284275
_rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0;
285276
//pre-calculate delays
286-
bitDelay = (F_CPU/speed);
287-
PinDescription *p = &g_APinDescription[rxPin];
277+
_bit_delay = (F_CPU/speed);
278+
PinDescription *p = &g_APinDescription[_rxPin];
288279
if (p->ulGPIOType == SOC_GPIO)
289280
{
290-
isSOCGpio = true;
281+
_isSOCGpio = true;
291282
}
292283
//toggling a pin takes about 62 ticks
293-
_tx_delay = bitDelay - 62;
284+
_tx_delay = _bit_delay - 62;
294285
//reading a pin takes about 70 ticks
295-
rxIntraBitDelay = bitDelay - 70;
286+
_rx_delay_intrabit = _bit_delay - 70;
296287
//it takes about 272 ticks from when the start bit is received to when the ISR is called
297-
rxCenteringDelay = (bitDelay / 2 - 272 - 55);
298-
if(rxCenteringDelay < 0)
288+
_rx_delay_centering = (_bit_delay / 2 - 272 - 55);
289+
if(_rx_delay_centering < 0)
299290
{
300-
firstIntraBitDelay = rxIntraBitDelay + rxCenteringDelay;
301-
if(firstIntraBitDelay < 0)
302-
firstIntraBitDelay = 0;
303-
rxCenteringDelay = 0;
291+
_rx_delay_first_intrabit = _rx_delay_intrabit + _rx_delay_centering;
292+
if(_rx_delay_first_intrabit < 0)
293+
_rx_delay_first_intrabit = 0;
294+
_rx_delay_centering = 0;
304295
}
305296
else
306297
{
307-
firstIntraBitDelay = rxIntraBitDelay;
298+
_rx_delay_first_intrabit = _rx_delay_intrabit;
308299
}
309300
//the first time the ISR is called is about 150 ticks longer
310-
initRxCenteringDelay = rxCenteringDelay - 150;
311-
if(initRxCenteringDelay < 0)
301+
_rx_delay_init_centering = _rx_delay_centering - 150;
302+
if(_rx_delay_init_centering < 0)
312303
{
313-
initRxIntraBitDelay = rxIntraBitDelay + initRxCenteringDelay;
314-
if(initRxIntraBitDelay < 0)
315-
initRxIntraBitDelay = 0;
316-
initRxCenteringDelay = 0;
304+
_rx_delay_init_intrabit = _rx_delay_intrabit + _rx_delay_init_centering;
305+
if(_rx_delay_init_intrabit < 0)
306+
_rx_delay_init_intrabit = 0;
307+
_rx_delay_init_centering = 0;
317308
}
318309

319310
#if _DEBUG

libraries/CurieSoftwareSerial/SoftwareSerial.h renamed to libraries/CurieSoftwareSerial/src/SoftwareSerial.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ class SoftwareSerial : public Stream
4040
// per object data
4141
uint32_t _receivePin;
4242
uint32_t _transmitPin;
43-
uint32_t _receiveBitMask;
44-
volatile uint32_t *_receivePortRegister;
45-
uint32_t _transmitBitMask;
46-
volatile uint32_t *_pcint_maskreg;
47-
uint32_t _pcint_maskvalue;
43+
uint32_t _isSOCGpio;
4844

4945
// delays
50-
uint32_t _rx_delay_centering;
51-
uint32_t _rx_delay_intrabit;
52-
uint32_t _rx_delay_stopbit;
53-
uint32_t _tx_delay;
46+
int _bit_delay;
47+
int _rx_delay_centering;
48+
int _rx_delay_init_centering;
49+
int _rx_delay_init_intrabit;
50+
int _rx_delay_first_intrabit;
51+
int _rx_delay_intrabit;
52+
int _rx_delay_stopbit;
53+
int _tx_delay;
5454

5555
uint32_t _buffer_overflow:1;
56-
uint32_t _inverse_logic:1;
56+
bool _inverse_logic = false;
5757

5858
// static data
5959
static char _receive_buffer[_SS_MAX_RX_BUFF];
@@ -94,8 +94,6 @@ class SoftwareSerial : public Stream
9494

9595
using Print::write;
9696

97-
// public only for easy access by interrupt handlers
98-
static inline void handle_interrupt() __attribute__((__always_inline__));
9997
};
10098

10199
// Arduino 0012 workaround

0 commit comments

Comments
 (0)