]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/ftdi/USBFTDILoopback/USBFTDILoopback.ino
lufa: usb-usb: Use LUFA startup instead of cusotom
[max/tmk_keyboard.git] / tmk_core / protocol / usb_hid / USB_Host_Shield_2.0 / examples / ftdi / USBFTDILoopback / USBFTDILoopback.ino
1 #include <cdcftdi.h>
2 #include <usbhub.h>
3
4 #include "pgmstrings.h"
5
6 // Satisfy the IDE, which needs to see the include statment in the ino too.
7 #ifdef dobogusinclude
8 #include <spi4teensy3.h>
9 #include <SPI.h>
10 #endif
11
12 class FTDIAsync : public FTDIAsyncOper
13 {
14 public:
15     uint8_t OnInit(FTDI *pftdi);
16 };
17
18 uint8_t FTDIAsync::OnInit(FTDI *pftdi)
19 {
20     uint8_t rcode = 0;
21
22     rcode = pftdi->SetBaudRate(115200);
23
24     if (rcode)
25     {
26         ErrorMessage<uint8_t>(PSTR("SetBaudRate"), rcode);
27         return rcode;
28     }
29     rcode = pftdi->SetFlowControl(FTDI_SIO_DISABLE_FLOW_CTRL);
30
31     if (rcode)
32         ErrorMessage<uint8_t>(PSTR("SetFlowControl"), rcode);
33
34     return rcode;
35 }
36
37 USB              Usb;
38 //USBHub         Hub(&Usb);
39 FTDIAsync        FtdiAsync;
40 FTDI             Ftdi(&Usb, &FtdiAsync);
41
42 uint32_t next_time;
43
44 void setup()
45 {
46   Serial.begin( 115200 );
47 #if !defined(__MIPSEL__)
48   while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
49 #endif
50   Serial.println("Start");
51
52   if (Usb.Init() == -1)
53       Serial.println("OSC did not start.");
54
55   delay( 200 );
56
57   next_time = millis() + 5000;
58 }
59
60 void loop()
61 {
62     Usb.Task();
63
64     if( Usb.getUsbTaskState() == USB_STATE_RUNNING )
65     {
66         uint8_t  rcode;
67         char strbuf[] = "DEADBEEF";
68         //char strbuf[] = "The quick brown fox jumps over the lazy dog";
69         //char strbuf[] = "This string contains 61 character to demonstrate FTDI buffers"; //add one symbol to it to see some garbage
70         Serial.print(".");
71
72         rcode = Ftdi.SndData(strlen(strbuf), (uint8_t*)strbuf);
73
74         if (rcode)
75             ErrorMessage<uint8_t>(PSTR("SndData"), rcode);
76
77         delay(50);
78
79         uint8_t  buf[64];
80
81         for (uint8_t i=0; i<64; i++)
82             buf[i] = 0;
83
84         uint16_t rcvd = 64;
85         rcode = Ftdi.RcvData(&rcvd, buf);
86
87         if (rcode && rcode != hrNAK)
88             ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
89
90         // The device reserves the first two bytes of data
91         //   to contain the current values of the modem and line status registers.
92         if (rcvd > 2)
93             Serial.print((char*)(buf+2));
94
95         delay(10);
96     }
97 }
98