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.0KB

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