No Description
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.

usb_cdc.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2019 Ha Thach (tinyusb.org)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. #include "bsp/board.h"
  26. #include "tusb.h"
  27. #include "config.h"
  28. #include "console.h"
  29. #include "log.h"
  30. #include "util.h"
  31. #include "usb_descriptors.h"
  32. #include "usb_cdc.h"
  33. static bool reroute_cdc_debug = false;
  34. void usb_cdc_write(const char *buf, uint32_t count) {
  35. #ifndef DISABLE_CDC_DTR_CHECK
  36. if (!tud_cdc_connected()) {
  37. return;
  38. }
  39. #endif // DISABLE_CDC_DTR_CHECK
  40. // implemented similar to Pico SDK stdio usb
  41. uint32_t len = 0;
  42. while (len < count) {
  43. uint32_t n = count - len;
  44. uint32_t available = tud_cdc_write_available();
  45. // only write as much as possible
  46. if (n > available) {
  47. n = available;
  48. }
  49. len += tud_cdc_write(buf + len, n);
  50. // run tud_task to actually move stuff from FIFO
  51. tud_task();
  52. tud_cdc_write_flush();
  53. }
  54. }
  55. void usb_cdc_set_reroute(bool reroute) {
  56. reroute_cdc_debug = reroute;
  57. }
  58. void cdc_task(void) {
  59. const uint32_t cdc_buf_len = 64;
  60. if (tud_cdc_available()) {
  61. char buf[cdc_buf_len + 1];
  62. uint32_t count = tud_cdc_read(buf, cdc_buf_len);
  63. if ((count >= 1) && (buf[0] == 0x18)) {
  64. // ASCII 0x18 = CAN (cancel)
  65. debug("switching to bootloader");
  66. reset_to_bootloader();
  67. } else if (reroute_cdc_debug) {
  68. debug_handle_input(buf, count);
  69. } else {
  70. // echo back
  71. usb_cdc_write(buf, count);
  72. cnsl_handle_input(buf, count);
  73. }
  74. }
  75. }
  76. // invoked when cdc when line state changed e.g connected/disconnected
  77. void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
  78. (void) itf;
  79. (void) rts;
  80. static bool last_dtr = false;
  81. if (dtr && !last_dtr) {
  82. // clear left-over console input
  83. cnsl_init();
  84. // show past history
  85. log_dump_to_usb();
  86. debug("terminal connected");
  87. } else if (!dtr && last_dtr) {
  88. debug("terminal disconnected");
  89. }
  90. last_dtr = dtr;
  91. }
  92. // invoked when CDC interface received data from host
  93. void tud_cdc_rx_cb(uint8_t itf) {
  94. (void) itf;
  95. cdc_task();
  96. }