Skip to content

Commit 9ae2f6f

Browse files
committed
Avoid initializing USB serial twice
1 parent 920b57a commit 9ae2f6f

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

STM32F1/cores/maple/usb_serial.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,17 @@ static void ifaceSetupHook(unsigned, void*);
5555

5656
#define USB_TIMEOUT 50
5757

58+
bool USBSerial::_hasBegun = false;
5859
USBSerial::USBSerial(void) {
5960
#if !BOARD_HAVE_SERIALUSB
6061
ASSERT(0);
6162
#endif
6263
}
6364

6465
void USBSerial::begin(void) {
66+
if (_hasBegun)
67+
return;
68+
_hasBegun = true;
6569
#if BOARD_HAVE_SERIALUSB
6670
usb_cdcacm_enable(BOARD_USB_DISC_DEV, BOARD_USB_DISC_BIT);
6771
usb_cdcacm_set_hooks(USB_CDCACM_HOOK_RX, rxHook);
@@ -90,6 +94,7 @@ void USBSerial::end(void) {
9094
usb_cdcacm_disable(BOARD_USB_DISC_DEV, BOARD_USB_DISC_BIT);
9195
usb_cdcacm_remove_hooks(USB_CDCACM_HOOK_RX | USB_CDCACM_HOOK_IFACE_SETUP);
9296
#endif
97+
_hasBegun = false;
9398
}
9499

95100
size_t USBSerial::write(uint8 ch) {

STM32F1/cores/maple/usb_serial.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ class USBSerial : public Stream {
7171
uint8 getDTR();
7272
uint8 isConnected();
7373
uint8 pending();
74+
75+
protected:
76+
static bool _hasBegun;
7477
};
7578

7679
#ifdef SERIAL_USB

0 commit comments

Comments
 (0)