FTDI example for the Arduino Due

Hi, I wanted to control an FTDI-Device by an Arduino Due. I found some post of people with similar need, but no working example.
The Due hat a native USB-port, which can be used as an USBHost. The examples with Keyboard and Mouse work fine, but are not easily extended to other devices. The problem is that USB-Devices have different registers to initialize and control communication. There is the FTDI example for the host-shield by Circuits@Home, but I did not get it to work for the DUE. The third example with the DUE-Host library is the ADK-example, which initialized the pipes on an attached device. After changing VID and PID I found the details of setting the BaudRate and FlowControl in the FTDI example for the shield.
Together I managed to control the FTDI-Device (VID 0403, PID 6001).
The header of a file ftd.h and the ftd.cpp file, which should be placed in ...\Documents\Arduino\libraries\USBHost\src, are in the attachment. (Too long to be placed here)

The example code, which reads out the name of the attached controller, is in the code section.

#include "variant.h"
#include <stdio.h>
#include <Usb.h>
#include <ftd.h>

/*
  FTDI Terminal Test

  This demonstrates USB Host connectivity between an
  FTDI-Device and an Arduino Due.

  The FDTI for the Arduino Due is a work in progress.
  The program is inspired and based on the Arduino ADK visit
  http://labs.arduino.cc/ADK/Index
  and the cdcftdi by Circuits@Home visit
  https://www.circuitsathome.com/about/

  created April 2017
  by Edmund

*/

// Accessory descriptor. For future.
char applicationName[] = "Arduino_Terminal"; // the app on your phone
char accessoryName[] = "Arduino Due"; // your Arduino board
char companyName[] = "Arduino SA";

// Make up anything you want for these
char versionNumber[] = "1.0";
char serialNumber[] = "1";
char url[] = "http://labs.arduino.cc/uploads/ADK/ArduinoTerminal/ThibaultTerminal_ICS_0001.apk";
boolean First = true;
USB_DEVICE_DESCRIPTOR buf;
#define RCVSIZE 128

USBHost Usb;
FTDI ftdi(&Usb, companyName, applicationName, accessoryName, versionNumber, url, serialNumber);

void setup() {
  Serial.begin(115000);
  cpu_irq_enable();
  printf("\r\nFTDI demo start\r\n");
  delay(200);
}

void loop() {
  uint8_t Rbuf[RCVSIZE];
  uint32_t nbread = 0;
  char helloworld[] = "*idn?\r";
  byte rcode;

  Usb.Task();
  delay(100);
  /* Set BaudRate and FlowControl */
  if ( Usb.getUsbTaskState() == USB_STATE_RUNNING ) {
    if (ftdi.isReady()) {
      if (First) {
        ftdi.SetBaudRate(115200);
        ftdi.SetFlowControl(FTDI_SIO_DISABLE_FLOW_CTRL, 0, 0);
        First = false;
      }
      else  {
        /* Write hello string to FTDI */
        rcode = ftdi.write(strlen(helloworld), (uint8_t *)helloworld);
        printf("ftdi write: %i, '%s'\r\n", rcode, helloworld);
        delay(1000);

        /* Read data from FTDI and print  */
        rcode = ftdi.read(&nbread, RCVSIZE, Rbuf);
        printf("ftdi read:  %i, Characters: %i\r\n", rcode, nbread);
        if (nbread > 0) {
          printf("RCV: '");
          for (uint32_t i = 2; i < nbread; ++i) printf("%c", (char)Rbuf[i]);  // First two byte are status lines
          printf("'\r\n");
        }
      }
    }
  }
}

Not all functionality of the USB-Port is integrated, but it worked for me.

Unfortunately, I can't control other USB to serial adaptors with this software.
I would like to communicate with other Arduino's, but each has its own USB-adaptor and the documentation for the few instructions needed to set up the communication is rather bad. Moreover, part of the communication with the Prolific USB to RS2323 adaptor (VID_067B PID_2303&REV_0300) (BaudRate and control-lines) works, but I get only junk back, so something seems to be missing.
Maybe, some others can contribute the parts needed for other USB-devices and adaptors or find a source for the commands needed.

Best regards

Edmund

ftd.h (6.49 KB)

ftd.cpp (12.5 KB)

yxmnas:
but I get only junk back

I spent two weeks trying to work something out. It seems that all 4 USB to serial adaptors I have work only in one mode: 1 start bit, 6 data bits and a parity bit (haven't checked whether it's odd or even). That causes junk in return for me. So, I built the adaptor from an FTDI32 chip and MAX232 level converter.