Software way of inverting signal on Arduino Due serial port (USARTClass)

AdderD:
In inverted mode only the data bits themselves are inverted. The start and stop bits aren't

OK

The hardware workaround is probably the easiest way to go.

I searched for a 100% software solution, and this one inverts ALL bits, stepwise:

1/ In Menu>File>Preferences, at the bottom of this window, click in the url:

c:\Users...\AppData\Local\Arduino15\preferences.txt

2/ Follow packages/arduino/hardware/sam/(your arduino DUE version)/cores/arduino/winterrupts.c

3/ In a blanck IDE window (no setup(), no loop()), copy winterrupts.c

4/ Modify void PIOD_Handler() in winterrupts.c

//  ************  BEFORE  ***************
void PIOD_Handler(void) {

  uint32_t isr = PIOD->PIO_ISR;
  uint32_t i;
  for (i = 0; i < 32; i++, isr >>= 1) {
    if ((isr & 0x1) == 0)
      continue;
    if (callbacksPioD[i])
      callbacksPioD[i]();
  }
}

// *************   AFTER   ***************
void (*piod_isr)(void) = (0UL);
void PIOD_Handler(void) {
  if (piod_isr) {
    piod_isr();
  }
  else {
    uint32_t isr = PIOD->PIO_ISR;
    uint32_t i;
    for (i = 0; i < 32; i++, isr >>= 1) {
      if ((isr & 0x1) == 0)
        continue;
      if (callbacksPioD[i])
        callbacksPioD[i]();
    }
  }
}

5/ Copy the "new" winterrupts.c code in winterrupts.c, and close this file

6/ Write a sketch to invert all bits received and transmitted by Serial1:

/*********************************************************************/
/*                   Shunt winterrupts.c for PIOD                    */
/*********************************************************************/

extern "C" void (*piod_isr)(void);
void setup() {
  Serial1.begin(115200);

  pinMode(25, INPUT); // Receives external TX instead of RX1
  // Jumper between pin 26 and RX1
  pinMode(26, OUTPUT); // Output inverted signal TX to RX1

  // Jumper between TX1 and pin 27
  pinMode(27, INPUT); // Receives TX1
  pinMode(28, OUTPUT); // Output inverted signal TX1 for external RX

  piod_isr = PIOD_ISR;

  attachInterrupt(25, __NOP, CHANGE); // Receives trigger from external RX
  attachInterrupt(27, __NOP, CHANGE); // Receicves trigger from internal TX1
}


void loop() {

}

void PIOD_ISR (void) { // Callback attachInterrupt function
  PIOD->PIO_ISR;  // Read and clear status register
  // TODO : test if PD0 is high or low with PIOD_PIO_PDSR
  // Output an inverted signal on PD1 with PIO_SODR/PIO_CODR
// test if PD2 is high or low and output inverted signal on PD3
}

Note 1: This is for PIOD pins only

Note 2 : A "standard" attachInterrupt works correctly below 200 KHz, while a "modified" attachInterrupt is capable of catching much higher frequencies.