SpenceKonde / megaTinyCore

Arduino core for the tinyAVR 0/1/2-series - Ones's digit 2,4,5,7 (pincount, 8,14,20,24), tens digit 0, 1, or 2 (featureset), preceded by flash in kb. Library maintainers: porting help available!
Other
551 stars 143 forks source link

Half duplex is totally fucked! #848

Closed SpenceKonde closed 1 year ago

SpenceKonde commented 1 year ago

1.6.2 is ready for release as soon as I can get a fix to the asm rx implementations working,

Two tinyAVR 2 series in half duplex mode each running this identical sketch:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial1.begin(115200, (SERIAL_HALF_DUPLEX | SERIAL_RS485));
  //pinMode(PIN_PA4, OUTPUT);
  //digitalWriteFast(PIN_PA4, LOW);
  delay(1000);
  Serial.println("Serial ports started, Serial1 half duplex RS485");
  Serial.println(random(500, 2500));
  delay(random(500, 2500));
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial1.available()) {
    delay(100);
    while (Serial1.available()) {
      char c = Serial1.read();
      Serial.write(c);
    }
  } else if (Serial.available()) {
    delay(100);
    while (Serial.available()) {
      Serial1.write(Serial.read());
    }
  }
}

Only connections between them are ground and PA1.

When either of them sends a message to the other, the destination device receives it...

But the SENDER also receives the second and last characters.

What the hell am I doing wrong when I clear self-sent data out of the RX buffer? Any suggestions on how to deal with this

Discussed in https://github.com/SpenceKonde/megaTinyCore/discussions/847

Originally posted by **coburnw** October 14, 2022 I have been toying with the 1.4.10 tag of DxCore with some pretty good success. Thank you! In the process of bringing up the RS485 link i found that the half duplex portion of the C serial ISR didn't make it into the assembly language ISR. These are the changes i found were needed to re-enable the full featured ISR and configure the serial port and _state variable for open drain mode. These changes are virtually untested so both standard and non-standard disclaimers apply... * checkout the 1.4.10 tag * create a file ``platform.local.txt`` in the same folder as platform.txt * add ``compiler.cpp.extra_flags=-DUSE_ASM_TXC=0 -DUSE_ASM_RXC=0`` to platform.local.txt * add the following stanza to UART.cpp just before [`` ctrla &= 0x2B;``](https://github.com/SpenceKonde/DxCore/blob/b0bb30f8cb703991d3fe97a3241091a221eab09b/megaavr/cores/dxcore/UART.cpp#L384) or line 384 ``` #if USE_ASM_TXC != 1 // OpenDrain mode only supported by C ISR if (ctrla & 0x04){ // see if user wants OpenDrain ctrlb |= USART_ODME_bm; } #endif ```
SpenceKonde commented 1 year ago

All my effort thus far has brought it no closer and arguably farther from working,

SpenceKonde commented 1 year ago

okaym we're getting closer sort of. I got receiving working, but the moment I try to transmit, the whole thing blows up and nothing every is sent.

SpenceKonde commented 1 year ago

This has been resolved, it was... an unnecessary cli() without reenabling interrupts. That was anticlimactic.