Davidelvig
Well-known member
Now that I have your attention...
I'm new to inter-board UART communication.
I'm testing 2 Teensy 3.2's using Serial2 on each, and the attached code.
Each works flawlessly, delivering 11.5Kbytes/seconds bi-directionally when hooked up as a loop-back (Rx2-Tx2 on the same board).
When connected to each other (cross-connected), it works sometimes flawlessly (similar speed), and other times one side detects erroneous bytes from the other.
(line 50 of the attached code detects a continuous stream of bytes >= 0x80, and all sent bytes should be < 0x80.)
I'm using HardwareSerial (Serial2) in both cases, using only Rx (9) and Tx (10) pins, plus a common ground (GND).
Both Teensy's are connected to their own MacBook via USB and share the very same codebase (shared on OneDrive)
I have not implemented RTC and CTS lines.
I must be missing a UART hand shaking phase. I think the second Teensy to start seems to be the one that fails in receipt of good bytes.
Perhaps byte receipt for the 2nd-to-start Teensy starts half-way through an inbound byte?
1) Is this an obvious and predictable problem with this setup?
2) Is there a good resource for learning about inter-board comms using UART?
Thanks
- Dave
p.s. My end goal is to communicate between my Teensy and either an nRF52 or ESP32 board for BLE comms. After I figure this out between 2 Teensy's
I'm new to inter-board UART communication.
I'm testing 2 Teensy 3.2's using Serial2 on each, and the attached code.
Each works flawlessly, delivering 11.5Kbytes/seconds bi-directionally when hooked up as a loop-back (Rx2-Tx2 on the same board).
When connected to each other (cross-connected), it works sometimes flawlessly (similar speed), and other times one side detects erroneous bytes from the other.
(line 50 of the attached code detects a continuous stream of bytes >= 0x80, and all sent bytes should be < 0x80.)
I'm using HardwareSerial (Serial2) in both cases, using only Rx (9) and Tx (10) pins, plus a common ground (GND).
Both Teensy's are connected to their own MacBook via USB and share the very same codebase (shared on OneDrive)
I have not implemented RTC and CTS lines.
I must be missing a UART hand shaking phase. I think the second Teensy to start seems to be the one that fails in receipt of good bytes.
Perhaps byte receipt for the 2nd-to-start Teensy starts half-way through an inbound byte?
1) Is this an obvious and predictable problem with this setup?
2) Is there a good resource for learning about inter-board comms using UART?
Thanks
- Dave
p.s. My end goal is to communicate between my Teensy and either an nRF52 or ESP32 board for BLE comms. After I figure this out between 2 Teensy's
Code:
#include <Arduino.h>
#include <TeensyID.h>
bool useCapitals = false;
void getDeviceID_Byte(void) {
// The following has a good chance of getting a device-unique ID byte - using a long MCU chip ID on Teensy
uint8_t deviceID_Byte;
uint32_t uid[4];
kinetisUID(uid);
int32_t seedValue = uid[0] | uid[1] | uid[2] | uid[3];
randomSeed(seedValue);
deviceID_Byte = random(0, 127);
Serial.printf("deviceID_Byte = %d\n", deviceID_Byte);
if (deviceID_Byte == 61) useCapitals = true; // determine which of 2 participating Teensy's should use capital letters. Your number will vary
}
void setup() {
Serial.begin(9600);
while (!Serial && millis() < 1000) {;}
delay(500);
Serial.printf("Serial start-up at %dms\n", millis());
Serial2.begin(115200);
delay(500); Serial.printf("Serial2 start-up at %dms\n", millis());
while (Serial2.available() > 0) Serial2.read(); //empty the inbound buffer
getDeviceID_Byte();
}
unsigned long delayCount = 0;
unsigned long lastReportMS = 0;
unsigned long goodRecBytes = 0;
unsigned long badRecBytes = 0;
unsigned long lastRecBytes = 0;
unsigned long sendBytes = 0;
unsigned long lastSendBytes = 0;
byte baseByte = 0;
byte lastByte = 0;
void loop() {
unsigned long millisNow = millis();
int inboundAvail = Serial2.available();
int outboundAvail = Serial2.availableForWrite();
// receive
if (inboundAvail > 0) {
byte inByte = Serial2.read();
if (inByte >= 0x80) {
inboundAvail = Serial2.available();
while ((inByte >= 0x80) && (inboundAvail > 1)) {
Serial.printf("%lu\tinByte too high: %d\tbytes available: %d\n", micros(), inByte, inboundAvail);
inByte = Serial2.read();
inboundAvail = Serial2.available();
}
Serial.println("_________________________");
}
if ((inByte != lastByte + 1) && (inByte != (lastByte - 25)) && (lastByte != 0)) {
badRecBytes++;
Serial.printf("%d\t%d\n", lastByte, inByte);
} else {
goodRecBytes++;
}
lastByte = inByte;
}
// send
if (outboundAvail > 10) {
byte outByte = (useCapitals) ? 'A' : 'a';
outByte += baseByte;
Serial2.write(outByte);
sendBytes++;
baseByte++; if (baseByte > 25) baseByte = 0;
} else {
Serial.printf("outboundAvail: %d\n", outboundAvail);
// delay(10);
delayCount++;
}
// print stats
if (millisNow > (lastReportMS + 1000)) {
Serial.print(useCapitals ? "CAPS\t" : "small\t");
Serial.printf("%10dms\tgoodIn: %lu\tbadIn: %lu\tsent: %lu\tdelayK: %lu\tin rate: %lu bytes/sec\tout rate: %lu bytes/sec\n",
millisNow, goodRecBytes, badRecBytes, sendBytes, delayCount/1000,
((badRecBytes + goodRecBytes) - lastRecBytes), (sendBytes - lastSendBytes));
lastReportMS = millisNow;
lastSendBytes = sendBytes;
lastRecBytes = badRecBytes + goodRecBytes;
}
}