cdc_example.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-12-04 tfx2001 first version
  9. */
  10. #include <stdint.h>
  11. #include <rtthread.h>
  12. #include <tusb.h>
  13. static void cdc_example(void)
  14. {
  15. uint8_t buffer[32];
  16. char ch;
  17. if (tud_cdc_connected())
  18. {
  19. rt_memset(buffer, 0, 32);
  20. tud_cdc_write_str("please enter something: ");
  21. tud_cdc_write_flush();
  22. tud_cdc_read_flush();
  23. for (int i = 0; i < 32; i++)
  24. {
  25. while (!tud_cdc_available()) { rt_thread_mdelay(10); }
  26. // read char
  27. ch = tud_cdc_read_char();
  28. *(buffer + i) = ch;
  29. // echo
  30. tud_cdc_write_char(ch);
  31. tud_cdc_write_flush();
  32. // end with CR
  33. if (ch == '\r') { break; }
  34. }
  35. tud_cdc_write_str("\r\nwhat you enter: ");
  36. tud_cdc_write((const char *) buffer, 32);
  37. tud_cdc_write_str("\r\n");
  38. tud_cdc_write_flush();
  39. }
  40. else
  41. {
  42. rt_kprintf("please open port and make sure DTR=1\n");
  43. }
  44. }
  45. MSH_CMD_EXPORT(cdc_example, TinyUSB cdc example)