/*
This sketch demonstrates an apparent failure of Serial2.available().
If the #define is commented out the program prints what it reads from Serial2
as hex with a cumulative received byte count. If the #define is compiled
control never escapes from the first call of Serial2.available().
To reproduce:
1.Connect two Teensy 4.0 board grounds and pin eight of the sending Teensy to
pin seven of this receiving Teensy. (It is only necessary that this sketch
run on a 4.0: anything capable of spewing serial at 9600 can run the sender
sketch).
2 Start this sketch. It will put "W" on a new line over and over again.
3.Run availablebug-sender.ino on the other board. If the failure reproduces
the stream of "W" output will not stop. If it fails to reproduce a cumulative
input byte count and the two hex digits of the received byte value come out
on one line per byte.
If the Serial2 baud rate is jacked up it's possible some "W" lines might
come out as well as the expected hex values even after the sender starts.
Test environment used: Teensyduino 1.53/Arudino 1.8.13/Ubuntu 18.04/AMD CPU
on "receiver" side and 1.53/1.8.13/Ubuntu 16.04/Intel CPU on "sender" side.
If the Serial2 baud rate is jacked up it's possible some "W" lines might
come out as well as the expected hex values even after the sender starts.
Test environment used: Teensyduino 1.53/Arudino 1.8.13/Ubuntu 18.04/AMD CPU
on "receiver" side and 1.53/1.8.13/Ubuntu 16.04/Intel CPU on "sender" side.
Note: This is a distillation of a more complex program that was sabotaged
by some interaction between Serial and Serial2. This is just the smallest test
case to demonstrate at least one interaction that stymied progress by making
"getting debug information" and "running the target application" mutually
exclusive. It's possible there are more bugs involved, as in some cases it
was as if Serial2.available had a "bank account of about 100k to 10meg calls
after which it simply got stuck and the program stopped making progress.
pete@apexprotofactory.com 8/27/2020
Added oscilloscope markers 9/2/2020 mborgerson
*/
const int ledpin = 13;
const int wpin = 14;
#define LEDON digitalWriteFast(ledpin, HIGH);
#define LEDOFF digitalWriteFast(ledpin, LOW);
// Marker to show transmission of 'w'
#define WON digitalWriteFast(wpin, HIGH);
#define WOFF digitalWriteFast(wpin, LOW);
// Comment this out to cause program to work properly
#define FAIL
static uint32_t count = 0;
void setup() {
pinMode(ledpin, OUTPUT);
pinMode(wpin, OUTPUT);
Serial.begin(9600);
while (! Serial) {
}
Serial2.begin(9600);
while (! Serial2) {
}
}
static uint8_t getByte() {
LEDOFF
while (!Serial2.available()) {
#ifdef FAIL
WON
Serial.println("W");
WOFF
#endif
}
LEDON
// insert a delay of about 0.1 bit time so the
// pulse is easier to find on the oscilloscope
delayMicroseconds(10);
return Serial2.read();
}
void loop() {
count += 1;
Serial.print(count);
Serial.print(" ");
Serial.println(getByte(), HEX);
}