cdc.ino 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "cdchost.h"
  2. #define DEVICE_ADDRESS 2
  3. USBHostCDC port(DEVICE_ADDRESS);
  4. void usbh_cdc_device_ready()
  5. {
  6. port.setControlLine(1, 1);
  7. port.setLineCoding(115200, 0, 0, 5);
  8. port.inpipe->inData();
  9. }
  10. void ctrl_pipe_cb(ext_pipe_event_msg_t event, usb_irp_t *irp)
  11. {
  12. Serial.printf("CTRL EVENT: 0x%x\n", event);
  13. }
  14. void port_cb(port_event_msg_t msg, USBHostPort *port)
  15. {
  16. Serial.printf("PORT EVENT: 0x%x\n", msg.port_event);
  17. }
  18. void cdc_datain_cb(ext_pipe_event_msg_t event, usb_irp_t *irp)
  19. {
  20. for (size_t i = 0; i < irp->actual_num_bytes; i++)
  21. {
  22. Serial.printf("%c", irp->data_buffer[i]);
  23. }
  24. }
  25. // this is optional callback, it is just a status check and/or echo from low level stack
  26. void cdc_dataout_cb(ext_pipe_event_msg_t event, usb_irp_t *irp)
  27. {
  28. Serial.printf("OUT EVENT: 0x%x, buffer_len: %d, sent: %d\n", event, irp->num_bytes, irp->actual_num_bytes);
  29. Serial.print("DATA: ");
  30. for (size_t i = 0; i < irp->actual_num_bytes; i++)
  31. {
  32. Serial.printf("%c", irp->data_buffer[i]);
  33. }
  34. Serial.println();
  35. }
  36. void setup()
  37. {
  38. Serial.begin(115200);
  39. port.onPortEvent(port_cb);
  40. port.onControlEvent(ctrl_pipe_cb);
  41. port.onDataIn(cdc_datain_cb);
  42. port.onDataOut(cdc_dataout_cb);
  43. port.init();
  44. }
  45. void loop()
  46. {
  47. delay(10);
  48. while (Serial.available())
  49. {
  50. test_strings();
  51. size_t l = Serial.available();
  52. uint8_t b[l];
  53. l = Serial.read(b, l);
  54. port.sendData(b, l);
  55. }
  56. }