cdc.c 1001 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include <stdint.h>
  2. #include <rtthread.h>
  3. #include <tusb.h>
  4. static void cdc_example(void)
  5. {
  6. uint8_t buffer[32];
  7. char ch;
  8. if (tud_cdc_connected())
  9. {
  10. rt_memset(buffer, 0, 32);
  11. tud_cdc_write_str("please enter something: ");
  12. tud_cdc_write_flush();
  13. tud_cdc_read_flush();
  14. for (int i = 0; i < 32; i++)
  15. {
  16. while (!tud_cdc_available()) { rt_thread_mdelay(10); }
  17. // read char
  18. ch = tud_cdc_read_char();
  19. *(buffer + i) = ch;
  20. // echo
  21. tud_cdc_write_char(ch);
  22. tud_cdc_write_flush();
  23. // end with CR
  24. if (ch == '\r') { break; }
  25. }
  26. tud_cdc_write_str("\r\nwhat you enter: ");
  27. tud_cdc_write((const char *) buffer, 32);
  28. tud_cdc_write_str("\r\n");
  29. tud_cdc_write_flush();
  30. }
  31. else
  32. {
  33. rt_kprintf("please open port and make sure DTR=1\n");
  34. }
  35. }
  36. MSH_CMD_EXPORT(cdc_example, TinyUSB cdc example)