diff options
author | Matthijs Kooijman <matthijs@stdin.nl> | 2014-04-22 22:24:43 +0200 |
---|---|---|
committer | Matthijs Kooijman <matthijs@stdin.nl> | 2015-01-26 17:03:25 +0100 |
commit | 157ec9754003497adaad50a74c16d455a68f342e (patch) | |
tree | 14306026d696f142168fa7c9409e7ba0fb5d0976 | |
parent | c69c0b52aa66714a501544175215fd9723532dc2 (diff) |
Optimize SoftwareSerial::recv
Similar to SoftwareSerial::write, this rewrites the loop to only touch
the MSB and then shift those bits up, allowing the compiler to generate
more efficient code. Unlike the write function however, it is not needed
to put all instance variables used into local variables, for some reason
the compiler already does this (and doing it manually even makes the
code bigger).
On the Arduino Uno using gcc 4.3 this saves 26 bytes. Using gcc 4.8 this
saves 30 bytes.
Note that this removes the else clause in the code, making the C code
unbalanced, which looks like it breaks timing balance. However, looking
at the code generated by the compiler, it turns out that the old code
was actually unbalanced, while the new code is properly balanced.
-rw-r--r-- | libraries/SoftwareSerial/SoftwareSerial.cpp | 8 |
1 files changed, 3 insertions, 5 deletions
diff --git a/libraries/SoftwareSerial/SoftwareSerial.cpp b/libraries/SoftwareSerial/SoftwareSerial.cpp index 621a2af..974aa1e 100644 --- a/libraries/SoftwareSerial/SoftwareSerial.cpp +++ b/libraries/SoftwareSerial/SoftwareSerial.cpp @@ -242,15 +242,13 @@ void SoftwareSerial::recv() DebugPulse(_DEBUG_PIN2, 1);
// Read each of the 8 bits
- for (uint8_t i=0x1; i; i <<= 1)
+ for (uint8_t i=8; i > 0; --i)
{
tunedDelay(_rx_delay_intrabit);
+ d >>= 1;
DebugPulse(_DEBUG_PIN2, 1);
- uint8_t noti = ~i;
if (rx_pin_read())
- d |= i;
- else // else clause added to ensure function timing is ~balanced
- d &= noti;
+ d |= 0x80;
}
// skip the stop bit
|