설명 없음
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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. void usb_cdc_write(const char *buf, uint32_t count) {
  34. #ifndef DISABLE_CDC_DTR_CHECK
  35. if (!tud_cdc_connected()) {
  36. return;
  37. }
  38. #endif // DISABLE_CDC_DTR_CHECK
  39. // implemented similar to Pico SDK stdio usb
  40. uint32_t len = 0;
  41. while (len < count) {
  42. uint32_t n = count - len;
  43. uint32_t available = tud_cdc_write_available();
  44. // only write as much as possible
  45. if (n > available) {
  46. n = available;
  47. }
  48. len += tud_cdc_write(buf + len, n);
  49. // run tud_task to actually move stuff from FIFO
  50. tud_task();
  51. tud_cdc_write_flush();
  52. }
  53. }
  54. void cdc_task(void) {
  55. const uint32_t cdc_buf_len = 64;
  56. if (tud_cdc_available()) {
  57. char buf[cdc_buf_len + 1];
  58. uint32_t count = tud_cdc_read(buf, cdc_buf_len);
  59. if ((count >= 1) && (buf[0] == 0x18)) {
  60. // ASCII 0x18 = CAN (cancel)
  61. debug("switching to bootloader");
  62. reset_to_bootloader();
  63. } else {
  64. // echo back
  65. usb_cdc_write(buf, count);
  66. cnsl_handle_input(buf, count);
  67. }
  68. }
  69. }
  70. // invoked when cdc when line state changed e.g connected/disconnected
  71. void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
  72. (void) itf;
  73. (void) rts;
  74. static bool last_dtr = false;
  75. if (dtr && !last_dtr) {
  76. // clear left-over console input
  77. cnsl_init();
  78. // show past history
  79. log_dump_to_usb();
  80. debug("terminal connected");
  81. } else if (!dtr && last_dtr) {
  82. debug("terminal disconnected");
  83. }
  84. last_dtr = dtr;
  85. }
  86. // invoked when CDC interface received data from host
  87. void tud_cdc_rx_cb(uint8_t itf) {
  88. (void) itf;
  89. cdc_task();
  90. }