summaryrefslogtreecommitdiff
path: root/lib/usbhost/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback
diff options
context:
space:
mode:
authorRyan <fauxpark@gmail.com>2021-08-18 18:20:25 +1000
committerGitHub <noreply@github.com>2021-08-18 18:20:25 +1000
commitb16091659cc9a724a8800f77e631643b4ab089ad (patch)
treee44933472c6d100bd4fc5d8a693d9d21e3c32f6f /lib/usbhost/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback
parentcf5e40c25139ff64ff246f1c6280e983ef75551c (diff)
Move USB Host Shield and Arduino core to `lib/` (#13973)
Diffstat (limited to 'lib/usbhost/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback')
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback/USBFTDILoopback.ino98
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback/pgmstrings.h52
2 files changed, 150 insertions, 0 deletions
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback/USBFTDILoopback.ino b/lib/usbhost/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback/USBFTDILoopback.ino
new file mode 100644
index 0000000000..5be7adc2f3
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback/USBFTDILoopback.ino
@@ -0,0 +1,98 @@
+#include <cdcftdi.h>
+#include <usbhub.h>
+
+#include "pgmstrings.h"
+
+// Satisfy the IDE, which needs to see the include statment in the ino too.
+#ifdef dobogusinclude
+#include <spi4teensy3.h>
+#include <SPI.h>
+#endif
+
+class FTDIAsync : public FTDIAsyncOper
+{
+public:
+ uint8_t OnInit(FTDI *pftdi);
+};
+
+uint8_t FTDIAsync::OnInit(FTDI *pftdi)
+{
+ uint8_t rcode = 0;
+
+ rcode = pftdi->SetBaudRate(115200);
+
+ if (rcode)
+ {
+ ErrorMessage<uint8_t>(PSTR("SetBaudRate"), rcode);
+ return rcode;
+ }
+ rcode = pftdi->SetFlowControl(FTDI_SIO_DISABLE_FLOW_CTRL);
+
+ if (rcode)
+ ErrorMessage<uint8_t>(PSTR("SetFlowControl"), rcode);
+
+ return rcode;
+}
+
+USB Usb;
+//USBHub Hub(&Usb);
+FTDIAsync FtdiAsync;
+FTDI Ftdi(&Usb, &FtdiAsync);
+
+uint32_t next_time;
+
+void setup()
+{
+ Serial.begin( 115200 );
+#if !defined(__MIPSEL__)
+ while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
+#endif
+ Serial.println("Start");
+
+ if (Usb.Init() == -1)
+ Serial.println("OSC did not start.");
+
+ delay( 200 );
+
+ next_time = millis() + 5000;
+}
+
+void loop()
+{
+ Usb.Task();
+
+ if( Usb.getUsbTaskState() == USB_STATE_RUNNING )
+ {
+ uint8_t rcode;
+ char strbuf[] = "DEADBEEF";
+ //char strbuf[] = "The quick brown fox jumps over the lazy dog";
+ //char strbuf[] = "This string contains 61 character to demonstrate FTDI buffers"; //add one symbol to it to see some garbage
+ Serial.print(".");
+
+ rcode = Ftdi.SndData(strlen(strbuf), (uint8_t*)strbuf);
+
+ if (rcode)
+ ErrorMessage<uint8_t>(PSTR("SndData"), rcode);
+
+ delay(50);
+
+ uint8_t buf[64];
+
+ for (uint8_t i=0; i<64; i++)
+ buf[i] = 0;
+
+ uint16_t rcvd = 64;
+ rcode = Ftdi.RcvData(&rcvd, buf);
+
+ if (rcode && rcode != hrNAK)
+ ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
+
+ // The device reserves the first two bytes of data
+ // to contain the current values of the modem and line status registers.
+ if (rcvd > 2)
+ Serial.print((char*)(buf+2));
+
+ delay(10);
+ }
+}
+
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback/pgmstrings.h b/lib/usbhost/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback/pgmstrings.h
new file mode 100644
index 0000000000..bdb0077ecc
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback/pgmstrings.h
@@ -0,0 +1,52 @@
+#if !defined(__PGMSTRINGS_H__)
+#define __PGMSTRINGS_H__
+
+#define LOBYTE(x) ((char*)(&(x)))[0]
+#define HIBYTE(x) ((char*)(&(x)))[1]
+#define BUFSIZE 256 //buffer size
+
+
+/* Print strings in Program Memory */
+const char Gen_Error_str[] PROGMEM = "\r\nRequest error. Error code:\t";
+const char Dev_Header_str[] PROGMEM ="\r\nDevice descriptor: ";
+const char Dev_Length_str[] PROGMEM ="\r\nDescriptor Length:\t";
+const char Dev_Type_str[] PROGMEM ="\r\nDescriptor type:\t";
+const char Dev_Version_str[] PROGMEM ="\r\nUSB version:\t\t";
+const char Dev_Class_str[] PROGMEM ="\r\nDevice class:\t\t";
+const char Dev_Subclass_str[] PROGMEM ="\r\nDevice Subclass:\t";
+const char Dev_Protocol_str[] PROGMEM ="\r\nDevice Protocol:\t";
+const char Dev_Pktsize_str[] PROGMEM ="\r\nMax.packet size:\t";
+const char Dev_Vendor_str[] PROGMEM ="\r\nVendor ID:\t\t";
+const char Dev_Product_str[] PROGMEM ="\r\nProduct ID:\t\t";
+const char Dev_Revision_str[] PROGMEM ="\r\nRevision ID:\t\t";
+const char Dev_Mfg_str[] PROGMEM ="\r\nMfg.string index:\t";
+const char Dev_Prod_str[] PROGMEM ="\r\nProd.string index:\t";
+const char Dev_Serial_str[] PROGMEM ="\r\nSerial number index:\t";
+const char Dev_Nconf_str[] PROGMEM ="\r\nNumber of conf.:\t";
+const char Conf_Trunc_str[] PROGMEM ="Total length truncated to 256 bytes";
+const char Conf_Header_str[] PROGMEM ="\r\nConfiguration descriptor:";
+const char Conf_Totlen_str[] PROGMEM ="\r\nTotal length:\t\t";
+const char Conf_Nint_str[] PROGMEM ="\r\nNum.intf:\t\t";
+const char Conf_Value_str[] PROGMEM ="\r\nConf.value:\t\t";
+const char Conf_String_str[] PROGMEM ="\r\nConf.string:\t\t";
+const char Conf_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t";
+const char Conf_Pwr_str[] PROGMEM ="\r\nMax.pwr:\t\t";
+const char Int_Header_str[] PROGMEM ="\r\n\r\nInterface descriptor:";
+const char Int_Number_str[] PROGMEM ="\r\nIntf.number:\t\t";
+const char Int_Alt_str[] PROGMEM ="\r\nAlt.:\t\t\t";
+const char Int_Endpoints_str[] PROGMEM ="\r\nEndpoints:\t\t";
+const char Int_Class_str[] PROGMEM ="\r\nIntf. Class:\t\t";
+const char Int_Subclass_str[] PROGMEM ="\r\nIntf. Subclass:\t\t";
+const char Int_Protocol_str[] PROGMEM ="\r\nIntf. Protocol:\t\t";
+const char Int_String_str[] PROGMEM ="\r\nIntf.string:\t\t";
+const char End_Header_str[] PROGMEM ="\r\n\r\nEndpoint descriptor:";
+const char End_Address_str[] PROGMEM ="\r\nEndpoint address:\t";
+const char End_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t";
+const char End_Pktsize_str[] PROGMEM ="\r\nMax.pkt size:\t\t";
+const char End_Interval_str[] PROGMEM ="\r\nPolling interval:\t";
+const char Unk_Header_str[] PROGMEM = "\r\nUnknown descriptor:";
+const char Unk_Length_str[] PROGMEM ="\r\nLength:\t\t";
+const char Unk_Type_str[] PROGMEM ="\r\nType:\t\t";
+const char Unk_Contents_str[] PROGMEM ="\r\nContents:\t";
+
+#endif // __PGMSTRINGS_H__ \ No newline at end of file