cdc.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "main.h"
  2. #include "cmsis_os.h"
  3. #include "cdc.h"
  4. #include "tusb.h"
  5. // echo to either Serial0 or Serial1
  6. // with Serial0 as all lower case, Serial1 as all upper case
  7. static void echo_serial_port(uint8_t itf, uint8_t buf[], uint32_t count)
  8. {
  9. uint8_t const case_diff = 'a' - 'A';
  10. for(uint32_t i=0; i<count; i++)
  11. {
  12. if (itf == 0)
  13. {
  14. // echo back 1st port as lower case
  15. if (isupper(buf[i])) buf[i] += case_diff;
  16. }
  17. else
  18. {
  19. // echo back 2nd port as upper case
  20. if (islower(buf[i])) buf[i] -= case_diff;
  21. }
  22. tud_cdc_n_write_char(itf, buf[i]);
  23. }
  24. tud_cdc_n_write_flush(itf);
  25. }
  26. //--------------------------------------------------------------------+
  27. // USB CDC
  28. //--------------------------------------------------------------------+
  29. void cdctask(void)
  30. {
  31. uint8_t itf;
  32. for (itf = 0; itf < CFG_TUD_CDC; itf++)
  33. {
  34. // connected() check for DTR bit
  35. // Most but not all terminal client set this when making connection
  36. // if ( tud_cdc_n_connected(itf) )
  37. {
  38. if ( tud_cdc_n_available(itf) )
  39. {
  40. uint8_t buf[64];
  41. uint32_t count = tud_cdc_n_read(itf, buf, sizeof(buf));
  42. // echo back to both serial ports
  43. echo_serial_port(0, buf, count);
  44. echo_serial_port(1, buf, count);
  45. }
  46. }
  47. }
  48. }