You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

USBFTDILoopback.ino 2.2KB

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