@@ -54,6 +54,9 @@ static void ifaceSetupHook(unsigned, void*);
5454 */
5555
5656#define USB_TIMEOUT 50
57+ #if BOARD_HAVE_SERIALUSB
58+ bool USBSerial::_hasBegun = false ;
59+ #endif
5760
5861USBSerial::USBSerial (void ) {
5962#if !BOARD_HAVE_SERIALUSB
@@ -62,7 +65,12 @@ USBSerial::USBSerial(void) {
6265}
6366
6467void USBSerial::begin (void ) {
68+
6569#if BOARD_HAVE_SERIALUSB
70+ if (_hasBegun)
71+ return ;
72+ _hasBegun = true ;
73+
6674 usb_cdcacm_enable (BOARD_USB_DISC_DEV, BOARD_USB_DISC_BIT);
6775 usb_cdcacm_set_hooks (USB_CDCACM_HOOK_RX, rxHook);
6876 usb_cdcacm_set_hooks (USB_CDCACM_HOOK_IFACE_SETUP, ifaceSetupHook);
@@ -75,6 +83,7 @@ void USBSerial::begin(unsigned long ignoreBaud)
7583volatile unsigned long removeCompilerWarningsIgnoreBaud=ignoreBaud;
7684
7785 ignoreBaud=removeCompilerWarningsIgnoreBaud;
86+ begin ();
7887}
7988void USBSerial::begin (unsigned long ignoreBaud, uint8_t ignore)
8089{
@@ -83,13 +92,16 @@ volatile uint8_t removeCompilerWarningsIgnore=ignore;
8392
8493 ignoreBaud=removeCompilerWarningsIgnoreBaud;
8594 ignore=removeCompilerWarningsIgnore;
95+ begin ();
8696}
8797
8898void USBSerial::end (void ) {
8999#if BOARD_HAVE_SERIALUSB
90100 usb_cdcacm_disable (BOARD_USB_DISC_DEV, BOARD_USB_DISC_BIT);
91101 usb_cdcacm_remove_hooks (USB_CDCACM_HOOK_RX | USB_CDCACM_HOOK_IFACE_SETUP);
102+ _hasBegun = false ;
92103#endif
104+
93105}
94106
95107size_t USBSerial::write (uint8 ch) {
0 commit comments