diff options
author | Matthijs Kooijman <matthijs@stdin.nl> | 2014-04-23 19:23:58 +0200 |
---|---|---|
committer | Matthijs Kooijman <matthijs@stdin.nl> | 2015-01-26 17:03:25 +0100 |
commit | 0a6072dccb5e8c278620ce8ae23fdf420dc807f1 (patch) | |
tree | ce6c8e05905749de2e94d812a40c13b69c8bc6ae /libraries/SoftwareSerial/SoftwareSerial.h | |
parent | 08c3bfdc9fd5da81932ec9e4df0b645c93f2ebdc (diff) |
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
Diffstat (limited to 'libraries/SoftwareSerial/SoftwareSerial.h')
-rw-r--r-- | libraries/SoftwareSerial/SoftwareSerial.h | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/libraries/SoftwareSerial/SoftwareSerial.h b/libraries/SoftwareSerial/SoftwareSerial.h index 2b12598..e28da98 100644 --- a/libraries/SoftwareSerial/SoftwareSerial.h +++ b/libraries/SoftwareSerial/SoftwareSerial.h @@ -56,6 +56,7 @@ private: volatile uint8_t *_pcint_maskreg;
uint8_t _pcint_maskvalue;
+ // Expressed as 4-cycle delays (must never be 0!)
uint16_t _rx_delay_centering;
uint16_t _rx_delay_intrabit;
uint16_t _rx_delay_stopbit;
@@ -78,6 +79,9 @@ private: void setRX(uint8_t receivePin);
void setRxIntMsk(bool enable) __attribute__((__always_inline__));
+ // Return num - sub, or 1 if the result would be < 1
+ static uint16_t subtract_cap(uint16_t num, uint16_t sub);
+
// private static method for timing
static inline void tunedDelay(uint16_t delay);
|