Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not enable cts when setting alternate rx xbar pin #754

Merged
merged 1 commit into from
Sep 3, 2024

Conversation

sndsgd
Copy link
Contributor

@sndsgd sndsgd commented Sep 1, 2024

SerialHardware.setRx() currently enables CTS if the provided pin is an xbar pin. AFAICT this prevents the pin from being used for rx, and also results in the tx buffer filling up (assuming you are sending data) and causes the program to halt.

Testing

You can use this sketch to confirm that tx buffer fills up after using setRX() with an xbar pin:

#define SERIAL_INTERFACE Serial1
#define SERIAL_BAUD_RATE 1000000

void setup() {
  while (!Serial) {}
  Serial.begin(9600);

  SERIAL_INTERFACE.begin(SERIAL_BAUD_RATE);
  SERIAL_INTERFACE.setRX(3);
}

uint8_t value = 0;
void loop() {
  Serial.printf("tx %d (availableForWrite=%d)\n", value, SERIAL_INTERFACE.availableForWrite());
  SERIAL_INTERFACE.write(value++);
}
tx 0 (availableForWrite=63)
tx 1 (availableForWrite=63)
tx 2 (availableForWrite=63)
tx 3 (availableForWrite=63)
tx 4 (availableForWrite=62)
tx 5 (availableForWrite=61)
tx 6 (availableForWrite=60)
tx 7 (availableForWrite=59)
tx 8 (availableForWrite=58)
tx 9 (availableForWrite=57)

... trend continues ...

tx 62 (availableForWrite=4)
tx 63 (availableForWrite=3)
tx 64 (availableForWrite=2)
tx 65 (availableForWrite=1)
tx 66 (availableForWrite=0)

...program halts...

To confirm this patch works you can setup two teensy4.x wired to use xbar pins for rx, and then make sure they can communicate back and forth.

Here is the setup I used to test:
PXL_20240901_171556740 (1)
I'm running the same sketch on both devices, and using pin 41 to set the mode each device will run as.

Here is my sketch:

#include <Arduino.h>

#define SERIAL_INTERFACE Serial1
#define SERIAL_BAUD_RATE 1000000

const uint8_t ledPin = 13;
const uint8_t modePin = 41;
const uint8_t rxPin = 4;
bool isMaster = false;

void blink(int on = 100, int off = 100) {
  digitalWrite(ledPin, HIGH);
  delay(on);
  digitalWrite(ledPin, LOW);
  delay(off);
}

void setup() {
  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);
  pinMode(modePin, INPUT);

  delay(100);
  isMaster = digitalRead(modePin);

  SERIAL_INTERFACE.begin(SERIAL_BAUD_RATE);
  SERIAL_INTERFACE.setRX(rxPin);

  if (isMaster) {
    blink(500, 100);
  } else {
    blink(100, 500);
  }
}

void loop() {
  if (isMaster) {
    uint8_t value = random(255) + 1;
    Serial.printf("tx %d (availableForWrite=%d)", value, SERIAL_INTERFACE.availableForWrite());
    SERIAL_INTERFACE.write(value);
    while (!SERIAL_INTERFACE.available()) {
      delay(1);
    }

    uint8_t response = SERIAL_INTERFACE.read();
    if (response != value) {
      Serial.printf(" FAILED %d (expected %d)\n", response, value);
      blink(500, 100);
      SERIAL_INTERFACE.clear();
    } else {
      Serial.printf(" OK\n", response, value);
    }

    delay(10);
  } else {
    int length = SERIAL_INTERFACE.available();
    for (int i = 0; i < length; i++) {
      uint8_t value = SERIAL_INTERFACE.read();
      Serial.printf("rx %d\n", value);
      SERIAL_INTERFACE.write(value);
      if (value == 0) {
        blink();
      }
    }
  }
}

Against this branch you see the expected output in the serial console for the master device:

tx 61 (availableForWrite=63) OK
tx 22 (availableForWrite=63) OK
tx 141 (availableForWrite=63) OK
tx 128 (availableForWrite=63) OK
tx 182 (availableForWrite=63) OK
tx 232 (availableForWrite=63) OK
tx 101 (availableForWrite=63) OK

... random number round trips continue ...

Against the master branch, you'll see the first tx message and nothing else.

@PaulStoffregen PaulStoffregen merged commit 59b8008 into PaulStoffregen:master Sep 3, 2024
@sndsgd sndsgd deleted the serial-rx-xbar branch September 4, 2024 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants