Description
Hardware:
Board: Adafruit Huzzah32 Feather
Core Installation/update date: 19-Feb-2018
IDE name: Arduino 1.8.5
Flash Frequency: 80MHz
Upload Speed: 921600
Description:
I have been fortunate thus far that everything I've tried has functioned flawlessly, so thank you very much for that.
On the Adafruit module, I am using the UART (pins 16/17) on Serial1 via:
HardwareSerial Serial1(2);
I am initializing it to 9600bps, and all is good. It works well.
However, I actually have a GPIO-driven 4-port UART multiplexer connected to those RX/TX pins. I have used this mux design for years, and the general algorithm when switching is:
- Serial1.end() to deinit the driver
- digitalWrite to two GPIO pins to select the desired port
- Serial1.begin(newSpeed) to reinit the driver
At the current time, all devices connected to all ports just happen to be operating at 9600bps, so this switchover should be occurring in a flawless manner without any framing errors or any issues at all.
However, there appears to be a bug somewhere in the end()/begin() code path that is causing extremely bad behavior. The way that it manifests itself is that after the end/begin reinit,
a) a fragment of the tail end of the data that had been previously successfully transmitted prior to the end() is spuriously transmitted redundantly after the begin()
b) buffer pointers appear to be getting somewhat confused, because after the reinit certain sequences of bytes are transmitted out-of-order - i.e. the string:
{"req":"time"}
...is commonly transmitted as...
e"}{"req:"tim
c) received data is wrong or reordered in a way that I have not yet been able to determine.
After many hours of debugging, I finally tried simply removing the calls to Serial1.end() and Serial1.begin(), knowing that (for now) my devices are all operating at 9600bps. After doing this, I am having zero bugs or issues - which is why I'm filing this issue.
I regret that I am on my way to the airport and haven't had a chance to create a repro sketch for you, but I felt it best to file this while it is fresh in my mind rather than to wait two weeks. Perhaps whoever is familiar with the HardwareSerial driver will recognize the failure to fully clear and reinitialize the driver context on begin().
Good hunting, and thanks again.