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

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