From 9c1ce9e1c5fb4fd640386a694c59a110af3e254e Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 9 Jun 2012 07:43:06 +1000 Subject: [PATCH] FastSerial: avoid buffer re-allocation on re-open if possible we commonly re-open serial ports a lot in the AUTO GPS driver --- libraries/FastSerial/FastSerial.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/libraries/FastSerial/FastSerial.cpp b/libraries/FastSerial/FastSerial.cpp index 67f71483c6..413c9041ab 100644 --- a/libraries/FastSerial/FastSerial.cpp +++ b/libraries/FastSerial/FastSerial.cpp @@ -82,6 +82,7 @@ void FastSerial::begin(long baud, unsigned int rxSpace, unsigned int txSpace) { uint16_t ubrr; bool use_u2x = true; + bool need_allocate = true; // if we are currently open... if (_open) { @@ -92,14 +93,23 @@ void FastSerial::begin(long baud, unsigned int rxSpace, unsigned int txSpace) if (0 == txSpace) txSpace = _txBuffer->mask + 1; - // close the port in its current configuration, clears _open - end(); + if (rxSpace == (_rxBuffer->mask + 1) && + txSpace == (_txBuffer->mask + 1)) { + // avoid re-allocating the buffers if possible + need_allocate = false; + *_ucsrb &= ~(_portEnableBits | _portTxBits); + } else { + // close the port in its current configuration, clears _open + end(); + } } - // allocate buffers - if (!_allocBuffer(_rxBuffer, rxSpace ? : _default_rx_buffer_size) || !_allocBuffer(_txBuffer, txSpace ? : _default_tx_buffer_size)) { - end(); - return; // couldn't allocate buffers - fatal + if (need_allocate) { + // allocate buffers + if (!_allocBuffer(_rxBuffer, rxSpace ? : _default_rx_buffer_size) || !_allocBuffer(_txBuffer, txSpace ? : _default_tx_buffer_size)) { + end(); + return; // couldn't allocate buffers - fatal + } } // reset buffer pointers