Skip to content

Commit 0a6072d

Browse files
Fix SoftwareSerial timings
Instead of using a lookup table with (wrong) timings, this calculates the timings in SoftwareSerial::begin. This is probably a bit slower, but since it typically happens once, this shouldn't be a problem. Additionally, since the lookup tables can be removed, this is also a lot smaller, as well as supporting arbitrary CPU speeds and baudrates, instead of the limited set that was defined before. Furthermore, this switches to use the _delay_loop_2 function from avr-libc instead of a handcoded delay function. The avr-libc function only takes two instructions, as opposed to four instructions for the old one. The compiler also inlines the avr-libc function, which makes the timings more reliable. The calculated timings directly rely on the instructions generated by the compiler, since a significant amount of time is spent processing (compared to the delays, especially at higher speeds). This means that if the code is changed, or a different compiler is used, the calculations might need changing (though a few cycles more or less shouldn't cause immediate breakage). The timings in the code have been calculated from the assembly generated by gcc 4.8.2 and gcc 4.3.2. The RX baudrates supported by SoftwareSerial are still not unlimited. At 16Mhz, using gcc 4.8.2, everything up to 115200 works. At 8Mhz, it works up to 57600. Using gcc 4.3.2, it also works up to 57600 at 16Mhz and up to 38400 at 8Mhz. Note that at these highest speeds, communication works, but is still quite sensitive to other interrupts (like the millis() interrupts) when bytes are sent back-to-back, so there still are corrupted bytes in RX. TX works up to 115200 for all combinations of compiler and clock rates. This fixes #2019
1 parent 08c3bfd commit 0a6072d

File tree

2 files changed

+69
-121
lines changed

2 files changed

+69
-121
lines changed

Diff for: libraries/SoftwareSerial/SoftwareSerial.cpp

+65-121
Original file line numberDiff line numberDiff line change
@@ -42,92 +42,7 @@ The latest version of this library can always be found at
4242
#include <avr/pgmspace.h>
4343
#include <Arduino.h>
4444
#include <SoftwareSerial.h>
45-
//
46-
// Lookup table
47-
//
48-
typedef struct _DELAY_TABLE
49-
{
50-
long baud;
51-
unsigned short rx_delay_centering;
52-
unsigned short rx_delay_intrabit;
53-
unsigned short rx_delay_stopbit;
54-
unsigned short tx_delay;
55-
} DELAY_TABLE;
56-
57-
#if F_CPU == 16000000
58-
59-
static const DELAY_TABLE PROGMEM table[] =
60-
{
61-
// baud rxcenter rxintra rxstop tx
62-
{ 115200, 1, 17, 17, 12, },
63-
{ 57600, 10, 37, 37, 33, },
64-
{ 38400, 25, 57, 57, 54, },
65-
{ 31250, 31, 70, 70, 68, },
66-
{ 28800, 34, 77, 77, 74, },
67-
{ 19200, 54, 117, 117, 114, },
68-
{ 14400, 74, 156, 156, 153, },
69-
{ 9600, 114, 236, 236, 233, },
70-
{ 4800, 233, 474, 474, 471, },
71-
{ 2400, 471, 950, 950, 947, },
72-
{ 1200, 947, 1902, 1902, 1899, },
73-
{ 600, 1902, 3804, 3804, 3800, },
74-
{ 300, 3804, 7617, 7617, 7614, },
75-
};
76-
77-
const int XMIT_START_ADJUSTMENT = 5;
78-
79-
#elif F_CPU == 8000000
80-
81-
static const DELAY_TABLE table[] PROGMEM =
82-
{
83-
// baud rxcenter rxintra rxstop tx
84-
{ 115200, 1, 5, 5, 3, },
85-
{ 57600, 1, 15, 15, 13, },
86-
{ 38400, 2, 25, 26, 23, },
87-
{ 31250, 7, 32, 33, 29, },
88-
{ 28800, 11, 35, 35, 32, },
89-
{ 19200, 20, 55, 55, 52, },
90-
{ 14400, 30, 75, 75, 72, },
91-
{ 9600, 50, 114, 114, 112, },
92-
{ 4800, 110, 233, 233, 230, },
93-
{ 2400, 229, 472, 472, 469, },
94-
{ 1200, 467, 948, 948, 945, },
95-
{ 600, 948, 1895, 1895, 1890, },
96-
{ 300, 1895, 3805, 3805, 3802, },
97-
};
98-
99-
const int XMIT_START_ADJUSTMENT = 4;
100-
101-
#elif F_CPU == 20000000
102-
103-
// 20MHz support courtesy of the good people at macegr.com.
104-
// Thanks, Garrett!
105-
106-
static const DELAY_TABLE PROGMEM table[] =
107-
{
108-
// baud rxcenter rxintra rxstop tx
109-
{ 115200, 3, 21, 21, 18, },
110-
{ 57600, 20, 43, 43, 41, },
111-
{ 38400, 37, 73, 73, 70, },
112-
{ 31250, 45, 89, 89, 88, },
113-
{ 28800, 46, 98, 98, 95, },
114-
{ 19200, 71, 148, 148, 145, },
115-
{ 14400, 96, 197, 197, 194, },
116-
{ 9600, 146, 297, 297, 294, },
117-
{ 4800, 296, 595, 595, 592, },
118-
{ 2400, 592, 1189, 1189, 1186, },
119-
{ 1200, 1187, 2379, 2379, 2376, },
120-
{ 600, 2379, 4759, 4759, 4755, },
121-
{ 300, 4759, 9523, 9523, 9520, },
122-
};
123-
124-
const int XMIT_START_ADJUSTMENT = 6;
125-
126-
#else
127-
128-
#error This version of SoftwareSerial supports only 20, 16 and 8MHz processors
129-
130-
#endif
45+
#include <util/delay_basic.h>
13146

13247
//
13348
// Statics
@@ -162,16 +77,7 @@ inline void DebugPulse(uint8_t pin, uint8_t count)
16277

16378
/* static */
16479
inline void SoftwareSerial::tunedDelay(uint16_t delay) {
165-
uint8_t tmp=0;
166-
167-
asm volatile("sbiw %0, 0x01 \n\t"
168-
"ldi %1, 0xFF \n\t"
169-
"cpi %A0, 0xFF \n\t"
170-
"cpc %B0, %1 \n\t"
171-
"brne .-10 \n\t"
172-
: "+r" (delay), "+a" (tmp)
173-
: "0" (delay)
174-
);
80+
_delay_loop_2(delay);
17581
}
17682

17783
// This function sets the current object as the "listening"
@@ -256,12 +162,6 @@ void SoftwareSerial::recv()
256162
d |= 0x80;
257163
}
258164

259-
// skip the stop bit
260-
tunedDelay(_rx_delay_stopbit);
261-
DebugPulse(_DEBUG_PIN2, 1);
262-
263-
// Re-enable interrupts when we're sure to be inside the stop bit
264-
setRxIntMsk(true);
265165
if (_inverse_logic)
266166
d = ~d;
267167

@@ -280,6 +180,14 @@ void SoftwareSerial::recv()
280180
#endif
281181
_buffer_overflow = true;
282182
}
183+
184+
// skip the stop bit
185+
tunedDelay(_rx_delay_stopbit);
186+
DebugPulse(_DEBUG_PIN1, 1);
187+
188+
// Re-enable interrupts when we're sure to be inside the stop bit
189+
setRxIntMsk(true);
190+
283191
}
284192

285193
#if GCC_VERSION < 40302
@@ -378,6 +286,13 @@ void SoftwareSerial::setRX(uint8_t rx)
378286
_receivePortRegister = portInputRegister(port);
379287
}
380288

289+
uint16_t SoftwareSerial::subtract_cap(uint16_t num, uint16_t sub) {
290+
if (num > sub)
291+
return num - sub;
292+
else
293+
return 1;
294+
}
295+
381296
//
382297
// Public methods
383298
//
@@ -386,26 +301,55 @@ void SoftwareSerial::begin(long speed)
386301
{
387302
_rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0;
388303

389-
for (unsigned i=0; i<sizeof(table)/sizeof(table[0]); ++i)
390-
{
391-
long baud = pgm_read_dword(&table[i].baud);
392-
if (baud == speed)
393-
{
394-
if (digitalPinToPCICR(_receivePin))
395-
{
396-
// Only setup rx when we have a valid PCINT for this pin
397-
_rx_delay_centering = pgm_read_word(&table[i].rx_delay_centering);
398-
_rx_delay_intrabit = pgm_read_word(&table[i].rx_delay_intrabit);
399-
_rx_delay_stopbit = pgm_read_word(&table[i].rx_delay_stopbit);
400-
}
401-
_tx_delay = pgm_read_word(&table[i].tx_delay);
402-
break;
403-
}
404-
}
304+
// Precalculate the various delays, in number of 4-cycle delays
305+
uint16_t bit_delay = (F_CPU / speed) / 4;
306+
307+
// 12 (gcc 4.8.2) or 13 (gcc 4.3.2) cycles from start bit to first bit,
308+
// 15 (gcc 4.8.2) or 16 (gcc 4.3.2) cycles between bits,
309+
// 12 (gcc 4.8.2) or 14 (gcc 4.3.2) cycles from last bit to stop bit
310+
// These are all close enough to just use 15 cycles, since the inter-bit
311+
// timings are the most critical (deviations stack 8 times)
312+
_tx_delay = subtract_cap(bit_delay, 15 / 4);
313+
314+
// Only setup rx when we have a valid PCINT for this pin
315+
if (digitalPinToPCICR(_receivePin)) {
316+
#if GCC_VERSION > 40800
317+
// Timings counted from gcc 4.8.2 output. This works up to 115200 on
318+
// 16Mhz and 57600 on 8Mhz.
319+
//
320+
// When the start bit occurs, there are 3 or 4 cycles before the
321+
// interrupt flag is set, 4 cycles before the PC is set to the right
322+
// interrupt vector address and the old PC is pushed on the stack,
323+
// and then 75 cycles of instructions (including the RJMP in the
324+
// ISR vector table) until the first delay. After the delay, there
325+
// are 17 more cycles until the pin value is read (excluding the
326+
// delay in the loop).
327+
// We want to have a total delay of 1.5 bit time. Inside the loop,
328+
// we already wait for 1 bit time - 23 cycles, so here we wait for
329+
// 0.5 bit time - (71 + 18 - 22) cycles.
330+
_rx_delay_centering = subtract_cap(bit_delay / 2, (4 + 4 + 75 + 17 - 23) / 4);
331+
332+
// There are 23 cycles in each loop iteration (excluding the delay)
333+
_rx_delay_intrabit = subtract_cap(bit_delay, 23 / 4);
334+
335+
// There are 37 cycles from the last bit read to the start of
336+
// stopbit delay and 11 cycles from the delay until the interrupt
337+
// mask is enabled again (which _must_ happen during the stopbit).
338+
// This delay aims at 3/4 of a bit time, meaning the end of the
339+
// delay will be at 1/4th of the stopbit. This allows some extra
340+
// time for ISR cleanup, which makes 115200 baud at 16Mhz work more
341+
// reliably
342+
_rx_delay_stopbit = subtract_cap(bit_delay * 3 / 4, (37 + 11) / 4);
343+
#else // Timings counted from gcc 4.3.2 output
344+
// Note that this code is a _lot_ slower, mostly due to bad register
345+
// allocation choices of gcc. This works up to 57600 on 16Mhz and
346+
// 38400 on 8Mhz.
347+
_rx_delay_centering = subtract_cap(bit_delay / 2, (4 + 4 + 97 + 29 - 11) / 4);
348+
_rx_delay_intrabit = subtract_cap(bit_delay, 11 / 4);
349+
_rx_delay_stopbit = subtract_cap(bit_delay * 3 / 4, (44 + 17) / 4);
350+
#endif
351+
405352

406-
// Set up RX interrupts, but only if we have a valid RX baud rate
407-
if (_rx_delay_stopbit)
408-
{
409353
// Enable the PCINT for the entire port here, but never disable it
410354
// (others might also need it, so we disable the interrupt by using
411355
// the per-pin PCMSK register).

Diff for: libraries/SoftwareSerial/SoftwareSerial.h

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class SoftwareSerial : public Stream
5656
volatile uint8_t *_pcint_maskreg;
5757
uint8_t _pcint_maskvalue;
5858

59+
// Expressed as 4-cycle delays (must never be 0!)
5960
uint16_t _rx_delay_centering;
6061
uint16_t _rx_delay_intrabit;
6162
uint16_t _rx_delay_stopbit;
@@ -78,6 +79,9 @@ class SoftwareSerial : public Stream
7879
void setRX(uint8_t receivePin);
7980
void setRxIntMsk(bool enable) __attribute__((__always_inline__));
8081

82+
// Return num - sub, or 1 if the result would be < 1
83+
static uint16_t subtract_cap(uint16_t num, uint16_t sub);
84+
8185
// private static method for timing
8286
static inline void tunedDelay(uint16_t delay);
8387

0 commit comments

Comments
 (0)