How To Use The Native USB Of The Arduino Due For High-Speed Communication
It seems to be an obvious statement, but there are many applications for the Arduino Due that require high-speed communication in a range that goes beyond regular UART baud rates, i.e. several Mbits/sec. Naturally, USB comes to mind, but surprisingly there are some obstacles when it comes to accessing the Arduino Due's USB ports. The good news is that these obstacles are entirely based on insufficient documentation, or more precisely hard-to-find information.
First of all:
The Arduino Due programming port is useless for high-speed serial communication
The programming port is limited to a baud rate of 115,200 baud. I haven't found out in detail where the limitation comes from (I am not a hardware specialist), but my suspicion is that it is a combination of hardware (UART-to-USB converter) and software (OS shortcomings) restrictions. There is a little trick to push the baud rate to 230,400. For more information on the topic refer to the following posts:
- Arduino Due: Using the Serial Monitor At 230400 Baud...
- ARM Cortex Processors - UART Programming Problem at Baud Rates Higher Than 115200...
In my specific case, I am developing a CAN Bus (Controller Area Network) gateway application that converts CAN data for transmission per USB. The effectiveness of such a gateway depends primarily on the serial communication speed, which should be at least as fast as the maximum CAN baud rate of 1 MBit/sec, if not exceed it. There are ways to filter CAN messages, i.e. allowing only certain CAN data frames to be transmitted per serial port, thus lowering the burden on the serial port. Nevertheless, this can only be a compromise, and it doesn't address the need for reliability during data bursts (short periods of 100% busload).
Use SerialUSB for high-speed serial communication
The SerialUSB class works only on the Arduino Due, and it allows access to the Native USB port (I believe it's USB 3.0 with speeds up to 4.8 Gbps). Unfortunately, detailed information on SerialUSB is hard to come by. The reason is that it is used in the very same way as the Arduino Serial function. The Arduino people kind of missed adding the SerialUSB reference to their documentation (It is mentioned somewhere in passing, but don't waste your time trying to find it). After all, the Arduino Due was listed temporarily as "not supported anymore," and then, miraculously, was revived as a product (for very good reasons, I may add).
However, when using the Arduino Due's Native USB port, use the following Setup() code to initialize the port:
SerialUSB.begin(2000000); // Initialize Native USB port
while(!SerialUSB); // Wait until connection is established
The baud rate was chosen randomly, since it doesn't apply to the USB port. Nevertheless, the compiler will report an error if you don't pass it. Otherwise, you can use all Serial functions per SerialUSB:
Please feel free to contact me through the Contact Us page on this website in case you have questions or comments.