Bridge USB Native port to Serial2

Hello all.
In a new project I need to connect a TTL device and my PC at a high rate communication. I thought the easiest way could be using the Native port of my Arduino Due.

So I need to reroute all the data traffic (at 20Mbps) between the Due Native port and Serial 2 port (TTL device).
I have implemeted all the data protocol, which is some HEX data, so I was planning to send every byte received from one port to the TX of the other port.

RX2 ------------------- Native Tx
Native Rx -------------------- TX2

1.- Is there a simple/effective way to achieve this?
2.- Is there a way to not tie up the CPU of the due? (via dma or similar)
3.- Is it better to buffer the communication?

Thank you.

I'll be curious to hear when speed you actually achieve?

My guess is it will fall short of 20 Mbit/sec. Due's native port can transmit fast, I believe in the range of 40 to 50 Mbit/sec (about 10% of the theoretical bandwidth of 480 Mbit/sec USB). But USB reception is slower, approx 1 Mbit/sec last time I measured. I'm not sure what baud rates are supported for Serial2. If do know transmit on Serial2 is polled with only the UART's 2 byte buffer, so you'll probably need to work around that somehow for the USB->Serial2 direction.

Good luck. I hope you'll follow up with details on how it works?

Thank you for your answer Paul.

I have used Native port before with baudrates at 40Mbit/s with no problems.
What do you mean by USB reception? Is it using the programming port?

What do you mean Serial2 is polled with a byte buffer?

Actually, in my design the TTL device will send data continuosly and the PC will send some short frames of three bytes, for configuration in asynchronous mode (after a user request).

Do you know where I can find a script doing something similar to this?
Is it possible a dma mode?

Thank you again

PakARD:
I have used Native port before with baudrates at 40Mbit/s with no problems.
What do you mean by USB reception? Is it using the programming port?

Data can flow both ways on the native port (and any of the serial ports). The speed is not the same in both directions. SerialUSB.write() is much faster than SerialUSB.read().

What do you mean Serial2 is polled with a byte buffer?

If you use Serial2.print("Hello World") on Arduino Uno, Leonardo, Mega, etc, all 11 bytes are quickly written to a buffer. Your program gets to keep running. The serial2 interrupt then moves the data from the buffer to the serial line while your program can to do other stuff.

On Due, only 2 bytes of transmit buffering are used. Serial.print() will not return until 9 bits have actually been transmitted. Your program gets to run again when those last 2 bytes are still buffered.

Actually, in my design the TTL device will send data continuosly and the PC will send some short frames of three bytes, for configuration in asynchronous mode (after a user request).

Do you know where I can find a script doing something similar to this?
Is it possible a dma mode?

I can't get more involved in your project (and really, you initially didn't even give the detail of how much data you were planning in each direction).

Maybe these details will help you plan your code? If not, I just can't spend more time.

Good luck. I hope it works out well. Maybe you'll post a followup after you build this?

OK. I am planning to get long 1038 bytes frames from Serial2 (sending them to the PC via Native port), and send from the PC short 2byte frames, to Serial2 TX.

However, I have found some issues.
First the buffer data input for Serial2 uart is 64 bytes, so I need to read as fast as I can in roder to not get it full and lose data.

On the other hand, tried to read some bytes (6 bytes) at the same time to save accessing time. But I think, this way the processor is not able to get the job done in time.

Here is my code:

int led = 13;
boolean estado_led=HIGH;

void setup() {
  
    // initialize the digital pin as an output.
  pinMode(led, OUTPUT); 
  
  
  // initialize both serial ports:  
  Serial.begin(115200);


 Serial.println(" ARDUINO ON1!! #");
  Serial2.begin(3000000);
  
  SerialUSB.begin(3000000);//10 Megas
 // while(!SerialUSB);
  
  Serial.println(" ARDUINO ON!2! #");

  Serial.println(" ARDUINO ON2!! #");
}

#define buffer_mensaje 1038 
char buf_msx[buffer_mensaje];   // 4 buffers of 256 readings
char buf_Rx[buffer_mensaje];   // 4 buffers of 256 readings




char lee_serie_of[2];
int breakpoint_1=0;
int breakpoint_2=0;
int cnt_loop=0;
int cnt_loop2=0;
int cnt_loop_total=0;
int cnt_rx2=0;
int cnt_tx2=0;
int cnt_s2rx2=0;




void loop() {
  cnt_loop++;
  
  if (cnt_loop>=100000){
  estado_led=!estado_led;
  cnt_loop=0;
    digitalWrite(led, estado_led);   // turn the LED on (HIGH is the voltage level)
   
  }


  
int incomingByte = 0;   // for incoming serial data

 
 if (SerialUSB.available() > 1) 
 {
      incomingByte = SerialUSB.readBytes(lee_serie_of, 2);
      Serial2.write(lee_serie_of[0]); 
      Serial2.write(lee_serie_of[1]); 
      cnt_tx2++;
    
      para_en_TX=true;

 }
   
   
  // read from port 1, send to port 0:
  /*while (Serial2.available()>0) {
    int inByte = Serial2.read();
     SerialUSB.write(inByte); 
  }/**/
//  if (Serial2.available()>1037) {

    if (Serial2.available()>6) 
    {
//   int inByte = Serial2.readBytes(buf_msx, 1038);   
//    SerialUSB.write((uint8_t *)buf_msx,buffer_mensaje);

   int inByte = Serial2.readBytes(&buf_Rx[cnt_s2rx2*6], 6);   
   cnt_s2rx2++;
   cnt_rx2++;

  }  
  
  
  if (cnt_s2rx2>0)//1038/6
  {
   
     SerialUSB.write((uint8_t *)buf_Rx,cnt_s2rx2*6);
     cnt_s2rx2=0;
  }
  

}