Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions SoftwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,9 @@ inline void DebugPulse(uint8_t pin, uint8_t count)
*pport = val | digitalPinToBitMask(pin);
*pport = val;
}
#else
(void) pin;
(void) count;
#endif
}

Expand Down Expand Up @@ -478,11 +481,11 @@ void SoftwareSerial::recv()
d = ~d;

// if buffer full, set the overflow flag and return
if ((_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF != _receive_buffer_head)
if ( ( (_receive_buffer_tail + 1) & _SS_MAX_RX_BUFF_MASK ) != _receive_buffer_head)
{
// save new data in buffer: tail points to where byte goes
_receive_buffer[_receive_buffer_tail] = d; // save new byte
_receive_buffer_tail = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF;
_receive_buffer_tail = (_receive_buffer_tail + 1) & _SS_MAX_RX_BUFF_MASK;
}
else
{
Expand Down Expand Up @@ -665,7 +668,7 @@ int SoftwareSerial::read()

// Read from "head"
uint8_t d = _receive_buffer[_receive_buffer_head]; // grab next byte
_receive_buffer_head = (_receive_buffer_head + 1) % _SS_MAX_RX_BUFF;
_receive_buffer_head = (_receive_buffer_head + 1) & _SS_MAX_RX_BUFF_MASK;
return d;
}

Expand All @@ -674,7 +677,7 @@ int SoftwareSerial::available()
if (!isListening())
return 0;

return (_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head) % _SS_MAX_RX_BUFF;
return (_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head) & _SS_MAX_RX_BUFF_MASK;
}

size_t SoftwareSerial::write(uint8_t b)
Expand Down
15 changes: 14 additions & 1 deletion SoftwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,24 @@ The latest version of this library can always be found at
* Definitions
******************************************************************************/

#define _SS_MAX_RX_BUFF 64 // RX buffer size
#ifndef GCC_VERSION
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#endif

const unsigned int _SS_MAX_RX_BUFF = 64u; // RX buffer size
const unsigned int _SS_MAX_RX_BUFF_MASK = _SS_MAX_RX_BUFF - 1u; // mask for buffer size

/******************************************************************************
* Compile time check that _SS_MAX_RX_BUFF is a power of 2
* https://stackoverflow.com/questions/3385515/static-assert-in-cj
******************************************************************************/

#define STATIC_ASSERT_IS_POWER_OF_2(_v_, _MSG_) \
typedef char static_assertion_##_MSG_[ \
( (_v_) && ( ( (_v_) & ( (_v_) - 1) ) == 0 ) ) ? 1 : -1 ]

STATIC_ASSERT_IS_POWER_OF_2(_SS_MAX_RX_BUFF, Max_RX_buffer_should_be_a_power_of_2);

#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__IMXRT1052__) || defined(__IMXRT1062__)

class SoftwareSerial : public Stream
Expand Down