adafruit / Adafruit_TinyUSB_Arduino

Arduino library for TinyUSB
MIT License
470 stars 124 forks source link

When closing and reopening the serial interface, it is no longer opened. #154

Closed ERKAFREEWILD closed 2 years ago

ERKAFREEWILD commented 2 years ago

Operating System

Windows 10

IDE version

Arduino

Board

Raspbery Pi Pico

BSP version

Raspbery Pi Pico

TinyUSB Library version

latest

Sketch

OpenCloseSerial(bool open)
{
  //TinyUSBDevice.detach();
  digitalWrite(LED_BUILTIN, 1);
  delay(100);
  digitalWrite(LED_BUILTIN, 0);

  while (!usb_hid.ready());

  Serial.end();

  TinyUSBDevice.detach();
  delay(100);
  TinyUSBDevice.attach();

  if (open) Serial.begin(115200);
  usb_hid.begin();
  digitalWrite(LED_BUILTIN, buffer[0]);
}

What happened ?

I noticed that when closing the serial connection (Serial.End()) and then reopening it, it is no longer opened. Because I found errors in Adafruit_USBD_CDC.cpp, in the function "91: void Adafruit_USBD_CDC::end(void)" the variable "_instance" is not set to INVALID_INSTANCE; deferred, it remains when it is reopened. (line 74) set.

70:     void Adafruit_USBD_CDC::begin(uint32_t baud) {
71:     (void) baud;
72: 
73: // already called begin()
74:  if (isValid()) {
75:    returns;
76:  }
I changed the code so that the variable "_instance" is set to INVALID_INSTANCE, then it worked.
93:  void Adafruit_USBD_CDC::end(void) {
94:  // Reset configuration descriptor without Serial as CDC
95:  TinyUSBDevice.clearConfiguration();
96   _instance_count = 0;
97:  _instance = INVALID_INSTANCE;
98:   }

Thank you Erhard Kaserer

How to reproduce ?

.

Debug Log

No response

Screenshots

No response

hathach commented 2 years ago

thank you for reporting the issue, #156 will fix this shortly.